cpu.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-09-15 Bernard first version
  9. * 2019-07-28 zdzn add smp support
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <board.h>
  14. #include "cp15.h"
  15. #define DBG_TAG "libcpu.aarch64.cpu"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include <string.h>
  19. #include "cpu.h"
  20. #include "psci_api.h"
  21. void (*system_off)(void);
  22. #ifdef RT_USING_SMP
  23. #ifdef RT_USING_FDT
  24. #include "dtb_node.h"
  25. struct dtb_node *_cpu_node[RT_CPUS_NR];
  26. #endif /* RT_USING_FDT */
  27. #define MPIDR_AFF_MASK 0x000000FF00FFFFFFul
  28. #define REPORT_ERR(retval) LOG_E("got error code %d in %s(), %s:%d", (retval), __func__, __FILE__, __LINE__)
  29. #define CHECK_RETVAL(retval) if (retval) {REPORT_ERR(retval);}
  30. /**
  31. * cpu_ops_tbl contains cpu_ops_t for each cpu kernel observed,
  32. * given cpu logical id 'i', its cpu_ops_t is 'cpu_ops_tbl[i]'
  33. */
  34. struct cpu_ops_t *cpu_ops_tbl[RT_CPUS_NR];
  35. #ifdef RT_USING_SMART
  36. // _id_to_mpidr is a table translate logical id to mpid, which is a 64-bit value
  37. rt_uint64_t rt_cpu_mpidr_early[RT_CPUS_NR] rt_weak = {[0 ... RT_CPUS_NR - 1] = ID_ERROR};
  38. #else
  39. /* The more common mpidr_el1 table, redefine it in BSP if it is in other cases */
  40. rt_weak rt_uint64_t rt_cpu_mpidr_early[] =
  41. {
  42. [0] = 0x80000000,
  43. [1] = 0x80000001,
  44. [2] = 0x80000002,
  45. [3] = 0x80000003,
  46. [4] = 0x80000004,
  47. [5] = 0x80000005,
  48. [6] = 0x80000006,
  49. [7] = 0x80000007,
  50. [RT_CPUS_NR] = 0
  51. };
  52. #endif /* RT_USING_SMART */
  53. void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock)
  54. {
  55. lock->slock = 0;
  56. }
  57. #define TICKET_SHIFT 16
  58. void rt_hw_spin_lock(rt_hw_spinlock_t *lock)
  59. {
  60. unsigned int tmp;
  61. struct __arch_tickets lockval, newval;
  62. asm volatile(
  63. /* Atomically increment the next ticket. */
  64. " prfm pstl1strm, %3\n"
  65. "1: ldaxr %w0, %3\n"
  66. " add %w1, %w0, %w5\n"
  67. " stxr %w2, %w1, %3\n"
  68. " cbnz %w2, 1b\n"
  69. /* Did we get the lock? */
  70. " eor %w1, %w0, %w0, ror #16\n"
  71. " cbz %w1, 3f\n"
  72. /*
  73. * No: spin on the owner. Send a local event to avoid missing an
  74. * unlock before the exclusive load.
  75. */
  76. " sevl\n"
  77. "2: wfe\n"
  78. " ldaxrh %w2, %4\n"
  79. " eor %w1, %w2, %w0, lsr #16\n"
  80. " cbnz %w1, 2b\n"
  81. /* We got the lock. Critical section starts here. */
  82. "3:"
  83. : "=&r"(lockval), "=&r"(newval), "=&r"(tmp), "+Q"(*lock)
  84. : "Q"(lock->tickets.owner), "I"(1 << TICKET_SHIFT)
  85. : "memory");
  86. rt_hw_dmb();
  87. }
  88. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock)
  89. {
  90. rt_hw_dmb();
  91. asm volatile(
  92. " stlrh %w1, %0\n"
  93. : "=Q"(lock->tickets.owner)
  94. : "r"(lock->tickets.owner + 1)
  95. : "memory");
  96. }
  97. static int _cpus_init_data_hardcoded(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
  98. {
  99. // load in cpu_hw_ids in cpuid_to_hwid,
  100. // cpu_ops to cpu_ops_tbl
  101. if (num_cpus > RT_CPUS_NR)
  102. {
  103. LOG_W("num_cpus (%d) greater than RT_CPUS_NR (%d)\n", num_cpus, RT_CPUS_NR);
  104. num_cpus = RT_CPUS_NR;
  105. }
  106. for (int i = 0; i < num_cpus; i++)
  107. {
  108. set_hwid(i, cpu_hw_ids[i]);
  109. cpu_ops_tbl[i] = cpu_ops[i];
  110. }
  111. return 0;
  112. }
  113. #ifdef RT_USING_FDT
  114. /** read ('size' * 4) bytes number from start, big-endian format */
  115. static rt_uint64_t _read_be_number(void *start, int size)
  116. {
  117. rt_uint64_t buf = 0;
  118. for (; size > 0; size--)
  119. {
  120. buf = (buf << 32) | fdt32_to_cpu(*(uint32_t *)start);
  121. start = (uint32_t *)start + 1;
  122. }
  123. return buf;
  124. }
  125. /** check device-type of the node, */
  126. static bool _node_is_cpu(struct dtb_node *node)
  127. {
  128. char *device_type = dtb_node_get_dtb_node_property_value(node, "device_type", NULL);
  129. if (device_type)
  130. {
  131. return !strcmp(device_type, "cpu");
  132. }
  133. return false;
  134. }
  135. static int _read_and_set_hwid(struct dtb_node *cpu, int *id_pool, int *pcpuid)
  136. {
  137. // size/address_cells is number of elements in reg array
  138. int size;
  139. static int address_cells, size_cells;
  140. if (!address_cells && !size_cells)
  141. dtb_node_get_dtb_node_cells(cpu, &address_cells, &size_cells);
  142. void *id_start = dtb_node_get_dtb_node_property_value(cpu, "reg", &size);
  143. rt_uint64_t mpid = _read_be_number(id_start, address_cells);
  144. *pcpuid = *id_pool;
  145. *id_pool = *id_pool + 1;
  146. set_hwid(*pcpuid, mpid);
  147. LOG_I("Using MPID 0x%lx as cpu %d", mpid, *pcpuid);
  148. // setting _cpu_node for cpu_init use
  149. _cpu_node[*pcpuid] = cpu;
  150. return 0;
  151. }
  152. static int _read_and_set_cpuops(struct dtb_node *cpu, int cpuid)
  153. {
  154. char *method = dtb_node_get_dtb_node_property_value(cpu, "enable-method", NULL);
  155. if (!method)
  156. {
  157. LOG_E("Cannot read method from cpu node");
  158. return -1;
  159. }
  160. struct cpu_ops_t *cpu_ops;
  161. if (!strcmp(method, cpu_ops_psci.method))
  162. {
  163. cpu_ops = &cpu_ops_psci;
  164. }
  165. else if (!strcmp(method, cpu_ops_spin_tbl.method))
  166. {
  167. cpu_ops = &cpu_ops_spin_tbl;
  168. }
  169. else
  170. {
  171. cpu_ops = RT_NULL;
  172. LOG_E("Not supported cpu_ops: %s", method);
  173. }
  174. cpu_ops_tbl[cpuid] = cpu_ops;
  175. LOG_D("Using boot method [%s] for cpu %d", cpu_ops->method, cpuid);
  176. return 0;
  177. }
  178. static int _cpus_init_data_fdt()
  179. {
  180. // cpuid_to_hwid and cpu_ops_tbl with fdt
  181. void *root = get_dtb_node_head();
  182. int id_pool = 0;
  183. int cpuid;
  184. struct dtb_node *cpus = dtb_node_get_dtb_node_by_path(root, "/cpus");
  185. // for each cpu node (device-type is cpu), read its mpid and set its cpuid_to_hwid
  186. for_each_node_child(cpus)
  187. {
  188. if (!_node_is_cpu(cpus))
  189. {
  190. continue;
  191. }
  192. if (id_pool > RT_CPUS_NR)
  193. {
  194. LOG_W("Reading more cpus from FDT than RT_CPUS_NR"
  195. "\n Parsing will not continue and only %d cpus will be used.", RT_CPUS_NR);
  196. break;
  197. }
  198. _read_and_set_hwid(cpus, &id_pool, &cpuid);
  199. _read_and_set_cpuops(cpus, cpuid);
  200. }
  201. return 0;
  202. }
  203. #endif /* RT_USING_FDT */
  204. /** init cpu with hardcoded infomation or parsing from FDT */
  205. static int _cpus_init(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
  206. {
  207. int retval;
  208. // first setup cpu_ops_tbl and cpuid_to_hwid
  209. if (num_cpus > 0)
  210. retval = _cpus_init_data_hardcoded(num_cpus, cpu_hw_ids, cpu_ops);
  211. else
  212. {
  213. retval = -1;
  214. #ifdef RT_USING_FDT
  215. retval = _cpus_init_data_fdt();
  216. #endif
  217. }
  218. if (retval)
  219. return retval;
  220. // using cpuid_to_hwid and cpu_ops_tbl to call method_init and cpu_init
  221. // assuming that cpuid 0 has already init
  222. for (int i = 1; i < RT_CPUS_NR; i++)
  223. {
  224. if (cpuid_to_hwid(i) == ID_ERROR)
  225. {
  226. LOG_E("Failed to find hardware id of CPU %d", i);
  227. continue;
  228. }
  229. if (cpu_ops_tbl[i] && cpu_ops_tbl[i]->cpu_init)
  230. {
  231. retval = cpu_ops_tbl[i]->cpu_init(i);
  232. CHECK_RETVAL(retval);
  233. }
  234. else
  235. {
  236. LOG_E("Failed to find cpu_init for cpu %d with cpu_ops[%p], cpu_ops->cpu_init[%p]"
  237. , cpuid_to_hwid(i), cpu_ops_tbl[i], cpu_ops_tbl[i] ? cpu_ops_tbl[i]->cpu_init : NULL);
  238. }
  239. }
  240. return 0;
  241. }
  242. static void _boot_secondary(void)
  243. {
  244. for (int i = 1; i < RT_CPUS_NR; i++)
  245. {
  246. int retval = -0xbad0; // mark no support operation
  247. if (cpu_ops_tbl[i] && cpu_ops_tbl[i]->cpu_boot)
  248. retval = cpu_ops_tbl[i]->cpu_boot(i);
  249. if (retval)
  250. {
  251. if (retval == -0xbad0)
  252. LOG_E("No cpu_ops was probed for CPU %d. Try to configure it or use fdt", i);
  253. else
  254. LOG_E("Failed to boot secondary CPU %d, error code %d", i, retval);
  255. } else {
  256. LOG_I("Secondary CPU %d booted", i);
  257. }
  258. }
  259. }
  260. rt_weak void rt_hw_secondary_cpu_up(void)
  261. {
  262. _boot_secondary();
  263. }
  264. /**
  265. * @brief boot cpu with hardcoded data
  266. *
  267. * @param num_cpus number of cpus
  268. * @param cpu_hw_ids each element represents a hwid of cpu[i]
  269. * @param cpu_ops each element represents a pointer to cpu_ops of cpu[i]
  270. * @return int 0 on success,
  271. */
  272. int rt_hw_cpu_boot_secondary(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
  273. {
  274. int retval = 0;
  275. if (num_cpus < 1 || !cpu_hw_ids || !cpu_ops)
  276. return -1;
  277. retval = _cpus_init(num_cpus, cpu_hw_ids, cpu_ops);
  278. CHECK_RETVAL(retval);
  279. return retval;
  280. }
  281. #define CPU_INIT_USING_FDT 0,0,0
  282. /**
  283. * @brief Initialize cpu infomation from fdt
  284. *
  285. * @return int
  286. */
  287. int rt_hw_cpu_init()
  288. {
  289. #ifdef RT_USING_FDT
  290. return _cpus_init(CPU_INIT_USING_FDT);
  291. #else
  292. LOG_E("CPU init failed since RT_USING_FDT was not defined");
  293. return -0xa; /* no fdt support */
  294. #endif /* RT_USING_FDT */
  295. }
  296. rt_weak void rt_hw_secondary_cpu_idle_exec(void)
  297. {
  298. asm volatile("wfe" ::
  299. : "memory", "cc");
  300. }
  301. #endif /*RT_USING_SMP*/
  302. /**
  303. * @addtogroup ARM CPU
  304. */
  305. /*@{*/
  306. const char *rt_hw_cpu_arch(void)
  307. {
  308. return "aarch64";
  309. }
  310. /** shutdown CPU */
  311. void rt_hw_cpu_shutdown(void)
  312. {
  313. rt_uint32_t level;
  314. rt_kprintf("shutdown...\n");
  315. if (system_off)
  316. system_off();
  317. LOG_E("system shutdown failed");
  318. level = rt_hw_interrupt_disable();
  319. while (level)
  320. {
  321. RT_ASSERT(0);
  322. }
  323. }
  324. MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_shutdown, shutdown, shutdown machine);
  325. #ifdef RT_USING_CPU_FFS
  326. /**
  327. * This function finds the first bit set (beginning with the least significant bit)
  328. * in value and return the index of that bit.
  329. *
  330. * Bits are numbered starting at 1 (the least significant bit). A return value of
  331. * zero from any of these functions means that the argument was zero.
  332. *
  333. * @return return the index of the first bit set. If value is 0, then this function
  334. * shall return 0.
  335. */
  336. int __rt_ffs(int value)
  337. {
  338. return __builtin_ffs(value);
  339. }
  340. #endif
  341. /*@}*/