pci_irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 *edge_level, int *active_high_low, 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, edge_level,
  227. active_high_low, 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. *edge_level = ACPI_LEVEL_SENSITIVE;
  236. *active_high_low = 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 *edge_level, int *active_high_low, 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 *edge_level,
  264. int *active_high_low, 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, edge_level, active_high_low, 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 *edge_level,
  291. int *active_high_low, 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. pci_read_config_byte(bridge, PCI_INTERRUPT_PIN,
  309. &bridge_pin);
  310. if (!bridge_pin) {
  311. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  312. "No interrupt pin configured for device %s\n",
  313. pci_name(bridge)));
  314. return_VALUE(-1);
  315. }
  316. /* Pin is from 0 to 3 */
  317. bridge_pin--;
  318. pin = bridge_pin;
  319. }
  320. irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
  321. pin, edge_level, active_high_low,
  322. link, func);
  323. }
  324. if (irq < 0) {
  325. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  326. "Unable to derive IRQ for device %s\n",
  327. pci_name(dev)));
  328. return_VALUE(-1);
  329. }
  330. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
  331. irq, pci_name(dev), pci_name(bridge)));
  332. return_VALUE(irq);
  333. }
  334. /*
  335. * acpi_pci_irq_enable
  336. * success: return 0
  337. * failure: return < 0
  338. */
  339. int acpi_pci_irq_enable(struct pci_dev *dev)
  340. {
  341. int irq = 0;
  342. u8 pin = 0;
  343. int edge_level = ACPI_LEVEL_SENSITIVE;
  344. int active_high_low = ACPI_ACTIVE_LOW;
  345. char *link = NULL;
  346. int rc;
  347. ACPI_FUNCTION_TRACE("acpi_pci_irq_enable");
  348. if (!dev)
  349. return_VALUE(-EINVAL);
  350. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  351. if (!pin) {
  352. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  353. "No interrupt pin configured for device %s\n",
  354. pci_name(dev)));
  355. return_VALUE(0);
  356. }
  357. pin--;
  358. if (!dev->bus) {
  359. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  360. "Invalid (NULL) 'bus' field\n"));
  361. return_VALUE(-ENODEV);
  362. }
  363. /*
  364. * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
  365. * values override any BIOS-assigned IRQs set during boot.
  366. */
  367. irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
  368. &edge_level, &active_high_low, &link,
  369. acpi_pci_allocate_irq);
  370. /*
  371. * If no PRT entry was found, we'll try to derive an IRQ from the
  372. * device's parent bridge.
  373. */
  374. if (irq < 0)
  375. irq = acpi_pci_irq_derive(dev, pin, &edge_level,
  376. &active_high_low, &link,
  377. acpi_pci_allocate_irq);
  378. /*
  379. * No IRQ known to the ACPI subsystem - maybe the BIOS /
  380. * driver reported one, then use it. Exit in any case.
  381. */
  382. if (irq < 0) {
  383. printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: no GSI",
  384. pci_name(dev), ('A' + pin));
  385. /* Interrupt Line values above 0xF are forbidden */
  386. if (dev->irq > 0 && (dev->irq <= 0xF)) {
  387. printk(" - using IRQ %d\n", dev->irq);
  388. acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
  389. ACPI_ACTIVE_LOW);
  390. return_VALUE(0);
  391. } else {
  392. printk("\n");
  393. return_VALUE(0);
  394. }
  395. }
  396. rc = acpi_register_gsi(irq, edge_level, active_high_low);
  397. if (rc < 0) {
  398. printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: failed "
  399. "to register GSI\n", pci_name(dev), ('A' + pin));
  400. return_VALUE(rc);
  401. }
  402. dev->irq = rc;
  403. printk(KERN_INFO PREFIX "PCI Interrupt %s[%c] -> ",
  404. pci_name(dev), 'A' + pin);
  405. if (link)
  406. printk("Link [%s] -> ", link);
  407. printk("GSI %u (%s, %s) -> IRQ %d\n", irq,
  408. (edge_level == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
  409. (active_high_low == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
  410. return_VALUE(0);
  411. }
  412. EXPORT_SYMBOL(acpi_pci_irq_enable);
  413. /* FIXME: implement x86/x86_64 version */
  414. void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
  415. {
  416. }
  417. void acpi_pci_irq_disable(struct pci_dev *dev)
  418. {
  419. int gsi = 0;
  420. u8 pin = 0;
  421. int edge_level = ACPI_LEVEL_SENSITIVE;
  422. int active_high_low = ACPI_ACTIVE_LOW;
  423. ACPI_FUNCTION_TRACE("acpi_pci_irq_disable");
  424. if (!dev || !dev->bus)
  425. return_VOID;
  426. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  427. if (!pin)
  428. return_VOID;
  429. pin--;
  430. /*
  431. * First we check the PCI IRQ routing table (PRT) for an IRQ.
  432. */
  433. gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
  434. &edge_level, &active_high_low, NULL,
  435. acpi_pci_free_irq);
  436. /*
  437. * If no PRT entry was found, we'll try to derive an IRQ from the
  438. * device's parent bridge.
  439. */
  440. if (gsi < 0)
  441. gsi = acpi_pci_irq_derive(dev, pin,
  442. &edge_level, &active_high_low, NULL,
  443. acpi_pci_free_irq);
  444. if (gsi < 0)
  445. return_VOID;
  446. /*
  447. * TBD: It might be worth clearing dev->irq by magic constant
  448. * (e.g. PCI_UNDEFINED_IRQ).
  449. */
  450. printk(KERN_INFO PREFIX "PCI interrupt for device %s disabled\n",
  451. pci_name(dev));
  452. acpi_unregister_gsi(gsi);
  453. return_VOID;
  454. }