mp_init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (C) 2015 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Based on code from the coreboot file of the same name
  7. */
  8. #include <common.h>
  9. #include <cpu.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <malloc.h>
  13. #include <asm/atomic.h>
  14. #include <asm/cpu.h>
  15. #include <asm/interrupt.h>
  16. #include <asm/lapic.h>
  17. #include <asm/mp.h>
  18. #include <asm/mtrr.h>
  19. #include <asm/sipi.h>
  20. #include <dm/device-internal.h>
  21. #include <dm/uclass-internal.h>
  22. #include <linux/linkage.h>
  23. /* This also needs to match the sipi.S assembly code for saved MSR encoding */
  24. struct saved_msr {
  25. uint32_t index;
  26. uint32_t lo;
  27. uint32_t hi;
  28. } __packed;
  29. struct mp_flight_plan {
  30. int num_records;
  31. struct mp_flight_record *records;
  32. };
  33. static struct mp_flight_plan mp_info;
  34. struct cpu_map {
  35. struct udevice *dev;
  36. int apic_id;
  37. int err_code;
  38. };
  39. static inline void barrier_wait(atomic_t *b)
  40. {
  41. while (atomic_read(b) == 0)
  42. asm("pause");
  43. mfence();
  44. }
  45. static inline void release_barrier(atomic_t *b)
  46. {
  47. mfence();
  48. atomic_set(b, 1);
  49. }
  50. /* Returns 1 if timeout waiting for APs. 0 if target APs found */
  51. static int wait_for_aps(atomic_t *val, int target, int total_delay,
  52. int delay_step)
  53. {
  54. int timeout = 0;
  55. int delayed = 0;
  56. while (atomic_read(val) != target) {
  57. udelay(delay_step);
  58. delayed += delay_step;
  59. if (delayed >= total_delay) {
  60. timeout = 1;
  61. break;
  62. }
  63. }
  64. return timeout;
  65. }
  66. static void ap_do_flight_plan(struct udevice *cpu)
  67. {
  68. int i;
  69. for (i = 0; i < mp_info.num_records; i++) {
  70. struct mp_flight_record *rec = &mp_info.records[i];
  71. atomic_inc(&rec->cpus_entered);
  72. barrier_wait(&rec->barrier);
  73. if (rec->ap_call != NULL)
  74. rec->ap_call(cpu, rec->ap_arg);
  75. }
  76. }
  77. static int find_cpu_by_apid_id(int apic_id, struct udevice **devp)
  78. {
  79. struct udevice *dev;
  80. *devp = NULL;
  81. for (uclass_find_first_device(UCLASS_CPU, &dev);
  82. dev;
  83. uclass_find_next_device(&dev)) {
  84. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  85. if (plat->cpu_id == apic_id) {
  86. *devp = dev;
  87. return 0;
  88. }
  89. }
  90. return -ENOENT;
  91. }
  92. /*
  93. * By the time APs call ap_init() caching has been setup, and microcode has
  94. * been loaded
  95. */
  96. static void ap_init(unsigned int cpu_index)
  97. {
  98. struct udevice *dev;
  99. int apic_id;
  100. int ret;
  101. /* Ensure the local apic is enabled */
  102. enable_lapic();
  103. apic_id = lapicid();
  104. ret = find_cpu_by_apid_id(apic_id, &dev);
  105. if (ret) {
  106. debug("Unknown CPU apic_id %x\n", apic_id);
  107. goto done;
  108. }
  109. debug("AP: slot %d apic_id %x, dev %s\n", cpu_index, apic_id,
  110. dev ? dev->name : "(apic_id not found)");
  111. /* Walk the flight plan */
  112. ap_do_flight_plan(dev);
  113. /* Park the AP */
  114. debug("parking\n");
  115. done:
  116. stop_this_cpu();
  117. }
  118. static const unsigned int fixed_mtrrs[NUM_FIXED_MTRRS] = {
  119. MTRR_FIX_64K_00000_MSR, MTRR_FIX_16K_80000_MSR, MTRR_FIX_16K_A0000_MSR,
  120. MTRR_FIX_4K_C0000_MSR, MTRR_FIX_4K_C8000_MSR, MTRR_FIX_4K_D0000_MSR,
  121. MTRR_FIX_4K_D8000_MSR, MTRR_FIX_4K_E0000_MSR, MTRR_FIX_4K_E8000_MSR,
  122. MTRR_FIX_4K_F0000_MSR, MTRR_FIX_4K_F8000_MSR,
  123. };
  124. static inline struct saved_msr *save_msr(int index, struct saved_msr *entry)
  125. {
  126. msr_t msr;
  127. msr = msr_read(index);
  128. entry->index = index;
  129. entry->lo = msr.lo;
  130. entry->hi = msr.hi;
  131. /* Return the next entry */
  132. entry++;
  133. return entry;
  134. }
  135. static int save_bsp_msrs(char *start, int size)
  136. {
  137. int msr_count;
  138. int num_var_mtrrs;
  139. struct saved_msr *msr_entry;
  140. int i;
  141. msr_t msr;
  142. /* Determine number of MTRRs need to be saved */
  143. msr = msr_read(MTRR_CAP_MSR);
  144. num_var_mtrrs = msr.lo & 0xff;
  145. /* 2 * num_var_mtrrs for base and mask. +1 for IA32_MTRR_DEF_TYPE */
  146. msr_count = 2 * num_var_mtrrs + NUM_FIXED_MTRRS + 1;
  147. if ((msr_count * sizeof(struct saved_msr)) > size) {
  148. printf("Cannot mirror all %d msrs.\n", msr_count);
  149. return -ENOSPC;
  150. }
  151. msr_entry = (void *)start;
  152. for (i = 0; i < NUM_FIXED_MTRRS; i++)
  153. msr_entry = save_msr(fixed_mtrrs[i], msr_entry);
  154. for (i = 0; i < num_var_mtrrs; i++) {
  155. msr_entry = save_msr(MTRR_PHYS_BASE_MSR(i), msr_entry);
  156. msr_entry = save_msr(MTRR_PHYS_MASK_MSR(i), msr_entry);
  157. }
  158. msr_entry = save_msr(MTRR_DEF_TYPE_MSR, msr_entry);
  159. return msr_count;
  160. }
  161. static int load_sipi_vector(atomic_t **ap_countp)
  162. {
  163. struct sipi_params_16bit *params16;
  164. struct sipi_params *params;
  165. static char msr_save[512];
  166. char *stack;
  167. ulong addr;
  168. int code_len;
  169. int size;
  170. int ret;
  171. /* Copy in the code */
  172. code_len = ap_start16_code_end - ap_start16;
  173. debug("Copying SIPI code to %x: %d bytes\n", AP_DEFAULT_BASE,
  174. code_len);
  175. memcpy((void *)AP_DEFAULT_BASE, ap_start16, code_len);
  176. addr = AP_DEFAULT_BASE + (ulong)sipi_params_16bit - (ulong)ap_start16;
  177. params16 = (struct sipi_params_16bit *)addr;
  178. params16->ap_start = (uint32_t)ap_start;
  179. params16->gdt = (uint32_t)gd->arch.gdt;
  180. params16->gdt_limit = X86_GDT_SIZE - 1;
  181. debug("gdt = %x, gdt_limit = %x\n", params16->gdt, params16->gdt_limit);
  182. params = (struct sipi_params *)sipi_params;
  183. debug("SIPI 32-bit params at %p\n", params);
  184. params->idt_ptr = (uint32_t)x86_get_idt();
  185. params->stack_size = CONFIG_AP_STACK_SIZE;
  186. size = params->stack_size * CONFIG_MAX_CPUS;
  187. stack = memalign(size, 4096);
  188. if (!stack)
  189. return -ENOMEM;
  190. params->stack_top = (u32)(stack + size);
  191. params->microcode_ptr = 0;
  192. params->msr_table_ptr = (u32)msr_save;
  193. ret = save_bsp_msrs(msr_save, sizeof(msr_save));
  194. if (ret < 0)
  195. return ret;
  196. params->msr_count = ret;
  197. params->c_handler = (uint32_t)&ap_init;
  198. *ap_countp = &params->ap_count;
  199. atomic_set(*ap_countp, 0);
  200. debug("SIPI vector is ready\n");
  201. return 0;
  202. }
  203. static int check_cpu_devices(int expected_cpus)
  204. {
  205. int i;
  206. for (i = 0; i < expected_cpus; i++) {
  207. struct udevice *dev;
  208. int ret;
  209. ret = uclass_find_device(UCLASS_CPU, i, &dev);
  210. if (ret) {
  211. debug("Cannot find CPU %d in device tree\n", i);
  212. return ret;
  213. }
  214. }
  215. return 0;
  216. }
  217. /* Returns 1 for timeout. 0 on success */
  218. static int apic_wait_timeout(int total_delay, int delay_step)
  219. {
  220. int total = 0;
  221. int timeout = 0;
  222. while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) {
  223. udelay(delay_step);
  224. total += delay_step;
  225. if (total >= total_delay) {
  226. timeout = 1;
  227. break;
  228. }
  229. }
  230. return timeout;
  231. }
  232. static int start_aps(int ap_count, atomic_t *num_aps)
  233. {
  234. int sipi_vector;
  235. /* Max location is 4KiB below 1MiB */
  236. const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12;
  237. if (ap_count == 0)
  238. return 0;
  239. /* The vector is sent as a 4k aligned address in one byte */
  240. sipi_vector = AP_DEFAULT_BASE >> 12;
  241. if (sipi_vector > max_vector_loc) {
  242. printf("SIPI vector too large! 0x%08x\n",
  243. sipi_vector);
  244. return -1;
  245. }
  246. debug("Attempting to start %d APs\n", ap_count);
  247. if ((lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY)) {
  248. debug("Waiting for ICR not to be busy...");
  249. if (apic_wait_timeout(1000, 50)) {
  250. debug("timed out. Aborting.\n");
  251. return -1;
  252. } else {
  253. debug("done.\n");
  254. }
  255. }
  256. /* Send INIT IPI to all but self */
  257. lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  258. lapic_write_around(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  259. LAPIC_DM_INIT);
  260. debug("Waiting for 10ms after sending INIT.\n");
  261. mdelay(10);
  262. /* Send 1st SIPI */
  263. if ((lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY)) {
  264. debug("Waiting for ICR not to be busy...");
  265. if (apic_wait_timeout(1000, 50)) {
  266. debug("timed out. Aborting.\n");
  267. return -1;
  268. } else {
  269. debug("done.\n");
  270. }
  271. }
  272. lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  273. lapic_write_around(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  274. LAPIC_DM_STARTUP | sipi_vector);
  275. debug("Waiting for 1st SIPI to complete...");
  276. if (apic_wait_timeout(10000, 50)) {
  277. debug("timed out.\n");
  278. return -1;
  279. } else {
  280. debug("done.\n");
  281. }
  282. /* Wait for CPUs to check in up to 200 us */
  283. wait_for_aps(num_aps, ap_count, 200, 15);
  284. /* Send 2nd SIPI */
  285. if ((lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY)) {
  286. debug("Waiting for ICR not to be busy...");
  287. if (apic_wait_timeout(1000, 50)) {
  288. debug("timed out. Aborting.\n");
  289. return -1;
  290. } else {
  291. debug("done.\n");
  292. }
  293. }
  294. lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  295. lapic_write_around(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  296. LAPIC_DM_STARTUP | sipi_vector);
  297. debug("Waiting for 2nd SIPI to complete...");
  298. if (apic_wait_timeout(10000, 50)) {
  299. debug("timed out.\n");
  300. return -1;
  301. } else {
  302. debug("done.\n");
  303. }
  304. /* Wait for CPUs to check in */
  305. if (wait_for_aps(num_aps, ap_count, 10000, 50)) {
  306. debug("Not all APs checked in: %d/%d.\n",
  307. atomic_read(num_aps), ap_count);
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. static int bsp_do_flight_plan(struct udevice *cpu, struct mp_params *mp_params)
  313. {
  314. int i;
  315. int ret = 0;
  316. const int timeout_us = 100000;
  317. const int step_us = 100;
  318. int num_aps = mp_params->num_cpus - 1;
  319. for (i = 0; i < mp_params->num_records; i++) {
  320. struct mp_flight_record *rec = &mp_params->flight_plan[i];
  321. /* Wait for APs if the record is not released */
  322. if (atomic_read(&rec->barrier) == 0) {
  323. /* Wait for the APs to check in */
  324. if (wait_for_aps(&rec->cpus_entered, num_aps,
  325. timeout_us, step_us)) {
  326. debug("MP record %d timeout.\n", i);
  327. ret = -1;
  328. }
  329. }
  330. if (rec->bsp_call != NULL)
  331. rec->bsp_call(cpu, rec->bsp_arg);
  332. release_barrier(&rec->barrier);
  333. }
  334. return ret;
  335. }
  336. static int init_bsp(struct udevice **devp)
  337. {
  338. char processor_name[CPU_MAX_NAME_LEN];
  339. int apic_id;
  340. int ret;
  341. cpu_get_name(processor_name);
  342. debug("CPU: %s.\n", processor_name);
  343. enable_lapic();
  344. apic_id = lapicid();
  345. ret = find_cpu_by_apid_id(apic_id, devp);
  346. if (ret) {
  347. printf("Cannot find boot CPU, APIC ID %d\n", apic_id);
  348. return ret;
  349. }
  350. return 0;
  351. }
  352. int mp_init(struct mp_params *p)
  353. {
  354. int num_aps;
  355. atomic_t *ap_count;
  356. struct udevice *cpu;
  357. int ret;
  358. /* This will cause the CPUs devices to be bound */
  359. struct uclass *uc;
  360. ret = uclass_get(UCLASS_CPU, &uc);
  361. if (ret)
  362. return ret;
  363. ret = init_bsp(&cpu);
  364. if (ret) {
  365. debug("Cannot init boot CPU: err=%d\n", ret);
  366. return ret;
  367. }
  368. if (p == NULL || p->flight_plan == NULL || p->num_records < 1) {
  369. printf("Invalid MP parameters\n");
  370. return -1;
  371. }
  372. ret = check_cpu_devices(p->num_cpus);
  373. if (ret)
  374. debug("Warning: Device tree does not describe all CPUs. Extra ones will not be started correctly\n");
  375. /* Copy needed parameters so that APs have a reference to the plan */
  376. mp_info.num_records = p->num_records;
  377. mp_info.records = p->flight_plan;
  378. /* Load the SIPI vector */
  379. ret = load_sipi_vector(&ap_count);
  380. if (ap_count == NULL)
  381. return -1;
  382. /*
  383. * Make sure SIPI data hits RAM so the APs that come up will see
  384. * the startup code even if the caches are disabled
  385. */
  386. wbinvd();
  387. /* Start the APs providing number of APs and the cpus_entered field */
  388. num_aps = p->num_cpus - 1;
  389. ret = start_aps(num_aps, ap_count);
  390. if (ret) {
  391. mdelay(1000);
  392. debug("%d/%d eventually checked in?\n", atomic_read(ap_count),
  393. num_aps);
  394. return ret;
  395. }
  396. /* Walk the flight plan for the BSP */
  397. ret = bsp_do_flight_plan(cpu, p);
  398. if (ret) {
  399. debug("CPU init failed: err=%d\n", ret);
  400. return ret;
  401. }
  402. return 0;
  403. }
  404. int mp_init_cpu(struct udevice *cpu, void *unused)
  405. {
  406. return device_probe(cpu);
  407. }