blob: 236765c6017b150d9f4704bae21b24275a9b96dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pci_bind.c - ACPI PCI Device Binding ($Revision: 2 $)
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 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/proc_fs.h>
31#include <linux/spinlock.h>
32#include <linux/pm.h>
33#include <linux/pci.h>
34#include <linux/acpi.h>
35#include <acpi/acpi_bus.h>
36#include <acpi/acpi_drivers.h>
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050039ACPI_MODULE_NAME("pci_bind");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41struct acpi_pci_data {
Len Brown4be44fc2005-08-05 00:44:28 -040042 struct acpi_pci_id id;
43 struct pci_bus *bus;
44 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
Alexander Chiangce597bb2009-06-10 19:55:09 +000047static int acpi_pci_bind(struct acpi_device *device);
Adrian Bunke5685b92007-10-24 18:24:42 +020048static int acpi_pci_unbind(struct acpi_device *device);
49
Adrian Bunk8713cbe2005-09-02 17:16:48 -040050static void acpi_pci_data_handler(acpi_handle handle, u32 function,
51 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /* TBD: Anything we need to do here? */
55
Patrick Mocheld550d982006-06-27 00:41:40 -040056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/**
Rajesh Shah4ce448e2005-04-28 00:25:53 -070060 * acpi_get_pci_id
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * ------------------
62 * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
63 * to resolve PCI information for ACPI-PCI devices defined in the namespace.
64 * This typically occurs when resolving PCI operation region information.
65 */
Len Brown4be44fc2005-08-05 00:44:28 -040066acpi_status acpi_get_pci_id(acpi_handle handle, struct acpi_pci_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Len Brown4be44fc2005-08-05 00:44:28 -040068 int result = 0;
69 acpi_status status = AE_OK;
70 struct acpi_device *device = NULL;
71 struct acpi_pci_data *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (!id)
Patrick Mocheld550d982006-06-27 00:41:40 -040075 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 result = acpi_bus_get_device(handle, &device);
78 if (result) {
Len Brown64684632006-06-26 23:41:38 -040079 printk(KERN_ERR PREFIX
80 "Invalid ACPI Bus context for device %s\n",
81 acpi_device_bid(device));
Patrick Mocheld550d982006-06-27 00:41:40 -040082 return AE_NOT_EXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
Len Brown4be44fc2005-08-05 00:44:28 -040085 status = acpi_get_data(handle, acpi_pci_data_handler, (void **)&data);
Rajesh Shah4ce448e2005-04-28 00:25:53 -070086 if (ACPI_FAILURE(status) || !data) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040087 ACPI_EXCEPTION((AE_INFO, status,
88 "Invalid ACPI-PCI context for device %s",
89 acpi_device_bid(device)));
Patrick Mocheld550d982006-06-27 00:41:40 -040090 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 *id = data->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Len Brown4be44fc2005-08-05 00:44:28 -040095 /*
96 id->segment = data->id.segment;
97 id->bus = data->id.bus;
98 id->device = data->id.device;
99 id->function = data->id.function;
100 */
101
102 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700103 "Device %s has PCI address %04x:%02x:%02x.%d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400104 acpi_device_bid(device), id->segment, id->bus,
105 id->device, id->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Patrick Mocheld550d982006-06-27 00:41:40 -0400107 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
Len Brown4be44fc2005-08-05 00:44:28 -0400109
Rajesh Shah4ce448e2005-04-28 00:25:53 -0700110EXPORT_SYMBOL(acpi_get_pci_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Alexander Chiangce597bb2009-06-10 19:55:09 +0000112static int acpi_pci_bind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Len Brown4be44fc2005-08-05 00:44:28 -0400114 int result = 0;
Len Brown087da3b2008-12-30 22:44:33 -0500115 acpi_status status;
116 struct acpi_pci_data *data;
117 struct acpi_pci_data *pdata;
118 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
119 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (!device || !device->parent)
Patrick Mocheld550d982006-06-27 00:41:40 -0400122 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Burman Yan36bcbec2006-12-19 12:56:11 -0800124 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown087da3b2008-12-30 22:44:33 -0500125 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400126 return -ENOMEM;
Len Brown087da3b2008-12-30 22:44:33 -0500127
128 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
129 if (ACPI_FAILURE(status)) {
130 kfree(data);
131 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Len Brown4be44fc2005-08-05 00:44:28 -0400134 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
Len Brown087da3b2008-12-30 22:44:33 -0500135 (char *)buffer.pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /*
138 * Segment & Bus
139 * -------------
140 * These are obtained via the parent device's ACPI-PCI context.
141 */
Len Brown4be44fc2005-08-05 00:44:28 -0400142 status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
143 (void **)&pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400145 ACPI_EXCEPTION((AE_INFO, status,
146 "Invalid ACPI-PCI context for parent device %s",
147 acpi_device_bid(device->parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 result = -ENODEV;
149 goto end;
150 }
151 data->id.segment = pdata->id.segment;
152 data->id.bus = pdata->bus->number;
153
154 /*
155 * Device & Function
156 * -----------------
157 * These are simply obtained from the device's _ADR method. Note
158 * that a value of zero is valid.
159 */
160 data->id.device = device->pnp.bus_address >> 16;
161 data->id.function = device->pnp.bus_address & 0xFFFF;
162
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700163 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %04x:%02x:%02x.%d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400164 data->id.segment, data->id.bus, data->id.device,
165 data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /*
168 * TBD: Support slot devices (e.g. function=0xFFFF).
169 */
170
171 /*
172 * Locate PCI Device
173 * -----------------
174 * Locate matching device in PCI namespace. If it doesn't exist
175 * this typically means that the device isn't currently inserted
176 * (e.g. docking station, port replicator, etc.).
177 */
Kenji Kaneshigedacd2542009-05-26 09:08:03 +0900178 data->dev = pci_get_slot(pdata->bus,
179 PCI_DEVFN(data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (!data->dev) {
Len Brown4be44fc2005-08-05 00:44:28 -0400181 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700182 "Device %04x:%02x:%02x.%d not present in PCI namespace\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400183 data->id.segment, data->id.bus,
184 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 result = -ENODEV;
186 goto end;
187 }
188 if (!data->dev->bus) {
Len Brown64684632006-06-26 23:41:38 -0400189 printk(KERN_ERR PREFIX
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700190 "Device %04x:%02x:%02x.%d has invalid 'bus' field\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400191 data->id.segment, data->id.bus,
Len Brown64684632006-06-26 23:41:38 -0400192 data->id.device, data->id.function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 result = -ENODEV;
194 goto end;
195 }
196
197 /*
198 * PCI Bridge?
199 * -----------
200 * If so, set the 'bus' field and install the 'bind' function to
201 * facilitate callbacks for all of its children.
202 */
203 if (data->dev->subordinate) {
Len Brown4be44fc2005-08-05 00:44:28 -0400204 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700205 "Device %04x:%02x:%02x.%d is a PCI bridge\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400206 data->id.segment, data->id.bus,
207 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 data->bus = data->dev->subordinate;
209 device->ops.bind = acpi_pci_bind;
210 device->ops.unbind = acpi_pci_unbind;
211 }
212
213 /*
214 * Attach ACPI-PCI Context
215 * -----------------------
216 * Thus binding the ACPI and PCI devices.
217 */
218 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
219 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400220 ACPI_EXCEPTION((AE_INFO, status,
221 "Unable to attach ACPI-PCI context to device %s",
222 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 result = -ENODEV;
224 goto end;
225 }
226
227 /*
228 * PCI Routing Table
229 * -----------------
230 * Evaluate and parse _PRT, if exists. This code is independent of
231 * PCI bridges (above) to allow parsing of _PRT objects within the
232 * scope of non-bridge devices. Note that _PRTs within the scope of
233 * a PCI bridge assume the bridge's subordinate bus number.
234 *
235 * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
236 */
237 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
238 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400239 if (data->bus) /* PCI-PCI bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 acpi_pci_irq_add_prt(device->handle, data->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400241 data->bus->number);
242 else /* non-bridge PCI device */
243 acpi_pci_irq_add_prt(device->handle, data->id.segment,
244 data->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Len Brown4be44fc2005-08-05 00:44:28 -0400247 end:
Len Brown087da3b2008-12-30 22:44:33 -0500248 kfree(buffer.pointer);
Kenji Kaneshigedacd2542009-05-26 09:08:03 +0900249 if (result) {
250 pci_dev_put(data->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 kfree(data);
Kenji Kaneshigedacd2542009-05-26 09:08:03 +0900252 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400253 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Adrian Bunke5685b92007-10-24 18:24:42 +0200256static int acpi_pci_unbind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Len Brown4be44fc2005-08-05 00:44:28 -0400258 int result = 0;
Len Brown087da3b2008-12-30 22:44:33 -0500259 acpi_status status;
260 struct acpi_pci_data *data;
261 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (!device || !device->parent)
Patrick Mocheld550d982006-06-27 00:41:40 -0400265 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Len Brown087da3b2008-12-30 22:44:33 -0500267 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
268 if (ACPI_FAILURE(status))
269 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
Len Brown087da3b2008-12-30 22:44:33 -0500272 (char *) buffer.pointer));
273 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Len Brown4be44fc2005-08-05 00:44:28 -0400275 status =
276 acpi_get_data(device->handle, acpi_pci_data_handler,
277 (void **)&data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 result = -ENODEV;
280 goto end;
281 }
282
283 status = acpi_detach_data(device->handle, acpi_pci_data_handler);
284 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400285 ACPI_EXCEPTION((AE_INFO, status,
286 "Unable to detach data from device %s",
287 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 result = -ENODEV;
289 goto end;
290 }
291 if (data->dev->subordinate) {
292 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
293 }
Kenji Kaneshigedacd2542009-05-26 09:08:03 +0900294 pci_dev_put(data->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 kfree(data);
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Len Brown4be44fc2005-08-05 00:44:28 -0400301int
302acpi_pci_bind_root(struct acpi_device *device,
303 struct acpi_pci_id *id, struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Len Brown4be44fc2005-08-05 00:44:28 -0400305 int result = 0;
Len Brown087da3b2008-12-30 22:44:33 -0500306 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400307 struct acpi_pci_data *data = NULL;
Len Brown087da3b2008-12-30 22:44:33 -0500308 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Len Brown4be44fc2005-08-05 00:44:28 -0400310 if (!device || !id || !bus) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400311 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
Burman Yan36bcbec2006-12-19 12:56:11 -0800314 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown087da3b2008-12-30 22:44:33 -0500315 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 data->id = *id;
319 data->bus = bus;
320 device->ops.bind = acpi_pci_bind;
321 device->ops.unbind = acpi_pci_unbind;
322
Len Brown087da3b2008-12-30 22:44:33 -0500323 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
324 if (ACPI_FAILURE(status)) {
325 kfree (data);
326 return -ENODEV;
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
Len Brown087da3b2008-12-30 22:44:33 -0500330 "%04x:%02x\n", (char *)buffer.pointer,
331 id->segment, id->bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
334 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400335 ACPI_EXCEPTION((AE_INFO, status,
336 "Unable to attach ACPI-PCI context to device %s",
Len Brown087da3b2008-12-30 22:44:33 -0500337 (char *)buffer.pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 result = -ENODEV;
339 goto end;
340 }
341
Len Brown4be44fc2005-08-05 00:44:28 -0400342 end:
Len Brown087da3b2008-12-30 22:44:33 -0500343 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (result != 0)
345 kfree(data);
346
Patrick Mocheld550d982006-06-27 00:41:40 -0400347 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}