blob: 95650f83ce2e35f709f951ca51c9422b821d37d5 [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
Adrian Bunke5685b92007-10-24 18:24:42 +020047static int acpi_pci_unbind(struct acpi_device *device);
48
Adrian Bunk8713cbe2005-09-02 17:16:48 -040049static void acpi_pci_data_handler(acpi_handle handle, u32 function,
50 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 /* TBD: Anything we need to do here? */
54
Patrick Mocheld550d982006-06-27 00:41:40 -040055 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/**
Rajesh Shah4ce448e2005-04-28 00:25:53 -070059 * acpi_get_pci_id
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * ------------------
61 * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
62 * to resolve PCI information for ACPI-PCI devices defined in the namespace.
63 * This typically occurs when resolving PCI operation region information.
64 */
Len Brown4be44fc2005-08-05 00:44:28 -040065acpi_status acpi_get_pci_id(acpi_handle handle, struct acpi_pci_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Len Brown4be44fc2005-08-05 00:44:28 -040067 int result = 0;
68 acpi_status status = AE_OK;
69 struct acpi_device *device = NULL;
70 struct acpi_pci_data *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (!id)
Patrick Mocheld550d982006-06-27 00:41:40 -040074 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 result = acpi_bus_get_device(handle, &device);
77 if (result) {
Len Brown64684632006-06-26 23:41:38 -040078 printk(KERN_ERR PREFIX
79 "Invalid ACPI Bus context for device %s\n",
80 acpi_device_bid(device));
Patrick Mocheld550d982006-06-27 00:41:40 -040081 return AE_NOT_EXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
Len Brown4be44fc2005-08-05 00:44:28 -040084 status = acpi_get_data(handle, acpi_pci_data_handler, (void **)&data);
Rajesh Shah4ce448e2005-04-28 00:25:53 -070085 if (ACPI_FAILURE(status) || !data) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040086 ACPI_EXCEPTION((AE_INFO, status,
87 "Invalid ACPI-PCI context for device %s",
88 acpi_device_bid(device)));
Patrick Mocheld550d982006-06-27 00:41:40 -040089 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
92 *id = data->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Len Brown4be44fc2005-08-05 00:44:28 -040094 /*
95 id->segment = data->id.segment;
96 id->bus = data->id.bus;
97 id->device = data->id.device;
98 id->function = data->id.function;
99 */
100
101 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700102 "Device %s has PCI address %04x:%02x:%02x.%d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400103 acpi_device_bid(device), id->segment, id->bus,
104 id->device, id->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Patrick Mocheld550d982006-06-27 00:41:40 -0400106 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
Len Brown4be44fc2005-08-05 00:44:28 -0400108
Rajesh Shah4ce448e2005-04-28 00:25:53 -0700109EXPORT_SYMBOL(acpi_get_pci_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Len Brown4be44fc2005-08-05 00:44:28 -0400111int acpi_pci_bind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Len Brown4be44fc2005-08-05 00:44:28 -0400113 int result = 0;
Len Brown087da3b2008-12-30 22:44:33 -0500114 acpi_status status;
115 struct acpi_pci_data *data;
116 struct acpi_pci_data *pdata;
117 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
118 acpi_handle handle;
Len Brown4be44fc2005-08-05 00:44:28 -0400119 struct pci_dev *dev;
120 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if (!device || !device->parent)
Patrick Mocheld550d982006-06-27 00:41:40 -0400124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Burman Yan36bcbec2006-12-19 12:56:11 -0800126 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown087da3b2008-12-30 22:44:33 -0500127 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400128 return -ENOMEM;
Len Brown087da3b2008-12-30 22:44:33 -0500129
130 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
131 if (ACPI_FAILURE(status)) {
132 kfree(data);
133 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Len Brown4be44fc2005-08-05 00:44:28 -0400136 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
Len Brown087da3b2008-12-30 22:44:33 -0500137 (char *)buffer.pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /*
140 * Segment & Bus
141 * -------------
142 * These are obtained via the parent device's ACPI-PCI context.
143 */
Len Brown4be44fc2005-08-05 00:44:28 -0400144 status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
145 (void **)&pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400147 ACPI_EXCEPTION((AE_INFO, status,
148 "Invalid ACPI-PCI context for parent device %s",
149 acpi_device_bid(device->parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 result = -ENODEV;
151 goto end;
152 }
153 data->id.segment = pdata->id.segment;
154 data->id.bus = pdata->bus->number;
155
156 /*
157 * Device & Function
158 * -----------------
159 * These are simply obtained from the device's _ADR method. Note
160 * that a value of zero is valid.
161 */
162 data->id.device = device->pnp.bus_address >> 16;
163 data->id.function = device->pnp.bus_address & 0xFFFF;
164
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700165 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %04x:%02x:%02x.%d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400166 data->id.segment, data->id.bus, data->id.device,
167 data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /*
170 * TBD: Support slot devices (e.g. function=0xFFFF).
171 */
172
173 /*
174 * Locate PCI Device
175 * -----------------
176 * Locate matching device in PCI namespace. If it doesn't exist
177 * this typically means that the device isn't currently inserted
178 * (e.g. docking station, port replicator, etc.).
Rajesh Shahc431ada2005-04-28 00:25:45 -0700179 * We cannot simply search the global pci device list, since
180 * PCI devices are added to the global pci list when the root
181 * bridge start ops are run, which may not have happened yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Rajesh Shahc431ada2005-04-28 00:25:45 -0700183 bus = pci_find_bus(data->id.segment, data->id.bus);
184 if (bus) {
185 list_for_each_entry(dev, &bus->devices, bus_list) {
186 if (dev->devfn == PCI_DEVFN(data->id.device,
Len Brown4be44fc2005-08-05 00:44:28 -0400187 data->id.function)) {
Rajesh Shahc431ada2005-04-28 00:25:45 -0700188 data->dev = dev;
189 break;
190 }
191 }
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!data->dev) {
Len Brown4be44fc2005-08-05 00:44:28 -0400194 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700195 "Device %04x:%02x:%02x.%d not present in PCI namespace\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400196 data->id.segment, data->id.bus,
197 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 result = -ENODEV;
199 goto end;
200 }
201 if (!data->dev->bus) {
Len Brown64684632006-06-26 23:41:38 -0400202 printk(KERN_ERR PREFIX
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700203 "Device %04x:%02x:%02x.%d has invalid 'bus' field\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400204 data->id.segment, data->id.bus,
Len Brown64684632006-06-26 23:41:38 -0400205 data->id.device, data->id.function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 result = -ENODEV;
207 goto end;
208 }
209
210 /*
211 * PCI Bridge?
212 * -----------
213 * If so, set the 'bus' field and install the 'bind' function to
214 * facilitate callbacks for all of its children.
215 */
216 if (data->dev->subordinate) {
Len Brown4be44fc2005-08-05 00:44:28 -0400217 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700218 "Device %04x:%02x:%02x.%d is a PCI bridge\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400219 data->id.segment, data->id.bus,
220 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 data->bus = data->dev->subordinate;
222 device->ops.bind = acpi_pci_bind;
223 device->ops.unbind = acpi_pci_unbind;
224 }
225
226 /*
227 * Attach ACPI-PCI Context
228 * -----------------------
229 * Thus binding the ACPI and PCI devices.
230 */
231 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
232 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400233 ACPI_EXCEPTION((AE_INFO, status,
234 "Unable to attach ACPI-PCI context to device %s",
235 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 result = -ENODEV;
237 goto end;
238 }
239
240 /*
241 * PCI Routing Table
242 * -----------------
243 * Evaluate and parse _PRT, if exists. This code is independent of
244 * PCI bridges (above) to allow parsing of _PRT objects within the
245 * scope of non-bridge devices. Note that _PRTs within the scope of
246 * a PCI bridge assume the bridge's subordinate bus number.
247 *
248 * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
249 */
250 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
251 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400252 if (data->bus) /* PCI-PCI bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 acpi_pci_irq_add_prt(device->handle, data->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400254 data->bus->number);
255 else /* non-bridge PCI device */
256 acpi_pci_irq_add_prt(device->handle, data->id.segment,
257 data->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Len Brown4be44fc2005-08-05 00:44:28 -0400260 end:
Len Brown087da3b2008-12-30 22:44:33 -0500261 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (result)
263 kfree(data);
264
Patrick Mocheld550d982006-06-27 00:41:40 -0400265 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Adrian Bunke5685b92007-10-24 18:24:42 +0200268static int acpi_pci_unbind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Len Brown4be44fc2005-08-05 00:44:28 -0400270 int result = 0;
Len Brown087da3b2008-12-30 22:44:33 -0500271 acpi_status status;
272 struct acpi_pci_data *data;
273 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (!device || !device->parent)
Patrick Mocheld550d982006-06-27 00:41:40 -0400277 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Len Brown087da3b2008-12-30 22:44:33 -0500279 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
280 if (ACPI_FAILURE(status))
281 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
Len Brown087da3b2008-12-30 22:44:33 -0500284 (char *) buffer.pointer));
285 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Len Brown4be44fc2005-08-05 00:44:28 -0400287 status =
288 acpi_get_data(device->handle, acpi_pci_data_handler,
289 (void **)&data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 result = -ENODEV;
292 goto end;
293 }
294
295 status = acpi_detach_data(device->handle, acpi_pci_data_handler);
296 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400297 ACPI_EXCEPTION((AE_INFO, status,
298 "Unable to detach data from device %s",
299 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 result = -ENODEV;
301 goto end;
302 }
303 if (data->dev->subordinate) {
304 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
305 }
306 kfree(data);
307
Len Brown4be44fc2005-08-05 00:44:28 -0400308 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400309 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Len Brown4be44fc2005-08-05 00:44:28 -0400312int
313acpi_pci_bind_root(struct acpi_device *device,
314 struct acpi_pci_id *id, struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Len Brown4be44fc2005-08-05 00:44:28 -0400316 int result = 0;
Len Brown087da3b2008-12-30 22:44:33 -0500317 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400318 struct acpi_pci_data *data = NULL;
Len Brown087da3b2008-12-30 22:44:33 -0500319 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Len Brown4be44fc2005-08-05 00:44:28 -0400321 if (!device || !id || !bus) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Burman Yan36bcbec2006-12-19 12:56:11 -0800325 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown087da3b2008-12-30 22:44:33 -0500326 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400327 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 data->id = *id;
330 data->bus = bus;
331 device->ops.bind = acpi_pci_bind;
332 device->ops.unbind = acpi_pci_unbind;
333
Len Brown087da3b2008-12-30 22:44:33 -0500334 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
335 if (ACPI_FAILURE(status)) {
336 kfree (data);
337 return -ENODEV;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
Len Brown087da3b2008-12-30 22:44:33 -0500341 "%04x:%02x\n", (char *)buffer.pointer,
342 id->segment, id->bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
345 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400346 ACPI_EXCEPTION((AE_INFO, status,
347 "Unable to attach ACPI-PCI context to device %s",
Len Brown087da3b2008-12-30 22:44:33 -0500348 (char *)buffer.pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 result = -ENODEV;
350 goto end;
351 }
352
Len Brown4be44fc2005-08-05 00:44:28 -0400353 end:
Len Brown087da3b2008-12-30 22:44:33 -0500354 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (result != 0)
356 kfree(data);
357
Patrick Mocheld550d982006-06-27 00:41:40 -0400358 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}