secondary_cpu.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2022-10-26 huanghe first commit
  11. * 2022-10-26 zhugengyu support aarch64
  12. *
  13. */
  14. #include <rtthread.h>
  15. #include "board.h"
  16. #include <gicv3.h>
  17. #include "rtconfig.h"
  18. #include "phytium_cpu.h"
  19. #if defined(TARGET_ARMV8_AARCH64)
  20. #include "cpuport.h"
  21. #include "gtimer.h"
  22. #include "mmu.h"
  23. #include "cp15.h"
  24. #endif
  25. #ifdef RT_USING_SMP
  26. #include <interrupt.h>
  27. #if defined(TARGET_ARMV8_AARCH64)
  28. #include "psci.h"
  29. extern void _secondary_cpu_entry(void);
  30. #else
  31. extern void rt_secondary_cpu_entry(void);
  32. #endif
  33. #include "fpsci.h"
  34. rt_uint64_t rt_cpu_mpidr_early[] =
  35. {
  36. #if defined(TARGET_E2000D)
  37. [0] = 0x80000200,
  38. [1] = 0x80000201,
  39. #elif defined(TARGET_E2000Q)
  40. [0] = 0x80000000,
  41. [1] = 0x80000100,
  42. [2] = 0x80000200,
  43. [3] = 0x80000201,
  44. #elif defined(TARGET_F2000_4) || defined(TARGET_D2000)
  45. [0] = 0x80000000,
  46. [1] = 0x80000001,
  47. [2] = 0x80000100,
  48. [3] = 0x80000101,
  49. #if defined(TARGET_D2000)
  50. [4] = 0x80000200,
  51. [5] = 0x80000201,
  52. [6] = 0x80000300,
  53. [7] = 0x80000301,
  54. #endif
  55. #endif
  56. };
  57. extern int rt_hw_timer_init(void);
  58. #include "fcache.h"
  59. void rt_hw_secondary_cpu_up(void)
  60. {
  61. rt_uint32_t i;
  62. rt_uint32_t cpu_mask = 0;
  63. int cpu_id;
  64. cpu_id = rt_hw_cpu_id();
  65. rt_kprintf("rt_hw_secondary_cpu_up is processing \r\n");
  66. for (i = 0; i < RT_CPUS_NR;i++)
  67. {
  68. if(i == cpu_id)
  69. {
  70. continue;
  71. }
  72. cpu_mask = 1 << phytium_cpu_id_mapping(i);
  73. #if defined(TARGET_ARMV8_AARCH64)
  74. /* code */
  75. char *entry = (char *)_secondary_cpu_entry;
  76. entry += PV_OFFSET;
  77. PsciCpuOn(cpu_mask, (uintptr)entry);
  78. __DSB();
  79. #else
  80. /* code */
  81. PsciCpuOn(cpu_mask, (uintptr)rt_secondary_cpu_entry);
  82. __asm__ volatile("dsb" ::: "memory");
  83. #endif
  84. }
  85. }
  86. void rt_hw_secondary_cpu_bsp_start(void)
  87. {
  88. /* spin lock init */
  89. rt_hw_spin_lock(&_cpus_lock);
  90. /* mmu init */
  91. #if defined(TARGET_ARMV8_AARCH64)
  92. extern unsigned long MMUTable[];
  93. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  94. #endif
  95. /* vector init */
  96. rt_hw_vector_init();
  97. /* interrupt init */
  98. #if defined(TARGET_ARMV8_AARCH64)
  99. arm_gic_cpu_init(0, 0);
  100. arm_gic_redist_init(0, 0);
  101. rt_kprintf("arm_gic_redist_init is over rt_hw_cpu_id() is %d \r\n", rt_hw_cpu_id());
  102. #else
  103. arm_gic_cpu_init(0);
  104. arm_gic_redist_init(0);
  105. #endif
  106. /* gtimer init */
  107. #if defined(TARGET_ARMV8_AARCH64)
  108. rt_hw_gtimer_init();
  109. #else
  110. rt_hw_timer_init();
  111. #endif
  112. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  113. /* start scheduler */
  114. rt_kprintf("\rcall cpu %d on success\n", rt_hw_cpu_id());
  115. rt_hw_secondary_cpu_idle_exec();
  116. rt_system_scheduler_start();
  117. }
  118. void rt_hw_secondary_cpu_idle_exec(void)
  119. {
  120. #if defined(TARGET_ARMV8_AARCH64)
  121. __WFE();
  122. #else
  123. asm volatile("wfe" ::
  124. : "memory", "cc");
  125. #endif
  126. }
  127. #endif