mpparse.c 22 KB

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