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->busn_res.start) {
104 buses = (buses & 0xff000000)
105 | ((unsigned int)(bus->primary) << 0)
106 | ((unsigned int)(bus->busn_res.start) << 8)
107 | ((unsigned int)(bus->busn_res.end) << 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 /* Check whether the PCI device is managed by native PCIe hotplug driver */
119 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
123 struct acpi_pci_root *root;
125 /* Check whether the PCIe port supports native PCIe hotplug */
126 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32))
128 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
132 * Check whether native PCIe hotplug has been enabled for
133 * this PCIe hierarchy.
135 tmp = acpi_find_root_bridge_handle(pdev);
138 root = acpi_pci_find_root(tmp);
141 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
147 /* callback routine to register each ACPI PCI slot object */
149 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
151 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
152 struct acpiphp_slot *slot;
153 struct acpiphp_func *newfunc;
155 acpi_status status = AE_OK;
156 unsigned long long adr, sun;
157 int device, function, retval;
158 struct pci_bus *pbus = bridge->pci_bus;
159 struct pci_dev *pdev;
161 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
164 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
165 if (ACPI_FAILURE(status)) {
166 warn("can't evaluate _ADR (%#x)\n", status);
170 device = (adr >> 16) & 0xffff;
171 function = adr & 0xffff;
174 if (pdev && device_is_managed_by_native_pciehp(pdev))
177 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
181 INIT_LIST_HEAD(&newfunc->sibling);
182 newfunc->handle = handle;
183 newfunc->function = function;
185 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
186 newfunc->flags = FUNC_HAS_EJ0;
188 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
189 newfunc->flags |= FUNC_HAS_STA;
191 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
192 newfunc->flags |= FUNC_HAS_PS0;
194 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
195 newfunc->flags |= FUNC_HAS_PS3;
197 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
198 newfunc->flags |= FUNC_HAS_DCK;
200 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
201 if (ACPI_FAILURE(status)) {
203 * use the count of the number of slots we've found
204 * for the number of the slot
206 sun = bridge->nr_slots+1;
209 /* search for objects that share the same slot */
210 for (slot = bridge->slots; slot; slot = slot->next)
211 if (slot->device == device) {
212 if (slot->sun != sun)
213 warn("sibling found, but _SUN doesn't match!\n");
218 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
224 slot->bridge = bridge;
225 slot->device = device;
227 INIT_LIST_HEAD(&slot->funcs);
228 mutex_init(&slot->crit_sect);
230 slot->next = bridge->slots;
231 bridge->slots = slot;
235 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
236 slot->sun, pci_domain_nr(pbus), pbus->number, device);
237 retval = acpiphp_register_hotplug_slot(slot);
239 if (retval == -EBUSY)
240 warn("Slot %llu already registered by another "
241 "hotplug driver\n", slot->sun);
243 warn("acpiphp_register_hotplug_slot failed "
244 "(err code = 0x%x)\n", retval);
249 newfunc->slot = slot;
250 list_add_tail(&newfunc->sibling, &slot->funcs);
252 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
254 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
258 if (is_dock_device(handle)) {
259 /* we don't want to call this device's _EJ0
260 * because we want the dock notify handler
261 * to call it after it calls _DCK
263 newfunc->flags &= ~FUNC_HAS_EJ0;
264 if (register_hotplug_dock_device(handle,
265 &acpiphp_dock_ops, newfunc))
266 dbg("failed to register dock device\n");
268 /* we need to be notified when dock events happen
269 * outside of the hotplug operation, since we may
270 * need to do fixups before we can hotplug.
272 newfunc->nb.notifier_call = post_dock_fixups;
273 if (register_dock_notifier(&newfunc->nb))
274 dbg("failed to register a dock notifier");
277 /* install notify handler */
278 if (!(newfunc->flags & FUNC_HAS_DCK)) {
279 status = acpi_install_notify_handler(handle,
281 handle_hotplug_event_func,
284 if (ACPI_FAILURE(status))
285 err("failed to register interrupt notify handler\n");
293 bridge->slots = slot->next;
301 /* see if it's worth looking at this bridge */
302 static int detect_ejectable_slots(acpi_handle handle)
304 int found = acpi_pci_detect_ejectable(handle);
306 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
307 is_pci_dock_device, NULL, (void *)&found, NULL);
312 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
313 static void init_bridge_misc(struct acpiphp_bridge *bridge)
317 /* must be added to the list prior to calling register_slot */
318 list_add(&bridge->list, &bridge_list);
320 /* register all slot objects under this bridge */
321 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
322 register_slot, NULL, bridge, NULL);
323 if (ACPI_FAILURE(status)) {
324 list_del(&bridge->list);
328 /* install notify handler */
329 if (bridge->type != BRIDGE_TYPE_HOST) {
330 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
331 status = acpi_remove_notify_handler(bridge->func->handle,
333 handle_hotplug_event_func);
334 if (ACPI_FAILURE(status))
335 err("failed to remove notify handler\n");
337 status = acpi_install_notify_handler(bridge->handle,
339 handle_hotplug_event_bridge,
342 if (ACPI_FAILURE(status)) {
343 err("failed to register interrupt notify handler\n");
349 /* find acpiphp_func from acpiphp_bridge */
350 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
352 struct acpiphp_bridge *bridge;
353 struct acpiphp_slot *slot;
354 struct acpiphp_func *func;
356 list_for_each_entry(bridge, &bridge_list, list) {
357 for (slot = bridge->slots; slot; slot = slot->next) {
358 list_for_each_entry(func, &slot->funcs, sibling) {
359 if (func->handle == handle)
369 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
371 acpi_handle dummy_handle;
373 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
374 "_STA", &dummy_handle)))
375 bridge->flags |= BRIDGE_HAS_STA;
377 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
378 "_EJ0", &dummy_handle)))
379 bridge->flags |= BRIDGE_HAS_EJ0;
381 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
382 "_PS0", &dummy_handle)))
383 bridge->flags |= BRIDGE_HAS_PS0;
385 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
386 "_PS3", &dummy_handle)))
387 bridge->flags |= BRIDGE_HAS_PS3;
389 /* is this ejectable p2p bridge? */
390 if (bridge->flags & BRIDGE_HAS_EJ0) {
391 struct acpiphp_func *func;
393 dbg("found ejectable p2p bridge\n");
395 /* make link between PCI bridge and PCI function */
396 func = acpiphp_bridge_handle_to_function(bridge->handle);
400 func->bridge = bridge;
405 /* allocate and initialize host bridge data structure */
406 static void add_host_bridge(acpi_handle *handle)
408 struct acpiphp_bridge *bridge;
409 struct acpi_pci_root *root = acpi_pci_find_root(handle);
411 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
415 bridge->type = BRIDGE_TYPE_HOST;
416 bridge->handle = handle;
418 bridge->pci_bus = root->bus;
420 init_bridge_misc(bridge);
424 /* allocate and initialize PCI-to-PCI bridge data structure */
425 static void add_p2p_bridge(acpi_handle *handle)
427 struct acpiphp_bridge *bridge;
429 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
430 if (bridge == NULL) {
431 err("out of memory\n");
435 bridge->type = BRIDGE_TYPE_P2P;
436 bridge->handle = handle;
437 config_p2p_bridge_flags(bridge);
439 bridge->pci_dev = acpi_get_pci_dev(handle);
440 bridge->pci_bus = bridge->pci_dev->subordinate;
441 if (!bridge->pci_bus) {
442 err("This is not a PCI-to-PCI bridge!\n");
447 * Grab a ref to the subordinate PCI bus in case the bus is
448 * removed via PCI core logical hotplug. The ref pins the bus
449 * (which we access during module unload).
451 get_device(&bridge->pci_bus->dev);
453 init_bridge_misc(bridge);
456 pci_dev_put(bridge->pci_dev);
462 /* callback routine to find P2P bridges */
464 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
469 dev = acpi_get_pci_dev(handle);
470 if (!dev || !dev->subordinate)
473 /* check if this bridge has ejectable slots */
474 if ((detect_ejectable_slots(handle) > 0)) {
475 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
476 add_p2p_bridge(handle);
479 /* search P2P bridges under this p2p bridge */
480 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
481 find_p2p_bridge, NULL, NULL, NULL);
482 if (ACPI_FAILURE(status))
483 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
491 /* find hot-pluggable slots, and then find P2P bridge */
492 static int add_bridge(acpi_handle handle)
495 unsigned long long tmp;
496 acpi_handle dummy_handle;
498 /* if the bridge doesn't have _STA, we assume it is always there */
499 status = acpi_get_handle(handle, "_STA", &dummy_handle);
500 if (ACPI_SUCCESS(status)) {
501 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
502 if (ACPI_FAILURE(status)) {
503 dbg("%s: _STA evaluation failure\n", __func__);
506 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
507 /* don't register this object */
511 /* check if this bridge has ejectable slots */
512 if (detect_ejectable_slots(handle) > 0) {
513 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
514 add_host_bridge(handle);
517 /* search P2P bridges under this host bridge */
518 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
519 find_p2p_bridge, NULL, NULL, NULL);
521 if (ACPI_FAILURE(status))
522 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
527 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
529 struct acpiphp_bridge *bridge;
531 list_for_each_entry(bridge, &bridge_list, list)
532 if (bridge->handle == handle)
538 static void cleanup_bridge(struct acpiphp_bridge *bridge)
540 struct acpiphp_slot *slot, *next;
541 struct acpiphp_func *func, *tmp;
543 acpi_handle handle = bridge->handle;
545 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
546 handle_hotplug_event_bridge);
547 if (ACPI_FAILURE(status))
548 err("failed to remove notify handler\n");
550 if ((bridge->type != BRIDGE_TYPE_HOST) &&
551 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
552 status = acpi_install_notify_handler(bridge->func->handle,
554 handle_hotplug_event_func,
556 if (ACPI_FAILURE(status))
557 err("failed to install interrupt notify handler\n");
560 slot = bridge->slots;
563 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
564 if (is_dock_device(func->handle)) {
565 unregister_hotplug_dock_device(func->handle);
566 unregister_dock_notifier(&func->nb);
568 if (!(func->flags & FUNC_HAS_DCK)) {
569 status = acpi_remove_notify_handler(func->handle,
571 handle_hotplug_event_func);
572 if (ACPI_FAILURE(status))
573 err("failed to remove notify handler\n");
575 list_del(&func->sibling);
578 acpiphp_unregister_hotplug_slot(slot);
579 list_del(&slot->funcs);
585 * Only P2P bridges have a pci_dev
588 put_device(&bridge->pci_bus->dev);
590 pci_dev_put(bridge->pci_dev);
591 list_del(&bridge->list);
596 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
598 struct acpiphp_bridge *bridge;
600 /* cleanup p2p bridges under this P2P bridge
601 in a depth-first manner */
602 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
603 cleanup_p2p_bridge, NULL, NULL, NULL);
605 bridge = acpiphp_handle_to_bridge(handle);
607 cleanup_bridge(bridge);
612 static void remove_bridge(acpi_handle handle)
614 struct acpiphp_bridge *bridge;
616 /* cleanup p2p bridges under this host bridge
617 in a depth-first manner */
618 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
619 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
622 * On root bridges with hotplug slots directly underneath (ie,
623 * no p2p bridge between), we call cleanup_bridge().
625 * The else clause cleans up root bridges that either had no
626 * hotplug slots at all, or had a p2p bridge underneath.
628 bridge = acpiphp_handle_to_bridge(handle);
630 cleanup_bridge(bridge);
632 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
633 handle_hotplug_event_bridge);
636 static int power_on_slot(struct acpiphp_slot *slot)
639 struct acpiphp_func *func;
642 /* if already enabled, just skip */
643 if (slot->flags & SLOT_POWEREDON)
646 list_for_each_entry(func, &slot->funcs, sibling) {
647 if (func->flags & FUNC_HAS_PS0) {
648 dbg("%s: executing _PS0\n", __func__);
649 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
650 if (ACPI_FAILURE(status)) {
651 warn("%s: _PS0 failed\n", __func__);
659 /* TBD: evaluate _STA to check if the slot is enabled */
661 slot->flags |= SLOT_POWEREDON;
668 static int power_off_slot(struct acpiphp_slot *slot)
671 struct acpiphp_func *func;
675 /* if already disabled, just skip */
676 if ((slot->flags & SLOT_POWEREDON) == 0)
679 list_for_each_entry(func, &slot->funcs, sibling) {
680 if (func->flags & FUNC_HAS_PS3) {
681 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
682 if (ACPI_FAILURE(status)) {
683 warn("%s: _PS3 failed\n", __func__);
691 /* TBD: evaluate _STA to check if the slot is disabled */
693 slot->flags &= (~SLOT_POWEREDON);
702 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
703 * @bus: bus to start search with
705 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
707 struct list_head *tmp;
708 unsigned char max, n;
711 * pci_bus_max_busnr will return the highest
712 * reserved busnr for all these children.
713 * that is equivalent to the bus->subordinate
714 * value. We don't want to use the parent's
715 * bus->subordinate value because it could have
718 max = bus->busn_res.start;
720 list_for_each(tmp, &bus->children) {
721 n = pci_bus_max_busnr(pci_bus_b(tmp));
730 * acpiphp_bus_add - add a new bus to acpi subsystem
731 * @func: acpiphp_func of the bridge
733 static int acpiphp_bus_add(struct acpiphp_func *func)
736 struct acpi_device *device, *pdevice;
739 acpi_get_parent(func->handle, &phandle);
740 if (acpi_bus_get_device(phandle, &pdevice)) {
741 dbg("no parent device, assuming NULL\n");
744 if (!acpi_bus_get_device(func->handle, &device)) {
745 dbg("bus exists... trim\n");
746 /* this shouldn't be in here, so remove
747 * the bus then re-add it...
749 ret_val = acpi_bus_trim(device, 1);
750 dbg("acpi_bus_trim return %x\n", ret_val);
753 ret_val = acpi_bus_add(&device, pdevice, func->handle,
754 ACPI_BUS_TYPE_DEVICE);
756 dbg("error adding bus, %x\n",
758 goto acpiphp_bus_add_out;
760 ret_val = acpi_bus_start(device);
768 * acpiphp_bus_trim - trim a bus from acpi subsystem
769 * @handle: handle to acpi namespace
771 static int acpiphp_bus_trim(acpi_handle handle)
773 struct acpi_device *device;
776 retval = acpi_bus_get_device(handle, &device);
778 dbg("acpi_device not found\n");
782 retval = acpi_bus_trim(device, 1);
784 err("cannot remove from acpi list\n");
789 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
791 struct acpiphp_func *func;
792 union acpi_object params[2];
793 struct acpi_object_list arg_list;
795 list_for_each_entry(func, &slot->funcs, sibling) {
797 arg_list.pointer = params;
798 params[0].type = ACPI_TYPE_INTEGER;
799 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
800 params[1].type = ACPI_TYPE_INTEGER;
801 params[1].integer.value = 1;
802 /* _REG is optional, we don't care about if there is failure */
803 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
808 * enable_device - enable, configure a slot
809 * @slot: slot to be enabled
811 * This function should be called per *physical slot*,
812 * not per each slot object in ACPI namespace.
814 static int __ref enable_device(struct acpiphp_slot *slot)
817 struct pci_bus *bus = slot->bridge->pci_bus;
818 struct acpiphp_func *func;
823 if (slot->flags & SLOT_ENABLED)
826 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
828 /* Maybe only part of funcs are added. */
829 dbg("No new device found\n");
833 max = acpiphp_max_busnr(bus);
834 for (pass = 0; pass < 2; pass++) {
835 list_for_each_entry(dev, &bus->devices, bus_list) {
836 if (PCI_SLOT(dev->devfn) != slot->device)
838 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
839 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
840 max = pci_scan_bridge(bus, dev, max, pass);
841 if (pass && dev->subordinate)
842 pci_bus_size_bridges(dev->subordinate);
847 list_for_each_entry(func, &slot->funcs, sibling)
848 acpiphp_bus_add(func);
850 pci_bus_assign_resources(bus);
851 acpiphp_sanitize_bus(bus);
852 acpiphp_set_hpp_values(bus);
853 acpiphp_set_acpi_region(slot);
854 pci_enable_bridges(bus);
856 list_for_each_entry(dev, &bus->devices, bus_list) {
857 /* Assume that newly added devices are powered on already. */
859 dev->current_state = PCI_D0;
862 pci_bus_add_devices(bus);
864 slot->flags |= SLOT_ENABLED;
865 list_for_each_entry(func, &slot->funcs, sibling) {
866 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
869 /* Do not set SLOT_ENABLED flag if some funcs
871 slot->flags &= (~SLOT_ENABLED);
875 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
876 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
881 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
882 if (ACPI_FAILURE(status))
883 warn("find_p2p_bridge failed (error code = 0x%x)\n",
893 static void disable_bridges(struct pci_bus *bus)
896 list_for_each_entry(dev, &bus->devices, bus_list) {
897 if (dev->subordinate) {
898 disable_bridges(dev->subordinate);
899 pci_disable_device(dev);
904 /* return first device in slot, acquiring a reference on it */
905 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
907 struct pci_bus *bus = slot->bridge->pci_bus;
909 struct pci_dev *ret = NULL;
911 down_read(&pci_bus_sem);
912 list_for_each_entry(dev, &bus->devices, bus_list)
913 if (PCI_SLOT(dev->devfn) == slot->device) {
914 ret = pci_dev_get(dev);
917 up_read(&pci_bus_sem);
923 * disable_device - disable a slot
924 * @slot: ACPI PHP slot
926 static int disable_device(struct acpiphp_slot *slot)
928 struct acpiphp_func *func;
929 struct pci_dev *pdev;
930 struct pci_bus *bus = slot->bridge->pci_bus;
932 /* The slot will be enabled when func 0 is added, so check
933 func 0 before disable the slot. */
934 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
939 list_for_each_entry(func, &slot->funcs, sibling) {
941 /* cleanup p2p bridges under this P2P bridge */
942 cleanup_p2p_bridge(func->bridge->handle,
949 * enable_device() enumerates all functions in this device via
950 * pci_scan_slot(), whether they have associated ACPI hotplug
951 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
954 while ((pdev = dev_in_slot(slot))) {
955 pci_stop_bus_device(pdev);
956 if (pdev->subordinate) {
957 disable_bridges(pdev->subordinate);
958 pci_disable_device(pdev);
960 __pci_remove_bus_device(pdev);
964 list_for_each_entry(func, &slot->funcs, sibling) {
965 acpiphp_bus_trim(func->handle);
968 slot->flags &= (~SLOT_ENABLED);
976 * get_slot_status - get ACPI slot status
977 * @slot: ACPI PHP slot
979 * If a slot has _STA for each function and if any one of them
980 * returned non-zero status, return it.
982 * If a slot doesn't have _STA and if any one of its functions'
983 * configuration space is configured, return 0x0f as a _STA.
985 * Otherwise return 0.
987 static unsigned int get_slot_status(struct acpiphp_slot *slot)
990 unsigned long long sta = 0;
992 struct acpiphp_func *func;
994 list_for_each_entry(func, &slot->funcs, sibling) {
995 if (func->flags & FUNC_HAS_STA) {
996 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
997 if (ACPI_SUCCESS(status) && sta)
1000 pci_bus_read_config_dword(slot->bridge->pci_bus,
1001 PCI_DEVFN(slot->device,
1003 PCI_VENDOR_ID, &dvid);
1004 if (dvid != 0xffffffff) {
1011 return (unsigned int)sta;
1015 * acpiphp_eject_slot - physically eject the slot
1016 * @slot: ACPI PHP slot
1018 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1021 struct acpiphp_func *func;
1022 struct acpi_object_list arg_list;
1023 union acpi_object arg;
1025 list_for_each_entry(func, &slot->funcs, sibling) {
1026 /* We don't want to call _EJ0 on non-existing functions. */
1027 if ((func->flags & FUNC_HAS_EJ0)) {
1028 /* _EJ0 method take one argument */
1030 arg_list.pointer = &arg;
1031 arg.type = ACPI_TYPE_INTEGER;
1032 arg.integer.value = 1;
1034 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1035 if (ACPI_FAILURE(status)) {
1036 warn("%s: _EJ0 failed\n", __func__);
1046 * acpiphp_check_bridge - re-enumerate devices
1047 * @bridge: where to begin re-enumeration
1049 * Iterate over all slots under this bridge and make sure that if a
1050 * card is present they are enabled, and if not they are disabled.
1052 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1054 struct acpiphp_slot *slot;
1056 int enabled, disabled;
1058 enabled = disabled = 0;
1060 for (slot = bridge->slots; slot; slot = slot->next) {
1061 unsigned int status = get_slot_status(slot);
1062 if (slot->flags & SLOT_ENABLED) {
1063 if (status == ACPI_STA_ALL)
1065 retval = acpiphp_disable_slot(slot);
1067 err("Error occurred in disabling\n");
1070 acpiphp_eject_slot(slot);
1074 if (status != ACPI_STA_ALL)
1076 retval = acpiphp_enable_slot(slot);
1078 err("Error occurred in enabling\n");
1085 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1091 static void acpiphp_set_hpp_values(struct pci_bus *bus)
1093 struct pci_dev *dev;
1095 list_for_each_entry(dev, &bus->devices, bus_list)
1096 pci_configure_slot(dev);
1100 * Remove devices for which we could not assign resources, call
1101 * arch specific code to fix-up the bus
1103 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1105 struct pci_dev *dev;
1107 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1109 list_for_each_entry(dev, &bus->devices, bus_list) {
1110 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1111 struct resource *res = &dev->resource[i];
1112 if ((res->flags & type_mask) && !res->start &&
1114 /* Could not assign a required resources
1115 * for this device, remove it */
1116 pci_stop_and_remove_bus_device(dev);
1123 /* Program resources in newly inserted bridge */
1124 static int acpiphp_configure_bridge (acpi_handle handle)
1126 struct pci_bus *bus;
1128 if (acpi_is_root_bridge(handle)) {
1129 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1132 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1133 bus = pdev->subordinate;
1137 pci_bus_size_bridges(bus);
1138 pci_bus_assign_resources(bus);
1139 acpiphp_sanitize_bus(bus);
1140 acpiphp_set_hpp_values(bus);
1141 pci_enable_bridges(bus);
1145 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1147 struct acpi_device *device, *pdevice;
1148 acpi_handle phandle;
1150 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1151 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1152 err("unexpected notification type %d\n", type);
1156 acpi_get_parent(handle, &phandle);
1157 if (acpi_bus_get_device(phandle, &pdevice)) {
1158 dbg("no parent device, assuming NULL\n");
1161 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1162 err("cannot add bridge to acpi list\n");
1165 if (!acpiphp_configure_bridge(handle) &&
1166 !acpi_bus_start(device))
1169 err("cannot configure and start bridge\n");
1174 * ACPI event handlers
1178 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1180 int *count = (int *)context;
1181 struct acpiphp_bridge *bridge;
1183 bridge = acpiphp_handle_to_bridge(handle);
1190 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1192 struct acpiphp_bridge *bridge;
1194 struct acpi_buffer buffer = { .length = sizeof(objname),
1195 .pointer = objname };
1197 bridge = acpiphp_handle_to_bridge(handle);
1199 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1200 dbg("%s: re-enumerating slots under %s\n",
1202 acpiphp_check_bridge(bridge);
1207 struct acpiphp_hp_work {
1208 struct work_struct work;
1214 static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type,
1216 void (*func)(struct work_struct *work))
1218 struct acpiphp_hp_work *hp_work;
1221 hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL);
1225 hp_work->handle = handle;
1226 hp_work->type = type;
1227 hp_work->context = context;
1229 INIT_WORK(&hp_work->work, func);
1230 ret = queue_work(kacpi_hotplug_wq, &hp_work->work);
1235 static void _handle_hotplug_event_bridge(struct work_struct *work)
1237 struct acpiphp_bridge *bridge;
1239 struct acpi_buffer buffer = { .length = sizeof(objname),
1240 .pointer = objname };
1241 struct acpi_device *device;
1242 int num_sub_bridges = 0;
1243 struct acpiphp_hp_work *hp_work;
1247 hp_work = container_of(work, struct acpiphp_hp_work, work);
1248 handle = hp_work->handle;
1249 type = hp_work->type;
1251 if (acpi_bus_get_device(handle, &device)) {
1252 /* This bridge must have just been physically inserted */
1253 handle_bridge_insertion(handle, type);
1257 bridge = acpiphp_handle_to_bridge(handle);
1258 if (type == ACPI_NOTIFY_BUS_CHECK) {
1259 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1260 count_sub_bridges, NULL, &num_sub_bridges, NULL);
1263 if (!bridge && !num_sub_bridges) {
1264 err("cannot get bridge info\n");
1268 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1271 case ACPI_NOTIFY_BUS_CHECK:
1272 /* bus re-enumerate */
1273 dbg("%s: Bus check notify on %s\n", __func__, objname);
1275 dbg("%s: re-enumerating slots under %s\n",
1277 acpiphp_check_bridge(bridge);
1279 if (num_sub_bridges)
1280 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1281 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1284 case ACPI_NOTIFY_DEVICE_CHECK:
1286 dbg("%s: Device check notify on %s\n", __func__, objname);
1287 acpiphp_check_bridge(bridge);
1290 case ACPI_NOTIFY_DEVICE_WAKE:
1292 dbg("%s: Device wake notify on %s\n", __func__, objname);
1295 case ACPI_NOTIFY_EJECT_REQUEST:
1296 /* request device eject */
1297 dbg("%s: Device eject notify on %s\n", __func__, objname);
1298 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1299 (bridge->flags & BRIDGE_HAS_EJ0)) {
1300 struct acpiphp_slot *slot;
1301 slot = bridge->func->slot;
1302 if (!acpiphp_disable_slot(slot))
1303 acpiphp_eject_slot(slot);
1307 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1308 printk(KERN_ERR "Device %s cannot be configured due"
1309 " to a frequency mismatch\n", objname);
1312 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1313 printk(KERN_ERR "Device %s cannot be configured due"
1314 " to a bus mode mismatch\n", objname);
1317 case ACPI_NOTIFY_POWER_FAULT:
1318 printk(KERN_ERR "Device %s has suffered a power fault\n",
1323 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1328 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1332 * handle_hotplug_event_bridge - handle ACPI event on bridges
1333 * @handle: Notify()'ed acpi_handle
1334 * @type: Notify code
1335 * @context: pointer to acpiphp_bridge structure
1337 * Handles ACPI event notification on {host,p2p} bridges.
1339 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1343 * Currently the code adds all hotplug events to the kacpid_wq
1344 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1345 * The proper way to fix this is to reorganize the code so that
1346 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1347 * For now just re-add this work to the kacpi_hotplug_wq so we
1348 * don't deadlock on hotplug actions.
1350 alloc_acpiphp_hp_work(handle, type, context,
1351 _handle_hotplug_event_bridge);
1354 static void _handle_hotplug_event_func(struct work_struct *work)
1356 struct acpiphp_func *func;
1358 struct acpi_buffer buffer = { .length = sizeof(objname),
1359 .pointer = objname };
1360 struct acpiphp_hp_work *hp_work;
1365 hp_work = container_of(work, struct acpiphp_hp_work, work);
1366 handle = hp_work->handle;
1367 type = hp_work->type;
1368 context = hp_work->context;
1370 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1372 func = (struct acpiphp_func *)context;
1375 case ACPI_NOTIFY_BUS_CHECK:
1376 /* bus re-enumerate */
1377 dbg("%s: Bus check notify on %s\n", __func__, objname);
1378 acpiphp_enable_slot(func->slot);
1381 case ACPI_NOTIFY_DEVICE_CHECK:
1382 /* device check : re-enumerate from parent bus */
1383 dbg("%s: Device check notify on %s\n", __func__, objname);
1384 acpiphp_check_bridge(func->slot->bridge);
1387 case ACPI_NOTIFY_DEVICE_WAKE:
1389 dbg("%s: Device wake notify on %s\n", __func__, objname);
1392 case ACPI_NOTIFY_EJECT_REQUEST:
1393 /* request device eject */
1394 dbg("%s: Device eject notify on %s\n", __func__, objname);
1395 if (!(acpiphp_disable_slot(func->slot)))
1396 acpiphp_eject_slot(func->slot);
1400 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1404 kfree(hp_work); /* allocated in handle_hotplug_event_func */
1408 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1409 * @handle: Notify()'ed acpi_handle
1410 * @type: Notify code
1411 * @context: pointer to acpiphp_func structure
1413 * Handles ACPI event notification on slots.
1415 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1419 * Currently the code adds all hotplug events to the kacpid_wq
1420 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1421 * The proper way to fix this is to reorganize the code so that
1422 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1423 * For now just re-add this work to the kacpi_hotplug_wq so we
1424 * don't deadlock on hotplug actions.
1426 alloc_acpiphp_hp_work(handle, type, context,
1427 _handle_hotplug_event_func);
1431 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1433 int *count = (int *)context;
1435 if (!acpi_is_root_bridge(handle))
1439 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1440 handle_hotplug_event_bridge, NULL);
1445 static struct acpi_pci_driver acpi_pci_hp_driver = {
1447 .remove = remove_bridge,
1451 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1453 int __init acpiphp_glue_init(void)
1457 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1458 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
1463 acpi_pci_register_driver(&acpi_pci_hp_driver);
1470 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1472 * This function frees all data allocated in acpiphp_glue_init().
1474 void acpiphp_glue_exit(void)
1476 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1481 * acpiphp_get_num_slots - count number of slots in a system
1483 int __init acpiphp_get_num_slots(void)
1485 struct acpiphp_bridge *bridge;
1488 list_for_each_entry(bridge, &bridge_list, list) {
1489 dbg("Bus %04x:%02x has %d slot%s\n",
1490 pci_domain_nr(bridge->pci_bus),
1491 bridge->pci_bus->number, bridge->nr_slots,
1492 bridge->nr_slots == 1 ? "" : "s");
1493 num_slots += bridge->nr_slots;
1496 dbg("Total %d slots\n", num_slots);
1503 * acpiphp_for_each_slot - call function for each slot
1504 * @fn: callback function
1505 * @data: context to be passed to callback function
1507 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1509 struct list_head *node;
1510 struct acpiphp_bridge *bridge;
1511 struct acpiphp_slot *slot;
1514 list_for_each (node, &bridge_list) {
1515 bridge = (struct acpiphp_bridge *)node;
1516 for (slot = bridge->slots; slot; slot = slot->next) {
1517 retval = fn(slot, data);
1530 * acpiphp_enable_slot - power on slot
1531 * @slot: ACPI PHP slot
1533 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1537 mutex_lock(&slot->crit_sect);
1539 /* wake up all functions */
1540 retval = power_on_slot(slot);
1544 if (get_slot_status(slot) == ACPI_STA_ALL) {
1545 /* configure all functions */
1546 retval = enable_device(slot);
1548 power_off_slot(slot);
1550 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1551 power_off_slot(slot);
1555 mutex_unlock(&slot->crit_sect);
1560 * acpiphp_disable_slot - power off slot
1561 * @slot: ACPI PHP slot
1563 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1567 mutex_lock(&slot->crit_sect);
1569 /* unconfigure all functions */
1570 retval = disable_device(slot);
1574 /* power off all functions */
1575 retval = power_off_slot(slot);
1580 mutex_unlock(&slot->crit_sect);
1589 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1591 return (slot->flags & SLOT_POWEREDON);
1599 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1603 sta = get_slot_status(slot);
1605 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1610 * adapter presence : 1
1613 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1617 sta = get_slot_status(slot);
1619 return (sta == 0) ? 0 : 1;