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