pci_irq.c 13 KB

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