pci_irq.c 15 KB

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