pci_link.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * pci_link.c - ACPI PCI Interrupt Link Device Driver ($Revision: 34 $)
  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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. *
  22. * TBD:
  23. * 1. Support more than one IRQ resource entry per link device (index).
  24. * 2. Implement start/stop mechanism and use ACPI Bus Driver facilities
  25. * for IRQ management (e.g. start()->_SRS).
  26. */
  27. #include <linux/syscore_ops.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/types.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/pm.h>
  34. #include <linux/pci.h>
  35. #include <linux/mutex.h>
  36. #include <linux/slab.h>
  37. #include <linux/acpi.h>
  38. #include "internal.h"
  39. #define _COMPONENT ACPI_PCI_COMPONENT
  40. ACPI_MODULE_NAME("pci_link");
  41. #define ACPI_PCI_LINK_CLASS "pci_irq_routing"
  42. #define ACPI_PCI_LINK_DEVICE_NAME "PCI Interrupt Link"
  43. #define ACPI_PCI_LINK_FILE_INFO "info"
  44. #define ACPI_PCI_LINK_FILE_STATUS "state"
  45. #define ACPI_PCI_LINK_MAX_POSSIBLE 16
  46. static int acpi_pci_link_add(struct acpi_device *device,
  47. const struct acpi_device_id *not_used);
  48. static void acpi_pci_link_remove(struct acpi_device *device);
  49. static const struct acpi_device_id link_device_ids[] = {
  50. {"PNP0C0F", 0},
  51. {"", 0},
  52. };
  53. static struct acpi_scan_handler pci_link_handler = {
  54. .ids = link_device_ids,
  55. .attach = acpi_pci_link_add,
  56. .detach = acpi_pci_link_remove,
  57. };
  58. /*
  59. * If a link is initialized, we never change its active and initialized
  60. * later even the link is disable. Instead, we just repick the active irq
  61. */
  62. struct acpi_pci_link_irq {
  63. u8 active; /* Current IRQ */
  64. u8 triggering; /* All IRQs */
  65. u8 polarity; /* All IRQs */
  66. u8 resource_type;
  67. u8 possible_count;
  68. u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
  69. u8 initialized:1;
  70. u8 reserved:7;
  71. };
  72. struct acpi_pci_link {
  73. struct list_head list;
  74. struct acpi_device *device;
  75. struct acpi_pci_link_irq irq;
  76. int refcnt;
  77. };
  78. static LIST_HEAD(acpi_link_list);
  79. static DEFINE_MUTEX(acpi_link_lock);
  80. /* --------------------------------------------------------------------------
  81. PCI Link Device Management
  82. -------------------------------------------------------------------------- */
  83. /*
  84. * set context (link) possible list from resource list
  85. */
  86. static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
  87. void *context)
  88. {
  89. struct acpi_pci_link *link = context;
  90. u32 i;
  91. switch (resource->type) {
  92. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  93. case ACPI_RESOURCE_TYPE_END_TAG:
  94. return AE_OK;
  95. case ACPI_RESOURCE_TYPE_IRQ:
  96. {
  97. struct acpi_resource_irq *p = &resource->data.irq;
  98. if (!p || !p->interrupt_count) {
  99. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  100. "Blank _PRS IRQ resource\n"));
  101. return AE_OK;
  102. }
  103. for (i = 0;
  104. (i < p->interrupt_count
  105. && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
  106. if (!p->interrupts[i]) {
  107. printk(KERN_WARNING PREFIX
  108. "Invalid _PRS IRQ %d\n",
  109. p->interrupts[i]);
  110. continue;
  111. }
  112. link->irq.possible[i] = p->interrupts[i];
  113. link->irq.possible_count++;
  114. }
  115. link->irq.triggering = p->triggering;
  116. link->irq.polarity = p->polarity;
  117. link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
  118. break;
  119. }
  120. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  121. {
  122. struct acpi_resource_extended_irq *p =
  123. &resource->data.extended_irq;
  124. if (!p || !p->interrupt_count) {
  125. printk(KERN_WARNING PREFIX
  126. "Blank _PRS EXT IRQ resource\n");
  127. return AE_OK;
  128. }
  129. for (i = 0;
  130. (i < p->interrupt_count
  131. && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
  132. if (!p->interrupts[i]) {
  133. printk(KERN_WARNING PREFIX
  134. "Invalid _PRS IRQ %d\n",
  135. p->interrupts[i]);
  136. continue;
  137. }
  138. link->irq.possible[i] = p->interrupts[i];
  139. link->irq.possible_count++;
  140. }
  141. link->irq.triggering = p->triggering;
  142. link->irq.polarity = p->polarity;
  143. link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
  144. break;
  145. }
  146. default:
  147. printk(KERN_ERR PREFIX "_PRS resource type 0x%x isn't an IRQ\n",
  148. resource->type);
  149. return AE_OK;
  150. }
  151. return AE_CTRL_TERMINATE;
  152. }
  153. static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
  154. {
  155. acpi_status status;
  156. status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
  157. acpi_pci_link_check_possible, link);
  158. if (ACPI_FAILURE(status)) {
  159. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
  160. return -ENODEV;
  161. }
  162. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  163. "Found %d possible IRQs\n",
  164. link->irq.possible_count));
  165. return 0;
  166. }
  167. static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
  168. void *context)
  169. {
  170. int *irq = context;
  171. switch (resource->type) {
  172. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  173. case ACPI_RESOURCE_TYPE_END_TAG:
  174. return AE_OK;
  175. case ACPI_RESOURCE_TYPE_IRQ:
  176. {
  177. struct acpi_resource_irq *p = &resource->data.irq;
  178. if (!p || !p->interrupt_count) {
  179. /*
  180. * IRQ descriptors may have no IRQ# bits set,
  181. * particularly those those w/ _STA disabled
  182. */
  183. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  184. "Blank _CRS IRQ resource\n"));
  185. return AE_OK;
  186. }
  187. *irq = p->interrupts[0];
  188. break;
  189. }
  190. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  191. {
  192. struct acpi_resource_extended_irq *p =
  193. &resource->data.extended_irq;
  194. if (!p || !p->interrupt_count) {
  195. /*
  196. * extended IRQ descriptors must
  197. * return at least 1 IRQ
  198. */
  199. printk(KERN_WARNING PREFIX
  200. "Blank _CRS EXT IRQ resource\n");
  201. return AE_OK;
  202. }
  203. *irq = p->interrupts[0];
  204. break;
  205. }
  206. break;
  207. default:
  208. printk(KERN_ERR PREFIX "_CRS resource type 0x%x isn't an IRQ\n",
  209. resource->type);
  210. return AE_OK;
  211. }
  212. return AE_CTRL_TERMINATE;
  213. }
  214. /*
  215. * Run _CRS and set link->irq.active
  216. *
  217. * return value:
  218. * 0 - success
  219. * !0 - failure
  220. */
  221. static int acpi_pci_link_get_current(struct acpi_pci_link *link)
  222. {
  223. int result = 0;
  224. acpi_status status;
  225. int irq = 0;
  226. link->irq.active = 0;
  227. /* in practice, status disabled is meaningless, ignore it */
  228. if (acpi_strict) {
  229. /* Query _STA, set link->device->status */
  230. result = acpi_bus_get_status(link->device);
  231. if (result) {
  232. printk(KERN_ERR PREFIX "Unable to read status\n");
  233. goto end;
  234. }
  235. if (!link->device->status.enabled) {
  236. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
  237. return 0;
  238. }
  239. }
  240. /*
  241. * Query and parse _CRS to get the current IRQ assignment.
  242. */
  243. status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
  244. acpi_pci_link_check_current, &irq);
  245. if (ACPI_FAILURE(status)) {
  246. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
  247. result = -ENODEV;
  248. goto end;
  249. }
  250. if (acpi_strict && !irq) {
  251. printk(KERN_ERR PREFIX "_CRS returned 0\n");
  252. result = -ENODEV;
  253. }
  254. link->irq.active = irq;
  255. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
  256. end:
  257. return result;
  258. }
  259. static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
  260. {
  261. int result;
  262. acpi_status status;
  263. struct {
  264. struct acpi_resource res;
  265. struct acpi_resource end;
  266. } *resource;
  267. struct acpi_buffer buffer = { 0, NULL };
  268. if (!irq)
  269. return -EINVAL;
  270. resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
  271. if (!resource)
  272. return -ENOMEM;
  273. buffer.length = sizeof(*resource) + 1;
  274. buffer.pointer = resource;
  275. switch (link->irq.resource_type) {
  276. case ACPI_RESOURCE_TYPE_IRQ:
  277. resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
  278. resource->res.length = sizeof(struct acpi_resource);
  279. resource->res.data.irq.triggering = link->irq.triggering;
  280. resource->res.data.irq.polarity =
  281. link->irq.polarity;
  282. if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
  283. resource->res.data.irq.sharable =
  284. ACPI_EXCLUSIVE;
  285. else
  286. resource->res.data.irq.sharable = ACPI_SHARED;
  287. resource->res.data.irq.interrupt_count = 1;
  288. resource->res.data.irq.interrupts[0] = irq;
  289. break;
  290. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  291. resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
  292. resource->res.length = sizeof(struct acpi_resource);
  293. resource->res.data.extended_irq.producer_consumer =
  294. ACPI_CONSUMER;
  295. resource->res.data.extended_irq.triggering =
  296. link->irq.triggering;
  297. resource->res.data.extended_irq.polarity =
  298. link->irq.polarity;
  299. if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
  300. resource->res.data.irq.sharable =
  301. ACPI_EXCLUSIVE;
  302. else
  303. resource->res.data.irq.sharable = ACPI_SHARED;
  304. resource->res.data.extended_irq.interrupt_count = 1;
  305. resource->res.data.extended_irq.interrupts[0] = irq;
  306. /* ignore resource_source, it's optional */
  307. break;
  308. default:
  309. printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
  310. result = -EINVAL;
  311. goto end;
  312. }
  313. resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
  314. resource->end.length = sizeof(struct acpi_resource);
  315. /* Attempt to set the resource */
  316. status = acpi_set_current_resources(link->device->handle, &buffer);
  317. /* check for total failure */
  318. if (ACPI_FAILURE(status)) {
  319. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
  320. result = -ENODEV;
  321. goto end;
  322. }
  323. /* Query _STA, set device->status */
  324. result = acpi_bus_get_status(link->device);
  325. if (result) {
  326. printk(KERN_ERR PREFIX "Unable to read status\n");
  327. goto end;
  328. }
  329. if (!link->device->status.enabled) {
  330. printk(KERN_WARNING PREFIX
  331. "%s [%s] disabled and referenced, BIOS bug\n",
  332. acpi_device_name(link->device),
  333. acpi_device_bid(link->device));
  334. }
  335. /* Query _CRS, set link->irq.active */
  336. result = acpi_pci_link_get_current(link);
  337. if (result) {
  338. goto end;
  339. }
  340. /*
  341. * Is current setting not what we set?
  342. * set link->irq.active
  343. */
  344. if (link->irq.active != irq) {
  345. /*
  346. * policy: when _CRS doesn't return what we just _SRS
  347. * assume _SRS worked and override _CRS value.
  348. */
  349. printk(KERN_WARNING PREFIX
  350. "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
  351. acpi_device_name(link->device),
  352. acpi_device_bid(link->device), link->irq.active, irq);
  353. link->irq.active = irq;
  354. }
  355. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
  356. end:
  357. kfree(resource);
  358. return result;
  359. }
  360. /* --------------------------------------------------------------------------
  361. PCI Link IRQ Management
  362. -------------------------------------------------------------------------- */
  363. /*
  364. * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
  365. * Link Devices to move the PIRQs around to minimize sharing.
  366. *
  367. * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
  368. * that the BIOS has already set to active. This is necessary because
  369. * ACPI has no automatic means of knowing what ISA IRQs are used. Note that
  370. * if the BIOS doesn't set a Link Device active, ACPI needs to program it
  371. * even if acpi_irq_nobalance is set.
  372. *
  373. * A tables of penalties avoids directing PCI interrupts to well known
  374. * ISA IRQs. Boot params are available to over-ride the default table:
  375. *
  376. * List interrupts that are free for PCI use.
  377. * acpi_irq_pci=n[,m]
  378. *
  379. * List interrupts that should not be used for PCI:
  380. * acpi_irq_isa=n[,m]
  381. *
  382. * Note that PCI IRQ routers have a list of possible IRQs,
  383. * which may not include the IRQs this table says are available.
  384. *
  385. * Since this heuristic can't tell the difference between a link
  386. * that no device will attach to, vs. a link which may be shared
  387. * by multiple active devices -- it is not optimal.
  388. *
  389. * If interrupt performance is that important, get an IO-APIC system
  390. * with a pin dedicated to each device. Or for that matter, an MSI
  391. * enabled system.
  392. */
  393. #define ACPI_MAX_IRQS 256
  394. #define ACPI_MAX_ISA_IRQ 16
  395. #define PIRQ_PENALTY_PCI_AVAILABLE (0)
  396. #define PIRQ_PENALTY_PCI_POSSIBLE (16*16)
  397. #define PIRQ_PENALTY_PCI_USING (16*16*16)
  398. #define PIRQ_PENALTY_ISA_TYPICAL (16*16*16*16)
  399. #define PIRQ_PENALTY_ISA_USED (16*16*16*16*16)
  400. #define PIRQ_PENALTY_ISA_ALWAYS (16*16*16*16*16*16)
  401. static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
  402. PIRQ_PENALTY_ISA_ALWAYS, /* IRQ0 timer */
  403. PIRQ_PENALTY_ISA_ALWAYS, /* IRQ1 keyboard */
  404. PIRQ_PENALTY_ISA_ALWAYS, /* IRQ2 cascade */
  405. PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
  406. PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
  407. PIRQ_PENALTY_ISA_TYPICAL, /* IRQ5 sometimes SoundBlaster */
  408. PIRQ_PENALTY_ISA_TYPICAL, /* IRQ6 */
  409. PIRQ_PENALTY_ISA_TYPICAL, /* IRQ7 parallel, spurious */
  410. PIRQ_PENALTY_ISA_TYPICAL, /* IRQ8 rtc, sometimes */
  411. PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ9 PCI, often acpi */
  412. PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ10 PCI */
  413. PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ11 PCI */
  414. PIRQ_PENALTY_ISA_USED, /* IRQ12 mouse */
  415. PIRQ_PENALTY_ISA_USED, /* IRQ13 fpe, sometimes */
  416. PIRQ_PENALTY_ISA_USED, /* IRQ14 ide0 */
  417. PIRQ_PENALTY_ISA_USED, /* IRQ15 ide1 */
  418. /* >IRQ15 */
  419. };
  420. int __init acpi_irq_penalty_init(void)
  421. {
  422. struct acpi_pci_link *link;
  423. int i;
  424. /*
  425. * Update penalties to facilitate IRQ balancing.
  426. */
  427. list_for_each_entry(link, &acpi_link_list, list) {
  428. /*
  429. * reflect the possible and active irqs in the penalty table --
  430. * useful for breaking ties.
  431. */
  432. if (link->irq.possible_count) {
  433. int penalty =
  434. PIRQ_PENALTY_PCI_POSSIBLE /
  435. link->irq.possible_count;
  436. for (i = 0; i < link->irq.possible_count; i++) {
  437. if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
  438. acpi_irq_penalty[link->irq.
  439. possible[i]] +=
  440. penalty;
  441. }
  442. } else if (link->irq.active) {
  443. acpi_irq_penalty[link->irq.active] +=
  444. PIRQ_PENALTY_PCI_POSSIBLE;
  445. }
  446. }
  447. return 0;
  448. }
  449. static int acpi_irq_balance = -1; /* 0: static, 1: balance */
  450. static int acpi_pci_link_allocate(struct acpi_pci_link *link)
  451. {
  452. int irq;
  453. int i;
  454. if (link->irq.initialized) {
  455. if (link->refcnt == 0)
  456. /* This means the link is disabled but initialized */
  457. acpi_pci_link_set(link, link->irq.active);
  458. return 0;
  459. }
  460. /*
  461. * search for active IRQ in list of possible IRQs.
  462. */
  463. for (i = 0; i < link->irq.possible_count; ++i) {
  464. if (link->irq.active == link->irq.possible[i])
  465. break;
  466. }
  467. /*
  468. * forget active IRQ that is not in possible list
  469. */
  470. if (i == link->irq.possible_count) {
  471. if (acpi_strict)
  472. printk(KERN_WARNING PREFIX "_CRS %d not found"
  473. " in _PRS\n", link->irq.active);
  474. link->irq.active = 0;
  475. }
  476. /*
  477. * if active found, use it; else pick entry from end of possible list.
  478. */
  479. if (link->irq.active)
  480. irq = link->irq.active;
  481. else
  482. irq = link->irq.possible[link->irq.possible_count - 1];
  483. if (acpi_irq_balance || !link->irq.active) {
  484. /*
  485. * Select the best IRQ. This is done in reverse to promote
  486. * the use of IRQs 9, 10, 11, and >15.
  487. */
  488. for (i = (link->irq.possible_count - 1); i >= 0; i--) {
  489. if (acpi_irq_penalty[irq] >
  490. acpi_irq_penalty[link->irq.possible[i]])
  491. irq = link->irq.possible[i];
  492. }
  493. }
  494. if (acpi_irq_penalty[irq] >= PIRQ_PENALTY_ISA_ALWAYS) {
  495. printk(KERN_ERR PREFIX "No IRQ available for %s [%s]. "
  496. "Try pci=noacpi or acpi=off\n",
  497. acpi_device_name(link->device),
  498. acpi_device_bid(link->device));
  499. return -ENODEV;
  500. }
  501. /* Attempt to enable the link device at this IRQ. */
  502. if (acpi_pci_link_set(link, irq)) {
  503. printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
  504. "Try pci=noacpi or acpi=off\n",
  505. acpi_device_name(link->device),
  506. acpi_device_bid(link->device));
  507. return -ENODEV;
  508. } else {
  509. acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
  510. printk(KERN_WARNING PREFIX "%s [%s] enabled at IRQ %d\n",
  511. acpi_device_name(link->device),
  512. acpi_device_bid(link->device), link->irq.active);
  513. }
  514. link->irq.initialized = 1;
  515. return 0;
  516. }
  517. /*
  518. * acpi_pci_link_allocate_irq
  519. * success: return IRQ >= 0
  520. * failure: return -1
  521. */
  522. int acpi_pci_link_allocate_irq(acpi_handle handle, int index, int *triggering,
  523. int *polarity, char **name)
  524. {
  525. int result;
  526. struct acpi_device *device;
  527. struct acpi_pci_link *link;
  528. result = acpi_bus_get_device(handle, &device);
  529. if (result) {
  530. printk(KERN_ERR PREFIX "Invalid link device\n");
  531. return -1;
  532. }
  533. link = acpi_driver_data(device);
  534. if (!link) {
  535. printk(KERN_ERR PREFIX "Invalid link context\n");
  536. return -1;
  537. }
  538. /* TBD: Support multiple index (IRQ) entries per Link Device */
  539. if (index) {
  540. printk(KERN_ERR PREFIX "Invalid index %d\n", index);
  541. return -1;
  542. }
  543. mutex_lock(&acpi_link_lock);
  544. if (acpi_pci_link_allocate(link)) {
  545. mutex_unlock(&acpi_link_lock);
  546. return -1;
  547. }
  548. if (!link->irq.active) {
  549. mutex_unlock(&acpi_link_lock);
  550. printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
  551. return -1;
  552. }
  553. link->refcnt++;
  554. mutex_unlock(&acpi_link_lock);
  555. if (triggering)
  556. *triggering = link->irq.triggering;
  557. if (polarity)
  558. *polarity = link->irq.polarity;
  559. if (name)
  560. *name = acpi_device_bid(link->device);
  561. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  562. "Link %s is referenced\n",
  563. acpi_device_bid(link->device)));
  564. return (link->irq.active);
  565. }
  566. /*
  567. * We don't change link's irq information here. After it is reenabled, we
  568. * continue use the info
  569. */
  570. int acpi_pci_link_free_irq(acpi_handle handle)
  571. {
  572. struct acpi_device *device;
  573. struct acpi_pci_link *link;
  574. acpi_status result;
  575. result = acpi_bus_get_device(handle, &device);
  576. if (result) {
  577. printk(KERN_ERR PREFIX "Invalid link device\n");
  578. return -1;
  579. }
  580. link = acpi_driver_data(device);
  581. if (!link) {
  582. printk(KERN_ERR PREFIX "Invalid link context\n");
  583. return -1;
  584. }
  585. mutex_lock(&acpi_link_lock);
  586. if (!link->irq.initialized) {
  587. mutex_unlock(&acpi_link_lock);
  588. printk(KERN_ERR PREFIX "Link isn't initialized\n");
  589. return -1;
  590. }
  591. #ifdef FUTURE_USE
  592. /*
  593. * The Link reference count allows us to _DISable an unused link
  594. * and suspend time, and set it again on resume.
  595. * However, 2.6.12 still has irq_router.resume
  596. * which blindly restores the link state.
  597. * So we disable the reference count method
  598. * to prevent duplicate acpi_pci_link_set()
  599. * which would harm some systems
  600. */
  601. link->refcnt--;
  602. #endif
  603. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  604. "Link %s is dereferenced\n",
  605. acpi_device_bid(link->device)));
  606. if (link->refcnt == 0)
  607. acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
  608. mutex_unlock(&acpi_link_lock);
  609. return (link->irq.active);
  610. }
  611. /* --------------------------------------------------------------------------
  612. Driver Interface
  613. -------------------------------------------------------------------------- */
  614. static int acpi_pci_link_add(struct acpi_device *device,
  615. const struct acpi_device_id *not_used)
  616. {
  617. int result;
  618. struct acpi_pci_link *link;
  619. int i;
  620. int found = 0;
  621. link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
  622. if (!link)
  623. return -ENOMEM;
  624. link->device = device;
  625. strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
  626. strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
  627. device->driver_data = link;
  628. mutex_lock(&acpi_link_lock);
  629. result = acpi_pci_link_get_possible(link);
  630. if (result)
  631. goto end;
  632. /* query and set link->irq.active */
  633. acpi_pci_link_get_current(link);
  634. printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
  635. acpi_device_bid(device));
  636. for (i = 0; i < link->irq.possible_count; i++) {
  637. if (link->irq.active == link->irq.possible[i]) {
  638. printk(KERN_CONT " *%d", link->irq.possible[i]);
  639. found = 1;
  640. } else
  641. printk(KERN_CONT " %d", link->irq.possible[i]);
  642. }
  643. printk(KERN_CONT ")");
  644. if (!found)
  645. printk(KERN_CONT " *%d", link->irq.active);
  646. if (!link->device->status.enabled)
  647. printk(KERN_CONT ", disabled.");
  648. printk(KERN_CONT "\n");
  649. list_add_tail(&link->list, &acpi_link_list);
  650. end:
  651. /* disable all links -- to be activated on use */
  652. acpi_evaluate_object(device->handle, "_DIS", NULL, NULL);
  653. mutex_unlock(&acpi_link_lock);
  654. if (result)
  655. kfree(link);
  656. return result < 0 ? result : 1;
  657. }
  658. static int acpi_pci_link_resume(struct acpi_pci_link *link)
  659. {
  660. if (link->refcnt && link->irq.active && link->irq.initialized)
  661. return (acpi_pci_link_set(link, link->irq.active));
  662. return 0;
  663. }
  664. static void irqrouter_resume(void)
  665. {
  666. struct acpi_pci_link *link;
  667. list_for_each_entry(link, &acpi_link_list, list) {
  668. acpi_pci_link_resume(link);
  669. }
  670. }
  671. static void acpi_pci_link_remove(struct acpi_device *device)
  672. {
  673. struct acpi_pci_link *link;
  674. link = acpi_driver_data(device);
  675. mutex_lock(&acpi_link_lock);
  676. list_del(&link->list);
  677. mutex_unlock(&acpi_link_lock);
  678. kfree(link);
  679. }
  680. /*
  681. * modify acpi_irq_penalty[] from cmdline
  682. */
  683. static int __init acpi_irq_penalty_update(char *str, int used)
  684. {
  685. int i;
  686. for (i = 0; i < 16; i++) {
  687. int retval;
  688. int irq;
  689. retval = get_option(&str, &irq);
  690. if (!retval)
  691. break; /* no number found */
  692. if (irq < 0)
  693. continue;
  694. if (irq >= ARRAY_SIZE(acpi_irq_penalty))
  695. continue;
  696. if (used)
  697. acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
  698. else
  699. acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
  700. if (retval != 2) /* no next number */
  701. break;
  702. }
  703. return 1;
  704. }
  705. /*
  706. * We'd like PNP to call this routine for the
  707. * single ISA_USED value for each legacy device.
  708. * But instead it calls us with each POSSIBLE setting.
  709. * There is no ISA_POSSIBLE weight, so we simply use
  710. * the (small) PCI_USING penalty.
  711. */
  712. void acpi_penalize_isa_irq(int irq, int active)
  713. {
  714. if (irq >= 0 && irq < ARRAY_SIZE(acpi_irq_penalty)) {
  715. if (active)
  716. acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
  717. else
  718. acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
  719. }
  720. }
  721. bool acpi_isa_irq_available(int irq)
  722. {
  723. return irq >= 0 && (irq >= ARRAY_SIZE(acpi_irq_penalty) ||
  724. acpi_irq_penalty[irq] < PIRQ_PENALTY_ISA_ALWAYS);
  725. }
  726. /*
  727. * Penalize IRQ used by ACPI SCI. If ACPI SCI pin attributes conflict with
  728. * PCI IRQ attributes, mark ACPI SCI as ISA_ALWAYS so it won't be use for
  729. * PCI IRQs.
  730. */
  731. void acpi_penalize_sci_irq(int irq, int trigger, int polarity)
  732. {
  733. if (irq >= 0 && irq < ARRAY_SIZE(acpi_irq_penalty)) {
  734. if (trigger != ACPI_MADT_TRIGGER_LEVEL ||
  735. polarity != ACPI_MADT_POLARITY_ACTIVE_LOW)
  736. acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_ALWAYS;
  737. else
  738. acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
  739. }
  740. }
  741. /*
  742. * Over-ride default table to reserve additional IRQs for use by ISA
  743. * e.g. acpi_irq_isa=5
  744. * Useful for telling ACPI how not to interfere with your ISA sound card.
  745. */
  746. static int __init acpi_irq_isa(char *str)
  747. {
  748. return acpi_irq_penalty_update(str, 1);
  749. }
  750. __setup("acpi_irq_isa=", acpi_irq_isa);
  751. /*
  752. * Over-ride default table to free additional IRQs for use by PCI
  753. * e.g. acpi_irq_pci=7,15
  754. * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
  755. */
  756. static int __init acpi_irq_pci(char *str)
  757. {
  758. return acpi_irq_penalty_update(str, 0);
  759. }
  760. __setup("acpi_irq_pci=", acpi_irq_pci);
  761. static int __init acpi_irq_nobalance_set(char *str)
  762. {
  763. acpi_irq_balance = 0;
  764. return 1;
  765. }
  766. __setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
  767. static int __init acpi_irq_balance_set(char *str)
  768. {
  769. acpi_irq_balance = 1;
  770. return 1;
  771. }
  772. __setup("acpi_irq_balance", acpi_irq_balance_set);
  773. static struct syscore_ops irqrouter_syscore_ops = {
  774. .resume = irqrouter_resume,
  775. };
  776. void __init acpi_pci_link_init(void)
  777. {
  778. if (acpi_noirq)
  779. return;
  780. if (acpi_irq_balance == -1) {
  781. /* no command line switch: enable balancing in IOAPIC mode */
  782. if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
  783. acpi_irq_balance = 1;
  784. else
  785. acpi_irq_balance = 0;
  786. }
  787. register_syscore_ops(&irqrouter_syscore_ops);
  788. acpi_scan_add_handler(&pci_link_handler);
  789. }