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