pci_irq.c 13 KB

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