pci_irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 (!prt->source)
  119. continue;
  120. if (dmi_check_system(quirk->system) &&
  121. entry->id.segment == quirk->segment &&
  122. entry->id.bus == quirk->bus &&
  123. entry->id.device == quirk->device &&
  124. entry->pin == quirk->pin &&
  125. !strcmp(prt->source, quirk->source) &&
  126. strlen(prt->source) >= strlen(quirk->actual_source)) {
  127. printk(KERN_WARNING PREFIX "firmware reports "
  128. "%04x:%02x:%02x PCI INT %c connected to %s; "
  129. "changing to %s\n",
  130. entry->id.segment, entry->id.bus,
  131. entry->id.device, pin_name(entry->pin),
  132. prt->source, quirk->actual_source);
  133. strcpy(prt->source, quirk->actual_source);
  134. }
  135. }
  136. }
  137. static int acpi_pci_irq_check_entry(acpi_handle handle, struct pci_dev *dev,
  138. int pin, struct acpi_pci_routing_table *prt,
  139. struct acpi_prt_entry **entry_ptr)
  140. {
  141. int segment = pci_domain_nr(dev->bus);
  142. int bus = dev->bus->number;
  143. int device = pci_ari_enabled(dev->bus) ? 0 : PCI_SLOT(dev->devfn);
  144. struct acpi_prt_entry *entry;
  145. if (((prt->address >> 16) & 0xffff) != device ||
  146. prt->pin + 1 != pin)
  147. return -ENODEV;
  148. entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
  149. if (!entry)
  150. return -ENOMEM;
  151. /*
  152. * Note that the _PRT uses 0=INTA, 1=INTB, etc, while PCI uses
  153. * 1=INTA, 2=INTB. We use the PCI encoding throughout, so convert
  154. * it here.
  155. */
  156. entry->id.segment = segment;
  157. entry->id.bus = bus;
  158. entry->id.device = (prt->address >> 16) & 0xFFFF;
  159. entry->pin = prt->pin + 1;
  160. do_prt_fixups(entry, prt);
  161. entry->index = prt->source_index;
  162. /*
  163. * Type 1: Dynamic
  164. * ---------------
  165. * The 'source' field specifies the PCI interrupt link device used to
  166. * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
  167. * indicates which resource descriptor in the resource template (of
  168. * the link device) this interrupt is allocated from.
  169. *
  170. * NOTE: Don't query the Link Device for IRQ information at this time
  171. * because Link Device enumeration may not have occurred yet
  172. * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
  173. * namespace).
  174. */
  175. if (prt->source[0])
  176. acpi_get_handle(handle, prt->source, &entry->link);
  177. /*
  178. * Type 2: Static
  179. * --------------
  180. * The 'source' field is NULL, and the 'source_index' field specifies
  181. * the IRQ value, which is hardwired to specific interrupt inputs on
  182. * the interrupt controller.
  183. */
  184. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
  185. " %04x:%02x:%02x[%c] -> %s[%d]\n",
  186. entry->id.segment, entry->id.bus,
  187. entry->id.device, pin_name(entry->pin),
  188. prt->source, entry->index));
  189. *entry_ptr = entry;
  190. return 0;
  191. }
  192. static int acpi_pci_irq_find_prt_entry(struct pci_dev *dev,
  193. int pin, struct acpi_prt_entry **entry_ptr)
  194. {
  195. acpi_status status;
  196. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  197. struct acpi_pci_routing_table *entry;
  198. acpi_handle handle = NULL;
  199. if (dev->bus->bridge)
  200. handle = ACPI_HANDLE(dev->bus->bridge);
  201. if (!handle)
  202. return -ENODEV;
  203. /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
  204. status = acpi_get_irq_routing_table(handle, &buffer);
  205. if (ACPI_FAILURE(status)) {
  206. kfree(buffer.pointer);
  207. return -ENODEV;
  208. }
  209. entry = buffer.pointer;
  210. while (entry && (entry->length > 0)) {
  211. if (!acpi_pci_irq_check_entry(handle, dev, pin,
  212. entry, entry_ptr))
  213. break;
  214. entry = (struct acpi_pci_routing_table *)
  215. ((unsigned long)entry + entry->length);
  216. }
  217. kfree(buffer.pointer);
  218. return 0;
  219. }
  220. /* --------------------------------------------------------------------------
  221. PCI Interrupt Routing Support
  222. -------------------------------------------------------------------------- */
  223. #ifdef CONFIG_X86_IO_APIC
  224. extern int noioapicquirk;
  225. extern int noioapicreroute;
  226. static int bridge_has_boot_interrupt_variant(struct pci_bus *bus)
  227. {
  228. struct pci_bus *bus_it;
  229. for (bus_it = bus ; bus_it ; bus_it = bus_it->parent) {
  230. if (!bus_it->self)
  231. return 0;
  232. if (bus_it->self->irq_reroute_variant)
  233. return bus_it->self->irq_reroute_variant;
  234. }
  235. return 0;
  236. }
  237. /*
  238. * Some chipsets (e.g. Intel 6700PXH) generate a legacy INTx when the IRQ
  239. * entry in the chipset's IO-APIC is masked (as, e.g. the RT kernel does
  240. * during interrupt handling). When this INTx generation cannot be disabled,
  241. * we reroute these interrupts to their legacy equivalent to get rid of
  242. * spurious interrupts.
  243. */
  244. static int acpi_reroute_boot_interrupt(struct pci_dev *dev,
  245. struct acpi_prt_entry *entry)
  246. {
  247. if (noioapicquirk || noioapicreroute) {
  248. return 0;
  249. } else {
  250. switch (bridge_has_boot_interrupt_variant(dev->bus)) {
  251. case 0:
  252. /* no rerouting necessary */
  253. return 0;
  254. case INTEL_IRQ_REROUTE_VARIANT:
  255. /*
  256. * Remap according to INTx routing table in 6700PXH
  257. * specs, intel order number 302628-002, section
  258. * 2.15.2. Other chipsets (80332, ...) have the same
  259. * mapping and are handled here as well.
  260. */
  261. dev_info(&dev->dev, "PCI IRQ %d -> rerouted to legacy "
  262. "IRQ %d\n", entry->index,
  263. (entry->index % 4) + 16);
  264. entry->index = (entry->index % 4) + 16;
  265. return 1;
  266. default:
  267. dev_warn(&dev->dev, "Cannot reroute IRQ %d to legacy "
  268. "IRQ: unknown mapping\n", entry->index);
  269. return -1;
  270. }
  271. }
  272. }
  273. #endif /* CONFIG_X86_IO_APIC */
  274. static struct acpi_prt_entry *acpi_pci_irq_lookup(struct pci_dev *dev, int pin)
  275. {
  276. struct acpi_prt_entry *entry = NULL;
  277. struct pci_dev *bridge;
  278. u8 bridge_pin, orig_pin = pin;
  279. int ret;
  280. ret = acpi_pci_irq_find_prt_entry(dev, pin, &entry);
  281. if (!ret && entry) {
  282. #ifdef CONFIG_X86_IO_APIC
  283. acpi_reroute_boot_interrupt(dev, entry);
  284. #endif /* CONFIG_X86_IO_APIC */
  285. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %s[%c] _PRT entry\n",
  286. pci_name(dev), pin_name(pin)));
  287. return entry;
  288. }
  289. /*
  290. * Attempt to derive an IRQ for this device from a parent bridge's
  291. * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
  292. */
  293. bridge = dev->bus->self;
  294. while (bridge) {
  295. pin = pci_swizzle_interrupt_pin(dev, pin);
  296. if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
  297. /* PC card has the same IRQ as its cardbridge */
  298. bridge_pin = bridge->pin;
  299. if (!bridge_pin) {
  300. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  301. "No interrupt pin configured for device %s\n",
  302. pci_name(bridge)));
  303. return NULL;
  304. }
  305. pin = bridge_pin;
  306. }
  307. ret = acpi_pci_irq_find_prt_entry(bridge, pin, &entry);
  308. if (!ret && entry) {
  309. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  310. "Derived GSI for %s INT %c from %s\n",
  311. pci_name(dev), pin_name(orig_pin),
  312. pci_name(bridge)));
  313. return entry;
  314. }
  315. dev = bridge;
  316. bridge = dev->bus->self;
  317. }
  318. dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
  319. pin_name(orig_pin));
  320. return NULL;
  321. }
  322. #if IS_ENABLED(CONFIG_ISA) || IS_ENABLED(CONFIG_EISA)
  323. static int acpi_isa_register_gsi(struct pci_dev *dev)
  324. {
  325. u32 dev_gsi;
  326. /* Interrupt Line values above 0xF are forbidden */
  327. if (dev->irq > 0 && (dev->irq <= 0xF) &&
  328. (acpi_isa_irq_to_gsi(dev->irq, &dev_gsi) == 0)) {
  329. dev_warn(&dev->dev, "PCI INT %c: no GSI - using ISA IRQ %d\n",
  330. pin_name(dev->pin), dev->irq);
  331. acpi_register_gsi(&dev->dev, dev_gsi,
  332. ACPI_LEVEL_SENSITIVE,
  333. ACPI_ACTIVE_LOW);
  334. return 0;
  335. }
  336. return -EINVAL;
  337. }
  338. #else
  339. static inline int acpi_isa_register_gsi(struct pci_dev *dev)
  340. {
  341. return -ENODEV;
  342. }
  343. #endif
  344. int acpi_pci_irq_enable(struct pci_dev *dev)
  345. {
  346. struct acpi_prt_entry *entry;
  347. int gsi;
  348. u8 pin;
  349. int triggering = ACPI_LEVEL_SENSITIVE;
  350. int polarity = ACPI_ACTIVE_LOW;
  351. char *link = NULL;
  352. char link_desc[16];
  353. int rc;
  354. pin = dev->pin;
  355. if (!pin) {
  356. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  357. "No interrupt pin configured for device %s\n",
  358. pci_name(dev)));
  359. return 0;
  360. }
  361. if (pci_has_managed_irq(dev))
  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. /*
  384. * No IRQ known to the ACPI subsystem - maybe the BIOS /
  385. * driver reported one, then use it. Exit in any case.
  386. */
  387. if (gsi < 0) {
  388. if (acpi_isa_register_gsi(dev))
  389. dev_warn(&dev->dev, "PCI INT %c: no GSI\n",
  390. pin_name(pin));
  391. kfree(entry);
  392. return 0;
  393. }
  394. rc = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
  395. if (rc < 0) {
  396. dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
  397. pin_name(pin));
  398. kfree(entry);
  399. return rc;
  400. }
  401. pci_set_managed_irq(dev, rc);
  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 || !pci_has_managed_irq(dev))
  420. return;
  421. entry = acpi_pci_irq_lookup(dev, pin);
  422. if (!entry)
  423. return;
  424. if (entry->link)
  425. gsi = acpi_pci_link_free_irq(entry->link);
  426. else
  427. gsi = entry->index;
  428. kfree(entry);
  429. /*
  430. * TBD: It might be worth clearing dev->irq by magic constant
  431. * (e.g. PCI_UNDEFINED_IRQ).
  432. */
  433. dev_dbg(&dev->dev, "PCI INT %c disabled\n", pin_name(pin));
  434. if (gsi >= 0) {
  435. acpi_unregister_gsi(gsi);
  436. pci_reset_managed_irq(dev);
  437. }
  438. }