mp_init.c 13 KB

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