blob: 5d19b39e9e2b1b94226aa58b50c4550cccc1ac35 [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
38
39#define _COMPONENT ACPI_PCI_COMPONENT
40ACPI_MODULE_NAME ("pci_bind")
41
42struct acpi_pci_data {
43 struct acpi_pci_id id;
44 struct pci_bus *bus;
45 struct pci_dev *dev;
46};
47
48
49void
50acpi_pci_data_handler (
51 acpi_handle handle,
52 u32 function,
53 void *context)
54{
55 ACPI_FUNCTION_TRACE("acpi_pci_data_handler");
56
57 /* TBD: Anything we need to do here? */
58
59 return_VOID;
60}
61
62
63/**
64 * acpi_os_get_pci_id
65 * ------------------
66 * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
67 * to resolve PCI information for ACPI-PCI devices defined in the namespace.
68 * This typically occurs when resolving PCI operation region information.
69 */
70#ifdef ACPI_FUTURE_USAGE
71acpi_status
72acpi_os_get_pci_id (
73 acpi_handle handle,
74 struct acpi_pci_id *id)
75{
76 int result = 0;
77 acpi_status status = AE_OK;
78 struct acpi_device *device = NULL;
79 struct acpi_pci_data *data = NULL;
80
81 ACPI_FUNCTION_TRACE("acpi_os_get_pci_id");
82
83 if (!id)
84 return_ACPI_STATUS(AE_BAD_PARAMETER);
85
86 result = acpi_bus_get_device(handle, &device);
87 if (result) {
88 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
89 "Invalid ACPI Bus context for device %s\n",
90 acpi_device_bid(device)));
91 return_ACPI_STATUS(AE_NOT_EXIST);
92 }
93
94 status = acpi_get_data(handle, acpi_pci_data_handler, (void**) &data);
95 if (ACPI_FAILURE(status) || !data || !data->dev) {
96 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
97 "Invalid ACPI-PCI context for device %s\n",
98 acpi_device_bid(device)));
99 return_ACPI_STATUS(status);
100 }
101
102 *id = data->id;
103
104 /*
105 id->segment = data->id.segment;
106 id->bus = data->id.bus;
107 id->device = data->id.device;
108 id->function = data->id.function;
109 */
110
111 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
112 "Device %s has PCI address %02x:%02x:%02x.%02x\n",
113 acpi_device_bid(device), id->segment, id->bus,
114 id->device, id->function));
115
116 return_ACPI_STATUS(AE_OK);
117}
118#endif /* ACPI_FUTURE_USAGE */
119
120
121int
122acpi_pci_bind (
123 struct acpi_device *device)
124{
125 int result = 0;
126 acpi_status status = AE_OK;
127 struct acpi_pci_data *data = NULL;
128 struct acpi_pci_data *pdata = NULL;
129 char *pathname = NULL;
130 struct acpi_buffer buffer = {0, NULL};
131 acpi_handle handle = NULL;
132
133 ACPI_FUNCTION_TRACE("acpi_pci_bind");
134
135 if (!device || !device->parent)
136 return_VALUE(-EINVAL);
137
138 pathname = kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
139 if(!pathname)
140 return_VALUE(-ENOMEM);
141 memset(pathname, 0, ACPI_PATHNAME_MAX);
142 buffer.length = ACPI_PATHNAME_MAX;
143 buffer.pointer = pathname;
144
145 data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
146 if (!data){
147 kfree (pathname);
148 return_VALUE(-ENOMEM);
149 }
150 memset(data, 0, sizeof(struct acpi_pci_data));
151
152 acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
153 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
154 pathname));
155
156 /*
157 * Segment & Bus
158 * -------------
159 * These are obtained via the parent device's ACPI-PCI context.
160 */
161 status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
162 (void**) &pdata);
163 if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
164 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
165 "Invalid ACPI-PCI context for parent device %s\n",
166 acpi_device_bid(device->parent)));
167 result = -ENODEV;
168 goto end;
169 }
170 data->id.segment = pdata->id.segment;
171 data->id.bus = pdata->bus->number;
172
173 /*
174 * Device & Function
175 * -----------------
176 * These are simply obtained from the device's _ADR method. Note
177 * that a value of zero is valid.
178 */
179 data->id.device = device->pnp.bus_address >> 16;
180 data->id.function = device->pnp.bus_address & 0xFFFF;
181
182 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
183 data->id.segment, data->id.bus, data->id.device,
184 data->id.function));
185
186 /*
187 * TBD: Support slot devices (e.g. function=0xFFFF).
188 */
189
190 /*
191 * Locate PCI Device
192 * -----------------
193 * Locate matching device in PCI namespace. If it doesn't exist
194 * this typically means that the device isn't currently inserted
195 * (e.g. docking station, port replicator, etc.).
196 */
197 data->dev = pci_find_slot(data->id.bus, PCI_DEVFN(data->id.device, data->id.function));
198 if (!data->dev) {
199 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
200 "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
201 data->id.segment, data->id.bus,
202 data->id.device, data->id.function));
203 result = -ENODEV;
204 goto end;
205 }
206 if (!data->dev->bus) {
207 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
208 "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
209 data->id.segment, data->id.bus,
210 data->id.device, data->id.function));
211 result = -ENODEV;
212 goto end;
213 }
214
215 /*
216 * PCI Bridge?
217 * -----------
218 * If so, set the 'bus' field and install the 'bind' function to
219 * facilitate callbacks for all of its children.
220 */
221 if (data->dev->subordinate) {
222 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
223 "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
224 data->id.segment, data->id.bus,
225 data->id.device, data->id.function));
226 data->bus = data->dev->subordinate;
227 device->ops.bind = acpi_pci_bind;
228 device->ops.unbind = acpi_pci_unbind;
229 }
230
231 /*
232 * Attach ACPI-PCI Context
233 * -----------------------
234 * Thus binding the ACPI and PCI devices.
235 */
236 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
237 if (ACPI_FAILURE(status)) {
238 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
239 "Unable to attach ACPI-PCI context to device %s\n",
240 acpi_device_bid(device)));
241 result = -ENODEV;
242 goto end;
243 }
244
245 /*
246 * PCI Routing Table
247 * -----------------
248 * Evaluate and parse _PRT, if exists. This code is independent of
249 * PCI bridges (above) to allow parsing of _PRT objects within the
250 * scope of non-bridge devices. Note that _PRTs within the scope of
251 * a PCI bridge assume the bridge's subordinate bus number.
252 *
253 * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
254 */
255 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
256 if (ACPI_SUCCESS(status)) {
257 if (data->bus) /* PCI-PCI bridge */
258 acpi_pci_irq_add_prt(device->handle, data->id.segment,
259 data->bus->number);
260 else /* non-bridge PCI device */
261 acpi_pci_irq_add_prt(device->handle, data->id.segment,
262 data->id.bus);
263 }
264
265end:
266 kfree(pathname);
267 if (result)
268 kfree(data);
269
270 return_VALUE(result);
271}
272
273int acpi_pci_unbind(
274 struct acpi_device *device)
275{
276 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};
281
282 ACPI_FUNCTION_TRACE("acpi_pci_unbind");
283
284 if (!device || !device->parent)
285 return_VALUE(-EINVAL);
286
287 pathname = (char *) kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
288 if(!pathname)
289 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",
296 pathname));
297 kfree(pathname);
298
299 status = acpi_get_data(device->handle, acpi_pci_data_handler, (void**)&data);
300 if (ACPI_FAILURE(status)) {
301 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
302 "Unable to get data from device %s\n",
303 acpi_device_bid(device)));
304 result = -ENODEV;
305 goto end;
306 }
307
308 status = acpi_detach_data(device->handle, acpi_pci_data_handler);
309 if (ACPI_FAILURE(status)) {
310 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
311 "Unable to detach data from device %s\n",
312 acpi_device_bid(device)));
313 result = -ENODEV;
314 goto end;
315 }
316 if (data->dev->subordinate) {
317 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
318 }
319 kfree(data);
320
321end:
322 return_VALUE(result);
323}
324
325int
326acpi_pci_bind_root (
327 struct acpi_device *device,
328 struct acpi_pci_id *id,
329 struct pci_bus *bus)
330{
331 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};
336
337 ACPI_FUNCTION_TRACE("acpi_pci_bind_root");
338
339 pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
340 if(!pathname)
341 return_VALUE(-ENOMEM);
342 memset(pathname, 0, ACPI_PATHNAME_MAX);
343
344 buffer.length = ACPI_PATHNAME_MAX;
345 buffer.pointer = pathname;
346
347 if (!device || !id || !bus){
348 kfree(pathname);
349 return_VALUE(-EINVAL);
350 }
351
352 data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
353 if (!data){
354 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 "
367 "%02x:%02x\n", pathname, id->segment, id->bus));
368
369 status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
370 if (ACPI_FAILURE(status)) {
371 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
372 "Unable to attach ACPI-PCI context to device %s\n",
373 pathname));
374 result = -ENODEV;
375 goto end;
376 }
377
378end:
379 kfree(pathname);
380 if (result != 0)
381 kfree(data);
382
383 return_VALUE(result);
384}