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