pci_irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pci_irq.c - ACPI PCI Interrupt Routing ($Revision: 11 $)
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. * Copyright (C) 2002 Dominik Brodowski <devel@brodo.de>
  8. * (c) Copyright 2008 Hewlett-Packard Development Company, L.P.
  9. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  10. */
  11. #include <linux/dmi.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/pm.h>
  18. #include <linux/pci.h>
  19. #include <linux/acpi.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #define PREFIX "ACPI: "
  23. #define _COMPONENT ACPI_PCI_COMPONENT
  24. ACPI_MODULE_NAME("pci_irq");
  25. struct acpi_prt_entry {
  26. struct acpi_pci_id id;
  27. u8 pin;
  28. acpi_handle link;
  29. u32 index; /* GSI, or link _CRS index */
  30. };
  31. static inline char pin_name(int pin)
  32. {
  33. return 'A' + pin - 1;
  34. }
  35. /* --------------------------------------------------------------------------
  36. PCI IRQ Routing Table (PRT) Support
  37. -------------------------------------------------------------------------- */
  38. /* http://bugzilla.kernel.org/show_bug.cgi?id=4773 */
  39. static const struct dmi_system_id medion_md9580[] = {
  40. {
  41. .ident = "Medion MD9580-F laptop",
  42. .matches = {
  43. DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
  44. DMI_MATCH(DMI_PRODUCT_NAME, "A555"),
  45. },
  46. },
  47. { }
  48. };
  49. /* http://bugzilla.kernel.org/show_bug.cgi?id=5044 */
  50. static const struct dmi_system_id dell_optiplex[] = {
  51. {
  52. .ident = "Dell Optiplex GX1",
  53. .matches = {
  54. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  55. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex GX1 600S+"),
  56. },
  57. },
  58. { }
  59. };
  60. /* http://bugzilla.kernel.org/show_bug.cgi?id=10138 */
  61. static const struct dmi_system_id hp_t5710[] = {
  62. {
  63. .ident = "HP t5710",
  64. .matches = {
  65. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  66. DMI_MATCH(DMI_PRODUCT_NAME, "hp t5000 series"),
  67. DMI_MATCH(DMI_BOARD_NAME, "098Ch"),
  68. },
  69. },
  70. { }
  71. };
  72. struct prt_quirk {
  73. const struct dmi_system_id *system;
  74. unsigned int segment;
  75. unsigned int bus;
  76. unsigned int device;
  77. unsigned char pin;
  78. const char *source; /* according to BIOS */
  79. const char *actual_source;
  80. };
  81. #define PCI_INTX_PIN(c) (c - 'A' + 1)
  82. /*
  83. * These systems have incorrect _PRT entries. The BIOS claims the PCI
  84. * interrupt at the listed segment/bus/device/pin is connected to the first
  85. * link device, but it is actually connected to the second.
  86. */
  87. static const struct prt_quirk prt_quirks[] = {
  88. { medion_md9580, 0, 0, 9, PCI_INTX_PIN('A'),
  89. "\\_SB_.PCI0.ISA_.LNKA",
  90. "\\_SB_.PCI0.ISA_.LNKB"},
  91. { dell_optiplex, 0, 0, 0xd, PCI_INTX_PIN('A'),
  92. "\\_SB_.LNKB",
  93. "\\_SB_.LNKA"},
  94. { hp_t5710, 0, 0, 1, PCI_INTX_PIN('A'),
  95. "\\_SB_.PCI0.LNK1",
  96. "\\_SB_.PCI0.LNK3"},
  97. };
  98. static void do_prt_fixups(struct acpi_prt_entry *entry,
  99. struct acpi_pci_routing_table *prt)
  100. {
  101. int i;
  102. const struct prt_quirk *quirk;
  103. for (i = 0; i < ARRAY_SIZE(prt_quirks); i++) {
  104. quirk = &prt_quirks[i];
  105. /* All current quirks involve link devices, not GSIs */
  106. if (dmi_check_system(quirk->system) &&
  107. entry->id.segment == quirk->segment &&
  108. entry->id.bus == quirk->bus &&
  109. entry->id.device == quirk->device &&
  110. entry->pin == quirk->pin &&
  111. !strcmp(prt->source, quirk->source) &&
  112. strlen(prt->source) >= strlen(quirk->actual_source)) {
  113. printk(KERN_WARNING PREFIX "firmware reports "
  114. "%04x:%02x:%02x PCI INT %c connected to %s; "
  115. "changing to %s\n",
  116. entry->id.segment, entry->id.bus,
  117. entry->id.device, pin_name(entry->pin),
  118. prt->source, quirk->actual_source);
  119. strcpy(prt->source, quirk->actual_source);
  120. }
  121. }
  122. }
  123. static int acpi_pci_irq_check_entry(acpi_handle handle, struct pci_dev *dev,
  124. int pin, struct acpi_pci_routing_table *prt,
  125. struct acpi_prt_entry **entry_ptr)
  126. {
  127. int segment = pci_domain_nr(dev->bus);
  128. int bus = dev->bus->number;
  129. int device = pci_ari_enabled(dev->bus) ? 0 : PCI_SLOT(dev->devfn);
  130. struct acpi_prt_entry *entry;
  131. if (((prt->address >> 16) & 0xffff) != device ||
  132. prt->pin + 1 != pin)
  133. return -ENODEV;
  134. entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
  135. if (!entry)
  136. return -ENOMEM;
  137. /*
  138. * Note that the _PRT uses 0=INTA, 1=INTB, etc, while PCI uses
  139. * 1=INTA, 2=INTB. We use the PCI encoding throughout, so convert
  140. * it here.
  141. */
  142. entry->id.segment = segment;
  143. entry->id.bus = bus;
  144. entry->id.device = (prt->address >> 16) & 0xFFFF;
  145. entry->pin = prt->pin + 1;
  146. do_prt_fixups(entry, prt);
  147. entry->index = prt->source_index;
  148. /*
  149. * Type 1: Dynamic
  150. * ---------------
  151. * The 'source' field specifies the PCI interrupt link device used to
  152. * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
  153. * indicates which resource descriptor in the resource template (of
  154. * the link device) this interrupt is allocated from.
  155. *
  156. * NOTE: Don't query the Link Device for IRQ information at this time
  157. * because Link Device enumeration may not have occurred yet
  158. * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
  159. * namespace).
  160. */
  161. if (prt->source[0])
  162. acpi_get_handle(handle, prt->source, &entry->link);
  163. /*
  164. * Type 2: Static
  165. * --------------
  166. * The 'source' field is NULL, and the 'source_index' field specifies
  167. * the IRQ value, which is hardwired to specific interrupt inputs on
  168. * the interrupt controller.
  169. */
  170. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
  171. " %04x:%02x:%02x[%c] -> %s[%d]\n",
  172. entry->id.segment, entry->id.bus,
  173. entry->id.device, pin_name(entry->pin),
  174. prt->source, entry->index));
  175. *entry_ptr = entry;
  176. return 0;
  177. }
  178. static int acpi_pci_irq_find_prt_entry(struct pci_dev *dev,
  179. int pin, struct acpi_prt_entry **entry_ptr)
  180. {
  181. acpi_status status;
  182. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  183. struct acpi_pci_routing_table *entry;
  184. acpi_handle handle = NULL;
  185. if (dev->bus->bridge)
  186. handle = ACPI_HANDLE(dev->bus->bridge);
  187. if (!handle)
  188. return -ENODEV;
  189. /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
  190. status = acpi_get_irq_routing_table(handle, &buffer);
  191. if (ACPI_FAILURE(status)) {
  192. kfree(buffer.pointer);
  193. return -ENODEV;
  194. }
  195. entry = buffer.pointer;
  196. while (entry && (entry->length > 0)) {
  197. if (!acpi_pci_irq_check_entry(handle, dev, pin,
  198. entry, entry_ptr))
  199. break;
  200. entry = (struct acpi_pci_routing_table *)
  201. ((unsigned long)entry + entry->length);
  202. }
  203. kfree(buffer.pointer);
  204. return 0;
  205. }
  206. /* --------------------------------------------------------------------------
  207. PCI Interrupt Routing Support
  208. -------------------------------------------------------------------------- */
  209. #ifdef CONFIG_X86_IO_APIC
  210. extern int noioapicquirk;
  211. extern int noioapicreroute;
  212. static int bridge_has_boot_interrupt_variant(struct pci_bus *bus)
  213. {
  214. struct pci_bus *bus_it;
  215. for (bus_it = bus ; bus_it ; bus_it = bus_it->parent) {
  216. if (!bus_it->self)
  217. return 0;
  218. if (bus_it->self->irq_reroute_variant)
  219. return bus_it->self->irq_reroute_variant;
  220. }
  221. return 0;
  222. }
  223. /*
  224. * Some chipsets (e.g. Intel 6700PXH) generate a legacy INTx when the IRQ
  225. * entry in the chipset's IO-APIC is masked (as, e.g. the RT kernel does
  226. * during interrupt handling). When this INTx generation cannot be disabled,
  227. * we reroute these interrupts to their legacy equivalent to get rid of
  228. * spurious interrupts.
  229. */
  230. static int acpi_reroute_boot_interrupt(struct pci_dev *dev,
  231. struct acpi_prt_entry *entry)
  232. {
  233. if (noioapicquirk || noioapicreroute) {
  234. return 0;
  235. } else {
  236. switch (bridge_has_boot_interrupt_variant(dev->bus)) {
  237. case 0:
  238. /* no rerouting necessary */
  239. return 0;
  240. case INTEL_IRQ_REROUTE_VARIANT:
  241. /*
  242. * Remap according to INTx routing table in 6700PXH
  243. * specs, intel order number 302628-002, section
  244. * 2.15.2. Other chipsets (80332, ...) have the same
  245. * mapping and are handled here as well.
  246. */
  247. dev_info(&dev->dev, "PCI IRQ %d -> rerouted to legacy "
  248. "IRQ %d\n", entry->index,
  249. (entry->index % 4) + 16);
  250. entry->index = (entry->index % 4) + 16;
  251. return 1;
  252. default:
  253. dev_warn(&dev->dev, "Cannot reroute IRQ %d to legacy "
  254. "IRQ: unknown mapping\n", entry->index);
  255. return -1;
  256. }
  257. }
  258. }
  259. #endif /* CONFIG_X86_IO_APIC */
  260. static struct acpi_prt_entry *acpi_pci_irq_lookup(struct pci_dev *dev, int pin)
  261. {
  262. struct acpi_prt_entry *entry = NULL;
  263. struct pci_dev *bridge;
  264. u8 bridge_pin, orig_pin = pin;
  265. int ret;
  266. ret = acpi_pci_irq_find_prt_entry(dev, pin, &entry);
  267. if (!ret && entry) {
  268. #ifdef CONFIG_X86_IO_APIC
  269. acpi_reroute_boot_interrupt(dev, entry);
  270. #endif /* CONFIG_X86_IO_APIC */
  271. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %s[%c] _PRT entry\n",
  272. pci_name(dev), pin_name(pin)));
  273. return entry;
  274. }
  275. /*
  276. * Attempt to derive an IRQ for this device from a parent bridge's
  277. * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
  278. */
  279. bridge = dev->bus->self;
  280. while (bridge) {
  281. pin = pci_swizzle_interrupt_pin(dev, pin);
  282. if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
  283. /* PC card has the same IRQ as its cardbridge */
  284. bridge_pin = bridge->pin;
  285. if (!bridge_pin) {
  286. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  287. "No interrupt pin configured for device %s\n",
  288. pci_name(bridge)));
  289. return NULL;
  290. }
  291. pin = bridge_pin;
  292. }
  293. ret = acpi_pci_irq_find_prt_entry(bridge, pin, &entry);
  294. if (!ret && entry) {
  295. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  296. "Derived GSI for %s INT %c from %s\n",
  297. pci_name(dev), pin_name(orig_pin),
  298. pci_name(bridge)));
  299. return entry;
  300. }
  301. dev = bridge;
  302. bridge = dev->bus->self;
  303. }
  304. dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
  305. pin_name(orig_pin));
  306. return NULL;
  307. }
  308. #if IS_ENABLED(CONFIG_ISA) || IS_ENABLED(CONFIG_EISA)
  309. static int acpi_isa_register_gsi(struct pci_dev *dev)
  310. {
  311. u32 dev_gsi;
  312. /* Interrupt Line values above 0xF are forbidden */
  313. if (dev->irq > 0 && (dev->irq <= 0xF) &&
  314. acpi_isa_irq_available(dev->irq) &&
  315. (acpi_isa_irq_to_gsi(dev->irq, &dev_gsi) == 0)) {
  316. dev_warn(&dev->dev, "PCI INT %c: no GSI - using ISA IRQ %d\n",
  317. pin_name(dev->pin), dev->irq);
  318. acpi_register_gsi(&dev->dev, dev_gsi,
  319. ACPI_LEVEL_SENSITIVE,
  320. ACPI_ACTIVE_LOW);
  321. return 0;
  322. }
  323. return -EINVAL;
  324. }
  325. #else
  326. static inline int acpi_isa_register_gsi(struct pci_dev *dev)
  327. {
  328. return -ENODEV;
  329. }
  330. #endif
  331. static inline bool acpi_pci_irq_valid(struct pci_dev *dev, u8 pin)
  332. {
  333. #ifdef CONFIG_X86
  334. /*
  335. * On x86 irq line 0xff means "unknown" or "no connection"
  336. * (PCI 3.0, Section 6.2.4, footnote on page 223).
  337. */
  338. if (dev->irq == 0xff) {
  339. dev->irq = IRQ_NOTCONNECTED;
  340. dev_warn(&dev->dev, "PCI INT %c: not connected\n",
  341. pin_name(pin));
  342. return false;
  343. }
  344. #endif
  345. return true;
  346. }
  347. int acpi_pci_irq_enable(struct pci_dev *dev)
  348. {
  349. struct acpi_prt_entry *entry;
  350. int gsi;
  351. u8 pin;
  352. int triggering = ACPI_LEVEL_SENSITIVE;
  353. /*
  354. * On ARM systems with the GIC interrupt model, level interrupts
  355. * are always polarity high by specification; PCI legacy
  356. * IRQs lines are inverted before reaching the interrupt
  357. * controller and must therefore be considered active high
  358. * as default.
  359. */
  360. int polarity = acpi_irq_model == ACPI_IRQ_MODEL_GIC ?
  361. ACPI_ACTIVE_HIGH : ACPI_ACTIVE_LOW;
  362. char *link = NULL;
  363. char link_desc[16];
  364. int rc;
  365. pin = dev->pin;
  366. if (!pin) {
  367. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  368. "No interrupt pin configured for device %s\n",
  369. pci_name(dev)));
  370. return 0;
  371. }
  372. if (dev->irq_managed && dev->irq > 0)
  373. return 0;
  374. entry = acpi_pci_irq_lookup(dev, pin);
  375. if (!entry) {
  376. /*
  377. * IDE legacy mode controller IRQs are magic. Why do compat
  378. * extensions always make such a nasty mess.
  379. */
  380. if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
  381. (dev->class & 0x05) == 0)
  382. return 0;
  383. }
  384. if (entry) {
  385. if (entry->link)
  386. gsi = acpi_pci_link_allocate_irq(entry->link,
  387. entry->index,
  388. &triggering, &polarity,
  389. &link);
  390. else
  391. gsi = entry->index;
  392. } else
  393. gsi = -1;
  394. if (gsi < 0) {
  395. /*
  396. * No IRQ known to the ACPI subsystem - maybe the BIOS /
  397. * driver reported one, then use it. Exit in any case.
  398. */
  399. if (!acpi_pci_irq_valid(dev, pin)) {
  400. kfree(entry);
  401. return 0;
  402. }
  403. if (acpi_isa_register_gsi(dev))
  404. dev_warn(&dev->dev, "PCI INT %c: no GSI\n",
  405. pin_name(pin));
  406. kfree(entry);
  407. return 0;
  408. }
  409. rc = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
  410. if (rc < 0) {
  411. dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
  412. pin_name(pin));
  413. kfree(entry);
  414. return rc;
  415. }
  416. dev->irq = rc;
  417. dev->irq_managed = 1;
  418. if (link)
  419. snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
  420. else
  421. link_desc[0] = '\0';
  422. dev_dbg(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
  423. pin_name(pin), link_desc, gsi,
  424. (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
  425. (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
  426. kfree(entry);
  427. return 0;
  428. }
  429. void acpi_pci_irq_disable(struct pci_dev *dev)
  430. {
  431. struct acpi_prt_entry *entry;
  432. int gsi;
  433. u8 pin;
  434. pin = dev->pin;
  435. if (!pin || !dev->irq_managed || dev->irq <= 0)
  436. return;
  437. /* Keep IOAPIC pin configuration when suspending */
  438. if (dev->dev.power.is_prepared)
  439. return;
  440. #ifdef CONFIG_PM
  441. if (dev->dev.power.runtime_status == RPM_SUSPENDING)
  442. return;
  443. #endif
  444. entry = acpi_pci_irq_lookup(dev, pin);
  445. if (!entry)
  446. return;
  447. if (entry->link)
  448. gsi = acpi_pci_link_free_irq(entry->link);
  449. else
  450. gsi = entry->index;
  451. kfree(entry);
  452. /*
  453. * TBD: It might be worth clearing dev->irq by magic constant
  454. * (e.g. PCI_UNDEFINED_IRQ).
  455. */
  456. dev_dbg(&dev->dev, "PCI INT %c disabled\n", pin_name(pin));
  457. if (gsi >= 0) {
  458. acpi_unregister_gsi(gsi);
  459. dev->irq_managed = 0;
  460. }
  461. }