blob: bc46de3d967f62904962e06bf9beef26095a9a78 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (!device || !device->parent)
Patrick Mocheld550d982006-06-27 00:41:40 -0400121 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Burman Yan36bcbec2006-12-19 12:56:11 -0800123 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown087da3b2008-12-30 22:44:33 -0500124 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400125 return -ENOMEM;
Len Brown087da3b2008-12-30 22:44:33 -0500126
127 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
128 if (ACPI_FAILURE(status)) {
129 kfree(data);
130 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Len Brown4be44fc2005-08-05 00:44:28 -0400133 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
Len Brown087da3b2008-12-30 22:44:33 -0500134 (char *)buffer.pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /*
137 * Segment & Bus
138 * -------------
139 * These are obtained via the parent device's ACPI-PCI context.
140 */
Len Brown4be44fc2005-08-05 00:44:28 -0400141 status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
142 (void **)&pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400144 ACPI_EXCEPTION((AE_INFO, status,
145 "Invalid ACPI-PCI context for parent device %s",
146 acpi_device_bid(device->parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 result = -ENODEV;
148 goto end;
149 }
150 data->id.segment = pdata->id.segment;
151 data->id.bus = pdata->bus->number;
152
153 /*
154 * Device & Function
155 * -----------------
156 * These are simply obtained from the device's _ADR method. Note
157 * that a value of zero is valid.
158 */
159 data->id.device = device->pnp.bus_address >> 16;
160 data->id.function = device->pnp.bus_address & 0xFFFF;
161
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700162 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %04x:%02x:%02x.%d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400163 data->id.segment, data->id.bus, data->id.device,
164 data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /*
167 * TBD: Support slot devices (e.g. function=0xFFFF).
168 */
169
170 /*
171 * Locate PCI Device
172 * -----------------
173 * Locate matching device in PCI namespace. If it doesn't exist
174 * this typically means that the device isn't currently inserted
175 * (e.g. docking station, port replicator, etc.).
176 */
Kenji Kaneshigedacd2542009-05-26 09:08:03 +0900177 data->dev = pci_get_slot(pdata->bus,
178 PCI_DEVFN(data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!data->dev) {
Len Brown4be44fc2005-08-05 00:44:28 -0400180 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700181 "Device %04x:%02x:%02x.%d not present in PCI namespace\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400182 data->id.segment, data->id.bus,
183 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 result = -ENODEV;
185 goto end;
186 }
187 if (!data->dev->bus) {
Len Brown64684632006-06-26 23:41:38 -0400188 printk(KERN_ERR PREFIX
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700189 "Device %04x:%02x:%02x.%d has invalid 'bus' field\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400190 data->id.segment, data->id.bus,
Len Brown64684632006-06-26 23:41:38 -0400191 data->id.device, data->id.function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 result = -ENODEV;
193 goto end;
194 }
195
196 /*
197 * PCI Bridge?
198 * -----------
199 * If so, set the 'bus' field and install the 'bind' function to
200 * facilitate callbacks for all of its children.
201 */
202 if (data->dev->subordinate) {
Len Brown4be44fc2005-08-05 00:44:28 -0400203 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700204 "Device %04x:%02x:%02x.%d is a PCI bridge\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400205 data->id.segment, data->id.bus,
206 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 data->bus = data->dev->subordinate;
208 device->ops.bind = acpi_pci_bind;
209 device->ops.unbind = acpi_pci_unbind;
210 }
211
212 /*
213 * Attach ACPI-PCI Context
214 * -----------------------
215 * Thus binding the ACPI and PCI devices.
216 */
217 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
218 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400219 ACPI_EXCEPTION((AE_INFO, status,
220 "Unable to attach ACPI-PCI context to device %s",
221 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 result = -ENODEV;
223 goto end;
224 }
225
226 /*
227 * PCI Routing Table
228 * -----------------
229 * Evaluate and parse _PRT, if exists. This code is independent of
230 * PCI bridges (above) to allow parsing of _PRT objects within the
231 * scope of non-bridge devices. Note that _PRTs within the scope of
232 * a PCI bridge assume the bridge's subordinate bus number.
233 *
234 * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
235 */
236 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
237 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400238 if (data->bus) /* PCI-PCI bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 acpi_pci_irq_add_prt(device->handle, data->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400240 data->bus->number);
241 else /* non-bridge PCI device */
242 acpi_pci_irq_add_prt(device->handle, data->id.segment,
243 data->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
Len Brown4be44fc2005-08-05 00:44:28 -0400246 end:
Len Brown087da3b2008-12-30 22:44:33 -0500247 kfree(buffer.pointer);
Kenji Kaneshigedacd2542009-05-26 09:08:03 +0900248 if (result) {
249 pci_dev_put(data->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 kfree(data);
Kenji Kaneshigedacd2542009-05-26 09:08:03 +0900251 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400252 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Adrian Bunke5685b92007-10-24 18:24:42 +0200255static int acpi_pci_unbind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Len Brown4be44fc2005-08-05 00:44:28 -0400257 int result = 0;
Len Brown087da3b2008-12-30 22:44:33 -0500258 acpi_status status;
259 struct acpi_pci_data *data;
260 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (!device || !device->parent)
Patrick Mocheld550d982006-06-27 00:41:40 -0400264 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Len Brown087da3b2008-12-30 22:44:33 -0500266 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
267 if (ACPI_FAILURE(status))
268 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
Len Brown087da3b2008-12-30 22:44:33 -0500271 (char *) buffer.pointer));
272 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Len Brown4be44fc2005-08-05 00:44:28 -0400274 status =
275 acpi_get_data(device->handle, acpi_pci_data_handler,
276 (void **)&data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 result = -ENODEV;
279 goto end;
280 }
281
282 status = acpi_detach_data(device->handle, acpi_pci_data_handler);
283 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400284 ACPI_EXCEPTION((AE_INFO, status,
285 "Unable to detach data from device %s",
286 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 result = -ENODEV;
288 goto end;
289 }
290 if (data->dev->subordinate) {
291 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
292 }
Kenji Kaneshigedacd2542009-05-26 09:08:03 +0900293 pci_dev_put(data->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 kfree(data);
295
Len Brown4be44fc2005-08-05 00:44:28 -0400296 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Len Brown4be44fc2005-08-05 00:44:28 -0400300int
301acpi_pci_bind_root(struct acpi_device *device,
302 struct acpi_pci_id *id, struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Len Brown4be44fc2005-08-05 00:44:28 -0400304 int result = 0;
Len Brown087da3b2008-12-30 22:44:33 -0500305 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400306 struct acpi_pci_data *data = NULL;
Len Brown087da3b2008-12-30 22:44:33 -0500307 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Len Brown4be44fc2005-08-05 00:44:28 -0400309 if (!device || !id || !bus) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
Burman Yan36bcbec2006-12-19 12:56:11 -0800313 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown087da3b2008-12-30 22:44:33 -0500314 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 data->id = *id;
318 data->bus = bus;
319 device->ops.bind = acpi_pci_bind;
320 device->ops.unbind = acpi_pci_unbind;
321
Len Brown087da3b2008-12-30 22:44:33 -0500322 status = acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
323 if (ACPI_FAILURE(status)) {
324 kfree (data);
325 return -ENODEV;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
Len Brown087da3b2008-12-30 22:44:33 -0500329 "%04x:%02x\n", (char *)buffer.pointer,
330 id->segment, id->bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
333 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400334 ACPI_EXCEPTION((AE_INFO, status,
335 "Unable to attach ACPI-PCI context to device %s",
Len Brown087da3b2008-12-30 22:44:33 -0500336 (char *)buffer.pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 result = -ENODEV;
338 goto end;
339 }
340
Len Brown4be44fc2005-08-05 00:44:28 -0400341 end:
Len Brown087da3b2008-12-30 22:44:33 -0500342 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (result != 0)
344 kfree(data);
345
Patrick Mocheld550d982006-06-27 00:41:40 -0400346 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}