Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ACPI PCI HotPlug glue functions to ACPI CA subsystem | |
3 | * | |
4 | * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com) | |
5 | * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com) | |
6 | * Copyright (C) 2002,2003 NEC Corporation | |
42f49a6a RS |
7 | * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com) |
8 | * Copyright (C) 2003-2005 Hewlett Packard | |
8e7561cf RS |
9 | * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com) |
10 | * Copyright (C) 2005 Intel Corporation | |
1da177e4 LT |
11 | * |
12 | * All rights reserved. | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or (at | |
17 | * your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, but | |
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
22 | * NON INFRINGEMENT. See the GNU General Public License for more | |
23 | * details. | |
24 | * | |
25 | * You should have received a copy of the GNU General Public License | |
26 | * along with this program; if not, write to the Free Software | |
27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
28 | * | |
29 | * Send feedback to <t-kochi@bq.jp.nec.com> | |
30 | * | |
31 | */ | |
32 | ||
42f49a6a RS |
33 | /* |
34 | * Lifetime rules for pci_dev: | |
35 | * - The one in acpiphp_func has its refcount elevated by pci_get_slot() | |
36 | * when the driver is loaded or when an insertion event occurs. It loses | |
37 | * a refcount when its ejected or the driver unloads. | |
38 | * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot() | |
39 | * when the bridge is scanned and it loses a refcount when the bridge | |
40 | * is removed. | |
41 | */ | |
42 | ||
1da177e4 LT |
43 | #include <linux/init.h> |
44 | #include <linux/module.h> | |
45 | ||
46 | #include <linux/kernel.h> | |
47 | #include <linux/pci.h> | |
48 | #include <linux/smp_lock.h> | |
6aa4cdd0 | 49 | #include <linux/mutex.h> |
1da177e4 LT |
50 | |
51 | #include "../pci.h" | |
52 | #include "pci_hotplug.h" | |
53 | #include "acpiphp.h" | |
54 | ||
55 | static LIST_HEAD(bridge_list); | |
56 | ||
57 | #define MY_NAME "acpiphp_glue" | |
58 | ||
59 | static void handle_hotplug_event_bridge (acpi_handle, u32, void *); | |
8e5dce35 KA |
60 | static void acpiphp_sanitize_bus(struct pci_bus *bus); |
61 | static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus); | |
62 | ||
1da177e4 LT |
63 | |
64 | /* | |
65 | * initialization & terminatation routines | |
66 | */ | |
67 | ||
68 | /** | |
69 | * is_ejectable - determine if a slot is ejectable | |
70 | * @handle: handle to acpi namespace | |
71 | * | |
72 | * Ejectable slot should satisfy at least these conditions: | |
73 | * | |
74 | * 1. has _ADR method | |
75 | * 2. has _EJ0 method | |
76 | * | |
77 | * optionally | |
78 | * | |
79 | * 1. has _STA method | |
80 | * 2. has _PS0 method | |
81 | * 3. has _PS3 method | |
82 | * 4. .. | |
83 | * | |
84 | */ | |
85 | static int is_ejectable(acpi_handle handle) | |
86 | { | |
87 | acpi_status status; | |
88 | acpi_handle tmp; | |
89 | ||
90 | status = acpi_get_handle(handle, "_ADR", &tmp); | |
91 | if (ACPI_FAILURE(status)) { | |
92 | return 0; | |
93 | } | |
94 | ||
95 | status = acpi_get_handle(handle, "_EJ0", &tmp); | |
96 | if (ACPI_FAILURE(status)) { | |
97 | return 0; | |
98 | } | |
99 | ||
100 | return 1; | |
101 | } | |
102 | ||
103 | ||
104 | /* callback routine to check the existence of ejectable slots */ | |
105 | static acpi_status | |
106 | is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv) | |
107 | { | |
108 | int *count = (int *)context; | |
109 | ||
110 | if (is_ejectable(handle)) { | |
111 | (*count)++; | |
112 | /* only one ejectable slot is enough */ | |
113 | return AE_CTRL_TERMINATE; | |
114 | } else { | |
115 | return AE_OK; | |
116 | } | |
117 | } | |
118 | ||
119 | ||
120 | /* callback routine to register each ACPI PCI slot object */ | |
121 | static acpi_status | |
122 | register_slot(acpi_handle handle, u32 lvl, void *context, void **rv) | |
123 | { | |
124 | struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context; | |
125 | struct acpiphp_slot *slot; | |
126 | struct acpiphp_func *newfunc; | |
20416ea5 | 127 | struct dependent_device *dd; |
1da177e4 LT |
128 | acpi_handle tmp; |
129 | acpi_status status = AE_OK; | |
130 | unsigned long adr, sun; | |
e27da381 | 131 | int device, function, retval; |
1da177e4 LT |
132 | |
133 | status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr); | |
134 | ||
135 | if (ACPI_FAILURE(status)) | |
136 | return AE_OK; | |
137 | ||
138 | status = acpi_get_handle(handle, "_EJ0", &tmp); | |
139 | ||
20416ea5 | 140 | if (ACPI_FAILURE(status) && !(is_dependent_device(handle))) |
1da177e4 LT |
141 | return AE_OK; |
142 | ||
143 | device = (adr >> 16) & 0xffff; | |
144 | function = adr & 0xffff; | |
145 | ||
f5afe806 | 146 | newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL); |
1da177e4 LT |
147 | if (!newfunc) |
148 | return AE_NO_MEMORY; | |
1da177e4 LT |
149 | |
150 | INIT_LIST_HEAD(&newfunc->sibling); | |
151 | newfunc->handle = handle; | |
152 | newfunc->function = function; | |
20416ea5 KA |
153 | if (ACPI_SUCCESS(status)) |
154 | newfunc->flags = FUNC_HAS_EJ0; | |
1da177e4 LT |
155 | |
156 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp))) | |
157 | newfunc->flags |= FUNC_HAS_STA; | |
158 | ||
159 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp))) | |
160 | newfunc->flags |= FUNC_HAS_PS0; | |
161 | ||
162 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp))) | |
163 | newfunc->flags |= FUNC_HAS_PS3; | |
164 | ||
20416ea5 KA |
165 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) { |
166 | newfunc->flags |= FUNC_HAS_DCK; | |
167 | /* add to devices dependent on dock station, | |
168 | * because this may actually be the dock bridge | |
169 | */ | |
170 | dd = alloc_dependent_device(handle); | |
171 | if (!dd) | |
172 | err("Can't allocate memory for " | |
173 | "new dependent device!\n"); | |
174 | else | |
175 | add_dependent_device(dd); | |
176 | } | |
177 | ||
1da177e4 LT |
178 | status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun); |
179 | if (ACPI_FAILURE(status)) | |
180 | sun = -1; | |
181 | ||
182 | /* search for objects that share the same slot */ | |
183 | for (slot = bridge->slots; slot; slot = slot->next) | |
184 | if (slot->device == device) { | |
185 | if (slot->sun != sun) | |
186 | warn("sibling found, but _SUN doesn't match!\n"); | |
187 | break; | |
188 | } | |
189 | ||
190 | if (!slot) { | |
f5afe806 | 191 | slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL); |
1da177e4 LT |
192 | if (!slot) { |
193 | kfree(newfunc); | |
194 | return AE_NO_MEMORY; | |
195 | } | |
196 | ||
1da177e4 | 197 | slot->bridge = bridge; |
1da177e4 LT |
198 | slot->device = device; |
199 | slot->sun = sun; | |
200 | INIT_LIST_HEAD(&slot->funcs); | |
6aa4cdd0 | 201 | mutex_init(&slot->crit_sect); |
1da177e4 LT |
202 | |
203 | slot->next = bridge->slots; | |
204 | bridge->slots = slot; | |
205 | ||
206 | bridge->nr_slots++; | |
207 | ||
42f49a6a RS |
208 | dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n", |
209 | slot->sun, pci_domain_nr(bridge->pci_bus), | |
210 | bridge->pci_bus->number, slot->device); | |
e27da381 MT |
211 | retval = acpiphp_register_hotplug_slot(slot); |
212 | if (retval) { | |
213 | warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval); | |
214 | goto err_exit; | |
215 | } | |
1da177e4 LT |
216 | } |
217 | ||
218 | newfunc->slot = slot; | |
219 | list_add_tail(&newfunc->sibling, &slot->funcs); | |
220 | ||
221 | /* associate corresponding pci_dev */ | |
42f49a6a | 222 | newfunc->pci_dev = pci_get_slot(bridge->pci_bus, |
1da177e4 LT |
223 | PCI_DEVFN(device, function)); |
224 | if (newfunc->pci_dev) { | |
1da177e4 LT |
225 | slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON); |
226 | } | |
227 | ||
20416ea5 KA |
228 | /* if this is a device dependent on a dock station, |
229 | * associate the acpiphp_func to the dependent_device | |
230 | * struct. | |
231 | */ | |
232 | if ((dd = get_dependent_device(handle))) { | |
233 | newfunc->flags |= FUNC_IS_DD; | |
234 | /* | |
235 | * we don't want any devices which is dependent | |
236 | * on the dock to have it's _EJ0 method executed. | |
237 | * because we need to run _DCK first. | |
238 | */ | |
239 | newfunc->flags &= ~FUNC_HAS_EJ0; | |
240 | dd->func = newfunc; | |
241 | add_pci_dependent_device(dd); | |
242 | } | |
243 | ||
1da177e4 | 244 | /* install notify handler */ |
20416ea5 KA |
245 | if (!(newfunc->flags & FUNC_HAS_DCK)) { |
246 | status = acpi_install_notify_handler(handle, | |
1da177e4 LT |
247 | ACPI_SYSTEM_NOTIFY, |
248 | handle_hotplug_event_func, | |
249 | newfunc); | |
250 | ||
20416ea5 KA |
251 | if (ACPI_FAILURE(status)) |
252 | err("failed to register interrupt notify handler\n"); | |
253 | } else | |
254 | status = AE_OK; | |
1da177e4 | 255 | |
20416ea5 | 256 | return status; |
e27da381 MT |
257 | |
258 | err_exit: | |
259 | bridge->nr_slots--; | |
260 | bridge->slots = slot->next; | |
261 | kfree(slot); | |
262 | kfree(newfunc); | |
263 | ||
264 | return AE_OK; | |
1da177e4 LT |
265 | } |
266 | ||
267 | ||
268 | /* see if it's worth looking at this bridge */ | |
269 | static int detect_ejectable_slots(acpi_handle *bridge_handle) | |
270 | { | |
271 | acpi_status status; | |
272 | int count; | |
273 | ||
274 | count = 0; | |
275 | ||
276 | /* only check slots defined directly below bridge object */ | |
277 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1, | |
278 | is_ejectable_slot, (void *)&count, NULL); | |
279 | ||
280 | return count; | |
281 | } | |
282 | ||
283 | ||
1da177e4 LT |
284 | /* decode ACPI 2.0 _HPP hot plug parameters */ |
285 | static void decode_hpp(struct acpiphp_bridge *bridge) | |
286 | { | |
287 | acpi_status status; | |
288 | struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER, | |
289 | .pointer = NULL}; | |
290 | union acpi_object *package; | |
291 | int i; | |
292 | ||
293 | /* default numbers */ | |
294 | bridge->hpp.cache_line_size = 0x10; | |
295 | bridge->hpp.latency_timer = 0x40; | |
296 | bridge->hpp.enable_SERR = 0; | |
297 | bridge->hpp.enable_PERR = 0; | |
298 | ||
299 | status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer); | |
300 | ||
301 | if (ACPI_FAILURE(status)) { | |
302 | dbg("_HPP evaluation failed\n"); | |
303 | return; | |
304 | } | |
305 | ||
306 | package = (union acpi_object *) buffer.pointer; | |
307 | ||
308 | if (!package || package->type != ACPI_TYPE_PACKAGE || | |
309 | package->package.count != 4 || !package->package.elements) { | |
310 | err("invalid _HPP object; ignoring\n"); | |
311 | goto err_exit; | |
312 | } | |
313 | ||
314 | for (i = 0; i < 4; i++) { | |
315 | if (package->package.elements[i].type != ACPI_TYPE_INTEGER) { | |
316 | err("invalid _HPP parameter type; ignoring\n"); | |
317 | goto err_exit; | |
318 | } | |
319 | } | |
320 | ||
321 | bridge->hpp.cache_line_size = package->package.elements[0].integer.value; | |
322 | bridge->hpp.latency_timer = package->package.elements[1].integer.value; | |
323 | bridge->hpp.enable_SERR = package->package.elements[2].integer.value; | |
324 | bridge->hpp.enable_PERR = package->package.elements[3].integer.value; | |
325 | ||
326 | dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n", | |
327 | bridge->hpp.cache_line_size, | |
328 | bridge->hpp.latency_timer, | |
329 | bridge->hpp.enable_SERR, | |
330 | bridge->hpp.enable_PERR); | |
331 | ||
332 | bridge->flags |= BRIDGE_HAS_HPP; | |
333 | ||
334 | err_exit: | |
335 | kfree(buffer.pointer); | |
336 | } | |
337 | ||
338 | ||
339 | /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */ | |
340 | static void init_bridge_misc(struct acpiphp_bridge *bridge) | |
341 | { | |
342 | acpi_status status; | |
343 | ||
344 | /* decode ACPI 2.0 _HPP (hot plug parameters) */ | |
345 | decode_hpp(bridge); | |
346 | ||
e27da381 MT |
347 | /* must be added to the list prior to calling register_slot */ |
348 | list_add(&bridge->list, &bridge_list); | |
349 | ||
1da177e4 LT |
350 | /* register all slot objects under this bridge */ |
351 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1, | |
352 | register_slot, bridge, NULL); | |
e27da381 MT |
353 | if (ACPI_FAILURE(status)) { |
354 | list_del(&bridge->list); | |
355 | return; | |
356 | } | |
1da177e4 LT |
357 | |
358 | /* install notify handler */ | |
8e7561cf RS |
359 | if (bridge->type != BRIDGE_TYPE_HOST) { |
360 | status = acpi_install_notify_handler(bridge->handle, | |
1da177e4 LT |
361 | ACPI_SYSTEM_NOTIFY, |
362 | handle_hotplug_event_bridge, | |
363 | bridge); | |
364 | ||
8e7561cf RS |
365 | if (ACPI_FAILURE(status)) { |
366 | err("failed to register interrupt notify handler\n"); | |
367 | } | |
1da177e4 | 368 | } |
1da177e4 LT |
369 | } |
370 | ||
371 | ||
372 | /* allocate and initialize host bridge data structure */ | |
42f49a6a | 373 | static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus) |
1da177e4 | 374 | { |
1da177e4 LT |
375 | struct acpiphp_bridge *bridge; |
376 | ||
f5afe806 | 377 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
1da177e4 LT |
378 | if (bridge == NULL) |
379 | return; | |
380 | ||
1da177e4 LT |
381 | bridge->type = BRIDGE_TYPE_HOST; |
382 | bridge->handle = handle; | |
1da177e4 | 383 | |
42f49a6a | 384 | bridge->pci_bus = pci_bus; |
1da177e4 LT |
385 | |
386 | spin_lock_init(&bridge->res_lock); | |
387 | ||
1da177e4 LT |
388 | init_bridge_misc(bridge); |
389 | } | |
390 | ||
391 | ||
392 | /* allocate and initialize PCI-to-PCI bridge data structure */ | |
42f49a6a | 393 | static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev) |
1da177e4 LT |
394 | { |
395 | struct acpiphp_bridge *bridge; | |
1da177e4 | 396 | |
f5afe806 | 397 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
1da177e4 LT |
398 | if (bridge == NULL) { |
399 | err("out of memory\n"); | |
400 | return; | |
401 | } | |
402 | ||
1da177e4 LT |
403 | bridge->type = BRIDGE_TYPE_P2P; |
404 | bridge->handle = handle; | |
1da177e4 | 405 | |
42f49a6a RS |
406 | bridge->pci_dev = pci_dev_get(pci_dev); |
407 | bridge->pci_bus = pci_dev->subordinate; | |
1da177e4 LT |
408 | if (!bridge->pci_bus) { |
409 | err("This is not a PCI-to-PCI bridge!\n"); | |
42f49a6a | 410 | goto err; |
1da177e4 LT |
411 | } |
412 | ||
413 | spin_lock_init(&bridge->res_lock); | |
414 | ||
1da177e4 | 415 | init_bridge_misc(bridge); |
42f49a6a RS |
416 | return; |
417 | err: | |
418 | pci_dev_put(pci_dev); | |
419 | kfree(bridge); | |
420 | return; | |
1da177e4 LT |
421 | } |
422 | ||
423 | ||
424 | /* callback routine to find P2P bridges */ | |
425 | static acpi_status | |
426 | find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv) | |
427 | { | |
428 | acpi_status status; | |
429 | acpi_handle dummy_handle; | |
1da177e4 | 430 | unsigned long tmp; |
42f49a6a | 431 | int device, function; |
1da177e4 | 432 | struct pci_dev *dev; |
42f49a6a | 433 | struct pci_bus *pci_bus = context; |
1da177e4 LT |
434 | |
435 | status = acpi_get_handle(handle, "_ADR", &dummy_handle); | |
436 | if (ACPI_FAILURE(status)) | |
437 | return AE_OK; /* continue */ | |
438 | ||
439 | status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp); | |
440 | if (ACPI_FAILURE(status)) { | |
441 | dbg("%s: _ADR evaluation failure\n", __FUNCTION__); | |
442 | return AE_OK; | |
443 | } | |
444 | ||
445 | device = (tmp >> 16) & 0xffff; | |
446 | function = tmp & 0xffff; | |
447 | ||
42f49a6a | 448 | dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function)); |
1da177e4 | 449 | |
42f49a6a RS |
450 | if (!dev || !dev->subordinate) |
451 | goto out; | |
1da177e4 LT |
452 | |
453 | /* check if this bridge has ejectable slots */ | |
20416ea5 KA |
454 | if ((detect_ejectable_slots(handle) > 0) || |
455 | (detect_dependent_devices(handle) > 0)) { | |
1da177e4 | 456 | dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev)); |
42f49a6a | 457 | add_p2p_bridge(handle, dev); |
1da177e4 LT |
458 | } |
459 | ||
7c8f25da KK |
460 | /* search P2P bridges under this p2p bridge */ |
461 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, | |
462 | find_p2p_bridge, dev->subordinate, NULL); | |
463 | if (ACPI_FAILURE(status)) | |
464 | warn("find_p2p_bridge faied (error code = 0x%x)\n", status); | |
465 | ||
42f49a6a RS |
466 | out: |
467 | pci_dev_put(dev); | |
1da177e4 LT |
468 | return AE_OK; |
469 | } | |
470 | ||
471 | ||
472 | /* find hot-pluggable slots, and then find P2P bridge */ | |
473 | static int add_bridge(acpi_handle handle) | |
474 | { | |
475 | acpi_status status; | |
476 | unsigned long tmp; | |
477 | int seg, bus; | |
478 | acpi_handle dummy_handle; | |
42f49a6a | 479 | struct pci_bus *pci_bus; |
1da177e4 LT |
480 | |
481 | /* if the bridge doesn't have _STA, we assume it is always there */ | |
482 | status = acpi_get_handle(handle, "_STA", &dummy_handle); | |
483 | if (ACPI_SUCCESS(status)) { | |
484 | status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp); | |
485 | if (ACPI_FAILURE(status)) { | |
486 | dbg("%s: _STA evaluation failure\n", __FUNCTION__); | |
487 | return 0; | |
488 | } | |
489 | if ((tmp & ACPI_STA_FUNCTIONING) == 0) | |
490 | /* don't register this object */ | |
491 | return 0; | |
492 | } | |
493 | ||
494 | /* get PCI segment number */ | |
495 | status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp); | |
496 | ||
497 | seg = ACPI_SUCCESS(status) ? tmp : 0; | |
498 | ||
499 | /* get PCI bus number */ | |
500 | status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp); | |
501 | ||
502 | if (ACPI_SUCCESS(status)) { | |
503 | bus = tmp; | |
504 | } else { | |
505 | warn("can't get bus number, assuming 0\n"); | |
506 | bus = 0; | |
507 | } | |
508 | ||
42f49a6a RS |
509 | pci_bus = pci_find_bus(seg, bus); |
510 | if (!pci_bus) { | |
511 | err("Can't find bus %04x:%02x\n", seg, bus); | |
512 | return 0; | |
513 | } | |
514 | ||
1da177e4 LT |
515 | /* check if this bridge has ejectable slots */ |
516 | if (detect_ejectable_slots(handle) > 0) { | |
517 | dbg("found PCI host-bus bridge with hot-pluggable slots\n"); | |
42f49a6a | 518 | add_host_bridge(handle, pci_bus); |
1da177e4 LT |
519 | return 0; |
520 | } | |
521 | ||
1da177e4 LT |
522 | /* search P2P bridges under this host bridge */ |
523 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, | |
42f49a6a | 524 | find_p2p_bridge, pci_bus, NULL); |
1da177e4 LT |
525 | |
526 | if (ACPI_FAILURE(status)) | |
527 | warn("find_p2p_bridge faied (error code = 0x%x)\n",status); | |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
42f49a6a RS |
532 | static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle) |
533 | { | |
534 | struct list_head *head; | |
535 | list_for_each(head, &bridge_list) { | |
536 | struct acpiphp_bridge *bridge = list_entry(head, | |
537 | struct acpiphp_bridge, list); | |
538 | if (bridge->handle == handle) | |
539 | return bridge; | |
540 | } | |
541 | ||
542 | return NULL; | |
543 | } | |
1da177e4 | 544 | |
364d5094 | 545 | static void cleanup_bridge(struct acpiphp_bridge *bridge) |
1da177e4 | 546 | { |
42f49a6a | 547 | struct list_head *list, *tmp; |
42f49a6a RS |
548 | struct acpiphp_slot *slot; |
549 | acpi_status status; | |
364d5094 | 550 | acpi_handle handle = bridge->handle; |
42f49a6a RS |
551 | |
552 | status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, | |
553 | handle_hotplug_event_bridge); | |
554 | if (ACPI_FAILURE(status)) | |
555 | err("failed to remove notify handler\n"); | |
556 | ||
557 | slot = bridge->slots; | |
558 | while (slot) { | |
559 | struct acpiphp_slot *next = slot->next; | |
560 | list_for_each_safe (list, tmp, &slot->funcs) { | |
561 | struct acpiphp_func *func; | |
562 | func = list_entry(list, struct acpiphp_func, sibling); | |
20416ea5 KA |
563 | if (!(func->flags & FUNC_HAS_DCK)) { |
564 | status = acpi_remove_notify_handler(func->handle, | |
42f49a6a RS |
565 | ACPI_SYSTEM_NOTIFY, |
566 | handle_hotplug_event_func); | |
20416ea5 KA |
567 | if (ACPI_FAILURE(status)) |
568 | err("failed to remove notify handler\n"); | |
569 | } | |
42f49a6a RS |
570 | pci_dev_put(func->pci_dev); |
571 | list_del(list); | |
572 | kfree(func); | |
573 | } | |
e27da381 MT |
574 | acpiphp_unregister_hotplug_slot(slot); |
575 | list_del(&slot->funcs); | |
42f49a6a RS |
576 | kfree(slot); |
577 | slot = next; | |
578 | } | |
579 | ||
580 | pci_dev_put(bridge->pci_dev); | |
581 | list_del(&bridge->list); | |
582 | kfree(bridge); | |
1da177e4 LT |
583 | } |
584 | ||
364d5094 RS |
585 | static acpi_status |
586 | cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv) | |
587 | { | |
588 | struct acpiphp_bridge *bridge; | |
589 | ||
590 | if (!(bridge = acpiphp_handle_to_bridge(handle))) | |
591 | return AE_OK; | |
592 | cleanup_bridge(bridge); | |
593 | return AE_OK; | |
594 | } | |
595 | ||
596 | static void remove_bridge(acpi_handle handle) | |
597 | { | |
598 | struct acpiphp_bridge *bridge; | |
599 | ||
600 | bridge = acpiphp_handle_to_bridge(handle); | |
601 | if (bridge) { | |
602 | cleanup_bridge(bridge); | |
603 | } else { | |
604 | /* clean-up p2p bridges under this host bridge */ | |
605 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, | |
7c8f25da KK |
606 | ACPI_UINT32_MAX, cleanup_p2p_bridge, |
607 | NULL, NULL); | |
364d5094 RS |
608 | } |
609 | } | |
1da177e4 | 610 | |
a0d399a8 KK |
611 | static struct pci_dev * get_apic_pci_info(acpi_handle handle) |
612 | { | |
613 | struct acpi_pci_id id; | |
614 | struct pci_bus *bus; | |
615 | struct pci_dev *dev; | |
616 | ||
617 | if (ACPI_FAILURE(acpi_get_pci_id(handle, &id))) | |
618 | return NULL; | |
619 | ||
620 | bus = pci_find_bus(id.segment, id.bus); | |
621 | if (!bus) | |
622 | return NULL; | |
623 | ||
624 | dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function)); | |
625 | if (!dev) | |
626 | return NULL; | |
627 | ||
628 | if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) && | |
629 | (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC)) | |
630 | { | |
631 | pci_dev_put(dev); | |
632 | return NULL; | |
633 | } | |
634 | ||
635 | return dev; | |
636 | } | |
637 | ||
638 | static int get_gsi_base(acpi_handle handle, u32 *gsi_base) | |
639 | { | |
640 | acpi_status status; | |
641 | int result = -1; | |
642 | unsigned long gsb; | |
643 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; | |
644 | union acpi_object *obj; | |
645 | void *table; | |
646 | ||
647 | status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb); | |
648 | if (ACPI_SUCCESS(status)) { | |
649 | *gsi_base = (u32)gsb; | |
650 | return 0; | |
651 | } | |
652 | ||
653 | status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer); | |
654 | if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer) | |
655 | return -1; | |
656 | ||
657 | obj = buffer.pointer; | |
658 | if (obj->type != ACPI_TYPE_BUFFER) | |
659 | goto out; | |
660 | ||
661 | table = obj->buffer.pointer; | |
662 | switch (((acpi_table_entry_header *)table)->type) { | |
663 | case ACPI_MADT_IOSAPIC: | |
664 | *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base; | |
665 | result = 0; | |
666 | break; | |
667 | case ACPI_MADT_IOAPIC: | |
668 | *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base; | |
669 | result = 0; | |
670 | break; | |
671 | default: | |
672 | break; | |
673 | } | |
674 | out: | |
675 | acpi_os_free(buffer.pointer); | |
676 | return result; | |
677 | } | |
678 | ||
679 | static acpi_status | |
680 | ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv) | |
681 | { | |
682 | acpi_status status; | |
683 | unsigned long sta; | |
684 | acpi_handle tmp; | |
685 | struct pci_dev *pdev; | |
686 | u32 gsi_base; | |
687 | u64 phys_addr; | |
688 | ||
689 | /* Evaluate _STA if present */ | |
690 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); | |
691 | if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL) | |
692 | return AE_CTRL_DEPTH; | |
693 | ||
694 | /* Scan only PCI bus scope */ | |
695 | status = acpi_get_handle(handle, "_HID", &tmp); | |
696 | if (ACPI_SUCCESS(status)) | |
697 | return AE_CTRL_DEPTH; | |
698 | ||
699 | if (get_gsi_base(handle, &gsi_base)) | |
700 | return AE_OK; | |
701 | ||
702 | pdev = get_apic_pci_info(handle); | |
703 | if (!pdev) | |
704 | return AE_OK; | |
705 | ||
706 | if (pci_enable_device(pdev)) { | |
707 | pci_dev_put(pdev); | |
708 | return AE_OK; | |
709 | } | |
710 | ||
711 | pci_set_master(pdev); | |
712 | ||
713 | if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) { | |
714 | pci_disable_device(pdev); | |
715 | pci_dev_put(pdev); | |
716 | return AE_OK; | |
717 | } | |
718 | ||
719 | phys_addr = pci_resource_start(pdev, 0); | |
720 | if (acpi_register_ioapic(handle, phys_addr, gsi_base)) { | |
721 | pci_release_region(pdev, 0); | |
722 | pci_disable_device(pdev); | |
723 | pci_dev_put(pdev); | |
724 | return AE_OK; | |
725 | } | |
726 | ||
727 | return AE_OK; | |
728 | } | |
729 | ||
730 | static int acpiphp_configure_ioapics(acpi_handle handle) | |
731 | { | |
732 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, | |
733 | ACPI_UINT32_MAX, ioapic_add, NULL, NULL); | |
734 | return 0; | |
735 | } | |
736 | ||
1da177e4 LT |
737 | static int power_on_slot(struct acpiphp_slot *slot) |
738 | { | |
739 | acpi_status status; | |
740 | struct acpiphp_func *func; | |
741 | struct list_head *l; | |
742 | int retval = 0; | |
743 | ||
744 | /* if already enabled, just skip */ | |
745 | if (slot->flags & SLOT_POWEREDON) | |
746 | goto err_exit; | |
747 | ||
748 | list_for_each (l, &slot->funcs) { | |
749 | func = list_entry(l, struct acpiphp_func, sibling); | |
750 | ||
751 | if (func->flags & FUNC_HAS_PS0) { | |
752 | dbg("%s: executing _PS0\n", __FUNCTION__); | |
753 | status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL); | |
754 | if (ACPI_FAILURE(status)) { | |
755 | warn("%s: _PS0 failed\n", __FUNCTION__); | |
756 | retval = -1; | |
757 | goto err_exit; | |
758 | } else | |
759 | break; | |
760 | } | |
761 | } | |
762 | ||
763 | /* TBD: evaluate _STA to check if the slot is enabled */ | |
764 | ||
765 | slot->flags |= SLOT_POWEREDON; | |
766 | ||
767 | err_exit: | |
768 | return retval; | |
769 | } | |
770 | ||
771 | ||
772 | static int power_off_slot(struct acpiphp_slot *slot) | |
773 | { | |
774 | acpi_status status; | |
775 | struct acpiphp_func *func; | |
776 | struct list_head *l; | |
1da177e4 LT |
777 | |
778 | int retval = 0; | |
779 | ||
780 | /* if already disabled, just skip */ | |
781 | if ((slot->flags & SLOT_POWEREDON) == 0) | |
782 | goto err_exit; | |
783 | ||
784 | list_for_each (l, &slot->funcs) { | |
785 | func = list_entry(l, struct acpiphp_func, sibling); | |
786 | ||
2f523b15 | 787 | if (func->flags & FUNC_HAS_PS3) { |
1da177e4 LT |
788 | status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL); |
789 | if (ACPI_FAILURE(status)) { | |
790 | warn("%s: _PS3 failed\n", __FUNCTION__); | |
791 | retval = -1; | |
792 | goto err_exit; | |
793 | } else | |
794 | break; | |
795 | } | |
796 | } | |
797 | ||
1da177e4 LT |
798 | /* TBD: evaluate _STA to check if the slot is disabled */ |
799 | ||
800 | slot->flags &= (~SLOT_POWEREDON); | |
801 | ||
802 | err_exit: | |
803 | return retval; | |
804 | } | |
805 | ||
806 | ||
15a1ae74 KA |
807 | |
808 | /** | |
809 | * acpiphp_max_busnr - return the highest reserved bus number under | |
810 | * the given bus. | |
811 | * @bus: bus to start search with | |
812 | * | |
813 | */ | |
814 | static unsigned char acpiphp_max_busnr(struct pci_bus *bus) | |
815 | { | |
816 | struct list_head *tmp; | |
817 | unsigned char max, n; | |
818 | ||
819 | /* | |
820 | * pci_bus_max_busnr will return the highest | |
821 | * reserved busnr for all these children. | |
822 | * that is equivalent to the bus->subordinate | |
823 | * value. We don't want to use the parent's | |
824 | * bus->subordinate value because it could have | |
825 | * padding in it. | |
826 | */ | |
827 | max = bus->secondary; | |
828 | ||
829 | list_for_each(tmp, &bus->children) { | |
830 | n = pci_bus_max_busnr(pci_bus_b(tmp)); | |
831 | if (n > max) | |
832 | max = n; | |
833 | } | |
834 | return max; | |
835 | } | |
836 | ||
837 | ||
838 | ||
839 | /** | |
840 | * get_func - get a pointer to acpiphp_func given a slot, device | |
841 | * @slot: slot to search | |
842 | * @dev: pci_dev struct to match. | |
843 | * | |
844 | * This function will increase the reference count of pci_dev, | |
845 | * so callers should call pci_dev_put when complete. | |
846 | * | |
847 | */ | |
848 | static struct acpiphp_func * | |
849 | get_func(struct acpiphp_slot *slot, struct pci_dev *dev) | |
850 | { | |
851 | struct acpiphp_func *func = NULL; | |
852 | struct pci_bus *bus = slot->bridge->pci_bus; | |
853 | struct pci_dev *pdev; | |
854 | ||
855 | list_for_each_entry(func, &slot->funcs, sibling) { | |
856 | pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, | |
857 | func->function)); | |
858 | if (pdev) { | |
859 | if (pdev == dev) | |
860 | break; | |
861 | pci_dev_put(pdev); | |
862 | } | |
863 | } | |
864 | return func; | |
865 | } | |
866 | ||
867 | ||
868 | /** | |
869 | * acpiphp_bus_add - add a new bus to acpi subsystem | |
870 | * @func: acpiphp_func of the bridge | |
871 | * | |
872 | */ | |
873 | static int acpiphp_bus_add(struct acpiphp_func *func) | |
874 | { | |
875 | acpi_handle phandle; | |
876 | struct acpi_device *device, *pdevice; | |
877 | int ret_val; | |
878 | ||
879 | acpi_get_parent(func->handle, &phandle); | |
880 | if (acpi_bus_get_device(phandle, &pdevice)) { | |
881 | dbg("no parent device, assuming NULL\n"); | |
882 | pdevice = NULL; | |
883 | } | |
20416ea5 KA |
884 | if (!acpi_bus_get_device(func->handle, &device)) { |
885 | dbg("bus exists... trim\n"); | |
886 | /* this shouldn't be in here, so remove | |
887 | * the bus then re-add it... | |
888 | */ | |
889 | ret_val = acpi_bus_trim(device, 1); | |
890 | dbg("acpi_bus_trim return %x\n", ret_val); | |
891 | } | |
892 | ||
893 | ret_val = acpi_bus_add(&device, pdevice, func->handle, | |
894 | ACPI_BUS_TYPE_DEVICE); | |
895 | if (ret_val) { | |
896 | dbg("error adding bus, %x\n", | |
897 | -ret_val); | |
898 | goto acpiphp_bus_add_out; | |
15a1ae74 KA |
899 | } |
900 | /* | |
901 | * try to start anyway. We could have failed to add | |
902 | * simply because this bus had previously been added | |
903 | * on another add. Don't bother with the return value | |
904 | * we just keep going. | |
905 | */ | |
906 | ret_val = acpi_bus_start(device); | |
907 | ||
908 | acpiphp_bus_add_out: | |
909 | return ret_val; | |
910 | } | |
911 | ||
912 | ||
913 | ||
1da177e4 LT |
914 | /** |
915 | * enable_device - enable, configure a slot | |
916 | * @slot: slot to be enabled | |
917 | * | |
918 | * This function should be called per *physical slot*, | |
919 | * not per each slot object in ACPI namespace. | |
920 | * | |
921 | */ | |
922 | static int enable_device(struct acpiphp_slot *slot) | |
923 | { | |
1da177e4 | 924 | struct pci_dev *dev; |
42f49a6a | 925 | struct pci_bus *bus = slot->bridge->pci_bus; |
1da177e4 LT |
926 | struct list_head *l; |
927 | struct acpiphp_func *func; | |
928 | int retval = 0; | |
42f49a6a | 929 | int num, max, pass; |
1da177e4 LT |
930 | |
931 | if (slot->flags & SLOT_ENABLED) | |
932 | goto err_exit; | |
933 | ||
934 | /* sanity check: dev should be NULL when hot-plugged in */ | |
42f49a6a | 935 | dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0)); |
1da177e4 LT |
936 | if (dev) { |
937 | /* This case shouldn't happen */ | |
938 | err("pci_dev structure already exists.\n"); | |
42f49a6a | 939 | pci_dev_put(dev); |
1da177e4 LT |
940 | retval = -1; |
941 | goto err_exit; | |
942 | } | |
943 | ||
42f49a6a RS |
944 | num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0)); |
945 | if (num == 0) { | |
1da177e4 LT |
946 | err("No new device found\n"); |
947 | retval = -1; | |
948 | goto err_exit; | |
949 | } | |
950 | ||
15a1ae74 | 951 | max = acpiphp_max_busnr(bus); |
42f49a6a RS |
952 | for (pass = 0; pass < 2; pass++) { |
953 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
954 | if (PCI_SLOT(dev->devfn) != slot->device) | |
955 | continue; | |
956 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || | |
c64b5eea | 957 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { |
42f49a6a | 958 | max = pci_scan_bridge(bus, dev, max, pass); |
15a1ae74 | 959 | if (pass && dev->subordinate) { |
c64b5eea | 960 | pci_bus_size_bridges(dev->subordinate); |
15a1ae74 KA |
961 | func = get_func(slot, dev); |
962 | if (func) { | |
963 | acpiphp_bus_add(func); | |
964 | /* side effect of get_func */ | |
965 | pci_dev_put(dev); | |
966 | } | |
967 | } | |
c64b5eea | 968 | } |
42f49a6a | 969 | } |
1da177e4 LT |
970 | } |
971 | ||
42f49a6a | 972 | pci_bus_assign_resources(bus); |
8e5dce35 KA |
973 | acpiphp_sanitize_bus(bus); |
974 | pci_enable_bridges(bus); | |
42f49a6a | 975 | pci_bus_add_devices(bus); |
0cccd0c2 MT |
976 | acpiphp_set_hpp_values(slot->bridge->handle, bus); |
977 | acpiphp_configure_ioapics(slot->bridge->handle); | |
42f49a6a | 978 | |
1da177e4 LT |
979 | /* associate pci_dev to our representation */ |
980 | list_for_each (l, &slot->funcs) { | |
981 | func = list_entry(l, struct acpiphp_func, sibling); | |
42f49a6a | 982 | func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device, |
1da177e4 | 983 | func->function)); |
1da177e4 LT |
984 | } |
985 | ||
986 | slot->flags |= SLOT_ENABLED; | |
987 | ||
1da177e4 LT |
988 | err_exit: |
989 | return retval; | |
990 | } | |
991 | ||
992 | ||
993 | /** | |
994 | * disable_device - disable a slot | |
995 | */ | |
996 | static int disable_device(struct acpiphp_slot *slot) | |
997 | { | |
998 | int retval = 0; | |
999 | struct acpiphp_func *func; | |
1000 | struct list_head *l; | |
1001 | ||
1002 | /* is this slot already disabled? */ | |
1003 | if (!(slot->flags & SLOT_ENABLED)) | |
1004 | goto err_exit; | |
1005 | ||
1006 | list_for_each (l, &slot->funcs) { | |
1007 | func = list_entry(l, struct acpiphp_func, sibling); | |
42f49a6a RS |
1008 | if (!func->pci_dev) |
1009 | continue; | |
1da177e4 | 1010 | |
42f49a6a RS |
1011 | pci_remove_bus_device(func->pci_dev); |
1012 | pci_dev_put(func->pci_dev); | |
1013 | func->pci_dev = NULL; | |
1da177e4 LT |
1014 | } |
1015 | ||
1016 | slot->flags &= (~SLOT_ENABLED); | |
1017 | ||
1018 | err_exit: | |
1019 | return retval; | |
1020 | } | |
1021 | ||
1022 | ||
1023 | /** | |
1024 | * get_slot_status - get ACPI slot status | |
1025 | * | |
1026 | * if a slot has _STA for each function and if any one of them | |
1027 | * returned non-zero status, return it | |
1028 | * | |
1029 | * if a slot doesn't have _STA and if any one of its functions' | |
1030 | * configuration space is configured, return 0x0f as a _STA | |
1031 | * | |
1032 | * otherwise return 0 | |
1033 | */ | |
1034 | static unsigned int get_slot_status(struct acpiphp_slot *slot) | |
1035 | { | |
1036 | acpi_status status; | |
1037 | unsigned long sta = 0; | |
1038 | u32 dvid; | |
1039 | struct list_head *l; | |
1040 | struct acpiphp_func *func; | |
1041 | ||
1042 | list_for_each (l, &slot->funcs) { | |
1043 | func = list_entry(l, struct acpiphp_func, sibling); | |
1044 | ||
1045 | if (func->flags & FUNC_HAS_STA) { | |
1046 | status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta); | |
1047 | if (ACPI_SUCCESS(status) && sta) | |
1048 | break; | |
1049 | } else { | |
1050 | pci_bus_read_config_dword(slot->bridge->pci_bus, | |
1051 | PCI_DEVFN(slot->device, | |
1052 | func->function), | |
1053 | PCI_VENDOR_ID, &dvid); | |
1054 | if (dvid != 0xffffffff) { | |
1055 | sta = ACPI_STA_ALL; | |
1056 | break; | |
1057 | } | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | return (unsigned int)sta; | |
1062 | } | |
1063 | ||
8d50e332 RS |
1064 | /** |
1065 | * acpiphp_eject_slot - physically eject the slot | |
1066 | */ | |
1067 | static int acpiphp_eject_slot(struct acpiphp_slot *slot) | |
1068 | { | |
1069 | acpi_status status; | |
1070 | struct acpiphp_func *func; | |
1071 | struct list_head *l; | |
1072 | struct acpi_object_list arg_list; | |
1073 | union acpi_object arg; | |
1074 | ||
1075 | list_for_each (l, &slot->funcs) { | |
1076 | func = list_entry(l, struct acpiphp_func, sibling); | |
1077 | ||
1078 | /* We don't want to call _EJ0 on non-existing functions. */ | |
1079 | if ((func->flags & FUNC_HAS_EJ0)) { | |
1080 | /* _EJ0 method take one argument */ | |
1081 | arg_list.count = 1; | |
1082 | arg_list.pointer = &arg; | |
1083 | arg.type = ACPI_TYPE_INTEGER; | |
1084 | arg.integer.value = 1; | |
1085 | ||
1086 | status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL); | |
1087 | if (ACPI_FAILURE(status)) { | |
1088 | warn("%s: _EJ0 failed\n", __FUNCTION__); | |
1089 | return -1; | |
1090 | } else | |
1091 | break; | |
1092 | } | |
1093 | } | |
1094 | return 0; | |
1095 | } | |
1096 | ||
1da177e4 LT |
1097 | /** |
1098 | * acpiphp_check_bridge - re-enumerate devices | |
1099 | * | |
1100 | * Iterate over all slots under this bridge and make sure that if a | |
1101 | * card is present they are enabled, and if not they are disabled. | |
1102 | */ | |
1103 | static int acpiphp_check_bridge(struct acpiphp_bridge *bridge) | |
1104 | { | |
1105 | struct acpiphp_slot *slot; | |
1106 | int retval = 0; | |
1107 | int enabled, disabled; | |
1108 | ||
1109 | enabled = disabled = 0; | |
1110 | ||
1111 | for (slot = bridge->slots; slot; slot = slot->next) { | |
1112 | unsigned int status = get_slot_status(slot); | |
1113 | if (slot->flags & SLOT_ENABLED) { | |
1114 | if (status == ACPI_STA_ALL) | |
1115 | continue; | |
1116 | retval = acpiphp_disable_slot(slot); | |
1117 | if (retval) { | |
1118 | err("Error occurred in disabling\n"); | |
1119 | goto err_exit; | |
8d50e332 RS |
1120 | } else { |
1121 | acpiphp_eject_slot(slot); | |
1da177e4 LT |
1122 | } |
1123 | disabled++; | |
1124 | } else { | |
1125 | if (status != ACPI_STA_ALL) | |
1126 | continue; | |
1127 | retval = acpiphp_enable_slot(slot); | |
1128 | if (retval) { | |
1129 | err("Error occurred in enabling\n"); | |
1130 | goto err_exit; | |
1131 | } | |
1132 | enabled++; | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled); | |
1137 | ||
1138 | err_exit: | |
1139 | return retval; | |
1140 | } | |
1141 | ||
8e7561cf RS |
1142 | static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge) |
1143 | { | |
1144 | u16 pci_cmd, pci_bctl; | |
1145 | struct pci_dev *cdev; | |
1146 | ||
1147 | /* Program hpp values for this device */ | |
1148 | if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL || | |
1149 | (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE && | |
1150 | (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI))) | |
1151 | return; | |
1152 | pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, | |
1153 | bridge->hpp.cache_line_size); | |
1154 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, | |
1155 | bridge->hpp.latency_timer); | |
1156 | pci_read_config_word(dev, PCI_COMMAND, &pci_cmd); | |
1157 | if (bridge->hpp.enable_SERR) | |
1158 | pci_cmd |= PCI_COMMAND_SERR; | |
1159 | else | |
1160 | pci_cmd &= ~PCI_COMMAND_SERR; | |
1161 | if (bridge->hpp.enable_PERR) | |
1162 | pci_cmd |= PCI_COMMAND_PARITY; | |
1163 | else | |
1164 | pci_cmd &= ~PCI_COMMAND_PARITY; | |
1165 | pci_write_config_word(dev, PCI_COMMAND, pci_cmd); | |
1166 | ||
1167 | /* Program bridge control value and child devices */ | |
1168 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) { | |
1169 | pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, | |
1170 | bridge->hpp.latency_timer); | |
1171 | pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl); | |
1172 | if (bridge->hpp.enable_SERR) | |
1173 | pci_bctl |= PCI_BRIDGE_CTL_SERR; | |
1174 | else | |
1175 | pci_bctl &= ~PCI_BRIDGE_CTL_SERR; | |
1176 | if (bridge->hpp.enable_PERR) | |
1177 | pci_bctl |= PCI_BRIDGE_CTL_PARITY; | |
1178 | else | |
1179 | pci_bctl &= ~PCI_BRIDGE_CTL_PARITY; | |
1180 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl); | |
1181 | if (dev->subordinate) { | |
1182 | list_for_each_entry(cdev, &dev->subordinate->devices, | |
1183 | bus_list) | |
1184 | program_hpp(cdev, bridge); | |
1185 | } | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus) | |
1190 | { | |
1191 | struct acpiphp_bridge bridge; | |
1192 | struct pci_dev *dev; | |
1193 | ||
1194 | memset(&bridge, 0, sizeof(bridge)); | |
1195 | bridge.handle = handle; | |
1196 | decode_hpp(&bridge); | |
1197 | list_for_each_entry(dev, &bus->devices, bus_list) | |
1198 | program_hpp(dev, &bridge); | |
1199 | ||
1200 | } | |
1201 | ||
1202 | /* | |
1203 | * Remove devices for which we could not assign resources, call | |
1204 | * arch specific code to fix-up the bus | |
1205 | */ | |
1206 | static void acpiphp_sanitize_bus(struct pci_bus *bus) | |
1207 | { | |
1208 | struct pci_dev *dev; | |
1209 | int i; | |
1210 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM; | |
1211 | ||
1212 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
1213 | for (i=0; i<PCI_BRIDGE_RESOURCES; i++) { | |
1214 | struct resource *res = &dev->resource[i]; | |
1215 | if ((res->flags & type_mask) && !res->start && | |
1216 | res->end) { | |
1217 | /* Could not assign a required resources | |
1218 | * for this device, remove it */ | |
1219 | pci_remove_bus_device(dev); | |
1220 | break; | |
1221 | } | |
1222 | } | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | /* Program resources in newly inserted bridge */ | |
1227 | static int acpiphp_configure_bridge (acpi_handle handle) | |
1228 | { | |
1229 | struct acpi_pci_id pci_id; | |
1230 | struct pci_bus *bus; | |
1231 | ||
1232 | if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) { | |
1233 | err("cannot get PCI domain and bus number for bridge\n"); | |
1234 | return -EINVAL; | |
1235 | } | |
1236 | bus = pci_find_bus(pci_id.segment, pci_id.bus); | |
1237 | if (!bus) { | |
1238 | err("cannot find bus %d:%d\n", | |
1239 | pci_id.segment, pci_id.bus); | |
1240 | return -EINVAL; | |
1241 | } | |
1242 | ||
1243 | pci_bus_size_bridges(bus); | |
1244 | pci_bus_assign_resources(bus); | |
1245 | acpiphp_sanitize_bus(bus); | |
1246 | acpiphp_set_hpp_values(handle, bus); | |
1247 | pci_enable_bridges(bus); | |
a0d399a8 | 1248 | acpiphp_configure_ioapics(handle); |
8e7561cf RS |
1249 | return 0; |
1250 | } | |
1251 | ||
1252 | static void handle_bridge_insertion(acpi_handle handle, u32 type) | |
1253 | { | |
1254 | struct acpi_device *device, *pdevice; | |
1255 | acpi_handle phandle; | |
1256 | ||
1257 | if ((type != ACPI_NOTIFY_BUS_CHECK) && | |
1258 | (type != ACPI_NOTIFY_DEVICE_CHECK)) { | |
1259 | err("unexpected notification type %d\n", type); | |
1260 | return; | |
1261 | } | |
1262 | ||
1263 | acpi_get_parent(handle, &phandle); | |
1264 | if (acpi_bus_get_device(phandle, &pdevice)) { | |
1265 | dbg("no parent device, assuming NULL\n"); | |
1266 | pdevice = NULL; | |
1267 | } | |
1268 | if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) { | |
1269 | err("cannot add bridge to acpi list\n"); | |
1270 | return; | |
1271 | } | |
1272 | if (!acpiphp_configure_bridge(handle) && | |
1273 | !acpi_bus_start(device)) | |
1274 | add_bridge(handle); | |
1275 | else | |
1276 | err("cannot configure and start bridge\n"); | |
1277 | ||
1278 | } | |
1279 | ||
1da177e4 LT |
1280 | /* |
1281 | * ACPI event handlers | |
1282 | */ | |
1283 | ||
1284 | /** | |
1285 | * handle_hotplug_event_bridge - handle ACPI event on bridges | |
1286 | * | |
1287 | * @handle: Notify()'ed acpi_handle | |
1288 | * @type: Notify code | |
1289 | * @context: pointer to acpiphp_bridge structure | |
1290 | * | |
1291 | * handles ACPI event notification on {host,p2p} bridges | |
1292 | * | |
1293 | */ | |
1294 | static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context) | |
1295 | { | |
1296 | struct acpiphp_bridge *bridge; | |
1297 | char objname[64]; | |
1298 | struct acpi_buffer buffer = { .length = sizeof(objname), | |
1299 | .pointer = objname }; | |
8e7561cf | 1300 | struct acpi_device *device; |
1da177e4 | 1301 | |
8e7561cf RS |
1302 | if (acpi_bus_get_device(handle, &device)) { |
1303 | /* This bridge must have just been physically inserted */ | |
1304 | handle_bridge_insertion(handle, type); | |
1305 | return; | |
1306 | } | |
1307 | ||
1308 | bridge = acpiphp_handle_to_bridge(handle); | |
1309 | if (!bridge) { | |
1310 | err("cannot get bridge info\n"); | |
1311 | return; | |
1312 | } | |
1da177e4 LT |
1313 | |
1314 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | |
1315 | ||
1316 | switch (type) { | |
1317 | case ACPI_NOTIFY_BUS_CHECK: | |
1318 | /* bus re-enumerate */ | |
1319 | dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname); | |
1320 | acpiphp_check_bridge(bridge); | |
1321 | break; | |
1322 | ||
1323 | case ACPI_NOTIFY_DEVICE_CHECK: | |
1324 | /* device check */ | |
1325 | dbg("%s: Device check notify on %s\n", __FUNCTION__, objname); | |
1326 | acpiphp_check_bridge(bridge); | |
1327 | break; | |
1328 | ||
1329 | case ACPI_NOTIFY_DEVICE_WAKE: | |
1330 | /* wake event */ | |
1331 | dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname); | |
1332 | break; | |
1333 | ||
1334 | case ACPI_NOTIFY_EJECT_REQUEST: | |
1335 | /* request device eject */ | |
1336 | dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname); | |
1337 | break; | |
1338 | ||
1339 | case ACPI_NOTIFY_FREQUENCY_MISMATCH: | |
1340 | printk(KERN_ERR "Device %s cannot be configured due" | |
1341 | " to a frequency mismatch\n", objname); | |
1342 | break; | |
1343 | ||
1344 | case ACPI_NOTIFY_BUS_MODE_MISMATCH: | |
1345 | printk(KERN_ERR "Device %s cannot be configured due" | |
1346 | " to a bus mode mismatch\n", objname); | |
1347 | break; | |
1348 | ||
1349 | case ACPI_NOTIFY_POWER_FAULT: | |
1350 | printk(KERN_ERR "Device %s has suffered a power fault\n", | |
1351 | objname); | |
1352 | break; | |
1353 | ||
1354 | default: | |
1355 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); | |
1356 | break; | |
1357 | } | |
1358 | } | |
1359 | ||
1da177e4 LT |
1360 | /** |
1361 | * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots) | |
1362 | * | |
1363 | * @handle: Notify()'ed acpi_handle | |
1364 | * @type: Notify code | |
1365 | * @context: pointer to acpiphp_func structure | |
1366 | * | |
1367 | * handles ACPI event notification on slots | |
1368 | * | |
1369 | */ | |
20416ea5 | 1370 | void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context) |
1da177e4 LT |
1371 | { |
1372 | struct acpiphp_func *func; | |
1373 | char objname[64]; | |
1374 | struct acpi_buffer buffer = { .length = sizeof(objname), | |
1375 | .pointer = objname }; | |
1376 | ||
1377 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | |
1378 | ||
1379 | func = (struct acpiphp_func *)context; | |
1380 | ||
1381 | switch (type) { | |
1382 | case ACPI_NOTIFY_BUS_CHECK: | |
1383 | /* bus re-enumerate */ | |
1384 | dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname); | |
1385 | acpiphp_enable_slot(func->slot); | |
1386 | break; | |
1387 | ||
1388 | case ACPI_NOTIFY_DEVICE_CHECK: | |
1389 | /* device check : re-enumerate from parent bus */ | |
1390 | dbg("%s: Device check notify on %s\n", __FUNCTION__, objname); | |
1391 | acpiphp_check_bridge(func->slot->bridge); | |
1392 | break; | |
1393 | ||
1394 | case ACPI_NOTIFY_DEVICE_WAKE: | |
1395 | /* wake event */ | |
1396 | dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname); | |
1397 | break; | |
1398 | ||
1399 | case ACPI_NOTIFY_EJECT_REQUEST: | |
1400 | /* request device eject */ | |
1401 | dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname); | |
8d50e332 RS |
1402 | if (!(acpiphp_disable_slot(func->slot))) |
1403 | acpiphp_eject_slot(func->slot); | |
1da177e4 LT |
1404 | break; |
1405 | ||
1406 | default: | |
1407 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); | |
1408 | break; | |
1409 | } | |
1410 | } | |
1411 | ||
8e7561cf RS |
1412 | static int is_root_bridge(acpi_handle handle) |
1413 | { | |
1414 | acpi_status status; | |
1415 | struct acpi_device_info *info; | |
1416 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; | |
1417 | int i; | |
1418 | ||
1419 | status = acpi_get_object_info(handle, &buffer); | |
1420 | if (ACPI_SUCCESS(status)) { | |
1421 | info = buffer.pointer; | |
1422 | if ((info->valid & ACPI_VALID_HID) && | |
1423 | !strcmp(PCI_ROOT_HID_STRING, | |
1424 | info->hardware_id.value)) { | |
1425 | acpi_os_free(buffer.pointer); | |
1426 | return 1; | |
1427 | } | |
1428 | if (info->valid & ACPI_VALID_CID) { | |
1429 | for (i=0; i < info->compatibility_id.count; i++) { | |
1430 | if (!strcmp(PCI_ROOT_HID_STRING, | |
1431 | info->compatibility_id.id[i].value)) { | |
1432 | acpi_os_free(buffer.pointer); | |
1433 | return 1; | |
1434 | } | |
1435 | } | |
1436 | } | |
1437 | } | |
1438 | return 0; | |
1439 | } | |
1440 | ||
1441 | static acpi_status | |
1442 | find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv) | |
1443 | { | |
1444 | int *count = (int *)context; | |
1445 | ||
1446 | if (is_root_bridge(handle)) { | |
1447 | acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, | |
1448 | handle_hotplug_event_bridge, NULL); | |
1449 | (*count)++; | |
1450 | } | |
1451 | return AE_OK ; | |
1452 | } | |
1da177e4 LT |
1453 | |
1454 | static struct acpi_pci_driver acpi_pci_hp_driver = { | |
1455 | .add = add_bridge, | |
1456 | .remove = remove_bridge, | |
1457 | }; | |
1458 | ||
1459 | /** | |
1460 | * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures | |
1461 | * | |
1462 | */ | |
1463 | int __init acpiphp_glue_init(void) | |
1464 | { | |
8e7561cf | 1465 | int num = 0; |
1da177e4 | 1466 | |
8e7561cf RS |
1467 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
1468 | ACPI_UINT32_MAX, find_root_bridges, &num, NULL); | |
1da177e4 LT |
1469 | |
1470 | if (num <= 0) | |
1471 | return -1; | |
8e7561cf RS |
1472 | else |
1473 | acpi_pci_register_driver(&acpi_pci_hp_driver); | |
1da177e4 LT |
1474 | |
1475 | return 0; | |
1476 | } | |
1477 | ||
1478 | ||
1479 | /** | |
1480 | * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures | |
1481 | * | |
1482 | * This function frees all data allocated in acpiphp_glue_init() | |
1483 | */ | |
1484 | void __exit acpiphp_glue_exit(void) | |
1485 | { | |
1da177e4 LT |
1486 | acpi_pci_unregister_driver(&acpi_pci_hp_driver); |
1487 | } | |
1488 | ||
1489 | ||
1490 | /** | |
1491 | * acpiphp_get_num_slots - count number of slots in a system | |
1492 | */ | |
1493 | int __init acpiphp_get_num_slots(void) | |
1494 | { | |
1495 | struct list_head *node; | |
1496 | struct acpiphp_bridge *bridge; | |
1497 | int num_slots; | |
1498 | ||
1499 | num_slots = 0; | |
1500 | ||
1501 | list_for_each (node, &bridge_list) { | |
1502 | bridge = (struct acpiphp_bridge *)node; | |
42f49a6a RS |
1503 | dbg("Bus %04x:%02x has %d slot%s\n", |
1504 | pci_domain_nr(bridge->pci_bus), | |
1505 | bridge->pci_bus->number, bridge->nr_slots, | |
1506 | bridge->nr_slots == 1 ? "" : "s"); | |
1da177e4 LT |
1507 | num_slots += bridge->nr_slots; |
1508 | } | |
1509 | ||
42f49a6a | 1510 | dbg("Total %d slots\n", num_slots); |
1da177e4 LT |
1511 | return num_slots; |
1512 | } | |
1513 | ||
1514 | ||
1515 | #if 0 | |
1516 | /** | |
1517 | * acpiphp_for_each_slot - call function for each slot | |
1518 | * @fn: callback function | |
1519 | * @data: context to be passed to callback function | |
1520 | * | |
1521 | */ | |
1522 | static int acpiphp_for_each_slot(acpiphp_callback fn, void *data) | |
1523 | { | |
1524 | struct list_head *node; | |
1525 | struct acpiphp_bridge *bridge; | |
1526 | struct acpiphp_slot *slot; | |
1527 | int retval = 0; | |
1528 | ||
1529 | list_for_each (node, &bridge_list) { | |
1530 | bridge = (struct acpiphp_bridge *)node; | |
1531 | for (slot = bridge->slots; slot; slot = slot->next) { | |
1532 | retval = fn(slot, data); | |
1533 | if (!retval) | |
1534 | goto err_exit; | |
1535 | } | |
1536 | } | |
1537 | ||
1538 | err_exit: | |
1539 | return retval; | |
1540 | } | |
1541 | #endif | |
1542 | ||
1da177e4 LT |
1543 | |
1544 | /** | |
1545 | * acpiphp_enable_slot - power on slot | |
1546 | */ | |
1547 | int acpiphp_enable_slot(struct acpiphp_slot *slot) | |
1548 | { | |
1549 | int retval; | |
1550 | ||
6aa4cdd0 | 1551 | mutex_lock(&slot->crit_sect); |
1da177e4 LT |
1552 | |
1553 | /* wake up all functions */ | |
1554 | retval = power_on_slot(slot); | |
1555 | if (retval) | |
1556 | goto err_exit; | |
1557 | ||
1558 | if (get_slot_status(slot) == ACPI_STA_ALL) | |
1559 | /* configure all functions */ | |
1560 | retval = enable_device(slot); | |
1561 | ||
1562 | err_exit: | |
6aa4cdd0 | 1563 | mutex_unlock(&slot->crit_sect); |
1da177e4 LT |
1564 | return retval; |
1565 | } | |
1566 | ||
1da177e4 LT |
1567 | /** |
1568 | * acpiphp_disable_slot - power off slot | |
1569 | */ | |
1570 | int acpiphp_disable_slot(struct acpiphp_slot *slot) | |
1571 | { | |
1572 | int retval = 0; | |
1573 | ||
6aa4cdd0 | 1574 | mutex_lock(&slot->crit_sect); |
1da177e4 LT |
1575 | |
1576 | /* unconfigure all functions */ | |
1577 | retval = disable_device(slot); | |
1578 | if (retval) | |
1579 | goto err_exit; | |
1580 | ||
1581 | /* power off all functions */ | |
1582 | retval = power_off_slot(slot); | |
1583 | if (retval) | |
1584 | goto err_exit; | |
1585 | ||
1da177e4 | 1586 | err_exit: |
6aa4cdd0 | 1587 | mutex_unlock(&slot->crit_sect); |
1da177e4 LT |
1588 | return retval; |
1589 | } | |
1590 | ||
1591 | ||
1592 | /* | |
1593 | * slot enabled: 1 | |
1594 | * slot disabled: 0 | |
1595 | */ | |
1596 | u8 acpiphp_get_power_status(struct acpiphp_slot *slot) | |
1597 | { | |
8d50e332 | 1598 | return (slot->flags & SLOT_POWEREDON); |
1da177e4 LT |
1599 | } |
1600 | ||
1601 | ||
1602 | /* | |
1603 | * latch closed: 1 | |
1604 | * latch open: 0 | |
1605 | */ | |
1606 | u8 acpiphp_get_latch_status(struct acpiphp_slot *slot) | |
1607 | { | |
1608 | unsigned int sta; | |
1609 | ||
1610 | sta = get_slot_status(slot); | |
1611 | ||
1612 | return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0; | |
1613 | } | |
1614 | ||
1615 | ||
1616 | /* | |
1617 | * adapter presence : 1 | |
1618 | * absence : 0 | |
1619 | */ | |
1620 | u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot) | |
1621 | { | |
1622 | unsigned int sta; | |
1623 | ||
1624 | sta = get_slot_status(slot); | |
1625 | ||
1626 | return (sta == 0) ? 0 : 1; | |
1627 | } | |
1628 | ||
1629 | ||
1630 | /* | |
1631 | * pci address (seg/bus/dev) | |
1632 | */ | |
1633 | u32 acpiphp_get_address(struct acpiphp_slot *slot) | |
1634 | { | |
1635 | u32 address; | |
42f49a6a | 1636 | struct pci_bus *pci_bus = slot->bridge->pci_bus; |
1da177e4 | 1637 | |
42f49a6a RS |
1638 | address = (pci_domain_nr(pci_bus) << 16) | |
1639 | (pci_bus->number << 8) | | |
1da177e4 LT |
1640 | slot->device; |
1641 | ||
1642 | return address; | |
1643 | } |