blob: a4955685e4c01523a7d1d0747ef248f053f2fc18 [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 Brown4be44fc2005-08-05 00:44:28 -040039ACPI_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
Len Brown4be44fc2005-08-05 00:44:28 -040047void acpi_pci_data_handler(acpi_handle handle, u32 function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 ACPI_FUNCTION_TRACE("acpi_pci_data_handler");
50
51 /* TBD: Anything we need to do here? */
52
53 return_VOID;
54}
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
Rajesh Shah4ce448e2005-04-28 00:25:53 -070070 ACPI_FUNCTION_TRACE("acpi_get_pci_id");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 if (!id)
73 return_ACPI_STATUS(AE_BAD_PARAMETER);
74
75 result = acpi_bus_get_device(handle, &device);
76 if (result) {
Len Brown4be44fc2005-08-05 00:44:28 -040077 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
78 "Invalid ACPI Bus context for device %s\n",
79 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return_ACPI_STATUS(AE_NOT_EXIST);
81 }
82
Len Brown4be44fc2005-08-05 00:44:28 -040083 status = acpi_get_data(handle, acpi_pci_data_handler, (void **)&data);
Rajesh Shah4ce448e2005-04-28 00:25:53 -070084 if (ACPI_FAILURE(status) || !data) {
Len Brown4be44fc2005-08-05 00:44:28 -040085 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
86 "Invalid ACPI-PCI context for device %s\n",
87 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return_ACPI_STATUS(status);
89 }
90
91 *id = data->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Len Brown4be44fc2005-08-05 00:44:28 -040093 /*
94 id->segment = data->id.segment;
95 id->bus = data->id.bus;
96 id->device = data->id.device;
97 id->function = data->id.function;
98 */
99
100 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
101 "Device %s has PCI address %02x:%02x:%02x.%02x\n",
102 acpi_device_bid(device), id->segment, id->bus,
103 id->device, id->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 return_ACPI_STATUS(AE_OK);
106}
Len Brown4be44fc2005-08-05 00:44:28 -0400107
Rajesh Shah4ce448e2005-04-28 00:25:53 -0700108EXPORT_SYMBOL(acpi_get_pci_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Len Brown4be44fc2005-08-05 00:44:28 -0400110int acpi_pci_bind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Len Brown4be44fc2005-08-05 00:44:28 -0400112 int result = 0;
113 acpi_status status = AE_OK;
114 struct acpi_pci_data *data = NULL;
115 struct acpi_pci_data *pdata = NULL;
116 char *pathname = NULL;
117 struct acpi_buffer buffer = { 0, NULL };
118 acpi_handle handle = NULL;
119 struct pci_dev *dev;
120 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 ACPI_FUNCTION_TRACE("acpi_pci_bind");
123
124 if (!device || !device->parent)
125 return_VALUE(-EINVAL);
126
127 pathname = kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400128 if (!pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return_VALUE(-ENOMEM);
130 memset(pathname, 0, ACPI_PATHNAME_MAX);
131 buffer.length = ACPI_PATHNAME_MAX;
132 buffer.pointer = pathname;
133
134 data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400135 if (!data) {
136 kfree(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return_VALUE(-ENOMEM);
138 }
139 memset(data, 0, sizeof(struct acpi_pci_data));
140
141 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400142 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
143 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /*
146 * Segment & Bus
147 * -------------
148 * These are obtained via the parent device's ACPI-PCI context.
149 */
Len Brown4be44fc2005-08-05 00:44:28 -0400150 status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
151 (void **)&pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
Len Brown4be44fc2005-08-05 00:44:28 -0400153 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
154 "Invalid ACPI-PCI context for parent device %s\n",
155 acpi_device_bid(device->parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 result = -ENODEV;
157 goto end;
158 }
159 data->id.segment = pdata->id.segment;
160 data->id.bus = pdata->bus->number;
161
162 /*
163 * Device & Function
164 * -----------------
165 * These are simply obtained from the device's _ADR method. Note
166 * that a value of zero is valid.
167 */
168 data->id.device = device->pnp.bus_address >> 16;
169 data->id.function = device->pnp.bus_address & 0xFFFF;
170
171 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400172 data->id.segment, data->id.bus, data->id.device,
173 data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /*
176 * TBD: Support slot devices (e.g. function=0xFFFF).
177 */
178
179 /*
180 * Locate PCI Device
181 * -----------------
182 * Locate matching device in PCI namespace. If it doesn't exist
183 * this typically means that the device isn't currently inserted
184 * (e.g. docking station, port replicator, etc.).
Rajesh Shahc431ada2005-04-28 00:25:45 -0700185 * We cannot simply search the global pci device list, since
186 * PCI devices are added to the global pci list when the root
187 * bridge start ops are run, which may not have happened yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
Rajesh Shahc431ada2005-04-28 00:25:45 -0700189 bus = pci_find_bus(data->id.segment, data->id.bus);
190 if (bus) {
191 list_for_each_entry(dev, &bus->devices, bus_list) {
192 if (dev->devfn == PCI_DEVFN(data->id.device,
Len Brown4be44fc2005-08-05 00:44:28 -0400193 data->id.function)) {
Rajesh Shahc431ada2005-04-28 00:25:45 -0700194 data->dev = dev;
195 break;
196 }
197 }
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!data->dev) {
Len Brown4be44fc2005-08-05 00:44:28 -0400200 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
201 "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
202 data->id.segment, data->id.bus,
203 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 result = -ENODEV;
205 goto end;
206 }
207 if (!data->dev->bus) {
Len Brown4be44fc2005-08-05 00:44:28 -0400208 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
209 "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
210 data->id.segment, data->id.bus,
211 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 result = -ENODEV;
213 goto end;
214 }
215
216 /*
217 * PCI Bridge?
218 * -----------
219 * If so, set the 'bus' field and install the 'bind' function to
220 * facilitate callbacks for all of its children.
221 */
222 if (data->dev->subordinate) {
Len Brown4be44fc2005-08-05 00:44:28 -0400223 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
224 "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
225 data->id.segment, data->id.bus,
226 data->id.device, data->id.function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 data->bus = data->dev->subordinate;
228 device->ops.bind = acpi_pci_bind;
229 device->ops.unbind = acpi_pci_unbind;
230 }
231
232 /*
233 * Attach ACPI-PCI Context
234 * -----------------------
235 * Thus binding the ACPI and PCI devices.
236 */
237 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
238 if (ACPI_FAILURE(status)) {
239 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -0400240 "Unable to attach ACPI-PCI context to device %s\n",
241 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 result = -ENODEV;
243 goto end;
244 }
245
246 /*
247 * PCI Routing Table
248 * -----------------
249 * Evaluate and parse _PRT, if exists. This code is independent of
250 * PCI bridges (above) to allow parsing of _PRT objects within the
251 * scope of non-bridge devices. Note that _PRTs within the scope of
252 * a PCI bridge assume the bridge's subordinate bus number.
253 *
254 * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
255 */
256 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
257 if (ACPI_SUCCESS(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400258 if (data->bus) /* PCI-PCI bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 acpi_pci_irq_add_prt(device->handle, data->id.segment,
Len Brown4be44fc2005-08-05 00:44:28 -0400260 data->bus->number);
261 else /* non-bridge PCI device */
262 acpi_pci_irq_add_prt(device->handle, data->id.segment,
263 data->id.bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Len Brown4be44fc2005-08-05 00:44:28 -0400266 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 kfree(pathname);
268 if (result)
269 kfree(data);
270
271 return_VALUE(result);
272}
273
Len Brown4be44fc2005-08-05 00:44:28 -0400274int acpi_pci_unbind(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Len Brown4be44fc2005-08-05 00:44:28 -0400276 int result = 0;
277 acpi_status status = AE_OK;
278 struct acpi_pci_data *data = NULL;
279 char *pathname = NULL;
280 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 ACPI_FUNCTION_TRACE("acpi_pci_unbind");
283
284 if (!device || !device->parent)
285 return_VALUE(-EINVAL);
286
Len Brown4be44fc2005-08-05 00:44:28 -0400287 pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
288 if (!pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return_VALUE(-ENOMEM);
290 memset(pathname, 0, ACPI_PATHNAME_MAX);
291
292 buffer.length = ACPI_PATHNAME_MAX;
293 buffer.pointer = pathname;
294 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
295 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400296 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 kfree(pathname);
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 status =
300 acpi_get_data(device->handle, acpi_pci_data_handler,
301 (void **)&data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (ACPI_FAILURE(status)) {
303 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -0400304 "Unable to get data from device %s\n",
305 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 result = -ENODEV;
307 goto end;
308 }
309
310 status = acpi_detach_data(device->handle, acpi_pci_data_handler);
311 if (ACPI_FAILURE(status)) {
312 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -0400313 "Unable to detach data from device %s\n",
314 acpi_device_bid(device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 result = -ENODEV;
316 goto end;
317 }
318 if (data->dev->subordinate) {
319 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
320 }
321 kfree(data);
322
Len Brown4be44fc2005-08-05 00:44:28 -0400323 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return_VALUE(result);
325}
326
Len Brown4be44fc2005-08-05 00:44:28 -0400327int
328acpi_pci_bind_root(struct acpi_device *device,
329 struct acpi_pci_id *id, struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Len Brown4be44fc2005-08-05 00:44:28 -0400331 int result = 0;
332 acpi_status status = AE_OK;
333 struct acpi_pci_data *data = NULL;
334 char *pathname = NULL;
335 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 ACPI_FUNCTION_TRACE("acpi_pci_bind_root");
338
339 pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400340 if (!pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return_VALUE(-ENOMEM);
342 memset(pathname, 0, ACPI_PATHNAME_MAX);
343
344 buffer.length = ACPI_PATHNAME_MAX;
345 buffer.pointer = pathname;
346
Len Brown4be44fc2005-08-05 00:44:28 -0400347 if (!device || !id || !bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 kfree(pathname);
349 return_VALUE(-EINVAL);
350 }
351
352 data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400353 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 kfree(pathname);
355 return_VALUE(-ENOMEM);
356 }
357 memset(data, 0, sizeof(struct acpi_pci_data));
358
359 data->id = *id;
360 data->bus = bus;
361 device->ops.bind = acpi_pci_bind;
362 device->ops.unbind = acpi_pci_unbind;
363
364 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
365
366 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
Len Brown4be44fc2005-08-05 00:44:28 -0400367 "%02x:%02x\n", pathname, id->segment, id->bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
370 if (ACPI_FAILURE(status)) {
371 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -0400372 "Unable to attach ACPI-PCI context to device %s\n",
373 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 result = -ENODEV;
375 goto end;
376 }
377
Len Brown4be44fc2005-08-05 00:44:28 -0400378 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 kfree(pathname);
380 if (result != 0)
381 kfree(data);
382
383 return_VALUE(result);
384}