setup.c 9.8 KB

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