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);
57 static DEFINE_MUTEX(bridge_mutex);
59 #define MY_NAME "acpiphp_glue"
61 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
62 static void acpiphp_sanitize_bus(struct pci_bus *bus);
63 static void acpiphp_set_hpp_values(struct pci_bus *bus);
64 static void hotplug_event_func(acpi_handle handle, u32 type, void *context);
65 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
66 static void free_bridge(struct kref *kref);
68 /* callback routine to check for the existence of a pci dock device */
70 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
72 int *count = (int *)context;
74 if (is_dock_device(handle)) {
76 return AE_CTRL_TERMINATE;
82 static inline void get_bridge(struct acpiphp_bridge *bridge)
84 kref_get(&bridge->ref);
87 static inline void put_bridge(struct acpiphp_bridge *bridge)
89 kref_put(&bridge->ref, free_bridge);
92 static void free_bridge(struct kref *kref)
94 struct acpiphp_bridge *bridge;
95 struct acpiphp_slot *slot, *next;
96 struct acpiphp_func *func, *tmp;
98 bridge = container_of(kref, struct acpiphp_bridge, ref);
100 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
101 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
107 /* Release reference acquired by acpiphp_bridge_handle_to_function() */
108 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
109 put_bridge(bridge->func->slot->bridge);
110 put_device(&bridge->pci_bus->dev);
111 pci_dev_put(bridge->pci_dev);
116 * the _DCK method can do funny things... and sometimes not
119 * TBD - figure out a way to only call fixups for
120 * systems that require them.
122 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
125 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
126 struct pci_bus *bus = func->slot->bridge->pci_bus;
132 /* fixup bad _DCK function that rewrites
133 * secondary bridge on slot
135 pci_read_config_dword(bus->self,
139 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
140 buses = (buses & 0xff000000)
141 | ((unsigned int)(bus->primary) << 0)
142 | ((unsigned int)(bus->busn_res.start) << 8)
143 | ((unsigned int)(bus->busn_res.end) << 16);
144 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
150 static const struct acpi_dock_ops acpiphp_dock_ops = {
151 .handler = hotplug_event_func,
154 /* Check whether the PCI device is managed by native PCIe hotplug driver */
155 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
159 struct acpi_pci_root *root;
161 /* Check whether the PCIe port supports native PCIe hotplug */
162 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32))
164 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
168 * Check whether native PCIe hotplug has been enabled for
169 * this PCIe hierarchy.
171 tmp = acpi_find_root_bridge_handle(pdev);
174 root = acpi_pci_find_root(tmp);
177 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
183 static void acpiphp_dock_init(void *data)
185 struct acpiphp_func *func = data;
187 get_bridge(func->slot->bridge);
190 static void acpiphp_dock_release(void *data)
192 struct acpiphp_func *func = data;
194 put_bridge(func->slot->bridge);
197 /* callback routine to register each ACPI PCI slot object */
199 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
201 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
202 struct acpiphp_slot *slot;
203 struct acpiphp_func *newfunc;
205 acpi_status status = AE_OK;
206 unsigned long long adr, sun;
207 int device, function, retval, found = 0;
208 struct pci_bus *pbus = bridge->pci_bus;
209 struct pci_dev *pdev;
212 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
215 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
216 if (ACPI_FAILURE(status)) {
217 warn("can't evaluate _ADR (%#x)\n", status);
221 device = (adr >> 16) & 0xffff;
222 function = adr & 0xffff;
224 pdev = bridge->pci_dev;
225 if (pdev && device_is_managed_by_native_pciehp(pdev))
228 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
232 newfunc->handle = handle;
233 newfunc->function = function;
235 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
236 newfunc->flags = FUNC_HAS_EJ0;
238 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
239 newfunc->flags |= FUNC_HAS_STA;
241 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
242 newfunc->flags |= FUNC_HAS_PS0;
244 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
245 newfunc->flags |= FUNC_HAS_PS3;
247 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
248 newfunc->flags |= FUNC_HAS_DCK;
250 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
251 if (ACPI_FAILURE(status)) {
253 * use the count of the number of slots we've found
254 * for the number of the slot
256 sun = bridge->nr_slots+1;
259 /* search for objects that share the same slot */
260 list_for_each_entry(slot, &bridge->slots, node)
261 if (slot->device == device) {
262 if (slot->sun != sun)
263 warn("sibling found, but _SUN doesn't match!\n");
269 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
275 slot->bridge = bridge;
276 slot->device = device;
278 INIT_LIST_HEAD(&slot->funcs);
279 mutex_init(&slot->crit_sect);
281 mutex_lock(&bridge_mutex);
282 list_add_tail(&slot->node, &bridge->slots);
283 mutex_unlock(&bridge_mutex);
286 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
287 slot->sun, pci_domain_nr(pbus), pbus->number, device);
288 retval = acpiphp_register_hotplug_slot(slot);
290 if (retval == -EBUSY)
291 warn("Slot %llu already registered by another "
292 "hotplug driver\n", slot->sun);
294 warn("acpiphp_register_hotplug_slot failed "
295 "(err code = 0x%x)\n", retval);
300 newfunc->slot = slot;
301 mutex_lock(&bridge_mutex);
302 list_add_tail(&newfunc->sibling, &slot->funcs);
303 mutex_unlock(&bridge_mutex);
305 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
307 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
309 if (is_dock_device(handle)) {
310 /* we don't want to call this device's _EJ0
311 * because we want the dock notify handler
312 * to call it after it calls _DCK
314 newfunc->flags &= ~FUNC_HAS_EJ0;
315 if (register_hotplug_dock_device(handle,
316 &acpiphp_dock_ops, newfunc,
317 acpiphp_dock_init, acpiphp_dock_release))
318 dbg("failed to register dock device\n");
320 /* we need to be notified when dock events happen
321 * outside of the hotplug operation, since we may
322 * need to do fixups before we can hotplug.
324 newfunc->nb.notifier_call = post_dock_fixups;
325 if (register_dock_notifier(&newfunc->nb))
326 dbg("failed to register a dock notifier");
329 /* install notify handler */
330 if (!(newfunc->flags & FUNC_HAS_DCK)) {
331 status = acpi_install_notify_handler(handle,
333 handle_hotplug_event_func,
336 if (ACPI_FAILURE(status))
337 err("failed to register interrupt notify handler\n");
345 mutex_lock(&bridge_mutex);
346 list_del(&slot->node);
347 mutex_unlock(&bridge_mutex);
355 /* see if it's worth looking at this bridge */
356 static int detect_ejectable_slots(acpi_handle handle)
358 int found = acpi_pci_detect_ejectable(handle);
360 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
361 is_pci_dock_device, NULL, (void *)&found, NULL);
366 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
367 static void init_bridge_misc(struct acpiphp_bridge *bridge)
371 /* must be added to the list prior to calling register_slot */
372 mutex_lock(&bridge_mutex);
373 list_add(&bridge->list, &bridge_list);
374 mutex_unlock(&bridge_mutex);
376 /* register all slot objects under this bridge */
377 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
378 register_slot, NULL, bridge, NULL);
379 if (ACPI_FAILURE(status)) {
380 mutex_lock(&bridge_mutex);
381 list_del(&bridge->list);
382 mutex_unlock(&bridge_mutex);
386 /* install notify handler for P2P bridges */
387 if (!pci_is_root_bus(bridge->pci_bus)) {
388 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
389 status = acpi_remove_notify_handler(bridge->func->handle,
391 handle_hotplug_event_func);
392 if (ACPI_FAILURE(status))
393 err("failed to remove notify handler\n");
395 status = acpi_install_notify_handler(bridge->handle,
397 handle_hotplug_event_bridge,
400 if (ACPI_FAILURE(status)) {
401 err("failed to register interrupt notify handler\n");
407 /* find acpiphp_func from acpiphp_bridge */
408 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
410 struct acpiphp_bridge *bridge;
411 struct acpiphp_slot *slot;
412 struct acpiphp_func *func = NULL;
414 mutex_lock(&bridge_mutex);
415 list_for_each_entry(bridge, &bridge_list, list) {
416 list_for_each_entry(slot, &bridge->slots, node) {
417 list_for_each_entry(func, &slot->funcs, sibling) {
418 if (func->handle == handle) {
419 get_bridge(func->slot->bridge);
420 mutex_unlock(&bridge_mutex);
426 mutex_unlock(&bridge_mutex);
432 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
434 struct acpiphp_bridge *bridge;
436 mutex_lock(&bridge_mutex);
437 list_for_each_entry(bridge, &bridge_list, list)
438 if (bridge->handle == handle) {
440 mutex_unlock(&bridge_mutex);
443 mutex_unlock(&bridge_mutex);
448 static void cleanup_bridge(struct acpiphp_bridge *bridge)
450 struct acpiphp_slot *slot;
451 struct acpiphp_func *func;
453 acpi_handle handle = bridge->handle;
455 if (!pci_is_root_bus(bridge->pci_bus)) {
456 status = acpi_remove_notify_handler(handle,
458 handle_hotplug_event_bridge);
459 if (ACPI_FAILURE(status))
460 err("failed to remove notify handler\n");
463 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
464 status = acpi_install_notify_handler(bridge->func->handle,
466 handle_hotplug_event_func,
468 if (ACPI_FAILURE(status))
469 err("failed to install interrupt notify handler\n");
472 list_for_each_entry(slot, &bridge->slots, node) {
473 list_for_each_entry(func, &slot->funcs, sibling) {
474 if (is_dock_device(func->handle)) {
475 unregister_hotplug_dock_device(func->handle);
476 unregister_dock_notifier(&func->nb);
478 if (!(func->flags & FUNC_HAS_DCK)) {
479 status = acpi_remove_notify_handler(func->handle,
481 handle_hotplug_event_func);
482 if (ACPI_FAILURE(status))
483 err("failed to remove notify handler\n");
486 acpiphp_unregister_hotplug_slot(slot);
489 mutex_lock(&bridge_mutex);
490 list_del(&bridge->list);
491 mutex_unlock(&bridge_mutex);
494 static int power_on_slot(struct acpiphp_slot *slot)
497 struct acpiphp_func *func;
500 /* if already enabled, just skip */
501 if (slot->flags & SLOT_POWEREDON)
504 list_for_each_entry(func, &slot->funcs, sibling) {
505 if (func->flags & FUNC_HAS_PS0) {
506 dbg("%s: executing _PS0\n", __func__);
507 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
508 if (ACPI_FAILURE(status)) {
509 warn("%s: _PS0 failed\n", __func__);
517 /* TBD: evaluate _STA to check if the slot is enabled */
519 slot->flags |= SLOT_POWEREDON;
526 static int power_off_slot(struct acpiphp_slot *slot)
529 struct acpiphp_func *func;
533 /* if already disabled, just skip */
534 if ((slot->flags & SLOT_POWEREDON) == 0)
537 list_for_each_entry(func, &slot->funcs, sibling) {
538 if (func->flags & FUNC_HAS_PS3) {
539 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
540 if (ACPI_FAILURE(status)) {
541 warn("%s: _PS3 failed\n", __func__);
549 /* TBD: evaluate _STA to check if the slot is disabled */
551 slot->flags &= (~SLOT_POWEREDON);
560 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
561 * @bus: bus to start search with
563 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
565 struct list_head *tmp;
566 unsigned char max, n;
569 * pci_bus_max_busnr will return the highest
570 * reserved busnr for all these children.
571 * that is equivalent to the bus->subordinate
572 * value. We don't want to use the parent's
573 * bus->subordinate value because it could have
576 max = bus->busn_res.start;
578 list_for_each(tmp, &bus->children) {
579 n = pci_bus_max_busnr(pci_bus_b(tmp));
588 * acpiphp_bus_add - add a new bus to acpi subsystem
589 * @func: acpiphp_func of the bridge
591 static int acpiphp_bus_add(struct acpiphp_func *func)
593 struct acpi_device *device;
596 if (!acpi_bus_get_device(func->handle, &device)) {
597 dbg("bus exists... trim\n");
598 /* this shouldn't be in here, so remove
599 * the bus then re-add it...
601 acpi_bus_trim(device);
604 ret_val = acpi_bus_scan(func->handle);
606 ret_val = acpi_bus_get_device(func->handle, &device);
609 dbg("error adding bus, %x\n", -ret_val);
616 * acpiphp_bus_trim - trim a bus from acpi subsystem
617 * @handle: handle to acpi namespace
619 static int acpiphp_bus_trim(acpi_handle handle)
621 struct acpi_device *device;
624 retval = acpi_bus_get_device(handle, &device);
626 dbg("acpi_device not found\n");
630 acpi_bus_trim(device);
634 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
636 struct acpiphp_func *func;
637 union acpi_object params[2];
638 struct acpi_object_list arg_list;
640 list_for_each_entry(func, &slot->funcs, sibling) {
642 arg_list.pointer = params;
643 params[0].type = ACPI_TYPE_INTEGER;
644 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
645 params[1].type = ACPI_TYPE_INTEGER;
646 params[1].integer.value = 1;
647 /* _REG is optional, we don't care about if there is failure */
648 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
652 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
654 struct acpiphp_func *func;
656 if (!dev->subordinate)
659 /* quirk, or pcie could set it already */
660 if (dev->is_hotplug_bridge)
663 if (PCI_SLOT(dev->devfn) != slot->device)
666 list_for_each_entry(func, &slot->funcs, sibling) {
667 if (PCI_FUNC(dev->devfn) == func->function) {
668 /* check if this bridge has ejectable slots */
669 if ((detect_ejectable_slots(func->handle) > 0))
670 dev->is_hotplug_bridge = 1;
677 * enable_device - enable, configure a slot
678 * @slot: slot to be enabled
680 * This function should be called per *physical slot*,
681 * not per each slot object in ACPI namespace.
683 static int __ref enable_device(struct acpiphp_slot *slot)
686 struct pci_bus *bus = slot->bridge->pci_bus;
687 struct acpiphp_func *func;
691 if (slot->flags & SLOT_ENABLED)
694 list_for_each_entry(func, &slot->funcs, sibling)
695 acpiphp_bus_add(func);
697 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
699 /* Maybe only part of funcs are added. */
700 dbg("No new device found\n");
704 max = acpiphp_max_busnr(bus);
705 for (pass = 0; pass < 2; pass++) {
706 list_for_each_entry(dev, &bus->devices, bus_list) {
707 if (PCI_SLOT(dev->devfn) != slot->device)
709 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
710 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
711 max = pci_scan_bridge(bus, dev, max, pass);
712 if (pass && dev->subordinate) {
713 check_hotplug_bridge(slot, dev);
714 pcibios_resource_survey_bus(dev->subordinate);
715 __pci_bus_size_bridges(dev->subordinate,
722 __pci_bus_assign_resources(bus, &add_list, NULL);
723 acpiphp_sanitize_bus(bus);
724 acpiphp_set_hpp_values(bus);
725 acpiphp_set_acpi_region(slot);
726 pci_enable_bridges(bus);
728 list_for_each_entry(dev, &bus->devices, bus_list) {
729 /* Assume that newly added devices are powered on already. */
731 dev->current_state = PCI_D0;
734 pci_bus_add_devices(bus);
736 slot->flags |= SLOT_ENABLED;
737 list_for_each_entry(func, &slot->funcs, sibling) {
738 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
741 /* Do not set SLOT_ENABLED flag if some funcs
743 slot->flags &= (~SLOT_ENABLED);
753 /* return first device in slot, acquiring a reference on it */
754 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
756 struct pci_bus *bus = slot->bridge->pci_bus;
758 struct pci_dev *ret = NULL;
760 down_read(&pci_bus_sem);
761 list_for_each_entry(dev, &bus->devices, bus_list)
762 if (PCI_SLOT(dev->devfn) == slot->device) {
763 ret = pci_dev_get(dev);
766 up_read(&pci_bus_sem);
772 * disable_device - disable a slot
773 * @slot: ACPI PHP slot
775 static int disable_device(struct acpiphp_slot *slot)
777 struct acpiphp_func *func;
778 struct pci_dev *pdev;
781 * enable_device() enumerates all functions in this device via
782 * pci_scan_slot(), whether they have associated ACPI hotplug
783 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
786 while ((pdev = dev_in_slot(slot))) {
787 pci_stop_and_remove_bus_device(pdev);
791 list_for_each_entry(func, &slot->funcs, sibling) {
792 acpiphp_bus_trim(func->handle);
795 slot->flags &= (~SLOT_ENABLED);
802 * get_slot_status - get ACPI slot status
803 * @slot: ACPI PHP slot
805 * If a slot has _STA for each function and if any one of them
806 * returned non-zero status, return it.
808 * If a slot doesn't have _STA and if any one of its functions'
809 * configuration space is configured, return 0x0f as a _STA.
811 * Otherwise return 0.
813 static unsigned int get_slot_status(struct acpiphp_slot *slot)
816 unsigned long long sta = 0;
818 struct acpiphp_func *func;
820 list_for_each_entry(func, &slot->funcs, sibling) {
821 if (func->flags & FUNC_HAS_STA) {
822 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
823 if (ACPI_SUCCESS(status) && sta)
826 pci_bus_read_config_dword(slot->bridge->pci_bus,
827 PCI_DEVFN(slot->device,
829 PCI_VENDOR_ID, &dvid);
830 if (dvid != 0xffffffff) {
837 return (unsigned int)sta;
841 * acpiphp_eject_slot - physically eject the slot
842 * @slot: ACPI PHP slot
844 int acpiphp_eject_slot(struct acpiphp_slot *slot)
847 struct acpiphp_func *func;
848 struct acpi_object_list arg_list;
849 union acpi_object arg;
851 list_for_each_entry(func, &slot->funcs, sibling) {
852 /* We don't want to call _EJ0 on non-existing functions. */
853 if ((func->flags & FUNC_HAS_EJ0)) {
854 /* _EJ0 method take one argument */
856 arg_list.pointer = &arg;
857 arg.type = ACPI_TYPE_INTEGER;
858 arg.integer.value = 1;
860 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
861 if (ACPI_FAILURE(status)) {
862 warn("%s: _EJ0 failed\n", __func__);
872 * acpiphp_check_bridge - re-enumerate devices
873 * @bridge: where to begin re-enumeration
875 * Iterate over all slots under this bridge and make sure that if a
876 * card is present they are enabled, and if not they are disabled.
878 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
880 struct acpiphp_slot *slot;
882 int enabled, disabled;
884 enabled = disabled = 0;
886 list_for_each_entry(slot, &bridge->slots, node) {
887 unsigned int status = get_slot_status(slot);
888 if (slot->flags & SLOT_ENABLED) {
889 if (status == ACPI_STA_ALL)
891 retval = acpiphp_disable_slot(slot);
893 err("Error occurred in disabling\n");
896 acpiphp_eject_slot(slot);
900 if (status != ACPI_STA_ALL)
902 retval = acpiphp_enable_slot(slot);
904 err("Error occurred in enabling\n");
911 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
917 static void acpiphp_set_hpp_values(struct pci_bus *bus)
921 list_for_each_entry(dev, &bus->devices, bus_list)
922 pci_configure_slot(dev);
926 * Remove devices for which we could not assign resources, call
927 * arch specific code to fix-up the bus
929 static void acpiphp_sanitize_bus(struct pci_bus *bus)
931 struct pci_dev *dev, *tmp;
933 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
935 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
936 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
937 struct resource *res = &dev->resource[i];
938 if ((res->flags & type_mask) && !res->start &&
940 /* Could not assign a required resources
941 * for this device, remove it */
942 pci_stop_and_remove_bus_device(dev);
950 * ACPI event handlers
954 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
956 struct acpiphp_bridge *bridge;
958 struct acpi_buffer buffer = { .length = sizeof(objname),
959 .pointer = objname };
961 bridge = acpiphp_handle_to_bridge(handle);
963 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
964 dbg("%s: re-enumerating slots under %s\n",
966 acpiphp_check_bridge(bridge);
972 void acpiphp_check_host_bridge(acpi_handle handle)
974 struct acpiphp_bridge *bridge;
976 bridge = acpiphp_handle_to_bridge(handle);
978 acpiphp_check_bridge(bridge);
982 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
983 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
986 static void _handle_hotplug_event_bridge(struct work_struct *work)
988 struct acpiphp_bridge *bridge;
990 struct acpi_buffer buffer = { .length = sizeof(objname),
991 .pointer = objname };
992 struct acpi_hp_work *hp_work;
996 hp_work = container_of(work, struct acpi_hp_work, work);
997 handle = hp_work->handle;
998 type = hp_work->type;
999 bridge = (struct acpiphp_bridge *)hp_work->context;
1001 acpi_scan_lock_acquire();
1003 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1006 case ACPI_NOTIFY_BUS_CHECK:
1007 /* bus re-enumerate */
1008 dbg("%s: Bus check notify on %s\n", __func__, objname);
1009 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1010 acpiphp_check_bridge(bridge);
1011 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1012 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1015 case ACPI_NOTIFY_DEVICE_CHECK:
1017 dbg("%s: Device check notify on %s\n", __func__, objname);
1018 acpiphp_check_bridge(bridge);
1021 case ACPI_NOTIFY_DEVICE_WAKE:
1023 dbg("%s: Device wake notify on %s\n", __func__, objname);
1026 case ACPI_NOTIFY_EJECT_REQUEST:
1027 /* request device eject */
1028 dbg("%s: Device eject notify on %s\n", __func__, objname);
1029 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
1030 struct acpiphp_slot *slot;
1031 slot = bridge->func->slot;
1032 if (!acpiphp_disable_slot(slot))
1033 acpiphp_eject_slot(slot);
1037 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1038 printk(KERN_ERR "Device %s cannot be configured due"
1039 " to a frequency mismatch\n", objname);
1042 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1043 printk(KERN_ERR "Device %s cannot be configured due"
1044 " to a bus mode mismatch\n", objname);
1047 case ACPI_NOTIFY_POWER_FAULT:
1048 printk(KERN_ERR "Device %s has suffered a power fault\n",
1053 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1057 acpi_scan_lock_release();
1058 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1063 * handle_hotplug_event_bridge - handle ACPI event on bridges
1064 * @handle: Notify()'ed acpi_handle
1065 * @type: Notify code
1066 * @context: pointer to acpiphp_bridge structure
1068 * Handles ACPI event notification on {host,p2p} bridges.
1070 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1073 struct acpiphp_bridge *bridge = context;
1076 * Currently the code adds all hotplug events to the kacpid_wq
1077 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1078 * The proper way to fix this is to reorganize the code so that
1079 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1080 * For now just re-add this work to the kacpi_hotplug_wq so we
1081 * don't deadlock on hotplug actions.
1084 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
1087 static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
1089 struct acpiphp_func *func = context;
1091 struct acpi_buffer buffer = { .length = sizeof(objname),
1092 .pointer = objname };
1094 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1097 case ACPI_NOTIFY_BUS_CHECK:
1098 /* bus re-enumerate */
1099 dbg("%s: Bus check notify on %s\n", __func__, objname);
1100 acpiphp_enable_slot(func->slot);
1103 case ACPI_NOTIFY_DEVICE_CHECK:
1104 /* device check : re-enumerate from parent bus */
1105 dbg("%s: Device check notify on %s\n", __func__, objname);
1106 acpiphp_check_bridge(func->slot->bridge);
1109 case ACPI_NOTIFY_DEVICE_WAKE:
1111 dbg("%s: Device wake notify on %s\n", __func__, objname);
1114 case ACPI_NOTIFY_EJECT_REQUEST:
1115 /* request device eject */
1116 dbg("%s: Device eject notify on %s\n", __func__, objname);
1117 if (!(acpiphp_disable_slot(func->slot)))
1118 acpiphp_eject_slot(func->slot);
1122 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1127 static void _handle_hotplug_event_func(struct work_struct *work)
1129 struct acpi_hp_work *hp_work;
1130 struct acpiphp_func *func;
1132 hp_work = container_of(work, struct acpi_hp_work, work);
1133 func = hp_work->context;
1134 acpi_scan_lock_acquire();
1136 hotplug_event_func(hp_work->handle, hp_work->type, func);
1138 acpi_scan_lock_release();
1139 kfree(hp_work); /* allocated in handle_hotplug_event_func */
1140 put_bridge(func->slot->bridge);
1144 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1145 * @handle: Notify()'ed acpi_handle
1146 * @type: Notify code
1147 * @context: pointer to acpiphp_func structure
1149 * Handles ACPI event notification on slots.
1151 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1154 struct acpiphp_func *func = context;
1157 * Currently the code adds all hotplug events to the kacpid_wq
1158 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1159 * The proper way to fix this is to reorganize the code so that
1160 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1161 * For now just re-add this work to the kacpi_hotplug_wq so we
1162 * don't deadlock on hotplug actions.
1164 get_bridge(func->slot->bridge);
1165 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
1169 * Create hotplug slots for the PCI bus.
1170 * It should always return 0 to avoid skipping following notifiers.
1172 void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
1174 acpi_handle dummy_handle;
1175 struct acpiphp_bridge *bridge;
1177 if (acpiphp_disabled)
1180 if (detect_ejectable_slots(handle) <= 0)
1183 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1184 if (bridge == NULL) {
1185 err("out of memory\n");
1189 INIT_LIST_HEAD(&bridge->slots);
1190 kref_init(&bridge->ref);
1191 bridge->handle = handle;
1192 bridge->pci_dev = pci_dev_get(bus->self);
1193 bridge->pci_bus = bus;
1196 * Grab a ref to the subordinate PCI bus in case the bus is
1197 * removed via PCI core logical hotplug. The ref pins the bus
1198 * (which we access during module unload).
1200 get_device(&bus->dev);
1202 if (!pci_is_root_bus(bridge->pci_bus) &&
1203 ACPI_SUCCESS(acpi_get_handle(bridge->handle,
1204 "_EJ0", &dummy_handle))) {
1205 dbg("found ejectable p2p bridge\n");
1206 bridge->flags |= BRIDGE_HAS_EJ0;
1207 bridge->func = acpiphp_bridge_handle_to_function(handle);
1210 init_bridge_misc(bridge);
1213 /* Destroy hotplug slots associated with the PCI bus */
1214 void acpiphp_remove_slots(struct pci_bus *bus)
1216 struct acpiphp_bridge *bridge, *tmp;
1218 if (acpiphp_disabled)
1221 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1222 if (bridge->pci_bus == bus) {
1223 cleanup_bridge(bridge);
1230 * acpiphp_enable_slot - power on slot
1231 * @slot: ACPI PHP slot
1233 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1237 mutex_lock(&slot->crit_sect);
1239 /* wake up all functions */
1240 retval = power_on_slot(slot);
1244 if (get_slot_status(slot) == ACPI_STA_ALL) {
1245 /* configure all functions */
1246 retval = enable_device(slot);
1248 power_off_slot(slot);
1250 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1251 power_off_slot(slot);
1255 mutex_unlock(&slot->crit_sect);
1260 * acpiphp_disable_slot - power off slot
1261 * @slot: ACPI PHP slot
1263 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1267 mutex_lock(&slot->crit_sect);
1269 /* unconfigure all functions */
1270 retval = disable_device(slot);
1274 /* power off all functions */
1275 retval = power_off_slot(slot);
1280 mutex_unlock(&slot->crit_sect);
1289 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1291 return (slot->flags & SLOT_POWEREDON);
1299 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1303 sta = get_slot_status(slot);
1305 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1310 * adapter presence : 1
1313 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1317 sta = get_slot_status(slot);
1319 return (sta == 0) ? 0 : 1;