secondary_cpu.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #if defined(TARGET_ARMV8_AARCH64)
  29. #include "psci.h"
  30. extern void _secondary_cpu_entry(void);
  31. #else
  32. extern void rt_secondary_cpu_entry(void);
  33. #endif
  34. #include "fpsci.h"
  35. rt_uint64_t rt_cpu_mpidr_early[] =
  36. {
  37. #if defined(TARGET_E2000D)
  38. [0] = 0x80000200,
  39. [1] = 0x80000201,
  40. #elif defined(TARGET_E2000Q) || defined(TARGET_PHYTIUMPI)
  41. [0] = 0x80000000,
  42. [1] = 0x80000100,
  43. [2] = 0x80000200,
  44. [3] = 0x80000201,
  45. #elif defined(TARGET_F2000_4) || defined(TARGET_D2000)
  46. [0] = 0x80000000,
  47. [1] = 0x80000001,
  48. [2] = 0x80000100,
  49. [3] = 0x80000101,
  50. #if defined(TARGET_D2000)
  51. [4] = 0x80000200,
  52. [5] = 0x80000201,
  53. [6] = 0x80000300,
  54. [7] = 0x80000301,
  55. #endif
  56. #endif
  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. rt_kprintf("cpu_mask = 0x%x \n", cpu_mask);
  76. char *entry = (char *)_secondary_cpu_entry;
  77. entry += PV_OFFSET;
  78. FPsciCpuMaskOn(cpu_mask, (uintptr)entry);
  79. __DSB();
  80. #else
  81. /* code */
  82. char *entry = (char *)rt_secondary_cpu_entry;
  83. entry += PV_OFFSET;
  84. FPsciCpuMaskOn(cpu_mask, (uintptr)entry);
  85. __asm__ volatile("dsb" ::: "memory");
  86. #endif
  87. }
  88. }
  89. /**
  90. * This function will initialize board
  91. */
  92. extern size_t MMUTable[];
  93. void rt_hw_secondary_cpu_bsp_start(void)
  94. {
  95. /* spin lock init */
  96. rt_hw_spin_lock(&_cpus_lock);
  97. /* mmu init */
  98. #if defined(TARGET_ARMV8_AARCH64)
  99. extern unsigned long MMUTable[];
  100. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  101. #else
  102. rt_uint32_t mmutable_p;
  103. mmutable_p = (rt_uint32_t)MMUTable + (rt_uint32_t)PV_OFFSET ;
  104. rt_hw_mmu_switch(mmutable_p) ;
  105. #endif
  106. /* vector init */
  107. rt_hw_vector_init();
  108. /* interrupt init */
  109. #if defined(TARGET_ARMV8_AARCH64)
  110. arm_gic_cpu_init(0, 0);
  111. arm_gic_redist_init(0, 0);
  112. rt_kprintf("arm_gic_redist_init is over rt_hw_cpu_id() is %d \r\n", rt_hw_cpu_id());
  113. #else
  114. arm_gic_cpu_init(0);
  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