pci_link.c 23 KB

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