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>
50 #include <linux/slab.h>
51 #include <linux/acpi.h>
56 static LIST_HEAD(bridge_list);
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 const 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 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
136 if (ACPI_FAILURE(status)) {
137 warn("can't evaluate _ADR (%#x)\n", status);
141 device = (adr >> 16) & 0xffff;
142 function = adr & 0xffff;
145 if (pdev && pci_is_pcie(pdev)) {
146 tmp = acpi_find_root_bridge_handle(pdev);
148 struct acpi_pci_root *root = acpi_pci_find_root(tmp);
150 if (root && (root->osc_control_set &
151 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
156 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
160 INIT_LIST_HEAD(&newfunc->sibling);
161 newfunc->handle = handle;
162 newfunc->function = function;
164 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
165 newfunc->flags = FUNC_HAS_EJ0;
167 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
168 newfunc->flags |= FUNC_HAS_STA;
170 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
171 newfunc->flags |= FUNC_HAS_PS0;
173 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
174 newfunc->flags |= FUNC_HAS_PS3;
176 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
177 newfunc->flags |= FUNC_HAS_DCK;
179 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
180 if (ACPI_FAILURE(status)) {
182 * use the count of the number of slots we've found
183 * for the number of the slot
185 sun = bridge->nr_slots+1;
188 /* search for objects that share the same slot */
189 for (slot = bridge->slots; slot; slot = slot->next)
190 if (slot->device == device) {
191 if (slot->sun != sun)
192 warn("sibling found, but _SUN doesn't match!\n");
197 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
203 slot->bridge = bridge;
204 slot->device = device;
206 INIT_LIST_HEAD(&slot->funcs);
207 mutex_init(&slot->crit_sect);
209 slot->next = bridge->slots;
210 bridge->slots = slot;
214 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
215 slot->sun, pci_domain_nr(pbus), pbus->number, device);
216 retval = acpiphp_register_hotplug_slot(slot);
218 if (retval == -EBUSY)
219 warn("Slot %llu already registered by another "
220 "hotplug driver\n", slot->sun);
222 warn("acpiphp_register_hotplug_slot failed "
223 "(err code = 0x%x)\n", retval);
228 newfunc->slot = slot;
229 list_add_tail(&newfunc->sibling, &slot->funcs);
231 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
233 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
237 if (is_dock_device(handle)) {
238 /* we don't want to call this device's _EJ0
239 * because we want the dock notify handler
240 * to call it after it calls _DCK
242 newfunc->flags &= ~FUNC_HAS_EJ0;
243 if (register_hotplug_dock_device(handle,
244 &acpiphp_dock_ops, newfunc))
245 dbg("failed to register dock device\n");
247 /* we need to be notified when dock events happen
248 * outside of the hotplug operation, since we may
249 * need to do fixups before we can hotplug.
251 newfunc->nb.notifier_call = post_dock_fixups;
252 if (register_dock_notifier(&newfunc->nb))
253 dbg("failed to register a dock notifier");
256 /* install notify handler */
257 if (!(newfunc->flags & FUNC_HAS_DCK)) {
258 status = acpi_install_notify_handler(handle,
260 handle_hotplug_event_func,
263 if (ACPI_FAILURE(status))
264 err("failed to register interrupt notify handler\n");
272 bridge->slots = slot->next;
280 /* see if it's worth looking at this bridge */
281 static int detect_ejectable_slots(acpi_handle handle)
283 int found = acpi_pci_detect_ejectable(handle);
285 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
286 is_pci_dock_device, NULL, (void *)&found, NULL);
291 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
292 static void init_bridge_misc(struct acpiphp_bridge *bridge)
296 /* must be added to the list prior to calling register_slot */
297 list_add(&bridge->list, &bridge_list);
299 /* register all slot objects under this bridge */
300 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
301 register_slot, NULL, bridge, NULL);
302 if (ACPI_FAILURE(status)) {
303 list_del(&bridge->list);
307 /* install notify handler */
308 if (bridge->type != BRIDGE_TYPE_HOST) {
309 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
310 status = acpi_remove_notify_handler(bridge->func->handle,
312 handle_hotplug_event_func);
313 if (ACPI_FAILURE(status))
314 err("failed to remove notify handler\n");
316 status = acpi_install_notify_handler(bridge->handle,
318 handle_hotplug_event_bridge,
321 if (ACPI_FAILURE(status)) {
322 err("failed to register interrupt notify handler\n");
328 /* find acpiphp_func from acpiphp_bridge */
329 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
331 struct acpiphp_bridge *bridge;
332 struct acpiphp_slot *slot;
333 struct acpiphp_func *func;
335 list_for_each_entry(bridge, &bridge_list, list) {
336 for (slot = bridge->slots; slot; slot = slot->next) {
337 list_for_each_entry(func, &slot->funcs, sibling) {
338 if (func->handle == handle)
348 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
350 acpi_handle dummy_handle;
352 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
353 "_STA", &dummy_handle)))
354 bridge->flags |= BRIDGE_HAS_STA;
356 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
357 "_EJ0", &dummy_handle)))
358 bridge->flags |= BRIDGE_HAS_EJ0;
360 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
361 "_PS0", &dummy_handle)))
362 bridge->flags |= BRIDGE_HAS_PS0;
364 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
365 "_PS3", &dummy_handle)))
366 bridge->flags |= BRIDGE_HAS_PS3;
368 /* is this ejectable p2p bridge? */
369 if (bridge->flags & BRIDGE_HAS_EJ0) {
370 struct acpiphp_func *func;
372 dbg("found ejectable p2p bridge\n");
374 /* make link between PCI bridge and PCI function */
375 func = acpiphp_bridge_handle_to_function(bridge->handle);
379 func->bridge = bridge;
384 /* allocate and initialize host bridge data structure */
385 static void add_host_bridge(acpi_handle *handle)
387 struct acpiphp_bridge *bridge;
388 struct acpi_pci_root *root = acpi_pci_find_root(handle);
390 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
394 bridge->type = BRIDGE_TYPE_HOST;
395 bridge->handle = handle;
397 bridge->pci_bus = root->bus;
399 spin_lock_init(&bridge->res_lock);
401 init_bridge_misc(bridge);
405 /* allocate and initialize PCI-to-PCI bridge data structure */
406 static void add_p2p_bridge(acpi_handle *handle)
408 struct acpiphp_bridge *bridge;
410 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
411 if (bridge == NULL) {
412 err("out of memory\n");
416 bridge->type = BRIDGE_TYPE_P2P;
417 bridge->handle = handle;
418 config_p2p_bridge_flags(bridge);
420 bridge->pci_dev = acpi_get_pci_dev(handle);
421 bridge->pci_bus = bridge->pci_dev->subordinate;
422 if (!bridge->pci_bus) {
423 err("This is not a PCI-to-PCI bridge!\n");
428 * Grab a ref to the subordinate PCI bus in case the bus is
429 * removed via PCI core logical hotplug. The ref pins the bus
430 * (which we access during module unload).
432 get_device(&bridge->pci_bus->dev);
433 spin_lock_init(&bridge->res_lock);
435 init_bridge_misc(bridge);
438 pci_dev_put(bridge->pci_dev);
444 /* callback routine to find P2P bridges */
446 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
451 dev = acpi_get_pci_dev(handle);
452 if (!dev || !dev->subordinate)
455 /* check if this bridge has ejectable slots */
456 if ((detect_ejectable_slots(handle) > 0)) {
457 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
458 add_p2p_bridge(handle);
461 /* search P2P bridges under this p2p bridge */
462 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
463 find_p2p_bridge, NULL, NULL, NULL);
464 if (ACPI_FAILURE(status))
465 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
473 /* find hot-pluggable slots, and then find P2P bridge */
474 static int add_bridge(acpi_handle handle)
477 unsigned long long tmp;
478 acpi_handle dummy_handle;
480 /* if the bridge doesn't have _STA, we assume it is always there */
481 status = acpi_get_handle(handle, "_STA", &dummy_handle);
482 if (ACPI_SUCCESS(status)) {
483 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
484 if (ACPI_FAILURE(status)) {
485 dbg("%s: _STA evaluation failure\n", __func__);
488 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
489 /* don't register this object */
493 /* check if this bridge has ejectable slots */
494 if (detect_ejectable_slots(handle) > 0) {
495 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
496 add_host_bridge(handle);
499 /* search P2P bridges under this host bridge */
500 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
501 find_p2p_bridge, NULL, NULL, NULL);
503 if (ACPI_FAILURE(status))
504 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
509 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
511 struct acpiphp_bridge *bridge;
513 list_for_each_entry(bridge, &bridge_list, list)
514 if (bridge->handle == handle)
520 static void cleanup_bridge(struct acpiphp_bridge *bridge)
522 struct acpiphp_slot *slot, *next;
523 struct acpiphp_func *func, *tmp;
525 acpi_handle handle = bridge->handle;
527 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
528 handle_hotplug_event_bridge);
529 if (ACPI_FAILURE(status))
530 err("failed to remove notify handler\n");
532 if ((bridge->type != BRIDGE_TYPE_HOST) &&
533 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
534 status = acpi_install_notify_handler(bridge->func->handle,
536 handle_hotplug_event_func,
538 if (ACPI_FAILURE(status))
539 err("failed to install interrupt notify handler\n");
542 slot = bridge->slots;
545 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
546 if (is_dock_device(func->handle)) {
547 unregister_hotplug_dock_device(func->handle);
548 unregister_dock_notifier(&func->nb);
550 if (!(func->flags & FUNC_HAS_DCK)) {
551 status = acpi_remove_notify_handler(func->handle,
553 handle_hotplug_event_func);
554 if (ACPI_FAILURE(status))
555 err("failed to remove notify handler\n");
557 list_del(&func->sibling);
560 acpiphp_unregister_hotplug_slot(slot);
561 list_del(&slot->funcs);
567 * Only P2P bridges have a pci_dev
570 put_device(&bridge->pci_bus->dev);
572 pci_dev_put(bridge->pci_dev);
573 list_del(&bridge->list);
578 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
580 struct acpiphp_bridge *bridge;
582 /* cleanup p2p bridges under this P2P bridge
583 in a depth-first manner */
584 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
585 cleanup_p2p_bridge, NULL, NULL, NULL);
587 bridge = acpiphp_handle_to_bridge(handle);
589 cleanup_bridge(bridge);
594 static void remove_bridge(acpi_handle handle)
596 struct acpiphp_bridge *bridge;
598 /* cleanup p2p bridges under this host bridge
599 in a depth-first manner */
600 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
601 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
604 * On root bridges with hotplug slots directly underneath (ie,
605 * no p2p bridge between), we call cleanup_bridge().
607 * The else clause cleans up root bridges that either had no
608 * hotplug slots at all, or had a p2p bridge underneath.
610 bridge = acpiphp_handle_to_bridge(handle);
612 cleanup_bridge(bridge);
614 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
615 handle_hotplug_event_bridge);
618 static int power_on_slot(struct acpiphp_slot *slot)
621 struct acpiphp_func *func;
624 /* if already enabled, just skip */
625 if (slot->flags & SLOT_POWEREDON)
628 list_for_each_entry(func, &slot->funcs, sibling) {
629 if (func->flags & FUNC_HAS_PS0) {
630 dbg("%s: executing _PS0\n", __func__);
631 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
632 if (ACPI_FAILURE(status)) {
633 warn("%s: _PS0 failed\n", __func__);
641 /* TBD: evaluate _STA to check if the slot is enabled */
643 slot->flags |= SLOT_POWEREDON;
650 static int power_off_slot(struct acpiphp_slot *slot)
653 struct acpiphp_func *func;
657 /* if already disabled, just skip */
658 if ((slot->flags & SLOT_POWEREDON) == 0)
661 list_for_each_entry(func, &slot->funcs, sibling) {
662 if (func->flags & FUNC_HAS_PS3) {
663 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
664 if (ACPI_FAILURE(status)) {
665 warn("%s: _PS3 failed\n", __func__);
673 /* TBD: evaluate _STA to check if the slot is disabled */
675 slot->flags &= (~SLOT_POWEREDON);
684 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
685 * @bus: bus to start search with
687 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
689 struct list_head *tmp;
690 unsigned char max, n;
693 * pci_bus_max_busnr will return the highest
694 * reserved busnr for all these children.
695 * that is equivalent to the bus->subordinate
696 * value. We don't want to use the parent's
697 * bus->subordinate value because it could have
700 max = bus->secondary;
702 list_for_each(tmp, &bus->children) {
703 n = pci_bus_max_busnr(pci_bus_b(tmp));
712 * acpiphp_bus_add - add a new bus to acpi subsystem
713 * @func: acpiphp_func of the bridge
715 static int acpiphp_bus_add(struct acpiphp_func *func)
718 struct acpi_device *device, *pdevice;
721 acpi_get_parent(func->handle, &phandle);
722 if (acpi_bus_get_device(phandle, &pdevice)) {
723 dbg("no parent device, assuming NULL\n");
726 if (!acpi_bus_get_device(func->handle, &device)) {
727 dbg("bus exists... trim\n");
728 /* this shouldn't be in here, so remove
729 * the bus then re-add it...
731 ret_val = acpi_bus_trim(device, 1);
732 dbg("acpi_bus_trim return %x\n", ret_val);
735 ret_val = acpi_bus_add(&device, pdevice, func->handle,
736 ACPI_BUS_TYPE_DEVICE);
738 dbg("error adding bus, %x\n",
740 goto acpiphp_bus_add_out;
742 ret_val = acpi_bus_start(device);
750 * acpiphp_bus_trim - trim a bus from acpi subsystem
751 * @handle: handle to acpi namespace
753 static int acpiphp_bus_trim(acpi_handle handle)
755 struct acpi_device *device;
758 retval = acpi_bus_get_device(handle, &device);
760 dbg("acpi_device not found\n");
764 retval = acpi_bus_trim(device, 1);
766 err("cannot remove from acpi list\n");
771 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
773 struct acpiphp_func *func;
774 union acpi_object params[2];
775 struct acpi_object_list arg_list;
777 list_for_each_entry(func, &slot->funcs, sibling) {
779 arg_list.pointer = params;
780 params[0].type = ACPI_TYPE_INTEGER;
781 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
782 params[1].type = ACPI_TYPE_INTEGER;
783 params[1].integer.value = 1;
784 /* _REG is optional, we don't care about if there is failure */
785 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
790 * enable_device - enable, configure a slot
791 * @slot: slot to be enabled
793 * This function should be called per *physical slot*,
794 * not per each slot object in ACPI namespace.
796 static int __ref enable_device(struct acpiphp_slot *slot)
799 struct pci_bus *bus = slot->bridge->pci_bus;
800 struct acpiphp_func *func;
805 if (slot->flags & SLOT_ENABLED)
808 /* sanity check: dev should be NULL when hot-plugged in */
809 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
811 /* This case shouldn't happen */
812 err("pci_dev structure already exists.\n");
818 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
820 err("No new device found\n");
825 max = acpiphp_max_busnr(bus);
826 for (pass = 0; pass < 2; pass++) {
827 list_for_each_entry(dev, &bus->devices, bus_list) {
828 if (PCI_SLOT(dev->devfn) != slot->device)
830 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
831 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
832 max = pci_scan_bridge(bus, dev, max, pass);
833 if (pass && dev->subordinate)
834 pci_bus_size_bridges(dev->subordinate);
839 list_for_each_entry(func, &slot->funcs, sibling)
840 acpiphp_bus_add(func);
842 pci_bus_assign_resources(bus);
843 acpiphp_sanitize_bus(bus);
844 acpiphp_set_hpp_values(bus);
845 acpiphp_set_acpi_region(slot);
846 pci_enable_bridges(bus);
848 list_for_each_entry(dev, &bus->devices, bus_list) {
849 /* Assume that newly added devices are powered on already. */
851 dev->current_state = PCI_D0;
854 pci_bus_add_devices(bus);
856 list_for_each_entry(func, &slot->funcs, sibling) {
857 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
862 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
863 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
868 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
869 if (ACPI_FAILURE(status))
870 warn("find_p2p_bridge failed (error code = 0x%x)\n",
875 slot->flags |= SLOT_ENABLED;
881 static void disable_bridges(struct pci_bus *bus)
884 list_for_each_entry(dev, &bus->devices, bus_list) {
885 if (dev->subordinate) {
886 disable_bridges(dev->subordinate);
887 pci_disable_device(dev);
893 * disable_device - disable a slot
894 * @slot: ACPI PHP slot
896 static int disable_device(struct acpiphp_slot *slot)
898 struct acpiphp_func *func;
899 struct pci_dev *pdev;
901 /* is this slot already disabled? */
902 if (!(slot->flags & SLOT_ENABLED))
905 list_for_each_entry(func, &slot->funcs, sibling) {
907 /* cleanup p2p bridges under this P2P bridge */
908 cleanup_p2p_bridge(func->bridge->handle,
913 pdev = pci_get_slot(slot->bridge->pci_bus,
914 PCI_DEVFN(slot->device, func->function));
916 pci_stop_bus_device(pdev);
917 if (pdev->subordinate) {
918 disable_bridges(pdev->subordinate);
919 pci_disable_device(pdev);
921 pci_remove_bus_device(pdev);
926 list_for_each_entry(func, &slot->funcs, sibling) {
927 acpiphp_bus_trim(func->handle);
930 slot->flags &= (~SLOT_ENABLED);
938 * get_slot_status - get ACPI slot status
939 * @slot: ACPI PHP slot
941 * If a slot has _STA for each function and if any one of them
942 * returned non-zero status, return it.
944 * If a slot doesn't have _STA and if any one of its functions'
945 * configuration space is configured, return 0x0f as a _STA.
947 * Otherwise return 0.
949 static unsigned int get_slot_status(struct acpiphp_slot *slot)
952 unsigned long long sta = 0;
954 struct acpiphp_func *func;
956 list_for_each_entry(func, &slot->funcs, sibling) {
957 if (func->flags & FUNC_HAS_STA) {
958 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
959 if (ACPI_SUCCESS(status) && sta)
962 pci_bus_read_config_dword(slot->bridge->pci_bus,
963 PCI_DEVFN(slot->device,
965 PCI_VENDOR_ID, &dvid);
966 if (dvid != 0xffffffff) {
973 return (unsigned int)sta;
977 * acpiphp_eject_slot - physically eject the slot
978 * @slot: ACPI PHP slot
980 int acpiphp_eject_slot(struct acpiphp_slot *slot)
983 struct acpiphp_func *func;
984 struct acpi_object_list arg_list;
985 union acpi_object arg;
987 list_for_each_entry(func, &slot->funcs, sibling) {
988 /* We don't want to call _EJ0 on non-existing functions. */
989 if ((func->flags & FUNC_HAS_EJ0)) {
990 /* _EJ0 method take one argument */
992 arg_list.pointer = &arg;
993 arg.type = ACPI_TYPE_INTEGER;
994 arg.integer.value = 1;
996 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
997 if (ACPI_FAILURE(status)) {
998 warn("%s: _EJ0 failed\n", __func__);
1008 * acpiphp_check_bridge - re-enumerate devices
1009 * @bridge: where to begin re-enumeration
1011 * Iterate over all slots under this bridge and make sure that if a
1012 * card is present they are enabled, and if not they are disabled.
1014 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1016 struct acpiphp_slot *slot;
1018 int enabled, disabled;
1020 enabled = disabled = 0;
1022 for (slot = bridge->slots; slot; slot = slot->next) {
1023 unsigned int status = get_slot_status(slot);
1024 if (slot->flags & SLOT_ENABLED) {
1025 if (status == ACPI_STA_ALL)
1027 retval = acpiphp_disable_slot(slot);
1029 err("Error occurred in disabling\n");
1032 acpiphp_eject_slot(slot);
1036 if (status != ACPI_STA_ALL)
1038 retval = acpiphp_enable_slot(slot);
1040 err("Error occurred in enabling\n");
1047 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1053 static void acpiphp_set_hpp_values(struct pci_bus *bus)
1055 struct pci_dev *dev;
1057 list_for_each_entry(dev, &bus->devices, bus_list)
1058 pci_configure_slot(dev);
1062 * Remove devices for which we could not assign resources, call
1063 * arch specific code to fix-up the bus
1065 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1067 struct pci_dev *dev;
1069 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1071 list_for_each_entry(dev, &bus->devices, bus_list) {
1072 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1073 struct resource *res = &dev->resource[i];
1074 if ((res->flags & type_mask) && !res->start &&
1076 /* Could not assign a required resources
1077 * for this device, remove it */
1078 pci_remove_bus_device(dev);
1085 /* Program resources in newly inserted bridge */
1086 static int acpiphp_configure_bridge (acpi_handle handle)
1088 struct pci_bus *bus;
1090 if (acpi_is_root_bridge(handle)) {
1091 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1094 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1095 bus = pdev->subordinate;
1099 pci_bus_size_bridges(bus);
1100 pci_bus_assign_resources(bus);
1101 acpiphp_sanitize_bus(bus);
1102 acpiphp_set_hpp_values(bus);
1103 pci_enable_bridges(bus);
1107 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1109 struct acpi_device *device, *pdevice;
1110 acpi_handle phandle;
1112 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1113 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1114 err("unexpected notification type %d\n", type);
1118 acpi_get_parent(handle, &phandle);
1119 if (acpi_bus_get_device(phandle, &pdevice)) {
1120 dbg("no parent device, assuming NULL\n");
1123 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1124 err("cannot add bridge to acpi list\n");
1127 if (!acpiphp_configure_bridge(handle) &&
1128 !acpi_bus_start(device))
1131 err("cannot configure and start bridge\n");
1136 * ACPI event handlers
1140 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1142 int *count = (int *)context;
1143 struct acpiphp_bridge *bridge;
1145 bridge = acpiphp_handle_to_bridge(handle);
1152 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1154 struct acpiphp_bridge *bridge;
1156 struct acpi_buffer buffer = { .length = sizeof(objname),
1157 .pointer = objname };
1159 bridge = acpiphp_handle_to_bridge(handle);
1161 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1162 dbg("%s: re-enumerating slots under %s\n",
1164 acpiphp_check_bridge(bridge);
1169 struct acpiphp_hp_work {
1170 struct work_struct work;
1176 static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type,
1178 void (*func)(struct work_struct *work))
1180 struct acpiphp_hp_work *hp_work;
1183 hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL);
1187 hp_work->handle = handle;
1188 hp_work->type = type;
1189 hp_work->context = context;
1191 INIT_WORK(&hp_work->work, func);
1192 ret = queue_work(kacpi_hotplug_wq, &hp_work->work);
1197 static void _handle_hotplug_event_bridge(struct work_struct *work)
1199 struct acpiphp_bridge *bridge;
1201 struct acpi_buffer buffer = { .length = sizeof(objname),
1202 .pointer = objname };
1203 struct acpi_device *device;
1204 int num_sub_bridges = 0;
1205 struct acpiphp_hp_work *hp_work;
1209 hp_work = container_of(work, struct acpiphp_hp_work, work);
1210 handle = hp_work->handle;
1211 type = hp_work->type;
1213 if (acpi_bus_get_device(handle, &device)) {
1214 /* This bridge must have just been physically inserted */
1215 handle_bridge_insertion(handle, type);
1219 bridge = acpiphp_handle_to_bridge(handle);
1220 if (type == ACPI_NOTIFY_BUS_CHECK) {
1221 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1222 count_sub_bridges, NULL, &num_sub_bridges, NULL);
1225 if (!bridge && !num_sub_bridges) {
1226 err("cannot get bridge info\n");
1230 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1233 case ACPI_NOTIFY_BUS_CHECK:
1234 /* bus re-enumerate */
1235 dbg("%s: Bus check notify on %s\n", __func__, objname);
1237 dbg("%s: re-enumerating slots under %s\n",
1239 acpiphp_check_bridge(bridge);
1241 if (num_sub_bridges)
1242 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1243 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1246 case ACPI_NOTIFY_DEVICE_CHECK:
1248 dbg("%s: Device check notify on %s\n", __func__, objname);
1249 acpiphp_check_bridge(bridge);
1252 case ACPI_NOTIFY_DEVICE_WAKE:
1254 dbg("%s: Device wake notify on %s\n", __func__, objname);
1257 case ACPI_NOTIFY_EJECT_REQUEST:
1258 /* request device eject */
1259 dbg("%s: Device eject notify on %s\n", __func__, objname);
1260 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1261 (bridge->flags & BRIDGE_HAS_EJ0)) {
1262 struct acpiphp_slot *slot;
1263 slot = bridge->func->slot;
1264 if (!acpiphp_disable_slot(slot))
1265 acpiphp_eject_slot(slot);
1269 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1270 printk(KERN_ERR "Device %s cannot be configured due"
1271 " to a frequency mismatch\n", objname);
1274 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1275 printk(KERN_ERR "Device %s cannot be configured due"
1276 " to a bus mode mismatch\n", objname);
1279 case ACPI_NOTIFY_POWER_FAULT:
1280 printk(KERN_ERR "Device %s has suffered a power fault\n",
1285 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1290 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1294 * handle_hotplug_event_bridge - handle ACPI event on bridges
1295 * @handle: Notify()'ed acpi_handle
1296 * @type: Notify code
1297 * @context: pointer to acpiphp_bridge structure
1299 * Handles ACPI event notification on {host,p2p} bridges.
1301 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1305 * Currently the code adds all hotplug events to the kacpid_wq
1306 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1307 * The proper way to fix this is to reorganize the code so that
1308 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1309 * For now just re-add this work to the kacpi_hotplug_wq so we
1310 * don't deadlock on hotplug actions.
1312 alloc_acpiphp_hp_work(handle, type, context,
1313 _handle_hotplug_event_bridge);
1316 static void _handle_hotplug_event_func(struct work_struct *work)
1318 struct acpiphp_func *func;
1320 struct acpi_buffer buffer = { .length = sizeof(objname),
1321 .pointer = objname };
1322 struct acpiphp_hp_work *hp_work;
1327 hp_work = container_of(work, struct acpiphp_hp_work, work);
1328 handle = hp_work->handle;
1329 type = hp_work->type;
1330 context = hp_work->context;
1332 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1334 func = (struct acpiphp_func *)context;
1337 case ACPI_NOTIFY_BUS_CHECK:
1338 /* bus re-enumerate */
1339 dbg("%s: Bus check notify on %s\n", __func__, objname);
1340 acpiphp_enable_slot(func->slot);
1343 case ACPI_NOTIFY_DEVICE_CHECK:
1344 /* device check : re-enumerate from parent bus */
1345 dbg("%s: Device check notify on %s\n", __func__, objname);
1346 acpiphp_check_bridge(func->slot->bridge);
1349 case ACPI_NOTIFY_DEVICE_WAKE:
1351 dbg("%s: Device wake notify on %s\n", __func__, objname);
1354 case ACPI_NOTIFY_EJECT_REQUEST:
1355 /* request device eject */
1356 dbg("%s: Device eject notify on %s\n", __func__, objname);
1357 if (!(acpiphp_disable_slot(func->slot)))
1358 acpiphp_eject_slot(func->slot);
1362 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1366 kfree(hp_work); /* allocated in handle_hotplug_event_func */
1370 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1371 * @handle: Notify()'ed acpi_handle
1372 * @type: Notify code
1373 * @context: pointer to acpiphp_func structure
1375 * Handles ACPI event notification on slots.
1377 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1381 * Currently the code adds all hotplug events to the kacpid_wq
1382 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1383 * The proper way to fix this is to reorganize the code so that
1384 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1385 * For now just re-add this work to the kacpi_hotplug_wq so we
1386 * don't deadlock on hotplug actions.
1388 alloc_acpiphp_hp_work(handle, type, context,
1389 _handle_hotplug_event_func);
1393 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1395 int *count = (int *)context;
1397 if (!acpi_is_root_bridge(handle))
1401 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1402 handle_hotplug_event_bridge, NULL);
1407 static struct acpi_pci_driver acpi_pci_hp_driver = {
1409 .remove = remove_bridge,
1413 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1415 int __init acpiphp_glue_init(void)
1419 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1420 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
1425 acpi_pci_register_driver(&acpi_pci_hp_driver);
1432 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1434 * This function frees all data allocated in acpiphp_glue_init().
1436 void acpiphp_glue_exit(void)
1438 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1443 * acpiphp_get_num_slots - count number of slots in a system
1445 int __init acpiphp_get_num_slots(void)
1447 struct acpiphp_bridge *bridge;
1450 list_for_each_entry(bridge, &bridge_list, list) {
1451 dbg("Bus %04x:%02x has %d slot%s\n",
1452 pci_domain_nr(bridge->pci_bus),
1453 bridge->pci_bus->number, bridge->nr_slots,
1454 bridge->nr_slots == 1 ? "" : "s");
1455 num_slots += bridge->nr_slots;
1458 dbg("Total %d slots\n", num_slots);
1465 * acpiphp_for_each_slot - call function for each slot
1466 * @fn: callback function
1467 * @data: context to be passed to callback function
1469 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1471 struct list_head *node;
1472 struct acpiphp_bridge *bridge;
1473 struct acpiphp_slot *slot;
1476 list_for_each (node, &bridge_list) {
1477 bridge = (struct acpiphp_bridge *)node;
1478 for (slot = bridge->slots; slot; slot = slot->next) {
1479 retval = fn(slot, data);
1492 * acpiphp_enable_slot - power on slot
1493 * @slot: ACPI PHP slot
1495 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1499 mutex_lock(&slot->crit_sect);
1501 /* wake up all functions */
1502 retval = power_on_slot(slot);
1506 if (get_slot_status(slot) == ACPI_STA_ALL) {
1507 /* configure all functions */
1508 retval = enable_device(slot);
1510 power_off_slot(slot);
1512 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1513 power_off_slot(slot);
1517 mutex_unlock(&slot->crit_sect);
1522 * acpiphp_disable_slot - power off slot
1523 * @slot: ACPI PHP slot
1525 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1529 mutex_lock(&slot->crit_sect);
1531 /* unconfigure all functions */
1532 retval = disable_device(slot);
1536 /* power off all functions */
1537 retval = power_off_slot(slot);
1542 mutex_unlock(&slot->crit_sect);
1551 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1553 return (slot->flags & SLOT_POWEREDON);
1561 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1565 sta = get_slot_status(slot);
1567 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1572 * adapter presence : 1
1575 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1579 sta = get_slot_status(slot);
1581 return (sta == 0) ? 0 : 1;