xref: /nrf52832-nimble/rt-thread/libcpu/risc-v/k210/cpuport_smp.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018/12/23     Bernard      The first version
9  * 2018/12/27     Jesven       Add secondary cpu boot
10  */
11 
12 #include <rthw.h>
13 #include <rtthread.h>
14 #include <stdint.h>
15 
16 #include "board.h"
17 #include <encoding.h>
18 #include <clint.h>
19 #include <atomic.h>
20 
21 #ifdef RT_USING_SMP
22 
rt_hw_cpu_id(void)23 int rt_hw_cpu_id(void)
24 {
25     return read_csr(mhartid);
26 }
27 
rt_hw_spin_lock(rt_hw_spinlock_t * lock)28 void rt_hw_spin_lock(rt_hw_spinlock_t *lock)
29 {
30     spinlock_lock((spinlock_t *)lock);
31 }
32 
rt_hw_spin_unlock(rt_hw_spinlock_t * lock)33 void rt_hw_spin_unlock(rt_hw_spinlock_t *lock)
34 {
35     spinlock_unlock((spinlock_t *)lock);
36 }
37 
rt_hw_ipi_send(int ipi_vector,unsigned int cpu_mask)38 void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask)
39 {
40     int idx;
41 
42     for (idx = 0; idx < RT_CPUS_NR; idx ++)
43     {
44         if (cpu_mask & (1 << idx))
45         {
46             clint_ipi_send(idx);
47         }
48     }
49 }
50 
51 extern rt_base_t secondary_boot_flag;
rt_hw_secondary_cpu_up(void)52 void rt_hw_secondary_cpu_up(void)
53 {
54     mb();
55     secondary_boot_flag = 0xa55a;
56 }
57 
58 extern void rt_hw_scondary_interrupt_init(void);
59 extern int rt_hw_tick_init(void);
60 extern int rt_hw_clint_ipi_enable(void);
61 
secondary_cpu_c_start(void)62 void secondary_cpu_c_start(void)
63 {
64     rt_hw_spin_lock(&_cpus_lock);
65 
66     /* initialize interrupt controller */
67     rt_hw_scondary_interrupt_init();
68 
69     rt_hw_tick_init();
70 
71     rt_hw_clint_ipi_enable();
72 
73     rt_system_scheduler_start();
74 }
75 
rt_hw_secondary_cpu_idle_exec(void)76 void rt_hw_secondary_cpu_idle_exec(void)
77 {
78     asm volatile ("wfi");
79 }
80 #endif /*RT_USING_SMP*/
81