secondary_cpu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_PE2202)
  39. [0] = RT_CORE_AFF(0),
  40. [1] = RT_CORE_AFF(1),
  41. #elif defined(TARGET_PE2204)
  42. [0] = RT_CORE_AFF(0),
  43. [1] = RT_CORE_AFF(1),
  44. [2] = RT_CORE_AFF(2),
  45. [3] = RT_CORE_AFF(3),
  46. #elif defined(TARGET_PD2408)
  47. [0] = RT_CORE_AFF(0),
  48. [1] = RT_CORE_AFF(1),
  49. [2] = RT_CORE_AFF(2),
  50. [3] = RT_CORE_AFF(3),
  51. [4] = RT_CORE_AFF(4),
  52. [5] = RT_CORE_AFF(5),
  53. [6] = RT_CORE_AFF(6),
  54. [7] = RT_CORE_AFF(7),
  55. #endif
  56. [RT_CPUS_NR] = 0
  57. };
  58. extern int rt_hw_timer_init(void);
  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. FPsciCpuMaskOn(cpu_mask, (uintptr)entry);
  78. __DSB();
  79. #else
  80. /* code */
  81. char *entry = (char *)rt_secondary_cpu_entry;
  82. entry += PV_OFFSET;
  83. FPsciCpuMaskOn(cpu_mask, (uintptr)entry);
  84. __asm__ volatile("dsb" ::: "memory");
  85. #endif
  86. }
  87. }
  88. /**
  89. * This function will initialize board
  90. */
  91. extern size_t MMUTable[];
  92. void rt_hw_secondary_cpu_bsp_start(void)
  93. {
  94. /* spin lock init */
  95. rt_hw_spin_lock(&_cpus_lock);
  96. /* mmu init */
  97. #if defined(TARGET_ARMV8_AARCH64)
  98. extern unsigned long MMUTable[];
  99. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  100. #else
  101. rt_uint32_t mmutable_p;
  102. mmutable_p = (rt_uint32_t)MMUTable + (rt_uint32_t)PV_OFFSET ;
  103. rt_hw_mmu_switch((void*)mmutable_p) ;
  104. #endif
  105. /* vector init */
  106. rt_hw_vector_init();
  107. /* interrupt init */
  108. #if defined(TARGET_ARMV8_AARCH64)
  109. arm_gic_cpu_init(0, 0);
  110. arm_gic_redist_address_set(0, platform_get_gic_redist_base(), rt_hw_cpu_id());
  111. phytium_aarch64_arm_gic_redist_init();
  112. #else
  113. arm_gic_cpu_init(0);
  114. arm_gic_redist_address_set(0, platform_get_gic_redist_base(), rt_hw_cpu_id());
  115. arm_gic_redist_init(0);
  116. #endif
  117. /* gtimer init */
  118. #if defined(TARGET_ARMV8_AARCH64)
  119. rt_hw_gtimer_init();
  120. #else
  121. rt_hw_timer_init();
  122. #endif
  123. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  124. /* start scheduler */
  125. rt_kprintf("\rcall cpu %d on success\n", rt_hw_cpu_id());
  126. rt_hw_secondary_cpu_idle_exec();
  127. rt_system_scheduler_start();
  128. }
  129. void rt_hw_secondary_cpu_idle_exec(void)
  130. {
  131. #if defined(TARGET_ARMV8_AARCH64)
  132. __WFE();
  133. #else
  134. asm volatile("wfe" ::
  135. : "memory", "cc");
  136. #endif
  137. }
  138. #endif