pci_irq.c 13 KB

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