mp_init.c 11 KB

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