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