secondary_cpu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. FPsciCpuMaskOn(cpu_mask, (uintptr)rt_secondary_cpu_entry);
  83. __asm__ volatile("dsb" ::: "memory");
  84. #endif
  85. }
  86. }
  87. void rt_hw_secondary_cpu_bsp_start(void)
  88. {
  89. /* spin lock init */
  90. rt_hw_spin_lock(&_cpus_lock);
  91. /* mmu init */
  92. #if defined(TARGET_ARMV8_AARCH64)
  93. extern unsigned long MMUTable[];
  94. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  95. #endif
  96. /* vector init */
  97. rt_hw_vector_init();
  98. /* interrupt init */
  99. #if defined(TARGET_ARMV8_AARCH64)
  100. arm_gic_cpu_init(0, 0);
  101. arm_gic_redist_init(0, 0);
  102. rt_kprintf("arm_gic_redist_init is over rt_hw_cpu_id() is %d \r\n", rt_hw_cpu_id());
  103. #else
  104. arm_gic_cpu_init(0);
  105. arm_gic_redist_init(0);
  106. #endif
  107. /* gtimer init */
  108. #if defined(TARGET_ARMV8_AARCH64)
  109. rt_hw_gtimer_init();
  110. #else
  111. rt_hw_timer_init();
  112. #endif
  113. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  114. /* start scheduler */
  115. rt_kprintf("\rcall cpu %d on success\n", rt_hw_cpu_id());
  116. rt_hw_secondary_cpu_idle_exec();
  117. rt_system_scheduler_start();
  118. }
  119. void rt_hw_secondary_cpu_idle_exec(void)
  120. {
  121. #if defined(TARGET_ARMV8_AARCH64)
  122. __WFE();
  123. #else
  124. asm volatile("wfe" ::
  125. : "memory", "cc");
  126. #endif
  127. }
  128. #endif