Merge branch 'ull' into test
[linux-2.6/mini2440.git] / drivers / pci / hotplug / acpiphp_glue.c
blob955aae4071f7a64eeb5cb716aaacf8ebb26c7a11
1 /*
2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
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
7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
12 * All rights reserved.
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.
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.
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.
29 * Send feedback to <kristen.c.accardi@intel.com>
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.
43 #include <linux/init.h>
44 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/pci.h>
48 #include <linux/pci_hotplug.h>
49 #include <linux/mutex.h>
51 #include "../pci.h"
52 #include "acpiphp.h"
54 static LIST_HEAD(bridge_list);
55 static LIST_HEAD(ioapic_list);
56 static DEFINE_SPINLOCK(ioapic_list_lock);
58 #define MY_NAME "acpiphp_glue"
60 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
61 static void acpiphp_sanitize_bus(struct pci_bus *bus);
62 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
63 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
67 * initialization & terminatation routines
70 /**
71 * is_ejectable - determine if a slot is ejectable
72 * @handle: handle to acpi namespace
74 * Ejectable slot should satisfy at least these conditions:
76 * 1. has _ADR method
77 * 2. has _EJ0 method
79 * optionally
81 * 1. has _STA method
82 * 2. has _PS0 method
83 * 3. has _PS3 method
84 * 4. ..
86 static int is_ejectable(acpi_handle handle)
88 acpi_status status;
89 acpi_handle tmp;
91 status = acpi_get_handle(handle, "_ADR", &tmp);
92 if (ACPI_FAILURE(status)) {
93 return 0;
96 status = acpi_get_handle(handle, "_EJ0", &tmp);
97 if (ACPI_FAILURE(status)) {
98 return 0;
101 return 1;
105 /* callback routine to check for the existence of ejectable slots */
106 static acpi_status
107 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
109 int *count = (int *)context;
111 if (is_ejectable(handle)) {
112 (*count)++;
113 /* only one ejectable slot is enough */
114 return AE_CTRL_TERMINATE;
115 } else {
116 return AE_OK;
120 /* callback routine to check for the existence of a pci dock device */
121 static acpi_status
122 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
124 int *count = (int *)context;
126 if (is_dock_device(handle)) {
127 (*count)++;
128 return AE_CTRL_TERMINATE;
129 } else {
130 return AE_OK;
138 * the _DCK method can do funny things... and sometimes not
139 * hah-hah funny.
141 * TBD - figure out a way to only call fixups for
142 * systems that require them.
144 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
145 void *v)
147 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
148 struct pci_bus *bus = func->slot->bridge->pci_bus;
149 u32 buses;
151 if (!bus->self)
152 return NOTIFY_OK;
154 /* fixup bad _DCK function that rewrites
155 * secondary bridge on slot
157 pci_read_config_dword(bus->self,
158 PCI_PRIMARY_BUS,
159 &buses);
161 if (((buses >> 8) & 0xff) != bus->secondary) {
162 buses = (buses & 0xff000000)
163 | ((unsigned int)(bus->primary) << 0)
164 | ((unsigned int)(bus->secondary) << 8)
165 | ((unsigned int)(bus->subordinate) << 16);
166 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
168 return NOTIFY_OK;
172 static struct acpi_dock_ops acpiphp_dock_ops = {
173 .handler = handle_hotplug_event_func,
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)
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 long adr, sun;
186 int device, function, retval;
188 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
190 if (ACPI_FAILURE(status))
191 return AE_OK;
193 status = acpi_get_handle(handle, "_EJ0", &tmp);
195 if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
196 return AE_OK;
198 device = (adr >> 16) & 0xffff;
199 function = adr & 0xffff;
201 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
202 if (!newfunc)
203 return AE_NO_MEMORY;
205 INIT_LIST_HEAD(&newfunc->sibling);
206 newfunc->handle = handle;
207 newfunc->function = function;
208 if (ACPI_SUCCESS(status))
209 newfunc->flags = FUNC_HAS_EJ0;
211 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
212 newfunc->flags |= FUNC_HAS_STA;
214 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
215 newfunc->flags |= FUNC_HAS_PS0;
217 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
218 newfunc->flags |= FUNC_HAS_PS3;
220 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
221 newfunc->flags |= FUNC_HAS_DCK;
223 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
224 if (ACPI_FAILURE(status)) {
226 * use the count of the number of slots we've found
227 * for the number of the slot
229 sun = bridge->nr_slots+1;
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;
240 if (!slot) {
241 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
242 if (!slot) {
243 kfree(newfunc);
244 return AE_NO_MEMORY;
247 slot->bridge = bridge;
248 slot->device = device;
249 slot->sun = sun;
250 INIT_LIST_HEAD(&slot->funcs);
251 mutex_init(&slot->crit_sect);
253 slot->next = bridge->slots;
254 bridge->slots = slot;
256 bridge->nr_slots++;
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);
261 retval = acpiphp_register_hotplug_slot(slot);
262 if (retval) {
263 if (retval == -EBUSY)
264 warn("Slot %d already registered by another "
265 "hotplug driver\n", slot->sun);
266 else
267 warn("acpiphp_register_hotplug_slot failed "
268 "(err code = 0x%x)\n", retval);
269 goto err_exit;
273 newfunc->slot = slot;
274 list_add_tail(&newfunc->sibling, &slot->funcs);
276 /* associate corresponding pci_dev */
277 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
278 PCI_DEVFN(device, function));
279 if (newfunc->pci_dev) {
280 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
283 if (is_dock_device(handle)) {
284 /* we don't want to call this device's _EJ0
285 * because we want the dock notify handler
286 * to call it after it calls _DCK
288 newfunc->flags &= ~FUNC_HAS_EJ0;
289 if (register_hotplug_dock_device(handle,
290 &acpiphp_dock_ops, newfunc))
291 dbg("failed to register dock device\n");
293 /* we need to be notified when dock events happen
294 * outside of the hotplug operation, since we may
295 * need to do fixups before we can hotplug.
297 newfunc->nb.notifier_call = post_dock_fixups;
298 if (register_dock_notifier(&newfunc->nb))
299 dbg("failed to register a dock notifier");
302 /* install notify handler */
303 if (!(newfunc->flags & FUNC_HAS_DCK)) {
304 status = acpi_install_notify_handler(handle,
305 ACPI_SYSTEM_NOTIFY,
306 handle_hotplug_event_func,
307 newfunc);
309 if (ACPI_FAILURE(status))
310 err("failed to register interrupt notify handler\n");
311 } else
312 status = AE_OK;
314 return status;
316 err_exit:
317 bridge->nr_slots--;
318 bridge->slots = slot->next;
319 kfree(slot);
320 kfree(newfunc);
322 return AE_OK;
326 /* see if it's worth looking at this bridge */
327 static int detect_ejectable_slots(acpi_handle *bridge_handle)
329 acpi_status status;
330 int count;
332 count = 0;
334 /* only check slots defined directly below bridge object */
335 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
336 is_ejectable_slot, (void *)&count, NULL);
339 * we also need to add this bridge if there is a dock bridge or
340 * other pci device on a dock station (removable)
342 if (!count)
343 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
344 (u32)1, is_pci_dock_device, (void *)&count,
345 NULL);
347 return count;
351 /* decode ACPI 2.0 _HPP hot plug parameters */
352 static void decode_hpp(struct acpiphp_bridge *bridge)
354 acpi_status status;
356 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
357 if (ACPI_FAILURE(status) ||
358 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
359 /* use default numbers */
360 printk(KERN_WARNING
361 "%s: Could not get hotplug parameters. Use defaults\n",
362 __func__);
363 bridge->hpp.t0 = &bridge->hpp.type0_data;
364 bridge->hpp.t0->revision = 0;
365 bridge->hpp.t0->cache_line_size = 0x10;
366 bridge->hpp.t0->latency_timer = 0x40;
367 bridge->hpp.t0->enable_serr = 0;
368 bridge->hpp.t0->enable_perr = 0;
374 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
375 static void init_bridge_misc(struct acpiphp_bridge *bridge)
377 acpi_status status;
379 /* decode ACPI 2.0 _HPP (hot plug parameters) */
380 decode_hpp(bridge);
382 /* must be added to the list prior to calling register_slot */
383 list_add(&bridge->list, &bridge_list);
385 /* register all slot objects under this bridge */
386 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
387 register_slot, bridge, NULL);
388 if (ACPI_FAILURE(status)) {
389 list_del(&bridge->list);
390 return;
393 /* install notify handler */
394 if (bridge->type != BRIDGE_TYPE_HOST) {
395 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
396 status = acpi_remove_notify_handler(bridge->func->handle,
397 ACPI_SYSTEM_NOTIFY,
398 handle_hotplug_event_func);
399 if (ACPI_FAILURE(status))
400 err("failed to remove notify handler\n");
402 status = acpi_install_notify_handler(bridge->handle,
403 ACPI_SYSTEM_NOTIFY,
404 handle_hotplug_event_bridge,
405 bridge);
407 if (ACPI_FAILURE(status)) {
408 err("failed to register interrupt notify handler\n");
414 /* find acpiphp_func from acpiphp_bridge */
415 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
417 struct list_head *node, *l;
418 struct acpiphp_bridge *bridge;
419 struct acpiphp_slot *slot;
420 struct acpiphp_func *func;
422 list_for_each(node, &bridge_list) {
423 bridge = list_entry(node, struct acpiphp_bridge, list);
424 for (slot = bridge->slots; slot; slot = slot->next) {
425 list_for_each(l, &slot->funcs) {
426 func = list_entry(l, struct acpiphp_func,
427 sibling);
428 if (func->handle == handle)
429 return func;
434 return NULL;
438 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
440 acpi_handle dummy_handle;
442 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
443 "_STA", &dummy_handle)))
444 bridge->flags |= BRIDGE_HAS_STA;
446 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
447 "_EJ0", &dummy_handle)))
448 bridge->flags |= BRIDGE_HAS_EJ0;
450 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
451 "_PS0", &dummy_handle)))
452 bridge->flags |= BRIDGE_HAS_PS0;
454 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
455 "_PS3", &dummy_handle)))
456 bridge->flags |= BRIDGE_HAS_PS3;
458 /* is this ejectable p2p bridge? */
459 if (bridge->flags & BRIDGE_HAS_EJ0) {
460 struct acpiphp_func *func;
462 dbg("found ejectable p2p bridge\n");
464 /* make link between PCI bridge and PCI function */
465 func = acpiphp_bridge_handle_to_function(bridge->handle);
466 if (!func)
467 return;
468 bridge->func = func;
469 func->bridge = bridge;
474 /* allocate and initialize host bridge data structure */
475 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
477 struct acpiphp_bridge *bridge;
479 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
480 if (bridge == NULL)
481 return;
483 bridge->type = BRIDGE_TYPE_HOST;
484 bridge->handle = handle;
486 bridge->pci_bus = pci_bus;
488 spin_lock_init(&bridge->res_lock);
490 init_bridge_misc(bridge);
494 /* allocate and initialize PCI-to-PCI bridge data structure */
495 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
497 struct acpiphp_bridge *bridge;
499 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
500 if (bridge == NULL) {
501 err("out of memory\n");
502 return;
505 bridge->type = BRIDGE_TYPE_P2P;
506 bridge->handle = handle;
507 config_p2p_bridge_flags(bridge);
509 bridge->pci_dev = pci_dev_get(pci_dev);
510 bridge->pci_bus = pci_dev->subordinate;
511 if (!bridge->pci_bus) {
512 err("This is not a PCI-to-PCI bridge!\n");
513 goto err;
516 spin_lock_init(&bridge->res_lock);
518 init_bridge_misc(bridge);
519 return;
520 err:
521 pci_dev_put(pci_dev);
522 kfree(bridge);
523 return;
527 /* callback routine to find P2P bridges */
528 static acpi_status
529 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
531 acpi_status status;
532 acpi_handle dummy_handle;
533 unsigned long long tmp;
534 int device, function;
535 struct pci_dev *dev;
536 struct pci_bus *pci_bus = context;
538 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
539 if (ACPI_FAILURE(status))
540 return AE_OK; /* continue */
542 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
543 if (ACPI_FAILURE(status)) {
544 dbg("%s: _ADR evaluation failure\n", __func__);
545 return AE_OK;
548 device = (tmp >> 16) & 0xffff;
549 function = tmp & 0xffff;
551 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
553 if (!dev || !dev->subordinate)
554 goto out;
556 /* check if this bridge has ejectable slots */
557 if ((detect_ejectable_slots(handle) > 0)) {
558 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
559 add_p2p_bridge(handle, dev);
562 /* search P2P bridges under this p2p bridge */
563 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
564 find_p2p_bridge, dev->subordinate, NULL);
565 if (ACPI_FAILURE(status))
566 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
568 out:
569 pci_dev_put(dev);
570 return AE_OK;
574 /* find hot-pluggable slots, and then find P2P bridge */
575 static int add_bridge(acpi_handle handle)
577 acpi_status status;
578 unsigned long long tmp;
579 int seg, bus;
580 acpi_handle dummy_handle;
581 struct pci_bus *pci_bus;
583 /* if the bridge doesn't have _STA, we assume it is always there */
584 status = acpi_get_handle(handle, "_STA", &dummy_handle);
585 if (ACPI_SUCCESS(status)) {
586 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
587 if (ACPI_FAILURE(status)) {
588 dbg("%s: _STA evaluation failure\n", __func__);
589 return 0;
591 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
592 /* don't register this object */
593 return 0;
596 /* get PCI segment number */
597 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
599 seg = ACPI_SUCCESS(status) ? tmp : 0;
601 /* get PCI bus number */
602 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
604 if (ACPI_SUCCESS(status)) {
605 bus = tmp;
606 } else {
607 warn("can't get bus number, assuming 0\n");
608 bus = 0;
611 pci_bus = pci_find_bus(seg, bus);
612 if (!pci_bus) {
613 err("Can't find bus %04x:%02x\n", seg, bus);
614 return 0;
617 /* check if this bridge has ejectable slots */
618 if (detect_ejectable_slots(handle) > 0) {
619 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
620 add_host_bridge(handle, pci_bus);
623 /* search P2P bridges under this host bridge */
624 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
625 find_p2p_bridge, pci_bus, NULL);
627 if (ACPI_FAILURE(status))
628 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
630 return 0;
633 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
635 struct list_head *head;
636 list_for_each(head, &bridge_list) {
637 struct acpiphp_bridge *bridge = list_entry(head,
638 struct acpiphp_bridge, list);
639 if (bridge->handle == handle)
640 return bridge;
643 return NULL;
646 static void cleanup_bridge(struct acpiphp_bridge *bridge)
648 struct list_head *list, *tmp;
649 struct acpiphp_slot *slot;
650 acpi_status status;
651 acpi_handle handle = bridge->handle;
653 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
654 handle_hotplug_event_bridge);
655 if (ACPI_FAILURE(status))
656 err("failed to remove notify handler\n");
658 if ((bridge->type != BRIDGE_TYPE_HOST) &&
659 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
660 status = acpi_install_notify_handler(bridge->func->handle,
661 ACPI_SYSTEM_NOTIFY,
662 handle_hotplug_event_func,
663 bridge->func);
664 if (ACPI_FAILURE(status))
665 err("failed to install interrupt notify handler\n");
668 slot = bridge->slots;
669 while (slot) {
670 struct acpiphp_slot *next = slot->next;
671 list_for_each_safe (list, tmp, &slot->funcs) {
672 struct acpiphp_func *func;
673 func = list_entry(list, struct acpiphp_func, sibling);
674 if (is_dock_device(func->handle)) {
675 unregister_hotplug_dock_device(func->handle);
676 unregister_dock_notifier(&func->nb);
678 if (!(func->flags & FUNC_HAS_DCK)) {
679 status = acpi_remove_notify_handler(func->handle,
680 ACPI_SYSTEM_NOTIFY,
681 handle_hotplug_event_func);
682 if (ACPI_FAILURE(status))
683 err("failed to remove notify handler\n");
685 pci_dev_put(func->pci_dev);
686 list_del(list);
687 kfree(func);
689 acpiphp_unregister_hotplug_slot(slot);
690 list_del(&slot->funcs);
691 kfree(slot);
692 slot = next;
695 pci_dev_put(bridge->pci_dev);
696 list_del(&bridge->list);
697 kfree(bridge);
700 static acpi_status
701 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
703 struct acpiphp_bridge *bridge;
705 /* cleanup p2p bridges under this P2P bridge
706 in a depth-first manner */
707 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
708 cleanup_p2p_bridge, NULL, NULL);
710 bridge = acpiphp_handle_to_bridge(handle);
711 if (bridge)
712 cleanup_bridge(bridge);
714 return AE_OK;
717 static void remove_bridge(acpi_handle handle)
719 struct acpiphp_bridge *bridge;
721 /* cleanup p2p bridges under this host bridge
722 in a depth-first manner */
723 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
724 (u32)1, cleanup_p2p_bridge, NULL, NULL);
727 * On root bridges with hotplug slots directly underneath (ie,
728 * no p2p bridge inbetween), we call cleanup_bridge().
730 * The else clause cleans up root bridges that either had no
731 * hotplug slots at all, or had a p2p bridge underneath.
733 bridge = acpiphp_handle_to_bridge(handle);
734 if (bridge)
735 cleanup_bridge(bridge);
736 else
737 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
738 handle_hotplug_event_bridge);
741 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
743 struct acpi_pci_id id;
744 struct pci_bus *bus;
745 struct pci_dev *dev;
747 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
748 return NULL;
750 bus = pci_find_bus(id.segment, id.bus);
751 if (!bus)
752 return NULL;
754 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
755 if (!dev)
756 return NULL;
758 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
759 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
761 pci_dev_put(dev);
762 return NULL;
765 return dev;
768 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
770 acpi_status status;
771 int result = -1;
772 unsigned long long gsb;
773 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
774 union acpi_object *obj;
775 void *table;
777 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
778 if (ACPI_SUCCESS(status)) {
779 *gsi_base = (u32)gsb;
780 return 0;
783 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
784 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
785 return -1;
787 obj = buffer.pointer;
788 if (obj->type != ACPI_TYPE_BUFFER)
789 goto out;
791 table = obj->buffer.pointer;
792 switch (((struct acpi_subtable_header *)table)->type) {
793 case ACPI_MADT_TYPE_IO_SAPIC:
794 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
795 result = 0;
796 break;
797 case ACPI_MADT_TYPE_IO_APIC:
798 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
799 result = 0;
800 break;
801 default:
802 break;
804 out:
805 kfree(buffer.pointer);
806 return result;
809 static acpi_status
810 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
812 acpi_status status;
813 unsigned long long sta;
814 acpi_handle tmp;
815 struct pci_dev *pdev;
816 u32 gsi_base;
817 u64 phys_addr;
818 struct acpiphp_ioapic *ioapic;
820 /* Evaluate _STA if present */
821 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
822 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
823 return AE_CTRL_DEPTH;
825 /* Scan only PCI bus scope */
826 status = acpi_get_handle(handle, "_HID", &tmp);
827 if (ACPI_SUCCESS(status))
828 return AE_CTRL_DEPTH;
830 if (get_gsi_base(handle, &gsi_base))
831 return AE_OK;
833 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
834 if (!ioapic)
835 return AE_NO_MEMORY;
837 pdev = get_apic_pci_info(handle);
838 if (!pdev)
839 goto exit_kfree;
841 if (pci_enable_device(pdev))
842 goto exit_pci_dev_put;
844 pci_set_master(pdev);
846 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
847 goto exit_pci_disable_device;
849 phys_addr = pci_resource_start(pdev, 0);
850 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
851 goto exit_pci_release_region;
853 ioapic->gsi_base = gsi_base;
854 ioapic->dev = pdev;
855 spin_lock(&ioapic_list_lock);
856 list_add_tail(&ioapic->list, &ioapic_list);
857 spin_unlock(&ioapic_list_lock);
859 return AE_OK;
861 exit_pci_release_region:
862 pci_release_region(pdev, 0);
863 exit_pci_disable_device:
864 pci_disable_device(pdev);
865 exit_pci_dev_put:
866 pci_dev_put(pdev);
867 exit_kfree:
868 kfree(ioapic);
870 return AE_OK;
873 static acpi_status
874 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
876 acpi_status status;
877 unsigned long long sta;
878 acpi_handle tmp;
879 u32 gsi_base;
880 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
882 /* Evaluate _STA if present */
883 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
884 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
885 return AE_CTRL_DEPTH;
887 /* Scan only PCI bus scope */
888 status = acpi_get_handle(handle, "_HID", &tmp);
889 if (ACPI_SUCCESS(status))
890 return AE_CTRL_DEPTH;
892 if (get_gsi_base(handle, &gsi_base))
893 return AE_OK;
895 acpi_unregister_ioapic(handle, gsi_base);
897 spin_lock(&ioapic_list_lock);
898 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
899 if (pos->gsi_base != gsi_base)
900 continue;
901 ioapic = pos;
902 list_del(&ioapic->list);
903 break;
905 spin_unlock(&ioapic_list_lock);
907 if (!ioapic)
908 return AE_OK;
910 pci_release_region(ioapic->dev, 0);
911 pci_disable_device(ioapic->dev);
912 pci_dev_put(ioapic->dev);
913 kfree(ioapic);
915 return AE_OK;
918 static int acpiphp_configure_ioapics(acpi_handle handle)
920 ioapic_add(handle, 0, NULL, NULL);
921 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
922 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
923 return 0;
926 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
928 ioapic_remove(handle, 0, NULL, NULL);
929 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
930 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
931 return 0;
934 static int power_on_slot(struct acpiphp_slot *slot)
936 acpi_status status;
937 struct acpiphp_func *func;
938 struct list_head *l;
939 int retval = 0;
941 /* if already enabled, just skip */
942 if (slot->flags & SLOT_POWEREDON)
943 goto err_exit;
945 list_for_each (l, &slot->funcs) {
946 func = list_entry(l, struct acpiphp_func, sibling);
948 if (func->flags & FUNC_HAS_PS0) {
949 dbg("%s: executing _PS0\n", __func__);
950 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
951 if (ACPI_FAILURE(status)) {
952 warn("%s: _PS0 failed\n", __func__);
953 retval = -1;
954 goto err_exit;
955 } else
956 break;
960 /* TBD: evaluate _STA to check if the slot is enabled */
962 slot->flags |= SLOT_POWEREDON;
964 err_exit:
965 return retval;
969 static int power_off_slot(struct acpiphp_slot *slot)
971 acpi_status status;
972 struct acpiphp_func *func;
973 struct list_head *l;
975 int retval = 0;
977 /* if already disabled, just skip */
978 if ((slot->flags & SLOT_POWEREDON) == 0)
979 goto err_exit;
981 list_for_each (l, &slot->funcs) {
982 func = list_entry(l, struct acpiphp_func, sibling);
984 if (func->flags & FUNC_HAS_PS3) {
985 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
986 if (ACPI_FAILURE(status)) {
987 warn("%s: _PS3 failed\n", __func__);
988 retval = -1;
989 goto err_exit;
990 } else
991 break;
995 /* TBD: evaluate _STA to check if the slot is disabled */
997 slot->flags &= (~SLOT_POWEREDON);
999 err_exit:
1000 return retval;
1006 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
1007 * @bus: bus to start search with
1009 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
1011 struct list_head *tmp;
1012 unsigned char max, n;
1015 * pci_bus_max_busnr will return the highest
1016 * reserved busnr for all these children.
1017 * that is equivalent to the bus->subordinate
1018 * value. We don't want to use the parent's
1019 * bus->subordinate value because it could have
1020 * padding in it.
1022 max = bus->secondary;
1024 list_for_each(tmp, &bus->children) {
1025 n = pci_bus_max_busnr(pci_bus_b(tmp));
1026 if (n > max)
1027 max = n;
1029 return max;
1034 * acpiphp_bus_add - add a new bus to acpi subsystem
1035 * @func: acpiphp_func of the bridge
1037 static int acpiphp_bus_add(struct acpiphp_func *func)
1039 acpi_handle phandle;
1040 struct acpi_device *device, *pdevice;
1041 int ret_val;
1043 acpi_get_parent(func->handle, &phandle);
1044 if (acpi_bus_get_device(phandle, &pdevice)) {
1045 dbg("no parent device, assuming NULL\n");
1046 pdevice = NULL;
1048 if (!acpi_bus_get_device(func->handle, &device)) {
1049 dbg("bus exists... trim\n");
1050 /* this shouldn't be in here, so remove
1051 * the bus then re-add it...
1053 ret_val = acpi_bus_trim(device, 1);
1054 dbg("acpi_bus_trim return %x\n", ret_val);
1057 ret_val = acpi_bus_add(&device, pdevice, func->handle,
1058 ACPI_BUS_TYPE_DEVICE);
1059 if (ret_val) {
1060 dbg("error adding bus, %x\n",
1061 -ret_val);
1062 goto acpiphp_bus_add_out;
1065 * try to start anyway. We could have failed to add
1066 * simply because this bus had previously been added
1067 * on another add. Don't bother with the return value
1068 * we just keep going.
1070 ret_val = acpi_bus_start(device);
1072 acpiphp_bus_add_out:
1073 return ret_val;
1078 * acpiphp_bus_trim - trim a bus from acpi subsystem
1079 * @handle: handle to acpi namespace
1081 static int acpiphp_bus_trim(acpi_handle handle)
1083 struct acpi_device *device;
1084 int retval;
1086 retval = acpi_bus_get_device(handle, &device);
1087 if (retval) {
1088 dbg("acpi_device not found\n");
1089 return retval;
1092 retval = acpi_bus_trim(device, 1);
1093 if (retval)
1094 err("cannot remove from acpi list\n");
1096 return retval;
1100 * enable_device - enable, configure a slot
1101 * @slot: slot to be enabled
1103 * This function should be called per *physical slot*,
1104 * not per each slot object in ACPI namespace.
1106 static int __ref enable_device(struct acpiphp_slot *slot)
1108 struct pci_dev *dev;
1109 struct pci_bus *bus = slot->bridge->pci_bus;
1110 struct list_head *l;
1111 struct acpiphp_func *func;
1112 int retval = 0;
1113 int num, max, pass;
1114 acpi_status status;
1116 if (slot->flags & SLOT_ENABLED)
1117 goto err_exit;
1119 /* sanity check: dev should be NULL when hot-plugged in */
1120 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1121 if (dev) {
1122 /* This case shouldn't happen */
1123 err("pci_dev structure already exists.\n");
1124 pci_dev_put(dev);
1125 retval = -1;
1126 goto err_exit;
1129 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1130 if (num == 0) {
1131 err("No new device found\n");
1132 retval = -1;
1133 goto err_exit;
1136 max = acpiphp_max_busnr(bus);
1137 for (pass = 0; pass < 2; pass++) {
1138 list_for_each_entry(dev, &bus->devices, bus_list) {
1139 if (PCI_SLOT(dev->devfn) != slot->device)
1140 continue;
1141 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1142 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1143 max = pci_scan_bridge(bus, dev, max, pass);
1144 if (pass && dev->subordinate)
1145 pci_bus_size_bridges(dev->subordinate);
1150 list_for_each (l, &slot->funcs) {
1151 func = list_entry(l, struct acpiphp_func, sibling);
1152 acpiphp_bus_add(func);
1155 pci_bus_assign_resources(bus);
1156 acpiphp_sanitize_bus(bus);
1157 acpiphp_set_hpp_values(slot->bridge->handle, bus);
1158 list_for_each_entry(func, &slot->funcs, sibling)
1159 acpiphp_configure_ioapics(func->handle);
1160 pci_enable_bridges(bus);
1161 pci_bus_add_devices(bus);
1163 /* associate pci_dev to our representation */
1164 list_for_each (l, &slot->funcs) {
1165 func = list_entry(l, struct acpiphp_func, sibling);
1166 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1167 func->function));
1168 if (!func->pci_dev)
1169 continue;
1171 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1172 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1173 continue;
1175 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1176 if (ACPI_FAILURE(status))
1177 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1178 status);
1181 slot->flags |= SLOT_ENABLED;
1183 err_exit:
1184 return retval;
1187 static void disable_bridges(struct pci_bus *bus)
1189 struct pci_dev *dev;
1190 list_for_each_entry(dev, &bus->devices, bus_list) {
1191 if (dev->subordinate) {
1192 disable_bridges(dev->subordinate);
1193 pci_disable_device(dev);
1199 * disable_device - disable a slot
1200 * @slot: ACPI PHP slot
1202 static int disable_device(struct acpiphp_slot *slot)
1204 int retval = 0;
1205 struct acpiphp_func *func;
1206 struct list_head *l;
1208 /* is this slot already disabled? */
1209 if (!(slot->flags & SLOT_ENABLED))
1210 goto err_exit;
1212 list_for_each (l, &slot->funcs) {
1213 func = list_entry(l, struct acpiphp_func, sibling);
1215 if (func->bridge) {
1216 /* cleanup p2p bridges under this P2P bridge */
1217 cleanup_p2p_bridge(func->bridge->handle,
1218 (u32)1, NULL, NULL);
1219 func->bridge = NULL;
1222 if (func->pci_dev) {
1223 pci_stop_bus_device(func->pci_dev);
1224 if (func->pci_dev->subordinate) {
1225 disable_bridges(func->pci_dev->subordinate);
1226 pci_disable_device(func->pci_dev);
1231 list_for_each (l, &slot->funcs) {
1232 func = list_entry(l, struct acpiphp_func, sibling);
1234 acpiphp_unconfigure_ioapics(func->handle);
1235 acpiphp_bus_trim(func->handle);
1236 /* try to remove anyway.
1237 * acpiphp_bus_add might have been failed */
1239 if (!func->pci_dev)
1240 continue;
1242 pci_remove_bus_device(func->pci_dev);
1243 pci_dev_put(func->pci_dev);
1244 func->pci_dev = NULL;
1247 slot->flags &= (~SLOT_ENABLED);
1249 err_exit:
1250 return retval;
1255 * get_slot_status - get ACPI slot status
1256 * @slot: ACPI PHP slot
1258 * If a slot has _STA for each function and if any one of them
1259 * returned non-zero status, return it.
1261 * If a slot doesn't have _STA and if any one of its functions'
1262 * configuration space is configured, return 0x0f as a _STA.
1264 * Otherwise return 0.
1266 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1268 acpi_status status;
1269 unsigned long long sta = 0;
1270 u32 dvid;
1271 struct list_head *l;
1272 struct acpiphp_func *func;
1274 list_for_each (l, &slot->funcs) {
1275 func = list_entry(l, struct acpiphp_func, sibling);
1277 if (func->flags & FUNC_HAS_STA) {
1278 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1279 if (ACPI_SUCCESS(status) && sta)
1280 break;
1281 } else {
1282 pci_bus_read_config_dword(slot->bridge->pci_bus,
1283 PCI_DEVFN(slot->device,
1284 func->function),
1285 PCI_VENDOR_ID, &dvid);
1286 if (dvid != 0xffffffff) {
1287 sta = ACPI_STA_ALL;
1288 break;
1293 return (unsigned int)sta;
1297 * acpiphp_eject_slot - physically eject the slot
1298 * @slot: ACPI PHP slot
1300 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1302 acpi_status status;
1303 struct acpiphp_func *func;
1304 struct list_head *l;
1305 struct acpi_object_list arg_list;
1306 union acpi_object arg;
1308 list_for_each (l, &slot->funcs) {
1309 func = list_entry(l, struct acpiphp_func, sibling);
1311 /* We don't want to call _EJ0 on non-existing functions. */
1312 if ((func->flags & FUNC_HAS_EJ0)) {
1313 /* _EJ0 method take one argument */
1314 arg_list.count = 1;
1315 arg_list.pointer = &arg;
1316 arg.type = ACPI_TYPE_INTEGER;
1317 arg.integer.value = 1;
1319 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1320 if (ACPI_FAILURE(status)) {
1321 warn("%s: _EJ0 failed\n", __func__);
1322 return -1;
1323 } else
1324 break;
1327 return 0;
1331 * acpiphp_check_bridge - re-enumerate devices
1332 * @bridge: where to begin re-enumeration
1334 * Iterate over all slots under this bridge and make sure that if a
1335 * card is present they are enabled, and if not they are disabled.
1337 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1339 struct acpiphp_slot *slot;
1340 int retval = 0;
1341 int enabled, disabled;
1343 enabled = disabled = 0;
1345 for (slot = bridge->slots; slot; slot = slot->next) {
1346 unsigned int status = get_slot_status(slot);
1347 if (slot->flags & SLOT_ENABLED) {
1348 if (status == ACPI_STA_ALL)
1349 continue;
1350 retval = acpiphp_disable_slot(slot);
1351 if (retval) {
1352 err("Error occurred in disabling\n");
1353 goto err_exit;
1354 } else {
1355 acpiphp_eject_slot(slot);
1357 disabled++;
1358 } else {
1359 if (status != ACPI_STA_ALL)
1360 continue;
1361 retval = acpiphp_enable_slot(slot);
1362 if (retval) {
1363 err("Error occurred in enabling\n");
1364 goto err_exit;
1366 enabled++;
1370 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1372 err_exit:
1373 return retval;
1376 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1378 u16 pci_cmd, pci_bctl;
1379 struct pci_dev *cdev;
1381 /* Program hpp values for this device */
1382 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1383 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1384 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1385 return;
1387 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1388 return;
1390 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1391 bridge->hpp.t0->cache_line_size);
1392 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1393 bridge->hpp.t0->latency_timer);
1394 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1395 if (bridge->hpp.t0->enable_serr)
1396 pci_cmd |= PCI_COMMAND_SERR;
1397 else
1398 pci_cmd &= ~PCI_COMMAND_SERR;
1399 if (bridge->hpp.t0->enable_perr)
1400 pci_cmd |= PCI_COMMAND_PARITY;
1401 else
1402 pci_cmd &= ~PCI_COMMAND_PARITY;
1403 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1405 /* Program bridge control value and child devices */
1406 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1407 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1408 bridge->hpp.t0->latency_timer);
1409 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1410 if (bridge->hpp.t0->enable_serr)
1411 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1412 else
1413 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1414 if (bridge->hpp.t0->enable_perr)
1415 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1416 else
1417 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1418 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1419 if (dev->subordinate) {
1420 list_for_each_entry(cdev, &dev->subordinate->devices,
1421 bus_list)
1422 program_hpp(cdev, bridge);
1427 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1429 struct acpiphp_bridge bridge;
1430 struct pci_dev *dev;
1432 memset(&bridge, 0, sizeof(bridge));
1433 bridge.handle = handle;
1434 bridge.pci_bus = bus;
1435 bridge.pci_dev = bus->self;
1436 decode_hpp(&bridge);
1437 list_for_each_entry(dev, &bus->devices, bus_list)
1438 program_hpp(dev, &bridge);
1443 * Remove devices for which we could not assign resources, call
1444 * arch specific code to fix-up the bus
1446 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1448 struct pci_dev *dev;
1449 int i;
1450 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1452 list_for_each_entry(dev, &bus->devices, bus_list) {
1453 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1454 struct resource *res = &dev->resource[i];
1455 if ((res->flags & type_mask) && !res->start &&
1456 res->end) {
1457 /* Could not assign a required resources
1458 * for this device, remove it */
1459 pci_remove_bus_device(dev);
1460 break;
1466 /* Program resources in newly inserted bridge */
1467 static int acpiphp_configure_bridge (acpi_handle handle)
1469 struct acpi_pci_id pci_id;
1470 struct pci_bus *bus;
1472 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1473 err("cannot get PCI domain and bus number for bridge\n");
1474 return -EINVAL;
1476 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1477 if (!bus) {
1478 err("cannot find bus %d:%d\n",
1479 pci_id.segment, pci_id.bus);
1480 return -EINVAL;
1483 pci_bus_size_bridges(bus);
1484 pci_bus_assign_resources(bus);
1485 acpiphp_sanitize_bus(bus);
1486 acpiphp_set_hpp_values(handle, bus);
1487 pci_enable_bridges(bus);
1488 acpiphp_configure_ioapics(handle);
1489 return 0;
1492 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1494 struct acpi_device *device, *pdevice;
1495 acpi_handle phandle;
1497 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1498 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1499 err("unexpected notification type %d\n", type);
1500 return;
1503 acpi_get_parent(handle, &phandle);
1504 if (acpi_bus_get_device(phandle, &pdevice)) {
1505 dbg("no parent device, assuming NULL\n");
1506 pdevice = NULL;
1508 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1509 err("cannot add bridge to acpi list\n");
1510 return;
1512 if (!acpiphp_configure_bridge(handle) &&
1513 !acpi_bus_start(device))
1514 add_bridge(handle);
1515 else
1516 err("cannot configure and start bridge\n");
1521 * ACPI event handlers
1524 static acpi_status
1525 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1527 int *count = (int *)context;
1528 struct acpiphp_bridge *bridge;
1530 bridge = acpiphp_handle_to_bridge(handle);
1531 if (bridge)
1532 (*count)++;
1533 return AE_OK ;
1536 static acpi_status
1537 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1539 struct acpiphp_bridge *bridge;
1540 char objname[64];
1541 struct acpi_buffer buffer = { .length = sizeof(objname),
1542 .pointer = objname };
1544 bridge = acpiphp_handle_to_bridge(handle);
1545 if (bridge) {
1546 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1547 dbg("%s: re-enumerating slots under %s\n",
1548 __func__, objname);
1549 acpiphp_check_bridge(bridge);
1551 return AE_OK ;
1555 * handle_hotplug_event_bridge - handle ACPI event on bridges
1556 * @handle: Notify()'ed acpi_handle
1557 * @type: Notify code
1558 * @context: pointer to acpiphp_bridge structure
1560 * Handles ACPI event notification on {host,p2p} bridges.
1562 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1564 struct acpiphp_bridge *bridge;
1565 char objname[64];
1566 struct acpi_buffer buffer = { .length = sizeof(objname),
1567 .pointer = objname };
1568 struct acpi_device *device;
1569 int num_sub_bridges = 0;
1571 if (acpi_bus_get_device(handle, &device)) {
1572 /* This bridge must have just been physically inserted */
1573 handle_bridge_insertion(handle, type);
1574 return;
1577 bridge = acpiphp_handle_to_bridge(handle);
1578 if (type == ACPI_NOTIFY_BUS_CHECK) {
1579 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1580 count_sub_bridges, &num_sub_bridges, NULL);
1583 if (!bridge && !num_sub_bridges) {
1584 err("cannot get bridge info\n");
1585 return;
1588 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1590 switch (type) {
1591 case ACPI_NOTIFY_BUS_CHECK:
1592 /* bus re-enumerate */
1593 dbg("%s: Bus check notify on %s\n", __func__, objname);
1594 if (bridge) {
1595 dbg("%s: re-enumerating slots under %s\n",
1596 __func__, objname);
1597 acpiphp_check_bridge(bridge);
1599 if (num_sub_bridges)
1600 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1601 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1602 break;
1604 case ACPI_NOTIFY_DEVICE_CHECK:
1605 /* device check */
1606 dbg("%s: Device check notify on %s\n", __func__, objname);
1607 acpiphp_check_bridge(bridge);
1608 break;
1610 case ACPI_NOTIFY_DEVICE_WAKE:
1611 /* wake event */
1612 dbg("%s: Device wake notify on %s\n", __func__, objname);
1613 break;
1615 case ACPI_NOTIFY_EJECT_REQUEST:
1616 /* request device eject */
1617 dbg("%s: Device eject notify on %s\n", __func__, objname);
1618 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1619 (bridge->flags & BRIDGE_HAS_EJ0)) {
1620 struct acpiphp_slot *slot;
1621 slot = bridge->func->slot;
1622 if (!acpiphp_disable_slot(slot))
1623 acpiphp_eject_slot(slot);
1625 break;
1627 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1628 printk(KERN_ERR "Device %s cannot be configured due"
1629 " to a frequency mismatch\n", objname);
1630 break;
1632 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1633 printk(KERN_ERR "Device %s cannot be configured due"
1634 " to a bus mode mismatch\n", objname);
1635 break;
1637 case ACPI_NOTIFY_POWER_FAULT:
1638 printk(KERN_ERR "Device %s has suffered a power fault\n",
1639 objname);
1640 break;
1642 default:
1643 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1644 break;
1649 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1650 * @handle: Notify()'ed acpi_handle
1651 * @type: Notify code
1652 * @context: pointer to acpiphp_func structure
1654 * Handles ACPI event notification on slots.
1656 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1658 struct acpiphp_func *func;
1659 char objname[64];
1660 struct acpi_buffer buffer = { .length = sizeof(objname),
1661 .pointer = objname };
1663 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1665 func = (struct acpiphp_func *)context;
1667 switch (type) {
1668 case ACPI_NOTIFY_BUS_CHECK:
1669 /* bus re-enumerate */
1670 dbg("%s: Bus check notify on %s\n", __func__, objname);
1671 acpiphp_enable_slot(func->slot);
1672 break;
1674 case ACPI_NOTIFY_DEVICE_CHECK:
1675 /* device check : re-enumerate from parent bus */
1676 dbg("%s: Device check notify on %s\n", __func__, objname);
1677 acpiphp_check_bridge(func->slot->bridge);
1678 break;
1680 case ACPI_NOTIFY_DEVICE_WAKE:
1681 /* wake event */
1682 dbg("%s: Device wake notify on %s\n", __func__, objname);
1683 break;
1685 case ACPI_NOTIFY_EJECT_REQUEST:
1686 /* request device eject */
1687 dbg("%s: Device eject notify on %s\n", __func__, objname);
1688 if (!(acpiphp_disable_slot(func->slot)))
1689 acpiphp_eject_slot(func->slot);
1690 break;
1692 default:
1693 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1694 break;
1699 static acpi_status
1700 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1702 int *count = (int *)context;
1704 if (acpi_root_bridge(handle)) {
1705 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1706 handle_hotplug_event_bridge, NULL);
1707 (*count)++;
1709 return AE_OK ;
1712 static struct acpi_pci_driver acpi_pci_hp_driver = {
1713 .add = add_bridge,
1714 .remove = remove_bridge,
1718 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1720 int __init acpiphp_glue_init(void)
1722 int num = 0;
1724 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1725 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1727 if (num <= 0)
1728 return -1;
1729 else
1730 acpi_pci_register_driver(&acpi_pci_hp_driver);
1732 return 0;
1737 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1739 * This function frees all data allocated in acpiphp_glue_init().
1741 void acpiphp_glue_exit(void)
1743 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1748 * acpiphp_get_num_slots - count number of slots in a system
1750 int __init acpiphp_get_num_slots(void)
1752 struct acpiphp_bridge *bridge;
1753 int num_slots = 0;
1755 list_for_each_entry (bridge, &bridge_list, list) {
1756 dbg("Bus %04x:%02x has %d slot%s\n",
1757 pci_domain_nr(bridge->pci_bus),
1758 bridge->pci_bus->number, bridge->nr_slots,
1759 bridge->nr_slots == 1 ? "" : "s");
1760 num_slots += bridge->nr_slots;
1763 dbg("Total %d slots\n", num_slots);
1764 return num_slots;
1768 #if 0
1770 * acpiphp_for_each_slot - call function for each slot
1771 * @fn: callback function
1772 * @data: context to be passed to callback function
1774 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1776 struct list_head *node;
1777 struct acpiphp_bridge *bridge;
1778 struct acpiphp_slot *slot;
1779 int retval = 0;
1781 list_for_each (node, &bridge_list) {
1782 bridge = (struct acpiphp_bridge *)node;
1783 for (slot = bridge->slots; slot; slot = slot->next) {
1784 retval = fn(slot, data);
1785 if (!retval)
1786 goto err_exit;
1790 err_exit:
1791 return retval;
1793 #endif
1797 * acpiphp_enable_slot - power on slot
1798 * @slot: ACPI PHP slot
1800 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1802 int retval;
1804 mutex_lock(&slot->crit_sect);
1806 /* wake up all functions */
1807 retval = power_on_slot(slot);
1808 if (retval)
1809 goto err_exit;
1811 if (get_slot_status(slot) == ACPI_STA_ALL) {
1812 /* configure all functions */
1813 retval = enable_device(slot);
1814 if (retval)
1815 power_off_slot(slot);
1816 } else {
1817 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1818 power_off_slot(slot);
1821 err_exit:
1822 mutex_unlock(&slot->crit_sect);
1823 return retval;
1827 * acpiphp_disable_slot - power off slot
1828 * @slot: ACPI PHP slot
1830 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1832 int retval = 0;
1834 mutex_lock(&slot->crit_sect);
1836 /* unconfigure all functions */
1837 retval = disable_device(slot);
1838 if (retval)
1839 goto err_exit;
1841 /* power off all functions */
1842 retval = power_off_slot(slot);
1843 if (retval)
1844 goto err_exit;
1846 err_exit:
1847 mutex_unlock(&slot->crit_sect);
1848 return retval;
1853 * slot enabled: 1
1854 * slot disabled: 0
1856 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1858 return (slot->flags & SLOT_POWEREDON);
1863 * latch open: 1
1864 * latch closed: 0
1866 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1868 unsigned int sta;
1870 sta = get_slot_status(slot);
1872 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1877 * adapter presence : 1
1878 * absence : 0
1880 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1882 unsigned int sta;
1884 sta = get_slot_status(slot);
1886 return (sta == 0) ? 0 : 1;