pci_link.c 24 KB

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