mp_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. !defined(CONFIG_INTEL_MID)
  208. params->microcode_ptr = ucode_base;
  209. debug("Microcode at %x\n", params->microcode_ptr);
  210. #endif
  211. params->msr_table_ptr = (u32)msr_save;
  212. ret = save_bsp_msrs(msr_save, sizeof(msr_save));
  213. if (ret < 0)
  214. return ret;
  215. params->msr_count = ret;
  216. params->c_handler = (uint32_t)&ap_init;
  217. *ap_countp = &params->ap_count;
  218. atomic_set(*ap_countp, 0);
  219. debug("SIPI vector is ready\n");
  220. return 0;
  221. }
  222. static int check_cpu_devices(int expected_cpus)
  223. {
  224. int i;
  225. for (i = 0; i < expected_cpus; i++) {
  226. struct udevice *dev;
  227. int ret;
  228. ret = uclass_find_device(UCLASS_CPU, i, &dev);
  229. if (ret) {
  230. debug("Cannot find CPU %d in device tree\n", i);
  231. return ret;
  232. }
  233. }
  234. return 0;
  235. }
  236. /* Returns 1 for timeout. 0 on success */
  237. static int apic_wait_timeout(int total_delay, const char *msg)
  238. {
  239. int total = 0;
  240. if (!(lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY))
  241. return 0;
  242. debug("Waiting for %s...", msg);
  243. while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY) {
  244. udelay(50);
  245. total += 50;
  246. if (total >= total_delay) {
  247. debug("timed out: aborting\n");
  248. return -ETIMEDOUT;
  249. }
  250. }
  251. debug("done\n");
  252. return 0;
  253. }
  254. static int start_aps(int ap_count, atomic_t *num_aps)
  255. {
  256. int sipi_vector;
  257. /* Max location is 4KiB below 1MiB */
  258. const int max_vector_loc = ((1 << 20) - (1 << 12)) >> 12;
  259. if (ap_count == 0)
  260. return 0;
  261. /* The vector is sent as a 4k aligned address in one byte */
  262. sipi_vector = AP_DEFAULT_BASE >> 12;
  263. if (sipi_vector > max_vector_loc) {
  264. printf("SIPI vector too large! 0x%08x\n",
  265. sipi_vector);
  266. return -1;
  267. }
  268. debug("Attempting to start %d APs\n", ap_count);
  269. if (apic_wait_timeout(1000, "ICR not to be busy"))
  270. return -ETIMEDOUT;
  271. /* Send INIT IPI to all but self */
  272. lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  273. lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  274. LAPIC_DM_INIT);
  275. debug("Waiting for 10ms after sending INIT\n");
  276. mdelay(10);
  277. /* Send 1st SIPI */
  278. if (apic_wait_timeout(1000, "ICR not to be busy"))
  279. return -ETIMEDOUT;
  280. lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  281. lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  282. LAPIC_DM_STARTUP | sipi_vector);
  283. if (apic_wait_timeout(10000, "first SIPI to complete"))
  284. return -ETIMEDOUT;
  285. /* Wait for CPUs to check in up to 200 us */
  286. wait_for_aps(num_aps, ap_count, 200, 15);
  287. /* Send 2nd SIPI */
  288. if (apic_wait_timeout(1000, "ICR not to be busy"))
  289. return -ETIMEDOUT;
  290. lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(0));
  291. lapic_write(LAPIC_ICR, LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT |
  292. LAPIC_DM_STARTUP | sipi_vector);
  293. if (apic_wait_timeout(10000, "second SIPI to complete"))
  294. return -ETIMEDOUT;
  295. /* Wait for CPUs to check in */
  296. if (wait_for_aps(num_aps, ap_count, 10000, 50)) {
  297. debug("Not all APs checked in: %d/%d\n",
  298. atomic_read(num_aps), ap_count);
  299. return -1;
  300. }
  301. return 0;
  302. }
  303. static int bsp_do_flight_plan(struct udevice *cpu, struct mp_params *mp_params)
  304. {
  305. int i;
  306. int ret = 0;
  307. const int timeout_us = 100000;
  308. const int step_us = 100;
  309. int num_aps = num_cpus - 1;
  310. for (i = 0; i < mp_params->num_records; i++) {
  311. struct mp_flight_record *rec = &mp_params->flight_plan[i];
  312. /* Wait for APs if the record is not released */
  313. if (atomic_read(&rec->barrier) == 0) {
  314. /* Wait for the APs to check in */
  315. if (wait_for_aps(&rec->cpus_entered, num_aps,
  316. timeout_us, step_us)) {
  317. debug("MP record %d timeout\n", i);
  318. ret = -1;
  319. }
  320. }
  321. if (rec->bsp_call != NULL)
  322. rec->bsp_call(cpu, rec->bsp_arg);
  323. release_barrier(&rec->barrier);
  324. }
  325. return ret;
  326. }
  327. static int init_bsp(struct udevice **devp)
  328. {
  329. char processor_name[CPU_MAX_NAME_LEN];
  330. int apic_id;
  331. int ret;
  332. cpu_get_name(processor_name);
  333. debug("CPU: %s\n", processor_name);
  334. apic_id = lapicid();
  335. ret = find_cpu_by_apic_id(apic_id, devp);
  336. if (ret) {
  337. printf("Cannot find boot CPU, APIC ID %d\n", apic_id);
  338. return ret;
  339. }
  340. return 0;
  341. }
  342. #ifdef CONFIG_QFW
  343. static int qemu_cpu_fixup(void)
  344. {
  345. int ret;
  346. int cpu_num;
  347. int cpu_online;
  348. struct udevice *dev, *pdev;
  349. struct cpu_platdata *plat;
  350. char *cpu;
  351. /* first we need to find '/cpus' */
  352. for (device_find_first_child(dm_root(), &pdev);
  353. pdev;
  354. device_find_next_child(&pdev)) {
  355. if (!strcmp(pdev->name, "cpus"))
  356. break;
  357. }
  358. if (!pdev) {
  359. printf("unable to find cpus device\n");
  360. return -ENODEV;
  361. }
  362. /* calculate cpus that are already bound */
  363. cpu_num = 0;
  364. for (uclass_find_first_device(UCLASS_CPU, &dev);
  365. dev;
  366. uclass_find_next_device(&dev)) {
  367. cpu_num++;
  368. }
  369. /* get actual cpu number */
  370. cpu_online = qemu_fwcfg_online_cpus();
  371. if (cpu_online < 0) {
  372. printf("unable to get online cpu number: %d\n", cpu_online);
  373. return cpu_online;
  374. }
  375. /* bind addtional cpus */
  376. dev = NULL;
  377. for (; cpu_num < cpu_online; cpu_num++) {
  378. /*
  379. * allocate device name here as device_bind_driver() does
  380. * not copy device name, 8 bytes are enough for
  381. * sizeof("cpu@") + 3 digits cpu number + '\0'
  382. */
  383. cpu = malloc(8);
  384. if (!cpu) {
  385. printf("unable to allocate device name\n");
  386. return -ENOMEM;
  387. }
  388. sprintf(cpu, "cpu@%d", cpu_num);
  389. ret = device_bind_driver(pdev, "cpu_qemu", cpu, &dev);
  390. if (ret) {
  391. printf("binding cpu@%d failed: %d\n", cpu_num, ret);
  392. return ret;
  393. }
  394. plat = dev_get_parent_platdata(dev);
  395. plat->cpu_id = cpu_num;
  396. }
  397. return 0;
  398. }
  399. #endif
  400. int mp_init(struct mp_params *p)
  401. {
  402. int num_aps;
  403. atomic_t *ap_count;
  404. struct udevice *cpu;
  405. int ret;
  406. /* This will cause the CPUs devices to be bound */
  407. struct uclass *uc;
  408. ret = uclass_get(UCLASS_CPU, &uc);
  409. if (ret)
  410. return ret;
  411. #ifdef CONFIG_QFW
  412. ret = qemu_cpu_fixup();
  413. if (ret)
  414. return ret;
  415. #endif
  416. ret = init_bsp(&cpu);
  417. if (ret) {
  418. debug("Cannot init boot CPU: err=%d\n", ret);
  419. return ret;
  420. }
  421. if (p == NULL || p->flight_plan == NULL || p->num_records < 1) {
  422. printf("Invalid MP parameters\n");
  423. return -1;
  424. }
  425. num_cpus = cpu_get_count(cpu);
  426. if (num_cpus < 0) {
  427. debug("Cannot get number of CPUs: err=%d\n", num_cpus);
  428. return num_cpus;
  429. }
  430. if (num_cpus < 2)
  431. debug("Warning: Only 1 CPU is detected\n");
  432. ret = check_cpu_devices(num_cpus);
  433. if (ret)
  434. debug("Warning: Device tree does not describe all CPUs. Extra ones will not be started correctly\n");
  435. /* Copy needed parameters so that APs have a reference to the plan */
  436. mp_info.num_records = p->num_records;
  437. mp_info.records = p->flight_plan;
  438. /* Load the SIPI vector */
  439. ret = load_sipi_vector(&ap_count, num_cpus);
  440. if (ap_count == NULL)
  441. return -1;
  442. /*
  443. * Make sure SIPI data hits RAM so the APs that come up will see
  444. * the startup code even if the caches are disabled
  445. */
  446. wbinvd();
  447. /* Start the APs providing number of APs and the cpus_entered field */
  448. num_aps = num_cpus - 1;
  449. ret = start_aps(num_aps, ap_count);
  450. if (ret) {
  451. mdelay(1000);
  452. debug("%d/%d eventually checked in?\n", atomic_read(ap_count),
  453. num_aps);
  454. return ret;
  455. }
  456. /* Walk the flight plan for the BSP */
  457. ret = bsp_do_flight_plan(cpu, p);
  458. if (ret) {
  459. debug("CPU init failed: err=%d\n", ret);
  460. return ret;
  461. }
  462. return 0;
  463. }
  464. int mp_init_cpu(struct udevice *cpu, void *unused)
  465. {
  466. struct cpu_platdata *plat = dev_get_parent_platdata(cpu);
  467. /*
  468. * Multiple APs are brought up simultaneously and they may get the same
  469. * seq num in the uclass_resolve_seq() during device_probe(). To avoid
  470. * this, set req_seq to the reg number in the device tree in advance.
  471. */
  472. cpu->req_seq = fdtdec_get_int(gd->fdt_blob, dev_of_offset(cpu), "reg",
  473. -1);
  474. plat->ucode_version = microcode_read_rev();
  475. plat->device_id = gd->arch.x86_device;
  476. return device_probe(cpu);
  477. }