mp_init.c 11 KB

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