setup.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-21 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #define DBG_TAG "cpu.aa64"
  12. #define DBG_LVL DBG_INFO
  13. #include <rtdbg.h>
  14. #include <smp_call.h>
  15. #include <cpu.h>
  16. #include <mmu.h>
  17. #include <cpuport.h>
  18. #include <interrupt.h>
  19. #include <gtimer.h>
  20. #include <setup.h>
  21. #include <stdlib.h>
  22. #include <ioremap.h>
  23. #include <rtdevice.h>
  24. #include <gic.h>
  25. #include <gicv3.h>
  26. #include <mm_memblock.h>
  27. #define SIZE_KB 1024
  28. #define SIZE_MB (1024 * SIZE_KB)
  29. #define SIZE_GB (1024 * SIZE_MB)
  30. extern rt_ubase_t _start, _end;
  31. extern void _secondary_cpu_entry(void);
  32. extern size_t MMUTable[];
  33. extern void *system_vectors;
  34. static void *fdt_ptr = RT_NULL;
  35. static rt_size_t fdt_size = 0;
  36. static rt_uint64_t initrd_ranges[3] = { };
  37. #ifdef RT_USING_SMP
  38. extern struct cpu_ops_t cpu_psci_ops;
  39. extern struct cpu_ops_t cpu_spin_table_ops;
  40. #else
  41. extern int rt_hw_cpu_id(void);
  42. #endif
  43. rt_uint64_t rt_cpu_mpidr_table[] =
  44. {
  45. [RT_CPUS_NR] = 0,
  46. };
  47. static struct cpu_ops_t *cpu_ops[] =
  48. {
  49. #ifdef RT_USING_SMP
  50. &cpu_psci_ops,
  51. &cpu_spin_table_ops,
  52. #endif
  53. };
  54. static struct rt_ofw_node *cpu_np[RT_CPUS_NR] = { };
  55. void rt_hw_fdt_install_early(void *fdt)
  56. {
  57. if (fdt != RT_NULL && !fdt_check_header(fdt))
  58. {
  59. fdt_ptr = fdt;
  60. fdt_size = fdt_totalsize(fdt);
  61. }
  62. }
  63. #ifdef RT_USING_HWTIMER
  64. static rt_ubase_t loops_per_tick[RT_CPUS_NR];
  65. static rt_ubase_t cpu_get_cycles(void)
  66. {
  67. rt_ubase_t cycles;
  68. rt_hw_sysreg_read(cntpct_el0, cycles);
  69. return cycles;
  70. }
  71. static void cpu_loops_per_tick_init(void)
  72. {
  73. rt_ubase_t offset;
  74. volatile rt_ubase_t freq, step, cycles_end1, cycles_end2;
  75. volatile rt_uint32_t cycles_count1 = 0, cycles_count2 = 0;
  76. rt_hw_sysreg_read(cntfrq_el0, freq);
  77. step = freq / RT_TICK_PER_SECOND;
  78. cycles_end1 = cpu_get_cycles() + step;
  79. while (cpu_get_cycles() < cycles_end1)
  80. {
  81. __asm__ volatile ("nop");
  82. __asm__ volatile ("add %0, %0, #1":"=r"(cycles_count1));
  83. }
  84. cycles_end2 = cpu_get_cycles() + step;
  85. while (cpu_get_cycles() < cycles_end2)
  86. {
  87. __asm__ volatile ("add %0, %0, #1":"=r"(cycles_count2));
  88. }
  89. if ((rt_int32_t)(cycles_count2 - cycles_count1) > 0)
  90. {
  91. offset = cycles_count2 - cycles_count1;
  92. }
  93. else
  94. {
  95. /* Impossible, but prepared for any eventualities */
  96. offset = cycles_count2 / 4;
  97. }
  98. loops_per_tick[rt_hw_cpu_id()] = offset;
  99. }
  100. static void cpu_us_delay(rt_uint32_t us)
  101. {
  102. volatile rt_base_t start = cpu_get_cycles(), cycles;
  103. cycles = ((us * 0x10c7UL) * loops_per_tick[rt_hw_cpu_id()] * RT_TICK_PER_SECOND) >> 32;
  104. while ((cpu_get_cycles() - start) < cycles)
  105. {
  106. rt_hw_cpu_relax();
  107. }
  108. }
  109. #endif /* RT_USING_HWTIMER */
  110. rt_weak void rt_hw_idle_wfi(void)
  111. {
  112. __asm__ volatile ("wfi");
  113. }
  114. static void system_vectors_init(void)
  115. {
  116. rt_hw_set_current_vbar((rt_ubase_t)&system_vectors);
  117. }
  118. rt_inline void cpu_info_init(void)
  119. {
  120. int i = 0;
  121. rt_uint64_t mpidr;
  122. struct rt_ofw_node *np;
  123. /* get boot cpu info */
  124. rt_hw_sysreg_read(mpidr_el1, mpidr);
  125. rt_ofw_foreach_cpu_node(np)
  126. {
  127. rt_uint64_t hwid = rt_ofw_get_cpu_hwid(np, 0);
  128. if ((mpidr & MPIDR_AFFINITY_MASK) != hwid)
  129. {
  130. /* Only save affinity and res make smp boot can check */
  131. hwid |= 1ULL << 31;
  132. }
  133. else
  134. {
  135. hwid = mpidr;
  136. }
  137. cpu_np[i] = np;
  138. rt_cpu_mpidr_table[i] = hwid;
  139. rt_ofw_data(np) = (void *)hwid;
  140. for (int idx = 0; idx < RT_ARRAY_SIZE(cpu_ops); ++idx)
  141. {
  142. struct cpu_ops_t *ops = cpu_ops[idx];
  143. if (ops->cpu_init)
  144. {
  145. ops->cpu_init(i, np);
  146. }
  147. }
  148. if (++i >= RT_CPUS_NR)
  149. {
  150. break;
  151. }
  152. }
  153. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, rt_cpu_mpidr_table, sizeof(rt_cpu_mpidr_table));
  154. #ifdef RT_USING_HWTIMER
  155. cpu_loops_per_tick_init();
  156. if (!rt_device_hwtimer_us_delay)
  157. {
  158. rt_device_hwtimer_us_delay = &cpu_us_delay;
  159. }
  160. #endif /* RT_USING_HWTIMER */
  161. }
  162. void rt_hw_common_setup(void)
  163. {
  164. rt_size_t kernel_start, kernel_end;
  165. rt_size_t heap_start, heap_end;
  166. rt_size_t init_page_start, init_page_end;
  167. rt_size_t fdt_start, fdt_end;
  168. rt_region_t init_page_region = { 0 };
  169. rt_region_t platform_mem_region = { 0 };
  170. static struct mem_desc platform_mem_desc;
  171. const rt_ubase_t pv_off = PV_OFFSET;
  172. system_vectors_init();
  173. #ifdef RT_USING_SMART
  174. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0xfffffffff0000000, 0x10000000, MMUTable, pv_off);
  175. #else
  176. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0xffffd0000000, 0x10000000, MMUTable, 0);
  177. #endif
  178. kernel_start = RT_ALIGN_DOWN((rt_size_t)rt_kmem_v2p((void *)&_start) - 64, ARCH_PAGE_SIZE);
  179. kernel_end = RT_ALIGN((rt_size_t)rt_kmem_v2p((void *)&_end), ARCH_PAGE_SIZE);
  180. heap_start = kernel_end;
  181. heap_end = RT_ALIGN(heap_start + ARCH_HEAP_SIZE, ARCH_PAGE_SIZE);
  182. init_page_start = heap_end;
  183. init_page_end = RT_ALIGN(init_page_start + ARCH_INIT_PAGE_SIZE, ARCH_PAGE_SIZE);
  184. fdt_start = init_page_end;
  185. fdt_end = RT_ALIGN(fdt_start + fdt_size, ARCH_PAGE_SIZE);
  186. platform_mem_region.start = kernel_start;
  187. platform_mem_region.end = fdt_end;
  188. rt_memblock_reserve_memory("kernel", kernel_start, kernel_end, MEMBLOCK_NONE);
  189. rt_memblock_reserve_memory("memheap", heap_start, heap_end, MEMBLOCK_NONE);
  190. rt_memblock_reserve_memory("init-page", init_page_start, init_page_end, MEMBLOCK_NONE);
  191. rt_memblock_reserve_memory("fdt", fdt_start, fdt_end, MEMBLOCK_NONE);
  192. rt_memmove((void *)(fdt_start - pv_off), (void *)(fdt_ptr - pv_off), fdt_size);
  193. fdt_ptr = (void *)fdt_start - pv_off;
  194. rt_system_heap_init((void *)(heap_start - pv_off), (void *)(heap_end - pv_off));
  195. init_page_region.start = init_page_start - pv_off;
  196. init_page_region.end = init_page_end - pv_off;
  197. rt_page_init(init_page_region);
  198. /* create MMU mapping of kernel memory */
  199. platform_mem_region.start = RT_ALIGN_DOWN(platform_mem_region.start, ARCH_PAGE_SIZE);
  200. platform_mem_region.end = RT_ALIGN(platform_mem_region.end, ARCH_PAGE_SIZE);
  201. platform_mem_desc.paddr_start = platform_mem_region.start;
  202. platform_mem_desc.vaddr_start = platform_mem_region.start - pv_off;
  203. platform_mem_desc.vaddr_end = platform_mem_region.end - pv_off - 1;
  204. platform_mem_desc.attr = NORMAL_MEM;
  205. rt_hw_mmu_setup(&rt_kernel_space, &platform_mem_desc, 1);
  206. if (rt_fdt_prefetch(fdt_ptr))
  207. {
  208. /* Platform cannot be initialized */
  209. RT_ASSERT(0);
  210. }
  211. rt_fdt_scan_chosen_stdout();
  212. rt_fdt_scan_initrd(initrd_ranges);
  213. rt_fdt_scan_memory();
  214. rt_memblock_setup_memory_environment();
  215. rt_fdt_earlycon_kick(FDT_EARLYCON_KICK_UPDATE);
  216. rt_fdt_unflatten();
  217. cpu_info_init();
  218. #ifdef RT_USING_PIC
  219. rt_pic_init();
  220. rt_pic_irq_init();
  221. #else
  222. /* initialize hardware interrupt */
  223. rt_hw_interrupt_init();
  224. /* initialize uart */
  225. rt_hw_uart_init();
  226. #endif
  227. #ifndef RT_HWTIMER_ARM_ARCH
  228. /* initialize timer for os tick */
  229. rt_hw_gtimer_init();
  230. #endif /* !RT_HWTIMER_ARM_ARCH */
  231. #ifdef RT_USING_COMPONENTS_INIT
  232. rt_components_board_init();
  233. #endif
  234. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  235. rt_ofw_console_setup();
  236. #endif
  237. rt_thread_idle_sethook(rt_hw_idle_wfi);
  238. #ifdef RT_USING_SMP
  239. rt_smp_call_init();
  240. /* Install the IPI handle */
  241. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  242. rt_hw_ipi_handler_install(RT_STOP_IPI, rt_scheduler_ipi_handler);
  243. rt_hw_ipi_handler_install(RT_SMP_CALL_IPI, rt_smp_call_ipi_handler);
  244. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  245. rt_hw_interrupt_umask(RT_STOP_IPI);
  246. rt_hw_interrupt_umask(RT_SMP_CALL_IPI);
  247. #endif
  248. }
  249. #ifdef RT_USING_SMP
  250. rt_weak void rt_hw_secondary_cpu_up(void)
  251. {
  252. int cpu_id = rt_hw_cpu_id();
  253. rt_uint64_t entry = (rt_uint64_t)rt_kmem_v2p(_secondary_cpu_entry);
  254. if (!entry)
  255. {
  256. LOG_E("Failed to translate '_secondary_cpu_entry' to physical address");
  257. RT_ASSERT(0);
  258. }
  259. /* Maybe we are no in the first cpu */
  260. for (int i = 0; i < RT_ARRAY_SIZE(cpu_np); ++i)
  261. {
  262. int err;
  263. const char *enable_method;
  264. if (!cpu_np[i] || i == cpu_id)
  265. {
  266. continue;
  267. }
  268. err = rt_ofw_prop_read_string(cpu_np[i], "enable-method", &enable_method);
  269. for (int idx = 0; !err && idx < RT_ARRAY_SIZE(cpu_ops); ++idx)
  270. {
  271. struct cpu_ops_t *ops = cpu_ops[idx];
  272. if (ops->method && !rt_strcmp(ops->method, enable_method) && ops->cpu_boot)
  273. {
  274. err = ops->cpu_boot(i, entry);
  275. break;
  276. }
  277. }
  278. if (err)
  279. {
  280. LOG_W("Call cpu %d on %s", i, "failed");
  281. }
  282. }
  283. }
  284. rt_weak void rt_hw_secondary_cpu_bsp_start(void)
  285. {
  286. int cpu_id = rt_hw_cpu_id();
  287. system_vectors_init();
  288. rt_hw_spin_lock(&_cpus_lock);
  289. /* Save all mpidr */
  290. rt_hw_sysreg_read(mpidr_el1, rt_cpu_mpidr_table[cpu_id]);
  291. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  292. #ifdef RT_USING_PIC
  293. rt_pic_irq_init();
  294. #else
  295. /* initialize vector table */
  296. rt_hw_vector_init();
  297. arm_gic_cpu_init(0, 0);
  298. #ifdef BSP_USING_GICV3
  299. arm_gic_redist_init(0, 0);
  300. #endif /* BSP_USING_GICV3 */
  301. #endif
  302. #ifndef RT_HWTIMER_ARM_ARCH
  303. /* initialize timer for os tick */
  304. rt_hw_gtimer_local_enable();
  305. #endif /* !RT_HWTIMER_ARM_ARCH */
  306. rt_dm_secondary_cpu_init();
  307. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  308. rt_hw_interrupt_umask(RT_STOP_IPI);
  309. rt_hw_interrupt_umask(RT_SMP_CALL_IPI);
  310. LOG_I("Call cpu %d on %s", cpu_id, "success");
  311. #ifdef RT_USING_HWTIMER
  312. if (rt_device_hwtimer_us_delay == &cpu_us_delay)
  313. {
  314. cpu_loops_per_tick_init();
  315. }
  316. #endif
  317. rt_system_scheduler_start();
  318. }
  319. rt_weak void rt_hw_secondary_cpu_idle_exec(void)
  320. {
  321. rt_hw_wfe();
  322. }
  323. #endif
  324. void rt_hw_console_output(const char *str)
  325. {
  326. rt_fdt_earlycon_output(str);
  327. }