blob: b59f59efb72f58d500c967f065e5865a8625f4f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
32#include <linux/sysdev.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/types.h>
37#include <linux/proc_fs.h>
38#include <linux/spinlock.h>
39#include <linux/pm.h>
40#include <linux/pci.h>
Ingo Molnar36e43092006-04-27 05:25:00 -040041#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
45
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070046#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050047ACPI_MODULE_NAME("pci_link");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_PCI_LINK_CLASS "pci_irq_routing"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ACPI_PCI_LINK_DEVICE_NAME "PCI Interrupt Link"
50#define ACPI_PCI_LINK_FILE_INFO "info"
51#define ACPI_PCI_LINK_FILE_STATUS "state"
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070052#define ACPI_PCI_LINK_MAX_POSSIBLE 16
53
Len Brown4be44fc2005-08-05 00:44:28 -040054static int acpi_pci_link_add(struct acpi_device *device);
55static int acpi_pci_link_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Thomas Renninger1ba90e32007-07-23 14:44:41 +020057static struct acpi_device_id link_device_ids[] = {
58 {"PNP0C0F", 0},
59 {"", 0},
60};
61MODULE_DEVICE_TABLE(acpi, link_device_ids);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static struct acpi_driver acpi_pci_link_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050064 .name = "pci_link",
Len Brown4be44fc2005-08-05 00:44:28 -040065 .class = ACPI_PCI_LINK_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020066 .ids = link_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040067 .ops = {
68 .add = acpi_pci_link_add,
69 .remove = acpi_pci_link_remove,
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070070 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
David Shaohua Li87bec662005-07-27 23:02:00 -040073/*
74 * If a link is initialized, we never change its active and initialized
75 * later even the link is disable. Instead, we just repick the active irq
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077struct acpi_pci_link_irq {
Len Brown4be44fc2005-08-05 00:44:28 -040078 u8 active; /* Current IRQ */
Bob Moore50eca3e2005-09-30 19:03:00 -040079 u8 triggering; /* All IRQs */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070080 u8 polarity; /* All IRQs */
Len Brown4be44fc2005-08-05 00:44:28 -040081 u8 resource_type;
82 u8 possible_count;
83 u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
84 u8 initialized:1;
85 u8 reserved:7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
88struct acpi_pci_link {
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070089 struct list_head node;
90 struct acpi_device *device;
91 struct acpi_pci_link_irq irq;
92 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95static struct {
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070096 int count;
97 struct list_head entries;
Len Brown4be44fc2005-08-05 00:44:28 -040098} acpi_link;
Adrian Bunke5685b92007-10-24 18:24:42 +020099static DEFINE_MUTEX(acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* --------------------------------------------------------------------------
102 PCI Link Device Management
103 -------------------------------------------------------------------------- */
104
105/*
106 * set context (link) possible list from resource list
107 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700108static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
109 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200111 struct acpi_pci_link *link = context;
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700112 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Len Browneca008c2005-09-22 00:25:18 -0400114 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400115 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600116 case ACPI_RESOURCE_TYPE_END_TAG:
Patrick Mocheld550d982006-06-27 00:41:40 -0400117 return AE_OK;
Bob Moore50eca3e2005-09-30 19:03:00 -0400118 case ACPI_RESOURCE_TYPE_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400119 {
120 struct acpi_resource_irq *p = &resource->data.irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400121 if (!p || !p->interrupt_count) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600122 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
123 "Blank _PRS IRQ resource\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400124 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Len Brown4be44fc2005-08-05 00:44:28 -0400126 for (i = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400127 (i < p->interrupt_count
Len Brown4be44fc2005-08-05 00:44:28 -0400128 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
129 if (!p->interrupts[i]) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600130 printk(KERN_WARNING PREFIX
131 "Invalid _PRS IRQ %d\n",
132 p->interrupts[i]);
Len Brown4be44fc2005-08-05 00:44:28 -0400133 continue;
134 }
135 link->irq.possible[i] = p->interrupts[i];
136 link->irq.possible_count++;
137 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400138 link->irq.triggering = p->triggering;
139 link->irq.polarity = p->polarity;
140 link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
Len Brown4be44fc2005-08-05 00:44:28 -0400141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400143 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400144 {
Bob Moore50eca3e2005-09-30 19:03:00 -0400145 struct acpi_resource_extended_irq *p =
Len Brown4be44fc2005-08-05 00:44:28 -0400146 &resource->data.extended_irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400147 if (!p || !p->interrupt_count) {
Len Browncece9292006-06-26 23:04:31 -0400148 printk(KERN_WARNING PREFIX
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600149 "Blank _PRS EXT IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400150 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Len Brown4be44fc2005-08-05 00:44:28 -0400152 for (i = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400153 (i < p->interrupt_count
Len Brown4be44fc2005-08-05 00:44:28 -0400154 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
155 if (!p->interrupts[i]) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600156 printk(KERN_WARNING PREFIX
157 "Invalid _PRS IRQ %d\n",
158 p->interrupts[i]);
Len Brown4be44fc2005-08-05 00:44:28 -0400159 continue;
160 }
161 link->irq.possible[i] = p->interrupts[i];
162 link->irq.possible_count++;
163 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400164 link->irq.triggering = p->triggering;
165 link->irq.polarity = p->polarity;
166 link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Len Brown4be44fc2005-08-05 00:44:28 -0400167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 default:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600170 printk(KERN_ERR PREFIX "_PRS resource type 0x%x isn't an IRQ\n",
171 resource->type);
Patrick Mocheld550d982006-06-27 00:41:40 -0400172 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
Patrick Mocheld550d982006-06-27 00:41:40 -0400175 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Len Brown4be44fc2005-08-05 00:44:28 -0400178static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Len Brown4be44fc2005-08-05 00:44:28 -0400180 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (!link)
Patrick Mocheld550d982006-06-27 00:41:40 -0400183 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Patrick Mochel67a71362006-05-19 16:54:42 -0400185 status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
Len Brown4be44fc2005-08-05 00:44:28 -0400186 acpi_pci_link_check_possible, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400188 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
Len Brown4be44fc2005-08-05 00:44:28 -0400192 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
193 "Found %d possible IRQs\n",
194 link->irq.possible_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Patrick Mocheld550d982006-06-27 00:41:40 -0400196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700199static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
200 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700202 int *irq = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Len Browneca008c2005-09-22 00:25:18 -0400204 switch (resource->type) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600205 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
206 case ACPI_RESOURCE_TYPE_END_TAG:
207 return AE_OK;
Bob Moore50eca3e2005-09-30 19:03:00 -0400208 case ACPI_RESOURCE_TYPE_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400209 {
210 struct acpi_resource_irq *p = &resource->data.irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400211 if (!p || !p->interrupt_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400212 /*
213 * IRQ descriptors may have no IRQ# bits set,
214 * particularly those those w/ _STA disabled
215 */
216 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600217 "Blank _CRS IRQ resource\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400218 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400219 }
220 *irq = p->interrupts[0];
221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400223 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400224 {
Bob Moore50eca3e2005-09-30 19:03:00 -0400225 struct acpi_resource_extended_irq *p =
Len Brown4be44fc2005-08-05 00:44:28 -0400226 &resource->data.extended_irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400227 if (!p || !p->interrupt_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400228 /*
229 * extended IRQ descriptors must
230 * return at least 1 IRQ
231 */
Len Browncece9292006-06-26 23:04:31 -0400232 printk(KERN_WARNING PREFIX
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600233 "Blank _CRS EXT IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400235 }
236 *irq = p->interrupts[0];
237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Len Brownd4ec6c72006-01-26 17:23:38 -0500239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 default:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600241 printk(KERN_ERR PREFIX "_CRS resource type 0x%x isn't an IRQ\n",
242 resource->type);
Patrick Mocheld550d982006-06-27 00:41:40 -0400243 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600245
Patrick Mocheld550d982006-06-27 00:41:40 -0400246 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/*
250 * Run _CRS and set link->irq.active
251 *
252 * return value:
253 * 0 - success
254 * !0 - failure
255 */
Len Brown4be44fc2005-08-05 00:44:28 -0400256static int acpi_pci_link_get_current(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Len Brown4be44fc2005-08-05 00:44:28 -0400258 int result = 0;
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700259 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400260 int irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Patrick Mochele0e4e112006-05-19 16:54:50 -0400262 if (!link)
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 link->irq.active = 0;
266
267 /* in practice, status disabled is meaningless, ignore it */
268 if (acpi_strict) {
269 /* Query _STA, set link->device->status */
270 result = acpi_bus_get_status(link->device);
271 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400272 printk(KERN_ERR PREFIX "Unable to read status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto end;
274 }
275
276 if (!link->device->status.enabled) {
277 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 }
281
282 /*
283 * Query and parse _CRS to get the current IRQ assignment.
284 */
285
Patrick Mochel67a71362006-05-19 16:54:42 -0400286 status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
Len Brown4be44fc2005-08-05 00:44:28 -0400287 acpi_pci_link_check_current, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400289 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 result = -ENODEV;
291 goto end;
292 }
293
294 if (acpi_strict && !irq) {
Len Brown64684632006-06-26 23:41:38 -0400295 printk(KERN_ERR PREFIX "_CRS returned 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 result = -ENODEV;
297 }
298
299 link->irq.active = irq;
300
301 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
302
Len Brown4be44fc2005-08-05 00:44:28 -0400303 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400304 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Len Brown4be44fc2005-08-05 00:44:28 -0400307static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700309 int result;
310 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct {
Len Brown4be44fc2005-08-05 00:44:28 -0400312 struct acpi_resource res;
313 struct acpi_resource end;
314 } *resource;
315 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!link || !irq)
Patrick Mocheld550d982006-06-27 00:41:40 -0400318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Burman Yan36bcbec2006-12-19 12:56:11 -0800320 resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400321 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400322 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Len Brown4be44fc2005-08-05 00:44:28 -0400324 buffer.length = sizeof(*resource) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 buffer.pointer = resource;
326
Len Brown4be44fc2005-08-05 00:44:28 -0400327 switch (link->irq.resource_type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400328 case ACPI_RESOURCE_TYPE_IRQ:
329 resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 resource->res.length = sizeof(struct acpi_resource);
Bob Moore50eca3e2005-09-30 19:03:00 -0400331 resource->res.data.irq.triggering = link->irq.triggering;
332 resource->res.data.irq.polarity =
333 link->irq.polarity;
334 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
335 resource->res.data.irq.sharable =
Len Brown4be44fc2005-08-05 00:44:28 -0400336 ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400338 resource->res.data.irq.sharable = ACPI_SHARED;
339 resource->res.data.irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 resource->res.data.irq.interrupts[0] = irq;
341 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400342
Bob Moore50eca3e2005-09-30 19:03:00 -0400343 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
344 resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 resource->res.length = sizeof(struct acpi_resource);
Len Brown4be44fc2005-08-05 00:44:28 -0400346 resource->res.data.extended_irq.producer_consumer =
347 ACPI_CONSUMER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400348 resource->res.data.extended_irq.triggering =
349 link->irq.triggering;
350 resource->res.data.extended_irq.polarity =
351 link->irq.polarity;
352 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
353 resource->res.data.irq.sharable =
Len Brown4be44fc2005-08-05 00:44:28 -0400354 ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400356 resource->res.data.irq.sharable = ACPI_SHARED;
357 resource->res.data.extended_irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 resource->res.data.extended_irq.interrupts[0] = irq;
359 /* ignore resource_source, it's optional */
360 break;
361 default:
Len Brown64684632006-06-26 23:41:38 -0400362 printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 result = -EINVAL;
364 goto end;
365
366 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400367 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* Attempt to set the resource */
Patrick Mochel67a71362006-05-19 16:54:42 -0400370 status = acpi_set_current_resources(link->device->handle, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* check for total failure */
373 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400374 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 result = -ENODEV;
376 goto end;
377 }
378
379 /* Query _STA, set device->status */
380 result = acpi_bus_get_status(link->device);
381 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400382 printk(KERN_ERR PREFIX "Unable to read status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto end;
384 }
385 if (!link->device->status.enabled) {
Len Browncece9292006-06-26 23:04:31 -0400386 printk(KERN_WARNING PREFIX
387 "%s [%s] disabled and referenced, BIOS bug\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400388 acpi_device_name(link->device),
Len Browncece9292006-06-26 23:04:31 -0400389 acpi_device_bid(link->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 /* Query _CRS, set link->irq.active */
393 result = acpi_pci_link_get_current(link);
394 if (result) {
395 goto end;
396 }
397
398 /*
399 * Is current setting not what we set?
400 * set link->irq.active
401 */
402 if (link->irq.active != irq) {
403 /*
404 * policy: when _CRS doesn't return what we just _SRS
405 * assume _SRS worked and override _CRS value.
406 */
Len Browncece9292006-06-26 23:04:31 -0400407 printk(KERN_WARNING PREFIX
408 "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400409 acpi_device_name(link->device),
Len Browncece9292006-06-26 23:04:31 -0400410 acpi_device_bid(link->device), link->irq.active, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 link->irq.active = irq;
412 }
413
414 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
Len Brown4be44fc2005-08-05 00:44:28 -0400415
416 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 kfree(resource);
Patrick Mocheld550d982006-06-27 00:41:40 -0400418 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/* --------------------------------------------------------------------------
422 PCI Link IRQ Management
423 -------------------------------------------------------------------------- */
424
425/*
426 * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
427 * Link Devices to move the PIRQs around to minimize sharing.
428 *
429 * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
430 * that the BIOS has already set to active. This is necessary because
431 * ACPI has no automatic means of knowing what ISA IRQs are used. Note that
432 * if the BIOS doesn't set a Link Device active, ACPI needs to program it
433 * even if acpi_irq_nobalance is set.
434 *
435 * A tables of penalties avoids directing PCI interrupts to well known
436 * ISA IRQs. Boot params are available to over-ride the default table:
437 *
438 * List interrupts that are free for PCI use.
439 * acpi_irq_pci=n[,m]
440 *
441 * List interrupts that should not be used for PCI:
442 * acpi_irq_isa=n[,m]
443 *
444 * Note that PCI IRQ routers have a list of possible IRQs,
445 * which may not include the IRQs this table says are available.
446 *
447 * Since this heuristic can't tell the difference between a link
448 * that no device will attach to, vs. a link which may be shared
449 * by multiple active devices -- it is not optimal.
450 *
451 * If interrupt performance is that important, get an IO-APIC system
452 * with a pin dedicated to each device. Or for that matter, an MSI
453 * enabled system.
454 */
455
456#define ACPI_MAX_IRQS 256
457#define ACPI_MAX_ISA_IRQ 16
458
459#define PIRQ_PENALTY_PCI_AVAILABLE (0)
460#define PIRQ_PENALTY_PCI_POSSIBLE (16*16)
461#define PIRQ_PENALTY_PCI_USING (16*16*16)
462#define PIRQ_PENALTY_ISA_TYPICAL (16*16*16*16)
463#define PIRQ_PENALTY_ISA_USED (16*16*16*16*16)
464#define PIRQ_PENALTY_ISA_ALWAYS (16*16*16*16*16*16)
465
466static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
467 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ0 timer */
468 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ1 keyboard */
469 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ2 cascade */
Len Brown4be44fc2005-08-05 00:44:28 -0400470 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
471 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ5 sometimes SoundBlaster */
473 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ6 */
474 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ7 parallel, spurious */
475 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ8 rtc, sometimes */
476 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ9 PCI, often acpi */
477 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ10 PCI */
478 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ11 PCI */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700479 PIRQ_PENALTY_ISA_USED, /* IRQ12 mouse */
480 PIRQ_PENALTY_ISA_USED, /* IRQ13 fpe, sometimes */
481 PIRQ_PENALTY_ISA_USED, /* IRQ14 ide0 */
482 PIRQ_PENALTY_ISA_USED, /* IRQ15 ide1 */
Len Brown4be44fc2005-08-05 00:44:28 -0400483 /* >IRQ15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484};
485
Len Brown4be44fc2005-08-05 00:44:28 -0400486int __init acpi_irq_penalty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700488 struct list_head *node;
489 struct acpi_pci_link *link;
490 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * Update penalties to facilitate IRQ balancing.
494 */
495 list_for_each(node, &acpi_link.entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 link = list_entry(node, struct acpi_pci_link, node);
497 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400498 printk(KERN_ERR PREFIX "Invalid link context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 continue;
500 }
501
502 /*
503 * reflect the possible and active irqs in the penalty table --
504 * useful for breaking ties.
505 */
506 if (link->irq.possible_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400507 int penalty =
508 PIRQ_PENALTY_PCI_POSSIBLE /
509 link->irq.possible_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 for (i = 0; i < link->irq.possible_count; i++) {
512 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
Len Brown4be44fc2005-08-05 00:44:28 -0400513 acpi_irq_penalty[link->irq.
514 possible[i]] +=
515 penalty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 } else if (link->irq.active) {
Len Brown4be44fc2005-08-05 00:44:28 -0400519 acpi_irq_penalty[link->irq.active] +=
520 PIRQ_PENALTY_PCI_POSSIBLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 }
523 /* Add a penalty for the SCI */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300524 acpi_irq_penalty[acpi_gbl_FADT.sci_interrupt] += PIRQ_PENALTY_PCI_USING;
Patrick Mocheld550d982006-06-27 00:41:40 -0400525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Bjorn Helgaas32836252008-11-05 16:17:52 -0700528static int acpi_irq_balance = -1; /* 0: static, 1: balance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Len Brown4be44fc2005-08-05 00:44:28 -0400530static int acpi_pci_link_allocate(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Len Brown4be44fc2005-08-05 00:44:28 -0400532 int irq;
533 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
David Shaohua Li87bec662005-07-27 23:02:00 -0400535 if (link->irq.initialized) {
536 if (link->refcnt == 0)
537 /* This means the link is disabled but initialized */
538 acpi_pci_link_set(link, link->irq.active);
Patrick Mocheld550d982006-06-27 00:41:40 -0400539 return 0;
David Shaohua Li87bec662005-07-27 23:02:00 -0400540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /*
543 * search for active IRQ in list of possible IRQs.
544 */
545 for (i = 0; i < link->irq.possible_count; ++i) {
546 if (link->irq.active == link->irq.possible[i])
547 break;
548 }
549 /*
550 * forget active IRQ that is not in possible list
551 */
552 if (i == link->irq.possible_count) {
553 if (acpi_strict)
Len Browncece9292006-06-26 23:04:31 -0400554 printk(KERN_WARNING PREFIX "_CRS %d not found"
555 " in _PRS\n", link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 link->irq.active = 0;
557 }
558
559 /*
560 * if active found, use it; else pick entry from end of possible list.
561 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700562 if (link->irq.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 irq = link->irq.active;
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700564 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 irq = link->irq.possible[link->irq.possible_count - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (acpi_irq_balance || !link->irq.active) {
568 /*
569 * Select the best IRQ. This is done in reverse to promote
570 * the use of IRQs 9, 10, 11, and >15.
571 */
572 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
Len Brown4be44fc2005-08-05 00:44:28 -0400573 if (acpi_irq_penalty[irq] >
574 acpi_irq_penalty[link->irq.possible[i]])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 irq = link->irq.possible[i];
576 }
577 }
578
579 /* Attempt to enable the link device at this IRQ. */
580 if (acpi_pci_link_set(link, irq)) {
Len Brown64684632006-06-26 23:41:38 -0400581 printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
582 "Try pci=noacpi or acpi=off\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400583 acpi_device_name(link->device),
Len Brown64684632006-06-26 23:41:38 -0400584 acpi_device_bid(link->device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400585 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 } else {
587 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
Frank Seidel4d939152009-02-04 17:03:07 +0100588 printk(KERN_WARNING PREFIX "%s [%s] enabled at IRQ %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400589 acpi_device_name(link->device),
590 acpi_device_bid(link->device), link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
593 link->irq.initialized = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
David Shaohua Li87bec662005-07-27 23:02:00 -0400598 * acpi_pci_link_allocate_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * success: return IRQ >= 0
600 * failure: return -1
601 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700602int acpi_pci_link_allocate_irq(acpi_handle handle, int index, int *triggering,
603 int *polarity, char **name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700605 int result;
606 struct acpi_device *device;
607 struct acpi_pci_link *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 result = acpi_bus_get_device(handle, &device);
610 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400611 printk(KERN_ERR PREFIX "Invalid link device\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200615 link = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400617 printk(KERN_ERR PREFIX "Invalid link context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400618 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
621 /* TBD: Support multiple index (IRQ) entries per Link Device */
622 if (index) {
Len Brown64684632006-06-26 23:41:38 -0400623 printk(KERN_ERR PREFIX "Invalid index %d\n", index);
Patrick Mocheld550d982006-06-27 00:41:40 -0400624 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Ingo Molnar36e43092006-04-27 05:25:00 -0400627 mutex_lock(&acpi_link_lock);
David Shaohua Li87bec662005-07-27 23:02:00 -0400628 if (acpi_pci_link_allocate(link)) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400629 mutex_unlock(&acpi_link_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400630 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400631 }
Len Brown4be44fc2005-08-05 00:44:28 -0400632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!link->irq.active) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400634 mutex_unlock(&acpi_link_lock);
Len Brown64684632006-06-26 23:41:38 -0400635 printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400636 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Len Brown4be44fc2005-08-05 00:44:28 -0400638 link->refcnt++;
Ingo Molnar36e43092006-04-27 05:25:00 -0400639 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Bob Moore50eca3e2005-09-30 19:03:00 -0400641 if (triggering)
642 *triggering = link->irq.triggering;
643 if (polarity)
644 *polarity = link->irq.polarity;
Len Brown4be44fc2005-08-05 00:44:28 -0400645 if (name)
646 *name = acpi_device_bid(link->device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400647 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400648 "Link %s is referenced\n",
649 acpi_device_bid(link->device)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400650 return (link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
David Shaohua Li87bec662005-07-27 23:02:00 -0400653/*
654 * We don't change link's irq information here. After it is reenabled, we
655 * continue use the info
656 */
Len Brown4be44fc2005-08-05 00:44:28 -0400657int acpi_pci_link_free_irq(acpi_handle handle)
David Shaohua Li87bec662005-07-27 23:02:00 -0400658{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700659 struct acpi_device *device;
660 struct acpi_pci_link *link;
Len Brown4be44fc2005-08-05 00:44:28 -0400661 acpi_status result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
David Shaohua Li87bec662005-07-27 23:02:00 -0400663 result = acpi_bus_get_device(handle, &device);
664 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400665 printk(KERN_ERR PREFIX "Invalid link device\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400666 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400667 }
668
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200669 link = acpi_driver_data(device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400670 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400671 printk(KERN_ERR PREFIX "Invalid link context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400672 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400673 }
674
Ingo Molnar36e43092006-04-27 05:25:00 -0400675 mutex_lock(&acpi_link_lock);
David Shaohua Li87bec662005-07-27 23:02:00 -0400676 if (!link->irq.initialized) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400677 mutex_unlock(&acpi_link_lock);
Len Brown64684632006-06-26 23:41:38 -0400678 printk(KERN_ERR PREFIX "Link isn't initialized\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400679 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400680 }
David Shaohua Liecc21eb2005-08-03 11:00:11 -0400681#ifdef FUTURE_USE
682 /*
683 * The Link reference count allows us to _DISable an unused link
684 * and suspend time, and set it again on resume.
685 * However, 2.6.12 still has irq_router.resume
686 * which blindly restores the link state.
687 * So we disable the reference count method
688 * to prevent duplicate acpi_pci_link_set()
689 * which would harm some systems
690 */
Len Brown4be44fc2005-08-05 00:44:28 -0400691 link->refcnt--;
David Shaohua Liecc21eb2005-08-03 11:00:11 -0400692#endif
David Shaohua Li87bec662005-07-27 23:02:00 -0400693 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400694 "Link %s is dereferenced\n",
695 acpi_device_bid(link->device)));
David Shaohua Li87bec662005-07-27 23:02:00 -0400696
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700697 if (link->refcnt == 0)
donald.d.dugger@intel.com383d7a12008-10-17 07:49:50 -0700698 acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700699
Ingo Molnar36e43092006-04-27 05:25:00 -0400700 mutex_unlock(&acpi_link_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400701 return (link->irq.active);
David Shaohua Li87bec662005-07-27 23:02:00 -0400702}
Len Brown4be44fc2005-08-05 00:44:28 -0400703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/* --------------------------------------------------------------------------
705 Driver Interface
706 -------------------------------------------------------------------------- */
707
Len Brown4be44fc2005-08-05 00:44:28 -0400708static int acpi_pci_link_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700710 int result;
711 struct acpi_pci_link *link;
712 int i;
Len Brown4be44fc2005-08-05 00:44:28 -0400713 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400716 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Burman Yan36bcbec2006-12-19 12:56:11 -0800718 link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (!link)
Patrick Mocheld550d982006-06-27 00:41:40 -0400720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 link->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
724 strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700725 device->driver_data = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Ingo Molnar36e43092006-04-27 05:25:00 -0400727 mutex_lock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 result = acpi_pci_link_get_possible(link);
729 if (result)
730 goto end;
731
732 /* query and set link->irq.active */
733 acpi_pci_link_get_current(link);
734
Dan Aloni0dc070b2007-07-09 11:33:18 -0700735 printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400736 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 for (i = 0; i < link->irq.possible_count; i++) {
738 if (link->irq.active == link->irq.possible[i]) {
739 printk(" *%d", link->irq.possible[i]);
740 found = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400741 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 printk(" %d", link->irq.possible[i]);
743 }
744
745 printk(")");
746
747 if (!found)
748 printk(" *%d", link->irq.active);
749
Len Brown4be44fc2005-08-05 00:44:28 -0400750 if (!link->device->status.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 printk(", disabled.");
752
753 printk("\n");
754
755 /* TBD: Acquire/release lock */
756 list_add_tail(&link->node, &acpi_link.entries);
757 acpi_link.count++;
758
Len Brown4be44fc2005-08-05 00:44:28 -0400759 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* disable all links -- to be activated on use */
donald.d.dugger@intel.com383d7a12008-10-17 07:49:50 -0700761 acpi_evaluate_object(device->handle, "_DIS", NULL, NULL);
Ingo Molnar36e43092006-04-27 05:25:00 -0400762 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (result)
765 kfree(link);
766
Patrick Mocheld550d982006-06-27 00:41:40 -0400767 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Len Brown4be44fc2005-08-05 00:44:28 -0400770static int acpi_pci_link_resume(struct acpi_pci_link *link)
Linus Torvalds697a2d62005-08-01 12:37:54 -0700771{
Linus Torvalds697a2d62005-08-01 12:37:54 -0700772 if (link->refcnt && link->irq.active && link->irq.initialized)
Patrick Mocheld550d982006-06-27 00:41:40 -0400773 return (acpi_pci_link_set(link, link->irq.active));
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700774
775 return 0;
Linus Torvalds697a2d62005-08-01 12:37:54 -0700776}
777
Len Brown4be44fc2005-08-05 00:44:28 -0400778static int irqrouter_resume(struct sys_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700780 struct list_head *node;
781 struct acpi_pci_link *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 list_for_each(node, &acpi_link.entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 link = list_entry(node, struct acpi_pci_link, node);
785 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400786 printk(KERN_ERR PREFIX "Invalid link context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 continue;
788 }
Linus Torvalds697a2d62005-08-01 12:37:54 -0700789 acpi_pci_link_resume(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Len Brown4be44fc2005-08-05 00:44:28 -0400794static int acpi_pci_link_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700796 struct acpi_pci_link *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400799 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200801 link = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Ingo Molnar36e43092006-04-27 05:25:00 -0400803 mutex_lock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 list_del(&link->node);
Ingo Molnar36e43092006-04-27 05:25:00 -0400805 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 kfree(link);
Patrick Mocheld550d982006-06-27 00:41:40 -0400808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/*
812 * modify acpi_irq_penalty[] from cmdline
813 */
814static int __init acpi_irq_penalty_update(char *str, int used)
815{
816 int i;
817
818 for (i = 0; i < 16; i++) {
819 int retval;
820 int irq;
821
Len Brown4be44fc2005-08-05 00:44:28 -0400822 retval = get_option(&str, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (!retval)
825 break; /* no number found */
826
827 if (irq < 0)
828 continue;
Len Brown4be44fc2005-08-05 00:44:28 -0400829
Bjorn Helgaasfa46d352008-08-01 15:58:17 -0600830 if (irq >= ARRAY_SIZE(acpi_irq_penalty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 continue;
832
833 if (used)
834 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
835 else
836 acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
837
838 if (retval != 2) /* no next number */
839 break;
840 }
841 return 1;
842}
843
844/*
845 * We'd like PNP to call this routine for the
846 * single ISA_USED value for each legacy device.
847 * But instead it calls us with each POSSIBLE setting.
848 * There is no ISA_POSSIBLE weight, so we simply use
849 * the (small) PCI_USING penalty.
850 */
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500851void acpi_penalize_isa_irq(int irq, int active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Bjorn Helgaasfa46d352008-08-01 15:58:17 -0600853 if (irq >= 0 && irq < ARRAY_SIZE(acpi_irq_penalty)) {
854 if (active)
855 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
856 else
857 acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861/*
862 * Over-ride default table to reserve additional IRQs for use by ISA
863 * e.g. acpi_irq_isa=5
864 * Useful for telling ACPI how not to interfere with your ISA sound card.
865 */
866static int __init acpi_irq_isa(char *str)
867{
868 return acpi_irq_penalty_update(str, 1);
869}
Len Brown4be44fc2005-08-05 00:44:28 -0400870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871__setup("acpi_irq_isa=", acpi_irq_isa);
872
873/*
874 * Over-ride default table to free additional IRQs for use by PCI
875 * e.g. acpi_irq_pci=7,15
876 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
877 */
878static int __init acpi_irq_pci(char *str)
879{
880 return acpi_irq_penalty_update(str, 0);
881}
Len Brown4be44fc2005-08-05 00:44:28 -0400882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883__setup("acpi_irq_pci=", acpi_irq_pci);
884
885static int __init acpi_irq_nobalance_set(char *str)
886{
887 acpi_irq_balance = 0;
888 return 1;
889}
Len Brown4be44fc2005-08-05 00:44:28 -0400890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
892
Roel Kluin8a383ef2008-12-09 20:45:30 +0100893static int __init acpi_irq_balance_set(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 acpi_irq_balance = 1;
896 return 1;
897}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Len Brown4be44fc2005-08-05 00:44:28 -0400899__setup("acpi_irq_balance", acpi_irq_balance_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
David Shaohua Li87bec662005-07-27 23:02:00 -0400901/* FIXME: we will remove this interface after all drivers call pci_disable_device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902static struct sysdev_class irqrouter_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100903 .name = "irqrouter",
Len Brown4be44fc2005-08-05 00:44:28 -0400904 .resume = irqrouter_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905};
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static struct sys_device device_irqrouter = {
Len Brown4be44fc2005-08-05 00:44:28 -0400908 .id = 0,
909 .cls = &irqrouter_sysdev_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910};
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static int __init irqrouter_init_sysfs(void)
913{
914 int error;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (acpi_disabled || acpi_noirq)
Patrick Mocheld550d982006-06-27 00:41:40 -0400917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 error = sysdev_class_register(&irqrouter_sysdev_class);
920 if (!error)
921 error = sysdev_register(&device_irqrouter);
922
Patrick Mocheld550d982006-06-27 00:41:40 -0400923 return error;
Len Brown4be44fc2005-08-05 00:44:28 -0400924}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926device_initcall(irqrouter_init_sysfs);
927
Len Brown4be44fc2005-08-05 00:44:28 -0400928static int __init acpi_pci_link_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (acpi_noirq)
Patrick Mocheld550d982006-06-27 00:41:40 -0400931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Bjorn Helgaas32836252008-11-05 16:17:52 -0700933 if (acpi_irq_balance == -1) {
934 /* no command line switch: enable balancing in IOAPIC mode */
935 if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
936 acpi_irq_balance = 1;
937 else
938 acpi_irq_balance = 0;
939 }
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 acpi_link.count = 0;
942 INIT_LIST_HEAD(&acpi_link.entries);
943
944 if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400945 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Patrick Mocheld550d982006-06-27 00:41:40 -0400947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950subsys_initcall(acpi_pci_link_init);