Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  pci_irq.c - ACPI PCI Interrupt Routing ($Revision: 11 $)
0004  *
0005  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
0006  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
0007  *  Copyright (C) 2002       Dominik Brodowski <devel@brodo.de>
0008  *  (c) Copyright 2008 Hewlett-Packard Development Company, L.P.
0009  *  Bjorn Helgaas <bjorn.helgaas@hp.com>
0010  */
0011 
0012 #define pr_fmt(fmt) "ACPI: PCI: " fmt
0013 
0014 #include <linux/dmi.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/init.h>
0018 #include <linux/types.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/pm.h>
0021 #include <linux/pci.h>
0022 #include <linux/acpi.h>
0023 #include <linux/slab.h>
0024 #include <linux/interrupt.h>
0025 
0026 struct acpi_prt_entry {
0027     struct acpi_pci_id  id;
0028     u8          pin;
0029     acpi_handle     link;
0030     u32         index;      /* GSI, or link _CRS index */
0031 };
0032 
0033 static inline char pin_name(int pin)
0034 {
0035     return 'A' + pin - 1;
0036 }
0037 
0038 /* --------------------------------------------------------------------------
0039                          PCI IRQ Routing Table (PRT) Support
0040    -------------------------------------------------------------------------- */
0041 
0042 /* http://bugzilla.kernel.org/show_bug.cgi?id=4773 */
0043 static const struct dmi_system_id medion_md9580[] = {
0044     {
0045         .ident = "Medion MD9580-F laptop",
0046         .matches = {
0047             DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
0048             DMI_MATCH(DMI_PRODUCT_NAME, "A555"),
0049         },
0050     },
0051     { }
0052 };
0053 
0054 /* http://bugzilla.kernel.org/show_bug.cgi?id=5044 */
0055 static const struct dmi_system_id dell_optiplex[] = {
0056     {
0057         .ident = "Dell Optiplex GX1",
0058         .matches = {
0059             DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
0060             DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex GX1 600S+"),
0061         },
0062     },
0063     { }
0064 };
0065 
0066 /* http://bugzilla.kernel.org/show_bug.cgi?id=10138 */
0067 static const struct dmi_system_id hp_t5710[] = {
0068     {
0069         .ident = "HP t5710",
0070         .matches = {
0071             DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
0072             DMI_MATCH(DMI_PRODUCT_NAME, "hp t5000 series"),
0073             DMI_MATCH(DMI_BOARD_NAME, "098Ch"),
0074         },
0075     },
0076     { }
0077 };
0078 
0079 struct prt_quirk {
0080     const struct dmi_system_id *system;
0081     unsigned int        segment;
0082     unsigned int        bus;
0083     unsigned int        device;
0084     unsigned char       pin;
0085     const char      *source;    /* according to BIOS */
0086     const char      *actual_source;
0087 };
0088 
0089 #define PCI_INTX_PIN(c)     (c - 'A' + 1)
0090 
0091 /*
0092  * These systems have incorrect _PRT entries.  The BIOS claims the PCI
0093  * interrupt at the listed segment/bus/device/pin is connected to the first
0094  * link device, but it is actually connected to the second.
0095  */
0096 static const struct prt_quirk prt_quirks[] = {
0097     { medion_md9580, 0, 0, 9, PCI_INTX_PIN('A'),
0098         "\\_SB_.PCI0.ISA_.LNKA",
0099         "\\_SB_.PCI0.ISA_.LNKB"},
0100     { dell_optiplex, 0, 0, 0xd, PCI_INTX_PIN('A'),
0101         "\\_SB_.LNKB",
0102         "\\_SB_.LNKA"},
0103     { hp_t5710, 0, 0, 1, PCI_INTX_PIN('A'),
0104         "\\_SB_.PCI0.LNK1",
0105         "\\_SB_.PCI0.LNK3"},
0106 };
0107 
0108 static void do_prt_fixups(struct acpi_prt_entry *entry,
0109               struct acpi_pci_routing_table *prt)
0110 {
0111     int i;
0112     const struct prt_quirk *quirk;
0113 
0114     for (i = 0; i < ARRAY_SIZE(prt_quirks); i++) {
0115         quirk = &prt_quirks[i];
0116 
0117         /* All current quirks involve link devices, not GSIs */
0118         if (dmi_check_system(quirk->system) &&
0119             entry->id.segment == quirk->segment &&
0120             entry->id.bus == quirk->bus &&
0121             entry->id.device == quirk->device &&
0122             entry->pin == quirk->pin &&
0123             !strcmp(prt->source, quirk->source) &&
0124             strlen(prt->source) >= strlen(quirk->actual_source)) {
0125             pr_warn("Firmware reports "
0126                 "%04x:%02x:%02x PCI INT %c connected to %s; "
0127                 "changing to %s\n",
0128                 entry->id.segment, entry->id.bus,
0129                 entry->id.device, pin_name(entry->pin),
0130                 prt->source, quirk->actual_source);
0131             strcpy(prt->source, quirk->actual_source);
0132         }
0133     }
0134 }
0135 
0136 static int acpi_pci_irq_check_entry(acpi_handle handle, struct pci_dev *dev,
0137                   int pin, struct acpi_pci_routing_table *prt,
0138                   struct acpi_prt_entry **entry_ptr)
0139 {
0140     int segment = pci_domain_nr(dev->bus);
0141     int bus = dev->bus->number;
0142     int device = pci_ari_enabled(dev->bus) ? 0 : PCI_SLOT(dev->devfn);
0143     struct acpi_prt_entry *entry;
0144 
0145     if (((prt->address >> 16) & 0xffff) != device ||
0146         prt->pin + 1 != pin)
0147         return -ENODEV;
0148 
0149     entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
0150     if (!entry)
0151         return -ENOMEM;
0152 
0153     /*
0154      * Note that the _PRT uses 0=INTA, 1=INTB, etc, while PCI uses
0155      * 1=INTA, 2=INTB.  We use the PCI encoding throughout, so convert
0156      * it here.
0157      */
0158     entry->id.segment = segment;
0159     entry->id.bus = bus;
0160     entry->id.device = (prt->address >> 16) & 0xFFFF;
0161     entry->pin = prt->pin + 1;
0162 
0163     do_prt_fixups(entry, prt);
0164 
0165     entry->index = prt->source_index;
0166 
0167     /*
0168      * Type 1: Dynamic
0169      * ---------------
0170      * The 'source' field specifies the PCI interrupt link device used to
0171      * configure the IRQ assigned to this slot|dev|pin.  The 'source_index'
0172      * indicates which resource descriptor in the resource template (of
0173      * the link device) this interrupt is allocated from.
0174      *
0175      * NOTE: Don't query the Link Device for IRQ information at this time
0176      *       because Link Device enumeration may not have occurred yet
0177      *       (e.g. exists somewhere 'below' this _PRT entry in the ACPI
0178      *       namespace).
0179      */
0180     if (prt->source[0])
0181         acpi_get_handle(handle, prt->source, &entry->link);
0182 
0183     /*
0184      * Type 2: Static
0185      * --------------
0186      * The 'source' field is NULL, and the 'source_index' field specifies
0187      * the IRQ value, which is hardwired to specific interrupt inputs on
0188      * the interrupt controller.
0189      */
0190     pr_debug("%04x:%02x:%02x[%c] -> %s[%d]\n",
0191          entry->id.segment, entry->id.bus, entry->id.device,
0192          pin_name(entry->pin), prt->source, entry->index);
0193 
0194     *entry_ptr = entry;
0195 
0196     return 0;
0197 }
0198 
0199 static int acpi_pci_irq_find_prt_entry(struct pci_dev *dev,
0200               int pin, struct acpi_prt_entry **entry_ptr)
0201 {
0202     acpi_status status;
0203     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0204     struct acpi_pci_routing_table *entry;
0205     acpi_handle handle = NULL;
0206 
0207     if (dev->bus->bridge)
0208         handle = ACPI_HANDLE(dev->bus->bridge);
0209 
0210     if (!handle)
0211         return -ENODEV;
0212 
0213     /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
0214     status = acpi_get_irq_routing_table(handle, &buffer);
0215     if (ACPI_FAILURE(status)) {
0216         kfree(buffer.pointer);
0217         return -ENODEV;
0218     }
0219 
0220     entry = buffer.pointer;
0221     while (entry && (entry->length > 0)) {
0222         if (!acpi_pci_irq_check_entry(handle, dev, pin,
0223                          entry, entry_ptr))
0224             break;
0225         entry = (struct acpi_pci_routing_table *)
0226             ((unsigned long)entry + entry->length);
0227     }
0228 
0229     kfree(buffer.pointer);
0230     return 0;
0231 }
0232 
0233 /* --------------------------------------------------------------------------
0234                           PCI Interrupt Routing Support
0235    -------------------------------------------------------------------------- */
0236 #ifdef CONFIG_X86_IO_APIC
0237 extern int noioapicquirk;
0238 extern int noioapicreroute;
0239 
0240 static int bridge_has_boot_interrupt_variant(struct pci_bus *bus)
0241 {
0242     struct pci_bus *bus_it;
0243 
0244     for (bus_it = bus ; bus_it ; bus_it = bus_it->parent) {
0245         if (!bus_it->self)
0246             return 0;
0247         if (bus_it->self->irq_reroute_variant)
0248             return bus_it->self->irq_reroute_variant;
0249     }
0250     return 0;
0251 }
0252 
0253 /*
0254  * Some chipsets (e.g. Intel 6700PXH) generate a legacy INTx when the IRQ
0255  * entry in the chipset's IO-APIC is masked (as, e.g. the RT kernel does
0256  * during interrupt handling). When this INTx generation cannot be disabled,
0257  * we reroute these interrupts to their legacy equivalent to get rid of
0258  * spurious interrupts.
0259  */
0260 static int acpi_reroute_boot_interrupt(struct pci_dev *dev,
0261                        struct acpi_prt_entry *entry)
0262 {
0263     if (noioapicquirk || noioapicreroute) {
0264         return 0;
0265     } else {
0266         switch (bridge_has_boot_interrupt_variant(dev->bus)) {
0267         case 0:
0268             /* no rerouting necessary */
0269             return 0;
0270         case INTEL_IRQ_REROUTE_VARIANT:
0271             /*
0272              * Remap according to INTx routing table in 6700PXH
0273              * specs, intel order number 302628-002, section
0274              * 2.15.2. Other chipsets (80332, ...) have the same
0275              * mapping and are handled here as well.
0276              */
0277             dev_info(&dev->dev, "PCI IRQ %d -> rerouted to legacy "
0278                  "IRQ %d\n", entry->index,
0279                  (entry->index % 4) + 16);
0280             entry->index = (entry->index % 4) + 16;
0281             return 1;
0282         default:
0283             dev_warn(&dev->dev, "Cannot reroute IRQ %d to legacy "
0284                  "IRQ: unknown mapping\n", entry->index);
0285             return -1;
0286         }
0287     }
0288 }
0289 #endif /* CONFIG_X86_IO_APIC */
0290 
0291 static struct acpi_prt_entry *acpi_pci_irq_lookup(struct pci_dev *dev, int pin)
0292 {
0293     struct acpi_prt_entry *entry = NULL;
0294     struct pci_dev *bridge;
0295     u8 bridge_pin, orig_pin = pin;
0296     int ret;
0297 
0298     ret = acpi_pci_irq_find_prt_entry(dev, pin, &entry);
0299     if (!ret && entry) {
0300 #ifdef CONFIG_X86_IO_APIC
0301         acpi_reroute_boot_interrupt(dev, entry);
0302 #endif /* CONFIG_X86_IO_APIC */
0303         dev_dbg(&dev->dev, "Found [%c] _PRT entry\n", pin_name(pin));
0304         return entry;
0305     }
0306 
0307     /*
0308      * Attempt to derive an IRQ for this device from a parent bridge's
0309      * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
0310      */
0311     bridge = dev->bus->self;
0312     while (bridge) {
0313         pin = pci_swizzle_interrupt_pin(dev, pin);
0314 
0315         if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
0316             /* PC card has the same IRQ as its cardbridge */
0317             bridge_pin = bridge->pin;
0318             if (!bridge_pin) {
0319                 dev_dbg(&bridge->dev, "No interrupt pin configured\n");
0320                 return NULL;
0321             }
0322             pin = bridge_pin;
0323         }
0324 
0325         ret = acpi_pci_irq_find_prt_entry(bridge, pin, &entry);
0326         if (!ret && entry) {
0327             dev_dbg(&dev->dev, "Derived GSI INT %c from %s\n",
0328                 pin_name(orig_pin), pci_name(bridge));
0329             return entry;
0330         }
0331 
0332         dev = bridge;
0333         bridge = dev->bus->self;
0334     }
0335 
0336     dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
0337          pin_name(orig_pin));
0338     return NULL;
0339 }
0340 
0341 #if IS_ENABLED(CONFIG_ISA) || IS_ENABLED(CONFIG_EISA)
0342 static int acpi_isa_register_gsi(struct pci_dev *dev)
0343 {
0344     u32 dev_gsi;
0345 
0346     /* Interrupt Line values above 0xF are forbidden */
0347     if (dev->irq > 0 && (dev->irq <= 0xF) &&
0348         acpi_isa_irq_available(dev->irq) &&
0349         (acpi_isa_irq_to_gsi(dev->irq, &dev_gsi) == 0)) {
0350         dev_warn(&dev->dev, "PCI INT %c: no GSI - using ISA IRQ %d\n",
0351              pin_name(dev->pin), dev->irq);
0352         acpi_register_gsi(&dev->dev, dev_gsi,
0353                   ACPI_LEVEL_SENSITIVE,
0354                   ACPI_ACTIVE_LOW);
0355         return 0;
0356     }
0357     return -EINVAL;
0358 }
0359 #else
0360 static inline int acpi_isa_register_gsi(struct pci_dev *dev)
0361 {
0362     return -ENODEV;
0363 }
0364 #endif
0365 
0366 static inline bool acpi_pci_irq_valid(struct pci_dev *dev, u8 pin)
0367 {
0368 #ifdef CONFIG_X86
0369     /*
0370      * On x86 irq line 0xff means "unknown" or "no connection"
0371      * (PCI 3.0, Section 6.2.4, footnote on page 223).
0372      */
0373     if (dev->irq == 0xff) {
0374         dev->irq = IRQ_NOTCONNECTED;
0375         dev_warn(&dev->dev, "PCI INT %c: not connected\n",
0376              pin_name(pin));
0377         return false;
0378     }
0379 #endif
0380     return true;
0381 }
0382 
0383 int acpi_pci_irq_enable(struct pci_dev *dev)
0384 {
0385     struct acpi_prt_entry *entry;
0386     int gsi;
0387     u8 pin;
0388     int triggering = ACPI_LEVEL_SENSITIVE;
0389     /*
0390      * On ARM systems with the GIC interrupt model, level interrupts
0391      * are always polarity high by specification; PCI legacy
0392      * IRQs lines are inverted before reaching the interrupt
0393      * controller and must therefore be considered active high
0394      * as default.
0395      */
0396     int polarity = acpi_irq_model == ACPI_IRQ_MODEL_GIC ?
0397                       ACPI_ACTIVE_HIGH : ACPI_ACTIVE_LOW;
0398     char *link = NULL;
0399     char link_desc[16];
0400     int rc;
0401 
0402     pin = dev->pin;
0403     if (!pin) {
0404         dev_dbg(&dev->dev, "No interrupt pin configured\n");
0405         return 0;
0406     }
0407 
0408     if (dev->irq_managed && dev->irq > 0)
0409         return 0;
0410 
0411     entry = acpi_pci_irq_lookup(dev, pin);
0412     if (!entry) {
0413         /*
0414          * IDE legacy mode controller IRQs are magic. Why do compat
0415          * extensions always make such a nasty mess.
0416          */
0417         if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
0418                 (dev->class & 0x05) == 0)
0419             return 0;
0420     }
0421 
0422     if (entry) {
0423         if (entry->link)
0424             gsi = acpi_pci_link_allocate_irq(entry->link,
0425                              entry->index,
0426                              &triggering, &polarity,
0427                              &link);
0428         else
0429             gsi = entry->index;
0430     } else
0431         gsi = -1;
0432 
0433     if (gsi < 0) {
0434         /*
0435          * No IRQ known to the ACPI subsystem - maybe the BIOS /
0436          * driver reported one, then use it. Exit in any case.
0437          */
0438         if (!acpi_pci_irq_valid(dev, pin)) {
0439             kfree(entry);
0440             return 0;
0441         }
0442 
0443         if (acpi_isa_register_gsi(dev))
0444             dev_warn(&dev->dev, "PCI INT %c: no GSI\n",
0445                  pin_name(pin));
0446 
0447         kfree(entry);
0448         return 0;
0449     }
0450 
0451     rc = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
0452     if (rc < 0) {
0453         dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
0454              pin_name(pin));
0455         kfree(entry);
0456         return rc;
0457     }
0458     dev->irq = rc;
0459     dev->irq_managed = 1;
0460 
0461     if (link)
0462         snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
0463     else
0464         link_desc[0] = '\0';
0465 
0466     dev_dbg(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
0467         pin_name(pin), link_desc, gsi,
0468         (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
0469         (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
0470 
0471     kfree(entry);
0472     return 0;
0473 }
0474 
0475 void acpi_pci_irq_disable(struct pci_dev *dev)
0476 {
0477     struct acpi_prt_entry *entry;
0478     int gsi;
0479     u8 pin;
0480 
0481     pin = dev->pin;
0482     if (!pin || !dev->irq_managed || dev->irq <= 0)
0483         return;
0484 
0485     /* Keep IOAPIC pin configuration when suspending */
0486     if (dev->dev.power.is_prepared)
0487         return;
0488 #ifdef  CONFIG_PM
0489     if (dev->dev.power.runtime_status == RPM_SUSPENDING)
0490         return;
0491 #endif
0492 
0493     entry = acpi_pci_irq_lookup(dev, pin);
0494     if (!entry)
0495         return;
0496 
0497     if (entry->link)
0498         gsi = acpi_pci_link_free_irq(entry->link);
0499     else
0500         gsi = entry->index;
0501 
0502     kfree(entry);
0503 
0504     /*
0505      * TBD: It might be worth clearing dev->irq by magic constant
0506      * (e.g. PCI_UNDEFINED_IRQ).
0507      */
0508 
0509     dev_dbg(&dev->dev, "PCI INT %c disabled\n", pin_name(pin));
0510     if (gsi >= 0) {
0511         acpi_unregister_gsi(gsi);
0512         dev->irq_managed = 0;
0513     }
0514 }