pci_irq.c 14 KB

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