pci_irq.c 13 KB

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