pci_irq.c 13 KB

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