pci_irq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 node;
  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. struct acpi_prt_list {
  51. int count;
  52. struct list_head entries;
  53. };
  54. static struct acpi_prt_list acpi_prt;
  55. static DEFINE_SPINLOCK(acpi_prt_lock);
  56. /* --------------------------------------------------------------------------
  57. PCI IRQ Routing Table (PRT) Support
  58. -------------------------------------------------------------------------- */
  59. static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(int segment,
  60. int bus,
  61. int device, int pin)
  62. {
  63. struct acpi_prt_entry *entry = NULL;
  64. if (!acpi_prt.count)
  65. return NULL;
  66. /*
  67. * Parse through all PRT entries looking for a match on the specified
  68. * PCI device's segment, bus, device, and pin (don't care about func).
  69. *
  70. */
  71. spin_lock(&acpi_prt_lock);
  72. list_for_each_entry(entry, &acpi_prt.entries, node) {
  73. if ((segment == entry->id.segment)
  74. && (bus == entry->id.bus)
  75. && (device == entry->id.device)
  76. && (pin == entry->pin)) {
  77. spin_unlock(&acpi_prt_lock);
  78. return entry;
  79. }
  80. }
  81. spin_unlock(&acpi_prt_lock);
  82. return NULL;
  83. }
  84. /* http://bugzilla.kernel.org/show_bug.cgi?id=4773 */
  85. static struct dmi_system_id medion_md9580[] = {
  86. {
  87. .ident = "Medion MD9580-F laptop",
  88. .matches = {
  89. DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
  90. DMI_MATCH(DMI_PRODUCT_NAME, "A555"),
  91. },
  92. },
  93. { }
  94. };
  95. /* http://bugzilla.kernel.org/show_bug.cgi?id=5044 */
  96. static struct dmi_system_id dell_optiplex[] = {
  97. {
  98. .ident = "Dell Optiplex GX1",
  99. .matches = {
  100. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  101. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex GX1 600S+"),
  102. },
  103. },
  104. { }
  105. };
  106. /* http://bugzilla.kernel.org/show_bug.cgi?id=10138 */
  107. static struct dmi_system_id hp_t5710[] = {
  108. {
  109. .ident = "HP t5710",
  110. .matches = {
  111. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  112. DMI_MATCH(DMI_PRODUCT_NAME, "hp t5000 series"),
  113. DMI_MATCH(DMI_BOARD_NAME, "098Ch"),
  114. },
  115. },
  116. { }
  117. };
  118. struct prt_quirk {
  119. struct dmi_system_id *system;
  120. unsigned int segment;
  121. unsigned int bus;
  122. unsigned int device;
  123. unsigned char pin;
  124. char *source; /* according to BIOS */
  125. char *actual_source;
  126. };
  127. /*
  128. * These systems have incorrect _PRT entries. The BIOS claims the PCI
  129. * interrupt at the listed segment/bus/device/pin is connected to the first
  130. * link device, but it is actually connected to the second.
  131. */
  132. static struct prt_quirk prt_quirks[] = {
  133. { medion_md9580, 0, 0, 9, 'A',
  134. "\\_SB_.PCI0.ISA_.LNKA",
  135. "\\_SB_.PCI0.ISA_.LNKB"},
  136. { dell_optiplex, 0, 0, 0xd, 'A',
  137. "\\_SB_.LNKB",
  138. "\\_SB_.LNKA"},
  139. { hp_t5710, 0, 0, 1, 'A',
  140. "\\_SB_.PCI0.LNK1",
  141. "\\_SB_.PCI0.LNK3"},
  142. };
  143. static void
  144. do_prt_fixups(struct acpi_prt_entry *entry, struct acpi_pci_routing_table *prt)
  145. {
  146. int i;
  147. struct prt_quirk *quirk;
  148. for (i = 0; i < ARRAY_SIZE(prt_quirks); i++) {
  149. quirk = &prt_quirks[i];
  150. /* All current quirks involve link devices, not GSIs */
  151. if (!prt->source)
  152. continue;
  153. if (dmi_check_system(quirk->system) &&
  154. entry->id.segment == quirk->segment &&
  155. entry->id.bus == quirk->bus &&
  156. entry->id.device == quirk->device &&
  157. entry->pin + 'A' == quirk->pin &&
  158. !strcmp(prt->source, quirk->source) &&
  159. strlen(prt->source) >= strlen(quirk->actual_source)) {
  160. printk(KERN_WARNING PREFIX "firmware reports "
  161. "%04x:%02x:%02x PCI INT %c connected to %s; "
  162. "changing to %s\n",
  163. entry->id.segment, entry->id.bus,
  164. entry->id.device, 'A' + entry->pin,
  165. prt->source, quirk->actual_source);
  166. strcpy(prt->source, quirk->actual_source);
  167. }
  168. }
  169. }
  170. static int
  171. acpi_pci_irq_add_entry(acpi_handle handle,
  172. int segment, int bus, struct acpi_pci_routing_table *prt)
  173. {
  174. struct acpi_prt_entry *entry = NULL;
  175. entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
  176. if (!entry)
  177. return -ENOMEM;
  178. entry->id.segment = segment;
  179. entry->id.bus = bus;
  180. entry->id.device = (prt->address >> 16) & 0xFFFF;
  181. entry->pin = prt->pin;
  182. do_prt_fixups(entry, prt);
  183. /*
  184. * Type 1: Dynamic
  185. * ---------------
  186. * The 'source' field specifies the PCI interrupt link device used to
  187. * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
  188. * indicates which resource descriptor in the resource template (of
  189. * the link device) this interrupt is allocated from.
  190. *
  191. * NOTE: Don't query the Link Device for IRQ information at this time
  192. * because Link Device enumeration may not have occurred yet
  193. * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
  194. * namespace).
  195. */
  196. if (prt->source[0]) {
  197. acpi_get_handle(handle, prt->source, &entry->link.handle);
  198. entry->link.index = prt->source_index;
  199. }
  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. else
  208. entry->link.index = prt->source_index;
  209. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
  210. " %04x:%02x:%02x[%c] -> %s[%d]\n",
  211. entry->id.segment, entry->id.bus,
  212. entry->id.device, ('A' + entry->pin), prt->source,
  213. entry->link.index));
  214. spin_lock(&acpi_prt_lock);
  215. list_add_tail(&entry->node, &acpi_prt.entries);
  216. acpi_prt.count++;
  217. spin_unlock(&acpi_prt_lock);
  218. return 0;
  219. }
  220. static void
  221. acpi_pci_irq_del_entry(int segment, int bus, struct acpi_prt_entry *entry)
  222. {
  223. if (segment == entry->id.segment && bus == entry->id.bus) {
  224. acpi_prt.count--;
  225. list_del(&entry->node);
  226. kfree(entry);
  227. }
  228. }
  229. int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
  230. {
  231. acpi_status status;
  232. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  233. struct acpi_pci_routing_table *entry;
  234. static int first_time = 1;
  235. if (first_time) {
  236. acpi_prt.count = 0;
  237. INIT_LIST_HEAD(&acpi_prt.entries);
  238. first_time = 0;
  239. }
  240. /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
  241. status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  242. if (ACPI_FAILURE(status))
  243. return -ENODEV;
  244. printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
  245. (char *) buffer.pointer);
  246. kfree(buffer.pointer);
  247. buffer.length = ACPI_ALLOCATE_BUFFER;
  248. buffer.pointer = NULL;
  249. status = acpi_get_irq_routing_table(handle, &buffer);
  250. if (ACPI_FAILURE(status)) {
  251. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
  252. acpi_format_exception(status)));
  253. kfree(buffer.pointer);
  254. return -ENODEV;
  255. }
  256. entry = buffer.pointer;
  257. while (entry && (entry->length > 0)) {
  258. acpi_pci_irq_add_entry(handle, segment, bus, entry);
  259. entry = (struct acpi_pci_routing_table *)
  260. ((unsigned long)entry + entry->length);
  261. }
  262. kfree(buffer.pointer);
  263. return 0;
  264. }
  265. void acpi_pci_irq_del_prt(int segment, int bus)
  266. {
  267. struct list_head *node = NULL, *n = NULL;
  268. struct acpi_prt_entry *entry = NULL;
  269. if (!acpi_prt.count) {
  270. return;
  271. }
  272. printk(KERN_DEBUG
  273. "ACPI: Delete PCI Interrupt Routing Table for %04x:%02x\n",
  274. segment, bus);
  275. spin_lock(&acpi_prt_lock);
  276. list_for_each_safe(node, n, &acpi_prt.entries) {
  277. entry = list_entry(node, struct acpi_prt_entry, node);
  278. acpi_pci_irq_del_entry(segment, bus, entry);
  279. }
  280. spin_unlock(&acpi_prt_lock);
  281. }
  282. /* --------------------------------------------------------------------------
  283. PCI Interrupt Routing Support
  284. -------------------------------------------------------------------------- */
  285. typedef int (*irq_lookup_func) (struct acpi_prt_entry *, int *, int *, char **);
  286. static int
  287. acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
  288. int *triggering, int *polarity, char **link)
  289. {
  290. int irq;
  291. if (entry->link.handle) {
  292. irq = acpi_pci_link_allocate_irq(entry->link.handle,
  293. entry->link.index, triggering,
  294. polarity, link);
  295. if (irq < 0) {
  296. printk(KERN_WARNING PREFIX
  297. "Invalid IRQ link routing entry\n");
  298. return -1;
  299. }
  300. } else {
  301. irq = entry->link.index;
  302. *triggering = ACPI_LEVEL_SENSITIVE;
  303. *polarity = ACPI_ACTIVE_LOW;
  304. }
  305. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found GSI %d\n", irq));
  306. return irq;
  307. }
  308. static int
  309. acpi_pci_free_irq(struct acpi_prt_entry *entry,
  310. int *triggering, int *polarity, char **link)
  311. {
  312. int irq;
  313. if (entry->link.handle) {
  314. irq = acpi_pci_link_free_irq(entry->link.handle);
  315. } else {
  316. irq = entry->link.index;
  317. }
  318. return irq;
  319. }
  320. /*
  321. * acpi_pci_irq_lookup
  322. * success: return IRQ >= 0
  323. * failure: return -1
  324. */
  325. static int
  326. acpi_pci_irq_lookup(struct pci_bus *bus,
  327. int device,
  328. int pin,
  329. int *triggering,
  330. int *polarity, char **link, irq_lookup_func func)
  331. {
  332. struct acpi_prt_entry *entry = NULL;
  333. int segment = pci_domain_nr(bus);
  334. int bus_nr = bus->number;
  335. int ret;
  336. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  337. "Searching for _PRT entry for %04x:%02x:%02x[%c]\n",
  338. segment, bus_nr, device, ('A' + pin)));
  339. entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
  340. if (!entry) {
  341. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_PRT entry not found\n"));
  342. return -1;
  343. }
  344. ret = func(entry, triggering, polarity, link);
  345. return ret;
  346. }
  347. /*
  348. * acpi_pci_irq_derive
  349. * success: return IRQ >= 0
  350. * failure: return < 0
  351. */
  352. static int
  353. acpi_pci_irq_derive(struct pci_dev *dev,
  354. int pin,
  355. int *triggering,
  356. int *polarity, char **link, irq_lookup_func func)
  357. {
  358. struct pci_dev *bridge = dev;
  359. int irq = -1;
  360. u8 bridge_pin = 0, orig_pin = pin;
  361. /*
  362. * Attempt to derive an IRQ for this device from a parent bridge's
  363. * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
  364. */
  365. while (irq < 0 && bridge->bus->self) {
  366. pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
  367. bridge = bridge->bus->self;
  368. if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
  369. /* PC card has the same IRQ as its cardbridge */
  370. bridge_pin = bridge->pin;
  371. if (!bridge_pin) {
  372. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  373. "No interrupt pin configured for device %s\n",
  374. pci_name(bridge)));
  375. return -1;
  376. }
  377. /* Pin is from 0 to 3 */
  378. bridge_pin--;
  379. pin = bridge_pin;
  380. }
  381. irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
  382. pin, triggering, polarity,
  383. link, func);
  384. }
  385. if (irq < 0) {
  386. dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
  387. 'A' + orig_pin);
  388. return -1;
  389. }
  390. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive GSI %d for device %s from %s\n",
  391. irq, pci_name(dev), pci_name(bridge)));
  392. return irq;
  393. }
  394. /*
  395. * acpi_pci_irq_enable
  396. * success: return 0
  397. * failure: return < 0
  398. */
  399. int acpi_pci_irq_enable(struct pci_dev *dev)
  400. {
  401. int gsi = 0;
  402. u8 pin = 0;
  403. int triggering = ACPI_LEVEL_SENSITIVE;
  404. int polarity = ACPI_ACTIVE_LOW;
  405. char *link = NULL;
  406. char link_desc[16];
  407. int rc;
  408. pin = dev->pin;
  409. if (!pin) {
  410. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  411. "No interrupt pin configured for device %s\n",
  412. pci_name(dev)));
  413. return 0;
  414. }
  415. pin--;
  416. /*
  417. * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
  418. * values override any BIOS-assigned IRQs set during boot.
  419. */
  420. gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
  421. &triggering, &polarity, &link,
  422. acpi_pci_allocate_irq);
  423. /*
  424. * If no PRT entry was found, we'll try to derive an IRQ from the
  425. * device's parent bridge.
  426. */
  427. if (gsi < 0)
  428. gsi = acpi_pci_irq_derive(dev, pin, &triggering,
  429. &polarity, &link,
  430. acpi_pci_allocate_irq);
  431. if (gsi < 0) {
  432. /*
  433. * IDE legacy mode controller IRQs are magic. Why do compat
  434. * extensions always make such a nasty mess.
  435. */
  436. if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
  437. (dev->class & 0x05) == 0)
  438. return 0;
  439. }
  440. /*
  441. * No IRQ known to the ACPI subsystem - maybe the BIOS /
  442. * driver reported one, then use it. Exit in any case.
  443. */
  444. if (gsi < 0) {
  445. dev_warn(&dev->dev, "PCI INT %c: no GSI", 'A' + pin);
  446. /* Interrupt Line values above 0xF are forbidden */
  447. if (dev->irq > 0 && (dev->irq <= 0xF)) {
  448. printk(" - using IRQ %d\n", dev->irq);
  449. acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
  450. ACPI_ACTIVE_LOW);
  451. return 0;
  452. } else {
  453. printk("\n");
  454. return 0;
  455. }
  456. }
  457. rc = acpi_register_gsi(gsi, triggering, polarity);
  458. if (rc < 0) {
  459. dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
  460. 'A' + pin);
  461. return rc;
  462. }
  463. dev->irq = rc;
  464. if (link)
  465. snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
  466. else
  467. link_desc[0] = '\0';
  468. dev_info(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
  469. 'A' + pin, link_desc, gsi,
  470. (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
  471. (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
  472. return 0;
  473. }
  474. /* FIXME: implement x86/x86_64 version */
  475. void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
  476. {
  477. }
  478. void acpi_pci_irq_disable(struct pci_dev *dev)
  479. {
  480. int gsi = 0;
  481. u8 pin = 0;
  482. int triggering = ACPI_LEVEL_SENSITIVE;
  483. int polarity = ACPI_ACTIVE_LOW;
  484. pin = dev->pin;
  485. if (!pin)
  486. return;
  487. pin--;
  488. /*
  489. * First we check the PCI IRQ routing table (PRT) for an IRQ.
  490. */
  491. gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
  492. &triggering, &polarity, NULL,
  493. acpi_pci_free_irq);
  494. /*
  495. * If no PRT entry was found, we'll try to derive an IRQ from the
  496. * device's parent bridge.
  497. */
  498. if (gsi < 0)
  499. gsi = acpi_pci_irq_derive(dev, pin,
  500. &triggering, &polarity, NULL,
  501. acpi_pci_free_irq);
  502. if (gsi < 0)
  503. return;
  504. /*
  505. * TBD: It might be worth clearing dev->irq by magic constant
  506. * (e.g. PCI_UNDEFINED_IRQ).
  507. */
  508. dev_info(&dev->dev, "PCI INT %c disabled\n", 'A' + pin);
  509. acpi_unregister_gsi(gsi);
  510. }