pci_irq.c 13 KB

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