irq.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Low-Level PCI Support for PC -- Routing of Interrupts
  3. *
  4. * (c) 1999--2000 Martin Mares <mj@ucw.cz>
  5. */
  6. #include <linux/config.h>
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/pci.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/dmi.h>
  15. #include <asm/io.h>
  16. #include <asm/smp.h>
  17. #include <asm/io_apic.h>
  18. #include <asm/hw_irq.h>
  19. #include <linux/acpi.h>
  20. #include "pci.h"
  21. #define PIRQ_SIGNATURE (('$' << 0) + ('P' << 8) + ('I' << 16) + ('R' << 24))
  22. #define PIRQ_VERSION 0x0100
  23. static int broken_hp_bios_irq9;
  24. static int acer_tm360_irqrouting;
  25. static struct irq_routing_table *pirq_table;
  26. static int pirq_enable_irq(struct pci_dev *dev);
  27. /*
  28. * Never use: 0, 1, 2 (timer, keyboard, and cascade)
  29. * Avoid using: 13, 14 and 15 (FP error and IDE).
  30. * Penalize: 3, 4, 6, 7, 12 (known ISA uses: serial, floppy, parallel and mouse)
  31. */
  32. unsigned int pcibios_irq_mask = 0xfff8;
  33. static int pirq_penalty[16] = {
  34. 1000000, 1000000, 1000000, 1000, 1000, 0, 1000, 1000,
  35. 0, 0, 0, 0, 1000, 100000, 100000, 100000
  36. };
  37. struct irq_router {
  38. char *name;
  39. u16 vendor, device;
  40. int (*get)(struct pci_dev *router, struct pci_dev *dev, int pirq);
  41. int (*set)(struct pci_dev *router, struct pci_dev *dev, int pirq, int new);
  42. };
  43. struct irq_router_handler {
  44. u16 vendor;
  45. int (*probe)(struct irq_router *r, struct pci_dev *router, u16 device);
  46. };
  47. int (*pcibios_enable_irq)(struct pci_dev *dev) = NULL;
  48. /*
  49. * Search 0xf0000 -- 0xfffff for the PCI IRQ Routing Table.
  50. */
  51. static struct irq_routing_table * __init pirq_find_routing_table(void)
  52. {
  53. u8 *addr;
  54. struct irq_routing_table *rt;
  55. int i;
  56. u8 sum;
  57. for(addr = (u8 *) __va(0xf0000); addr < (u8 *) __va(0x100000); addr += 16) {
  58. rt = (struct irq_routing_table *) addr;
  59. if (rt->signature != PIRQ_SIGNATURE ||
  60. rt->version != PIRQ_VERSION ||
  61. rt->size % 16 ||
  62. rt->size < sizeof(struct irq_routing_table))
  63. continue;
  64. sum = 0;
  65. for(i=0; i<rt->size; i++)
  66. sum += addr[i];
  67. if (!sum) {
  68. DBG("PCI: Interrupt Routing Table found at 0x%p\n", rt);
  69. return rt;
  70. }
  71. }
  72. return NULL;
  73. }
  74. /*
  75. * If we have a IRQ routing table, use it to search for peer host
  76. * bridges. It's a gross hack, but since there are no other known
  77. * ways how to get a list of buses, we have to go this way.
  78. */
  79. static void __init pirq_peer_trick(void)
  80. {
  81. struct irq_routing_table *rt = pirq_table;
  82. u8 busmap[256];
  83. int i;
  84. struct irq_info *e;
  85. memset(busmap, 0, sizeof(busmap));
  86. for(i=0; i < (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info); i++) {
  87. e = &rt->slots[i];
  88. #ifdef DEBUG
  89. {
  90. int j;
  91. DBG("%02x:%02x slot=%02x", e->bus, e->devfn/8, e->slot);
  92. for(j=0; j<4; j++)
  93. DBG(" %d:%02x/%04x", j, e->irq[j].link, e->irq[j].bitmap);
  94. DBG("\n");
  95. }
  96. #endif
  97. busmap[e->bus] = 1;
  98. }
  99. for(i = 1; i < 256; i++) {
  100. if (!busmap[i] || pci_find_bus(0, i))
  101. continue;
  102. if (pci_scan_bus(i, &pci_root_ops, NULL))
  103. printk(KERN_INFO "PCI: Discovered primary peer bus %02x [IRQ]\n", i);
  104. }
  105. pcibios_last_bus = -1;
  106. }
  107. /*
  108. * Code for querying and setting of IRQ routes on various interrupt routers.
  109. */
  110. void eisa_set_level_irq(unsigned int irq)
  111. {
  112. unsigned char mask = 1 << (irq & 7);
  113. unsigned int port = 0x4d0 + (irq >> 3);
  114. unsigned char val;
  115. static u16 eisa_irq_mask;
  116. if (irq >= 16 || (1 << irq) & eisa_irq_mask)
  117. return;
  118. eisa_irq_mask |= (1 << irq);
  119. printk("PCI: setting IRQ %u as level-triggered\n", irq);
  120. val = inb(port);
  121. if (!(val & mask)) {
  122. DBG(" -> edge");
  123. outb(val | mask, port);
  124. }
  125. }
  126. /*
  127. * Common IRQ routing practice: nybbles in config space,
  128. * offset by some magic constant.
  129. */
  130. static unsigned int read_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr)
  131. {
  132. u8 x;
  133. unsigned reg = offset + (nr >> 1);
  134. pci_read_config_byte(router, reg, &x);
  135. return (nr & 1) ? (x >> 4) : (x & 0xf);
  136. }
  137. static void write_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr, unsigned int val)
  138. {
  139. u8 x;
  140. unsigned reg = offset + (nr >> 1);
  141. pci_read_config_byte(router, reg, &x);
  142. x = (nr & 1) ? ((x & 0x0f) | (val << 4)) : ((x & 0xf0) | val);
  143. pci_write_config_byte(router, reg, x);
  144. }
  145. /*
  146. * ALI pirq entries are damn ugly, and completely undocumented.
  147. * This has been figured out from pirq tables, and it's not a pretty
  148. * picture.
  149. */
  150. static int pirq_ali_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  151. {
  152. static unsigned char irqmap[16] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
  153. return irqmap[read_config_nybble(router, 0x48, pirq-1)];
  154. }
  155. static int pirq_ali_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  156. {
  157. static unsigned char irqmap[16] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
  158. unsigned int val = irqmap[irq];
  159. if (val) {
  160. write_config_nybble(router, 0x48, pirq-1, val);
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. /*
  166. * The Intel PIIX4 pirq rules are fairly simple: "pirq" is
  167. * just a pointer to the config space.
  168. */
  169. static int pirq_piix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  170. {
  171. u8 x;
  172. pci_read_config_byte(router, pirq, &x);
  173. return (x < 16) ? x : 0;
  174. }
  175. static int pirq_piix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  176. {
  177. pci_write_config_byte(router, pirq, irq);
  178. return 1;
  179. }
  180. /*
  181. * The VIA pirq rules are nibble-based, like ALI,
  182. * but without the ugly irq number munging.
  183. * However, PIRQD is in the upper instead of lower 4 bits.
  184. */
  185. static int pirq_via_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  186. {
  187. return read_config_nybble(router, 0x55, pirq == 4 ? 5 : pirq);
  188. }
  189. static int pirq_via_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  190. {
  191. write_config_nybble(router, 0x55, pirq == 4 ? 5 : pirq, irq);
  192. return 1;
  193. }
  194. /*
  195. * ITE 8330G pirq rules are nibble-based
  196. * FIXME: pirqmap may be { 1, 0, 3, 2 },
  197. * 2+3 are both mapped to irq 9 on my system
  198. */
  199. static int pirq_ite_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  200. {
  201. static unsigned char pirqmap[4] = { 1, 0, 2, 3 };
  202. return read_config_nybble(router,0x43, pirqmap[pirq-1]);
  203. }
  204. static int pirq_ite_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  205. {
  206. static unsigned char pirqmap[4] = { 1, 0, 2, 3 };
  207. write_config_nybble(router, 0x43, pirqmap[pirq-1], irq);
  208. return 1;
  209. }
  210. /*
  211. * OPTI: high four bits are nibble pointer..
  212. * I wonder what the low bits do?
  213. */
  214. static int pirq_opti_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  215. {
  216. return read_config_nybble(router, 0xb8, pirq >> 4);
  217. }
  218. static int pirq_opti_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  219. {
  220. write_config_nybble(router, 0xb8, pirq >> 4, irq);
  221. return 1;
  222. }
  223. /*
  224. * Cyrix: nibble offset 0x5C
  225. * 0x5C bits 7:4 is INTB bits 3:0 is INTA
  226. * 0x5D bits 7:4 is INTD bits 3:0 is INTC
  227. */
  228. static int pirq_cyrix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  229. {
  230. return read_config_nybble(router, 0x5C, (pirq-1)^1);
  231. }
  232. static int pirq_cyrix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  233. {
  234. write_config_nybble(router, 0x5C, (pirq-1)^1, irq);
  235. return 1;
  236. }
  237. /*
  238. * PIRQ routing for SiS 85C503 router used in several SiS chipsets.
  239. * We have to deal with the following issues here:
  240. * - vendors have different ideas about the meaning of link values
  241. * - some onboard devices (integrated in the chipset) have special
  242. * links and are thus routed differently (i.e. not via PCI INTA-INTD)
  243. * - different revision of the router have a different layout for
  244. * the routing registers, particularly for the onchip devices
  245. *
  246. * For all routing registers the common thing is we have one byte
  247. * per routeable link which is defined as:
  248. * bit 7 IRQ mapping enabled (0) or disabled (1)
  249. * bits [6:4] reserved (sometimes used for onchip devices)
  250. * bits [3:0] IRQ to map to
  251. * allowed: 3-7, 9-12, 14-15
  252. * reserved: 0, 1, 2, 8, 13
  253. *
  254. * The config-space registers located at 0x41/0x42/0x43/0x44 are
  255. * always used to route the normal PCI INT A/B/C/D respectively.
  256. * Apparently there are systems implementing PCI routing table using
  257. * link values 0x01-0x04 and others using 0x41-0x44 for PCI INTA..D.
  258. * We try our best to handle both link mappings.
  259. *
  260. * Currently (2003-05-21) it appears most SiS chipsets follow the
  261. * definition of routing registers from the SiS-5595 southbridge.
  262. * According to the SiS 5595 datasheets the revision id's of the
  263. * router (ISA-bridge) should be 0x01 or 0xb0.
  264. *
  265. * Furthermore we've also seen lspci dumps with revision 0x00 and 0xb1.
  266. * Looks like these are used in a number of SiS 5xx/6xx/7xx chipsets.
  267. * They seem to work with the current routing code. However there is
  268. * some concern because of the two USB-OHCI HCs (original SiS 5595
  269. * had only one). YMMV.
  270. *
  271. * Onchip routing for router rev-id 0x01/0xb0 and probably 0x00/0xb1:
  272. *
  273. * 0x61: IDEIRQ:
  274. * bits [6:5] must be written 01
  275. * bit 4 channel-select primary (0), secondary (1)
  276. *
  277. * 0x62: USBIRQ:
  278. * bit 6 OHCI function disabled (0), enabled (1)
  279. *
  280. * 0x6a: ACPI/SCI IRQ: bits 4-6 reserved
  281. *
  282. * 0x7e: Data Acq. Module IRQ - bits 4-6 reserved
  283. *
  284. * We support USBIRQ (in addition to INTA-INTD) and keep the
  285. * IDE, ACPI and DAQ routing untouched as set by the BIOS.
  286. *
  287. * Currently the only reported exception is the new SiS 65x chipset
  288. * which includes the SiS 69x southbridge. Here we have the 85C503
  289. * router revision 0x04 and there are changes in the register layout
  290. * mostly related to the different USB HCs with USB 2.0 support.
  291. *
  292. * Onchip routing for router rev-id 0x04 (try-and-error observation)
  293. *
  294. * 0x60/0x61/0x62/0x63: 1xEHCI and 3xOHCI (companion) USB-HCs
  295. * bit 6-4 are probably unused, not like 5595
  296. */
  297. #define PIRQ_SIS_IRQ_MASK 0x0f
  298. #define PIRQ_SIS_IRQ_DISABLE 0x80
  299. #define PIRQ_SIS_USB_ENABLE 0x40
  300. static int pirq_sis_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  301. {
  302. u8 x;
  303. int reg;
  304. reg = pirq;
  305. if (reg >= 0x01 && reg <= 0x04)
  306. reg += 0x40;
  307. pci_read_config_byte(router, reg, &x);
  308. return (x & PIRQ_SIS_IRQ_DISABLE) ? 0 : (x & PIRQ_SIS_IRQ_MASK);
  309. }
  310. static int pirq_sis_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  311. {
  312. u8 x;
  313. int reg;
  314. reg = pirq;
  315. if (reg >= 0x01 && reg <= 0x04)
  316. reg += 0x40;
  317. pci_read_config_byte(router, reg, &x);
  318. x &= ~(PIRQ_SIS_IRQ_MASK | PIRQ_SIS_IRQ_DISABLE);
  319. x |= irq ? irq: PIRQ_SIS_IRQ_DISABLE;
  320. pci_write_config_byte(router, reg, x);
  321. return 1;
  322. }
  323. /*
  324. * VLSI: nibble offset 0x74 - educated guess due to routing table and
  325. * config space of VLSI 82C534 PCI-bridge/router (1004:0102)
  326. * Tested on HP OmniBook 800 covering PIRQ 1, 2, 4, 8 for onboard
  327. * devices, PIRQ 3 for non-pci(!) soundchip and (untested) PIRQ 6
  328. * for the busbridge to the docking station.
  329. */
  330. static int pirq_vlsi_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  331. {
  332. if (pirq > 8) {
  333. printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
  334. return 0;
  335. }
  336. return read_config_nybble(router, 0x74, pirq-1);
  337. }
  338. static int pirq_vlsi_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  339. {
  340. if (pirq > 8) {
  341. printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
  342. return 0;
  343. }
  344. write_config_nybble(router, 0x74, pirq-1, irq);
  345. return 1;
  346. }
  347. /*
  348. * ServerWorks: PCI interrupts mapped to system IRQ lines through Index
  349. * and Redirect I/O registers (0x0c00 and 0x0c01). The Index register
  350. * format is (PCIIRQ## | 0x10), e.g.: PCIIRQ10=0x1a. The Redirect
  351. * register is a straight binary coding of desired PIC IRQ (low nibble).
  352. *
  353. * The 'link' value in the PIRQ table is already in the correct format
  354. * for the Index register. There are some special index values:
  355. * 0x00 for ACPI (SCI), 0x01 for USB, 0x02 for IDE0, 0x04 for IDE1,
  356. * and 0x03 for SMBus.
  357. */
  358. static int pirq_serverworks_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  359. {
  360. outb_p(pirq, 0xc00);
  361. return inb(0xc01) & 0xf;
  362. }
  363. static int pirq_serverworks_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  364. {
  365. outb_p(pirq, 0xc00);
  366. outb_p(irq, 0xc01);
  367. return 1;
  368. }
  369. /* Support for AMD756 PCI IRQ Routing
  370. * Jhon H. Caicedo <jhcaiced@osso.org.co>
  371. * Jun/21/2001 0.2.0 Release, fixed to use "nybble" functions... (jhcaiced)
  372. * Jun/19/2001 Alpha Release 0.1.0 (jhcaiced)
  373. * The AMD756 pirq rules are nibble-based
  374. * offset 0x56 0-3 PIRQA 4-7 PIRQB
  375. * offset 0x57 0-3 PIRQC 4-7 PIRQD
  376. */
  377. static int pirq_amd756_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  378. {
  379. u8 irq;
  380. irq = 0;
  381. if (pirq <= 4)
  382. {
  383. irq = read_config_nybble(router, 0x56, pirq - 1);
  384. }
  385. printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d get irq : %2d\n",
  386. dev->vendor, dev->device, pirq, irq);
  387. return irq;
  388. }
  389. static int pirq_amd756_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  390. {
  391. printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d SET irq : %2d\n",
  392. dev->vendor, dev->device, pirq, irq);
  393. if (pirq <= 4)
  394. {
  395. write_config_nybble(router, 0x56, pirq - 1, irq);
  396. }
  397. return 1;
  398. }
  399. #ifdef CONFIG_PCI_BIOS
  400. static int pirq_bios_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  401. {
  402. struct pci_dev *bridge;
  403. int pin = pci_get_interrupt_pin(dev, &bridge);
  404. return pcibios_set_irq_routing(bridge, pin, irq);
  405. }
  406. #endif
  407. static __init int intel_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  408. {
  409. static struct pci_device_id pirq_440gx[] = {
  410. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443GX_0) },
  411. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443GX_2) },
  412. { },
  413. };
  414. /* 440GX has a proprietary PIRQ router -- don't use it */
  415. if (pci_dev_present(pirq_440gx))
  416. return 0;
  417. switch(device)
  418. {
  419. case PCI_DEVICE_ID_INTEL_82371FB_0:
  420. case PCI_DEVICE_ID_INTEL_82371SB_0:
  421. case PCI_DEVICE_ID_INTEL_82371AB_0:
  422. case PCI_DEVICE_ID_INTEL_82371MX:
  423. case PCI_DEVICE_ID_INTEL_82443MX_0:
  424. case PCI_DEVICE_ID_INTEL_82801AA_0:
  425. case PCI_DEVICE_ID_INTEL_82801AB_0:
  426. case PCI_DEVICE_ID_INTEL_82801BA_0:
  427. case PCI_DEVICE_ID_INTEL_82801BA_10:
  428. case PCI_DEVICE_ID_INTEL_82801CA_0:
  429. case PCI_DEVICE_ID_INTEL_82801CA_12:
  430. case PCI_DEVICE_ID_INTEL_82801DB_0:
  431. case PCI_DEVICE_ID_INTEL_82801E_0:
  432. case PCI_DEVICE_ID_INTEL_82801EB_0:
  433. case PCI_DEVICE_ID_INTEL_ESB_1:
  434. case PCI_DEVICE_ID_INTEL_ICH6_0:
  435. case PCI_DEVICE_ID_INTEL_ICH6_1:
  436. case PCI_DEVICE_ID_INTEL_ICH7_0:
  437. case PCI_DEVICE_ID_INTEL_ICH7_1:
  438. case PCI_DEVICE_ID_INTEL_ESB2_0:
  439. r->name = "PIIX/ICH";
  440. r->get = pirq_piix_get;
  441. r->set = pirq_piix_set;
  442. return 1;
  443. }
  444. return 0;
  445. }
  446. static __init int via_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  447. {
  448. /* FIXME: We should move some of the quirk fixup stuff here */
  449. switch(device)
  450. {
  451. case PCI_DEVICE_ID_VIA_82C586_0:
  452. case PCI_DEVICE_ID_VIA_82C596:
  453. case PCI_DEVICE_ID_VIA_82C686:
  454. case PCI_DEVICE_ID_VIA_8231:
  455. /* FIXME: add new ones for 8233/5 */
  456. r->name = "VIA";
  457. r->get = pirq_via_get;
  458. r->set = pirq_via_set;
  459. return 1;
  460. }
  461. return 0;
  462. }
  463. static __init int vlsi_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  464. {
  465. switch(device)
  466. {
  467. case PCI_DEVICE_ID_VLSI_82C534:
  468. r->name = "VLSI 82C534";
  469. r->get = pirq_vlsi_get;
  470. r->set = pirq_vlsi_set;
  471. return 1;
  472. }
  473. return 0;
  474. }
  475. static __init int serverworks_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  476. {
  477. switch(device)
  478. {
  479. case PCI_DEVICE_ID_SERVERWORKS_OSB4:
  480. case PCI_DEVICE_ID_SERVERWORKS_CSB5:
  481. r->name = "ServerWorks";
  482. r->get = pirq_serverworks_get;
  483. r->set = pirq_serverworks_set;
  484. return 1;
  485. }
  486. return 0;
  487. }
  488. static __init int sis_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  489. {
  490. if (device != PCI_DEVICE_ID_SI_503)
  491. return 0;
  492. r->name = "SIS";
  493. r->get = pirq_sis_get;
  494. r->set = pirq_sis_set;
  495. return 1;
  496. }
  497. static __init int cyrix_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  498. {
  499. switch(device)
  500. {
  501. case PCI_DEVICE_ID_CYRIX_5520:
  502. r->name = "NatSemi";
  503. r->get = pirq_cyrix_get;
  504. r->set = pirq_cyrix_set;
  505. return 1;
  506. }
  507. return 0;
  508. }
  509. static __init int opti_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  510. {
  511. switch(device)
  512. {
  513. case PCI_DEVICE_ID_OPTI_82C700:
  514. r->name = "OPTI";
  515. r->get = pirq_opti_get;
  516. r->set = pirq_opti_set;
  517. return 1;
  518. }
  519. return 0;
  520. }
  521. static __init int ite_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  522. {
  523. switch(device)
  524. {
  525. case PCI_DEVICE_ID_ITE_IT8330G_0:
  526. r->name = "ITE";
  527. r->get = pirq_ite_get;
  528. r->set = pirq_ite_set;
  529. return 1;
  530. }
  531. return 0;
  532. }
  533. static __init int ali_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  534. {
  535. switch(device)
  536. {
  537. case PCI_DEVICE_ID_AL_M1533:
  538. case PCI_DEVICE_ID_AL_M1563:
  539. printk("PCI: Using ALI IRQ Router\n");
  540. r->name = "ALI";
  541. r->get = pirq_ali_get;
  542. r->set = pirq_ali_set;
  543. return 1;
  544. }
  545. return 0;
  546. }
  547. static __init int amd_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
  548. {
  549. switch(device)
  550. {
  551. case PCI_DEVICE_ID_AMD_VIPER_740B:
  552. r->name = "AMD756";
  553. break;
  554. case PCI_DEVICE_ID_AMD_VIPER_7413:
  555. r->name = "AMD766";
  556. break;
  557. case PCI_DEVICE_ID_AMD_VIPER_7443:
  558. r->name = "AMD768";
  559. break;
  560. default:
  561. return 0;
  562. }
  563. r->get = pirq_amd756_get;
  564. r->set = pirq_amd756_set;
  565. return 1;
  566. }
  567. static __initdata struct irq_router_handler pirq_routers[] = {
  568. { PCI_VENDOR_ID_INTEL, intel_router_probe },
  569. { PCI_VENDOR_ID_AL, ali_router_probe },
  570. { PCI_VENDOR_ID_ITE, ite_router_probe },
  571. { PCI_VENDOR_ID_VIA, via_router_probe },
  572. { PCI_VENDOR_ID_OPTI, opti_router_probe },
  573. { PCI_VENDOR_ID_SI, sis_router_probe },
  574. { PCI_VENDOR_ID_CYRIX, cyrix_router_probe },
  575. { PCI_VENDOR_ID_VLSI, vlsi_router_probe },
  576. { PCI_VENDOR_ID_SERVERWORKS, serverworks_router_probe },
  577. { PCI_VENDOR_ID_AMD, amd_router_probe },
  578. /* Someone with docs needs to add the ATI Radeon IGP */
  579. { 0, NULL }
  580. };
  581. static struct irq_router pirq_router;
  582. static struct pci_dev *pirq_router_dev;
  583. /*
  584. * FIXME: should we have an option to say "generic for
  585. * chipset" ?
  586. */
  587. static void __init pirq_find_router(struct irq_router *r)
  588. {
  589. struct irq_routing_table *rt = pirq_table;
  590. struct irq_router_handler *h;
  591. #ifdef CONFIG_PCI_BIOS
  592. if (!rt->signature) {
  593. printk(KERN_INFO "PCI: Using BIOS for IRQ routing\n");
  594. r->set = pirq_bios_set;
  595. r->name = "BIOS";
  596. return;
  597. }
  598. #endif
  599. /* Default unless a driver reloads it */
  600. r->name = "default";
  601. r->get = NULL;
  602. r->set = NULL;
  603. DBG("PCI: Attempting to find IRQ router for %04x:%04x\n",
  604. rt->rtr_vendor, rt->rtr_device);
  605. pirq_router_dev = pci_find_slot(rt->rtr_bus, rt->rtr_devfn);
  606. if (!pirq_router_dev) {
  607. DBG("PCI: Interrupt router not found at %02x:%02x\n", rt->rtr_bus, rt->rtr_devfn);
  608. return;
  609. }
  610. for( h = pirq_routers; h->vendor; h++) {
  611. /* First look for a router match */
  612. if (rt->rtr_vendor == h->vendor && h->probe(r, pirq_router_dev, rt->rtr_device))
  613. break;
  614. /* Fall back to a device match */
  615. if (pirq_router_dev->vendor == h->vendor && h->probe(r, pirq_router_dev, pirq_router_dev->device))
  616. break;
  617. }
  618. printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %s\n",
  619. pirq_router.name,
  620. pirq_router_dev->vendor,
  621. pirq_router_dev->device,
  622. pci_name(pirq_router_dev));
  623. }
  624. static struct irq_info *pirq_get_info(struct pci_dev *dev)
  625. {
  626. struct irq_routing_table *rt = pirq_table;
  627. int entries = (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
  628. struct irq_info *info;
  629. for (info = rt->slots; entries--; info++)
  630. if (info->bus == dev->bus->number && PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn))
  631. return info;
  632. return NULL;
  633. }
  634. static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
  635. {
  636. u8 pin;
  637. struct irq_info *info;
  638. int i, pirq, newirq;
  639. int irq = 0;
  640. u32 mask;
  641. struct irq_router *r = &pirq_router;
  642. struct pci_dev *dev2 = NULL;
  643. char *msg = NULL;
  644. /* Find IRQ pin */
  645. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  646. if (!pin) {
  647. DBG(" -> no interrupt pin\n");
  648. return 0;
  649. }
  650. pin = pin - 1;
  651. /* Find IRQ routing entry */
  652. if (!pirq_table)
  653. return 0;
  654. DBG("IRQ for %s[%c]", pci_name(dev), 'A' + pin);
  655. info = pirq_get_info(dev);
  656. if (!info) {
  657. DBG(" -> not found in routing table\n");
  658. return 0;
  659. }
  660. pirq = info->irq[pin].link;
  661. mask = info->irq[pin].bitmap;
  662. if (!pirq) {
  663. DBG(" -> not routed\n");
  664. return 0;
  665. }
  666. DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask, pirq_table->exclusive_irqs);
  667. mask &= pcibios_irq_mask;
  668. /* Work around broken HP Pavilion Notebooks which assign USB to
  669. IRQ 9 even though it is actually wired to IRQ 11 */
  670. if (broken_hp_bios_irq9 && pirq == 0x59 && dev->irq == 9) {
  671. dev->irq = 11;
  672. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 11);
  673. r->set(pirq_router_dev, dev, pirq, 11);
  674. }
  675. /* same for Acer Travelmate 360, but with CB and irq 11 -> 10 */
  676. if (acer_tm360_irqrouting && dev->irq == 11 && dev->vendor == PCI_VENDOR_ID_O2) {
  677. pirq = 0x68;
  678. mask = 0x400;
  679. dev->irq = r->get(pirq_router_dev, dev, pirq);
  680. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  681. }
  682. /*
  683. * Find the best IRQ to assign: use the one
  684. * reported by the device if possible.
  685. */
  686. newirq = dev->irq;
  687. if (!((1 << newirq) & mask)) {
  688. if ( pci_probe & PCI_USE_PIRQ_MASK) newirq = 0;
  689. else printk(KERN_WARNING "PCI: IRQ %i for device %s doesn't match PIRQ mask - try pci=usepirqmask\n", newirq, pci_name(dev));
  690. }
  691. if (!newirq && assign) {
  692. for (i = 0; i < 16; i++) {
  693. if (!(mask & (1 << i)))
  694. continue;
  695. if (pirq_penalty[i] < pirq_penalty[newirq] && can_request_irq(i, SA_SHIRQ))
  696. newirq = i;
  697. }
  698. }
  699. DBG(" -> newirq=%d", newirq);
  700. /* Check if it is hardcoded */
  701. if ((pirq & 0xf0) == 0xf0) {
  702. irq = pirq & 0xf;
  703. DBG(" -> hardcoded IRQ %d\n", irq);
  704. msg = "Hardcoded";
  705. } else if ( r->get && (irq = r->get(pirq_router_dev, dev, pirq)) && \
  706. ((!(pci_probe & PCI_USE_PIRQ_MASK)) || ((1 << irq) & mask)) ) {
  707. DBG(" -> got IRQ %d\n", irq);
  708. msg = "Found";
  709. } else if (newirq && r->set && (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) {
  710. DBG(" -> assigning IRQ %d", newirq);
  711. if (r->set(pirq_router_dev, dev, pirq, newirq)) {
  712. eisa_set_level_irq(newirq);
  713. DBG(" ... OK\n");
  714. msg = "Assigned";
  715. irq = newirq;
  716. }
  717. }
  718. if (!irq) {
  719. DBG(" ... failed\n");
  720. if (newirq && mask == (1 << newirq)) {
  721. msg = "Guessed";
  722. irq = newirq;
  723. } else
  724. return 0;
  725. }
  726. printk(KERN_INFO "PCI: %s IRQ %d for device %s\n", msg, irq, pci_name(dev));
  727. /* Update IRQ for all devices with the same pirq value */
  728. while ((dev2 = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev2)) != NULL) {
  729. pci_read_config_byte(dev2, PCI_INTERRUPT_PIN, &pin);
  730. if (!pin)
  731. continue;
  732. pin--;
  733. info = pirq_get_info(dev2);
  734. if (!info)
  735. continue;
  736. if (info->irq[pin].link == pirq) {
  737. /* We refuse to override the dev->irq information. Give a warning! */
  738. if ( dev2->irq && dev2->irq != irq && \
  739. (!(pci_probe & PCI_USE_PIRQ_MASK) || \
  740. ((1 << dev2->irq) & mask)) ) {
  741. #ifndef CONFIG_PCI_MSI
  742. printk(KERN_INFO "IRQ routing conflict for %s, have irq %d, want irq %d\n",
  743. pci_name(dev2), dev2->irq, irq);
  744. #endif
  745. continue;
  746. }
  747. dev2->irq = irq;
  748. pirq_penalty[irq]++;
  749. if (dev != dev2)
  750. printk(KERN_INFO "PCI: Sharing IRQ %d with %s\n", irq, pci_name(dev2));
  751. }
  752. }
  753. return 1;
  754. }
  755. static void __init pcibios_fixup_irqs(void)
  756. {
  757. struct pci_dev *dev = NULL;
  758. u8 pin;
  759. DBG("PCI: IRQ fixup\n");
  760. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  761. /*
  762. * If the BIOS has set an out of range IRQ number, just ignore it.
  763. * Also keep track of which IRQ's are already in use.
  764. */
  765. if (dev->irq >= 16) {
  766. DBG("%s: ignoring bogus IRQ %d\n", pci_name(dev), dev->irq);
  767. dev->irq = 0;
  768. }
  769. /* If the IRQ is already assigned to a PCI device, ignore its ISA use penalty */
  770. if (pirq_penalty[dev->irq] >= 100 && pirq_penalty[dev->irq] < 100000)
  771. pirq_penalty[dev->irq] = 0;
  772. pirq_penalty[dev->irq]++;
  773. }
  774. dev = NULL;
  775. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  776. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  777. #ifdef CONFIG_X86_IO_APIC
  778. /*
  779. * Recalculate IRQ numbers if we use the I/O APIC.
  780. */
  781. if (io_apic_assign_pci_irqs)
  782. {
  783. int irq;
  784. if (pin) {
  785. pin--; /* interrupt pins are numbered starting from 1 */
  786. irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
  787. /*
  788. * Busses behind bridges are typically not listed in the MP-table.
  789. * In this case we have to look up the IRQ based on the parent bus,
  790. * parent slot, and pin number. The SMP code detects such bridged
  791. * busses itself so we should get into this branch reliably.
  792. */
  793. if (irq < 0 && dev->bus->parent) { /* go back to the bridge */
  794. struct pci_dev * bridge = dev->bus->self;
  795. pin = (pin + PCI_SLOT(dev->devfn)) % 4;
  796. irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number,
  797. PCI_SLOT(bridge->devfn), pin);
  798. if (irq >= 0)
  799. printk(KERN_WARNING "PCI: using PPB %s[%c] to get irq %d\n",
  800. pci_name(bridge), 'A' + pin, irq);
  801. }
  802. if (irq >= 0) {
  803. if (use_pci_vector() &&
  804. !platform_legacy_irq(irq))
  805. irq = IO_APIC_VECTOR(irq);
  806. printk(KERN_INFO "PCI->APIC IRQ transform: %s[%c] -> IRQ %d\n",
  807. pci_name(dev), 'A' + pin, irq);
  808. dev->irq = irq;
  809. }
  810. }
  811. }
  812. #endif
  813. /*
  814. * Still no IRQ? Try to lookup one...
  815. */
  816. if (pin && !dev->irq)
  817. pcibios_lookup_irq(dev, 0);
  818. }
  819. }
  820. /*
  821. * Work around broken HP Pavilion Notebooks which assign USB to
  822. * IRQ 9 even though it is actually wired to IRQ 11
  823. */
  824. static int __init fix_broken_hp_bios_irq9(struct dmi_system_id *d)
  825. {
  826. if (!broken_hp_bios_irq9) {
  827. broken_hp_bios_irq9 = 1;
  828. printk(KERN_INFO "%s detected - fixing broken IRQ routing\n", d->ident);
  829. }
  830. return 0;
  831. }
  832. /*
  833. * Work around broken Acer TravelMate 360 Notebooks which assign
  834. * Cardbus to IRQ 11 even though it is actually wired to IRQ 10
  835. */
  836. static int __init fix_acer_tm360_irqrouting(struct dmi_system_id *d)
  837. {
  838. if (!acer_tm360_irqrouting) {
  839. acer_tm360_irqrouting = 1;
  840. printk(KERN_INFO "%s detected - fixing broken IRQ routing\n", d->ident);
  841. }
  842. return 0;
  843. }
  844. static struct dmi_system_id __initdata pciirq_dmi_table[] = {
  845. {
  846. .callback = fix_broken_hp_bios_irq9,
  847. .ident = "HP Pavilion N5400 Series Laptop",
  848. .matches = {
  849. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  850. DMI_MATCH(DMI_BIOS_VERSION, "GE.M1.03"),
  851. DMI_MATCH(DMI_PRODUCT_VERSION, "HP Pavilion Notebook Model GE"),
  852. DMI_MATCH(DMI_BOARD_VERSION, "OmniBook N32N-736"),
  853. },
  854. },
  855. {
  856. .callback = fix_acer_tm360_irqrouting,
  857. .ident = "Acer TravelMate 36x Laptop",
  858. .matches = {
  859. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  860. DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 360"),
  861. },
  862. },
  863. { }
  864. };
  865. static int __init pcibios_irq_init(void)
  866. {
  867. DBG("PCI: IRQ init\n");
  868. if (pcibios_enable_irq || raw_pci_ops == NULL)
  869. return 0;
  870. dmi_check_system(pciirq_dmi_table);
  871. pirq_table = pirq_find_routing_table();
  872. #ifdef CONFIG_PCI_BIOS
  873. if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
  874. pirq_table = pcibios_get_irq_routing_table();
  875. #endif
  876. if (pirq_table) {
  877. pirq_peer_trick();
  878. pirq_find_router(&pirq_router);
  879. if (pirq_table->exclusive_irqs) {
  880. int i;
  881. for (i=0; i<16; i++)
  882. if (!(pirq_table->exclusive_irqs & (1 << i)))
  883. pirq_penalty[i] += 100;
  884. }
  885. /* If we're using the I/O APIC, avoid using the PCI IRQ routing table */
  886. if (io_apic_assign_pci_irqs)
  887. pirq_table = NULL;
  888. }
  889. pcibios_enable_irq = pirq_enable_irq;
  890. pcibios_fixup_irqs();
  891. return 0;
  892. }
  893. subsys_initcall(pcibios_irq_init);
  894. static void pirq_penalize_isa_irq(int irq)
  895. {
  896. /*
  897. * If any ISAPnP device reports an IRQ in its list of possible
  898. * IRQ's, we try to avoid assigning it to PCI devices.
  899. */
  900. if (irq < 16)
  901. pirq_penalty[irq] += 100;
  902. }
  903. void pcibios_penalize_isa_irq(int irq)
  904. {
  905. #ifdef CONFIG_ACPI_PCI
  906. if (!acpi_noirq)
  907. acpi_penalize_isa_irq(irq);
  908. else
  909. #endif
  910. pirq_penalize_isa_irq(irq);
  911. }
  912. static int pirq_enable_irq(struct pci_dev *dev)
  913. {
  914. u8 pin;
  915. extern int via_interrupt_line_quirk;
  916. struct pci_dev *temp_dev;
  917. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  918. if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) {
  919. char *msg = "";
  920. pin--; /* interrupt pins are numbered starting from 1 */
  921. if (io_apic_assign_pci_irqs) {
  922. int irq;
  923. irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
  924. /*
  925. * Busses behind bridges are typically not listed in the MP-table.
  926. * In this case we have to look up the IRQ based on the parent bus,
  927. * parent slot, and pin number. The SMP code detects such bridged
  928. * busses itself so we should get into this branch reliably.
  929. */
  930. temp_dev = dev;
  931. while (irq < 0 && dev->bus->parent) { /* go back to the bridge */
  932. struct pci_dev * bridge = dev->bus->self;
  933. pin = (pin + PCI_SLOT(dev->devfn)) % 4;
  934. irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number,
  935. PCI_SLOT(bridge->devfn), pin);
  936. if (irq >= 0)
  937. printk(KERN_WARNING "PCI: using PPB %s[%c] to get irq %d\n",
  938. pci_name(bridge), 'A' + pin, irq);
  939. dev = bridge;
  940. }
  941. dev = temp_dev;
  942. if (irq >= 0) {
  943. #ifdef CONFIG_PCI_MSI
  944. if (!platform_legacy_irq(irq))
  945. irq = IO_APIC_VECTOR(irq);
  946. #endif
  947. printk(KERN_INFO "PCI->APIC IRQ transform: %s[%c] -> IRQ %d\n",
  948. pci_name(dev), 'A' + pin, irq);
  949. dev->irq = irq;
  950. return 0;
  951. } else
  952. msg = " Probably buggy MP table.";
  953. } else if (pci_probe & PCI_BIOS_IRQ_SCAN)
  954. msg = "";
  955. else
  956. msg = " Please try using pci=biosirq.";
  957. /* With IDE legacy devices the IRQ lookup failure is not a problem.. */
  958. if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE && !(dev->class & 0x5))
  959. return 0;
  960. printk(KERN_WARNING "PCI: No IRQ known for interrupt pin %c of device %s.%s\n",
  961. 'A' + pin, pci_name(dev), msg);
  962. }
  963. /* VIA bridges use interrupt line for apic/pci steering across
  964. the V-Link */
  965. else if (via_interrupt_line_quirk)
  966. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq & 15);
  967. return 0;
  968. }
  969. int pci_vector_resources(int last, int nr_released)
  970. {
  971. int count = nr_released;
  972. int next = last;
  973. int offset = (last % 8);
  974. while (next < FIRST_SYSTEM_VECTOR) {
  975. next += 8;
  976. #ifdef CONFIG_X86_64
  977. if (next == IA32_SYSCALL_VECTOR)
  978. continue;
  979. #else
  980. if (next == SYSCALL_VECTOR)
  981. continue;
  982. #endif
  983. count++;
  984. if (next >= FIRST_SYSTEM_VECTOR) {
  985. if (offset%8) {
  986. next = FIRST_DEVICE_VECTOR + offset;
  987. offset++;
  988. continue;
  989. }
  990. count--;
  991. }
  992. }
  993. return count;
  994. }