cpuport_smp.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <stdint.h>
  14. #include "board.h"
  15. #include <encoding.h>
  16. #include <clint.h>
  17. #include <atomic.h>
  18. #ifdef RT_USING_SMP
  19. int rt_hw_cpu_id(void)
  20. {
  21. return read_csr(mhartid);
  22. }
  23. void rt_hw_spin_lock(rt_hw_spinlock_t *lock)
  24. {
  25. spinlock_lock((spinlock_t *)lock);
  26. }
  27. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock)
  28. {
  29. spinlock_unlock((spinlock_t *)lock);
  30. }
  31. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask)
  32. {
  33. int idx;
  34. for (idx = 0; idx < RT_CPUS_NR; idx ++)
  35. {
  36. if (cpu_mask & (1 << idx))
  37. {
  38. clint_ipi_send(idx);
  39. }
  40. }
  41. }
  42. extern rt_base_t secondary_boot_flag;
  43. void rt_hw_secondary_cpu_up(void)
  44. {
  45. mb();
  46. secondary_boot_flag = 0xa55a;
  47. }
  48. extern void rt_hw_scondary_interrupt_init(void);
  49. extern int rt_hw_tick_init(void);
  50. extern int rt_hw_clint_ipi_enable(void);
  51. void secondary_cpu_c_start(void)
  52. {
  53. rt_hw_spin_lock(&_cpus_lock);
  54. /* initialize interrupt controller */
  55. rt_hw_scondary_interrupt_init();
  56. rt_hw_tick_init();
  57. rt_hw_clint_ipi_enable();
  58. rt_system_scheduler_start();
  59. }
  60. void rt_hw_secondary_cpu_idle_exec(void)
  61. {
  62. asm volatile ("wfi");
  63. }
  64. #endif /*RT_USING_SMP*/