pci_irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/pm.h>
  33. #include <linux/pci.h>
  34. #include <linux/acpi.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/acpi_drivers.h>
  37. #define _COMPONENT ACPI_PCI_COMPONENT
  38. ACPI_MODULE_NAME("pci_irq");
  39. static struct acpi_prt_list acpi_prt;
  40. static DEFINE_SPINLOCK(acpi_prt_lock);
  41. /* --------------------------------------------------------------------------
  42. PCI IRQ Routing Table (PRT) Support
  43. -------------------------------------------------------------------------- */
  44. static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(int segment,
  45. int bus,
  46. int device, int pin)
  47. {
  48. struct acpi_prt_entry *entry = NULL;
  49. if (!acpi_prt.count)
  50. return NULL;
  51. /*
  52. * Parse through all PRT entries looking for a match on the specified
  53. * PCI device's segment, bus, device, and pin (don't care about func).
  54. *
  55. */
  56. spin_lock(&acpi_prt_lock);
  57. list_for_each_entry(entry, &acpi_prt.entries, node) {
  58. if ((segment == entry->id.segment)
  59. && (bus == entry->id.bus)
  60. && (device == entry->id.device)
  61. && (pin == entry->pin)) {
  62. spin_unlock(&acpi_prt_lock);
  63. return entry;
  64. }
  65. }
  66. spin_unlock(&acpi_prt_lock);
  67. return NULL;
  68. }
  69. static int
  70. acpi_pci_irq_add_entry(acpi_handle handle,
  71. int segment, int bus, struct acpi_pci_routing_table *prt)
  72. {
  73. struct acpi_prt_entry *entry = NULL;
  74. if (!prt)
  75. return -EINVAL;
  76. entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
  77. if (!entry)
  78. return -ENOMEM;
  79. entry->id.segment = segment;
  80. entry->id.bus = bus;
  81. entry->id.device = (prt->address >> 16) & 0xFFFF;
  82. entry->id.function = prt->address & 0xFFFF;
  83. entry->pin = prt->pin;
  84. /*
  85. * Type 1: Dynamic
  86. * ---------------
  87. * The 'source' field specifies the PCI interrupt link device used to
  88. * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
  89. * indicates which resource descriptor in the resource template (of
  90. * the link device) this interrupt is allocated from.
  91. *
  92. * NOTE: Don't query the Link Device for IRQ information at this time
  93. * because Link Device enumeration may not have occurred yet
  94. * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
  95. * namespace).
  96. */
  97. if (prt->source[0]) {
  98. acpi_get_handle(handle, prt->source, &entry->link.handle);
  99. entry->link.index = prt->source_index;
  100. }
  101. /*
  102. * Type 2: Static
  103. * --------------
  104. * The 'source' field is NULL, and the 'source_index' field specifies
  105. * the IRQ value, which is hardwired to specific interrupt inputs on
  106. * the interrupt controller.
  107. */
  108. else
  109. entry->link.index = prt->source_index;
  110. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
  111. " %02X:%02X:%02X[%c] -> %s[%d]\n",
  112. entry->id.segment, entry->id.bus,
  113. entry->id.device, ('A' + entry->pin), prt->source,
  114. entry->link.index));
  115. spin_lock(&acpi_prt_lock);
  116. list_add_tail(&entry->node, &acpi_prt.entries);
  117. acpi_prt.count++;
  118. spin_unlock(&acpi_prt_lock);
  119. return 0;
  120. }
  121. static void
  122. acpi_pci_irq_del_entry(int segment, int bus, struct acpi_prt_entry *entry)
  123. {
  124. if (segment == entry->id.segment && bus == entry->id.bus) {
  125. acpi_prt.count--;
  126. list_del(&entry->node);
  127. kfree(entry);
  128. }
  129. }
  130. int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
  131. {
  132. acpi_status status = AE_OK;
  133. char *pathname = NULL;
  134. struct acpi_buffer buffer = { 0, NULL };
  135. struct acpi_pci_routing_table *prt = NULL;
  136. struct acpi_pci_routing_table *entry = NULL;
  137. static int first_time = 1;
  138. pathname = kzalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
  139. if (!pathname)
  140. return -ENOMEM;
  141. if (first_time) {
  142. acpi_prt.count = 0;
  143. INIT_LIST_HEAD(&acpi_prt.entries);
  144. first_time = 0;
  145. }
  146. /*
  147. * NOTE: We're given a 'handle' to the _PRT object's parent device
  148. * (either a PCI root bridge or PCI-PCI bridge).
  149. */
  150. buffer.length = ACPI_PATHNAME_MAX;
  151. buffer.pointer = pathname;
  152. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  153. printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
  154. pathname);
  155. /*
  156. * Evaluate this _PRT and add its entries to our global list (acpi_prt).
  157. */
  158. buffer.length = 0;
  159. buffer.pointer = NULL;
  160. kfree(pathname);
  161. status = acpi_get_irq_routing_table(handle, &buffer);
  162. if (status != AE_BUFFER_OVERFLOW) {
  163. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
  164. acpi_format_exception(status)));
  165. return -ENODEV;
  166. }
  167. prt = kzalloc(buffer.length, GFP_KERNEL);
  168. if (!prt) {
  169. return -ENOMEM;
  170. }
  171. buffer.pointer = prt;
  172. status = acpi_get_irq_routing_table(handle, &buffer);
  173. if (ACPI_FAILURE(status)) {
  174. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
  175. acpi_format_exception(status)));
  176. kfree(buffer.pointer);
  177. return -ENODEV;
  178. }
  179. entry = prt;
  180. while (entry && (entry->length > 0)) {
  181. acpi_pci_irq_add_entry(handle, segment, bus, entry);
  182. entry = (struct acpi_pci_routing_table *)
  183. ((unsigned long)entry + entry->length);
  184. }
  185. kfree(prt);
  186. return 0;
  187. }
  188. void acpi_pci_irq_del_prt(int segment, int bus)
  189. {
  190. struct list_head *node = NULL, *n = NULL;
  191. struct acpi_prt_entry *entry = NULL;
  192. if (!acpi_prt.count) {
  193. return;
  194. }
  195. printk(KERN_DEBUG
  196. "ACPI: Delete PCI Interrupt Routing Table for %x:%x\n", segment,
  197. bus);
  198. spin_lock(&acpi_prt_lock);
  199. list_for_each_safe(node, n, &acpi_prt.entries) {
  200. entry = list_entry(node, struct acpi_prt_entry, node);
  201. acpi_pci_irq_del_entry(segment, bus, entry);
  202. }
  203. spin_unlock(&acpi_prt_lock);
  204. }
  205. /* --------------------------------------------------------------------------
  206. PCI Interrupt Routing Support
  207. -------------------------------------------------------------------------- */
  208. typedef int (*irq_lookup_func) (struct acpi_prt_entry *, int *, int *, char **);
  209. static int
  210. acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
  211. int *triggering, int *polarity, char **link)
  212. {
  213. int irq;
  214. if (entry->link.handle) {
  215. irq = acpi_pci_link_allocate_irq(entry->link.handle,
  216. entry->link.index, triggering,
  217. polarity, link);
  218. if (irq < 0) {
  219. printk(KERN_WARNING PREFIX
  220. "Invalid IRQ link routing entry\n");
  221. return -1;
  222. }
  223. } else {
  224. irq = entry->link.index;
  225. *triggering = ACPI_LEVEL_SENSITIVE;
  226. *polarity = ACPI_ACTIVE_LOW;
  227. }
  228. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", irq));
  229. return irq;
  230. }
  231. static int
  232. acpi_pci_free_irq(struct acpi_prt_entry *entry,
  233. int *triggering, int *polarity, char **link)
  234. {
  235. int irq;
  236. if (entry->link.handle) {
  237. irq = acpi_pci_link_free_irq(entry->link.handle);
  238. } else {
  239. irq = entry->link.index;
  240. }
  241. return irq;
  242. }
  243. /*
  244. * acpi_pci_irq_lookup
  245. * success: return IRQ >= 0
  246. * failure: return -1
  247. */
  248. static int
  249. acpi_pci_irq_lookup(struct pci_bus *bus,
  250. int device,
  251. int pin,
  252. int *triggering,
  253. int *polarity, char **link, irq_lookup_func func)
  254. {
  255. struct acpi_prt_entry *entry = NULL;
  256. int segment = pci_domain_nr(bus);
  257. int bus_nr = bus->number;
  258. int ret;
  259. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  260. "Searching for PRT entry for %02x:%02x:%02x[%c]\n",
  261. segment, bus_nr, device, ('A' + pin)));
  262. entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
  263. if (!entry) {
  264. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PRT entry not found\n"));
  265. return -1;
  266. }
  267. ret = func(entry, triggering, polarity, link);
  268. return ret;
  269. }
  270. /*
  271. * acpi_pci_irq_derive
  272. * success: return IRQ >= 0
  273. * failure: return < 0
  274. */
  275. static int
  276. acpi_pci_irq_derive(struct pci_dev *dev,
  277. int pin,
  278. int *triggering,
  279. int *polarity, char **link, irq_lookup_func func)
  280. {
  281. struct pci_dev *bridge = dev;
  282. int irq = -1;
  283. u8 bridge_pin = 0;
  284. if (!dev)
  285. return -EINVAL;
  286. /*
  287. * Attempt to derive an IRQ for this device from a parent bridge's
  288. * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
  289. */
  290. while (irq < 0 && bridge->bus->self) {
  291. pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
  292. bridge = bridge->bus->self;
  293. if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
  294. /* PC card has the same IRQ as its cardbridge */
  295. bridge_pin = bridge->pin;
  296. if (!bridge_pin) {
  297. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  298. "No interrupt pin configured for device %s\n",
  299. pci_name(bridge)));
  300. return -1;
  301. }
  302. /* Pin is from 0 to 3 */
  303. bridge_pin--;
  304. pin = bridge_pin;
  305. }
  306. irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
  307. pin, triggering, polarity,
  308. link, func);
  309. }
  310. if (irq < 0) {
  311. printk(KERN_WARNING PREFIX "Unable to derive IRQ for device %s\n",
  312. pci_name(dev));
  313. return -1;
  314. }
  315. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
  316. irq, pci_name(dev), pci_name(bridge)));
  317. return irq;
  318. }
  319. /*
  320. * acpi_pci_irq_enable
  321. * success: return 0
  322. * failure: return < 0
  323. */
  324. int acpi_pci_irq_enable(struct pci_dev *dev)
  325. {
  326. int irq = 0;
  327. u8 pin = 0;
  328. int triggering = ACPI_LEVEL_SENSITIVE;
  329. int polarity = ACPI_ACTIVE_LOW;
  330. char *link = NULL;
  331. int rc;
  332. if (!dev)
  333. return -EINVAL;
  334. pin = dev->pin;
  335. if (!pin) {
  336. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  337. "No interrupt pin configured for device %s\n",
  338. pci_name(dev)));
  339. return 0;
  340. }
  341. pin--;
  342. if (!dev->bus) {
  343. printk(KERN_ERR PREFIX "Invalid (NULL) 'bus' field\n");
  344. return -ENODEV;
  345. }
  346. /*
  347. * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
  348. * values override any BIOS-assigned IRQs set during boot.
  349. */
  350. irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
  351. &triggering, &polarity, &link,
  352. acpi_pci_allocate_irq);
  353. /*
  354. * If no PRT entry was found, we'll try to derive an IRQ from the
  355. * device's parent bridge.
  356. */
  357. if (irq < 0)
  358. irq = acpi_pci_irq_derive(dev, pin, &triggering,
  359. &polarity, &link,
  360. acpi_pci_allocate_irq);
  361. if (irq < 0) {
  362. /*
  363. * IDE legacy mode controller IRQs are magic. Why do compat
  364. * extensions always make such a nasty mess.
  365. */
  366. if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
  367. (dev->class & 0x05) == 0)
  368. return 0;
  369. }
  370. /*
  371. * No IRQ known to the ACPI subsystem - maybe the BIOS /
  372. * driver reported one, then use it. Exit in any case.
  373. */
  374. if (irq < 0) {
  375. printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: no GSI",
  376. pci_name(dev), ('A' + pin));
  377. /* Interrupt Line values above 0xF are forbidden */
  378. if (dev->irq > 0 && (dev->irq <= 0xF)) {
  379. printk(" - using IRQ %d\n", dev->irq);
  380. acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
  381. ACPI_ACTIVE_LOW);
  382. return 0;
  383. } else {
  384. printk("\n");
  385. return 0;
  386. }
  387. }
  388. rc = acpi_register_gsi(irq, triggering, polarity);
  389. if (rc < 0) {
  390. printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: failed "
  391. "to register GSI\n", pci_name(dev), ('A' + pin));
  392. return rc;
  393. }
  394. dev->irq = rc;
  395. printk(KERN_INFO PREFIX "PCI Interrupt %s[%c] -> ",
  396. pci_name(dev), 'A' + pin);
  397. if (link)
  398. printk("Link [%s] -> ", link);
  399. printk("GSI %u (%s, %s) -> IRQ %d\n", irq,
  400. (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
  401. (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
  402. return 0;
  403. }
  404. EXPORT_SYMBOL(acpi_pci_irq_enable);
  405. /* FIXME: implement x86/x86_64 version */
  406. void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
  407. {
  408. }
  409. void acpi_pci_irq_disable(struct pci_dev *dev)
  410. {
  411. int gsi = 0;
  412. u8 pin = 0;
  413. int triggering = ACPI_LEVEL_SENSITIVE;
  414. int polarity = ACPI_ACTIVE_LOW;
  415. if (!dev || !dev->bus)
  416. return;
  417. pin = dev->pin;
  418. if (!pin)
  419. return;
  420. pin--;
  421. /*
  422. * First we check the PCI IRQ routing table (PRT) for an IRQ.
  423. */
  424. gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
  425. &triggering, &polarity, NULL,
  426. acpi_pci_free_irq);
  427. /*
  428. * If no PRT entry was found, we'll try to derive an IRQ from the
  429. * device's parent bridge.
  430. */
  431. if (gsi < 0)
  432. gsi = acpi_pci_irq_derive(dev, pin,
  433. &triggering, &polarity, NULL,
  434. acpi_pci_free_irq);
  435. if (gsi < 0)
  436. return;
  437. /*
  438. * TBD: It might be worth clearing dev->irq by magic constant
  439. * (e.g. PCI_UNDEFINED_IRQ).
  440. */
  441. printk(KERN_INFO PREFIX "PCI interrupt for device %s disabled\n",
  442. pci_name(dev));
  443. acpi_unregister_gsi(gsi);
  444. return;
  445. }