blob: bc3d914dfc3e397880581b56b33188f575ea160d [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
Rafael J. Wysockic3146df2011-03-12 22:16:51 +010028#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/spinlock.h>
34#include <linux/pm.h>
35#include <linux/pci.h>
Ingo Molnar36e43092006-04-27 05:25:00 -040036#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Lv Zheng8b484632013-12-03 08:49:16 +080038#include <linux/acpi.h>
Sinan Kaya103544d2016-04-17 13:36:53 -040039#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Rashikac071b6042013-12-17 14:58:31 +053041#include "internal.h"
42
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070043#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050044ACPI_MODULE_NAME("pci_link");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_PCI_LINK_CLASS "pci_irq_routing"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#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"
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070049#define ACPI_PCI_LINK_MAX_POSSIBLE 16
50
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +010051static int acpi_pci_link_add(struct acpi_device *device,
52 const struct acpi_device_id *not_used);
53static void acpi_pci_link_remove(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Márton Némethc97adf92010-01-10 17:15:36 +010055static const struct acpi_device_id link_device_ids[] = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020056 {"PNP0C0F", 0},
57 {"", 0},
58};
Thomas Renninger1ba90e32007-07-23 14:44:41 +020059
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +010060static struct acpi_scan_handler pci_link_handler = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020061 .ids = link_device_ids,
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +010062 .attach = acpi_pci_link_add,
63 .detach = acpi_pci_link_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
David Shaohua Li87bec662005-07-27 23:02:00 -040066/*
67 * If a link is initialized, we never change its active and initialized
68 * later even the link is disable. Instead, we just repick the active irq
69 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct acpi_pci_link_irq {
Sinan Kaya37c59392015-12-09 11:18:28 -050071 u32 active; /* Current IRQ */
Bob Moore50eca3e2005-09-30 19:03:00 -040072 u8 triggering; /* All IRQs */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070073 u8 polarity; /* All IRQs */
Len Brown4be44fc2005-08-05 00:44:28 -040074 u8 resource_type;
75 u8 possible_count;
Sinan Kaya37c59392015-12-09 11:18:28 -050076 u32 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
Len Brown4be44fc2005-08-05 00:44:28 -040077 u8 initialized:1;
78 u8 reserved:7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81struct acpi_pci_link {
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -070082 struct list_head list;
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070083 struct acpi_device *device;
84 struct acpi_pci_link_irq irq;
85 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -070088static LIST_HEAD(acpi_link_list);
Adrian Bunke5685b92007-10-24 18:24:42 +020089static DEFINE_MUTEX(acpi_link_lock);
Sinan Kayaf1caa612016-10-24 00:31:31 -040090static int sci_irq = -1, sci_penalty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* --------------------------------------------------------------------------
93 PCI Link Device Management
94 -------------------------------------------------------------------------- */
95
96/*
97 * set context (link) possible list from resource list
98 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070099static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
100 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200102 struct acpi_pci_link *link = context;
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700103 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Len Browneca008c2005-09-22 00:25:18 -0400105 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400106 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600107 case ACPI_RESOURCE_TYPE_END_TAG:
Patrick Mocheld550d982006-06-27 00:41:40 -0400108 return AE_OK;
Bob Moore50eca3e2005-09-30 19:03:00 -0400109 case ACPI_RESOURCE_TYPE_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400110 {
111 struct acpi_resource_irq *p = &resource->data.irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400112 if (!p || !p->interrupt_count) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600113 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
114 "Blank _PRS IRQ resource\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400115 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Len Brown4be44fc2005-08-05 00:44:28 -0400117 for (i = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400118 (i < p->interrupt_count
Len Brown4be44fc2005-08-05 00:44:28 -0400119 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
120 if (!p->interrupts[i]) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600121 printk(KERN_WARNING PREFIX
122 "Invalid _PRS IRQ %d\n",
123 p->interrupts[i]);
Len Brown4be44fc2005-08-05 00:44:28 -0400124 continue;
125 }
126 link->irq.possible[i] = p->interrupts[i];
127 link->irq.possible_count++;
128 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400129 link->irq.triggering = p->triggering;
130 link->irq.polarity = p->polarity;
131 link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
Len Brown4be44fc2005-08-05 00:44:28 -0400132 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400134 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400135 {
Bob Moore50eca3e2005-09-30 19:03:00 -0400136 struct acpi_resource_extended_irq *p =
Len Brown4be44fc2005-08-05 00:44:28 -0400137 &resource->data.extended_irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400138 if (!p || !p->interrupt_count) {
Len Browncece9292006-06-26 23:04:31 -0400139 printk(KERN_WARNING PREFIX
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600140 "Blank _PRS EXT IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400141 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Len Brown4be44fc2005-08-05 00:44:28 -0400143 for (i = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400144 (i < p->interrupt_count
Len Brown4be44fc2005-08-05 00:44:28 -0400145 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
146 if (!p->interrupts[i]) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600147 printk(KERN_WARNING PREFIX
148 "Invalid _PRS IRQ %d\n",
149 p->interrupts[i]);
Len Brown4be44fc2005-08-05 00:44:28 -0400150 continue;
151 }
152 link->irq.possible[i] = p->interrupts[i];
153 link->irq.possible_count++;
154 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400155 link->irq.triggering = p->triggering;
156 link->irq.polarity = p->polarity;
157 link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 default:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600161 printk(KERN_ERR PREFIX "_PRS resource type 0x%x isn't an IRQ\n",
162 resource->type);
Patrick Mocheld550d982006-06-27 00:41:40 -0400163 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
Patrick Mocheld550d982006-06-27 00:41:40 -0400166 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Len Brown4be44fc2005-08-05 00:44:28 -0400169static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Len Brown4be44fc2005-08-05 00:44:28 -0400171 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Patrick Mochel67a71362006-05-19 16:54:42 -0400173 status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
Len Brown4be44fc2005-08-05 00:44:28 -0400174 acpi_pci_link_check_possible, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400176 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400177 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
Len Brown4be44fc2005-08-05 00:44:28 -0400180 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
181 "Found %d possible IRQs\n",
182 link->irq.possible_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Patrick Mocheld550d982006-06-27 00:41:40 -0400184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700187static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
188 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700190 int *irq = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Len Browneca008c2005-09-22 00:25:18 -0400192 switch (resource->type) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600193 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
194 case ACPI_RESOURCE_TYPE_END_TAG:
195 return AE_OK;
Bob Moore50eca3e2005-09-30 19:03:00 -0400196 case ACPI_RESOURCE_TYPE_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400197 {
198 struct acpi_resource_irq *p = &resource->data.irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400199 if (!p || !p->interrupt_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400200 /*
201 * IRQ descriptors may have no IRQ# bits set,
202 * particularly those those w/ _STA disabled
203 */
204 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600205 "Blank _CRS IRQ resource\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400206 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400207 }
208 *irq = p->interrupts[0];
209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400211 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400212 {
Bob Moore50eca3e2005-09-30 19:03:00 -0400213 struct acpi_resource_extended_irq *p =
Len Brown4be44fc2005-08-05 00:44:28 -0400214 &resource->data.extended_irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400215 if (!p || !p->interrupt_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400216 /*
217 * extended IRQ descriptors must
218 * return at least 1 IRQ
219 */
Len Browncece9292006-06-26 23:04:31 -0400220 printk(KERN_WARNING PREFIX
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600221 "Blank _CRS EXT IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400222 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400223 }
224 *irq = p->interrupts[0];
225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Len Brownd4ec6c72006-01-26 17:23:38 -0500227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 default:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600229 printk(KERN_ERR PREFIX "_CRS resource type 0x%x isn't an IRQ\n",
230 resource->type);
Patrick Mocheld550d982006-06-27 00:41:40 -0400231 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600233
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/*
238 * Run _CRS and set link->irq.active
239 *
240 * return value:
241 * 0 - success
242 * !0 - failure
243 */
Len Brown4be44fc2005-08-05 00:44:28 -0400244static int acpi_pci_link_get_current(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Len Brown4be44fc2005-08-05 00:44:28 -0400246 int result = 0;
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700247 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400248 int irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 link->irq.active = 0;
251
252 /* in practice, status disabled is meaningless, ignore it */
253 if (acpi_strict) {
254 /* Query _STA, set link->device->status */
255 result = acpi_bus_get_status(link->device);
256 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400257 printk(KERN_ERR PREFIX "Unable to read status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto end;
259 }
260
261 if (!link->device->status.enabled) {
262 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 }
266
267 /*
268 * Query and parse _CRS to get the current IRQ assignment.
269 */
270
Patrick Mochel67a71362006-05-19 16:54:42 -0400271 status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 acpi_pci_link_check_current, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400274 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 result = -ENODEV;
276 goto end;
277 }
278
279 if (acpi_strict && !irq) {
Len Brown64684632006-06-26 23:41:38 -0400280 printk(KERN_ERR PREFIX "_CRS returned 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 result = -ENODEV;
282 }
283
284 link->irq.active = irq;
285
286 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400289 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Len Brown4be44fc2005-08-05 00:44:28 -0400292static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700294 int result;
295 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct {
Len Brown4be44fc2005-08-05 00:44:28 -0400297 struct acpi_resource res;
298 struct acpi_resource end;
299 } *resource;
300 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Bjorn Helgaas6eca4b42009-02-17 14:00:50 -0700302 if (!irq)
Patrick Mocheld550d982006-06-27 00:41:40 -0400303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Burman Yan36bcbec2006-12-19 12:56:11 -0800305 resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400306 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400307 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Len Brown4be44fc2005-08-05 00:44:28 -0400309 buffer.length = sizeof(*resource) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 buffer.pointer = resource;
311
Len Brown4be44fc2005-08-05 00:44:28 -0400312 switch (link->irq.resource_type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400313 case ACPI_RESOURCE_TYPE_IRQ:
314 resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 resource->res.length = sizeof(struct acpi_resource);
Bob Moore50eca3e2005-09-30 19:03:00 -0400316 resource->res.data.irq.triggering = link->irq.triggering;
317 resource->res.data.irq.polarity =
318 link->irq.polarity;
319 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
320 resource->res.data.irq.sharable =
Len Brown4be44fc2005-08-05 00:44:28 -0400321 ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400323 resource->res.data.irq.sharable = ACPI_SHARED;
324 resource->res.data.irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 resource->res.data.irq.interrupts[0] = irq;
326 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400327
Bob Moore50eca3e2005-09-30 19:03:00 -0400328 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
329 resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 resource->res.length = sizeof(struct acpi_resource);
Len Brown4be44fc2005-08-05 00:44:28 -0400331 resource->res.data.extended_irq.producer_consumer =
332 ACPI_CONSUMER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400333 resource->res.data.extended_irq.triggering =
334 link->irq.triggering;
335 resource->res.data.extended_irq.polarity =
336 link->irq.polarity;
337 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
338 resource->res.data.irq.sharable =
Len Brown4be44fc2005-08-05 00:44:28 -0400339 ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400341 resource->res.data.irq.sharable = ACPI_SHARED;
342 resource->res.data.extended_irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 resource->res.data.extended_irq.interrupts[0] = irq;
344 /* ignore resource_source, it's optional */
345 break;
346 default:
Len Brown64684632006-06-26 23:41:38 -0400347 printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 result = -EINVAL;
349 goto end;
350
351 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400352 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
Yinghai Luf084dbb2013-03-23 19:16:37 +0000353 resource->end.length = sizeof(struct acpi_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Attempt to set the resource */
Patrick Mochel67a71362006-05-19 16:54:42 -0400356 status = acpi_set_current_resources(link->device->handle, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /* check for total failure */
359 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400360 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 result = -ENODEV;
362 goto end;
363 }
364
365 /* Query _STA, set device->status */
366 result = acpi_bus_get_status(link->device);
367 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400368 printk(KERN_ERR PREFIX "Unable to read status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto end;
370 }
371 if (!link->device->status.enabled) {
Len Browncece9292006-06-26 23:04:31 -0400372 printk(KERN_WARNING PREFIX
373 "%s [%s] disabled and referenced, BIOS bug\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400374 acpi_device_name(link->device),
Len Browncece9292006-06-26 23:04:31 -0400375 acpi_device_bid(link->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 /* Query _CRS, set link->irq.active */
379 result = acpi_pci_link_get_current(link);
380 if (result) {
381 goto end;
382 }
383
384 /*
385 * Is current setting not what we set?
386 * set link->irq.active
387 */
388 if (link->irq.active != irq) {
389 /*
390 * policy: when _CRS doesn't return what we just _SRS
391 * assume _SRS worked and override _CRS value.
392 */
Len Browncece9292006-06-26 23:04:31 -0400393 printk(KERN_WARNING PREFIX
394 "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400395 acpi_device_name(link->device),
Len Browncece9292006-06-26 23:04:31 -0400396 acpi_device_bid(link->device), link->irq.active, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 link->irq.active = irq;
398 }
399
400 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
Len Brown4be44fc2005-08-05 00:44:28 -0400401
402 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 kfree(resource);
Patrick Mocheld550d982006-06-27 00:41:40 -0400404 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/* --------------------------------------------------------------------------
408 PCI Link IRQ Management
409 -------------------------------------------------------------------------- */
410
411/*
412 * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
413 * Link Devices to move the PIRQs around to minimize sharing.
414 *
415 * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
416 * that the BIOS has already set to active. This is necessary because
417 * ACPI has no automatic means of knowing what ISA IRQs are used. Note that
418 * if the BIOS doesn't set a Link Device active, ACPI needs to program it
419 * even if acpi_irq_nobalance is set.
420 *
421 * A tables of penalties avoids directing PCI interrupts to well known
422 * ISA IRQs. Boot params are available to over-ride the default table:
423 *
424 * List interrupts that are free for PCI use.
425 * acpi_irq_pci=n[,m]
426 *
427 * List interrupts that should not be used for PCI:
428 * acpi_irq_isa=n[,m]
429 *
430 * Note that PCI IRQ routers have a list of possible IRQs,
431 * which may not include the IRQs this table says are available.
432 *
433 * Since this heuristic can't tell the difference between a link
434 * that no device will attach to, vs. a link which may be shared
435 * by multiple active devices -- it is not optimal.
436 *
437 * If interrupt performance is that important, get an IO-APIC system
438 * with a pin dedicated to each device. Or for that matter, an MSI
439 * enabled system.
440 */
441
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400442#define ACPI_MAX_ISA_IRQS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#define PIRQ_PENALTY_PCI_POSSIBLE (16*16)
445#define PIRQ_PENALTY_PCI_USING (16*16*16)
446#define PIRQ_PENALTY_ISA_TYPICAL (16*16*16*16)
447#define PIRQ_PENALTY_ISA_USED (16*16*16*16*16)
448#define PIRQ_PENALTY_ISA_ALWAYS (16*16*16*16*16*16)
449
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400450static int acpi_isa_irq_penalty[ACPI_MAX_ISA_IRQS] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ0 timer */
452 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ1 keyboard */
453 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ2 cascade */
Len Brown4be44fc2005-08-05 00:44:28 -0400454 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
455 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ5 sometimes SoundBlaster */
457 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ6 */
458 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ7 parallel, spurious */
459 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ8 rtc, sometimes */
Sinan Kaya103544d2016-04-17 13:36:53 -0400460 0, /* IRQ9 PCI, often acpi */
461 0, /* IRQ10 PCI */
462 0, /* IRQ11 PCI */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700463 PIRQ_PENALTY_ISA_USED, /* IRQ12 mouse */
464 PIRQ_PENALTY_ISA_USED, /* IRQ13 fpe, sometimes */
465 PIRQ_PENALTY_ISA_USED, /* IRQ14 ide0 */
466 PIRQ_PENALTY_ISA_USED, /* IRQ15 ide1 */
Rafael J. Wysockie2497142016-02-24 13:55:38 +0100467 /* >IRQ15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468};
469
Sinan Kaya103544d2016-04-17 13:36:53 -0400470static int acpi_irq_pci_sharing_penalty(int irq)
471{
472 struct acpi_pci_link *link;
473 int penalty = 0;
Sinan Kaya4a6e68b2016-06-29 04:27:35 -0400474 int i;
Sinan Kaya103544d2016-04-17 13:36:53 -0400475
476 list_for_each_entry(link, &acpi_link_list, list) {
477 /*
478 * If a link is active, penalize its IRQ heavily
479 * so we try to choose a different IRQ.
480 */
481 if (link->irq.active && link->irq.active == irq)
482 penalty += PIRQ_PENALTY_PCI_USING;
Sinan Kaya103544d2016-04-17 13:36:53 -0400483
Sinan Kaya4a6e68b2016-06-29 04:27:35 -0400484 /*
485 * penalize the IRQs PCI might use, but not as severely.
486 */
487 for (i = 0; i < link->irq.possible_count; i++)
488 if (link->irq.possible[i] == irq)
489 penalty += PIRQ_PENALTY_PCI_POSSIBLE /
490 link->irq.possible_count;
Sinan Kaya103544d2016-04-17 13:36:53 -0400491 }
492
493 return penalty;
494}
495
496static int acpi_irq_get_penalty(int irq)
497{
498 int penalty = 0;
499
Sinan Kayaf1caa612016-10-24 00:31:31 -0400500 if (irq == sci_irq)
501 penalty += sci_penalty;
Sinan Kaya103544d2016-04-17 13:36:53 -0400502
Sinan Kayaf7eca372016-06-29 04:27:37 -0400503 if (irq < ACPI_MAX_ISA_IRQS)
504 return penalty + acpi_isa_irq_penalty[irq];
505
Sinan Kayaf1caa612016-10-24 00:31:31 -0400506 return penalty + acpi_irq_pci_sharing_penalty(irq);
Sinan Kaya103544d2016-04-17 13:36:53 -0400507}
508
Sinan Kaya487cf9172016-06-29 04:27:36 -0400509int __init acpi_irq_penalty_init(void)
510{
511 struct acpi_pci_link *link;
512 int i;
513
514 /*
515 * Update penalties to facilitate IRQ balancing.
516 */
517 list_for_each_entry(link, &acpi_link_list, list) {
518
519 /*
520 * reflect the possible and active irqs in the penalty table --
521 * useful for breaking ties.
522 */
523 if (link->irq.possible_count) {
524 int penalty =
525 PIRQ_PENALTY_PCI_POSSIBLE /
526 link->irq.possible_count;
527
528 for (i = 0; i < link->irq.possible_count; i++) {
529 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQS)
530 acpi_isa_irq_penalty[link->irq.
531 possible[i]] +=
532 penalty;
533 }
534
535 } else if (link->irq.active &&
536 (link->irq.active < ACPI_MAX_ISA_IRQS)) {
537 acpi_isa_irq_penalty[link->irq.active] +=
538 PIRQ_PENALTY_PCI_POSSIBLE;
539 }
540 }
541
542 return 0;
543}
544
Bjorn Helgaas32836252008-11-05 16:17:52 -0700545static int acpi_irq_balance = -1; /* 0: static, 1: balance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Len Brown4be44fc2005-08-05 00:44:28 -0400547static int acpi_pci_link_allocate(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Len Brown4be44fc2005-08-05 00:44:28 -0400549 int irq;
550 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
David Shaohua Li87bec662005-07-27 23:02:00 -0400552 if (link->irq.initialized) {
553 if (link->refcnt == 0)
554 /* This means the link is disabled but initialized */
555 acpi_pci_link_set(link, link->irq.active);
Patrick Mocheld550d982006-06-27 00:41:40 -0400556 return 0;
David Shaohua Li87bec662005-07-27 23:02:00 -0400557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /*
560 * search for active IRQ in list of possible IRQs.
561 */
562 for (i = 0; i < link->irq.possible_count; ++i) {
563 if (link->irq.active == link->irq.possible[i])
564 break;
565 }
566 /*
567 * forget active IRQ that is not in possible list
568 */
569 if (i == link->irq.possible_count) {
570 if (acpi_strict)
Len Browncece9292006-06-26 23:04:31 -0400571 printk(KERN_WARNING PREFIX "_CRS %d not found"
572 " in _PRS\n", link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 link->irq.active = 0;
574 }
575
576 /*
577 * if active found, use it; else pick entry from end of possible list.
578 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700579 if (link->irq.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 irq = link->irq.active;
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700581 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 irq = link->irq.possible[link->irq.possible_count - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (acpi_irq_balance || !link->irq.active) {
585 /*
586 * Select the best IRQ. This is done in reverse to promote
587 * the use of IRQs 9, 10, 11, and >15.
588 */
589 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
Sinan Kaya103544d2016-04-17 13:36:53 -0400590 if (acpi_irq_get_penalty(irq) >
591 acpi_irq_get_penalty(link->irq.possible[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 irq = link->irq.possible[i];
593 }
594 }
Sinan Kaya103544d2016-04-17 13:36:53 -0400595 if (acpi_irq_get_penalty(irq) >= PIRQ_PENALTY_ISA_ALWAYS) {
Jiang Liu5ebc7602015-09-17 14:02:45 +0800596 printk(KERN_ERR PREFIX "No IRQ available for %s [%s]. "
597 "Try pci=noacpi or acpi=off\n",
598 acpi_device_name(link->device),
599 acpi_device_bid(link->device));
600 return -ENODEV;
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Attempt to enable the link device at this IRQ. */
604 if (acpi_pci_link_set(link, irq)) {
Len Brown64684632006-06-26 23:41:38 -0400605 printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
606 "Try pci=noacpi or acpi=off\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400607 acpi_device_name(link->device),
Len Brown64684632006-06-26 23:41:38 -0400608 acpi_device_bid(link->device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400609 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 } else {
Sinan Kaya98756f52016-10-24 00:31:32 -0400611 if (link->irq.active < ACPI_MAX_ISA_IRQS)
612 acpi_isa_irq_penalty[link->irq.active] +=
613 PIRQ_PENALTY_PCI_USING;
614
Frank Seidel4d939152009-02-04 17:03:07 +0100615 printk(KERN_WARNING PREFIX "%s [%s] enabled at IRQ %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400616 acpi_device_name(link->device),
617 acpi_device_bid(link->device), link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 link->irq.initialized = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
David Shaohua Li87bec662005-07-27 23:02:00 -0400625 * acpi_pci_link_allocate_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * success: return IRQ >= 0
627 * failure: return -1
628 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700629int acpi_pci_link_allocate_irq(acpi_handle handle, int index, int *triggering,
630 int *polarity, char **name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700632 int result;
633 struct acpi_device *device;
634 struct acpi_pci_link *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 result = acpi_bus_get_device(handle, &device);
637 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400638 printk(KERN_ERR PREFIX "Invalid link device\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400639 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200642 link = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400644 printk(KERN_ERR PREFIX "Invalid link context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400645 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
648 /* TBD: Support multiple index (IRQ) entries per Link Device */
649 if (index) {
Len Brown64684632006-06-26 23:41:38 -0400650 printk(KERN_ERR PREFIX "Invalid index %d\n", index);
Patrick Mocheld550d982006-06-27 00:41:40 -0400651 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Ingo Molnar36e43092006-04-27 05:25:00 -0400654 mutex_lock(&acpi_link_lock);
David Shaohua Li87bec662005-07-27 23:02:00 -0400655 if (acpi_pci_link_allocate(link)) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400656 mutex_unlock(&acpi_link_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400657 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400658 }
Len Brown4be44fc2005-08-05 00:44:28 -0400659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!link->irq.active) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400661 mutex_unlock(&acpi_link_lock);
Len Brown64684632006-06-26 23:41:38 -0400662 printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400663 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Len Brown4be44fc2005-08-05 00:44:28 -0400665 link->refcnt++;
Ingo Molnar36e43092006-04-27 05:25:00 -0400666 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Bob Moore50eca3e2005-09-30 19:03:00 -0400668 if (triggering)
669 *triggering = link->irq.triggering;
670 if (polarity)
671 *polarity = link->irq.polarity;
Len Brown4be44fc2005-08-05 00:44:28 -0400672 if (name)
673 *name = acpi_device_bid(link->device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400674 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400675 "Link %s is referenced\n",
676 acpi_device_bid(link->device)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400677 return (link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
David Shaohua Li87bec662005-07-27 23:02:00 -0400680/*
681 * We don't change link's irq information here. After it is reenabled, we
682 * continue use the info
683 */
Len Brown4be44fc2005-08-05 00:44:28 -0400684int acpi_pci_link_free_irq(acpi_handle handle)
David Shaohua Li87bec662005-07-27 23:02:00 -0400685{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700686 struct acpi_device *device;
687 struct acpi_pci_link *link;
Len Brown4be44fc2005-08-05 00:44:28 -0400688 acpi_status result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
David Shaohua Li87bec662005-07-27 23:02:00 -0400690 result = acpi_bus_get_device(handle, &device);
691 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400692 printk(KERN_ERR PREFIX "Invalid link device\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400693 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400694 }
695
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200696 link = acpi_driver_data(device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400697 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400698 printk(KERN_ERR PREFIX "Invalid link context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400699 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400700 }
701
Ingo Molnar36e43092006-04-27 05:25:00 -0400702 mutex_lock(&acpi_link_lock);
David Shaohua Li87bec662005-07-27 23:02:00 -0400703 if (!link->irq.initialized) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400704 mutex_unlock(&acpi_link_lock);
Len Brown64684632006-06-26 23:41:38 -0400705 printk(KERN_ERR PREFIX "Link isn't initialized\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400706 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400707 }
David Shaohua Liecc21eb2005-08-03 11:00:11 -0400708#ifdef FUTURE_USE
709 /*
710 * The Link reference count allows us to _DISable an unused link
711 * and suspend time, and set it again on resume.
712 * However, 2.6.12 still has irq_router.resume
713 * which blindly restores the link state.
714 * So we disable the reference count method
715 * to prevent duplicate acpi_pci_link_set()
716 * which would harm some systems
717 */
Len Brown4be44fc2005-08-05 00:44:28 -0400718 link->refcnt--;
David Shaohua Liecc21eb2005-08-03 11:00:11 -0400719#endif
David Shaohua Li87bec662005-07-27 23:02:00 -0400720 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400721 "Link %s is dereferenced\n",
722 acpi_device_bid(link->device)));
David Shaohua Li87bec662005-07-27 23:02:00 -0400723
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700724 if (link->refcnt == 0)
donald.d.dugger@intel.com383d7a12008-10-17 07:49:50 -0700725 acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700726
Ingo Molnar36e43092006-04-27 05:25:00 -0400727 mutex_unlock(&acpi_link_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return (link->irq.active);
David Shaohua Li87bec662005-07-27 23:02:00 -0400729}
Len Brown4be44fc2005-08-05 00:44:28 -0400730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/* --------------------------------------------------------------------------
732 Driver Interface
733 -------------------------------------------------------------------------- */
734
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100735static int acpi_pci_link_add(struct acpi_device *device,
736 const struct acpi_device_id *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700738 int result;
739 struct acpi_pci_link *link;
740 int i;
Len Brown4be44fc2005-08-05 00:44:28 -0400741 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Burman Yan36bcbec2006-12-19 12:56:11 -0800743 link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!link)
Patrick Mocheld550d982006-06-27 00:41:40 -0400745 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 link->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
749 strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700750 device->driver_data = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Ingo Molnar36e43092006-04-27 05:25:00 -0400752 mutex_lock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 result = acpi_pci_link_get_possible(link);
754 if (result)
755 goto end;
756
757 /* query and set link->irq.active */
758 acpi_pci_link_get_current(link);
759
Dan Aloni0dc070b2007-07-09 11:33:18 -0700760 printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400761 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 for (i = 0; i < link->irq.possible_count; i++) {
763 if (link->irq.active == link->irq.possible[i]) {
Kay Sieversbe964472012-05-08 17:24:20 +0200764 printk(KERN_CONT " *%d", link->irq.possible[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 found = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400766 } else
Kay Sieversbe964472012-05-08 17:24:20 +0200767 printk(KERN_CONT " %d", link->irq.possible[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Kay Sieversbe964472012-05-08 17:24:20 +0200770 printk(KERN_CONT ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (!found)
Kay Sieversbe964472012-05-08 17:24:20 +0200773 printk(KERN_CONT " *%d", link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Len Brown4be44fc2005-08-05 00:44:28 -0400775 if (!link->device->status.enabled)
Kay Sieversbe964472012-05-08 17:24:20 +0200776 printk(KERN_CONT ", disabled.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Kay Sieversbe964472012-05-08 17:24:20 +0200778 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -0700780 list_add_tail(&link->list, &acpi_link_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Len Brown4be44fc2005-08-05 00:44:28 -0400782 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* disable all links -- to be activated on use */
donald.d.dugger@intel.com383d7a12008-10-17 07:49:50 -0700784 acpi_evaluate_object(device->handle, "_DIS", NULL, NULL);
Ingo Molnar36e43092006-04-27 05:25:00 -0400785 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (result)
788 kfree(link);
789
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100790 return result < 0 ? result : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Len Brown4be44fc2005-08-05 00:44:28 -0400793static int acpi_pci_link_resume(struct acpi_pci_link *link)
Linus Torvalds697a2d62005-08-01 12:37:54 -0700794{
Linus Torvalds697a2d62005-08-01 12:37:54 -0700795 if (link->refcnt && link->irq.active && link->irq.initialized)
Patrick Mocheld550d982006-06-27 00:41:40 -0400796 return (acpi_pci_link_set(link, link->irq.active));
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700797
798 return 0;
Linus Torvalds697a2d62005-08-01 12:37:54 -0700799}
800
Rafael J. Wysockic3146df2011-03-12 22:16:51 +0100801static void irqrouter_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700803 struct acpi_pci_link *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -0700805 list_for_each_entry(link, &acpi_link_list, list) {
Linus Torvalds697a2d62005-08-01 12:37:54 -0700806 acpi_pci_link_resume(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100810static void acpi_pci_link_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700812 struct acpi_pci_link *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200814 link = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Ingo Molnar36e43092006-04-27 05:25:00 -0400816 mutex_lock(&acpi_link_lock);
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -0700817 list_del(&link->list);
Ingo Molnar36e43092006-04-27 05:25:00 -0400818 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 kfree(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823/*
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400824 * modify acpi_isa_irq_penalty[] from cmdline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
826static int __init acpi_irq_penalty_update(char *str, int used)
827{
828 int i;
829
830 for (i = 0; i < 16; i++) {
831 int retval;
832 int irq;
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400833 int new_penalty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Len Brown4be44fc2005-08-05 00:44:28 -0400835 retval = get_option(&str, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (!retval)
838 break; /* no number found */
839
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400840 /* see if this is a ISA IRQ */
841 if ((irq < 0) || (irq >= ACPI_MAX_ISA_IRQS))
Rafael J. Wysockie2497142016-02-24 13:55:38 +0100842 continue;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (used)
Sinan Kayaeeaed4b2016-10-24 00:31:30 -0400845 new_penalty = acpi_isa_irq_penalty[irq] +
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400846 PIRQ_PENALTY_ISA_USED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 else
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400848 new_penalty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400850 acpi_isa_irq_penalty[irq] = new_penalty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (retval != 2) /* no next number */
852 break;
853 }
854 return 1;
855}
856
857/*
858 * We'd like PNP to call this routine for the
859 * single ISA_USED value for each legacy device.
860 * But instead it calls us with each POSSIBLE setting.
861 * There is no ISA_POSSIBLE weight, so we simply use
862 * the (small) PCI_USING penalty.
863 */
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500864void acpi_penalize_isa_irq(int irq, int active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400866 if ((irq >= 0) && (irq < ARRAY_SIZE(acpi_isa_irq_penalty)))
Sinan Kayaeeaed4b2016-10-24 00:31:30 -0400867 acpi_isa_irq_penalty[irq] +=
Sinan Kaya54794582016-06-29 04:27:38 -0400868 (active ? PIRQ_PENALTY_ISA_USED : PIRQ_PENALTY_PCI_USING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Jiang Liu5ebc7602015-09-17 14:02:45 +0800871bool acpi_isa_irq_available(int irq)
872{
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400873 return irq >= 0 && (irq >= ARRAY_SIZE(acpi_isa_irq_penalty) ||
Sinan Kaya103544d2016-04-17 13:36:53 -0400874 acpi_irq_get_penalty(irq) < PIRQ_PENALTY_ISA_ALWAYS);
Jiang Liu5ebc7602015-09-17 14:02:45 +0800875}
876
Sinan Kayaf1caa612016-10-24 00:31:31 -0400877void acpi_penalize_sci_irq(int irq, int trigger, int polarity)
878{
879 sci_irq = irq;
880
881 if (trigger == ACPI_MADT_TRIGGER_LEVEL &&
882 polarity == ACPI_MADT_POLARITY_ACTIVE_LOW)
883 sci_penalty = PIRQ_PENALTY_PCI_USING;
884 else
885 sci_penalty = PIRQ_PENALTY_ISA_ALWAYS;
886}
887
Jiang Liu5d0ddfe2015-08-21 15:36:23 +0800888/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 * Over-ride default table to reserve additional IRQs for use by ISA
890 * e.g. acpi_irq_isa=5
891 * Useful for telling ACPI how not to interfere with your ISA sound card.
892 */
893static int __init acpi_irq_isa(char *str)
894{
895 return acpi_irq_penalty_update(str, 1);
896}
Len Brown4be44fc2005-08-05 00:44:28 -0400897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898__setup("acpi_irq_isa=", acpi_irq_isa);
899
900/*
901 * Over-ride default table to free additional IRQs for use by PCI
902 * e.g. acpi_irq_pci=7,15
903 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
904 */
905static int __init acpi_irq_pci(char *str)
906{
907 return acpi_irq_penalty_update(str, 0);
908}
Len Brown4be44fc2005-08-05 00:44:28 -0400909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910__setup("acpi_irq_pci=", acpi_irq_pci);
911
912static int __init acpi_irq_nobalance_set(char *str)
913{
914 acpi_irq_balance = 0;
915 return 1;
916}
Len Brown4be44fc2005-08-05 00:44:28 -0400917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
919
Roel Kluin8a383ef2008-12-09 20:45:30 +0100920static int __init acpi_irq_balance_set(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 acpi_irq_balance = 1;
923 return 1;
924}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Len Brown4be44fc2005-08-05 00:44:28 -0400926__setup("acpi_irq_balance", acpi_irq_balance_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Rafael J. Wysockic3146df2011-03-12 22:16:51 +0100928static struct syscore_ops irqrouter_syscore_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400929 .resume = irqrouter_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930};
931
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100932void __init acpi_pci_link_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (acpi_noirq)
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100935 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Bjorn Helgaas32836252008-11-05 16:17:52 -0700937 if (acpi_irq_balance == -1) {
938 /* no command line switch: enable balancing in IOAPIC mode */
939 if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
940 acpi_irq_balance = 1;
941 else
942 acpi_irq_balance = 0;
943 }
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100944 register_syscore_ops(&irqrouter_syscore_ops);
945 acpi_scan_add_handler(&pci_link_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}