secondary_cpu.c 3.3 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. #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] = 0x80000200,
  43. [1] = 0x80000201,
  44. [2] = 0x80000000,
  45. [3] = 0x80000100,
  46. #endif
  47. };
  48. extern int rt_hw_timer_init(void);
  49. void rt_hw_secondary_cpu_up(void)
  50. {
  51. rt_uint32_t i;
  52. rt_uint32_t cpu_mask = 0;
  53. int cpu_id;
  54. cpu_id = rt_hw_cpu_id();
  55. rt_kprintf("rt_hw_secondary_cpu_up is processing \r\n");
  56. for (i = 0; i < RT_CPUS_NR; i++)
  57. {
  58. if (i == cpu_id)
  59. {
  60. continue;
  61. }
  62. cpu_mask = 1<<phytium_cpu_id_mapping(i);
  63. #if defined(TARGET_ARMV8_AARCH64)
  64. /* code */
  65. rt_kprintf("cpu_mask = 0x%x \n", cpu_mask);
  66. char *entry = (char *)_secondary_cpu_entry;
  67. entry += PV_OFFSET;
  68. FPsciCpuMaskOn(cpu_mask, (uintptr)entry);
  69. __DSB();
  70. #else
  71. /* code */
  72. char *entry = (char *)rt_secondary_cpu_entry;
  73. entry += PV_OFFSET;
  74. FPsciCpuMaskOn(cpu_mask, (uintptr)entry);
  75. __asm__ volatile("dsb" ::: "memory");
  76. #endif
  77. }
  78. }
  79. /**
  80. * This function will initialize board
  81. */
  82. extern size_t MMUTable[];
  83. void rt_hw_secondary_cpu_bsp_start(void)
  84. {
  85. /* spin lock init */
  86. rt_hw_spin_lock(&_cpus_lock);
  87. /* mmu init */
  88. #if defined(TARGET_ARMV8_AARCH64)
  89. extern unsigned long MMUTable[];
  90. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  91. #else
  92. rt_uint32_t mmutable_p;
  93. mmutable_p = (rt_uint32_t)MMUTable + (rt_uint32_t)PV_OFFSET ;
  94. rt_hw_mmu_switch((void*)mmutable_p) ;
  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_address_set(0, platform_get_gic_redist_base(), rt_hw_cpu_id());
  102. phytium_aarch64_arm_gic_redist_init();
  103. #else
  104. arm_gic_cpu_init(0);
  105. arm_gic_redist_address_set(0, platform_get_gic_redist_base(), rt_hw_cpu_id());
  106. arm_gic_redist_init(0);
  107. #endif
  108. /* gtimer init */
  109. #if defined(TARGET_ARMV8_AARCH64)
  110. rt_hw_gtimer_init();
  111. #else
  112. rt_hw_timer_init();
  113. #endif
  114. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  115. /* start scheduler */
  116. rt_kprintf("\rcall cpu %d on success\n", rt_hw_cpu_id());
  117. rt_hw_secondary_cpu_idle_exec();
  118. rt_system_scheduler_start();
  119. }
  120. void rt_hw_secondary_cpu_idle_exec(void)
  121. {
  122. #if defined(TARGET_ARMV8_AARCH64)
  123. __WFE();
  124. #else
  125. asm volatile("wfe" ::
  126. : "memory", "cc");
  127. #endif
  128. }
  129. #endif