pci_irq.c 13 KB

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