pci_irq.c 14 KB

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