mpparse.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel Multiprocessor Specification 1.1 and 1.4
  4. * compliant MP-table parsing routines.
  5. *
  6. * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
  7. * (c) 1998, 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
  8. * (c) 2008 Alexey Starikovskiy <astarikovskiy@suse.de>
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/memblock.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/mc146818rtc.h>
  16. #include <linux/bitops.h>
  17. #include <linux/acpi.h>
  18. #include <linux/smp.h>
  19. #include <linux/pci.h>
  20. #include <asm/i8259.h>
  21. #include <asm/io_apic.h>
  22. #include <asm/acpi.h>
  23. #include <asm/irqdomain.h>
  24. #include <asm/mtrr.h>
  25. #include <asm/mpspec.h>
  26. #include <asm/proto.h>
  27. #include <asm/bios_ebda.h>
  28. #include <asm/e820/api.h>
  29. #include <asm/setup.h>
  30. #include <asm/smp.h>
  31. #include <asm/apic.h>
  32. /*
  33. * Checksum an MP configuration block.
  34. */
  35. static unsigned int num_procs __initdata;
  36. static int __init mpf_checksum(unsigned char *mp, int len)
  37. {
  38. int sum = 0;
  39. while (len--)
  40. sum += *mp++;
  41. return sum & 0xFF;
  42. }
  43. static void __init MP_processor_info(struct mpc_cpu *m)
  44. {
  45. char *bootup_cpu = "";
  46. topology_register_apic(m->apicid, CPU_ACPIID_INVALID, m->cpuflag & CPU_ENABLED);
  47. if (!(m->cpuflag & CPU_ENABLED))
  48. return;
  49. if (m->cpuflag & CPU_BOOTPROCESSOR)
  50. bootup_cpu = " (Bootup-CPU)";
  51. pr_info("Processor #%d%s\n", m->apicid, bootup_cpu);
  52. num_procs++;
  53. }
  54. #ifdef CONFIG_X86_IO_APIC
  55. static void __init mpc_oem_bus_info(struct mpc_bus *m, char *str)
  56. {
  57. memcpy(str, m->bustype, 6);
  58. str[6] = 0;
  59. apic_pr_verbose("Bus #%d is %s\n", m->busid, str);
  60. }
  61. static void __init MP_bus_info(struct mpc_bus *m)
  62. {
  63. char str[7];
  64. mpc_oem_bus_info(m, str);
  65. #if MAX_MP_BUSSES < 256
  66. if (m->busid >= MAX_MP_BUSSES) {
  67. pr_warn("MP table busid value (%d) for bustype %s is too large, max. supported is %d\n",
  68. m->busid, str, MAX_MP_BUSSES - 1);
  69. return;
  70. }
  71. #endif
  72. set_bit(m->busid, mp_bus_not_pci);
  73. if (strncmp(str, BUSTYPE_ISA, sizeof(BUSTYPE_ISA) - 1) == 0) {
  74. #ifdef CONFIG_EISA
  75. mp_bus_id_to_type[m->busid] = MP_BUS_ISA;
  76. #endif
  77. } else if (strncmp(str, BUSTYPE_PCI, sizeof(BUSTYPE_PCI) - 1) == 0) {
  78. clear_bit(m->busid, mp_bus_not_pci);
  79. #ifdef CONFIG_EISA
  80. mp_bus_id_to_type[m->busid] = MP_BUS_PCI;
  81. } else if (strncmp(str, BUSTYPE_EISA, sizeof(BUSTYPE_EISA) - 1) == 0) {
  82. mp_bus_id_to_type[m->busid] = MP_BUS_EISA;
  83. #endif
  84. } else
  85. pr_warn("Unknown bustype %s - ignoring\n", str);
  86. }
  87. static void __init MP_ioapic_info(struct mpc_ioapic *m)
  88. {
  89. struct ioapic_domain_cfg cfg = {
  90. .type = IOAPIC_DOMAIN_LEGACY,
  91. .ops = &mp_ioapic_irqdomain_ops,
  92. };
  93. if (m->flags & MPC_APIC_USABLE)
  94. mp_register_ioapic(m->apicid, m->apicaddr, gsi_top, &cfg);
  95. }
  96. static void __init print_mp_irq_info(struct mpc_intsrc *mp_irq)
  97. {
  98. apic_printk(APIC_VERBOSE,
  99. "Int: type %d, pol %d, trig %d, bus %02x, IRQ %02x, APIC ID %x, APIC INT %02x\n",
  100. mp_irq->irqtype, mp_irq->irqflag & 3,
  101. (mp_irq->irqflag >> 2) & 3, mp_irq->srcbus,
  102. mp_irq->srcbusirq, mp_irq->dstapic, mp_irq->dstirq);
  103. }
  104. #else /* CONFIG_X86_IO_APIC */
  105. static inline void __init MP_bus_info(struct mpc_bus *m) {}
  106. static inline void __init MP_ioapic_info(struct mpc_ioapic *m) {}
  107. #endif /* CONFIG_X86_IO_APIC */
  108. static void __init MP_lintsrc_info(struct mpc_lintsrc *m)
  109. {
  110. apic_printk(APIC_VERBOSE,
  111. "Lint: type %d, pol %d, trig %d, bus %02x, IRQ %02x, APIC ID %x, APIC LINT %02x\n",
  112. m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbusid,
  113. m->srcbusirq, m->destapic, m->destapiclint);
  114. }
  115. /*
  116. * Read/parse the MPC
  117. */
  118. static int __init smp_check_mpc(struct mpc_table *mpc, char *oem, char *str)
  119. {
  120. if (memcmp(mpc->signature, MPC_SIGNATURE, 4)) {
  121. pr_err("MPTABLE: bad signature [%c%c%c%c]!\n",
  122. mpc->signature[0], mpc->signature[1],
  123. mpc->signature[2], mpc->signature[3]);
  124. return 0;
  125. }
  126. if (mpf_checksum((unsigned char *)mpc, mpc->length)) {
  127. pr_err("MPTABLE: checksum error!\n");
  128. return 0;
  129. }
  130. if (mpc->spec != 0x01 && mpc->spec != 0x04) {
  131. pr_err("MPTABLE: bad table version (%d)!!\n", mpc->spec);
  132. return 0;
  133. }
  134. if (!mpc->lapic) {
  135. pr_err("MPTABLE: null local APIC address!\n");
  136. return 0;
  137. }
  138. memcpy(oem, mpc->oem, 8);
  139. oem[8] = 0;
  140. pr_info("MPTABLE: OEM ID: %s\n", oem);
  141. memcpy(str, mpc->productid, 12);
  142. str[12] = 0;
  143. pr_info("MPTABLE: Product ID: %s\n", str);
  144. pr_info("MPTABLE: APIC at: 0x%X\n", mpc->lapic);
  145. return 1;
  146. }
  147. static void skip_entry(unsigned char **ptr, int *count, int size)
  148. {
  149. *ptr += size;
  150. *count += size;
  151. }
  152. static void __init smp_dump_mptable(struct mpc_table *mpc, unsigned char *mpt)
  153. {
  154. pr_err("Your mptable is wrong, contact your HW vendor!\n");
  155. pr_cont("type %x\n", *mpt);
  156. print_hex_dump(KERN_ERR, " ", DUMP_PREFIX_ADDRESS, 16,
  157. 1, mpc, mpc->length, 1);
  158. }
  159. static int __init smp_read_mpc(struct mpc_table *mpc, unsigned early)
  160. {
  161. char str[16];
  162. char oem[10];
  163. int count = sizeof(*mpc);
  164. unsigned char *mpt = ((unsigned char *)mpc) + count;
  165. if (!smp_check_mpc(mpc, oem, str))
  166. return 0;
  167. if (early) {
  168. /* Initialize the lapic mapping */
  169. if (!acpi_lapic)
  170. register_lapic_address(mpc->lapic);
  171. return 1;
  172. }
  173. /* Now process the configuration blocks. */
  174. while (count < mpc->length) {
  175. switch (*mpt) {
  176. case MP_PROCESSOR:
  177. /* ACPI may have already provided this data */
  178. if (!acpi_lapic)
  179. MP_processor_info((struct mpc_cpu *)mpt);
  180. skip_entry(&mpt, &count, sizeof(struct mpc_cpu));
  181. break;
  182. case MP_BUS:
  183. MP_bus_info((struct mpc_bus *)mpt);
  184. skip_entry(&mpt, &count, sizeof(struct mpc_bus));
  185. break;
  186. case MP_IOAPIC:
  187. MP_ioapic_info((struct mpc_ioapic *)mpt);
  188. skip_entry(&mpt, &count, sizeof(struct mpc_ioapic));
  189. break;
  190. case MP_INTSRC:
  191. mp_save_irq((struct mpc_intsrc *)mpt);
  192. skip_entry(&mpt, &count, sizeof(struct mpc_intsrc));
  193. break;
  194. case MP_LINTSRC:
  195. MP_lintsrc_info((struct mpc_lintsrc *)mpt);
  196. skip_entry(&mpt, &count, sizeof(struct mpc_lintsrc));
  197. break;
  198. default:
  199. /* wrong mptable */
  200. smp_dump_mptable(mpc, mpt);
  201. count = mpc->length;
  202. break;
  203. }
  204. }
  205. if (!num_procs && !acpi_lapic)
  206. pr_err("MPTABLE: no processors registered!\n");
  207. return num_procs || acpi_lapic;
  208. }
  209. #ifdef CONFIG_X86_IO_APIC
  210. static int __init ELCR_trigger(unsigned int irq)
  211. {
  212. unsigned int port;
  213. port = PIC_ELCR1 + (irq >> 3);
  214. return (inb(port) >> (irq & 7)) & 1;
  215. }
  216. static void __init construct_default_ioirq_mptable(int mpc_default_type)
  217. {
  218. struct mpc_intsrc intsrc;
  219. int i;
  220. int ELCR_fallback = 0;
  221. intsrc.type = MP_INTSRC;
  222. intsrc.irqflag = MP_IRQTRIG_DEFAULT | MP_IRQPOL_DEFAULT;
  223. intsrc.srcbus = 0;
  224. intsrc.dstapic = mpc_ioapic_id(0);
  225. intsrc.irqtype = mp_INT;
  226. /*
  227. * If true, we have an ISA/PCI system with no IRQ entries
  228. * in the MP table. To prevent the PCI interrupts from being set up
  229. * incorrectly, we try to use the ELCR. The sanity check to see if
  230. * there is good ELCR data is very simple - IRQ0, 1, 2 and 13 can
  231. * never be level sensitive, so we simply see if the ELCR agrees.
  232. * If it does, we assume it's valid.
  233. */
  234. if (mpc_default_type == 5) {
  235. pr_info("ISA/PCI bus type with no IRQ information... falling back to ELCR\n");
  236. if (ELCR_trigger(0) || ELCR_trigger(1) || ELCR_trigger(2) ||
  237. ELCR_trigger(13))
  238. pr_err("ELCR contains invalid data... not using ELCR\n");
  239. else {
  240. pr_info("Using ELCR to identify PCI interrupts\n");
  241. ELCR_fallback = 1;
  242. }
  243. }
  244. for (i = 0; i < 16; i++) {
  245. switch (mpc_default_type) {
  246. case 2:
  247. if (i == 0 || i == 13)
  248. continue; /* IRQ0 & IRQ13 not connected */
  249. fallthrough;
  250. default:
  251. if (i == 2)
  252. continue; /* IRQ2 is never connected */
  253. }
  254. if (ELCR_fallback) {
  255. /*
  256. * If the ELCR indicates a level-sensitive interrupt, we
  257. * copy that information over to the MP table in the
  258. * irqflag field (level sensitive, active high polarity).
  259. */
  260. if (ELCR_trigger(i)) {
  261. intsrc.irqflag = MP_IRQTRIG_LEVEL |
  262. MP_IRQPOL_ACTIVE_HIGH;
  263. } else {
  264. intsrc.irqflag = MP_IRQTRIG_DEFAULT |
  265. MP_IRQPOL_DEFAULT;
  266. }
  267. }
  268. intsrc.srcbusirq = i;
  269. intsrc.dstirq = i ? i : 2; /* IRQ0 to INTIN2 */
  270. mp_save_irq(&intsrc);
  271. }
  272. intsrc.irqtype = mp_ExtINT;
  273. intsrc.srcbusirq = 0;
  274. intsrc.dstirq = 0; /* 8259A to INTIN0 */
  275. mp_save_irq(&intsrc);
  276. }
  277. static void __init construct_ioapic_table(int mpc_default_type)
  278. {
  279. struct mpc_ioapic ioapic;
  280. struct mpc_bus bus;
  281. bus.type = MP_BUS;
  282. bus.busid = 0;
  283. switch (mpc_default_type) {
  284. default:
  285. pr_err("???\nUnknown standard configuration %d\n",
  286. mpc_default_type);
  287. fallthrough;
  288. case 1:
  289. case 5:
  290. memcpy(bus.bustype, "ISA ", 6);
  291. break;
  292. case 2:
  293. case 6:
  294. case 3:
  295. memcpy(bus.bustype, "EISA ", 6);
  296. break;
  297. }
  298. MP_bus_info(&bus);
  299. if (mpc_default_type > 4) {
  300. bus.busid = 1;
  301. memcpy(bus.bustype, "PCI ", 6);
  302. MP_bus_info(&bus);
  303. }
  304. ioapic.type = MP_IOAPIC;
  305. ioapic.apicid = 2;
  306. ioapic.apicver = mpc_default_type > 4 ? 0x10 : 0x01;
  307. ioapic.flags = MPC_APIC_USABLE;
  308. ioapic.apicaddr = IO_APIC_DEFAULT_PHYS_BASE;
  309. MP_ioapic_info(&ioapic);
  310. /*
  311. * We set up most of the low 16 IO-APIC pins according to MPS rules.
  312. */
  313. construct_default_ioirq_mptable(mpc_default_type);
  314. }
  315. #else
  316. static inline void __init construct_ioapic_table(int mpc_default_type) { }
  317. #endif
  318. static inline void __init construct_default_ISA_mptable(int mpc_default_type)
  319. {
  320. struct mpc_cpu processor;
  321. struct mpc_lintsrc lintsrc;
  322. int linttypes[2] = { mp_ExtINT, mp_NMI };
  323. int i;
  324. /*
  325. * 2 CPUs, numbered 0 & 1.
  326. */
  327. processor.type = MP_PROCESSOR;
  328. /* Either an integrated APIC or a discrete 82489DX. */
  329. processor.apicver = mpc_default_type > 4 ? 0x10 : 0x01;
  330. processor.cpuflag = CPU_ENABLED;
  331. processor.cpufeature = (boot_cpu_data.x86 << 8) |
  332. (boot_cpu_data.x86_model << 4) | boot_cpu_data.x86_stepping;
  333. processor.featureflag = boot_cpu_data.x86_capability[CPUID_1_EDX];
  334. processor.reserved[0] = 0;
  335. processor.reserved[1] = 0;
  336. for (i = 0; i < 2; i++) {
  337. processor.apicid = i;
  338. MP_processor_info(&processor);
  339. }
  340. construct_ioapic_table(mpc_default_type);
  341. lintsrc.type = MP_LINTSRC;
  342. lintsrc.irqflag = MP_IRQTRIG_DEFAULT | MP_IRQPOL_DEFAULT;
  343. lintsrc.srcbusid = 0;
  344. lintsrc.srcbusirq = 0;
  345. lintsrc.destapic = MP_APIC_ALL;
  346. for (i = 0; i < 2; i++) {
  347. lintsrc.irqtype = linttypes[i];
  348. lintsrc.destapiclint = i;
  349. MP_lintsrc_info(&lintsrc);
  350. }
  351. }
  352. static unsigned long mpf_base;
  353. static bool mpf_found;
  354. static unsigned long __init get_mpc_size(unsigned long physptr)
  355. {
  356. struct mpc_table *mpc;
  357. unsigned long size;
  358. mpc = early_memremap(physptr, PAGE_SIZE);
  359. size = mpc->length;
  360. early_memunmap(mpc, PAGE_SIZE);
  361. apic_pr_verbose(" mpc: %lx-%lx\n", physptr, physptr + size);
  362. return size;
  363. }
  364. static int __init check_physptr(struct mpf_intel *mpf, unsigned int early)
  365. {
  366. struct mpc_table *mpc;
  367. unsigned long size;
  368. size = get_mpc_size(mpf->physptr);
  369. mpc = early_memremap(mpf->physptr, size);
  370. /*
  371. * Read the physical hardware table. Anything here will
  372. * override the defaults.
  373. */
  374. if (!smp_read_mpc(mpc, early)) {
  375. #ifdef CONFIG_X86_LOCAL_APIC
  376. smp_found_config = 0;
  377. #endif
  378. pr_err("BIOS bug, MP table errors detected!...\n");
  379. pr_cont("... disabling SMP support. (tell your hw vendor)\n");
  380. early_memunmap(mpc, size);
  381. return -1;
  382. }
  383. early_memunmap(mpc, size);
  384. if (early)
  385. return -1;
  386. #ifdef CONFIG_X86_IO_APIC
  387. /*
  388. * If there are no explicit MP IRQ entries, then we are
  389. * broken. We set up most of the low 16 IO-APIC pins to
  390. * ISA defaults and hope it will work.
  391. */
  392. if (!mp_irq_entries) {
  393. struct mpc_bus bus;
  394. pr_err("BIOS bug, no explicit IRQ entries, using default mptable. (tell your hw vendor)\n");
  395. bus.type = MP_BUS;
  396. bus.busid = 0;
  397. memcpy(bus.bustype, "ISA ", 6);
  398. MP_bus_info(&bus);
  399. construct_default_ioirq_mptable(0);
  400. }
  401. #endif
  402. return 0;
  403. }
  404. /*
  405. * Scan the memory blocks for an SMP configuration block.
  406. */
  407. static __init void mpparse_get_smp_config(unsigned int early)
  408. {
  409. struct mpf_intel *mpf;
  410. if (!smp_found_config)
  411. return;
  412. if (!mpf_found)
  413. return;
  414. if (acpi_lapic && early)
  415. return;
  416. /*
  417. * MPS doesn't support hyperthreading, aka only have
  418. * thread 0 apic id in MPS table
  419. */
  420. if (acpi_lapic && acpi_ioapic)
  421. return;
  422. mpf = early_memremap(mpf_base, sizeof(*mpf));
  423. if (!mpf) {
  424. pr_err("MPTABLE: error mapping MP table\n");
  425. return;
  426. }
  427. pr_info("Intel MultiProcessor Specification v1.%d\n",
  428. mpf->specification);
  429. #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86_32)
  430. if (mpf->feature2 & (1 << 7)) {
  431. pr_info(" IMCR and PIC compatibility mode.\n");
  432. pic_mode = 1;
  433. } else {
  434. pr_info(" Virtual Wire compatibility mode.\n");
  435. pic_mode = 0;
  436. }
  437. #endif
  438. /*
  439. * Now see if we need to read further.
  440. */
  441. if (mpf->feature1) {
  442. if (early) {
  443. /* Local APIC has default address */
  444. register_lapic_address(APIC_DEFAULT_PHYS_BASE);
  445. goto out;
  446. }
  447. pr_info("Default MP configuration #%d\n", mpf->feature1);
  448. construct_default_ISA_mptable(mpf->feature1);
  449. } else if (mpf->physptr) {
  450. if (check_physptr(mpf, early))
  451. goto out;
  452. } else
  453. BUG();
  454. if (!early && !acpi_lapic)
  455. pr_info("Processors: %d\n", num_procs);
  456. /*
  457. * Only use the first configuration found.
  458. */
  459. out:
  460. early_memunmap(mpf, sizeof(*mpf));
  461. }
  462. void __init mpparse_parse_early_smp_config(void)
  463. {
  464. mpparse_get_smp_config(true);
  465. }
  466. void __init mpparse_parse_smp_config(void)
  467. {
  468. mpparse_get_smp_config(false);
  469. }
  470. static void __init smp_reserve_memory(struct mpf_intel *mpf)
  471. {
  472. memblock_reserve(mpf->physptr, get_mpc_size(mpf->physptr));
  473. }
  474. static int __init smp_scan_config(unsigned long base, unsigned long length)
  475. {
  476. unsigned int *bp;
  477. struct mpf_intel *mpf;
  478. int ret = 0;
  479. apic_pr_verbose("Scan for SMP in [mem %#010lx-%#010lx]\n", base, base + length - 1);
  480. BUILD_BUG_ON(sizeof(*mpf) != 16);
  481. while (length > 0) {
  482. bp = early_memremap(base, length);
  483. mpf = (struct mpf_intel *)bp;
  484. if ((*bp == SMP_MAGIC_IDENT) &&
  485. (mpf->length == 1) &&
  486. !mpf_checksum((unsigned char *)bp, 16) &&
  487. ((mpf->specification == 1)
  488. || (mpf->specification == 4))) {
  489. #ifdef CONFIG_X86_LOCAL_APIC
  490. smp_found_config = 1;
  491. #endif
  492. mpf_base = base;
  493. mpf_found = true;
  494. pr_info("found SMP MP-table at [mem %#010lx-%#010lx]\n",
  495. base, base + sizeof(*mpf) - 1);
  496. memblock_reserve(base, sizeof(*mpf));
  497. if (mpf->physptr)
  498. smp_reserve_memory(mpf);
  499. ret = 1;
  500. }
  501. early_memunmap(bp, length);
  502. if (ret)
  503. break;
  504. base += 16;
  505. length -= 16;
  506. }
  507. return ret;
  508. }
  509. void __init mpparse_find_mptable(void)
  510. {
  511. unsigned int address;
  512. /*
  513. * FIXME: Linux assumes you have 640K of base ram..
  514. * this continues the error...
  515. *
  516. * 1) Scan the bottom 1K for a signature
  517. * 2) Scan the top 1K of base RAM
  518. * 3) Scan the 64K of bios
  519. */
  520. if (smp_scan_config(0x0, 0x400) ||
  521. smp_scan_config(639 * 0x400, 0x400) ||
  522. smp_scan_config(0xF0000, 0x10000))
  523. return;
  524. /*
  525. * If it is an SMP machine we should know now, unless the
  526. * configuration is in an EISA bus machine with an
  527. * extended bios data area.
  528. *
  529. * there is a real-mode segmented pointer pointing to the
  530. * 4K EBDA area at 0x40E, calculate and scan it here.
  531. *
  532. * NOTE! There are Linux loaders that will corrupt the EBDA
  533. * area, and as such this kind of SMP config may be less
  534. * trustworthy, simply because the SMP table may have been
  535. * stomped on during early boot. These loaders are buggy and
  536. * should be fixed.
  537. *
  538. * MP1.4 SPEC states to only scan first 1K of 4K EBDA.
  539. */
  540. address = get_bios_ebda();
  541. if (address)
  542. smp_scan_config(address, 0x400);
  543. }
  544. #ifdef CONFIG_X86_IO_APIC
  545. static u8 __initdata irq_used[MAX_IRQ_SOURCES];
  546. static int __init get_MP_intsrc_index(struct mpc_intsrc *m)
  547. {
  548. int i;
  549. if (m->irqtype != mp_INT)
  550. return 0;
  551. if (m->irqflag != (MP_IRQTRIG_LEVEL | MP_IRQPOL_ACTIVE_LOW))
  552. return 0;
  553. /* not legacy */
  554. for (i = 0; i < mp_irq_entries; i++) {
  555. if (mp_irqs[i].irqtype != mp_INT)
  556. continue;
  557. if (mp_irqs[i].irqflag != (MP_IRQTRIG_LEVEL |
  558. MP_IRQPOL_ACTIVE_LOW))
  559. continue;
  560. if (mp_irqs[i].srcbus != m->srcbus)
  561. continue;
  562. if (mp_irqs[i].srcbusirq != m->srcbusirq)
  563. continue;
  564. if (irq_used[i]) {
  565. /* already claimed */
  566. return -2;
  567. }
  568. irq_used[i] = 1;
  569. return i;
  570. }
  571. /* not found */
  572. return -1;
  573. }
  574. #define SPARE_SLOT_NUM 20
  575. static struct mpc_intsrc __initdata *m_spare[SPARE_SLOT_NUM];
  576. static void __init check_irq_src(struct mpc_intsrc *m, int *nr_m_spare)
  577. {
  578. int i;
  579. apic_pr_verbose("OLD ");
  580. print_mp_irq_info(m);
  581. i = get_MP_intsrc_index(m);
  582. if (i > 0) {
  583. memcpy(m, &mp_irqs[i], sizeof(*m));
  584. apic_pr_verbose("NEW ");
  585. print_mp_irq_info(&mp_irqs[i]);
  586. return;
  587. }
  588. if (!i) {
  589. /* legacy, do nothing */
  590. return;
  591. }
  592. if (*nr_m_spare < SPARE_SLOT_NUM) {
  593. /*
  594. * not found (-1), or duplicated (-2) are invalid entries,
  595. * we need to use the slot later
  596. */
  597. m_spare[*nr_m_spare] = m;
  598. *nr_m_spare += 1;
  599. }
  600. }
  601. static int __init
  602. check_slot(unsigned long mpc_new_phys, unsigned long mpc_new_length, int count)
  603. {
  604. if (!mpc_new_phys || count <= mpc_new_length) {
  605. WARN(1, "update_mptable: No spare slots (length: %x)\n", count);
  606. return -1;
  607. }
  608. return 0;
  609. }
  610. #else /* CONFIG_X86_IO_APIC */
  611. static
  612. inline void __init check_irq_src(struct mpc_intsrc *m, int *nr_m_spare) {}
  613. #endif /* CONFIG_X86_IO_APIC */
  614. static int __init replace_intsrc_all(struct mpc_table *mpc,
  615. unsigned long mpc_new_phys,
  616. unsigned long mpc_new_length)
  617. {
  618. #ifdef CONFIG_X86_IO_APIC
  619. int i;
  620. #endif
  621. int count = sizeof(*mpc);
  622. int nr_m_spare = 0;
  623. unsigned char *mpt = ((unsigned char *)mpc) + count;
  624. pr_info("mpc_length %x\n", mpc->length);
  625. while (count < mpc->length) {
  626. switch (*mpt) {
  627. case MP_PROCESSOR:
  628. skip_entry(&mpt, &count, sizeof(struct mpc_cpu));
  629. break;
  630. case MP_BUS:
  631. skip_entry(&mpt, &count, sizeof(struct mpc_bus));
  632. break;
  633. case MP_IOAPIC:
  634. skip_entry(&mpt, &count, sizeof(struct mpc_ioapic));
  635. break;
  636. case MP_INTSRC:
  637. check_irq_src((struct mpc_intsrc *)mpt, &nr_m_spare);
  638. skip_entry(&mpt, &count, sizeof(struct mpc_intsrc));
  639. break;
  640. case MP_LINTSRC:
  641. skip_entry(&mpt, &count, sizeof(struct mpc_lintsrc));
  642. break;
  643. default:
  644. /* wrong mptable */
  645. smp_dump_mptable(mpc, mpt);
  646. goto out;
  647. }
  648. }
  649. #ifdef CONFIG_X86_IO_APIC
  650. for (i = 0; i < mp_irq_entries; i++) {
  651. if (irq_used[i])
  652. continue;
  653. if (mp_irqs[i].irqtype != mp_INT)
  654. continue;
  655. if (mp_irqs[i].irqflag != (MP_IRQTRIG_LEVEL |
  656. MP_IRQPOL_ACTIVE_LOW))
  657. continue;
  658. if (nr_m_spare > 0) {
  659. apic_pr_verbose("*NEW* found\n");
  660. nr_m_spare--;
  661. memcpy(m_spare[nr_m_spare], &mp_irqs[i], sizeof(mp_irqs[i]));
  662. m_spare[nr_m_spare] = NULL;
  663. } else {
  664. struct mpc_intsrc *m = (struct mpc_intsrc *)mpt;
  665. count += sizeof(struct mpc_intsrc);
  666. if (check_slot(mpc_new_phys, mpc_new_length, count) < 0)
  667. goto out;
  668. memcpy(m, &mp_irqs[i], sizeof(*m));
  669. mpc->length = count;
  670. mpt += sizeof(struct mpc_intsrc);
  671. }
  672. print_mp_irq_info(&mp_irqs[i]);
  673. }
  674. #endif
  675. out:
  676. /* update checksum */
  677. mpc->checksum = 0;
  678. mpc->checksum -= mpf_checksum((unsigned char *)mpc, mpc->length);
  679. return 0;
  680. }
  681. int enable_update_mptable;
  682. static int __init update_mptable_setup(char *str)
  683. {
  684. enable_update_mptable = 1;
  685. #ifdef CONFIG_PCI
  686. pci_routeirq = 1;
  687. #endif
  688. return 0;
  689. }
  690. early_param("update_mptable", update_mptable_setup);
  691. static unsigned long __initdata mpc_new_phys;
  692. static unsigned long mpc_new_length __initdata = 4096;
  693. /* alloc_mptable or alloc_mptable=4k */
  694. static int __initdata alloc_mptable;
  695. static int __init parse_alloc_mptable_opt(char *p)
  696. {
  697. enable_update_mptable = 1;
  698. #ifdef CONFIG_PCI
  699. pci_routeirq = 1;
  700. #endif
  701. alloc_mptable = 1;
  702. if (!p)
  703. return 0;
  704. mpc_new_length = memparse(p, &p);
  705. return 0;
  706. }
  707. early_param("alloc_mptable", parse_alloc_mptable_opt);
  708. void __init e820__memblock_alloc_reserved_mpc_new(void)
  709. {
  710. if (enable_update_mptable && alloc_mptable)
  711. mpc_new_phys = e820__memblock_alloc_reserved(mpc_new_length, 4);
  712. }
  713. static int __init update_mp_table(void)
  714. {
  715. char str[16];
  716. char oem[10];
  717. struct mpf_intel *mpf;
  718. struct mpc_table *mpc, *mpc_new;
  719. unsigned long size;
  720. if (!enable_update_mptable)
  721. return 0;
  722. if (!mpf_found)
  723. return 0;
  724. mpf = early_memremap(mpf_base, sizeof(*mpf));
  725. if (!mpf) {
  726. pr_err("MPTABLE: mpf early_memremap() failed\n");
  727. return 0;
  728. }
  729. /*
  730. * Now see if we need to go further.
  731. */
  732. if (mpf->feature1)
  733. goto do_unmap_mpf;
  734. if (!mpf->physptr)
  735. goto do_unmap_mpf;
  736. size = get_mpc_size(mpf->physptr);
  737. mpc = early_memremap(mpf->physptr, size);
  738. if (!mpc) {
  739. pr_err("MPTABLE: mpc early_memremap() failed\n");
  740. goto do_unmap_mpf;
  741. }
  742. if (!smp_check_mpc(mpc, oem, str))
  743. goto do_unmap_mpc;
  744. pr_info("mpf: %llx\n", (u64)mpf_base);
  745. pr_info("physptr: %x\n", mpf->physptr);
  746. if (mpc_new_phys && mpc->length > mpc_new_length) {
  747. mpc_new_phys = 0;
  748. pr_info("mpc_new_length is %ld, please use alloc_mptable=8k\n",
  749. mpc_new_length);
  750. }
  751. if (!mpc_new_phys) {
  752. unsigned char old, new;
  753. /* check if we can change the position */
  754. mpc->checksum = 0;
  755. old = mpf_checksum((unsigned char *)mpc, mpc->length);
  756. mpc->checksum = 0xff;
  757. new = mpf_checksum((unsigned char *)mpc, mpc->length);
  758. if (old == new) {
  759. pr_info("mpc is readonly, please try alloc_mptable instead\n");
  760. goto do_unmap_mpc;
  761. }
  762. pr_info("use in-position replacing\n");
  763. } else {
  764. mpc_new = early_memremap(mpc_new_phys, mpc_new_length);
  765. if (!mpc_new) {
  766. pr_err("MPTABLE: new mpc early_memremap() failed\n");
  767. goto do_unmap_mpc;
  768. }
  769. mpf->physptr = mpc_new_phys;
  770. memcpy(mpc_new, mpc, mpc->length);
  771. early_memunmap(mpc, size);
  772. mpc = mpc_new;
  773. size = mpc_new_length;
  774. /* check if we can modify that */
  775. if (mpc_new_phys - mpf->physptr) {
  776. struct mpf_intel *mpf_new;
  777. /* steal 16 bytes from [0, 1k) */
  778. mpf_new = early_memremap(0x400 - 16, sizeof(*mpf_new));
  779. if (!mpf_new) {
  780. pr_err("MPTABLE: new mpf early_memremap() failed\n");
  781. goto do_unmap_mpc;
  782. }
  783. pr_info("mpf new: %x\n", 0x400 - 16);
  784. memcpy(mpf_new, mpf, 16);
  785. early_memunmap(mpf, sizeof(*mpf));
  786. mpf = mpf_new;
  787. mpf->physptr = mpc_new_phys;
  788. }
  789. mpf->checksum = 0;
  790. mpf->checksum -= mpf_checksum((unsigned char *)mpf, 16);
  791. pr_info("physptr new: %x\n", mpf->physptr);
  792. }
  793. /*
  794. * only replace the one with mp_INT and
  795. * MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW,
  796. * already in mp_irqs , stored by ... and mp_config_acpi_gsi,
  797. * may need pci=routeirq for all coverage
  798. */
  799. replace_intsrc_all(mpc, mpc_new_phys, mpc_new_length);
  800. do_unmap_mpc:
  801. early_memunmap(mpc, size);
  802. do_unmap_mpf:
  803. early_memunmap(mpf, sizeof(*mpf));
  804. return 0;
  805. }
  806. late_initcall(update_mp_table);