pci-irq.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:21k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 <asm/io.h>
  15. #include <asm/smp.h>
  16. #include <asm/io_apic.h>
  17. #include "pci-x86_64.h"
  18. #define PIRQ_SIGNATURE (('$' << 0) + ('P' << 8) + ('I' << 16) + ('R' << 24))
  19. #define PIRQ_VERSION 0x0100
  20. static struct irq_routing_table *pirq_table;
  21. /*
  22.  * Never use: 0, 1, 2 (timer, keyboard, and cascade)
  23.  * Avoid using: 13, 14 and 15 (FP error and IDE).
  24.  * Penalize: 3, 4, 6, 7, 12 (known ISA uses: serial, floppy, parallel and mouse)
  25.  */
  26. unsigned int pcibios_irq_mask = 0xfff8;
  27. static int pirq_penalty[16] = {
  28. 1000000, 1000000, 1000000, 1000, 1000, 0, 1000, 1000,
  29. 0, 0, 0, 0, 1000, 100000, 100000, 100000
  30. };
  31. struct irq_router {
  32. char *name;
  33. u16 vendor, device;
  34. int (*get)(struct pci_dev *router, struct pci_dev *dev, int pirq);
  35. int (*set)(struct pci_dev *router, struct pci_dev *dev, int pirq, int new);
  36. };
  37. /*
  38.  *  Search 0xf0000 -- 0xfffff for the PCI IRQ Routing Table.
  39.  */
  40. static struct irq_routing_table * __init pirq_find_routing_table(void)
  41. {
  42. u8 *addr;
  43. struct irq_routing_table *rt;
  44. int i;
  45. u8 sum;
  46. for(addr = (u8 *) __va(0xf0000); addr < (u8 *) __va(0x100000); addr += 16) {
  47. rt = (struct irq_routing_table *) addr;
  48. if (rt->signature != PIRQ_SIGNATURE ||
  49.     rt->version != PIRQ_VERSION ||
  50.     rt->size % 16 ||
  51.     rt->size < sizeof(struct irq_routing_table))
  52. continue;
  53. sum = 0;
  54. for(i=0; i<rt->size; i++)
  55. sum += addr[i];
  56. if (!sum) {
  57. DBG("PCI: Interrupt Routing Table found at 0x%pn", rt);
  58. return rt;
  59. }
  60. }
  61. return NULL;
  62. }
  63. /*
  64.  *  If we have a IRQ routing table, use it to search for peer host
  65.  *  bridges.  It's a gross hack, but since there are no other known
  66.  *  ways how to get a list of buses, we have to go this way.
  67.  *
  68.  *  [maybe x86-64 architecture should define way to query this info in
  69.  more reasonable way?]
  70.  */
  71. static void __init pirq_peer_trick(void)
  72. {
  73. struct irq_routing_table *rt = pirq_table;
  74. u8 busmap[256];
  75. int i;
  76. struct irq_info *e;
  77. memset(busmap, 0, sizeof(busmap));
  78. for(i=0; i < (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info); i++) {
  79. e = &rt->slots[i];
  80. #ifdef DEBUG
  81. {
  82. int j;
  83. DBG("%02x:%02x slot=%02x", e->bus, e->devfn/8, e->slot);
  84. for(j=0; j<4; j++)
  85. DBG(" %d:%02x/%04x", j, e->irq[j].link, e->irq[j].bitmap);
  86. DBG("n");
  87. }
  88. #endif
  89. busmap[e->bus] = 1;
  90. }
  91. for(i=1; i<256; i++)
  92. /*
  93.  *  It might be a secondary bus, but in this case its parent is already
  94.  *  known (ascending bus order) and therefore pci_scan_bus returns immediately.
  95.  */
  96. if (busmap[i] && pci_scan_bus(i, pci_root_bus->ops, NULL))
  97. printk(KERN_INFO "PCI: Discovered primary peer bus %02x [IRQ]n", i);
  98. pcibios_last_bus = -1;
  99. }
  100. /*
  101.  *  Code for querying and setting of IRQ routes on various interrupt routers.
  102.  */
  103. static void eisa_set_level_irq(unsigned int irq)
  104. {
  105. unsigned char mask = 1 << (irq & 7);
  106. unsigned int port = 0x4d0 + (irq >> 3);
  107. unsigned char val = inb(port);
  108. if (!(val & mask)) {
  109. DBG(" -> edge");
  110. outb(val | mask, port);
  111. }
  112. }
  113. /*
  114.  * Common IRQ routing practice: nybbles in config space,
  115.  * offset by some magic constant.
  116.  */
  117. static unsigned int read_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr)
  118. {
  119. u8 x;
  120. unsigned reg = offset + (nr >> 1);
  121. pci_read_config_byte(router, reg, &x);
  122. return (nr & 1) ? (x >> 4) : (x & 0xf);
  123. }
  124. static void write_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr, unsigned int val)
  125. {
  126. u8 x;
  127. unsigned reg = offset + (nr >> 1);
  128. pci_read_config_byte(router, reg, &x);
  129. x = (nr & 1) ? ((x & 0x0f) | (val << 4)) : ((x & 0xf0) | val);
  130. pci_write_config_byte(router, reg, x);
  131. }
  132. /*
  133.  * ALI pirq entries are damn ugly, and completely undocumented.
  134.  * This has been figured out from pirq tables, and it's not a pretty
  135.  * picture.
  136.  */
  137. static int pirq_ali_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  138. {
  139. static unsigned char irqmap[16] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
  140. return irqmap[read_config_nybble(router, 0x48, pirq-1)];
  141. }
  142. static int pirq_ali_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  143. {
  144. static unsigned char irqmap[16] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
  145. unsigned int val = irqmap[irq];
  146. if (val) {
  147. write_config_nybble(router, 0x48, pirq-1, val);
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. /*
  153.  * The Intel PIIX4 pirq rules are fairly simple: "pirq" is
  154.  * just a pointer to the config space.
  155.  */
  156. static int pirq_piix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  157. {
  158. u8 x;
  159. pci_read_config_byte(router, pirq, &x);
  160. return (x < 16) ? x : 0;
  161. }
  162. static int pirq_piix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  163. {
  164. pci_write_config_byte(router, pirq, irq);
  165. return 1;
  166. }
  167. /*
  168.  * The VIA pirq rules are nibble-based, like ALI,
  169.  * but without the ugly irq number munging.
  170.  */
  171. static int pirq_via_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  172. {
  173. return read_config_nybble(router, 0x55, pirq);
  174. }
  175. static int pirq_via_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  176. {
  177. write_config_nybble(router, 0x55, pirq, irq);
  178. return 1;
  179. }
  180. /*
  181.  * OPTI: high four bits are nibble pointer..
  182.  * I wonder what the low bits do?
  183.  */
  184. static int pirq_opti_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  185. {
  186. return read_config_nybble(router, 0xb8, pirq >> 4);
  187. }
  188. static int pirq_opti_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  189. {
  190. write_config_nybble(router, 0xb8, pirq >> 4, irq);
  191. return 1;
  192. }
  193. /*
  194.  * Cyrix: nibble offset 0x5C
  195.  */
  196. static int pirq_cyrix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  197. {
  198. return read_config_nybble(router, 0x5C, pirq-1);
  199. }
  200. static int pirq_cyrix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  201. {
  202. write_config_nybble(router, 0x5C, pirq-1, irq);
  203. return 1;
  204. }
  205. /*
  206.  * PIRQ routing for SiS 85C503 router used in several SiS chipsets
  207.  * According to the SiS 5595 datasheet (preliminary V1.0, 12/24/1997)
  208.  * the related registers work as follows:
  209.  *
  210.  * general: one byte per re-routable IRQ,
  211.  *  bit 7      IRQ mapping enabled (0) or disabled (1)
  212.  *  bits [6:4] reserved
  213.  *  bits [3:0] IRQ to map to
  214.  *      allowed: 3-7, 9-12, 14-15
  215.  *      reserved: 0, 1, 2, 8, 13
  216.  *
  217.  * individual registers in device config space:
  218.  *
  219.  * 0x41/0x42/0x43/0x44: PCI INT A/B/C/D - bits as in general case
  220.  *
  221.  * 0x61: IDEIRQ: bits as in general case - but:
  222.  * bits [6:5] must be written 01
  223.  * bit 4 channel-select primary (0), secondary (1)
  224.  *
  225.  * 0x62: USBIRQ: bits as in general case - but:
  226.  * bit 4 OHCI function disabled (0), enabled (1)
  227.  *
  228.  * 0x6a: ACPI/SCI IRQ - bits as in general case
  229.  *
  230.  * 0x7e: Data Acq. Module IRQ - bits as in general case
  231.  *
  232.  * Apparently there are systems implementing PCI routing table using both
  233.  * link values 0x01-0x04 and 0x41-0x44 for PCI INTA..D, but register offsets
  234.  * like 0x62 as link values for USBIRQ e.g. So there is no simple
  235.  * "register = offset + pirq" relation.
  236.  * Currently we support PCI INTA..D and USBIRQ and try our best to handle
  237.  * both link mappings.
  238.  * IDE/ACPI/DAQ mapping is currently unsupported (left untouched as set by BIOS).
  239.  */
  240. static int pirq_sis_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  241. {
  242. u8 x;
  243. int reg = pirq;
  244. switch(pirq) {
  245. case 0x01:
  246. case 0x02:
  247. case 0x03:
  248. case 0x04:
  249. reg += 0x40;
  250. case 0x41:
  251. case 0x42:
  252. case 0x43:
  253. case 0x44:
  254. case 0x62:
  255. pci_read_config_byte(router, reg, &x);
  256. if (reg != 0x62)
  257. break;
  258. if (!(x & 0x40))
  259. return 0;
  260. break;
  261. case 0x61:
  262. case 0x6a:
  263. case 0x7e:
  264. printk(KERN_INFO "SiS pirq: advanced IDE/ACPI/DAQ mapping not yet implementedn");
  265. return 0;
  266. default:
  267. printk(KERN_INFO "SiS router pirq escape (%d)n", pirq);
  268. return 0;
  269. }
  270. return (x & 0x80) ? 0 : (x & 0x0f);
  271. }
  272. static int pirq_sis_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  273. {
  274. u8 x;
  275. int reg = pirq;
  276. switch(pirq) {
  277. case 0x01:
  278. case 0x02:
  279. case 0x03:
  280. case 0x04:
  281. reg += 0x40;
  282. case 0x41:
  283. case 0x42:
  284. case 0x43:
  285. case 0x44:
  286. case 0x62:
  287. x = (irq&0x0f) ? (irq&0x0f) : 0x80;
  288. if (reg != 0x62)
  289. break;
  290. /* always mark OHCI enabled, as nothing else knows about this */
  291. x |= 0x40;
  292. break;
  293. case 0x61:
  294. case 0x6a:
  295. case 0x7e:
  296. printk(KERN_INFO "advanced SiS pirq mapping not yet implementedn");
  297. return 0;
  298. default:
  299. printk(KERN_INFO "SiS router pirq escape (%d)n", pirq);
  300. return 0;
  301. }
  302. pci_write_config_byte(router, reg, x);
  303. return 1;
  304. }
  305. /*
  306.  * VLSI: nibble offset 0x74 - educated guess due to routing table and
  307.  *       config space of VLSI 82C534 PCI-bridge/router (1004:0102)
  308.  *       Tested on HP OmniBook 800 covering PIRQ 1, 2, 4, 8 for onboard
  309.  *       devices, PIRQ 3 for non-pci(!) soundchip and (untested) PIRQ 6
  310.  *       for the busbridge to the docking station.
  311.  */
  312. static int pirq_vlsi_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  313. {
  314. if (pirq > 8) {
  315. printk(KERN_INFO "VLSI router pirq escape (%d)n", pirq);
  316. return 0;
  317. }
  318. return read_config_nybble(router, 0x74, pirq-1);
  319. }
  320. static int pirq_vlsi_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  321. {
  322. if (pirq > 8) {
  323. printk(KERN_INFO "VLSI router pirq escape (%d)n", pirq);
  324. return 0;
  325. }
  326. write_config_nybble(router, 0x74, pirq-1, irq);
  327. return 1;
  328. }
  329. /*
  330.  * ServerWorks: PCI interrupts mapped to system IRQ lines through Index
  331.  * and Redirect I/O registers (0x0c00 and 0x0c01).  The Index register
  332.  * format is (PCIIRQ## | 0x10), e.g.: PCIIRQ10=0x1a.  The Redirect
  333.  * register is a straight binary coding of desired PIC IRQ (low nibble).
  334.  *
  335.  * The 'link' value in the PIRQ table is already in the correct format
  336.  * for the Index register.  There are some special index values:
  337.  * 0x00 for ACPI (SCI), 0x01 for USB, 0x02 for IDE0, 0x04 for IDE1,
  338.  * and 0x03 for SMBus.
  339.  */
  340. static int pirq_serverworks_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  341. {
  342. outb_p(pirq, 0xc00);
  343. return inb(0xc01) & 0xf;
  344. }
  345. static int pirq_serverworks_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  346. {
  347. outb_p(pirq, 0xc00);
  348. outb_p(irq, 0xc01);
  349. return 1;
  350. }
  351. /* Support for AMD756 PCI IRQ Routing
  352.  * Jhon H. Caicedo <jhcaiced@osso.org.co>
  353.  * Jun/21/2001 0.2.0 Release, fixed to use "nybble" functions... (jhcaiced)
  354.  * Jun/19/2001 Alpha Release 0.1.0 (jhcaiced)
  355.  * The AMD756 pirq rules are nibble-based
  356.  * offset 0x56 0-3 PIRQA  4-7  PIRQB
  357.  * offset 0x57 0-3 PIRQC  4-7  PIRQD
  358.  */
  359. static int pirq_amd756_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
  360. {
  361. u8 irq;
  362. irq = 0;
  363. if (pirq <= 4)
  364. {
  365. irq = read_config_nybble(router, 0x56, pirq - 1);
  366. }
  367. printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d get irq : %2dn",
  368. dev->vendor, dev->device, pirq, irq);
  369. return irq;
  370. }
  371. static int pirq_amd756_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  372. {
  373. printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d SET irq : %2dn", 
  374. dev->vendor, dev->device, pirq, irq);
  375. if (pirq <= 4)
  376. {
  377. write_config_nybble(router, 0x56, pirq - 1, irq);
  378. }
  379. return 1;
  380. }
  381. #ifdef CONFIG_PCI_BIOS
  382. static int pirq_bios_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
  383. {
  384. struct pci_dev *bridge;
  385. int pin = pci_get_interrupt_pin(dev, &bridge);
  386. return pcibios_set_irq_routing(bridge, pin, irq);
  387. }
  388. static struct irq_router pirq_bios_router =
  389. { "BIOS", 0, 0, NULL, pirq_bios_set };
  390. #endif
  391. static struct irq_router pirq_routers[] = {
  392. { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371FB_0, pirq_piix_get, pirq_piix_set },
  393. { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0, pirq_piix_get, pirq_piix_set },
  394. { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0, pirq_piix_get, pirq_piix_set },
  395. { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371MX,   pirq_piix_get, pirq_piix_set },
  396. { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_0, pirq_piix_get, pirq_piix_set },
  397. { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, pirq_piix_get, pirq_piix_set },
  398. { "ALI", PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, pirq_ali_get, pirq_ali_set },
  399. { "VIA", PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_0, pirq_via_get, pirq_via_set },
  400. { "VIA", PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C596, pirq_via_get, pirq_via_set },
  401. { "VIA", PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686, pirq_via_get, pirq_via_set },
  402. { "OPTI", PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C700, pirq_opti_get, pirq_opti_set },
  403. { "NatSemi", PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5520, pirq_cyrix_get, pirq_cyrix_set },
  404. { "SIS", PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503, pirq_sis_get, pirq_sis_set },
  405. { "VLSI 82C534", PCI_VENDOR_ID_VLSI, PCI_DEVICE_ID_VLSI_82C534, pirq_vlsi_get, pirq_vlsi_set },
  406. { "ServerWorks", PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4,
  407.   pirq_serverworks_get, pirq_serverworks_set },
  408. { "AMD756 VIPER", PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_740B,
  409. pirq_amd756_get, pirq_amd756_set },
  410. { "AMD766", PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7413,
  411. pirq_amd756_get, pirq_amd756_set },
  412. { "AMD768", PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7443,
  413. pirq_amd756_get, pirq_amd756_set },
  414. { "default", 0, 0, NULL, NULL }
  415. };
  416. static struct irq_router *pirq_router;
  417. static struct pci_dev *pirq_router_dev;
  418. static void __init pirq_find_router(void)
  419. {
  420. struct irq_routing_table *rt = pirq_table;
  421. struct irq_router *r;
  422. #ifdef CONFIG_PCI_BIOS
  423. if (!rt->signature) {
  424. printk(KERN_INFO "PCI: Using BIOS for IRQ routingn");
  425. pirq_router = &pirq_bios_router;
  426. return;
  427. }
  428. #endif
  429. DBG("PCI: Attempting to find IRQ router for %04x:%04xn",
  430.     rt->rtr_vendor, rt->rtr_device);
  431. /* fall back to default router if nothing else found */
  432. pirq_router = &pirq_routers[ARRAY_SIZE(pirq_routers) - 1];
  433. pirq_router_dev = pci_find_slot(rt->rtr_bus, rt->rtr_devfn);
  434. if (!pirq_router_dev) {
  435. DBG("PCI: Interrupt router not found at %02x:%02xn", rt->rtr_bus, rt->rtr_devfn);
  436. return;
  437. }
  438. for(r=pirq_routers; r->vendor; r++) {
  439. /* Exact match against router table entry? Use it! */
  440. if (r->vendor == rt->rtr_vendor && r->device == rt->rtr_device) {
  441. pirq_router = r;
  442. break;
  443. }
  444. /* Match against router device entry? Use it as a fallback */
  445. if (r->vendor == pirq_router_dev->vendor && r->device == pirq_router_dev->device) {
  446. pirq_router = r;
  447. }
  448. }
  449. printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %sn",
  450. pirq_router->name,
  451. pirq_router_dev->vendor,
  452. pirq_router_dev->device,
  453. pirq_router_dev->slot_name);
  454. }
  455. static struct irq_info *pirq_get_info(struct pci_dev *dev)
  456. {
  457. struct irq_routing_table *rt = pirq_table;
  458. int entries = (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
  459. struct irq_info *info;
  460. for (info = rt->slots; entries--; info++)
  461. if (info->bus == dev->bus->number && PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn))
  462. return info;
  463. return NULL;
  464. }
  465. static void pcibios_test_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
  466. {
  467. }
  468. static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
  469. {
  470. u8 pin;
  471. struct irq_info *info;
  472. int i, pirq, newirq;
  473. int irq = 0;
  474. u32 mask;
  475. struct irq_router *r = pirq_router;
  476. struct pci_dev *dev2;
  477. char *msg = NULL;
  478. if (!pirq_table)
  479. return 0;
  480. /* Find IRQ routing entry */
  481. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  482. if (!pin) {
  483. DBG(" -> no interrupt pinn");
  484. return 0;
  485. }
  486. pin = pin - 1;
  487. DBG("IRQ for %s:%d", dev->slot_name, pin);
  488. info = pirq_get_info(dev);
  489. if (!info) {
  490. DBG(" -> not found in routing tablen");
  491. return 0;
  492. }
  493. pirq = info->irq[pin].link;
  494. mask = info->irq[pin].bitmap;
  495. if (!pirq) {
  496. DBG(" -> not routedn");
  497. return 0;
  498. }
  499. DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask, pirq_table->exclusive_irqs);
  500. mask &= pcibios_irq_mask;
  501. /*
  502.  * Find the best IRQ to assign: use the one
  503.  * reported by the device if possible.
  504.  */
  505. newirq = dev->irq;
  506. if (!newirq && assign) {
  507. for (i = 0; i < 16; i++) {
  508. if (!(mask & (1 << i)))
  509. continue;
  510. if (pirq_penalty[i] < pirq_penalty[newirq] &&
  511.     !request_irq(i, pcibios_test_irq_handler, SA_SHIRQ, "pci-test", dev)) {
  512. free_irq(i, dev);
  513. newirq = i;
  514. }
  515. }
  516. }
  517. DBG(" -> newirq=%d", newirq);
  518. /* Check if it is hardcoded */
  519. if ((pirq & 0xf0) == 0xf0) {
  520. irq = pirq & 0xf;
  521. DBG(" -> hardcoded IRQ %dn", irq);
  522. msg = "Hardcoded";
  523. } else if (r->get && (irq = r->get(pirq_router_dev, dev, pirq))) {
  524. DBG(" -> got IRQ %dn", irq);
  525. msg = "Found";
  526. } else if (newirq && r->set && (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) {
  527. DBG(" -> assigning IRQ %d", newirq);
  528. if (r->set(pirq_router_dev, dev, pirq, newirq)) {
  529. eisa_set_level_irq(newirq);
  530. DBG(" ... OKn");
  531. msg = "Assigned";
  532. irq = newirq;
  533. }
  534. }
  535. if (!irq) {
  536. DBG(" ... failedn");
  537. if (newirq && mask == (1 << newirq)) {
  538. msg = "Guessed";
  539. irq = newirq;
  540. } else
  541. return 0;
  542. }
  543. printk(KERN_INFO "PCI: %s IRQ %d for device %sn", msg, irq, dev->slot_name);
  544. /* Update IRQ for all devices with the same pirq value */
  545. pci_for_each_dev(dev2) {
  546. pci_read_config_byte(dev2, PCI_INTERRUPT_PIN, &pin);
  547. if (!pin)
  548. continue;
  549. pin--;
  550. info = pirq_get_info(dev2);
  551. if (!info)
  552. continue;
  553. if (info->irq[pin].link == pirq) {
  554. /* We refuse to override the dev->irq information. Give a warning! */
  555.      if (dev2->irq && dev2->irq != irq) {
  556.      printk(KERN_INFO "IRQ routing conflict for %s, have irq %d, want irq %dn",
  557.        dev2->slot_name, dev2->irq, irq);
  558.      continue;
  559.      }
  560. dev2->irq = irq;
  561. pirq_penalty[irq]++;
  562. if (dev != dev2)
  563. printk(KERN_INFO "PCI: Sharing IRQ %d with %sn", irq, dev2->slot_name);
  564. }
  565. }
  566. return 1;
  567. }
  568. void __init pcibios_irq_init(void)
  569. {
  570. DBG("PCI: IRQ initn");
  571. pirq_table = pirq_find_routing_table();
  572. #ifdef CONFIG_PCI_BIOS
  573. if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
  574. pirq_table = pcibios_get_irq_routing_table();
  575. #endif
  576. if (pirq_table) {
  577. pirq_peer_trick();
  578. pirq_find_router();
  579. if (pirq_table->exclusive_irqs) {
  580. int i;
  581. for (i=0; i<16; i++)
  582. if (!(pirq_table->exclusive_irqs & (1 << i)))
  583. pirq_penalty[i] += 100;
  584. }
  585. /* If we're using the I/O APIC, avoid using the PCI IRQ routing table */
  586. if (io_apic_assign_pci_irqs)
  587. pirq_table = NULL;
  588. }
  589. }
  590. void __init pcibios_fixup_irqs(void)
  591. {
  592. struct pci_dev *dev;
  593. u8 pin;
  594. DBG("PCI: IRQ fixupn");
  595. pci_for_each_dev(dev) {
  596. /*
  597.  * If the BIOS has set an out of range IRQ number, just ignore it.
  598.  * Also keep track of which IRQ's are already in use.
  599.  */
  600. if (dev->irq >= 16) {
  601. DBG("%s: ignoring bogus IRQ %dn", dev->slot_name, dev->irq);
  602. dev->irq = 0;
  603. }
  604. /* If the IRQ is already assigned to a PCI device, ignore its ISA use penalty */
  605. if (pirq_penalty[dev->irq] >= 100 && pirq_penalty[dev->irq] < 100000)
  606. pirq_penalty[dev->irq] = 0;
  607. pirq_penalty[dev->irq]++;
  608. }
  609. pci_for_each_dev(dev) {
  610. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  611. #ifdef CONFIG_X86_IO_APIC
  612. /*
  613.  * Recalculate IRQ numbers if we use the I/O APIC.
  614.  */
  615. if (io_apic_assign_pci_irqs)
  616. {
  617. int irq;
  618. if (pin) {
  619. pin--; /* interrupt pins are numbered starting from 1 */
  620. irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
  621. /*
  622.  * Busses behind bridges are typically not listed in the MP-table.
  623.  * In this case we have to look up the IRQ based on the parent bus,
  624.  * parent slot, and pin number. The SMP code detects such bridged
  625.  * busses itself so we should get into this branch reliably.
  626.  */
  627. if (irq < 0 && dev->bus->parent) { /* go back to the bridge */
  628. struct pci_dev * bridge = dev->bus->self;
  629. pin = (pin + PCI_SLOT(dev->devfn)) % 4;
  630. irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 
  631. PCI_SLOT(bridge->devfn), pin);
  632. if (irq >= 0)
  633. printk(KERN_WARNING "PCI: using PPB(B%d,I%d,P%d) to get irq %dn", 
  634. bridge->bus->number, PCI_SLOT(bridge->devfn), pin, irq);
  635. }
  636. if (irq >= 0) {
  637. printk(KERN_INFO "PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %dn",
  638. dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
  639. dev->irq = irq;
  640. }
  641. }
  642. }
  643. #endif
  644. /*
  645.  * Still no IRQ? Try to lookup one...
  646.  */
  647. if (pin && !dev->irq)
  648. pcibios_lookup_irq(dev, 0);
  649. }
  650. }
  651. void pcibios_penalize_isa_irq(int irq)
  652. {
  653. /*
  654.  *  If any ISAPnP device reports an IRQ in its list of possible
  655.  *  IRQ's, we try to avoid assigning it to PCI devices.
  656.  */
  657. pirq_penalty[irq] += 100;
  658. }
  659. void pcibios_enable_irq(struct pci_dev *dev)
  660. {
  661. u8 pin;
  662. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  663. if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) {
  664. char *msg;
  665. if (io_apic_assign_pci_irqs)
  666. msg = " Probably buggy MP table.";
  667. else if (pci_probe & PCI_BIOS_IRQ_SCAN)
  668. msg = "";
  669. else
  670. msg = " Please try using pci=biosirq.";
  671. printk(KERN_WARNING "PCI: No IRQ known for interrupt pin %c of device %s.%sn",
  672.        'A' + pin - 1, dev->slot_name, msg);
  673. }
  674. }