mpparse_64.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * Intel Multiprocessor Specification 1.1 and 1.4
  3. * compliant MP-table parsing routines.
  4. *
  5. * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
  6. * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
  7. *
  8. * Fixes
  9. * Erich Boleyn : MP v1.4 and additional changes.
  10. * Alan Cox : Added EBDA scanning
  11. * Ingo Molnar : various cleanups and rewrites
  12. * Maciej W. Rozycki: Bits for default MP configurations
  13. * Paul Diefenbaugh: Added full ACPI support
  14. */
  15. #include <linux/mm.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/kernel_stat.h>
  20. #include <linux/mc146818rtc.h>
  21. #include <linux/acpi.h>
  22. #include <linux/module.h>
  23. #include <asm/smp.h>
  24. #include <asm/mtrr.h>
  25. #include <asm/mpspec.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/io_apic.h>
  28. #include <asm/proto.h>
  29. #include <asm/acpi.h>
  30. #include <asm/bios_ebda.h>
  31. #include <mach_apic.h>
  32. /* Have we found an MP table */
  33. int smp_found_config;
  34. /*
  35. * Various Linux-internal data structures created from the
  36. * MP-table.
  37. */
  38. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  39. int mp_bus_id_to_pci_bus[MAX_MP_BUSSES] = {[0 ... MAX_MP_BUSSES - 1] = -1 };
  40. static int mp_current_pci_id = 0;
  41. /* # of MP IRQ source entries */
  42. struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  43. /* MP IRQ source entries */
  44. int mp_irq_entries;
  45. /* Make it easy to share the UP and SMP code: */
  46. #ifndef CONFIG_X86_SMP
  47. unsigned int num_processors;
  48. unsigned disabled_cpus __cpuinitdata;
  49. #ifndef CONFIG_X86_LOCAL_APIC
  50. unsigned int boot_cpu_physical_apicid = -1U;
  51. #endif
  52. #endif
  53. /*
  54. * Intel MP BIOS table parsing routines:
  55. */
  56. /*
  57. * Checksum an MP configuration block.
  58. */
  59. static int __init mpf_checksum(unsigned char *mp, int len)
  60. {
  61. int sum = 0;
  62. while (len--)
  63. sum += *mp++;
  64. return sum & 0xFF;
  65. }
  66. static void __cpuinit MP_processor_info(struct mpc_config_processor *m)
  67. {
  68. char *bootup_cpu = "";
  69. if (!(m->mpc_cpuflag & CPU_ENABLED)) {
  70. disabled_cpus++;
  71. return;
  72. }
  73. if (m->mpc_cpuflag & CPU_BOOTPROCESSOR) {
  74. bootup_cpu = " (Bootup-CPU)";
  75. boot_cpu_physical_apicid = m->mpc_apicid;
  76. }
  77. printk(KERN_INFO "Processor #%d%s\n", m->mpc_apicid, bootup_cpu);
  78. generic_processor_info(m->mpc_apicid, 0);
  79. }
  80. static void __init MP_bus_info(struct mpc_config_bus *m)
  81. {
  82. char str[7];
  83. memcpy(str, m->mpc_bustype, 6);
  84. str[6] = 0;
  85. Dprintk("Bus #%d is %s\n", m->mpc_busid, str);
  86. if (strncmp(str, "ISA", 3) == 0) {
  87. set_bit(m->mpc_busid, mp_bus_not_pci);
  88. } else if (strncmp(str, "PCI", 3) == 0) {
  89. clear_bit(m->mpc_busid, mp_bus_not_pci);
  90. mp_bus_id_to_pci_bus[m->mpc_busid] = mp_current_pci_id;
  91. mp_current_pci_id++;
  92. } else {
  93. printk(KERN_ERR "Unknown bustype %s\n", str);
  94. }
  95. }
  96. static int bad_ioapic(unsigned long address)
  97. {
  98. if (nr_ioapics >= MAX_IO_APICS) {
  99. printk(KERN_ERR "ERROR: Max # of I/O APICs (%d) exceeded "
  100. "(found %d)\n", MAX_IO_APICS, nr_ioapics);
  101. panic("Recompile kernel with bigger MAX_IO_APICS!\n");
  102. }
  103. if (!address) {
  104. printk(KERN_ERR "WARNING: Bogus (zero) I/O APIC address"
  105. " found in table, skipping!\n");
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. static void __init MP_ioapic_info(struct mpc_config_ioapic *m)
  111. {
  112. if (!(m->mpc_flags & MPC_APIC_USABLE))
  113. return;
  114. printk(KERN_INFO "I/O APIC #%d at 0x%X.\n", m->mpc_apicid,
  115. m->mpc_apicaddr);
  116. if (bad_ioapic(m->mpc_apicaddr))
  117. return;
  118. mp_ioapics[nr_ioapics] = *m;
  119. nr_ioapics++;
  120. }
  121. static void __init MP_intsrc_info(struct mpc_config_intsrc *m)
  122. {
  123. mp_irqs[mp_irq_entries] = *m;
  124. Dprintk("Int: type %d, pol %d, trig %d, bus %d,"
  125. " IRQ %02x, APIC ID %x, APIC INT %02x\n",
  126. m->mpc_irqtype, m->mpc_irqflag & 3,
  127. (m->mpc_irqflag >> 2) & 3, m->mpc_srcbus,
  128. m->mpc_srcbusirq, m->mpc_dstapic, m->mpc_dstirq);
  129. if (++mp_irq_entries >= MAX_IRQ_SOURCES)
  130. panic("Max # of irq sources exceeded!!\n");
  131. }
  132. static void __init MP_lintsrc_info(struct mpc_config_lintsrc *m)
  133. {
  134. Dprintk("Lint: type %d, pol %d, trig %d, bus %d,"
  135. " IRQ %02x, APIC ID %x, APIC LINT %02x\n",
  136. m->mpc_irqtype, m->mpc_irqflag & 3,
  137. (m->mpc_irqflag >> 2) & 3, m->mpc_srcbusid,
  138. m->mpc_srcbusirq, m->mpc_destapic, m->mpc_destapiclint);
  139. }
  140. /*
  141. * Read/parse the MPC
  142. */
  143. static int __init smp_read_mpc(struct mp_config_table *mpc, unsigned early)
  144. {
  145. char str[16];
  146. int count = sizeof(*mpc);
  147. unsigned char *mpt = ((unsigned char *)mpc) + count;
  148. if (memcmp(mpc->mpc_signature, MPC_SIGNATURE, 4)) {
  149. printk(KERN_ERR "MPTABLE: bad signature [%c%c%c%c]!\n",
  150. mpc->mpc_signature[0],
  151. mpc->mpc_signature[1],
  152. mpc->mpc_signature[2], mpc->mpc_signature[3]);
  153. return 0;
  154. }
  155. if (mpf_checksum((unsigned char *)mpc, mpc->mpc_length)) {
  156. printk(KERN_ERR "MPTABLE: checksum error!\n");
  157. return 0;
  158. }
  159. if (mpc->mpc_spec != 0x01 && mpc->mpc_spec != 0x04) {
  160. printk(KERN_ERR "MPTABLE: bad table version (%d)!!\n",
  161. mpc->mpc_spec);
  162. return 0;
  163. }
  164. if (!mpc->mpc_lapic) {
  165. printk(KERN_ERR "MPTABLE: null local APIC address!\n");
  166. return 0;
  167. }
  168. memcpy(str, mpc->mpc_oem, 8);
  169. str[8] = 0;
  170. printk(KERN_INFO "MPTABLE: OEM ID: %s ", str);
  171. memcpy(str, mpc->mpc_productid, 12);
  172. str[12] = 0;
  173. printk(KERN_INFO "MPTABLE: Product ID: %s ", str);
  174. printk(KERN_INFO "MPTABLE: APIC at: 0x%X\n", mpc->mpc_lapic);
  175. /* save the local APIC address, it might be non-default */
  176. if (!acpi_lapic)
  177. mp_lapic_addr = mpc->mpc_lapic;
  178. if (early)
  179. return 1;
  180. /*
  181. * Now process the configuration blocks.
  182. */
  183. while (count < mpc->mpc_length) {
  184. switch (*mpt) {
  185. case MP_PROCESSOR:
  186. {
  187. struct mpc_config_processor *m =
  188. (struct mpc_config_processor *)mpt;
  189. if (!acpi_lapic)
  190. MP_processor_info(m);
  191. mpt += sizeof(*m);
  192. count += sizeof(*m);
  193. break;
  194. }
  195. case MP_BUS:
  196. {
  197. struct mpc_config_bus *m =
  198. (struct mpc_config_bus *)mpt;
  199. MP_bus_info(m);
  200. mpt += sizeof(*m);
  201. count += sizeof(*m);
  202. break;
  203. }
  204. case MP_IOAPIC:
  205. {
  206. struct mpc_config_ioapic *m =
  207. (struct mpc_config_ioapic *)mpt;
  208. MP_ioapic_info(m);
  209. mpt += sizeof(*m);
  210. count += sizeof(*m);
  211. break;
  212. }
  213. case MP_INTSRC:
  214. {
  215. struct mpc_config_intsrc *m =
  216. (struct mpc_config_intsrc *)mpt;
  217. MP_intsrc_info(m);
  218. mpt += sizeof(*m);
  219. count += sizeof(*m);
  220. break;
  221. }
  222. case MP_LINTSRC:
  223. {
  224. struct mpc_config_lintsrc *m =
  225. (struct mpc_config_lintsrc *)mpt;
  226. MP_lintsrc_info(m);
  227. mpt += sizeof(*m);
  228. count += sizeof(*m);
  229. break;
  230. }
  231. }
  232. }
  233. setup_apic_routing();
  234. if (!num_processors)
  235. printk(KERN_ERR "MPTABLE: no processors registered!\n");
  236. return num_processors;
  237. }
  238. static int __init ELCR_trigger(unsigned int irq)
  239. {
  240. unsigned int port;
  241. port = 0x4d0 + (irq >> 3);
  242. return (inb(port) >> (irq & 7)) & 1;
  243. }
  244. static void __init construct_default_ioirq_mptable(int mpc_default_type)
  245. {
  246. struct mpc_config_intsrc intsrc;
  247. int i;
  248. int ELCR_fallback = 0;
  249. intsrc.mpc_type = MP_INTSRC;
  250. intsrc.mpc_irqflag = 0; /* conforming */
  251. intsrc.mpc_srcbus = 0;
  252. intsrc.mpc_dstapic = mp_ioapics[0].mpc_apicid;
  253. intsrc.mpc_irqtype = mp_INT;
  254. /*
  255. * If true, we have an ISA/PCI system with no IRQ entries
  256. * in the MP table. To prevent the PCI interrupts from being set up
  257. * incorrectly, we try to use the ELCR. The sanity check to see if
  258. * there is good ELCR data is very simple - IRQ0, 1, 2 and 13 can
  259. * never be level sensitive, so we simply see if the ELCR agrees.
  260. * If it does, we assume it's valid.
  261. */
  262. if (mpc_default_type == 5) {
  263. printk(KERN_INFO "ISA/PCI bus type with no IRQ information... "
  264. "falling back to ELCR\n");
  265. if (ELCR_trigger(0) || ELCR_trigger(1) || ELCR_trigger(2) ||
  266. ELCR_trigger(13))
  267. printk(KERN_ERR "ELCR contains invalid data... "
  268. "not using ELCR\n");
  269. else {
  270. printk(KERN_INFO
  271. "Using ELCR to identify PCI interrupts\n");
  272. ELCR_fallback = 1;
  273. }
  274. }
  275. for (i = 0; i < 16; i++) {
  276. switch (mpc_default_type) {
  277. case 2:
  278. if (i == 0 || i == 13)
  279. continue; /* IRQ0 & IRQ13 not connected */
  280. /* fall through */
  281. default:
  282. if (i == 2)
  283. continue; /* IRQ2 is never connected */
  284. }
  285. if (ELCR_fallback) {
  286. /*
  287. * If the ELCR indicates a level-sensitive interrupt, we
  288. * copy that information over to the MP table in the
  289. * irqflag field (level sensitive, active high polarity).
  290. */
  291. if (ELCR_trigger(i))
  292. intsrc.mpc_irqflag = 13;
  293. else
  294. intsrc.mpc_irqflag = 0;
  295. }
  296. intsrc.mpc_srcbusirq = i;
  297. intsrc.mpc_dstirq = i ? i : 2; /* IRQ0 to INTIN2 */
  298. MP_intsrc_info(&intsrc);
  299. }
  300. intsrc.mpc_irqtype = mp_ExtINT;
  301. intsrc.mpc_srcbusirq = 0;
  302. intsrc.mpc_dstirq = 0; /* 8259A to INTIN0 */
  303. MP_intsrc_info(&intsrc);
  304. }
  305. static inline void __init construct_default_ISA_mptable(int mpc_default_type)
  306. {
  307. struct mpc_config_processor processor;
  308. struct mpc_config_bus bus;
  309. struct mpc_config_ioapic ioapic;
  310. struct mpc_config_lintsrc lintsrc;
  311. int linttypes[2] = { mp_ExtINT, mp_NMI };
  312. int i;
  313. /*
  314. * local APIC has default address
  315. */
  316. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  317. /*
  318. * 2 CPUs, numbered 0 & 1.
  319. */
  320. processor.mpc_type = MP_PROCESSOR;
  321. processor.mpc_apicver = 0;
  322. processor.mpc_cpuflag = CPU_ENABLED;
  323. processor.mpc_cpufeature = 0;
  324. processor.mpc_featureflag = 0;
  325. processor.mpc_reserved[0] = 0;
  326. processor.mpc_reserved[1] = 0;
  327. for (i = 0; i < 2; i++) {
  328. processor.mpc_apicid = i;
  329. MP_processor_info(&processor);
  330. }
  331. bus.mpc_type = MP_BUS;
  332. bus.mpc_busid = 0;
  333. switch (mpc_default_type) {
  334. default:
  335. printk(KERN_ERR "???\nUnknown standard configuration %d\n",
  336. mpc_default_type);
  337. /* fall through */
  338. case 1:
  339. case 5:
  340. memcpy(bus.mpc_bustype, "ISA ", 6);
  341. break;
  342. }
  343. MP_bus_info(&bus);
  344. if (mpc_default_type > 4) {
  345. bus.mpc_busid = 1;
  346. memcpy(bus.mpc_bustype, "PCI ", 6);
  347. MP_bus_info(&bus);
  348. }
  349. ioapic.mpc_type = MP_IOAPIC;
  350. ioapic.mpc_apicid = 2;
  351. ioapic.mpc_apicver = 0;
  352. ioapic.mpc_flags = MPC_APIC_USABLE;
  353. ioapic.mpc_apicaddr = 0xFEC00000;
  354. MP_ioapic_info(&ioapic);
  355. /*
  356. * We set up most of the low 16 IO-APIC pins according to MPS rules.
  357. */
  358. construct_default_ioirq_mptable(mpc_default_type);
  359. lintsrc.mpc_type = MP_LINTSRC;
  360. lintsrc.mpc_irqflag = 0; /* conforming */
  361. lintsrc.mpc_srcbusid = 0;
  362. lintsrc.mpc_srcbusirq = 0;
  363. lintsrc.mpc_destapic = MP_APIC_ALL;
  364. for (i = 0; i < 2; i++) {
  365. lintsrc.mpc_irqtype = linttypes[i];
  366. lintsrc.mpc_destapiclint = i;
  367. MP_lintsrc_info(&lintsrc);
  368. }
  369. }
  370. static struct intel_mp_floating *mpf_found;
  371. /*
  372. * Scan the memory blocks for an SMP configuration block.
  373. */
  374. static void __init __get_smp_config(unsigned early)
  375. {
  376. struct intel_mp_floating *mpf = mpf_found;
  377. if (acpi_lapic && early)
  378. return;
  379. /*
  380. * ACPI supports both logical (e.g. Hyper-Threading) and physical
  381. * processors, where MPS only supports physical.
  382. */
  383. if (acpi_lapic && acpi_ioapic) {
  384. printk(KERN_INFO "Using ACPI (MADT) for SMP configuration "
  385. "information\n");
  386. return;
  387. } else if (acpi_lapic)
  388. printk(KERN_INFO "Using ACPI for processor (LAPIC) "
  389. "configuration information\n");
  390. printk(KERN_INFO "Intel MultiProcessor Specification v1.%d\n",
  391. mpf->mpf_specification);
  392. /*
  393. * Now see if we need to read further.
  394. */
  395. if (mpf->mpf_feature1 != 0) {
  396. if (early) {
  397. /*
  398. * local APIC has default address
  399. */
  400. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  401. return;
  402. }
  403. printk(KERN_INFO "Default MP configuration #%d\n",
  404. mpf->mpf_feature1);
  405. construct_default_ISA_mptable(mpf->mpf_feature1);
  406. } else if (mpf->mpf_physptr) {
  407. /*
  408. * Read the physical hardware table. Anything here will
  409. * override the defaults.
  410. */
  411. if (!smp_read_mpc(phys_to_virt(mpf->mpf_physptr), early)) {
  412. smp_found_config = 0;
  413. printk(KERN_ERR
  414. "BIOS bug, MP table errors detected!...\n");
  415. printk(KERN_ERR "... disabling SMP support. "
  416. "(tell your hw vendor)\n");
  417. return;
  418. }
  419. if (early)
  420. return;
  421. /*
  422. * If there are no explicit MP IRQ entries, then we are
  423. * broken. We set up most of the low 16 IO-APIC pins to
  424. * ISA defaults and hope it will work.
  425. */
  426. if (!mp_irq_entries) {
  427. struct mpc_config_bus bus;
  428. printk(KERN_ERR "BIOS bug, no explicit IRQ entries, "
  429. "using default mptable. "
  430. "(tell your hw vendor)\n");
  431. bus.mpc_type = MP_BUS;
  432. bus.mpc_busid = 0;
  433. memcpy(bus.mpc_bustype, "ISA ", 6);
  434. MP_bus_info(&bus);
  435. construct_default_ioirq_mptable(0);
  436. }
  437. } else
  438. BUG();
  439. if (!early)
  440. printk(KERN_INFO "Processors: %d\n", num_processors);
  441. /*
  442. * Only use the first configuration found.
  443. */
  444. }
  445. void __init early_get_smp_config(void)
  446. {
  447. __get_smp_config(1);
  448. }
  449. void __init get_smp_config(void)
  450. {
  451. __get_smp_config(0);
  452. }
  453. static int __init smp_scan_config(unsigned long base, unsigned long length,
  454. unsigned reserve)
  455. {
  456. extern void __bad_mpf_size(void);
  457. unsigned int *bp = phys_to_virt(base);
  458. struct intel_mp_floating *mpf;
  459. Dprintk("Scan SMP from %p for %ld bytes.\n", bp, length);
  460. if (sizeof(*mpf) != 16)
  461. __bad_mpf_size();
  462. while (length > 0) {
  463. mpf = (struct intel_mp_floating *)bp;
  464. if ((*bp == SMP_MAGIC_IDENT) &&
  465. (mpf->mpf_length == 1) &&
  466. !mpf_checksum((unsigned char *)bp, 16) &&
  467. ((mpf->mpf_specification == 1)
  468. || (mpf->mpf_specification == 4))) {
  469. smp_found_config = 1;
  470. mpf_found = mpf;
  471. if (!reserve)
  472. return 1;
  473. reserve_bootmem_generic(virt_to_phys(mpf), PAGE_SIZE);
  474. if (mpf->mpf_physptr)
  475. reserve_bootmem_generic(mpf->mpf_physptr,
  476. PAGE_SIZE);
  477. return 1;
  478. }
  479. bp += 4;
  480. length -= 16;
  481. }
  482. return 0;
  483. }
  484. static void __init __find_smp_config(unsigned reserve)
  485. {
  486. unsigned int address;
  487. /*
  488. * FIXME: Linux assumes you have 640K of base ram..
  489. * this continues the error...
  490. *
  491. * 1) Scan the bottom 1K for a signature
  492. * 2) Scan the top 1K of base RAM
  493. * 3) Scan the 64K of bios
  494. */
  495. if (smp_scan_config(0x0, 0x400, reserve) ||
  496. smp_scan_config(639 * 0x400, 0x400, reserve) ||
  497. smp_scan_config(0xF0000, 0x10000, reserve))
  498. return;
  499. /*
  500. * If it is an SMP machine we should know now.
  501. *
  502. * there is a real-mode segmented pointer pointing to the
  503. * 4K EBDA area at 0x40E, calculate and scan it here.
  504. *
  505. * NOTE! There are Linux loaders that will corrupt the EBDA
  506. * area, and as such this kind of SMP config may be less
  507. * trustworthy, simply because the SMP table may have been
  508. * stomped on during early boot. These loaders are buggy and
  509. * should be fixed.
  510. *
  511. * MP1.4 SPEC states to only scan first 1K of 4K EBDA.
  512. */
  513. address = get_bios_ebda();
  514. if (address)
  515. smp_scan_config(address, 0x400, reserve);
  516. }
  517. void __init early_find_smp_config(void)
  518. {
  519. __find_smp_config(0);
  520. }
  521. void __init find_smp_config(void)
  522. {
  523. __find_smp_config(1);
  524. }
  525. /* --------------------------------------------------------------------------
  526. ACPI-based MP Configuration
  527. -------------------------------------------------------------------------- */
  528. #ifdef CONFIG_ACPI
  529. void __init mp_register_lapic_address(u64 address)
  530. {
  531. mp_lapic_addr = (unsigned long)address;
  532. set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
  533. if (boot_cpu_physical_apicid == -1U)
  534. boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
  535. }
  536. void __cpuinit mp_register_lapic(int id, u8 enabled)
  537. {
  538. if (!enabled) {
  539. ++disabled_cpus;
  540. return;
  541. }
  542. generic_processor_info(id, 0);
  543. }
  544. #define MP_ISA_BUS 0
  545. #define MP_MAX_IOAPIC_PIN 127
  546. static struct mp_ioapic_routing {
  547. int apic_id;
  548. int gsi_base;
  549. int gsi_end;
  550. u32 pin_programmed[4];
  551. } mp_ioapic_routing[MAX_IO_APICS];
  552. static int mp_find_ioapic(int gsi)
  553. {
  554. int i = 0;
  555. /* Find the IOAPIC that manages this GSI. */
  556. for (i = 0; i < nr_ioapics; i++) {
  557. if ((gsi >= mp_ioapic_routing[i].gsi_base)
  558. && (gsi <= mp_ioapic_routing[i].gsi_end))
  559. return i;
  560. }
  561. printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
  562. return -1;
  563. }
  564. static u8 uniq_ioapic_id(u8 id)
  565. {
  566. int i;
  567. DECLARE_BITMAP(used, 256);
  568. bitmap_zero(used, 256);
  569. for (i = 0; i < nr_ioapics; i++) {
  570. struct mpc_config_ioapic *ia = &mp_ioapics[i];
  571. __set_bit(ia->mpc_apicid, used);
  572. }
  573. if (!test_bit(id, used))
  574. return id;
  575. return find_first_zero_bit(used, 256);
  576. }
  577. void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
  578. {
  579. int idx = 0;
  580. if (bad_ioapic(address))
  581. return;
  582. idx = nr_ioapics;
  583. mp_ioapics[idx].mpc_type = MP_IOAPIC;
  584. mp_ioapics[idx].mpc_flags = MPC_APIC_USABLE;
  585. mp_ioapics[idx].mpc_apicaddr = address;
  586. set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
  587. mp_ioapics[idx].mpc_apicid = uniq_ioapic_id(id);
  588. mp_ioapics[idx].mpc_apicver = 0;
  589. /*
  590. * Build basic IRQ lookup table to facilitate gsi->io_apic lookups
  591. * and to prevent reprogramming of IOAPIC pins (PCI IRQs).
  592. */
  593. mp_ioapic_routing[idx].apic_id = mp_ioapics[idx].mpc_apicid;
  594. mp_ioapic_routing[idx].gsi_base = gsi_base;
  595. mp_ioapic_routing[idx].gsi_end = gsi_base +
  596. io_apic_get_redir_entries(idx);
  597. printk(KERN_INFO "IOAPIC[%d]: apic_id %d, address 0x%x, "
  598. "GSI %d-%d\n", idx, mp_ioapics[idx].mpc_apicid,
  599. mp_ioapics[idx].mpc_apicaddr,
  600. mp_ioapic_routing[idx].gsi_base,
  601. mp_ioapic_routing[idx].gsi_end);
  602. nr_ioapics++;
  603. }
  604. void __init mp_override_legacy_irq(u8 bus_irq, u8 polarity, u8 trigger, u32 gsi)
  605. {
  606. struct mpc_config_intsrc intsrc;
  607. int ioapic = -1;
  608. int pin = -1;
  609. /*
  610. * Convert 'gsi' to 'ioapic.pin'.
  611. */
  612. ioapic = mp_find_ioapic(gsi);
  613. if (ioapic < 0)
  614. return;
  615. pin = gsi - mp_ioapic_routing[ioapic].gsi_base;
  616. /*
  617. * TBD: This check is for faulty timer entries, where the override
  618. * erroneously sets the trigger to level, resulting in a HUGE
  619. * increase of timer interrupts!
  620. */
  621. if ((bus_irq == 0) && (trigger == 3))
  622. trigger = 1;
  623. intsrc.mpc_type = MP_INTSRC;
  624. intsrc.mpc_irqtype = mp_INT;
  625. intsrc.mpc_irqflag = (trigger << 2) | polarity;
  626. intsrc.mpc_srcbus = MP_ISA_BUS;
  627. intsrc.mpc_srcbusirq = bus_irq; /* IRQ */
  628. intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid; /* APIC ID */
  629. intsrc.mpc_dstirq = pin; /* INTIN# */
  630. Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, %d-%d\n",
  631. intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3,
  632. (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus,
  633. intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, intsrc.mpc_dstirq);
  634. mp_irqs[mp_irq_entries] = intsrc;
  635. if (++mp_irq_entries == MAX_IRQ_SOURCES)
  636. panic("Max # of irq sources exceeded!\n");
  637. }
  638. void __init mp_config_acpi_legacy_irqs(void)
  639. {
  640. struct mpc_config_intsrc intsrc;
  641. int i = 0;
  642. int ioapic = -1;
  643. /*
  644. * Fabricate the legacy ISA bus (bus #31).
  645. */
  646. set_bit(MP_ISA_BUS, mp_bus_not_pci);
  647. /*
  648. * Locate the IOAPIC that manages the ISA IRQs (0-15).
  649. */
  650. ioapic = mp_find_ioapic(0);
  651. if (ioapic < 0)
  652. return;
  653. intsrc.mpc_type = MP_INTSRC;
  654. intsrc.mpc_irqflag = 0; /* Conforming */
  655. intsrc.mpc_srcbus = MP_ISA_BUS;
  656. intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;
  657. /*
  658. * Use the default configuration for the IRQs 0-15. Unless
  659. * overridden by (MADT) interrupt source override entries.
  660. */
  661. for (i = 0; i < 16; i++) {
  662. int idx;
  663. for (idx = 0; idx < mp_irq_entries; idx++) {
  664. struct mpc_config_intsrc *irq = mp_irqs + idx;
  665. /* Do we already have a mapping for this ISA IRQ? */
  666. if (irq->mpc_srcbus == MP_ISA_BUS
  667. && irq->mpc_srcbusirq == i)
  668. break;
  669. /* Do we already have a mapping for this IOAPIC pin */
  670. if ((irq->mpc_dstapic == intsrc.mpc_dstapic) &&
  671. (irq->mpc_dstirq == i))
  672. break;
  673. }
  674. if (idx != mp_irq_entries) {
  675. printk(KERN_DEBUG "ACPI: IRQ%d used by override.\n", i);
  676. continue; /* IRQ already used */
  677. }
  678. intsrc.mpc_irqtype = mp_INT;
  679. intsrc.mpc_srcbusirq = i; /* Identity mapped */
  680. intsrc.mpc_dstirq = i;
  681. Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, "
  682. "%d-%d\n", intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3,
  683. (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus,
  684. intsrc.mpc_srcbusirq, intsrc.mpc_dstapic,
  685. intsrc.mpc_dstirq);
  686. mp_irqs[mp_irq_entries] = intsrc;
  687. if (++mp_irq_entries == MAX_IRQ_SOURCES)
  688. panic("Max # of irq sources exceeded!\n");
  689. }
  690. }
  691. int mp_register_gsi(u32 gsi, int triggering, int polarity)
  692. {
  693. int ioapic = -1;
  694. int ioapic_pin = 0;
  695. int idx, bit = 0;
  696. if (acpi_irq_model != ACPI_IRQ_MODEL_IOAPIC)
  697. return gsi;
  698. /* Don't set up the ACPI SCI because it's already set up */
  699. if (acpi_gbl_FADT.sci_interrupt == gsi)
  700. return gsi;
  701. ioapic = mp_find_ioapic(gsi);
  702. if (ioapic < 0) {
  703. printk(KERN_WARNING "No IOAPIC for GSI %u\n", gsi);
  704. return gsi;
  705. }
  706. ioapic_pin = gsi - mp_ioapic_routing[ioapic].gsi_base;
  707. /*
  708. * Avoid pin reprogramming. PRTs typically include entries
  709. * with redundant pin->gsi mappings (but unique PCI devices);
  710. * we only program the IOAPIC on the first.
  711. */
  712. bit = ioapic_pin % 32;
  713. idx = (ioapic_pin < 32) ? 0 : (ioapic_pin / 32);
  714. if (idx > 3) {
  715. printk(KERN_ERR "Invalid reference to IOAPIC pin "
  716. "%d-%d\n", mp_ioapic_routing[ioapic].apic_id,
  717. ioapic_pin);
  718. return gsi;
  719. }
  720. if ((1 << bit) & mp_ioapic_routing[ioapic].pin_programmed[idx]) {
  721. Dprintk(KERN_DEBUG "Pin %d-%d already programmed\n",
  722. mp_ioapic_routing[ioapic].apic_id, ioapic_pin);
  723. return gsi;
  724. }
  725. mp_ioapic_routing[ioapic].pin_programmed[idx] |= (1 << bit);
  726. io_apic_set_pci_routing(ioapic, ioapic_pin, gsi,
  727. triggering == ACPI_EDGE_SENSITIVE ? 0 : 1,
  728. polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
  729. return gsi;
  730. }
  731. #endif /* CONFIG_ACPI */