pci_irq.c 12 KB

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