mp_init.c 13 KB

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