secondary_cpu.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * 2023-07-26 huanghe update psci uage
  13. *
  14. */
  15. #include <rtthread.h>
  16. #include "board.h"
  17. #include <gicv3.h>
  18. #include "rtconfig.h"
  19. #include "phytium_cpu.h"
  20. #if defined(TARGET_ARMV8_AARCH64)
  21. #include "cpuport.h"
  22. #include "gtimer.h"
  23. #include "mmu.h"
  24. #include "cp15.h"
  25. #endif
  26. #ifdef RT_USING_SMP
  27. #include <interrupt.h>
  28. #include "phytium_interrupt.h"
  29. #if defined(TARGET_ARMV8_AARCH64)
  30. #include "psci.h"
  31. extern void _secondary_cpu_entry(void);
  32. #else
  33. extern void rt_secondary_cpu_entry(void);
  34. #endif
  35. #include "fpsci.h"
  36. rt_uint64_t rt_cpu_mpidr_early[] =
  37. {
  38. #if defined(TARGET_E2000D)
  39. [0] = 0x80000200,
  40. [1] = 0x80000201,
  41. #elif defined(TARGET_E2000Q) || defined(TARGET_PHYTIUMPI)
  42. [0] = 0x80000000,
  43. [1] = 0x80000100,
  44. [2] = 0x80000200,
  45. [3] = 0x80000201,
  46. #elif defined(TARGET_F2000_4) || defined(TARGET_D2000)
  47. [0] = 0x80000000,
  48. [1] = 0x80000001,
  49. [2] = 0x80000100,
  50. [3] = 0x80000101,
  51. #if defined(TARGET_D2000)
  52. [4] = 0x80000200,
  53. [5] = 0x80000201,
  54. [6] = 0x80000300,
  55. [7] = 0x80000301,
  56. #endif
  57. #endif
  58. };
  59. extern int rt_hw_timer_init(void);
  60. void rt_hw_secondary_cpu_up(void)
  61. {
  62. rt_uint32_t i;
  63. rt_uint32_t cpu_mask = 0;
  64. int cpu_id;
  65. cpu_id = rt_hw_cpu_id();
  66. rt_kprintf("rt_hw_secondary_cpu_up is processing \r\n");
  67. for (i = 0; i < RT_CPUS_NR; i++)
  68. {
  69. if (i == cpu_id)
  70. {
  71. continue;
  72. }
  73. cpu_mask = 1<<phytium_cpu_id_mapping(i);
  74. #if defined(TARGET_ARMV8_AARCH64)
  75. /* code */
  76. rt_kprintf("cpu_mask = 0x%x \n", cpu_mask);
  77. char *entry = (char *)_secondary_cpu_entry;
  78. entry += PV_OFFSET;
  79. FPsciCpuMaskOn(cpu_mask, (uintptr)entry);
  80. __DSB();
  81. #else
  82. /* code */
  83. char *entry = (char *)rt_secondary_cpu_entry;
  84. entry += PV_OFFSET;
  85. FPsciCpuMaskOn(cpu_mask, (uintptr)entry);
  86. __asm__ volatile("dsb" ::: "memory");
  87. #endif
  88. }
  89. }
  90. /**
  91. * This function will initialize board
  92. */
  93. extern size_t MMUTable[];
  94. void rt_hw_secondary_cpu_bsp_start(void)
  95. {
  96. /* spin lock init */
  97. rt_hw_spin_lock(&_cpus_lock);
  98. /* mmu init */
  99. #if defined(TARGET_ARMV8_AARCH64)
  100. extern unsigned long MMUTable[];
  101. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  102. #else
  103. rt_uint32_t mmutable_p;
  104. mmutable_p = (rt_uint32_t)MMUTable + (rt_uint32_t)PV_OFFSET ;
  105. rt_hw_mmu_switch(mmutable_p) ;
  106. #endif
  107. /* vector init */
  108. rt_hw_vector_init();
  109. /* interrupt init */
  110. #if defined(TARGET_ARMV8_AARCH64)
  111. arm_gic_cpu_init(0, 0);
  112. phytium_aarch64_arm_gic_redist_init();
  113. rt_kprintf("arm_gic_redist_init is over rt_hw_cpu_id() is %d \r\n", rt_hw_cpu_id());
  114. #else
  115. arm_gic_cpu_init(0);
  116. arm_gic_redist_init(0);
  117. #endif
  118. /* gtimer init */
  119. #if defined(TARGET_ARMV8_AARCH64)
  120. rt_hw_gtimer_init();
  121. #else
  122. rt_hw_timer_init();
  123. #endif
  124. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  125. /* start scheduler */
  126. rt_kprintf("\rcall cpu %d on success\n", rt_hw_cpu_id());
  127. rt_hw_secondary_cpu_idle_exec();
  128. rt_system_scheduler_start();
  129. }
  130. void rt_hw_secondary_cpu_idle_exec(void)
  131. {
  132. #if defined(TARGET_ARMV8_AARCH64)
  133. __WFE();
  134. #else
  135. asm volatile("wfe" ::
  136. : "memory", "cc");
  137. #endif
  138. }
  139. #endif