pci_irq.c 13 KB

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