1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2023-10-25 Shell Move ffs to cpuport, add general implementation
9 * by inline assembly
10 * 2024-01-18 Shell support rt_hw_thread_self to improve overall performance
11 */
12
13 #ifndef CPUPORT_H__
14 #define CPUPORT_H__
15
16 #include <armv8.h>
17 #include <rtcompiler.h>
18 #include <rttypes.h>
19
20 #ifdef RT_USING_SMP
21
22 /**
23 * Spinlock
24 */
25
26 typedef struct
27 {
28 rt_uint32_t value;
29 } rt_hw_spinlock_t;
30
31 #endif /* RT_USING_SMP */
32
33 #define rt_hw_barrier(cmd, ...) \
34 __asm__ volatile (RT_STRINGIFY(cmd) " "RT_STRINGIFY(__VA_ARGS__):::"memory")
35
36 #define rt_hw_isb() rt_hw_barrier(isb)
37 #define rt_hw_dmb() rt_hw_barrier(dmb, ish)
38 #define rt_hw_wmb() rt_hw_barrier(dmb, ishst)
39 #define rt_hw_rmb() rt_hw_barrier(dmb, ishld)
40 #define rt_hw_dsb() rt_hw_barrier(dsb, ish)
41
42 #define rt_hw_wfi() rt_hw_barrier(wfi)
43 #define rt_hw_wfe() rt_hw_barrier(wfe)
44 #define rt_hw_sev() rt_hw_barrier(sev)
45
46 #define rt_hw_cpu_relax() rt_hw_barrier(yield)
47
48 #define rt_hw_sysreg_write(sysreg, val) \
49 __asm__ volatile ("msr "RT_STRINGIFY(sysreg)", %0"::"r"((rt_uint64_t)(val)))
50
51 #define rt_hw_sysreg_read(sysreg, val) \
52 __asm__ volatile ("mrs %0, "RT_STRINGIFY(sysreg)"":"=r"((val)))
53
54 void _thread_start(void);
55
56 #ifdef ARCH_USING_HW_THREAD_SELF
rt_hw_thread_self(void)57 rt_inline struct rt_thread *rt_hw_thread_self(void)
58 {
59 struct rt_thread *thread;
60 __asm__ volatile ("mrs %0, " RT_STRINGIFY(ARM64_THREAD_REG) :"=r"(thread));
61
62 return thread;
63 }
64
rt_hw_thread_set_self(struct rt_thread * thread)65 rt_inline void rt_hw_thread_set_self(struct rt_thread *thread)
66 {
67 __asm__ volatile ("msr " RT_STRINGIFY(ARM64_THREAD_REG) ", %0"::"r"(thread));
68 }
69
70 #endif /* ARCH_USING_HW_THREAD_SELF */
71
72 #endif /*CPUPORT_H__*/
73