pci_irq.c 14 KB

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