pci_irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. typedef int (*irq_lookup_func)(struct acpi_prt_entry *, int *, int *, char **);
  230. static int
  231. acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
  232. int *edge_level,
  233. int *active_high_low,
  234. char **link)
  235. {
  236. int irq;
  237. ACPI_FUNCTION_TRACE("acpi_pci_allocate_irq");
  238. if (entry->link.handle) {
  239. irq = acpi_pci_link_allocate_irq(entry->link.handle,
  240. entry->link.index, edge_level, active_high_low, link);
  241. if (irq < 0) {
  242. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid IRQ link routing entry\n"));
  243. return_VALUE(-1);
  244. }
  245. } else {
  246. irq = entry->link.index;
  247. *edge_level = ACPI_LEVEL_SENSITIVE;
  248. *active_high_low = ACPI_ACTIVE_LOW;
  249. }
  250. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", irq));
  251. return_VALUE(irq);
  252. }
  253. static int
  254. acpi_pci_free_irq(struct acpi_prt_entry *entry,
  255. int *edge_level,
  256. int *active_high_low,
  257. char **link)
  258. {
  259. int irq;
  260. ACPI_FUNCTION_TRACE("acpi_pci_free_irq");
  261. if (entry->link.handle) {
  262. irq = acpi_pci_link_free_irq(entry->link.handle);
  263. } else {
  264. irq = entry->link.index;
  265. }
  266. return_VALUE(irq);
  267. }
  268. /*
  269. * acpi_pci_irq_lookup
  270. * success: return IRQ >= 0
  271. * failure: return -1
  272. */
  273. static int
  274. acpi_pci_irq_lookup (
  275. struct pci_bus *bus,
  276. int device,
  277. int pin,
  278. int *edge_level,
  279. int *active_high_low,
  280. char **link,
  281. irq_lookup_func func)
  282. {
  283. struct acpi_prt_entry *entry = NULL;
  284. int segment = pci_domain_nr(bus);
  285. int bus_nr = bus->number;
  286. int ret;
  287. ACPI_FUNCTION_TRACE("acpi_pci_irq_lookup");
  288. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  289. "Searching for PRT entry for %02x:%02x:%02x[%c]\n",
  290. segment, bus_nr, device, ('A' + pin)));
  291. entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
  292. if (!entry) {
  293. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PRT entry not found\n"));
  294. return_VALUE(-1);
  295. }
  296. ret = func(entry, edge_level, active_high_low, link);
  297. return_VALUE(ret);
  298. }
  299. /*
  300. * acpi_pci_irq_derive
  301. * success: return IRQ >= 0
  302. * failure: return < 0
  303. */
  304. static int
  305. acpi_pci_irq_derive (
  306. struct pci_dev *dev,
  307. int pin,
  308. int *edge_level,
  309. int *active_high_low,
  310. char **link,
  311. irq_lookup_func func)
  312. {
  313. struct pci_dev *bridge = dev;
  314. int irq = -1;
  315. u8 bridge_pin = 0;
  316. ACPI_FUNCTION_TRACE("acpi_pci_irq_derive");
  317. if (!dev)
  318. return_VALUE(-EINVAL);
  319. /*
  320. * Attempt to derive an IRQ for this device from a parent bridge's
  321. * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
  322. */
  323. while (irq < 0 && bridge->bus->self) {
  324. pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
  325. bridge = bridge->bus->self;
  326. if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
  327. /* PC card has the same IRQ as its cardbridge */
  328. pci_read_config_byte(bridge, PCI_INTERRUPT_PIN, &bridge_pin);
  329. if (!bridge_pin) {
  330. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  331. "No interrupt pin configured for device %s\n", pci_name(bridge)));
  332. return_VALUE(-1);
  333. }
  334. /* Pin is from 0 to 3 */
  335. bridge_pin --;
  336. pin = bridge_pin;
  337. }
  338. irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
  339. pin, edge_level, active_high_low, link, func);
  340. }
  341. if (irq < 0) {
  342. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Unable to derive IRQ for device %s\n", pci_name(dev)));
  343. return_VALUE(-1);
  344. }
  345. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
  346. irq, pci_name(dev), pci_name(bridge)));
  347. return_VALUE(irq);
  348. }
  349. /*
  350. * acpi_pci_irq_enable
  351. * success: return 0
  352. * failure: return < 0
  353. */
  354. int
  355. acpi_pci_irq_enable (
  356. struct pci_dev *dev)
  357. {
  358. int irq = 0;
  359. u8 pin = 0;
  360. int edge_level = ACPI_LEVEL_SENSITIVE;
  361. int active_high_low = ACPI_ACTIVE_LOW;
  362. char *link = NULL;
  363. ACPI_FUNCTION_TRACE("acpi_pci_irq_enable");
  364. if (!dev)
  365. return_VALUE(-EINVAL);
  366. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  367. if (!pin) {
  368. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No interrupt pin configured for device %s\n", pci_name(dev)));
  369. return_VALUE(0);
  370. }
  371. pin--;
  372. if (!dev->bus) {
  373. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) 'bus' field\n"));
  374. return_VALUE(-ENODEV);
  375. }
  376. /*
  377. * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
  378. * values override any BIOS-assigned IRQs set during boot.
  379. */
  380. irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
  381. &edge_level, &active_high_low, &link, acpi_pci_allocate_irq);
  382. /*
  383. * If no PRT entry was found, we'll try to derive an IRQ from the
  384. * device's parent bridge.
  385. */
  386. if (irq < 0)
  387. irq = acpi_pci_irq_derive(dev, pin, &edge_level,
  388. &active_high_low, &link, acpi_pci_allocate_irq);
  389. /*
  390. * No IRQ known to the ACPI subsystem - maybe the BIOS /
  391. * driver reported one, then use it. Exit in any case.
  392. */
  393. if (irq < 0) {
  394. printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: no GSI",
  395. pci_name(dev), ('A' + pin));
  396. /* Interrupt Line values above 0xF are forbidden */
  397. if (dev->irq >= 0 && (dev->irq <= 0xF)) {
  398. printk(" - using IRQ %d\n", dev->irq);
  399. return_VALUE(0);
  400. }
  401. else {
  402. printk("\n");
  403. return_VALUE(0);
  404. }
  405. }
  406. dev->irq = acpi_register_gsi(irq, edge_level, active_high_low);
  407. printk(KERN_INFO PREFIX "PCI Interrupt %s[%c] -> ",
  408. pci_name(dev), 'A' + pin);
  409. if (link)
  410. printk("Link [%s] -> ", link);
  411. printk("GSI %u (%s, %s) -> IRQ %d\n", irq,
  412. (edge_level == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
  413. (active_high_low == ACPI_ACTIVE_LOW) ? "low" : "high",
  414. dev->irq);
  415. return_VALUE(0);
  416. }
  417. EXPORT_SYMBOL(acpi_pci_irq_enable);
  418. /* FIXME: implement x86/x86_64 version */
  419. void __attribute__((weak)) acpi_unregister_gsi(u32 i) {}
  420. void
  421. acpi_pci_irq_disable (
  422. struct pci_dev *dev)
  423. {
  424. int gsi = 0;
  425. u8 pin = 0;
  426. int edge_level = ACPI_LEVEL_SENSITIVE;
  427. int active_high_low = ACPI_ACTIVE_LOW;
  428. ACPI_FUNCTION_TRACE("acpi_pci_irq_disable");
  429. if (!dev)
  430. return_VOID;
  431. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  432. if (!pin)
  433. return_VOID;
  434. pin--;
  435. if (!dev->bus)
  436. return_VOID;
  437. /*
  438. * First we check the PCI IRQ routing table (PRT) for an IRQ.
  439. */
  440. gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
  441. &edge_level, &active_high_low, NULL, acpi_pci_free_irq);
  442. /*
  443. * If no PRT entry was found, we'll try to derive an IRQ from the
  444. * device's parent bridge.
  445. */
  446. if (gsi < 0)
  447. gsi = acpi_pci_irq_derive(dev, pin,
  448. &edge_level, &active_high_low, NULL, acpi_pci_free_irq);
  449. if (gsi < 0)
  450. return_VOID;
  451. /*
  452. * TBD: It might be worth clearing dev->irq by magic constant
  453. * (e.g. PCI_UNDEFINED_IRQ).
  454. */
  455. printk(KERN_INFO PREFIX "PCI interrupt for device %s disabled\n",
  456. pci_name(dev));
  457. acpi_unregister_gsi(gsi);
  458. return_VOID;
  459. }