blob: db11f7771ef1b9ed401c99fb047c275bd8f7079f [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * pci_link.c - ACPI PCI Interrupt Link Device Driver ($Revision: 34 $)
4 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2002 Dominik Brodowski <devel@brodo.de>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * TBD:
10 * 1. Support more than one IRQ resource entry per link device (index).
11 * 2. Implement start/stop mechanism and use ACPI Bus Driver facilities
12 * for IRQ management (e.g. start()->_SRS).
13 */
14
Rafael J. Wysockic3146df22011-03-12 22:16:51 +010015#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/spinlock.h>
21#include <linux/pm.h>
22#include <linux/pci.h>
Ingo Molnar36e43092006-04-27 05:25:00 -040023#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Lv Zheng8b484632013-12-03 08:49:16 +080025#include <linux/acpi.h>
Sinan Kaya103544d2016-04-17 13:36:53 -040026#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Rashikac071b6042013-12-17 14:58:31 +053028#include "internal.h"
29
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070030#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050031ACPI_MODULE_NAME("pci_link");
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define ACPI_PCI_LINK_CLASS "pci_irq_routing"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define ACPI_PCI_LINK_DEVICE_NAME "PCI Interrupt Link"
34#define ACPI_PCI_LINK_FILE_INFO "info"
35#define ACPI_PCI_LINK_FILE_STATUS "state"
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070036#define ACPI_PCI_LINK_MAX_POSSIBLE 16
37
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +010038static int acpi_pci_link_add(struct acpi_device *device,
39 const struct acpi_device_id *not_used);
40static void acpi_pci_link_remove(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Márton Némethc97adf92010-01-10 17:15:36 +010042static const struct acpi_device_id link_device_ids[] = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020043 {"PNP0C0F", 0},
44 {"", 0},
45};
Thomas Renninger1ba90e32007-07-23 14:44:41 +020046
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +010047static struct acpi_scan_handler pci_link_handler = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020048 .ids = link_device_ids,
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +010049 .attach = acpi_pci_link_add,
50 .detach = acpi_pci_link_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
David Shaohua Li87bec662005-07-27 23:02:00 -040053/*
54 * If a link is initialized, we never change its active and initialized
55 * later even the link is disable. Instead, we just repick the active irq
56 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057struct acpi_pci_link_irq {
Sinan Kaya37c59392015-12-09 11:18:28 -050058 u32 active; /* Current IRQ */
Bob Moore50eca3e2005-09-30 19:03:00 -040059 u8 triggering; /* All IRQs */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070060 u8 polarity; /* All IRQs */
Len Brown4be44fc2005-08-05 00:44:28 -040061 u8 resource_type;
62 u8 possible_count;
Sinan Kaya37c59392015-12-09 11:18:28 -050063 u32 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
Len Brown4be44fc2005-08-05 00:44:28 -040064 u8 initialized:1;
65 u8 reserved:7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68struct acpi_pci_link {
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -070069 struct list_head list;
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070070 struct acpi_device *device;
71 struct acpi_pci_link_irq irq;
72 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -070075static LIST_HEAD(acpi_link_list);
Adrian Bunke5685b92007-10-24 18:24:42 +020076static DEFINE_MUTEX(acpi_link_lock);
Sinan Kayaf1caa612016-10-24 00:31:31 -040077static int sci_irq = -1, sci_penalty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* --------------------------------------------------------------------------
80 PCI Link Device Management
81 -------------------------------------------------------------------------- */
82
83/*
84 * set context (link) possible list from resource list
85 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -070086static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
87 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Jan Engelhardt50dd0962006-10-01 00:28:50 +020089 struct acpi_pci_link *link = context;
Bjorn Helgaasc9d62442009-02-17 14:00:45 -070090 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Len Browneca008c2005-09-22 00:25:18 -040092 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -040093 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -060094 case ACPI_RESOURCE_TYPE_END_TAG:
Patrick Mocheld550d982006-06-27 00:41:40 -040095 return AE_OK;
Bob Moore50eca3e2005-09-30 19:03:00 -040096 case ACPI_RESOURCE_TYPE_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -040097 {
98 struct acpi_resource_irq *p = &resource->data.irq;
Bob Moore50eca3e2005-09-30 19:03:00 -040099 if (!p || !p->interrupt_count) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600100 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
101 "Blank _PRS IRQ resource\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400102 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Len Brown4be44fc2005-08-05 00:44:28 -0400104 for (i = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400105 (i < p->interrupt_count
Len Brown4be44fc2005-08-05 00:44:28 -0400106 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
107 if (!p->interrupts[i]) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600108 printk(KERN_WARNING PREFIX
109 "Invalid _PRS IRQ %d\n",
110 p->interrupts[i]);
Len Brown4be44fc2005-08-05 00:44:28 -0400111 continue;
112 }
113 link->irq.possible[i] = p->interrupts[i];
114 link->irq.possible_count++;
115 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400116 link->irq.triggering = p->triggering;
117 link->irq.polarity = p->polarity;
118 link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
Len Brown4be44fc2005-08-05 00:44:28 -0400119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400121 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400122 {
Bob Moore50eca3e2005-09-30 19:03:00 -0400123 struct acpi_resource_extended_irq *p =
Len Brown4be44fc2005-08-05 00:44:28 -0400124 &resource->data.extended_irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400125 if (!p || !p->interrupt_count) {
Len Browncece9292006-06-26 23:04:31 -0400126 printk(KERN_WARNING PREFIX
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600127 "Blank _PRS EXT IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400128 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Len Brown4be44fc2005-08-05 00:44:28 -0400130 for (i = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400131 (i < p->interrupt_count
Len Brown4be44fc2005-08-05 00:44:28 -0400132 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
133 if (!p->interrupts[i]) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600134 printk(KERN_WARNING PREFIX
135 "Invalid _PRS IRQ %d\n",
136 p->interrupts[i]);
Len Brown4be44fc2005-08-05 00:44:28 -0400137 continue;
138 }
139 link->irq.possible[i] = p->interrupts[i];
140 link->irq.possible_count++;
141 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400142 link->irq.triggering = p->triggering;
143 link->irq.polarity = p->polarity;
144 link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 default:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600148 printk(KERN_ERR PREFIX "_PRS resource type 0x%x isn't an IRQ\n",
149 resource->type);
Patrick Mocheld550d982006-06-27 00:41:40 -0400150 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
Patrick Mocheld550d982006-06-27 00:41:40 -0400153 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Len Brown4be44fc2005-08-05 00:44:28 -0400156static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Len Brown4be44fc2005-08-05 00:44:28 -0400158 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Patrick Mochel67a71362006-05-19 16:54:42 -0400160 status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
Len Brown4be44fc2005-08-05 00:44:28 -0400161 acpi_pci_link_check_possible, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (ACPI_FAILURE(status)) {
Alex Hung92d1b382018-02-22 21:43:44 -0800163 acpi_handle_debug(link->device->handle, "_PRS not present or invalid");
164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Len Brown4be44fc2005-08-05 00:44:28 -0400167 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
168 "Found %d possible IRQs\n",
169 link->irq.possible_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Patrick Mocheld550d982006-06-27 00:41:40 -0400171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700174static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
175 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700177 int *irq = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Len Browneca008c2005-09-22 00:25:18 -0400179 switch (resource->type) {
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600180 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
181 case ACPI_RESOURCE_TYPE_END_TAG:
182 return AE_OK;
Bob Moore50eca3e2005-09-30 19:03:00 -0400183 case ACPI_RESOURCE_TYPE_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400184 {
185 struct acpi_resource_irq *p = &resource->data.irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400186 if (!p || !p->interrupt_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400187 /*
188 * IRQ descriptors may have no IRQ# bits set,
189 * particularly those those w/ _STA disabled
190 */
191 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600192 "Blank _CRS IRQ resource\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400193 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400194 }
195 *irq = p->interrupts[0];
196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400198 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400199 {
Bob Moore50eca3e2005-09-30 19:03:00 -0400200 struct acpi_resource_extended_irq *p =
Len Brown4be44fc2005-08-05 00:44:28 -0400201 &resource->data.extended_irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400202 if (!p || !p->interrupt_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400203 /*
204 * extended IRQ descriptors must
205 * return at least 1 IRQ
206 */
Len Browncece9292006-06-26 23:04:31 -0400207 printk(KERN_WARNING PREFIX
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600208 "Blank _CRS EXT IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400210 }
211 *irq = p->interrupts[0];
212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Len Brownd4ec6c72006-01-26 17:23:38 -0500214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 default:
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600216 printk(KERN_ERR PREFIX "_CRS resource type 0x%x isn't an IRQ\n",
217 resource->type);
Patrick Mocheld550d982006-06-27 00:41:40 -0400218 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Bjorn Helgaas4a5e3632008-07-15 09:42:57 -0600220
Patrick Mocheld550d982006-06-27 00:41:40 -0400221 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
225 * Run _CRS and set link->irq.active
226 *
227 * return value:
228 * 0 - success
229 * !0 - failure
230 */
Len Brown4be44fc2005-08-05 00:44:28 -0400231static int acpi_pci_link_get_current(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Len Brown4be44fc2005-08-05 00:44:28 -0400233 int result = 0;
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700234 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400235 int irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 link->irq.active = 0;
238
239 /* in practice, status disabled is meaningless, ignore it */
240 if (acpi_strict) {
241 /* Query _STA, set link->device->status */
242 result = acpi_bus_get_status(link->device);
243 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400244 printk(KERN_ERR PREFIX "Unable to read status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto end;
246 }
247
248 if (!link->device->status.enabled) {
249 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 }
253
254 /*
255 * Query and parse _CRS to get the current IRQ assignment.
256 */
257
Patrick Mochel67a71362006-05-19 16:54:42 -0400258 status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
Len Brown4be44fc2005-08-05 00:44:28 -0400259 acpi_pci_link_check_current, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400261 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 result = -ENODEV;
263 goto end;
264 }
265
266 if (acpi_strict && !irq) {
Len Brown64684632006-06-26 23:41:38 -0400267 printk(KERN_ERR PREFIX "_CRS returned 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 result = -ENODEV;
269 }
270
271 link->irq.active = irq;
272
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
274
Len Brown4be44fc2005-08-05 00:44:28 -0400275 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400276 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Len Brown4be44fc2005-08-05 00:44:28 -0400279static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700281 int result;
282 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct {
Len Brown4be44fc2005-08-05 00:44:28 -0400284 struct acpi_resource res;
285 struct acpi_resource end;
286 } *resource;
287 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Bjorn Helgaas6eca4b42009-02-17 14:00:50 -0700289 if (!irq)
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Burman Yan36bcbec2006-12-19 12:56:11 -0800292 resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400293 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400294 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Len Brown4be44fc2005-08-05 00:44:28 -0400296 buffer.length = sizeof(*resource) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 buffer.pointer = resource;
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 switch (link->irq.resource_type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400300 case ACPI_RESOURCE_TYPE_IRQ:
301 resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 resource->res.length = sizeof(struct acpi_resource);
Bob Moore50eca3e2005-09-30 19:03:00 -0400303 resource->res.data.irq.triggering = link->irq.triggering;
304 resource->res.data.irq.polarity =
305 link->irq.polarity;
306 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800307 resource->res.data.irq.shareable =
Len Brown4be44fc2005-08-05 00:44:28 -0400308 ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 else
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800310 resource->res.data.irq.shareable = ACPI_SHARED;
Bob Moore50eca3e2005-09-30 19:03:00 -0400311 resource->res.data.irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 resource->res.data.irq.interrupts[0] = irq;
313 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400314
Bob Moore50eca3e2005-09-30 19:03:00 -0400315 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
316 resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 resource->res.length = sizeof(struct acpi_resource);
Len Brown4be44fc2005-08-05 00:44:28 -0400318 resource->res.data.extended_irq.producer_consumer =
319 ACPI_CONSUMER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400320 resource->res.data.extended_irq.triggering =
321 link->irq.triggering;
322 resource->res.data.extended_irq.polarity =
323 link->irq.polarity;
324 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800325 resource->res.data.irq.shareable =
Len Brown4be44fc2005-08-05 00:44:28 -0400326 ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 else
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800328 resource->res.data.irq.shareable = ACPI_SHARED;
Bob Moore50eca3e2005-09-30 19:03:00 -0400329 resource->res.data.extended_irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 resource->res.data.extended_irq.interrupts[0] = irq;
331 /* ignore resource_source, it's optional */
332 break;
333 default:
Len Brown64684632006-06-26 23:41:38 -0400334 printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 result = -EINVAL;
336 goto end;
337
338 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400339 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
Yinghai Luf084dbb2013-03-23 19:16:37 +0000340 resource->end.length = sizeof(struct acpi_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Attempt to set the resource */
Patrick Mochel67a71362006-05-19 16:54:42 -0400343 status = acpi_set_current_resources(link->device->handle, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* check for total failure */
346 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400347 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 result = -ENODEV;
349 goto end;
350 }
351
352 /* Query _STA, set device->status */
353 result = acpi_bus_get_status(link->device);
354 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400355 printk(KERN_ERR PREFIX "Unable to read status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto end;
357 }
358 if (!link->device->status.enabled) {
Len Browncece9292006-06-26 23:04:31 -0400359 printk(KERN_WARNING PREFIX
360 "%s [%s] disabled and referenced, BIOS bug\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400361 acpi_device_name(link->device),
Len Browncece9292006-06-26 23:04:31 -0400362 acpi_device_bid(link->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
365 /* Query _CRS, set link->irq.active */
366 result = acpi_pci_link_get_current(link);
367 if (result) {
368 goto end;
369 }
370
371 /*
372 * Is current setting not what we set?
373 * set link->irq.active
374 */
375 if (link->irq.active != irq) {
376 /*
377 * policy: when _CRS doesn't return what we just _SRS
378 * assume _SRS worked and override _CRS value.
379 */
Len Browncece9292006-06-26 23:04:31 -0400380 printk(KERN_WARNING PREFIX
381 "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400382 acpi_device_name(link->device),
Len Browncece9292006-06-26 23:04:31 -0400383 acpi_device_bid(link->device), link->irq.active, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 link->irq.active = irq;
385 }
386
387 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
Len Brown4be44fc2005-08-05 00:44:28 -0400388
389 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 kfree(resource);
Patrick Mocheld550d982006-06-27 00:41:40 -0400391 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/* --------------------------------------------------------------------------
395 PCI Link IRQ Management
396 -------------------------------------------------------------------------- */
397
398/*
399 * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
400 * Link Devices to move the PIRQs around to minimize sharing.
401 *
402 * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
403 * that the BIOS has already set to active. This is necessary because
404 * ACPI has no automatic means of knowing what ISA IRQs are used. Note that
405 * if the BIOS doesn't set a Link Device active, ACPI needs to program it
406 * even if acpi_irq_nobalance is set.
407 *
408 * A tables of penalties avoids directing PCI interrupts to well known
409 * ISA IRQs. Boot params are available to over-ride the default table:
410 *
411 * List interrupts that are free for PCI use.
412 * acpi_irq_pci=n[,m]
413 *
414 * List interrupts that should not be used for PCI:
415 * acpi_irq_isa=n[,m]
416 *
417 * Note that PCI IRQ routers have a list of possible IRQs,
418 * which may not include the IRQs this table says are available.
419 *
420 * Since this heuristic can't tell the difference between a link
421 * that no device will attach to, vs. a link which may be shared
422 * by multiple active devices -- it is not optimal.
423 *
424 * If interrupt performance is that important, get an IO-APIC system
425 * with a pin dedicated to each device. Or for that matter, an MSI
426 * enabled system.
427 */
428
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400429#define ACPI_MAX_ISA_IRQS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#define PIRQ_PENALTY_PCI_POSSIBLE (16*16)
432#define PIRQ_PENALTY_PCI_USING (16*16*16)
433#define PIRQ_PENALTY_ISA_TYPICAL (16*16*16*16)
434#define PIRQ_PENALTY_ISA_USED (16*16*16*16*16)
435#define PIRQ_PENALTY_ISA_ALWAYS (16*16*16*16*16*16)
436
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400437static int acpi_isa_irq_penalty[ACPI_MAX_ISA_IRQS] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ0 timer */
439 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ1 keyboard */
440 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ2 cascade */
Len Brown4be44fc2005-08-05 00:44:28 -0400441 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
442 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ5 sometimes SoundBlaster */
444 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ6 */
445 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ7 parallel, spurious */
446 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ8 rtc, sometimes */
Sinan Kaya103544d2016-04-17 13:36:53 -0400447 0, /* IRQ9 PCI, often acpi */
448 0, /* IRQ10 PCI */
449 0, /* IRQ11 PCI */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700450 PIRQ_PENALTY_ISA_USED, /* IRQ12 mouse */
451 PIRQ_PENALTY_ISA_USED, /* IRQ13 fpe, sometimes */
452 PIRQ_PENALTY_ISA_USED, /* IRQ14 ide0 */
453 PIRQ_PENALTY_ISA_USED, /* IRQ15 ide1 */
Rafael J. Wysockie2497142016-02-24 13:55:38 +0100454 /* >IRQ15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455};
456
Sinan Kaya103544d2016-04-17 13:36:53 -0400457static int acpi_irq_pci_sharing_penalty(int irq)
458{
459 struct acpi_pci_link *link;
460 int penalty = 0;
Sinan Kaya4a6e68b2016-06-29 04:27:35 -0400461 int i;
Sinan Kaya103544d2016-04-17 13:36:53 -0400462
463 list_for_each_entry(link, &acpi_link_list, list) {
464 /*
465 * If a link is active, penalize its IRQ heavily
466 * so we try to choose a different IRQ.
467 */
468 if (link->irq.active && link->irq.active == irq)
469 penalty += PIRQ_PENALTY_PCI_USING;
Sinan Kaya103544d2016-04-17 13:36:53 -0400470
Sinan Kaya4a6e68b2016-06-29 04:27:35 -0400471 /*
472 * penalize the IRQs PCI might use, but not as severely.
473 */
474 for (i = 0; i < link->irq.possible_count; i++)
475 if (link->irq.possible[i] == irq)
476 penalty += PIRQ_PENALTY_PCI_POSSIBLE /
477 link->irq.possible_count;
Sinan Kaya103544d2016-04-17 13:36:53 -0400478 }
479
480 return penalty;
481}
482
483static int acpi_irq_get_penalty(int irq)
484{
485 int penalty = 0;
486
Sinan Kayaf1caa612016-10-24 00:31:31 -0400487 if (irq == sci_irq)
488 penalty += sci_penalty;
Sinan Kaya103544d2016-04-17 13:36:53 -0400489
Sinan Kayaf7eca372016-06-29 04:27:37 -0400490 if (irq < ACPI_MAX_ISA_IRQS)
491 return penalty + acpi_isa_irq_penalty[irq];
492
Sinan Kayaf1caa612016-10-24 00:31:31 -0400493 return penalty + acpi_irq_pci_sharing_penalty(irq);
Sinan Kaya103544d2016-04-17 13:36:53 -0400494}
495
Sinan Kaya487cf9172016-06-29 04:27:36 -0400496int __init acpi_irq_penalty_init(void)
497{
498 struct acpi_pci_link *link;
499 int i;
500
501 /*
502 * Update penalties to facilitate IRQ balancing.
503 */
504 list_for_each_entry(link, &acpi_link_list, list) {
505
506 /*
507 * reflect the possible and active irqs in the penalty table --
508 * useful for breaking ties.
509 */
510 if (link->irq.possible_count) {
511 int penalty =
512 PIRQ_PENALTY_PCI_POSSIBLE /
513 link->irq.possible_count;
514
515 for (i = 0; i < link->irq.possible_count; i++) {
516 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQS)
517 acpi_isa_irq_penalty[link->irq.
518 possible[i]] +=
519 penalty;
520 }
521
522 } else if (link->irq.active &&
523 (link->irq.active < ACPI_MAX_ISA_IRQS)) {
524 acpi_isa_irq_penalty[link->irq.active] +=
525 PIRQ_PENALTY_PCI_POSSIBLE;
526 }
527 }
528
529 return 0;
530}
531
Bjorn Helgaas32836252008-11-05 16:17:52 -0700532static int acpi_irq_balance = -1; /* 0: static, 1: balance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Len Brown4be44fc2005-08-05 00:44:28 -0400534static int acpi_pci_link_allocate(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Len Brown4be44fc2005-08-05 00:44:28 -0400536 int irq;
537 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David Shaohua Li87bec662005-07-27 23:02:00 -0400539 if (link->irq.initialized) {
540 if (link->refcnt == 0)
541 /* This means the link is disabled but initialized */
542 acpi_pci_link_set(link, link->irq.active);
Patrick Mocheld550d982006-06-27 00:41:40 -0400543 return 0;
David Shaohua Li87bec662005-07-27 23:02:00 -0400544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /*
547 * search for active IRQ in list of possible IRQs.
548 */
549 for (i = 0; i < link->irq.possible_count; ++i) {
550 if (link->irq.active == link->irq.possible[i])
551 break;
552 }
553 /*
554 * forget active IRQ that is not in possible list
555 */
556 if (i == link->irq.possible_count) {
557 if (acpi_strict)
Len Browncece9292006-06-26 23:04:31 -0400558 printk(KERN_WARNING PREFIX "_CRS %d not found"
559 " in _PRS\n", link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 link->irq.active = 0;
561 }
562
563 /*
564 * if active found, use it; else pick entry from end of possible list.
565 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700566 if (link->irq.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 irq = link->irq.active;
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700568 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 irq = link->irq.possible[link->irq.possible_count - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (acpi_irq_balance || !link->irq.active) {
572 /*
573 * Select the best IRQ. This is done in reverse to promote
574 * the use of IRQs 9, 10, 11, and >15.
575 */
576 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
Sinan Kaya103544d2016-04-17 13:36:53 -0400577 if (acpi_irq_get_penalty(irq) >
578 acpi_irq_get_penalty(link->irq.possible[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 irq = link->irq.possible[i];
580 }
581 }
Sinan Kaya103544d2016-04-17 13:36:53 -0400582 if (acpi_irq_get_penalty(irq) >= PIRQ_PENALTY_ISA_ALWAYS) {
Jiang Liu5ebc7602015-09-17 14:02:45 +0800583 printk(KERN_ERR PREFIX "No IRQ available for %s [%s]. "
584 "Try pci=noacpi or acpi=off\n",
585 acpi_device_name(link->device),
586 acpi_device_bid(link->device));
587 return -ENODEV;
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* Attempt to enable the link device at this IRQ. */
591 if (acpi_pci_link_set(link, irq)) {
Len Brown64684632006-06-26 23:41:38 -0400592 printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
593 "Try pci=noacpi or acpi=off\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400594 acpi_device_name(link->device),
Len Brown64684632006-06-26 23:41:38 -0400595 acpi_device_bid(link->device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400596 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 } else {
Sinan Kaya98756f52016-10-24 00:31:32 -0400598 if (link->irq.active < ACPI_MAX_ISA_IRQS)
599 acpi_isa_irq_penalty[link->irq.active] +=
600 PIRQ_PENALTY_PCI_USING;
601
Sinan Kaya90fd94e2018-01-16 13:51:04 -0500602 pr_info("%s [%s] enabled at IRQ %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400603 acpi_device_name(link->device),
604 acpi_device_bid(link->device), link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 link->irq.initialized = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*
David Shaohua Li87bec662005-07-27 23:02:00 -0400612 * acpi_pci_link_allocate_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * success: return IRQ >= 0
614 * failure: return -1
615 */
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700616int acpi_pci_link_allocate_irq(acpi_handle handle, int index, int *triggering,
617 int *polarity, char **name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700619 int result;
620 struct acpi_device *device;
621 struct acpi_pci_link *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 result = acpi_bus_get_device(handle, &device);
624 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400625 printk(KERN_ERR PREFIX "Invalid link device\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400626 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200629 link = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400631 printk(KERN_ERR PREFIX "Invalid link context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400632 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
635 /* TBD: Support multiple index (IRQ) entries per Link Device */
636 if (index) {
Len Brown64684632006-06-26 23:41:38 -0400637 printk(KERN_ERR PREFIX "Invalid index %d\n", index);
Patrick Mocheld550d982006-06-27 00:41:40 -0400638 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640
Ingo Molnar36e43092006-04-27 05:25:00 -0400641 mutex_lock(&acpi_link_lock);
David Shaohua Li87bec662005-07-27 23:02:00 -0400642 if (acpi_pci_link_allocate(link)) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400643 mutex_unlock(&acpi_link_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400644 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400645 }
Len Brown4be44fc2005-08-05 00:44:28 -0400646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!link->irq.active) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400648 mutex_unlock(&acpi_link_lock);
Len Brown64684632006-06-26 23:41:38 -0400649 printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400650 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Len Brown4be44fc2005-08-05 00:44:28 -0400652 link->refcnt++;
Ingo Molnar36e43092006-04-27 05:25:00 -0400653 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Bob Moore50eca3e2005-09-30 19:03:00 -0400655 if (triggering)
656 *triggering = link->irq.triggering;
657 if (polarity)
658 *polarity = link->irq.polarity;
Len Brown4be44fc2005-08-05 00:44:28 -0400659 if (name)
660 *name = acpi_device_bid(link->device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400661 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400662 "Link %s is referenced\n",
663 acpi_device_bid(link->device)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400664 return (link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
David Shaohua Li87bec662005-07-27 23:02:00 -0400667/*
668 * We don't change link's irq information here. After it is reenabled, we
669 * continue use the info
670 */
Len Brown4be44fc2005-08-05 00:44:28 -0400671int acpi_pci_link_free_irq(acpi_handle handle)
David Shaohua Li87bec662005-07-27 23:02:00 -0400672{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700673 struct acpi_device *device;
674 struct acpi_pci_link *link;
Len Brown4be44fc2005-08-05 00:44:28 -0400675 acpi_status result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
David Shaohua Li87bec662005-07-27 23:02:00 -0400677 result = acpi_bus_get_device(handle, &device);
678 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400679 printk(KERN_ERR PREFIX "Invalid link device\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400680 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400681 }
682
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200683 link = acpi_driver_data(device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400684 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400685 printk(KERN_ERR PREFIX "Invalid link context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400686 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400687 }
688
Ingo Molnar36e43092006-04-27 05:25:00 -0400689 mutex_lock(&acpi_link_lock);
David Shaohua Li87bec662005-07-27 23:02:00 -0400690 if (!link->irq.initialized) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400691 mutex_unlock(&acpi_link_lock);
Len Brown64684632006-06-26 23:41:38 -0400692 printk(KERN_ERR PREFIX "Link isn't initialized\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400693 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400694 }
David Shaohua Liecc21eb2005-08-03 11:00:11 -0400695#ifdef FUTURE_USE
696 /*
697 * The Link reference count allows us to _DISable an unused link
698 * and suspend time, and set it again on resume.
699 * However, 2.6.12 still has irq_router.resume
700 * which blindly restores the link state.
701 * So we disable the reference count method
702 * to prevent duplicate acpi_pci_link_set()
703 * which would harm some systems
704 */
Len Brown4be44fc2005-08-05 00:44:28 -0400705 link->refcnt--;
David Shaohua Liecc21eb2005-08-03 11:00:11 -0400706#endif
David Shaohua Li87bec662005-07-27 23:02:00 -0400707 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400708 "Link %s is dereferenced\n",
709 acpi_device_bid(link->device)));
David Shaohua Li87bec662005-07-27 23:02:00 -0400710
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700711 if (link->refcnt == 0)
donald.d.dugger@intel.com383d7a12008-10-17 07:49:50 -0700712 acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700713
Ingo Molnar36e43092006-04-27 05:25:00 -0400714 mutex_unlock(&acpi_link_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400715 return (link->irq.active);
David Shaohua Li87bec662005-07-27 23:02:00 -0400716}
Len Brown4be44fc2005-08-05 00:44:28 -0400717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718/* --------------------------------------------------------------------------
719 Driver Interface
720 -------------------------------------------------------------------------- */
721
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100722static int acpi_pci_link_add(struct acpi_device *device,
723 const struct acpi_device_id *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700725 int result;
726 struct acpi_pci_link *link;
727 int i;
Len Brown4be44fc2005-08-05 00:44:28 -0400728 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Burman Yan36bcbec2006-12-19 12:56:11 -0800730 link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!link)
Patrick Mocheld550d982006-06-27 00:41:40 -0400732 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 link->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
736 strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700737 device->driver_data = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Ingo Molnar36e43092006-04-27 05:25:00 -0400739 mutex_lock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 result = acpi_pci_link_get_possible(link);
741 if (result)
742 goto end;
743
744 /* query and set link->irq.active */
745 acpi_pci_link_get_current(link);
746
Dan Aloni0dc070b2007-07-09 11:33:18 -0700747 printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400748 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 for (i = 0; i < link->irq.possible_count; i++) {
750 if (link->irq.active == link->irq.possible[i]) {
Kay Sieversbe964472012-05-08 17:24:20 +0200751 printk(KERN_CONT " *%d", link->irq.possible[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 found = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400753 } else
Kay Sieversbe964472012-05-08 17:24:20 +0200754 printk(KERN_CONT " %d", link->irq.possible[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Kay Sieversbe964472012-05-08 17:24:20 +0200757 printk(KERN_CONT ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (!found)
Kay Sieversbe964472012-05-08 17:24:20 +0200760 printk(KERN_CONT " *%d", link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Len Brown4be44fc2005-08-05 00:44:28 -0400762 if (!link->device->status.enabled)
Kay Sieversbe964472012-05-08 17:24:20 +0200763 printk(KERN_CONT ", disabled.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Kay Sieversbe964472012-05-08 17:24:20 +0200765 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -0700767 list_add_tail(&link->list, &acpi_link_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Len Brown4be44fc2005-08-05 00:44:28 -0400769 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* disable all links -- to be activated on use */
donald.d.dugger@intel.com383d7a12008-10-17 07:49:50 -0700771 acpi_evaluate_object(device->handle, "_DIS", NULL, NULL);
Ingo Molnar36e43092006-04-27 05:25:00 -0400772 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (result)
775 kfree(link);
776
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100777 return result < 0 ? result : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Len Brown4be44fc2005-08-05 00:44:28 -0400780static int acpi_pci_link_resume(struct acpi_pci_link *link)
Linus Torvalds697a2d62005-08-01 12:37:54 -0700781{
Linus Torvalds697a2d62005-08-01 12:37:54 -0700782 if (link->refcnt && link->irq.active && link->irq.initialized)
Patrick Mocheld550d982006-06-27 00:41:40 -0400783 return (acpi_pci_link_set(link, link->irq.active));
Bjorn Helgaas1c9ca3a2009-02-17 14:00:40 -0700784
785 return 0;
Linus Torvalds697a2d62005-08-01 12:37:54 -0700786}
787
Rafael J. Wysockic3146df22011-03-12 22:16:51 +0100788static void irqrouter_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700790 struct acpi_pci_link *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -0700792 list_for_each_entry(link, &acpi_link_list, list) {
Linus Torvalds697a2d62005-08-01 12:37:54 -0700793 acpi_pci_link_resume(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100797static void acpi_pci_link_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Bjorn Helgaasc9d62442009-02-17 14:00:45 -0700799 struct acpi_pci_link *link;
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);
Bjorn Helgaas5f0dcca2009-02-17 14:00:55 -0700804 list_del(&link->list);
Ingo Molnar36e43092006-04-27 05:25:00 -0400805 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 kfree(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/*
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400811 * modify acpi_isa_irq_penalty[] from cmdline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
813static int __init acpi_irq_penalty_update(char *str, int used)
814{
815 int i;
816
817 for (i = 0; i < 16; i++) {
818 int retval;
819 int irq;
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400820 int new_penalty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
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
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400827 /* see if this is a ISA IRQ */
828 if ((irq < 0) || (irq >= ACPI_MAX_ISA_IRQS))
Rafael J. Wysockie2497142016-02-24 13:55:38 +0100829 continue;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (used)
Sinan Kayaeeaed4b2016-10-24 00:31:30 -0400832 new_penalty = acpi_isa_irq_penalty[irq] +
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400833 PIRQ_PENALTY_ISA_USED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 else
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400835 new_penalty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400837 acpi_isa_irq_penalty[irq] = new_penalty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 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{
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400853 if ((irq >= 0) && (irq < ARRAY_SIZE(acpi_isa_irq_penalty)))
Sinan Kayaeeaed4b2016-10-24 00:31:30 -0400854 acpi_isa_irq_penalty[irq] +=
Sinan Kaya54794582016-06-29 04:27:38 -0400855 (active ? PIRQ_PENALTY_ISA_USED : PIRQ_PENALTY_PCI_USING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Jiang Liu5ebc7602015-09-17 14:02:45 +0800858bool acpi_isa_irq_available(int irq)
859{
Sinan Kaya5c5087a2016-04-17 13:36:54 -0400860 return irq >= 0 && (irq >= ARRAY_SIZE(acpi_isa_irq_penalty) ||
Sinan Kaya103544d2016-04-17 13:36:53 -0400861 acpi_irq_get_penalty(irq) < PIRQ_PENALTY_ISA_ALWAYS);
Jiang Liu5ebc7602015-09-17 14:02:45 +0800862}
863
Sinan Kayaf1caa612016-10-24 00:31:31 -0400864void acpi_penalize_sci_irq(int irq, int trigger, int polarity)
865{
866 sci_irq = irq;
867
868 if (trigger == ACPI_MADT_TRIGGER_LEVEL &&
869 polarity == ACPI_MADT_POLARITY_ACTIVE_LOW)
870 sci_penalty = PIRQ_PENALTY_PCI_USING;
871 else
872 sci_penalty = PIRQ_PENALTY_ISA_ALWAYS;
873}
874
Jiang Liu5d0ddfe2015-08-21 15:36:23 +0800875/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * Over-ride default table to reserve additional IRQs for use by ISA
877 * e.g. acpi_irq_isa=5
878 * Useful for telling ACPI how not to interfere with your ISA sound card.
879 */
880static int __init acpi_irq_isa(char *str)
881{
882 return acpi_irq_penalty_update(str, 1);
883}
Len Brown4be44fc2005-08-05 00:44:28 -0400884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885__setup("acpi_irq_isa=", acpi_irq_isa);
886
887/*
888 * Over-ride default table to free additional IRQs for use by PCI
889 * e.g. acpi_irq_pci=7,15
890 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
891 */
892static int __init acpi_irq_pci(char *str)
893{
894 return acpi_irq_penalty_update(str, 0);
895}
Len Brown4be44fc2005-08-05 00:44:28 -0400896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897__setup("acpi_irq_pci=", acpi_irq_pci);
898
899static int __init acpi_irq_nobalance_set(char *str)
900{
901 acpi_irq_balance = 0;
902 return 1;
903}
Len Brown4be44fc2005-08-05 00:44:28 -0400904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
906
Roel Kluin8a383ef2008-12-09 20:45:30 +0100907static int __init acpi_irq_balance_set(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 acpi_irq_balance = 1;
910 return 1;
911}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Len Brown4be44fc2005-08-05 00:44:28 -0400913__setup("acpi_irq_balance", acpi_irq_balance_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Rafael J. Wysockic3146df22011-03-12 22:16:51 +0100915static struct syscore_ops irqrouter_syscore_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400916 .resume = irqrouter_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917};
918
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100919void __init acpi_pci_link_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (acpi_noirq)
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100922 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Bjorn Helgaas32836252008-11-05 16:17:52 -0700924 if (acpi_irq_balance == -1) {
925 /* no command line switch: enable balancing in IOAPIC mode */
926 if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
927 acpi_irq_balance = 1;
928 else
929 acpi_irq_balance = 0;
930 }
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +0100931 register_syscore_ops(&irqrouter_syscore_ops);
932 acpi_scan_add_handler(&pci_link_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}