Merge branch 'bkl-removal' of git://git.lwn.net/linux-2.6
[deliverable/linux.git] / drivers / acpi / pci_bind.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4 38#define _COMPONENT ACPI_PCI_COMPONENT
f52fd66d 39ACPI_MODULE_NAME("pci_bind");
1da177e4
LT
40
41struct acpi_pci_data {
4be44fcd
LB
42 struct acpi_pci_id id;
43 struct pci_bus *bus;
44 struct pci_dev *dev;
1da177e4
LT
45};
46
e5685b9d
AB
47static int acpi_pci_unbind(struct acpi_device *device);
48
8713cbef
AB
49static void acpi_pci_data_handler(acpi_handle handle, u32 function,
50 void *context)
1da177e4 51{
1da177e4
LT
52
53 /* TBD: Anything we need to do here? */
54
d550d98d 55 return;
1da177e4
LT
56}
57
1da177e4 58/**
4ce448e5 59 * acpi_get_pci_id
1da177e4
LT
60 * ------------------
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 */
4be44fcd 65acpi_status acpi_get_pci_id(acpi_handle handle, struct acpi_pci_id *id)
1da177e4 66{
4be44fcd
LB
67 int result = 0;
68 acpi_status status = AE_OK;
69 struct acpi_device *device = NULL;
70 struct acpi_pci_data *data = NULL;
1da177e4 71
1da177e4
LT
72
73 if (!id)
d550d98d 74 return AE_BAD_PARAMETER;
1da177e4
LT
75
76 result = acpi_bus_get_device(handle, &device);
77 if (result) {
6468463a
LB
78 printk(KERN_ERR PREFIX
79 "Invalid ACPI Bus context for device %s\n",
80 acpi_device_bid(device));
d550d98d 81 return AE_NOT_EXIST;
1da177e4
LT
82 }
83
4be44fcd 84 status = acpi_get_data(handle, acpi_pci_data_handler, (void **)&data);
4ce448e5 85 if (ACPI_FAILURE(status) || !data) {
a6fc6720
TR
86 ACPI_EXCEPTION((AE_INFO, status,
87 "Invalid ACPI-PCI context for device %s",
88 acpi_device_bid(device)));
d550d98d 89 return status;
1da177e4
LT
90 }
91
92 *id = data->id;
4be44fcd 93
1da177e4 94 /*
4be44fcd
LB
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 */
1da177e4 100
4be44fcd
LB
101 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
102 "Device %s has PCI address %02x:%02x:%02x.%02x\n",
103 acpi_device_bid(device), id->segment, id->bus,
104 id->device, id->function));
1da177e4 105
d550d98d 106 return AE_OK;
1da177e4 107}
4be44fcd 108
4ce448e5 109EXPORT_SYMBOL(acpi_get_pci_id);
1da177e4 110
4be44fcd 111int acpi_pci_bind(struct acpi_device *device)
1da177e4 112{
4be44fcd
LB
113 int result = 0;
114 acpi_status status = AE_OK;
115 struct acpi_pci_data *data = NULL;
116 struct acpi_pci_data *pdata = NULL;
117 char *pathname = NULL;
118 struct acpi_buffer buffer = { 0, NULL };
119 acpi_handle handle = NULL;
120 struct pci_dev *dev;
121 struct pci_bus *bus;
1da177e4 122
1da177e4
LT
123
124 if (!device || !device->parent)
d550d98d 125 return -EINVAL;
1da177e4 126
36bcbec7 127 pathname = kzalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
4be44fcd 128 if (!pathname)
d550d98d 129 return -ENOMEM;
1da177e4
LT
130 buffer.length = ACPI_PATHNAME_MAX;
131 buffer.pointer = pathname;
132
36bcbec7 133 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
4be44fcd
LB
134 if (!data) {
135 kfree(pathname);
d550d98d 136 return -ENOMEM;
1da177e4 137 }
1da177e4
LT
138
139 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
4be44fcd
LB
140 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
141 pathname));
1da177e4
LT
142
143 /*
144 * Segment & Bus
145 * -------------
146 * These are obtained via the parent device's ACPI-PCI context.
147 */
4be44fcd
LB
148 status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
149 (void **)&pdata);
1da177e4 150 if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
a6fc6720
TR
151 ACPI_EXCEPTION((AE_INFO, status,
152 "Invalid ACPI-PCI context for parent device %s",
153 acpi_device_bid(device->parent)));
1da177e4
LT
154 result = -ENODEV;
155 goto end;
156 }
157 data->id.segment = pdata->id.segment;
158 data->id.bus = pdata->bus->number;
159
160 /*
161 * Device & Function
162 * -----------------
163 * These are simply obtained from the device's _ADR method. Note
164 * that a value of zero is valid.
165 */
166 data->id.device = device->pnp.bus_address >> 16;
167 data->id.function = device->pnp.bus_address & 0xFFFF;
168
169 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
4be44fcd
LB
170 data->id.segment, data->id.bus, data->id.device,
171 data->id.function));
1da177e4
LT
172
173 /*
174 * TBD: Support slot devices (e.g. function=0xFFFF).
175 */
176
177 /*
178 * Locate PCI Device
179 * -----------------
180 * Locate matching device in PCI namespace. If it doesn't exist
181 * this typically means that the device isn't currently inserted
182 * (e.g. docking station, port replicator, etc.).
c431ada4
RS
183 * We cannot simply search the global pci device list, since
184 * PCI devices are added to the global pci list when the root
185 * bridge start ops are run, which may not have happened yet.
1da177e4 186 */
c431ada4
RS
187 bus = pci_find_bus(data->id.segment, data->id.bus);
188 if (bus) {
189 list_for_each_entry(dev, &bus->devices, bus_list) {
190 if (dev->devfn == PCI_DEVFN(data->id.device,
4be44fcd 191 data->id.function)) {
c431ada4
RS
192 data->dev = dev;
193 break;
194 }
195 }
196 }
1da177e4 197 if (!data->dev) {
4be44fcd
LB
198 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
199 "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
200 data->id.segment, data->id.bus,
201 data->id.device, data->id.function));
1da177e4
LT
202 result = -ENODEV;
203 goto end;
204 }
205 if (!data->dev->bus) {
6468463a
LB
206 printk(KERN_ERR PREFIX
207 "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
a6fc6720 208 data->id.segment, data->id.bus,
6468463a 209 data->id.device, data->id.function);
1da177e4
LT
210 result = -ENODEV;
211 goto end;
212 }
213
214 /*
215 * PCI Bridge?
216 * -----------
217 * If so, set the 'bus' field and install the 'bind' function to
218 * facilitate callbacks for all of its children.
219 */
220 if (data->dev->subordinate) {
4be44fcd
LB
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
222 "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
223 data->id.segment, data->id.bus,
224 data->id.device, data->id.function));
1da177e4
LT
225 data->bus = data->dev->subordinate;
226 device->ops.bind = acpi_pci_bind;
227 device->ops.unbind = acpi_pci_unbind;
228 }
229
230 /*
231 * Attach ACPI-PCI Context
232 * -----------------------
233 * Thus binding the ACPI and PCI devices.
234 */
235 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
236 if (ACPI_FAILURE(status)) {
a6fc6720
TR
237 ACPI_EXCEPTION((AE_INFO, status,
238 "Unable to attach ACPI-PCI context to device %s",
239 acpi_device_bid(device)));
1da177e4
LT
240 result = -ENODEV;
241 goto end;
242 }
243
244 /*
245 * PCI Routing Table
246 * -----------------
247 * Evaluate and parse _PRT, if exists. This code is independent of
248 * PCI bridges (above) to allow parsing of _PRT objects within the
249 * scope of non-bridge devices. Note that _PRTs within the scope of
250 * a PCI bridge assume the bridge's subordinate bus number.
251 *
252 * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
253 */
254 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
255 if (ACPI_SUCCESS(status)) {
4be44fcd 256 if (data->bus) /* PCI-PCI bridge */
1da177e4 257 acpi_pci_irq_add_prt(device->handle, data->id.segment,
4be44fcd
LB
258 data->bus->number);
259 else /* non-bridge PCI device */
260 acpi_pci_irq_add_prt(device->handle, data->id.segment,
261 data->id.bus);
1da177e4
LT
262 }
263
4be44fcd 264 end:
1da177e4
LT
265 kfree(pathname);
266 if (result)
267 kfree(data);
268
d550d98d 269 return result;
1da177e4
LT
270}
271
e5685b9d 272static int acpi_pci_unbind(struct acpi_device *device)
1da177e4 273{
4be44fcd
LB
274 int result = 0;
275 acpi_status status = AE_OK;
276 struct acpi_pci_data *data = NULL;
277 char *pathname = NULL;
278 struct acpi_buffer buffer = { 0, NULL };
1da177e4 279
1da177e4
LT
280
281 if (!device || !device->parent)
d550d98d 282 return -EINVAL;
1da177e4 283
36bcbec7 284 pathname = kzalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
4be44fcd 285 if (!pathname)
d550d98d 286 return -ENOMEM;
1da177e4
LT
287
288 buffer.length = ACPI_PATHNAME_MAX;
289 buffer.pointer = pathname;
290 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
4be44fcd 292 pathname));
1da177e4
LT
293 kfree(pathname);
294
4be44fcd
LB
295 status =
296 acpi_get_data(device->handle, acpi_pci_data_handler,
297 (void **)&data);
1da177e4 298 if (ACPI_FAILURE(status)) {
1da177e4
LT
299 result = -ENODEV;
300 goto end;
301 }
302
303 status = acpi_detach_data(device->handle, acpi_pci_data_handler);
304 if (ACPI_FAILURE(status)) {
a6fc6720
TR
305 ACPI_EXCEPTION((AE_INFO, status,
306 "Unable to detach data from device %s",
307 acpi_device_bid(device)));
1da177e4
LT
308 result = -ENODEV;
309 goto end;
310 }
311 if (data->dev->subordinate) {
312 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
313 }
314 kfree(data);
315
4be44fcd 316 end:
d550d98d 317 return result;
1da177e4
LT
318}
319
4be44fcd
LB
320int
321acpi_pci_bind_root(struct acpi_device *device,
322 struct acpi_pci_id *id, struct pci_bus *bus)
1da177e4 323{
4be44fcd
LB
324 int result = 0;
325 acpi_status status = AE_OK;
326 struct acpi_pci_data *data = NULL;
327 char *pathname = NULL;
328 struct acpi_buffer buffer = { 0, NULL };
1da177e4 329
36bcbec7 330 pathname = kzalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
4be44fcd 331 if (!pathname)
d550d98d 332 return -ENOMEM;
1da177e4
LT
333
334 buffer.length = ACPI_PATHNAME_MAX;
335 buffer.pointer = pathname;
336
4be44fcd 337 if (!device || !id || !bus) {
1da177e4 338 kfree(pathname);
d550d98d 339 return -EINVAL;
1da177e4
LT
340 }
341
36bcbec7 342 data = kzalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
4be44fcd 343 if (!data) {
1da177e4 344 kfree(pathname);
d550d98d 345 return -ENOMEM;
1da177e4 346 }
1da177e4
LT
347
348 data->id = *id;
349 data->bus = bus;
350 device->ops.bind = acpi_pci_bind;
351 device->ops.unbind = acpi_pci_unbind;
352
353 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
354
355 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
4be44fcd 356 "%02x:%02x\n", pathname, id->segment, id->bus));
1da177e4
LT
357
358 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
359 if (ACPI_FAILURE(status)) {
a6fc6720
TR
360 ACPI_EXCEPTION((AE_INFO, status,
361 "Unable to attach ACPI-PCI context to device %s",
362 pathname));
1da177e4
LT
363 result = -ENODEV;
364 goto end;
365 }
366
4be44fcd 367 end:
1da177e4
LT
368 kfree(pathname);
369 if (result != 0)
370 kfree(data);
371
d550d98d 372 return result;
1da177e4 373}
This page took 0.391686 seconds and 5 git commands to generate.