2020-01-10 10:38:21 +08:00
|
|
|
/*
|
2023-11-28 14:20:11 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2020-01-10 10:38:21 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2011-09-15 Bernard first version
|
|
|
|
* 2019-07-28 zdzn add smp support
|
2023-11-28 14:20:11 +08:00
|
|
|
* 2023-02-21 GuEe-GUI mov cpu ofw init to setup
|
2024-05-16 15:45:12 +08:00
|
|
|
* 2024-04-29 Shell Add generic ticket spinlock using C11 atomic
|
2020-01-10 10:38:21 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
2023-11-28 14:20:11 +08:00
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <cpu.h>
|
2022-12-20 17:49:37 +08:00
|
|
|
|
|
|
|
#define DBG_TAG "libcpu.aarch64.cpu"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2022-01-07 13:49:06 +08:00
|
|
|
#ifdef RT_USING_SMP
|
2022-12-20 17:49:37 +08:00
|
|
|
|
|
|
|
#define REPORT_ERR(retval) LOG_E("got error code %d in %s(), %s:%d", (retval), __func__, __FILE__, __LINE__)
|
|
|
|
#define CHECK_RETVAL(retval) if (retval) {REPORT_ERR(retval);}
|
2023-11-28 14:20:11 +08:00
|
|
|
#define cpuid_to_hwid(cpuid) \
|
|
|
|
((((cpuid) >= 0) && ((cpuid) < RT_CPUS_NR)) ? rt_cpu_mpidr_early[cpuid] : ID_ERROR)
|
|
|
|
#define set_hwid(cpuid, hwid) \
|
|
|
|
((((cpuid) >= 0) && ((cpuid) < RT_CPUS_NR)) ? (rt_cpu_mpidr_early[cpuid] = (hwid)) : ID_ERROR)
|
|
|
|
#define get_cpu_node(cpuid) \
|
|
|
|
((((cpuid) >= 0) && ((cpuid) < RT_CPUS_NR)) ? _cpu_node[cpuid] : NULL)
|
|
|
|
#define set_cpu_node(cpuid, node) \
|
|
|
|
((((cpuid) >= 0) && ((cpuid) < RT_CPUS_NR)) ? (_cpu_node[cpuid] = node) : NULL)
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-11-28 14:20:11 +08:00
|
|
|
typedef rt_hw_spinlock_t arch_spinlock_t;
|
2022-12-20 17:49:37 +08:00
|
|
|
struct cpu_ops_t *cpu_ops_tbl[RT_CPUS_NR];
|
|
|
|
|
|
|
|
#ifdef RT_USING_SMART
|
|
|
|
// _id_to_mpidr is a table translate logical id to mpid, which is a 64-bit value
|
|
|
|
rt_uint64_t rt_cpu_mpidr_early[RT_CPUS_NR] rt_weak = {[0 ... RT_CPUS_NR - 1] = ID_ERROR};
|
|
|
|
#else
|
2022-01-07 13:49:06 +08:00
|
|
|
/* The more common mpidr_el1 table, redefine it in BSP if it is in other cases */
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_weak rt_uint64_t rt_cpu_mpidr_early[] =
|
2022-01-07 13:49:06 +08:00
|
|
|
{
|
|
|
|
[0] = 0x80000000,
|
|
|
|
[1] = 0x80000001,
|
|
|
|
[2] = 0x80000002,
|
|
|
|
[3] = 0x80000003,
|
|
|
|
[4] = 0x80000004,
|
|
|
|
[5] = 0x80000005,
|
|
|
|
[6] = 0x80000006,
|
|
|
|
[7] = 0x80000007,
|
|
|
|
[RT_CPUS_NR] = 0
|
|
|
|
};
|
2022-12-20 17:49:37 +08:00
|
|
|
#endif /* RT_USING_SMART */
|
2020-01-10 10:38:21 +08:00
|
|
|
|
2024-05-16 15:45:12 +08:00
|
|
|
/* in support of C11 atomic */
|
|
|
|
#if __STDC_VERSION__ >= 201112L
|
|
|
|
#include <stdatomic.h>
|
|
|
|
|
|
|
|
union _spinlock
|
2020-01-10 10:38:21 +08:00
|
|
|
{
|
2024-05-16 15:45:12 +08:00
|
|
|
_Atomic(rt_uint32_t) _value;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
_Atomic(rt_uint16_t) owner;
|
|
|
|
_Atomic(rt_uint16_t) next;
|
|
|
|
} ticket;
|
|
|
|
};
|
|
|
|
|
|
|
|
void rt_hw_spin_lock_init(rt_hw_spinlock_t *_lock)
|
|
|
|
{
|
|
|
|
union _spinlock *lock = (void *)_lock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* just a dummy note that this is an atomic operation, though it alway is
|
|
|
|
* even without usage of atomic API in arm64
|
|
|
|
*/
|
|
|
|
atomic_store_explicit(&lock->_value, 0, memory_order_relaxed);
|
2020-01-10 10:38:21 +08:00
|
|
|
}
|
|
|
|
|
2024-05-16 15:45:12 +08:00
|
|
|
rt_bool_t rt_hw_spin_trylock(rt_hw_spinlock_t *_lock)
|
2020-01-10 10:38:21 +08:00
|
|
|
{
|
2024-05-16 15:45:12 +08:00
|
|
|
rt_bool_t rc;
|
|
|
|
rt_uint32_t readonce;
|
|
|
|
union _spinlock temp;
|
|
|
|
union _spinlock *lock = (void *)_lock;
|
|
|
|
|
|
|
|
readonce = atomic_load_explicit(&lock->_value, memory_order_acquire);
|
|
|
|
temp._value = readonce;
|
|
|
|
|
|
|
|
if (temp.ticket.owner != temp.ticket.next)
|
|
|
|
{
|
|
|
|
rc = RT_FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp.ticket.next += 1;
|
|
|
|
rc = atomic_compare_exchange_strong_explicit(
|
|
|
|
&lock->_value, &readonce, temp._value,
|
|
|
|
memory_order_acquire, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
return rc;
|
2020-01-10 10:38:21 +08:00
|
|
|
}
|
|
|
|
|
2024-05-16 15:45:12 +08:00
|
|
|
rt_inline rt_base_t _load_acq_exclusive(_Atomic(rt_uint16_t) *halfword)
|
2020-01-10 10:38:21 +08:00
|
|
|
{
|
2024-05-16 15:45:12 +08:00
|
|
|
rt_uint32_t old;
|
|
|
|
__asm__ volatile("ldaxrh %w0, [%1]"
|
|
|
|
: "=&r"(old)
|
|
|
|
: "r"(halfword)
|
|
|
|
: "memory");
|
|
|
|
return old;
|
2023-10-25 20:31:25 +08:00
|
|
|
}
|
|
|
|
|
2024-05-16 15:45:12 +08:00
|
|
|
rt_inline void _send_event_local(void)
|
2023-10-25 20:31:25 +08:00
|
|
|
{
|
2024-05-16 15:45:12 +08:00
|
|
|
__asm__ volatile("sevl");
|
2023-10-25 20:31:25 +08:00
|
|
|
}
|
|
|
|
|
2024-05-16 15:45:12 +08:00
|
|
|
rt_inline void _wait_for_event(void)
|
2023-10-25 20:31:25 +08:00
|
|
|
{
|
2024-05-16 15:45:12 +08:00
|
|
|
__asm__ volatile("wfe" ::: "memory");
|
2023-10-25 20:31:25 +08:00
|
|
|
}
|
|
|
|
|
2024-05-16 15:45:12 +08:00
|
|
|
void rt_hw_spin_lock(rt_hw_spinlock_t *_lock)
|
2023-10-25 20:31:25 +08:00
|
|
|
{
|
2024-05-16 15:45:12 +08:00
|
|
|
union _spinlock *lock = (void *)_lock;
|
|
|
|
rt_uint16_t ticket =
|
|
|
|
atomic_fetch_add_explicit(&lock->ticket.next, 1, memory_order_relaxed);
|
|
|
|
|
|
|
|
if (atomic_load_explicit(&lock->ticket.owner, memory_order_acquire) !=
|
|
|
|
ticket)
|
|
|
|
{
|
|
|
|
_send_event_local();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
_wait_for_event();
|
|
|
|
}
|
|
|
|
while (_load_acq_exclusive(&lock->ticket.owner) != ticket);
|
|
|
|
}
|
2023-10-25 20:31:25 +08:00
|
|
|
}
|
|
|
|
|
2024-05-16 15:45:12 +08:00
|
|
|
void rt_hw_spin_unlock(rt_hw_spinlock_t *_lock)
|
2023-10-25 20:31:25 +08:00
|
|
|
{
|
2024-05-16 15:45:12 +08:00
|
|
|
union _spinlock *lock = (void *)_lock;
|
|
|
|
atomic_fetch_add_explicit(&lock->ticket.owner, 1, memory_order_release);
|
2020-01-10 10:38:21 +08:00
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2024-05-16 15:45:12 +08:00
|
|
|
#endif
|
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
static int _cpus_init_data_hardcoded(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
|
|
|
|
{
|
|
|
|
// load in cpu_hw_ids in cpuid_to_hwid,
|
|
|
|
// cpu_ops to cpu_ops_tbl
|
|
|
|
if (num_cpus > RT_CPUS_NR)
|
|
|
|
{
|
|
|
|
LOG_W("num_cpus (%d) greater than RT_CPUS_NR (%d)\n", num_cpus, RT_CPUS_NR);
|
|
|
|
num_cpus = RT_CPUS_NR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < num_cpus; i++)
|
|
|
|
{
|
|
|
|
set_hwid(i, cpu_hw_ids[i]);
|
|
|
|
cpu_ops_tbl[i] = cpu_ops[i];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** init cpu with hardcoded infomation or parsing from FDT */
|
|
|
|
static int _cpus_init(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
// first setup cpu_ops_tbl and cpuid_to_hwid
|
|
|
|
if (num_cpus > 0)
|
|
|
|
retval = _cpus_init_data_hardcoded(num_cpus, cpu_hw_ids, cpu_ops);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
// using cpuid_to_hwid and cpu_ops_tbl to call method_init and cpu_init
|
|
|
|
// assuming that cpuid 0 has already init
|
|
|
|
for (int i = 1; i < RT_CPUS_NR; i++)
|
|
|
|
{
|
2023-12-22 15:44:45 +08:00
|
|
|
if (rt_cpu_mpidr_early[i] == ID_ERROR)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
|
|
|
LOG_E("Failed to find hardware id of CPU %d", i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpu_ops_tbl[i] && cpu_ops_tbl[i]->cpu_init)
|
|
|
|
{
|
2023-11-28 14:20:11 +08:00
|
|
|
retval = cpu_ops_tbl[i]->cpu_init(i, RT_NULL);
|
2022-12-20 17:49:37 +08:00
|
|
|
CHECK_RETVAL(retval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_E("Failed to find cpu_init for cpu %d with cpu_ops[%p], cpu_ops->cpu_init[%p]"
|
2023-12-22 15:44:45 +08:00
|
|
|
, rt_cpu_mpidr_early[i], cpu_ops_tbl[i], cpu_ops_tbl[i] ? cpu_ops_tbl[i]->cpu_init : NULL);
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief boot cpu with hardcoded data
|
|
|
|
*
|
|
|
|
* @param num_cpus number of cpus
|
|
|
|
* @param cpu_hw_ids each element represents a hwid of cpu[i]
|
|
|
|
* @param cpu_ops each element represents a pointer to cpu_ops of cpu[i]
|
|
|
|
* @return int 0 on success,
|
|
|
|
*/
|
|
|
|
int rt_hw_cpu_boot_secondary(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
if (num_cpus < 1 || !cpu_hw_ids || !cpu_ops)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
retval = _cpus_init(num_cpus, cpu_hw_ids, cpu_ops);
|
|
|
|
CHECK_RETVAL(retval);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:38:21 +08:00
|
|
|
#endif /*RT_USING_SMP*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup ARM CPU
|
|
|
|
*/
|
|
|
|
/*@{*/
|
|
|
|
|
2023-03-27 17:09:37 +08:00
|
|
|
const char *rt_hw_cpu_arch(void)
|
|
|
|
{
|
|
|
|
return "aarch64";
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:38:21 +08:00
|
|
|
/*@}*/
|