blob: 388300de005d37df692f142649e0e1a1ab75d9c0 [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 Bunk8713cbe2005-09-02 17:16:48 -040047static void acpi_pci_data_handler(acpi_handle handle, u32 function,
48 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 /* TBD: Anything we need to do here? */
52
Patrick Mocheld550d982006-06-27 00:41:40 -040053 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/**
Rajesh Shah4ce448e2005-04-28 00:25:53 -070057 * acpi_get_pci_id
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * ------------------
59 * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
60 * to resolve PCI information for ACPI-PCI devices defined in the namespace.
61 * This typically occurs when resolving PCI operation region information.
62 */
Len Brown4be44fc2005-08-05 00:44:28 -040063acpi_status acpi_get_pci_id(acpi_handle handle, struct acpi_pci_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Len Brown4be44fc2005-08-05 00:44:28 -040065 int result = 0;
66 acpi_status status = AE_OK;
67 struct acpi_device *device = NULL;
68 struct acpi_pci_data *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 if (!id)
Patrick Mocheld550d982006-06-27 00:41:40 -040072 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 result = acpi_bus_get_device(handle, &device);
75 if (result) {
Len Brown64684632006-06-26 23:41:38 -040076 printk(KERN_ERR PREFIX
77 "Invalid ACPI Bus context for device %s\n",
78 acpi_device_bid(device));
Patrick Mocheld550d982006-06-27 00:41:40 -040079 return AE_NOT_EXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
Len Brown4be44fc2005-08-05 00:44:28 -040082 status = acpi_get_data(handle, acpi_pci_data_handler, (void **)&data);
Rajesh Shah4ce448e2005-04-28 00:25:53 -070083 if (ACPI_FAILURE(status) || !data) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040084 ACPI_EXCEPTION((AE_INFO, status,
85 "Invalid ACPI-PCI context for device %s",
86 acpi_device_bid(device)));
Patrick Mocheld550d982006-06-27 00:41:40 -040087 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89
90 *id = data->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Len Brown4be44fc2005-08-05 00:44:28 -040092 /*
93 id->segment = data->id.segment;
94 id->bus = data->id.bus;
95 id->device = data->id.device;
96 id->function = data->id.function;
97 */
98
99 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
100 "Device %s has PCI address %02x:%02x:%02x.%02x\n",
101 acpi_device_bid(device), id->segment, id->bus,
102 id->device, id->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Patrick Mocheld550d982006-06-27 00:41:40 -0400104 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Len Brown4be44fc2005-08-05 00:44:28 -0400106
Rajesh Shah4ce448e2005-04-28 00:25:53 -0700107EXPORT_SYMBOL(acpi_get_pci_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Len Brown4be44fc2005-08-05 00:44:28 -0400109int acpi_pci_bind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Len Brown4be44fc2005-08-05 00:44:28 -0400111 int result = 0;
112 acpi_status status = AE_OK;
113 struct acpi_pci_data *data = NULL;
114 struct acpi_pci_data *pdata = NULL;
115 char *pathname = NULL;
116 struct acpi_buffer buffer = { 0, NULL };
117 acpi_handle handle = NULL;
118 struct pci_dev *dev;
119 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (!device || !device->parent)
Patrick Mocheld550d982006-06-27 00:41:40 -0400123 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Burman Yan36bcbec2006-12-19 12:56:11 -0800125 pathname = kzalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400126 if (!pathname)
Patrick Mocheld550d982006-06-27 00:41:40 -0400127 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 buffer.length = ACPI_PATHNAME_MAX;
129 buffer.pointer = pathname;
130
Burman Yan36bcbec2006-12-19 12:56:11 -0800131 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400132 if (!data) {
133 kfree(pathname);
Patrick Mocheld550d982006-06-27 00:41:40 -0400134 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400138 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
139 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /*
142 * Segment & Bus
143 * -------------
144 * These are obtained via the parent device's ACPI-PCI context.
145 */
Len Brown4be44fc2005-08-05 00:44:28 -0400146 status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
147 (void **)&pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400149 ACPI_EXCEPTION((AE_INFO, status,
150 "Invalid ACPI-PCI context for parent device %s",
151 acpi_device_bid(device->parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 result = -ENODEV;
153 goto end;
154 }
155 data->id.segment = pdata->id.segment;
156 data->id.bus = pdata->bus->number;
157
158 /*
159 * Device & Function
160 * -----------------
161 * These are simply obtained from the device's _ADR method. Note
162 * that a value of zero is valid.
163 */
164 data->id.device = device->pnp.bus_address >> 16;
165 data->id.function = device->pnp.bus_address & 0xFFFF;
166
167 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400168 data->id.segment, data->id.bus, data->id.device,
169 data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /*
172 * TBD: Support slot devices (e.g. function=0xFFFF).
173 */
174
175 /*
176 * Locate PCI Device
177 * -----------------
178 * Locate matching device in PCI namespace. If it doesn't exist
179 * this typically means that the device isn't currently inserted
180 * (e.g. docking station, port replicator, etc.).
Rajesh Shahc431ada2005-04-28 00:25:45 -0700181 * We cannot simply search the global pci device list, since
182 * PCI devices are added to the global pci list when the root
183 * bridge start ops are run, which may not have happened yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
Rajesh Shahc431ada2005-04-28 00:25:45 -0700185 bus = pci_find_bus(data->id.segment, data->id.bus);
186 if (bus) {
187 list_for_each_entry(dev, &bus->devices, bus_list) {
188 if (dev->devfn == PCI_DEVFN(data->id.device,
Len Brown4be44fc2005-08-05 00:44:28 -0400189 data->id.function)) {
Rajesh Shahc431ada2005-04-28 00:25:45 -0700190 data->dev = dev;
191 break;
192 }
193 }
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!data->dev) {
Len Brown4be44fc2005-08-05 00:44:28 -0400196 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
197 "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
198 data->id.segment, data->id.bus,
199 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 result = -ENODEV;
201 goto end;
202 }
203 if (!data->dev->bus) {
Len Brown64684632006-06-26 23:41:38 -0400204 printk(KERN_ERR PREFIX
205 "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400206 data->id.segment, data->id.bus,
Len Brown64684632006-06-26 23:41:38 -0400207 data->id.device, data->id.function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 result = -ENODEV;
209 goto end;
210 }
211
212 /*
213 * PCI Bridge?
214 * -----------
215 * If so, set the 'bus' field and install the 'bind' function to
216 * facilitate callbacks for all of its children.
217 */
218 if (data->dev->subordinate) {
Len Brown4be44fc2005-08-05 00:44:28 -0400219 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
220 "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
221 data->id.segment, data->id.bus,
222 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 data->bus = data->dev->subordinate;
224 device->ops.bind = acpi_pci_bind;
225 device->ops.unbind = acpi_pci_unbind;
226 }
227
228 /*
229 * Attach ACPI-PCI Context
230 * -----------------------
231 * Thus binding the ACPI and PCI devices.
232 */
233 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
234 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400235 ACPI_EXCEPTION((AE_INFO, status,
236 "Unable to attach ACPI-PCI context to device %s",
237 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 result = -ENODEV;
239 goto end;
240 }
241
242 /*
243 * PCI Routing Table
244 * -----------------
245 * Evaluate and parse _PRT, if exists. This code is independent of
246 * PCI bridges (above) to allow parsing of _PRT objects within the
247 * scope of non-bridge devices. Note that _PRTs within the scope of
248 * a PCI bridge assume the bridge's subordinate bus number.
249 *
250 * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
251 */
252 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
253 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400254 if (data->bus) /* PCI-PCI bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 acpi_pci_irq_add_prt(device->handle, data->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400256 data->bus->number);
257 else /* non-bridge PCI device */
258 acpi_pci_irq_add_prt(device->handle, data->id.segment,
259 data->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kfree(pathname);
264 if (result)
265 kfree(data);
266
Patrick Mocheld550d982006-06-27 00:41:40 -0400267 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Len Brown4be44fc2005-08-05 00:44:28 -0400270int acpi_pci_unbind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Len Brown4be44fc2005-08-05 00:44:28 -0400272 int result = 0;
273 acpi_status status = AE_OK;
274 struct acpi_pci_data *data = NULL;
275 char *pathname = NULL;
276 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (!device || !device->parent)
Patrick Mocheld550d982006-06-27 00:41:40 -0400280 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Burman Yan36bcbec2006-12-19 12:56:11 -0800282 pathname = kzalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400283 if (!pathname)
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 buffer.length = ACPI_PATHNAME_MAX;
287 buffer.pointer = pathname;
288 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
289 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400290 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 kfree(pathname);
292
Len Brown4be44fc2005-08-05 00:44:28 -0400293 status =
294 acpi_get_data(device->handle, acpi_pci_data_handler,
295 (void **)&data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 result = -ENODEV;
298 goto end;
299 }
300
301 status = acpi_detach_data(device->handle, acpi_pci_data_handler);
302 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400303 ACPI_EXCEPTION((AE_INFO, status,
304 "Unable to detach data from device %s",
305 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 result = -ENODEV;
307 goto end;
308 }
309 if (data->dev->subordinate) {
310 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
311 }
312 kfree(data);
313
Len Brown4be44fc2005-08-05 00:44:28 -0400314 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Len Brown4be44fc2005-08-05 00:44:28 -0400318int
319acpi_pci_bind_root(struct acpi_device *device,
320 struct acpi_pci_id *id, struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Len Brown4be44fc2005-08-05 00:44:28 -0400322 int result = 0;
323 acpi_status status = AE_OK;
324 struct acpi_pci_data *data = NULL;
325 char *pathname = NULL;
326 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Burman Yan36bcbec2006-12-19 12:56:11 -0800328 pathname = kzalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400329 if (!pathname)
Patrick Mocheld550d982006-06-27 00:41:40 -0400330 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 buffer.length = ACPI_PATHNAME_MAX;
333 buffer.pointer = pathname;
334
Len Brown4be44fc2005-08-05 00:44:28 -0400335 if (!device || !id || !bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 kfree(pathname);
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
Burman Yan36bcbec2006-12-19 12:56:11 -0800340 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400341 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 kfree(pathname);
Patrick Mocheld550d982006-06-27 00:41:40 -0400343 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 data->id = *id;
347 data->bus = bus;
348 device->ops.bind = acpi_pci_bind;
349 device->ops.unbind = acpi_pci_unbind;
350
351 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
352
353 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
Len Brown4be44fc2005-08-05 00:44:28 -0400354 "%02x:%02x\n", pathname, id->segment, id->bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
357 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400358 ACPI_EXCEPTION((AE_INFO, status,
359 "Unable to attach ACPI-PCI context to device %s",
360 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 result = -ENODEV;
362 goto end;
363 }
364
Len Brown4be44fc2005-08-05 00:44:28 -0400365 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 kfree(pathname);
367 if (result != 0)
368 kfree(data);
369
Patrick Mocheld550d982006-06-27 00:41:40 -0400370 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}