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