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
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_bridge has its refcount elevated by pci_get_slot()
36 * when the bridge is scanned and it loses a refcount when the bridge
38 * - When a P2P bridge is present, we elevate the refcount on the subordinate
39 * bus. It loses the refcount when the the driver unloads.
42 #include <linux/init.h>
43 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/pci.h>
47 #include <linux/pci_hotplug.h>
48 #include <linux/pci-acpi.h>
49 #include <linux/mutex.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(struct pci_bus *bus);
63 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
65 /* callback routine to check for the existence of a pci dock device */
67 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
69 int *count = (int *)context;
71 if (is_dock_device(handle)) {
73 return AE_CTRL_TERMINATE;
80 * the _DCK method can do funny things... and sometimes not
83 * TBD - figure out a way to only call fixups for
84 * systems that require them.
86 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
89 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90 struct pci_bus *bus = func->slot->bridge->pci_bus;
96 /* fixup bad _DCK function that rewrites
97 * secondary bridge on slot
99 pci_read_config_dword(bus->self,
103 if (((buses >> 8) & 0xff) != bus->secondary) {
104 buses = (buses & 0xff000000)
105 | ((unsigned int)(bus->primary) << 0)
106 | ((unsigned int)(bus->secondary) << 8)
107 | ((unsigned int)(bus->subordinate) << 16);
108 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
114 static struct acpi_dock_ops acpiphp_dock_ops = {
115 .handler = handle_hotplug_event_func,
118 /* callback routine to register each ACPI PCI slot object */
120 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
122 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
123 struct acpiphp_slot *slot;
124 struct acpiphp_func *newfunc;
126 acpi_status status = AE_OK;
127 unsigned long long adr, sun;
128 int device, function, retval;
129 struct pci_bus *pbus = bridge->pci_bus;
130 struct pci_dev *pdev;
132 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
135 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
136 device = (adr >> 16) & 0xffff;
137 function = adr & 0xffff;
139 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
143 INIT_LIST_HEAD(&newfunc->sibling);
144 newfunc->handle = handle;
145 newfunc->function = function;
147 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
148 newfunc->flags = FUNC_HAS_EJ0;
150 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
151 newfunc->flags |= FUNC_HAS_STA;
153 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
154 newfunc->flags |= FUNC_HAS_PS0;
156 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
157 newfunc->flags |= FUNC_HAS_PS3;
159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
160 newfunc->flags |= FUNC_HAS_DCK;
162 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
163 if (ACPI_FAILURE(status)) {
165 * use the count of the number of slots we've found
166 * for the number of the slot
168 sun = bridge->nr_slots+1;
171 /* search for objects that share the same slot */
172 for (slot = bridge->slots; slot; slot = slot->next)
173 if (slot->device == device) {
174 if (slot->sun != sun)
175 warn("sibling found, but _SUN doesn't match!\n");
180 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
186 slot->bridge = bridge;
187 slot->device = device;
189 INIT_LIST_HEAD(&slot->funcs);
190 mutex_init(&slot->crit_sect);
192 slot->next = bridge->slots;
193 bridge->slots = slot;
197 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
198 slot->sun, pci_domain_nr(pbus), pbus->number, device);
199 retval = acpiphp_register_hotplug_slot(slot);
201 if (retval == -EBUSY)
202 warn("Slot %llu already registered by another "
203 "hotplug driver\n", slot->sun);
205 warn("acpiphp_register_hotplug_slot failed "
206 "(err code = 0x%x)\n", retval);
211 newfunc->slot = slot;
212 list_add_tail(&newfunc->sibling, &slot->funcs);
214 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
216 pdev->current_state = PCI_D0;
217 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
221 if (is_dock_device(handle)) {
222 /* we don't want to call this device's _EJ0
223 * because we want the dock notify handler
224 * to call it after it calls _DCK
226 newfunc->flags &= ~FUNC_HAS_EJ0;
227 if (register_hotplug_dock_device(handle,
228 &acpiphp_dock_ops, newfunc))
229 dbg("failed to register dock device\n");
231 /* we need to be notified when dock events happen
232 * outside of the hotplug operation, since we may
233 * need to do fixups before we can hotplug.
235 newfunc->nb.notifier_call = post_dock_fixups;
236 if (register_dock_notifier(&newfunc->nb))
237 dbg("failed to register a dock notifier");
240 /* install notify handler */
241 if (!(newfunc->flags & FUNC_HAS_DCK)) {
242 status = acpi_install_notify_handler(handle,
244 handle_hotplug_event_func,
247 if (ACPI_FAILURE(status))
248 err("failed to register interrupt notify handler\n");
256 bridge->slots = slot->next;
264 /* see if it's worth looking at this bridge */
265 static int detect_ejectable_slots(acpi_handle handle)
267 int found = acpi_pci_detect_ejectable(handle);
269 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
270 is_pci_dock_device, (void *)&found, NULL);
275 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
276 static void init_bridge_misc(struct acpiphp_bridge *bridge)
280 /* must be added to the list prior to calling register_slot */
281 list_add(&bridge->list, &bridge_list);
283 /* register all slot objects under this bridge */
284 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
285 register_slot, bridge, NULL);
286 if (ACPI_FAILURE(status)) {
287 list_del(&bridge->list);
291 /* install notify handler */
292 if (bridge->type != BRIDGE_TYPE_HOST) {
293 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
294 status = acpi_remove_notify_handler(bridge->func->handle,
296 handle_hotplug_event_func);
297 if (ACPI_FAILURE(status))
298 err("failed to remove notify handler\n");
300 status = acpi_install_notify_handler(bridge->handle,
302 handle_hotplug_event_bridge,
305 if (ACPI_FAILURE(status)) {
306 err("failed to register interrupt notify handler\n");
312 /* find acpiphp_func from acpiphp_bridge */
313 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
315 struct list_head *node, *l;
316 struct acpiphp_bridge *bridge;
317 struct acpiphp_slot *slot;
318 struct acpiphp_func *func;
320 list_for_each(node, &bridge_list) {
321 bridge = list_entry(node, struct acpiphp_bridge, list);
322 for (slot = bridge->slots; slot; slot = slot->next) {
323 list_for_each(l, &slot->funcs) {
324 func = list_entry(l, struct acpiphp_func,
326 if (func->handle == handle)
336 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
338 acpi_handle dummy_handle;
340 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
341 "_STA", &dummy_handle)))
342 bridge->flags |= BRIDGE_HAS_STA;
344 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
345 "_EJ0", &dummy_handle)))
346 bridge->flags |= BRIDGE_HAS_EJ0;
348 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
349 "_PS0", &dummy_handle)))
350 bridge->flags |= BRIDGE_HAS_PS0;
352 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
353 "_PS3", &dummy_handle)))
354 bridge->flags |= BRIDGE_HAS_PS3;
356 /* is this ejectable p2p bridge? */
357 if (bridge->flags & BRIDGE_HAS_EJ0) {
358 struct acpiphp_func *func;
360 dbg("found ejectable p2p bridge\n");
362 /* make link between PCI bridge and PCI function */
363 func = acpiphp_bridge_handle_to_function(bridge->handle);
367 func->bridge = bridge;
372 /* allocate and initialize host bridge data structure */
373 static void add_host_bridge(acpi_handle *handle)
375 struct acpiphp_bridge *bridge;
376 struct acpi_pci_root *root = acpi_pci_find_root(handle);
378 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
382 bridge->type = BRIDGE_TYPE_HOST;
383 bridge->handle = handle;
385 bridge->pci_bus = root->bus;
387 spin_lock_init(&bridge->res_lock);
389 init_bridge_misc(bridge);
393 /* allocate and initialize PCI-to-PCI bridge data structure */
394 static void add_p2p_bridge(acpi_handle *handle)
396 struct acpiphp_bridge *bridge;
398 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
399 if (bridge == NULL) {
400 err("out of memory\n");
404 bridge->type = BRIDGE_TYPE_P2P;
405 bridge->handle = handle;
406 config_p2p_bridge_flags(bridge);
408 bridge->pci_dev = acpi_get_pci_dev(handle);
409 bridge->pci_bus = bridge->pci_dev->subordinate;
410 if (!bridge->pci_bus) {
411 err("This is not a PCI-to-PCI bridge!\n");
416 * Grab a ref to the subordinate PCI bus in case the bus is
417 * removed via PCI core logical hotplug. The ref pins the bus
418 * (which we access during module unload).
420 get_device(&bridge->pci_bus->dev);
421 spin_lock_init(&bridge->res_lock);
423 init_bridge_misc(bridge);
426 pci_dev_put(bridge->pci_dev);
432 /* callback routine to find P2P bridges */
434 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
439 dev = acpi_get_pci_dev(handle);
440 if (!dev || !dev->subordinate)
443 /* check if this bridge has ejectable slots */
444 if ((detect_ejectable_slots(handle) > 0)) {
445 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
446 add_p2p_bridge(handle);
449 /* search P2P bridges under this p2p bridge */
450 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
451 find_p2p_bridge, NULL, NULL);
452 if (ACPI_FAILURE(status))
453 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
461 /* find hot-pluggable slots, and then find P2P bridge */
462 static int add_bridge(acpi_handle handle)
465 unsigned long long tmp;
466 acpi_handle dummy_handle;
468 /* if the bridge doesn't have _STA, we assume it is always there */
469 status = acpi_get_handle(handle, "_STA", &dummy_handle);
470 if (ACPI_SUCCESS(status)) {
471 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
472 if (ACPI_FAILURE(status)) {
473 dbg("%s: _STA evaluation failure\n", __func__);
476 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
477 /* don't register this object */
481 /* check if this bridge has ejectable slots */
482 if (detect_ejectable_slots(handle) > 0) {
483 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
484 add_host_bridge(handle);
487 /* search P2P bridges under this host bridge */
488 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
489 find_p2p_bridge, NULL, NULL);
491 if (ACPI_FAILURE(status))
492 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
497 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
499 struct list_head *head;
500 list_for_each(head, &bridge_list) {
501 struct acpiphp_bridge *bridge = list_entry(head,
502 struct acpiphp_bridge, list);
503 if (bridge->handle == handle)
510 static void cleanup_bridge(struct acpiphp_bridge *bridge)
512 struct list_head *list, *tmp;
513 struct acpiphp_slot *slot;
515 acpi_handle handle = bridge->handle;
517 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
518 handle_hotplug_event_bridge);
519 if (ACPI_FAILURE(status))
520 err("failed to remove notify handler\n");
522 if ((bridge->type != BRIDGE_TYPE_HOST) &&
523 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
524 status = acpi_install_notify_handler(bridge->func->handle,
526 handle_hotplug_event_func,
528 if (ACPI_FAILURE(status))
529 err("failed to install interrupt notify handler\n");
532 slot = bridge->slots;
534 struct acpiphp_slot *next = slot->next;
535 list_for_each_safe (list, tmp, &slot->funcs) {
536 struct acpiphp_func *func;
537 func = list_entry(list, struct acpiphp_func, sibling);
538 if (is_dock_device(func->handle)) {
539 unregister_hotplug_dock_device(func->handle);
540 unregister_dock_notifier(&func->nb);
542 if (!(func->flags & FUNC_HAS_DCK)) {
543 status = acpi_remove_notify_handler(func->handle,
545 handle_hotplug_event_func);
546 if (ACPI_FAILURE(status))
547 err("failed to remove notify handler\n");
552 acpiphp_unregister_hotplug_slot(slot);
553 list_del(&slot->funcs);
559 * Only P2P bridges have a pci_dev
562 put_device(&bridge->pci_bus->dev);
564 pci_dev_put(bridge->pci_dev);
565 list_del(&bridge->list);
570 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
572 struct acpiphp_bridge *bridge;
574 /* cleanup p2p bridges under this P2P bridge
575 in a depth-first manner */
576 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
577 cleanup_p2p_bridge, NULL, NULL);
579 bridge = acpiphp_handle_to_bridge(handle);
581 cleanup_bridge(bridge);
586 static void remove_bridge(acpi_handle handle)
588 struct acpiphp_bridge *bridge;
590 /* cleanup p2p bridges under this host bridge
591 in a depth-first manner */
592 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
593 (u32)1, cleanup_p2p_bridge, NULL, NULL);
596 * On root bridges with hotplug slots directly underneath (ie,
597 * no p2p bridge inbetween), we call cleanup_bridge().
599 * The else clause cleans up root bridges that either had no
600 * hotplug slots at all, or had a p2p bridge underneath.
602 bridge = acpiphp_handle_to_bridge(handle);
604 cleanup_bridge(bridge);
606 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
607 handle_hotplug_event_bridge);
610 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
614 dev = acpi_get_pci_dev(handle);
618 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
619 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
628 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
632 unsigned long long gsb;
633 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
634 union acpi_object *obj;
637 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
638 if (ACPI_SUCCESS(status)) {
639 *gsi_base = (u32)gsb;
643 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
644 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
647 obj = buffer.pointer;
648 if (obj->type != ACPI_TYPE_BUFFER)
651 table = obj->buffer.pointer;
652 switch (((struct acpi_subtable_header *)table)->type) {
653 case ACPI_MADT_TYPE_IO_SAPIC:
654 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
657 case ACPI_MADT_TYPE_IO_APIC:
658 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
665 kfree(buffer.pointer);
670 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
673 unsigned long long sta;
675 struct pci_dev *pdev;
678 struct acpiphp_ioapic *ioapic;
680 /* Evaluate _STA if present */
681 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
682 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
683 return AE_CTRL_DEPTH;
685 /* Scan only PCI bus scope */
686 status = acpi_get_handle(handle, "_HID", &tmp);
687 if (ACPI_SUCCESS(status))
688 return AE_CTRL_DEPTH;
690 if (get_gsi_base(handle, &gsi_base))
693 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
697 pdev = get_apic_pci_info(handle);
701 if (pci_enable_device(pdev))
702 goto exit_pci_dev_put;
704 pci_set_master(pdev);
706 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
707 goto exit_pci_disable_device;
709 phys_addr = pci_resource_start(pdev, 0);
710 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
711 goto exit_pci_release_region;
713 ioapic->gsi_base = gsi_base;
715 spin_lock(&ioapic_list_lock);
716 list_add_tail(&ioapic->list, &ioapic_list);
717 spin_unlock(&ioapic_list_lock);
721 exit_pci_release_region:
722 pci_release_region(pdev, 0);
723 exit_pci_disable_device:
724 pci_disable_device(pdev);
734 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
737 unsigned long long sta;
740 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
742 /* Evaluate _STA if present */
743 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
744 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
745 return AE_CTRL_DEPTH;
747 /* Scan only PCI bus scope */
748 status = acpi_get_handle(handle, "_HID", &tmp);
749 if (ACPI_SUCCESS(status))
750 return AE_CTRL_DEPTH;
752 if (get_gsi_base(handle, &gsi_base))
755 acpi_unregister_ioapic(handle, gsi_base);
757 spin_lock(&ioapic_list_lock);
758 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
759 if (pos->gsi_base != gsi_base)
762 list_del(&ioapic->list);
765 spin_unlock(&ioapic_list_lock);
770 pci_release_region(ioapic->dev, 0);
771 pci_disable_device(ioapic->dev);
772 pci_dev_put(ioapic->dev);
778 static int acpiphp_configure_ioapics(acpi_handle handle)
780 ioapic_add(handle, 0, NULL, NULL);
781 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
782 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
786 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
788 ioapic_remove(handle, 0, NULL, NULL);
789 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
790 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
794 static int power_on_slot(struct acpiphp_slot *slot)
797 struct acpiphp_func *func;
801 /* if already enabled, just skip */
802 if (slot->flags & SLOT_POWEREDON)
805 list_for_each (l, &slot->funcs) {
806 func = list_entry(l, struct acpiphp_func, sibling);
808 if (func->flags & FUNC_HAS_PS0) {
809 dbg("%s: executing _PS0\n", __func__);
810 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
811 if (ACPI_FAILURE(status)) {
812 warn("%s: _PS0 failed\n", __func__);
820 /* TBD: evaluate _STA to check if the slot is enabled */
822 slot->flags |= SLOT_POWEREDON;
829 static int power_off_slot(struct acpiphp_slot *slot)
832 struct acpiphp_func *func;
837 /* if already disabled, just skip */
838 if ((slot->flags & SLOT_POWEREDON) == 0)
841 list_for_each (l, &slot->funcs) {
842 func = list_entry(l, struct acpiphp_func, sibling);
844 if (func->flags & FUNC_HAS_PS3) {
845 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
846 if (ACPI_FAILURE(status)) {
847 warn("%s: _PS3 failed\n", __func__);
855 /* TBD: evaluate _STA to check if the slot is disabled */
857 slot->flags &= (~SLOT_POWEREDON);
866 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
867 * @bus: bus to start search with
869 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
871 struct list_head *tmp;
872 unsigned char max, n;
875 * pci_bus_max_busnr will return the highest
876 * reserved busnr for all these children.
877 * that is equivalent to the bus->subordinate
878 * value. We don't want to use the parent's
879 * bus->subordinate value because it could have
882 max = bus->secondary;
884 list_for_each(tmp, &bus->children) {
885 n = pci_bus_max_busnr(pci_bus_b(tmp));
894 * acpiphp_bus_add - add a new bus to acpi subsystem
895 * @func: acpiphp_func of the bridge
897 static int acpiphp_bus_add(struct acpiphp_func *func)
900 struct acpi_device *device, *pdevice;
903 acpi_get_parent(func->handle, &phandle);
904 if (acpi_bus_get_device(phandle, &pdevice)) {
905 dbg("no parent device, assuming NULL\n");
908 if (!acpi_bus_get_device(func->handle, &device)) {
909 dbg("bus exists... trim\n");
910 /* this shouldn't be in here, so remove
911 * the bus then re-add it...
913 ret_val = acpi_bus_trim(device, 1);
914 dbg("acpi_bus_trim return %x\n", ret_val);
917 ret_val = acpi_bus_add(&device, pdevice, func->handle,
918 ACPI_BUS_TYPE_DEVICE);
920 dbg("error adding bus, %x\n",
922 goto acpiphp_bus_add_out;
925 * try to start anyway. We could have failed to add
926 * simply because this bus had previously been added
927 * on another add. Don't bother with the return value
928 * we just keep going.
930 ret_val = acpi_bus_start(device);
938 * acpiphp_bus_trim - trim a bus from acpi subsystem
939 * @handle: handle to acpi namespace
941 static int acpiphp_bus_trim(acpi_handle handle)
943 struct acpi_device *device;
946 retval = acpi_bus_get_device(handle, &device);
948 dbg("acpi_device not found\n");
952 retval = acpi_bus_trim(device, 1);
954 err("cannot remove from acpi list\n");
960 * enable_device - enable, configure a slot
961 * @slot: slot to be enabled
963 * This function should be called per *physical slot*,
964 * not per each slot object in ACPI namespace.
966 static int __ref enable_device(struct acpiphp_slot *slot)
969 struct pci_bus *bus = slot->bridge->pci_bus;
971 struct acpiphp_func *func;
976 if (slot->flags & SLOT_ENABLED)
979 /* sanity check: dev should be NULL when hot-plugged in */
980 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
982 /* This case shouldn't happen */
983 err("pci_dev structure already exists.\n");
989 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
991 err("No new device found\n");
996 max = acpiphp_max_busnr(bus);
997 for (pass = 0; pass < 2; pass++) {
998 list_for_each_entry(dev, &bus->devices, bus_list) {
999 if (PCI_SLOT(dev->devfn) != slot->device)
1001 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1002 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1003 max = pci_scan_bridge(bus, dev, max, pass);
1004 if (pass && dev->subordinate)
1005 pci_bus_size_bridges(dev->subordinate);
1010 list_for_each (l, &slot->funcs) {
1011 func = list_entry(l, struct acpiphp_func, sibling);
1012 acpiphp_bus_add(func);
1015 pci_bus_assign_resources(bus);
1016 acpiphp_sanitize_bus(bus);
1017 acpiphp_set_hpp_values(bus);
1018 list_for_each_entry(func, &slot->funcs, sibling)
1019 acpiphp_configure_ioapics(func->handle);
1020 pci_enable_bridges(bus);
1021 pci_bus_add_devices(bus);
1023 list_for_each (l, &slot->funcs) {
1024 func = list_entry(l, struct acpiphp_func, sibling);
1025 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1030 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1031 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1036 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1037 if (ACPI_FAILURE(status))
1038 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1043 slot->flags |= SLOT_ENABLED;
1049 static void disable_bridges(struct pci_bus *bus)
1051 struct pci_dev *dev;
1052 list_for_each_entry(dev, &bus->devices, bus_list) {
1053 if (dev->subordinate) {
1054 disable_bridges(dev->subordinate);
1055 pci_disable_device(dev);
1061 * disable_device - disable a slot
1062 * @slot: ACPI PHP slot
1064 static int disable_device(struct acpiphp_slot *slot)
1066 struct acpiphp_func *func;
1067 struct pci_dev *pdev;
1069 /* is this slot already disabled? */
1070 if (!(slot->flags & SLOT_ENABLED))
1073 list_for_each_entry(func, &slot->funcs, sibling) {
1075 /* cleanup p2p bridges under this P2P bridge */
1076 cleanup_p2p_bridge(func->bridge->handle,
1077 (u32)1, NULL, NULL);
1078 func->bridge = NULL;
1081 pdev = pci_get_slot(slot->bridge->pci_bus,
1082 PCI_DEVFN(slot->device, func->function));
1084 pci_stop_bus_device(pdev);
1085 if (pdev->subordinate) {
1086 disable_bridges(pdev->subordinate);
1087 pci_disable_device(pdev);
1089 pci_remove_bus_device(pdev);
1094 list_for_each_entry(func, &slot->funcs, sibling) {
1095 acpiphp_unconfigure_ioapics(func->handle);
1096 acpiphp_bus_trim(func->handle);
1099 slot->flags &= (~SLOT_ENABLED);
1107 * get_slot_status - get ACPI slot status
1108 * @slot: ACPI PHP slot
1110 * If a slot has _STA for each function and if any one of them
1111 * returned non-zero status, return it.
1113 * If a slot doesn't have _STA and if any one of its functions'
1114 * configuration space is configured, return 0x0f as a _STA.
1116 * Otherwise return 0.
1118 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1121 unsigned long long sta = 0;
1123 struct list_head *l;
1124 struct acpiphp_func *func;
1126 list_for_each (l, &slot->funcs) {
1127 func = list_entry(l, struct acpiphp_func, sibling);
1129 if (func->flags & FUNC_HAS_STA) {
1130 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1131 if (ACPI_SUCCESS(status) && sta)
1134 pci_bus_read_config_dword(slot->bridge->pci_bus,
1135 PCI_DEVFN(slot->device,
1137 PCI_VENDOR_ID, &dvid);
1138 if (dvid != 0xffffffff) {
1145 return (unsigned int)sta;
1149 * acpiphp_eject_slot - physically eject the slot
1150 * @slot: ACPI PHP slot
1152 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1155 struct acpiphp_func *func;
1156 struct list_head *l;
1157 struct acpi_object_list arg_list;
1158 union acpi_object arg;
1160 list_for_each (l, &slot->funcs) {
1161 func = list_entry(l, struct acpiphp_func, sibling);
1163 /* We don't want to call _EJ0 on non-existing functions. */
1164 if ((func->flags & FUNC_HAS_EJ0)) {
1165 /* _EJ0 method take one argument */
1167 arg_list.pointer = &arg;
1168 arg.type = ACPI_TYPE_INTEGER;
1169 arg.integer.value = 1;
1171 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1172 if (ACPI_FAILURE(status)) {
1173 warn("%s: _EJ0 failed\n", __func__);
1183 * acpiphp_check_bridge - re-enumerate devices
1184 * @bridge: where to begin re-enumeration
1186 * Iterate over all slots under this bridge and make sure that if a
1187 * card is present they are enabled, and if not they are disabled.
1189 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1191 struct acpiphp_slot *slot;
1193 int enabled, disabled;
1195 enabled = disabled = 0;
1197 for (slot = bridge->slots; slot; slot = slot->next) {
1198 unsigned int status = get_slot_status(slot);
1199 if (slot->flags & SLOT_ENABLED) {
1200 if (status == ACPI_STA_ALL)
1202 retval = acpiphp_disable_slot(slot);
1204 err("Error occurred in disabling\n");
1207 acpiphp_eject_slot(slot);
1211 if (status != ACPI_STA_ALL)
1213 retval = acpiphp_enable_slot(slot);
1215 err("Error occurred in enabling\n");
1222 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1228 static void acpiphp_set_hpp_values(struct pci_bus *bus)
1230 struct pci_dev *dev;
1232 list_for_each_entry(dev, &bus->devices, bus_list)
1233 pci_configure_slot(dev);
1237 * Remove devices for which we could not assign resources, call
1238 * arch specific code to fix-up the bus
1240 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1242 struct pci_dev *dev;
1244 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1246 list_for_each_entry(dev, &bus->devices, bus_list) {
1247 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1248 struct resource *res = &dev->resource[i];
1249 if ((res->flags & type_mask) && !res->start &&
1251 /* Could not assign a required resources
1252 * for this device, remove it */
1253 pci_remove_bus_device(dev);
1260 /* Program resources in newly inserted bridge */
1261 static int acpiphp_configure_bridge (acpi_handle handle)
1263 struct pci_bus *bus;
1265 if (acpi_is_root_bridge(handle)) {
1266 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1269 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1270 bus = pdev->subordinate;
1274 pci_bus_size_bridges(bus);
1275 pci_bus_assign_resources(bus);
1276 acpiphp_sanitize_bus(bus);
1277 acpiphp_set_hpp_values(bus);
1278 pci_enable_bridges(bus);
1279 acpiphp_configure_ioapics(handle);
1283 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1285 struct acpi_device *device, *pdevice;
1286 acpi_handle phandle;
1288 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1289 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1290 err("unexpected notification type %d\n", type);
1294 acpi_get_parent(handle, &phandle);
1295 if (acpi_bus_get_device(phandle, &pdevice)) {
1296 dbg("no parent device, assuming NULL\n");
1299 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1300 err("cannot add bridge to acpi list\n");
1303 if (!acpiphp_configure_bridge(handle) &&
1304 !acpi_bus_start(device))
1307 err("cannot configure and start bridge\n");
1312 * ACPI event handlers
1316 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1318 int *count = (int *)context;
1319 struct acpiphp_bridge *bridge;
1321 bridge = acpiphp_handle_to_bridge(handle);
1328 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1330 struct acpiphp_bridge *bridge;
1332 struct acpi_buffer buffer = { .length = sizeof(objname),
1333 .pointer = objname };
1335 bridge = acpiphp_handle_to_bridge(handle);
1337 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1338 dbg("%s: re-enumerating slots under %s\n",
1340 acpiphp_check_bridge(bridge);
1346 * handle_hotplug_event_bridge - handle ACPI event on bridges
1347 * @handle: Notify()'ed acpi_handle
1348 * @type: Notify code
1349 * @context: pointer to acpiphp_bridge structure
1351 * Handles ACPI event notification on {host,p2p} bridges.
1353 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1355 struct acpiphp_bridge *bridge;
1357 struct acpi_buffer buffer = { .length = sizeof(objname),
1358 .pointer = objname };
1359 struct acpi_device *device;
1360 int num_sub_bridges = 0;
1362 if (acpi_bus_get_device(handle, &device)) {
1363 /* This bridge must have just been physically inserted */
1364 handle_bridge_insertion(handle, type);
1368 bridge = acpiphp_handle_to_bridge(handle);
1369 if (type == ACPI_NOTIFY_BUS_CHECK) {
1370 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1371 count_sub_bridges, &num_sub_bridges, NULL);
1374 if (!bridge && !num_sub_bridges) {
1375 err("cannot get bridge info\n");
1379 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1382 case ACPI_NOTIFY_BUS_CHECK:
1383 /* bus re-enumerate */
1384 dbg("%s: Bus check notify on %s\n", __func__, objname);
1386 dbg("%s: re-enumerating slots under %s\n",
1388 acpiphp_check_bridge(bridge);
1390 if (num_sub_bridges)
1391 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1392 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1395 case ACPI_NOTIFY_DEVICE_CHECK:
1397 dbg("%s: Device check notify on %s\n", __func__, objname);
1398 acpiphp_check_bridge(bridge);
1401 case ACPI_NOTIFY_DEVICE_WAKE:
1403 dbg("%s: Device wake notify on %s\n", __func__, objname);
1406 case ACPI_NOTIFY_EJECT_REQUEST:
1407 /* request device eject */
1408 dbg("%s: Device eject notify on %s\n", __func__, objname);
1409 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1410 (bridge->flags & BRIDGE_HAS_EJ0)) {
1411 struct acpiphp_slot *slot;
1412 slot = bridge->func->slot;
1413 if (!acpiphp_disable_slot(slot))
1414 acpiphp_eject_slot(slot);
1418 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1419 printk(KERN_ERR "Device %s cannot be configured due"
1420 " to a frequency mismatch\n", objname);
1423 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1424 printk(KERN_ERR "Device %s cannot be configured due"
1425 " to a bus mode mismatch\n", objname);
1428 case ACPI_NOTIFY_POWER_FAULT:
1429 printk(KERN_ERR "Device %s has suffered a power fault\n",
1434 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1440 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1441 * @handle: Notify()'ed acpi_handle
1442 * @type: Notify code
1443 * @context: pointer to acpiphp_func structure
1445 * Handles ACPI event notification on slots.
1447 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1449 struct acpiphp_func *func;
1451 struct acpi_buffer buffer = { .length = sizeof(objname),
1452 .pointer = objname };
1454 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1456 func = (struct acpiphp_func *)context;
1459 case ACPI_NOTIFY_BUS_CHECK:
1460 /* bus re-enumerate */
1461 dbg("%s: Bus check notify on %s\n", __func__, objname);
1462 acpiphp_enable_slot(func->slot);
1465 case ACPI_NOTIFY_DEVICE_CHECK:
1466 /* device check : re-enumerate from parent bus */
1467 dbg("%s: Device check notify on %s\n", __func__, objname);
1468 acpiphp_check_bridge(func->slot->bridge);
1471 case ACPI_NOTIFY_DEVICE_WAKE:
1473 dbg("%s: Device wake notify on %s\n", __func__, objname);
1476 case ACPI_NOTIFY_EJECT_REQUEST:
1477 /* request device eject */
1478 dbg("%s: Device eject notify on %s\n", __func__, objname);
1479 if (!(acpiphp_disable_slot(func->slot)))
1480 acpiphp_eject_slot(func->slot);
1484 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1491 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1493 int *count = (int *)context;
1495 if (acpi_is_root_bridge(handle)) {
1496 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1497 handle_hotplug_event_bridge, NULL);
1503 static struct acpi_pci_driver acpi_pci_hp_driver = {
1505 .remove = remove_bridge,
1509 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1511 int __init acpiphp_glue_init(void)
1515 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1516 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1521 acpi_pci_register_driver(&acpi_pci_hp_driver);
1528 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1530 * This function frees all data allocated in acpiphp_glue_init().
1532 void acpiphp_glue_exit(void)
1534 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1539 * acpiphp_get_num_slots - count number of slots in a system
1541 int __init acpiphp_get_num_slots(void)
1543 struct acpiphp_bridge *bridge;
1546 list_for_each_entry (bridge, &bridge_list, list) {
1547 dbg("Bus %04x:%02x has %d slot%s\n",
1548 pci_domain_nr(bridge->pci_bus),
1549 bridge->pci_bus->number, bridge->nr_slots,
1550 bridge->nr_slots == 1 ? "" : "s");
1551 num_slots += bridge->nr_slots;
1554 dbg("Total %d slots\n", num_slots);
1561 * acpiphp_for_each_slot - call function for each slot
1562 * @fn: callback function
1563 * @data: context to be passed to callback function
1565 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1567 struct list_head *node;
1568 struct acpiphp_bridge *bridge;
1569 struct acpiphp_slot *slot;
1572 list_for_each (node, &bridge_list) {
1573 bridge = (struct acpiphp_bridge *)node;
1574 for (slot = bridge->slots; slot; slot = slot->next) {
1575 retval = fn(slot, data);
1588 * acpiphp_enable_slot - power on slot
1589 * @slot: ACPI PHP slot
1591 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1595 mutex_lock(&slot->crit_sect);
1597 /* wake up all functions */
1598 retval = power_on_slot(slot);
1602 if (get_slot_status(slot) == ACPI_STA_ALL) {
1603 /* configure all functions */
1604 retval = enable_device(slot);
1606 power_off_slot(slot);
1608 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1609 power_off_slot(slot);
1613 mutex_unlock(&slot->crit_sect);
1618 * acpiphp_disable_slot - power off slot
1619 * @slot: ACPI PHP slot
1621 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1625 mutex_lock(&slot->crit_sect);
1627 /* unconfigure all functions */
1628 retval = disable_device(slot);
1632 /* power off all functions */
1633 retval = power_off_slot(slot);
1638 mutex_unlock(&slot->crit_sect);
1647 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1649 return (slot->flags & SLOT_POWEREDON);
1657 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1661 sta = get_slot_status(slot);
1663 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1668 * adapter presence : 1
1671 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1675 sta = get_slot_status(slot);
1677 return (sta == 0) ? 0 : 1;