mp_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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/msr.h>
  19. #include <asm/mtrr.h>
  20. #include <asm/processor.h>
  21. #include <asm/sipi.h>
  22. #include <asm/fw_cfg.h>
  23. #include <dm/device-internal.h>
  24. #include <dm/uclass-internal.h>
  25. #include <dm/lists.h>
  26. #include <dm/root.h>
  27. #include <linux/linkage.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. /* Total CPUs include BSP */
  30. static int num_cpus;
  31. /* This also needs to match the sipi.S assembly code for saved MSR encoding */
  32. struct saved_msr {
  33. uint32_t index;
  34. uint32_t lo;
  35. uint32_t hi;
  36. } __packed;
  37. struct mp_flight_plan {
  38. int num_records;
  39. struct mp_flight_record *records;
  40. };
  41. static struct mp_flight_plan mp_info;
  42. struct cpu_map {
  43. struct udevice *dev;
  44. int apic_id;
  45. int err_code;
  46. };
  47. static inline void barrier_wait(atomic_t *b)
  48. {
  49. while (atomic_read(b) == 0)
  50. asm("pause");
  51. mfence();
  52. }
  53. static inline void release_barrier(atomic_t *b)
  54. {
  55. mfence();
  56. atomic_set(b, 1);
  57. }
  58. static inline void stop_this_cpu(void)
  59. {
  60. /* Called by an AP when it is ready to halt and wait for a new task */
  61. for (;;)
  62. cpu_hlt();
  63. }
  64. /* Returns 1 if timeout waiting for APs. 0 if target APs found */
  65. static int wait_for_aps(atomic_t *val, int target, int total_delay,
  66. int delay_step)
  67. {
  68. int timeout = 0;
  69. int delayed = 0;
  70. while (atomic_read(val) != target) {
  71. udelay(delay_step);
  72. delayed += delay_step;
  73. if (delayed >= total_delay) {
  74. timeout = 1;
  75. break;
  76. }
  77. }
  78. return timeout;
  79. }
  80. static void ap_do_flight_plan(struct udevice *cpu)
  81. {
  82. int i;
  83. for (i = 0; i < mp_info.num_records; i++) {
  84. struct mp_flight_record *rec = &mp_info.records[i];
  85. atomic_inc(&rec->cpus_entered);
  86. barrier_wait(&rec->barrier);
  87. if (rec->ap_call != NULL)
  88. rec->ap_call(cpu, rec->ap_arg);
  89. }
  90. }
  91. static int find_cpu_by_apic_id(int apic_id, struct udevice **devp)
  92. {
  93. struct udevice *dev;
  94. *devp = NULL;
  95. for (uclass_find_first_device(UCLASS_CPU, &dev);
  96. dev;
  97. uclass_find_next_device(&dev)) {
  98. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  99. if (plat->cpu_id == apic_id) {
  100. *devp = dev;
  101. return 0;
  102. }
  103. }
  104. return -ENOENT;
  105. }
  106. /*
  107. * By the time APs call ap_init() caching has been setup, and microcode has
  108. * been loaded
  109. */
  110. static void ap_init(unsigned int cpu_index)
  111. {
  112. struct udevice *dev;
  113. int apic_id;
  114. int ret;
  115. /* Ensure the local apic is enabled */
  116. enable_lapic();
  117. apic_id = lapicid();
  118. ret = find_cpu_by_apic_id(apic_id, &dev);
  119. if (ret) {
  120. debug("Unknown CPU apic_id %x\n", apic_id);
  121. goto done;
  122. }
  123. debug("AP: slot %d apic_id %x, dev %s\n", cpu_index, apic_id,
  124. dev ? dev->name : "(apic_id not found)");
  125. /* Walk the flight plan */
  126. ap_do_flight_plan(dev);
  127. /* Park the AP */
  128. debug("parking\n");
  129. done:
  130. stop_this_cpu();
  131. }
  132. static const unsigned int fixed_mtrrs[NUM_FIXED_MTRRS] = {
  133. MTRR_FIX_64K_00000_MSR, MTRR_FIX_16K_80000_MSR, MTRR_FIX_16K_A0000_MSR,
  134. MTRR_FIX_4K_C0000_MSR, MTRR_FIX_4K_C8000_MSR, MTRR_FIX_4K_D0000_MSR,
  135. MTRR_FIX_4K_D8000_MSR, MTRR_FIX_4K_E0000_MSR, MTRR_FIX_4K_E8000_MSR,
  136. MTRR_FIX_4K_F0000_MSR, MTRR_FIX_4K_F8000_MSR,
  137. };
  138. static inline struct saved_msr *save_msr(int index, struct saved_msr *entry)
  139. {
  140. msr_t msr;
  141. msr = msr_read(index);
  142. entry->index = index;
  143. entry->lo = msr.lo;
  144. entry->hi = msr.hi;
  145. /* Return the next entry */
  146. entry++;
  147. return entry;
  148. }
  149. static int save_bsp_msrs(char *start, int size)
  150. {
  151. int msr_count;
  152. int num_var_mtrrs;
  153. struct saved_msr *msr_entry;
  154. int i;
  155. msr_t msr;
  156. /* Determine number of MTRRs need to be saved */
  157. msr = msr_read(MTRR_CAP_MSR);
  158. num_var_mtrrs = msr.lo & 0xff;
  159. /* 2 * num_var_mtrrs for base and mask. +1 for IA32_MTRR_DEF_TYPE */
  160. msr_count = 2 * num_var_mtrrs + NUM_FIXED_MTRRS + 1;
  161. if ((msr_count * sizeof(struct saved_msr)) > size) {
  162. printf("Cannot mirror all %d msrs.\n", msr_count);
  163. return -ENOSPC;
  164. }
  165. msr_entry = (void *)start;
  166. for (i = 0; i < NUM_FIXED_MTRRS; i++)
  167. msr_entry = save_msr(fixed_mtrrs[i], msr_entry);
  168. for (i = 0; i < num_var_mtrrs; i++) {
  169. msr_entry = save_msr(MTRR_PHYS_BASE_MSR(i), msr_entry);
  170. msr_entry = save_msr(MTRR_PHYS_MASK_MSR(i), msr_entry);
  171. }
  172. msr_entry = save_msr(MTRR_DEF_TYPE_MSR, msr_entry);
  173. return msr_count;
  174. }
  175. static int load_sipi_vector(atomic_t **ap_countp, int num_cpus)
  176. {
  177. struct sipi_params_16bit *params16;
  178. struct sipi_params *params;
  179. static char msr_save[512];
  180. char *stack;
  181. ulong addr;
  182. int code_len;
  183. int size;
  184. int ret;
  185. /* Copy in the code */
  186. code_len = ap_start16_code_end - ap_start16;
  187. debug("Copying SIPI code to %x: %d bytes\n", AP_DEFAULT_BASE,
  188. code_len);
  189. memcpy((void *)AP_DEFAULT_BASE, ap_start16, code_len);
  190. addr = AP_DEFAULT_BASE + (ulong)sipi_params_16bit - (ulong)ap_start16;
  191. params16 = (struct sipi_params_16bit *)addr;
  192. params16->ap_start = (uint32_t)ap_start;
  193. params16->gdt = (uint32_t)gd->arch.gdt;
  194. params16->gdt_limit = X86_GDT_SIZE - 1;
  195. debug("gdt = %x, gdt_limit = %x\n", params16->gdt, params16->gdt_limit);
  196. params = (struct sipi_params *)sipi_params;
  197. debug("SIPI 32-bit params at %p\n", params);
  198. params->idt_ptr = (uint32_t)x86_get_idt();
  199. params->stack_size = CONFIG_AP_STACK_SIZE;
  200. size = params->stack_size * num_cpus;
  201. stack = memalign(4096, size);
  202. if (!stack)
  203. return -ENOMEM;
  204. params->stack_top = (u32)(stack + size);
  205. params->microcode_ptr = 0;
  206. params->msr_table_ptr = (u32)msr_save;
  207. ret = save_bsp_msrs(msr_save, sizeof(msr_save));
  208. if (ret < 0)
  209. return ret;
  210. params->msr_count = ret;
  211. params->c_handler = (uint32_t)&ap_init;
  212. *ap_countp = &params->ap_count;
  213. atomic_set(*ap_countp, 0);
  214. debug("SIPI vector is ready\n");
  215. return 0;
  216. }
  217. static int check_cpu_devices(int expected_cpus)
  218. {
  219. int i;
  220. for (i = 0; i < expected_cpus; i++) {
  221. struct udevice *dev;
  222. int ret;
  223. ret = uclass_find_device(UCLASS_CPU, i, &dev);
  224. if (ret) {
  225. debug("Cannot find CPU %d in device tree\n", i);
  226. return ret;
  227. }
  228. }
  229. return 0;
  230. }
  231. /* Returns 1 for timeout. 0 on success */
  232. static int apic_wait_timeout(int total_delay, int delay_step)
  233. {
  234. int total = 0;
  235. int timeout = 0;
  236. while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) {
  237. udelay(delay_step);
  238. total += delay_step;
  239. if (total >= total_delay) {
  240. timeout = 1;
  241. break;
  242. }
  243. }
  244. return timeout;
  245. }
  246. static int start_aps(int ap_count, atomic_t *num_aps)
  247. {
  248. int sipi_vector;
  249. /* Max location is 4KiB below 1MiB */
  250. const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12;
  251. if (ap_count == 0)
  252. return 0;
  253. /* The vector is sent as a 4k aligned address in one byte */
  254. sipi_vector = AP_DEFAULT_BASE >> 12;
  255. if (sipi_vector > max_vector_loc) {
  256. printf("SIPI vector too large! 0x%08x\n",
  257. sipi_vector);
  258. return -1;
  259. }
  260. debug("Attempting to start %d APs\n", ap_count);
  261. if ((lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY)) {
  262. debug("Waiting for ICR not to be busy...");
  263. if (apic_wait_timeout(1000, 50)) {
  264. debug("timed out. Aborting.\n");
  265. return -1;
  266. } else {
  267. debug("done.\n");
  268. }
  269. }
  270. /* Send INIT IPI to all but self */
  271. lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  272. lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  273. LAPIC_DM_INIT);
  274. debug("Waiting for 10ms after sending INIT.\n");
  275. mdelay(10);
  276. /* Send 1st SIPI */
  277. if ((lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY)) {
  278. debug("Waiting for ICR not to be busy...");
  279. if (apic_wait_timeout(1000, 50)) {
  280. debug("timed out. Aborting.\n");
  281. return -1;
  282. } else {
  283. debug("done.\n");
  284. }
  285. }
  286. lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  287. lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  288. LAPIC_DM_STARTUP | sipi_vector);
  289. debug("Waiting for 1st SIPI to complete...");
  290. if (apic_wait_timeout(10000, 50)) {
  291. debug("timed out.\n");
  292. return -1;
  293. } else {
  294. debug("done.\n");
  295. }
  296. /* Wait for CPUs to check in up to 200 us */
  297. wait_for_aps(num_aps, ap_count, 200, 15);
  298. /* Send 2nd SIPI */
  299. if ((lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY)) {
  300. debug("Waiting for ICR not to be busy...");
  301. if (apic_wait_timeout(1000, 50)) {
  302. debug("timed out. Aborting.\n");
  303. return -1;
  304. } else {
  305. debug("done.\n");
  306. }
  307. }
  308. lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  309. lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  310. LAPIC_DM_STARTUP | sipi_vector);
  311. debug("Waiting for 2nd SIPI to complete...");
  312. if (apic_wait_timeout(10000, 50)) {
  313. debug("timed out.\n");
  314. return -1;
  315. } else {
  316. debug("done.\n");
  317. }
  318. /* Wait for CPUs to check in */
  319. if (wait_for_aps(num_aps, ap_count, 10000, 50)) {
  320. debug("Not all APs checked in: %d/%d.\n",
  321. atomic_read(num_aps), ap_count);
  322. return -1;
  323. }
  324. return 0;
  325. }
  326. static int bsp_do_flight_plan(struct udevice *cpu, struct mp_params *mp_params)
  327. {
  328. int i;
  329. int ret = 0;
  330. const int timeout_us = 100000;
  331. const int step_us = 100;
  332. int num_aps = num_cpus - 1;
  333. for (i = 0; i < mp_params->num_records; i++) {
  334. struct mp_flight_record *rec = &mp_params->flight_plan[i];
  335. /* Wait for APs if the record is not released */
  336. if (atomic_read(&rec->barrier) == 0) {
  337. /* Wait for the APs to check in */
  338. if (wait_for_aps(&rec->cpus_entered, num_aps,
  339. timeout_us, step_us)) {
  340. debug("MP record %d timeout.\n", i);
  341. ret = -1;
  342. }
  343. }
  344. if (rec->bsp_call != NULL)
  345. rec->bsp_call(cpu, rec->bsp_arg);
  346. release_barrier(&rec->barrier);
  347. }
  348. return ret;
  349. }
  350. static int init_bsp(struct udevice **devp)
  351. {
  352. char processor_name[CPU_MAX_NAME_LEN];
  353. int apic_id;
  354. int ret;
  355. cpu_get_name(processor_name);
  356. debug("CPU: %s.\n", processor_name);
  357. lapic_setup();
  358. apic_id = lapicid();
  359. ret = find_cpu_by_apic_id(apic_id, devp);
  360. if (ret) {
  361. printf("Cannot find boot CPU, APIC ID %d\n", apic_id);
  362. return ret;
  363. }
  364. return 0;
  365. }
  366. #ifdef CONFIG_QEMU
  367. static int qemu_cpu_fixup(void)
  368. {
  369. int ret;
  370. int cpu_num;
  371. int cpu_online;
  372. struct udevice *dev, *pdev;
  373. struct cpu_platdata *plat;
  374. char *cpu;
  375. /* first we need to find '/cpus' */
  376. for (device_find_first_child(dm_root(), &pdev);
  377. pdev;
  378. device_find_next_child(&pdev)) {
  379. if (!strcmp(pdev->name, "cpus"))
  380. break;
  381. }
  382. if (!pdev) {
  383. printf("unable to find cpus device\n");
  384. return -ENODEV;
  385. }
  386. /* calculate cpus that are already bound */
  387. cpu_num = 0;
  388. for (uclass_find_first_device(UCLASS_CPU, &dev);
  389. dev;
  390. uclass_find_next_device(&dev)) {
  391. cpu_num++;
  392. }
  393. /* get actual cpu number */
  394. cpu_online = qemu_fwcfg_online_cpus();
  395. if (cpu_online < 0) {
  396. printf("unable to get online cpu number: %d\n", cpu_online);
  397. return cpu_online;
  398. }
  399. /* bind addtional cpus */
  400. dev = NULL;
  401. for (; cpu_num < cpu_online; cpu_num++) {
  402. /*
  403. * allocate device name here as device_bind_driver() does
  404. * not copy device name, 8 bytes are enough for
  405. * sizeof("cpu@") + 3 digits cpu number + '\0'
  406. */
  407. cpu = malloc(8);
  408. if (!cpu) {
  409. printf("unable to allocate device name\n");
  410. return -ENOMEM;
  411. }
  412. sprintf(cpu, "cpu@%d", cpu_num);
  413. ret = device_bind_driver(pdev, "cpu_qemu", cpu, &dev);
  414. if (ret) {
  415. printf("binding cpu@%d failed: %d\n", cpu_num, ret);
  416. return ret;
  417. }
  418. plat = dev_get_parent_platdata(dev);
  419. plat->cpu_id = cpu_num;
  420. }
  421. return 0;
  422. }
  423. #endif
  424. int mp_init(struct mp_params *p)
  425. {
  426. int num_aps;
  427. atomic_t *ap_count;
  428. struct udevice *cpu;
  429. int ret;
  430. /* This will cause the CPUs devices to be bound */
  431. struct uclass *uc;
  432. ret = uclass_get(UCLASS_CPU, &uc);
  433. if (ret)
  434. return ret;
  435. #ifdef CONFIG_QEMU
  436. ret = qemu_cpu_fixup();
  437. if (ret)
  438. return ret;
  439. #endif
  440. ret = init_bsp(&cpu);
  441. if (ret) {
  442. debug("Cannot init boot CPU: err=%d\n", ret);
  443. return ret;
  444. }
  445. if (p == NULL || p->flight_plan == NULL || p->num_records < 1) {
  446. printf("Invalid MP parameters\n");
  447. return -1;
  448. }
  449. num_cpus = cpu_get_count(cpu);
  450. if (num_cpus < 0) {
  451. debug("Cannot get number of CPUs: err=%d\n", num_cpus);
  452. return num_cpus;
  453. }
  454. if (num_cpus < 2)
  455. debug("Warning: Only 1 CPU is detected\n");
  456. ret = check_cpu_devices(num_cpus);
  457. if (ret)
  458. debug("Warning: Device tree does not describe all CPUs. Extra ones will not be started correctly\n");
  459. /* Copy needed parameters so that APs have a reference to the plan */
  460. mp_info.num_records = p->num_records;
  461. mp_info.records = p->flight_plan;
  462. /* Load the SIPI vector */
  463. ret = load_sipi_vector(&ap_count, num_cpus);
  464. if (ap_count == NULL)
  465. return -1;
  466. /*
  467. * Make sure SIPI data hits RAM so the APs that come up will see
  468. * the startup code even if the caches are disabled
  469. */
  470. wbinvd();
  471. /* Start the APs providing number of APs and the cpus_entered field */
  472. num_aps = num_cpus - 1;
  473. ret = start_aps(num_aps, ap_count);
  474. if (ret) {
  475. mdelay(1000);
  476. debug("%d/%d eventually checked in?\n", atomic_read(ap_count),
  477. num_aps);
  478. return ret;
  479. }
  480. /* Walk the flight plan for the BSP */
  481. ret = bsp_do_flight_plan(cpu, p);
  482. if (ret) {
  483. debug("CPU init failed: err=%d\n", ret);
  484. return ret;
  485. }
  486. return 0;
  487. }
  488. int mp_init_cpu(struct udevice *cpu, void *unused)
  489. {
  490. /*
  491. * Multiple APs are brought up simultaneously and they may get the same
  492. * seq num in the uclass_resolve_seq() during device_probe(). To avoid
  493. * this, set req_seq to the reg number in the device tree in advance.
  494. */
  495. cpu->req_seq = fdtdec_get_int(gd->fdt_blob, cpu->of_offset, "reg", -1);
  496. return device_probe(cpu);
  497. }