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