pci_irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/pm.h>
  34. #include <linux/pci.h>
  35. #include <linux/acpi.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/acpi_drivers.h>
  38. #define _COMPONENT ACPI_PCI_COMPONENT
  39. ACPI_MODULE_NAME("pci_irq");
  40. struct acpi_prt_entry {
  41. struct list_head list;
  42. struct acpi_pci_id id;
  43. u8 pin;
  44. struct {
  45. acpi_handle handle;
  46. u32 index;
  47. } link;
  48. u32 irq;
  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 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 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 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. struct dmi_system_id *system;
  115. unsigned int segment;
  116. unsigned int bus;
  117. unsigned int device;
  118. unsigned char pin;
  119. char *source; /* according to BIOS */
  120. 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 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
  140. do_prt_fixups(struct acpi_prt_entry *entry, struct acpi_pci_routing_table *prt)
  141. {
  142. int i;
  143. 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
  167. acpi_pci_irq_add_entry(acpi_handle handle,
  168. int segment, int bus, 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 = segment;
  180. entry->id.bus = bus;
  181. entry->id.device = (prt->address >> 16) & 0xFFFF;
  182. entry->pin = prt->pin + 1;
  183. do_prt_fixups(entry, prt);
  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.handle);
  199. entry->link.index = prt->source_index;
  200. }
  201. /*
  202. * Type 2: Static
  203. * --------------
  204. * The 'source' field is NULL, and the 'source_index' field specifies
  205. * the IRQ value, which is hardwired to specific interrupt inputs on
  206. * the interrupt controller.
  207. */
  208. else
  209. entry->link.index = prt->source_index;
  210. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
  211. " %04x:%02x:%02x[%c] -> %s[%d]\n",
  212. entry->id.segment, entry->id.bus,
  213. entry->id.device, pin_name(entry->pin),
  214. prt->source, entry->link.index));
  215. spin_lock(&acpi_prt_lock);
  216. list_add_tail(&entry->list, &acpi_prt_list);
  217. spin_unlock(&acpi_prt_lock);
  218. return 0;
  219. }
  220. int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
  221. {
  222. acpi_status status;
  223. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  224. struct acpi_pci_routing_table *entry;
  225. /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
  226. status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  227. if (ACPI_FAILURE(status))
  228. return -ENODEV;
  229. printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
  230. (char *) buffer.pointer);
  231. kfree(buffer.pointer);
  232. buffer.length = ACPI_ALLOCATE_BUFFER;
  233. buffer.pointer = NULL;
  234. status = acpi_get_irq_routing_table(handle, &buffer);
  235. if (ACPI_FAILURE(status)) {
  236. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
  237. acpi_format_exception(status)));
  238. kfree(buffer.pointer);
  239. return -ENODEV;
  240. }
  241. entry = buffer.pointer;
  242. while (entry && (entry->length > 0)) {
  243. acpi_pci_irq_add_entry(handle, segment, bus, entry);
  244. entry = (struct acpi_pci_routing_table *)
  245. ((unsigned long)entry + entry->length);
  246. }
  247. kfree(buffer.pointer);
  248. return 0;
  249. }
  250. void acpi_pci_irq_del_prt(int segment, int bus)
  251. {
  252. struct acpi_prt_entry *entry, *tmp;
  253. printk(KERN_DEBUG
  254. "ACPI: Delete PCI Interrupt Routing Table for %04x:%02x\n",
  255. segment, bus);
  256. spin_lock(&acpi_prt_lock);
  257. list_for_each_entry_safe(entry, tmp, &acpi_prt_list, list) {
  258. if (segment == entry->id.segment && bus == entry->id.bus) {
  259. list_del(&entry->list);
  260. kfree(entry);
  261. }
  262. }
  263. spin_unlock(&acpi_prt_lock);
  264. }
  265. /* --------------------------------------------------------------------------
  266. PCI Interrupt Routing Support
  267. -------------------------------------------------------------------------- */
  268. static int
  269. acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
  270. int *triggering, int *polarity, char **link)
  271. {
  272. int irq;
  273. if (entry->link.handle) {
  274. irq = acpi_pci_link_allocate_irq(entry->link.handle,
  275. entry->link.index, triggering,
  276. polarity, link);
  277. if (irq < 0) {
  278. printk(KERN_WARNING PREFIX
  279. "Invalid IRQ link routing entry\n");
  280. return -1;
  281. }
  282. } else {
  283. irq = entry->link.index;
  284. *triggering = ACPI_LEVEL_SENSITIVE;
  285. *polarity = ACPI_ACTIVE_LOW;
  286. }
  287. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found GSI %d\n", irq));
  288. return irq;
  289. }
  290. static int
  291. acpi_pci_free_irq(struct acpi_prt_entry *entry)
  292. {
  293. int irq;
  294. if (entry->link.handle) {
  295. irq = acpi_pci_link_free_irq(entry->link.handle);
  296. } else {
  297. irq = entry->link.index;
  298. }
  299. return irq;
  300. }
  301. static struct acpi_prt_entry *
  302. acpi_pci_irq_lookup(struct pci_dev *dev, int pin)
  303. {
  304. struct acpi_prt_entry *entry;
  305. struct pci_dev *bridge;
  306. u8 bridge_pin, orig_pin = pin;
  307. entry = acpi_pci_irq_find_prt_entry(dev, pin);
  308. if (entry) {
  309. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %s[%c] _PRT entry\n",
  310. pci_name(dev), pin_name(pin)));
  311. return entry;
  312. }
  313. /*
  314. * Attempt to derive an IRQ for this device from a parent bridge's
  315. * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
  316. */
  317. bridge = dev->bus->self;
  318. while (bridge) {
  319. pin = (((pin - 1) + PCI_SLOT(dev->devfn)) % 4) + 1;
  320. if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
  321. /* PC card has the same IRQ as its cardbridge */
  322. bridge_pin = bridge->pin;
  323. if (!bridge_pin) {
  324. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  325. "No interrupt pin configured for device %s\n",
  326. pci_name(bridge)));
  327. return NULL;
  328. }
  329. pin = bridge_pin;
  330. }
  331. entry = acpi_pci_irq_find_prt_entry(bridge, pin);
  332. if (entry) {
  333. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  334. "Derived GSI for %s INT %c from %s\n",
  335. pci_name(dev), pin_name(orig_pin),
  336. pci_name(bridge)));
  337. return entry;
  338. }
  339. dev = bridge;
  340. bridge = dev->bus->self;
  341. }
  342. dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
  343. pin_name(orig_pin));
  344. return NULL;
  345. }
  346. /*
  347. * acpi_pci_irq_enable
  348. * success: return 0
  349. * failure: return < 0
  350. */
  351. int acpi_pci_irq_enable(struct pci_dev *dev)
  352. {
  353. struct acpi_prt_entry *entry;
  354. int gsi = 0;
  355. u8 pin = 0;
  356. int triggering = ACPI_LEVEL_SENSITIVE;
  357. int polarity = ACPI_ACTIVE_LOW;
  358. char *link = NULL;
  359. char link_desc[16];
  360. int rc;
  361. pin = dev->pin;
  362. if (!pin) {
  363. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  364. "No interrupt pin configured for device %s\n",
  365. pci_name(dev)));
  366. return 0;
  367. }
  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. gsi = acpi_pci_allocate_irq(entry, &triggering, &polarity,
  380. &link);
  381. else
  382. gsi = -1;
  383. /*
  384. * No IRQ known to the ACPI subsystem - maybe the BIOS /
  385. * driver reported one, then use it. Exit in any case.
  386. */
  387. if (gsi < 0) {
  388. dev_warn(&dev->dev, "PCI INT %c: no GSI", pin_name(pin));
  389. /* Interrupt Line values above 0xF are forbidden */
  390. if (dev->irq > 0 && (dev->irq <= 0xF)) {
  391. printk(" - using IRQ %d\n", dev->irq);
  392. acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
  393. ACPI_ACTIVE_LOW);
  394. return 0;
  395. } else {
  396. printk("\n");
  397. return 0;
  398. }
  399. }
  400. rc = acpi_register_gsi(gsi, triggering, polarity);
  401. if (rc < 0) {
  402. dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
  403. pin_name(pin));
  404. return rc;
  405. }
  406. dev->irq = rc;
  407. if (link)
  408. snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
  409. else
  410. link_desc[0] = '\0';
  411. dev_info(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
  412. pin_name(pin), link_desc, gsi,
  413. (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
  414. (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
  415. return 0;
  416. }
  417. /* FIXME: implement x86/x86_64 version */
  418. void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
  419. {
  420. }
  421. void acpi_pci_irq_disable(struct pci_dev *dev)
  422. {
  423. struct acpi_prt_entry *entry;
  424. int gsi = 0;
  425. u8 pin = 0;
  426. pin = dev->pin;
  427. if (!pin)
  428. return;
  429. entry = acpi_pci_irq_lookup(dev, pin);
  430. if (!entry)
  431. return;
  432. gsi = acpi_pci_free_irq(entry);
  433. /*
  434. * TBD: It might be worth clearing dev->irq by magic constant
  435. * (e.g. PCI_UNDEFINED_IRQ).
  436. */
  437. dev_info(&dev->dev, "PCI INT %c disabled\n", pin_name(pin));
  438. acpi_unregister_gsi(gsi);
  439. }