cpu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifdef RT_USING_SMP
  21. void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock)
  22. {
  23. lock->slock = 0;
  24. }
  25. #define TICKET_SHIFT 16
  26. void rt_hw_spin_lock(rt_hw_spinlock_t *lock)
  27. {
  28. unsigned int tmp;
  29. struct __arch_tickets lockval, newval;
  30. asm volatile(
  31. /* Atomically increment the next ticket. */
  32. " prfm pstl1strm, %3\n"
  33. "1: ldaxr %w0, %3\n"
  34. " add %w1, %w0, %w5\n"
  35. " stxr %w2, %w1, %3\n"
  36. " cbnz %w2, 1b\n"
  37. /* Did we get the lock? */
  38. " eor %w1, %w0, %w0, ror #16\n"
  39. " cbz %w1, 3f\n"
  40. /*
  41. * No: spin on the owner. Send a local event to avoid missing an
  42. * unlock before the exclusive load.
  43. */
  44. " sevl\n"
  45. "2: wfe\n"
  46. " ldaxrh %w2, %4\n"
  47. " eor %w1, %w2, %w0, lsr #16\n"
  48. " cbnz %w1, 2b\n"
  49. /* We got the lock. Critical section starts here. */
  50. "3:"
  51. : "=&r"(lockval), "=&r"(newval), "=&r"(tmp), "+Q"(*lock)
  52. : "Q"(lock->tickets.owner), "I"(1 << TICKET_SHIFT)
  53. : "memory");
  54. rt_hw_dmb();
  55. }
  56. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock)
  57. {
  58. rt_hw_dmb();
  59. asm volatile(
  60. " stlrh %w1, %0\n"
  61. : "=Q"(lock->tickets.owner)
  62. : "r"(lock->tickets.owner + 1)
  63. : "memory");
  64. }
  65. /**
  66. * cpu_ops_tbl contains cpu_ops_t for each cpu kernel observed,
  67. * given cpu logical id 'i', its cpu_ops_t is 'cpu_ops_tbl[i]'
  68. */
  69. struct cpu_ops_t *cpu_ops_tbl[RT_CPUS_NR];
  70. // _id_to_mpidr is a table translate logical id to mpid, which is a 64-bit value
  71. rt_uint64_t rt_cpu_mpidr_early[RT_CPUS_NR] RT_WEAK = {[0 ... RT_CPUS_NR - 1] = ID_ERROR};
  72. #ifdef RT_USING_FDT
  73. #include "dtb_node.h"
  74. struct dtb_node *_cpu_node[RT_CPUS_NR];
  75. #endif /* RT_USING_FDT */
  76. #define MPIDR_AFF_MASK 0x000000FF00FFFFFFul
  77. #define REPORT_ERR(retval) LOG_E("got error code %d in %s(), %s:%d", (retval), __func__, __FILE__, __LINE__)
  78. #define CHECK_RETVAL(retval) if (retval) {REPORT_ERR(retval);}
  79. static int _cpus_init_data_hardcoded(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
  80. {
  81. // load in cpu_hw_ids in cpuid_to_hwid,
  82. // cpu_ops to cpu_ops_tbl
  83. if (num_cpus > RT_CPUS_NR)
  84. {
  85. LOG_W("num_cpus (%d) greater than RT_CPUS_NR (%d)\n", num_cpus, RT_CPUS_NR);
  86. num_cpus = RT_CPUS_NR;
  87. }
  88. for (int i = 0; i < num_cpus; i++)
  89. {
  90. set_hwid(i, cpu_hw_ids[i]);
  91. cpu_ops_tbl[i] = cpu_ops[i];
  92. }
  93. return 0;
  94. }
  95. #ifdef RT_USING_FDT
  96. /** read ('size' * 4) bytes number from start, big-endian format */
  97. static rt_uint64_t _read_be_number(void *start, int size)
  98. {
  99. rt_uint64_t buf = 0;
  100. for (; size > 0; size--)
  101. buf = (buf << 32) | fdt32_to_cpu(*(uint32_t *)start++);
  102. return buf;
  103. }
  104. /** check device-type of the node, */
  105. static bool _node_is_cpu(struct dtb_node *node)
  106. {
  107. char *device_type = dtb_node_get_dtb_node_property_value(node, "device_type", NULL);
  108. if (device_type)
  109. {
  110. return !strcmp(device_type, "cpu");
  111. }
  112. return false;
  113. }
  114. static int _read_and_set_hwid(struct dtb_node *cpu, int *id_pool, int *pcpuid)
  115. {
  116. // size/address_cells is number of elements in reg array
  117. int size;
  118. static int address_cells, size_cells;
  119. if (!address_cells && !size_cells)
  120. dtb_node_get_dtb_node_cells(cpu, &address_cells, &size_cells);
  121. void *id_start = dtb_node_get_dtb_node_property_value(cpu, "reg", &size);
  122. rt_uint64_t mpid = _read_be_number(id_start, address_cells);
  123. *pcpuid = *id_pool;
  124. *id_pool = *id_pool + 1;
  125. set_hwid(*pcpuid, mpid);
  126. LOG_I("Using MPID 0x%lx as cpu %d", mpid, *pcpuid);
  127. // setting _cpu_node for cpu_init use
  128. _cpu_node[*pcpuid] = cpu;
  129. return 0;
  130. }
  131. static int _read_and_set_cpuops(struct dtb_node *cpu, int cpuid)
  132. {
  133. char *method = dtb_node_get_dtb_node_property_value(cpu, "enable-method", NULL);
  134. if (!method)
  135. {
  136. LOG_E("Cannot read method from cpu node");
  137. return -1;
  138. }
  139. struct cpu_ops_t *cpu_ops;
  140. if (!strcmp(method, cpu_ops_psci.method))
  141. {
  142. cpu_ops = &cpu_ops_psci;
  143. }
  144. else if (!strcmp(method, cpu_ops_spin_tbl.method))
  145. {
  146. cpu_ops = &cpu_ops_spin_tbl;
  147. }
  148. else
  149. {
  150. cpu_ops = RT_NULL;
  151. LOG_E("Not supported cpu_ops: %s", method);
  152. }
  153. cpu_ops_tbl[cpuid] = cpu_ops;
  154. LOG_D("Using boot method [%s] for cpu %d", cpu_ops->method, cpuid);
  155. return 0;
  156. }
  157. static int _cpus_init_data_fdt()
  158. {
  159. // cpuid_to_hwid and cpu_ops_tbl with fdt
  160. void *root = get_dtb_node_head();
  161. int id_pool = 0;
  162. int cpuid;
  163. struct dtb_node *cpus = dtb_node_get_dtb_node_by_path(root, "/cpus");
  164. // for each cpu node (device-type is cpu), read its mpid and set its cpuid_to_hwid
  165. for_each_node_child(cpus)
  166. {
  167. if (!_node_is_cpu(cpus))
  168. {
  169. continue;
  170. }
  171. if (id_pool > RT_CPUS_NR)
  172. {
  173. LOG_W("Reading more cpus from FDT than RT_CPUS_NR"
  174. "\n Parsing will not continue and only %d cpus will be used.", RT_CPUS_NR);
  175. break;
  176. }
  177. _read_and_set_hwid(cpus, &id_pool, &cpuid);
  178. _read_and_set_cpuops(cpus, cpuid);
  179. }
  180. return 0;
  181. }
  182. #endif /* RT_USING_FDT */
  183. /** init cpu with hardcoded infomation or parsing from FDT */
  184. static int _cpus_init(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
  185. {
  186. int retval;
  187. // first setup cpu_ops_tbl and cpuid_to_hwid
  188. if (num_cpus > 0)
  189. retval = _cpus_init_data_hardcoded(num_cpus, cpu_hw_ids, cpu_ops);
  190. else
  191. {
  192. retval = -1;
  193. #ifdef RT_USING_FDT
  194. retval = _cpus_init_data_fdt();
  195. #endif
  196. }
  197. if (retval)
  198. return retval;
  199. // using cpuid_to_hwid and cpu_ops_tbl to call method_init and cpu_init
  200. // assuming that cpuid 0 has already init
  201. for (int i = 1; i < RT_CPUS_NR; i++)
  202. {
  203. if (cpuid_to_hwid(i) == ID_ERROR)
  204. {
  205. LOG_E("Failed to find hardware id of CPU %d", i);
  206. continue;
  207. }
  208. if (cpu_ops_tbl[i] && cpu_ops_tbl[i]->cpu_init)
  209. {
  210. retval = cpu_ops_tbl[i]->cpu_init(i);
  211. CHECK_RETVAL(retval);
  212. }
  213. else
  214. {
  215. LOG_E("Failed to find cpu_init for cpu %d with cpu_ops[%p], cpu_ops->cpu_init[%p]"
  216. , cpuid_to_hwid(i), cpu_ops_tbl[i], cpu_ops_tbl[i] ? cpu_ops_tbl[i]->cpu_init : NULL);
  217. }
  218. }
  219. return 0;
  220. }
  221. static void _boot_secondary(void)
  222. {
  223. for (int i = 1; i < RT_CPUS_NR; i++)
  224. {
  225. int retval = -0xbad0; // mark no support operation
  226. if (cpu_ops_tbl[i] && cpu_ops_tbl[i]->cpu_boot)
  227. retval = cpu_ops_tbl[i]->cpu_boot(i);
  228. if (retval)
  229. {
  230. LOG_E("Failed to boot secondary CPU %d, error code %d", i, retval);
  231. } else {
  232. LOG_I("Secondary CPU %d booted", i);
  233. }
  234. }
  235. }
  236. RT_WEAK void rt_hw_secondary_cpu_up(void)
  237. {
  238. _boot_secondary();
  239. }
  240. /**
  241. * @brief boot cpu with hardcoded data
  242. *
  243. * @param num_cpus number of cpus
  244. * @param cpu_hw_ids each element represents a hwid of cpu[i]
  245. * @param cpu_ops each element represents a pointer to cpu_ops of cpu[i]
  246. * @return int 0 on success,
  247. */
  248. int rt_hw_cpu_boot_secondary(int num_cpus, rt_uint64_t *cpu_hw_ids, struct cpu_ops_t *cpu_ops[])
  249. {
  250. int retval = 0;
  251. if (num_cpus < 1 || !cpu_hw_ids || !cpu_ops)
  252. return -1;
  253. retval = _cpus_init(num_cpus, cpu_hw_ids, cpu_ops);
  254. CHECK_RETVAL(retval);
  255. return retval;
  256. }
  257. #define CPU_INIT_USING_FDT 0,0,0
  258. /**
  259. * @brief Initialize cpu infomation from fdt
  260. *
  261. * @return int
  262. */
  263. int rt_hw_cpu_init()
  264. {
  265. #ifdef RT_USING_FDT
  266. return _cpus_init(CPU_INIT_USING_FDT);
  267. #else
  268. LOG_E("CPU init failed since RT_USING_FDT was not defined");
  269. return -0xa; /* no fdt support */
  270. #endif /* RT_USING_FDT */
  271. }
  272. RT_WEAK void rt_hw_secondary_cpu_idle_exec(void)
  273. {
  274. asm volatile("wfe" ::
  275. : "memory", "cc");
  276. }
  277. #endif /*RT_USING_SMP*/
  278. /**
  279. * @addtogroup ARM CPU
  280. */
  281. /*@{*/
  282. /** shutdown CPU */
  283. void rt_hw_cpu_shutdown()
  284. {
  285. rt_uint32_t level;
  286. rt_kprintf("shutdown...\n");
  287. level = rt_hw_interrupt_disable();
  288. while (level)
  289. {
  290. RT_ASSERT(0);
  291. }
  292. }
  293. /*@}*/