pci_irq.c 13 KB

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