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);
58 static DEFINE_MUTEX(acpiphp_context_lock);
60 #define MY_NAME "acpiphp_glue"
62 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
63 static void acpiphp_sanitize_bus(struct pci_bus *bus);
64 static void acpiphp_set_hpp_values(struct pci_bus *bus);
65 static void 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 void acpiphp_context_handler(acpi_handle handle, void *context)
84 /* Intentionally empty. */
88 * acpiphp_init_context - Create hotplug context and grab a reference to it.
89 * @handle: ACPI object handle to create the context for.
91 * Call under acpiphp_context_lock.
93 static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
95 struct acpiphp_context *context;
98 context = kzalloc(sizeof(*context), GFP_KERNEL);
102 context->handle = handle;
103 context->refcount = 1;
104 status = acpi_attach_data(handle, acpiphp_context_handler, context);
105 if (ACPI_FAILURE(status)) {
113 * acpiphp_get_context - Get hotplug context and grab a reference to it.
114 * @handle: ACPI object handle to get the context for.
116 * Call under acpiphp_context_lock.
118 static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
120 struct acpiphp_context *context = NULL;
124 status = acpi_get_data(handle, acpiphp_context_handler, &data);
125 if (ACPI_SUCCESS(status)) {
133 * acpiphp_put_context - Drop a reference to ACPI hotplug context.
134 * @handle: ACPI object handle to put the context for.
136 * The context object is removed if there are no more references to it.
138 * Call under acpiphp_context_lock.
140 static void acpiphp_put_context(struct acpiphp_context *context)
142 if (--context->refcount)
145 WARN_ON(context->func || context->bridge);
146 acpi_detach_data(context->handle, acpiphp_context_handler);
150 static inline void get_bridge(struct acpiphp_bridge *bridge)
152 kref_get(&bridge->ref);
155 static inline void put_bridge(struct acpiphp_bridge *bridge)
157 kref_put(&bridge->ref, free_bridge);
160 static void free_bridge(struct kref *kref)
162 struct acpiphp_context *context;
163 struct acpiphp_bridge *bridge;
164 struct acpiphp_slot *slot, *next;
165 struct acpiphp_func *func, *tmp;
167 mutex_lock(&acpiphp_context_lock);
169 bridge = container_of(kref, struct acpiphp_bridge, ref);
171 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
172 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
173 context = func->context;
174 context->func = NULL;
175 acpiphp_put_context(context);
181 context = bridge->context;
182 /* Release the reference acquired by acpiphp_enumerate_slots(). */
183 if (context->handler_for_func)
184 put_bridge(bridge->func->slot->bridge);
186 put_device(&bridge->pci_bus->dev);
187 pci_dev_put(bridge->pci_dev);
188 context->bridge = NULL;
189 acpiphp_put_context(context);
192 mutex_unlock(&acpiphp_context_lock);
196 * the _DCK method can do funny things... and sometimes not
199 * TBD - figure out a way to only call fixups for
200 * systems that require them.
202 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
204 struct acpiphp_func *func = data;
205 struct pci_bus *bus = func->slot->bridge->pci_bus;
211 /* fixup bad _DCK function that rewrites
212 * secondary bridge on slot
214 pci_read_config_dword(bus->self,
218 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
219 buses = (buses & 0xff000000)
220 | ((unsigned int)(bus->primary) << 0)
221 | ((unsigned int)(bus->busn_res.start) << 8)
222 | ((unsigned int)(bus->busn_res.end) << 16);
223 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
228 static const struct acpi_dock_ops acpiphp_dock_ops = {
229 .fixup = post_dock_fixups,
230 .handler = hotplug_event_func,
233 /* Check whether the PCI device is managed by native PCIe hotplug driver */
234 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
238 struct acpi_pci_root *root;
240 /* Check whether the PCIe port supports native PCIe hotplug */
241 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32))
243 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
247 * Check whether native PCIe hotplug has been enabled for
248 * this PCIe hierarchy.
250 tmp = acpi_find_root_bridge_handle(pdev);
253 root = acpi_pci_find_root(tmp);
256 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
262 static void acpiphp_dock_init(void *data)
264 struct acpiphp_func *func = data;
266 get_bridge(func->slot->bridge);
269 static void acpiphp_dock_release(void *data)
271 struct acpiphp_func *func = data;
273 put_bridge(func->slot->bridge);
276 /* callback routine to register each ACPI PCI slot object */
277 static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
280 struct acpiphp_bridge *bridge = data;
281 struct acpiphp_context *context;
282 struct acpiphp_slot *slot;
283 struct acpiphp_func *newfunc;
284 acpi_status status = AE_OK;
285 unsigned long long adr, sun;
286 int device, function, retval, found = 0;
287 struct pci_bus *pbus = bridge->pci_bus;
288 struct pci_dev *pdev;
291 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
294 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
295 if (ACPI_FAILURE(status)) {
296 warn("can't evaluate _ADR (%#x)\n", status);
300 device = (adr >> 16) & 0xffff;
301 function = adr & 0xffff;
303 pdev = bridge->pci_dev;
304 if (pdev && device_is_managed_by_native_pciehp(pdev))
307 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
311 newfunc->handle = handle;
312 newfunc->function = function;
314 mutex_lock(&acpiphp_context_lock);
315 context = acpiphp_init_context(handle);
317 mutex_unlock(&acpiphp_context_lock);
318 acpi_handle_err(handle, "No hotplug context\n");
322 newfunc->context = context;
323 context->func = newfunc;
324 mutex_unlock(&acpiphp_context_lock);
326 if (acpi_has_method(handle, "_EJ0"))
327 newfunc->flags = FUNC_HAS_EJ0;
329 if (acpi_has_method(handle, "_STA"))
330 newfunc->flags |= FUNC_HAS_STA;
332 if (acpi_has_method(handle, "_PS0"))
333 newfunc->flags |= FUNC_HAS_PS0;
335 if (acpi_has_method(handle, "_PS3"))
336 newfunc->flags |= FUNC_HAS_PS3;
338 if (acpi_has_method(handle, "_DCK"))
339 newfunc->flags |= FUNC_HAS_DCK;
341 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
342 if (ACPI_FAILURE(status)) {
344 * use the count of the number of slots we've found
345 * for the number of the slot
347 sun = bridge->nr_slots+1;
350 /* search for objects that share the same slot */
351 list_for_each_entry(slot, &bridge->slots, node)
352 if (slot->device == device) {
353 if (slot->sun != sun)
354 warn("sibling found, but _SUN doesn't match!\n");
360 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
362 status = AE_NO_MEMORY;
366 slot->bridge = bridge;
367 slot->device = device;
369 INIT_LIST_HEAD(&slot->funcs);
370 mutex_init(&slot->crit_sect);
372 mutex_lock(&bridge_mutex);
373 list_add_tail(&slot->node, &bridge->slots);
374 mutex_unlock(&bridge_mutex);
377 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
378 slot->sun, pci_domain_nr(pbus), pbus->number, device);
379 retval = acpiphp_register_hotplug_slot(slot);
381 if (retval == -EBUSY)
382 warn("Slot %llu already registered by another "
383 "hotplug driver\n", slot->sun);
385 warn("acpiphp_register_hotplug_slot failed "
386 "(err code = 0x%x)\n", retval);
393 newfunc->slot = slot;
394 mutex_lock(&bridge_mutex);
395 list_add_tail(&newfunc->sibling, &slot->funcs);
396 mutex_unlock(&bridge_mutex);
398 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
400 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
402 if (is_dock_device(handle)) {
403 /* we don't want to call this device's _EJ0
404 * because we want the dock notify handler
405 * to call it after it calls _DCK
407 newfunc->flags &= ~FUNC_HAS_EJ0;
408 if (register_hotplug_dock_device(handle,
409 &acpiphp_dock_ops, newfunc,
410 acpiphp_dock_init, acpiphp_dock_release))
411 dbg("failed to register dock device\n");
414 /* install notify handler */
415 if (!(newfunc->flags & FUNC_HAS_DCK)) {
416 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
417 handle_hotplug_event,
419 if (ACPI_SUCCESS(status))
420 context->handler_for_func = true;
422 err("failed to register interrupt notify handler\n");
429 mutex_lock(&bridge_mutex);
430 list_del(&slot->node);
431 mutex_unlock(&bridge_mutex);
435 mutex_lock(&acpiphp_context_lock);
436 context->func = NULL;
437 acpiphp_put_context(context);
438 mutex_unlock(&acpiphp_context_lock);
444 /* see if it's worth looking at this bridge */
445 static int detect_ejectable_slots(acpi_handle handle)
447 int found = acpi_pci_detect_ejectable(handle);
449 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
450 is_pci_dock_device, NULL, (void *)&found, NULL);
455 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
457 struct acpiphp_bridge *bridge;
459 mutex_lock(&bridge_mutex);
460 list_for_each_entry(bridge, &bridge_list, list)
461 if (bridge->handle == handle) {
463 mutex_unlock(&bridge_mutex);
466 mutex_unlock(&bridge_mutex);
471 static void cleanup_bridge(struct acpiphp_bridge *bridge)
473 struct acpiphp_slot *slot;
474 struct acpiphp_func *func;
476 acpi_handle handle = bridge->handle;
478 if (!bridge->context->handler_for_func) {
479 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
480 handle_hotplug_event);
481 if (ACPI_FAILURE(status))
482 err("failed to remove notify handler\n");
485 list_for_each_entry(slot, &bridge->slots, node) {
486 list_for_each_entry(func, &slot->funcs, sibling) {
487 if (is_dock_device(func->handle)) {
488 unregister_hotplug_dock_device(func->handle);
490 if (!(func->flags & FUNC_HAS_DCK)) {
491 func->context->handler_for_func = false;
492 status = acpi_remove_notify_handler(func->handle,
494 handle_hotplug_event);
495 if (ACPI_FAILURE(status))
496 err("failed to remove notify handler\n");
499 acpiphp_unregister_hotplug_slot(slot);
502 mutex_lock(&bridge_mutex);
503 list_del(&bridge->list);
504 mutex_unlock(&bridge_mutex);
507 static int power_on_slot(struct acpiphp_slot *slot)
510 struct acpiphp_func *func;
513 /* if already enabled, just skip */
514 if (slot->flags & SLOT_POWEREDON)
517 list_for_each_entry(func, &slot->funcs, sibling) {
518 if (func->flags & FUNC_HAS_PS0) {
519 dbg("%s: executing _PS0\n", __func__);
520 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
521 if (ACPI_FAILURE(status)) {
522 warn("%s: _PS0 failed\n", __func__);
530 /* TBD: evaluate _STA to check if the slot is enabled */
532 slot->flags |= SLOT_POWEREDON;
539 static int power_off_slot(struct acpiphp_slot *slot)
542 struct acpiphp_func *func;
546 /* if already disabled, just skip */
547 if ((slot->flags & SLOT_POWEREDON) == 0)
550 list_for_each_entry(func, &slot->funcs, sibling) {
551 if (func->flags & FUNC_HAS_PS3) {
552 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
553 if (ACPI_FAILURE(status)) {
554 warn("%s: _PS3 failed\n", __func__);
562 /* TBD: evaluate _STA to check if the slot is disabled */
564 slot->flags &= (~SLOT_POWEREDON);
573 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
574 * @bus: bus to start search with
576 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
578 struct list_head *tmp;
579 unsigned char max, n;
582 * pci_bus_max_busnr will return the highest
583 * reserved busnr for all these children.
584 * that is equivalent to the bus->subordinate
585 * value. We don't want to use the parent's
586 * bus->subordinate value because it could have
589 max = bus->busn_res.start;
591 list_for_each(tmp, &bus->children) {
592 n = pci_bus_max_busnr(pci_bus_b(tmp));
601 * acpiphp_bus_add - add a new bus to acpi subsystem
602 * @func: acpiphp_func of the bridge
604 static int acpiphp_bus_add(struct acpiphp_func *func)
606 struct acpi_device *device;
609 if (!acpi_bus_get_device(func->handle, &device)) {
610 dbg("bus exists... trim\n");
611 /* this shouldn't be in here, so remove
612 * the bus then re-add it...
614 acpi_bus_trim(device);
617 ret_val = acpi_bus_scan(func->handle);
619 ret_val = acpi_bus_get_device(func->handle, &device);
622 dbg("error adding bus, %x\n", -ret_val);
629 * acpiphp_bus_trim - trim a bus from acpi subsystem
630 * @handle: handle to acpi namespace
632 static int acpiphp_bus_trim(acpi_handle handle)
634 struct acpi_device *device;
637 retval = acpi_bus_get_device(handle, &device);
639 dbg("acpi_device not found\n");
643 acpi_bus_trim(device);
647 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
649 struct acpiphp_func *func;
650 union acpi_object params[2];
651 struct acpi_object_list arg_list;
653 list_for_each_entry(func, &slot->funcs, sibling) {
655 arg_list.pointer = params;
656 params[0].type = ACPI_TYPE_INTEGER;
657 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
658 params[1].type = ACPI_TYPE_INTEGER;
659 params[1].integer.value = 1;
660 /* _REG is optional, we don't care about if there is failure */
661 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
665 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
667 struct acpiphp_func *func;
669 if (!dev->subordinate)
672 /* quirk, or pcie could set it already */
673 if (dev->is_hotplug_bridge)
676 if (PCI_SLOT(dev->devfn) != slot->device)
679 list_for_each_entry(func, &slot->funcs, sibling) {
680 if (PCI_FUNC(dev->devfn) == func->function) {
681 /* check if this bridge has ejectable slots */
682 if ((detect_ejectable_slots(func->handle) > 0))
683 dev->is_hotplug_bridge = 1;
690 * enable_device - enable, configure a slot
691 * @slot: slot to be enabled
693 * This function should be called per *physical slot*,
694 * not per each slot object in ACPI namespace.
696 static int __ref enable_device(struct acpiphp_slot *slot)
699 struct pci_bus *bus = slot->bridge->pci_bus;
700 struct acpiphp_func *func;
704 if (slot->flags & SLOT_ENABLED)
707 list_for_each_entry(func, &slot->funcs, sibling)
708 acpiphp_bus_add(func);
710 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
712 /* Maybe only part of funcs are added. */
713 dbg("No new device found\n");
717 max = acpiphp_max_busnr(bus);
718 for (pass = 0; pass < 2; pass++) {
719 list_for_each_entry(dev, &bus->devices, bus_list) {
720 if (PCI_SLOT(dev->devfn) != slot->device)
722 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
723 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
724 max = pci_scan_bridge(bus, dev, max, pass);
725 if (pass && dev->subordinate) {
726 check_hotplug_bridge(slot, dev);
727 pcibios_resource_survey_bus(dev->subordinate);
728 __pci_bus_size_bridges(dev->subordinate,
735 __pci_bus_assign_resources(bus, &add_list, NULL);
736 acpiphp_sanitize_bus(bus);
737 acpiphp_set_hpp_values(bus);
738 acpiphp_set_acpi_region(slot);
739 pci_enable_bridges(bus);
741 list_for_each_entry(dev, &bus->devices, bus_list) {
742 /* Assume that newly added devices are powered on already. */
744 dev->current_state = PCI_D0;
747 pci_bus_add_devices(bus);
749 slot->flags |= SLOT_ENABLED;
750 list_for_each_entry(func, &slot->funcs, sibling) {
751 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
754 /* Do not set SLOT_ENABLED flag if some funcs
756 slot->flags &= (~SLOT_ENABLED);
766 /* return first device in slot, acquiring a reference on it */
767 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
769 struct pci_bus *bus = slot->bridge->pci_bus;
771 struct pci_dev *ret = NULL;
773 down_read(&pci_bus_sem);
774 list_for_each_entry(dev, &bus->devices, bus_list)
775 if (PCI_SLOT(dev->devfn) == slot->device) {
776 ret = pci_dev_get(dev);
779 up_read(&pci_bus_sem);
785 * disable_device - disable a slot
786 * @slot: ACPI PHP slot
788 static int disable_device(struct acpiphp_slot *slot)
790 struct acpiphp_func *func;
791 struct pci_dev *pdev;
794 * enable_device() enumerates all functions in this device via
795 * pci_scan_slot(), whether they have associated ACPI hotplug
796 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
799 while ((pdev = dev_in_slot(slot))) {
800 pci_stop_and_remove_bus_device(pdev);
804 list_for_each_entry(func, &slot->funcs, sibling) {
805 acpiphp_bus_trim(func->handle);
808 slot->flags &= (~SLOT_ENABLED);
815 * get_slot_status - get ACPI slot status
816 * @slot: ACPI PHP slot
818 * If a slot has _STA for each function and if any one of them
819 * returned non-zero status, return it.
821 * If a slot doesn't have _STA and if any one of its functions'
822 * configuration space is configured, return 0x0f as a _STA.
824 * Otherwise return 0.
826 static unsigned int get_slot_status(struct acpiphp_slot *slot)
829 unsigned long long sta = 0;
831 struct acpiphp_func *func;
833 list_for_each_entry(func, &slot->funcs, sibling) {
834 if (func->flags & FUNC_HAS_STA) {
835 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
836 if (ACPI_SUCCESS(status) && sta)
839 pci_bus_read_config_dword(slot->bridge->pci_bus,
840 PCI_DEVFN(slot->device,
842 PCI_VENDOR_ID, &dvid);
843 if (dvid != 0xffffffff) {
850 return (unsigned int)sta;
854 * acpiphp_eject_slot - physically eject the slot
855 * @slot: ACPI PHP slot
857 int acpiphp_eject_slot(struct acpiphp_slot *slot)
859 struct acpiphp_func *func;
861 list_for_each_entry(func, &slot->funcs, sibling) {
862 /* We don't want to call _EJ0 on non-existing functions. */
863 if ((func->flags & FUNC_HAS_EJ0)) {
864 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
874 * acpiphp_check_bridge - re-enumerate devices
875 * @bridge: where to begin re-enumeration
877 * Iterate over all slots under this bridge and make sure that if a
878 * card is present they are enabled, and if not they are disabled.
880 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
882 struct acpiphp_slot *slot;
884 int enabled, disabled;
886 enabled = disabled = 0;
888 list_for_each_entry(slot, &bridge->slots, node) {
889 unsigned int status = get_slot_status(slot);
890 if (slot->flags & SLOT_ENABLED) {
891 if (status == ACPI_STA_ALL)
893 retval = acpiphp_disable_slot(slot);
895 err("Error occurred in disabling\n");
898 acpiphp_eject_slot(slot);
902 if (status != ACPI_STA_ALL)
904 retval = acpiphp_enable_slot(slot);
906 err("Error occurred in enabling\n");
913 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
919 static void acpiphp_set_hpp_values(struct pci_bus *bus)
923 list_for_each_entry(dev, &bus->devices, bus_list)
924 pci_configure_slot(dev);
928 * Remove devices for which we could not assign resources, call
929 * arch specific code to fix-up the bus
931 static void acpiphp_sanitize_bus(struct pci_bus *bus)
933 struct pci_dev *dev, *tmp;
935 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
937 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
938 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
939 struct resource *res = &dev->resource[i];
940 if ((res->flags & type_mask) && !res->start &&
942 /* Could not assign a required resources
943 * for this device, remove it */
944 pci_stop_and_remove_bus_device(dev);
952 * ACPI event handlers
956 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
958 struct acpiphp_bridge *bridge;
960 struct acpi_buffer buffer = { .length = sizeof(objname),
961 .pointer = objname };
963 bridge = acpiphp_handle_to_bridge(handle);
965 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
966 dbg("%s: re-enumerating slots under %s\n",
968 acpiphp_check_bridge(bridge);
974 void acpiphp_check_host_bridge(acpi_handle handle)
976 struct acpiphp_bridge *bridge;
978 bridge = acpiphp_handle_to_bridge(handle);
980 acpiphp_check_bridge(bridge);
984 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
985 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
988 static void _handle_hotplug_event_bridge(struct work_struct *work)
990 struct acpiphp_bridge *bridge;
992 struct acpi_buffer buffer = { .length = sizeof(objname),
993 .pointer = objname };
994 struct acpi_hp_work *hp_work;
998 hp_work = container_of(work, struct acpi_hp_work, work);
999 handle = hp_work->handle;
1000 type = hp_work->type;
1001 bridge = (struct acpiphp_bridge *)hp_work->context;
1003 acpi_scan_lock_acquire();
1005 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1008 case ACPI_NOTIFY_BUS_CHECK:
1009 /* bus re-enumerate */
1010 dbg("%s: Bus check notify on %s\n", __func__, objname);
1011 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1012 acpiphp_check_bridge(bridge);
1013 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1014 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1017 case ACPI_NOTIFY_DEVICE_CHECK:
1019 dbg("%s: Device check notify on %s\n", __func__, objname);
1020 acpiphp_check_bridge(bridge);
1023 case ACPI_NOTIFY_DEVICE_WAKE:
1025 dbg("%s: Device wake notify on %s\n", __func__, objname);
1028 case ACPI_NOTIFY_EJECT_REQUEST:
1029 /* request device eject */
1030 dbg("%s: Device eject notify on %s\n", __func__, objname);
1031 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
1032 struct acpiphp_slot *slot;
1033 slot = bridge->func->slot;
1034 if (!acpiphp_disable_slot(slot))
1035 acpiphp_eject_slot(slot);
1039 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1040 printk(KERN_ERR "Device %s cannot be configured due"
1041 " to a frequency mismatch\n", objname);
1044 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1045 printk(KERN_ERR "Device %s cannot be configured due"
1046 " to a bus mode mismatch\n", objname);
1049 case ACPI_NOTIFY_POWER_FAULT:
1050 printk(KERN_ERR "Device %s has suffered a power fault\n",
1055 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1059 acpi_scan_lock_release();
1060 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1064 static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
1066 struct acpiphp_func *func = context;
1068 struct acpi_buffer buffer = { .length = sizeof(objname),
1069 .pointer = objname };
1071 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1074 case ACPI_NOTIFY_BUS_CHECK:
1075 /* bus re-enumerate */
1076 dbg("%s: Bus check notify on %s\n", __func__, objname);
1077 acpiphp_enable_slot(func->slot);
1080 case ACPI_NOTIFY_DEVICE_CHECK:
1081 /* device check : re-enumerate from parent bus */
1082 dbg("%s: Device check notify on %s\n", __func__, objname);
1083 acpiphp_check_bridge(func->slot->bridge);
1086 case ACPI_NOTIFY_DEVICE_WAKE:
1088 dbg("%s: Device wake notify on %s\n", __func__, objname);
1091 case ACPI_NOTIFY_EJECT_REQUEST:
1092 /* request device eject */
1093 dbg("%s: Device eject notify on %s\n", __func__, objname);
1094 if (!(acpiphp_disable_slot(func->slot)))
1095 acpiphp_eject_slot(func->slot);
1099 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1104 static void _handle_hotplug_event_func(struct work_struct *work)
1106 struct acpi_hp_work *hp_work;
1107 struct acpiphp_func *func;
1109 hp_work = container_of(work, struct acpi_hp_work, work);
1110 func = hp_work->context;
1111 acpi_scan_lock_acquire();
1113 hotplug_event_func(hp_work->handle, hp_work->type, func);
1115 acpi_scan_lock_release();
1116 kfree(hp_work); /* allocated in handle_hotplug_event_func */
1117 put_bridge(func->slot->bridge);
1121 * handle_hotplug_event - handle ACPI hotplug event
1122 * @handle: Notify()'ed acpi_handle
1123 * @type: Notify code
1124 * @data: pointer to acpiphp_context structure
1126 * Handles ACPI event notification on slots.
1128 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
1130 struct acpiphp_context *context;
1131 void (*work_func)(struct work_struct *work) = NULL;
1133 mutex_lock(&acpiphp_context_lock);
1134 context = acpiphp_get_context(handle);
1136 if (context->bridge) {
1137 get_bridge(context->bridge);
1138 data = context->bridge;
1139 work_func = _handle_hotplug_event_bridge;
1140 } else if (context->func) {
1141 get_bridge(context->func->slot->bridge);
1142 data = context->func;
1143 work_func = _handle_hotplug_event_func;
1145 acpiphp_put_context(context);
1147 mutex_unlock(&acpiphp_context_lock);
1150 * Currently the code adds all hotplug events to the kacpid_wq
1151 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1152 * The proper way to fix this is to reorganize the code so that
1153 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1154 * For now just re-add this work to the kacpi_hotplug_wq so we
1155 * don't deadlock on hotplug actions.
1158 alloc_acpi_hp_work(handle, type, data, work_func);
1162 * Create hotplug slots for the PCI bus.
1163 * It should always return 0 to avoid skipping following notifiers.
1165 void acpiphp_enumerate_slots(struct pci_bus *bus)
1167 struct acpiphp_context *context;
1168 struct acpiphp_bridge *bridge;
1172 if (acpiphp_disabled)
1175 handle = ACPI_HANDLE(bus->bridge);
1176 if (!handle || detect_ejectable_slots(handle) <= 0)
1179 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1181 acpi_handle_err(handle, "No memory for bridge object\n");
1185 INIT_LIST_HEAD(&bridge->slots);
1186 kref_init(&bridge->ref);
1187 bridge->handle = handle;
1188 bridge->pci_dev = pci_dev_get(bus->self);
1189 bridge->pci_bus = bus;
1191 mutex_lock(&acpiphp_context_lock);
1192 context = acpiphp_get_context(handle);
1194 context = acpiphp_init_context(handle);
1196 mutex_unlock(&acpiphp_context_lock);
1197 acpi_handle_err(handle, "No hotplug context\n");
1202 bridge->context = context;
1203 context->bridge = bridge;
1204 mutex_unlock(&acpiphp_context_lock);
1207 * Grab a ref to the subordinate PCI bus in case the bus is
1208 * removed via PCI core logical hotplug. The ref pins the bus
1209 * (which we access during module unload).
1211 get_device(&bus->dev);
1213 /* must be added to the list prior to calling register_slot */
1214 mutex_lock(&bridge_mutex);
1215 list_add(&bridge->list, &bridge_list);
1216 mutex_unlock(&bridge_mutex);
1218 /* register all slot objects under this bridge */
1219 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1220 register_slot, NULL, bridge, NULL);
1221 if (ACPI_FAILURE(status)) {
1222 acpi_handle_err(bridge->handle, "failed to register slots\n");
1226 if (pci_is_root_bus(bridge->pci_bus))
1229 if (acpi_has_method(bridge->handle, "_EJ0")) {
1230 dbg("found ejectable p2p bridge\n");
1231 bridge->flags |= BRIDGE_HAS_EJ0;
1233 if (context->handler_for_func) {
1234 /* Notify handler already installed. */
1235 bridge->func = context->func;
1236 get_bridge(context->func->slot->bridge);
1240 /* install notify handler for P2P bridges */
1241 status = acpi_install_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
1242 handle_hotplug_event, NULL);
1243 if (ACPI_SUCCESS(status))
1246 acpi_handle_err(bridge->handle, "failed to register notify handler\n");
1249 cleanup_bridge(bridge);
1253 /* Destroy hotplug slots associated with the PCI bus */
1254 void acpiphp_remove_slots(struct pci_bus *bus)
1256 struct acpiphp_bridge *bridge, *tmp;
1258 if (acpiphp_disabled)
1261 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1262 if (bridge->pci_bus == bus) {
1263 cleanup_bridge(bridge);
1270 * acpiphp_enable_slot - power on slot
1271 * @slot: ACPI PHP slot
1273 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1277 mutex_lock(&slot->crit_sect);
1279 /* wake up all functions */
1280 retval = power_on_slot(slot);
1284 if (get_slot_status(slot) == ACPI_STA_ALL) {
1285 /* configure all functions */
1286 retval = enable_device(slot);
1288 power_off_slot(slot);
1290 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1291 power_off_slot(slot);
1295 mutex_unlock(&slot->crit_sect);
1300 * acpiphp_disable_slot - power off slot
1301 * @slot: ACPI PHP slot
1303 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1307 mutex_lock(&slot->crit_sect);
1309 /* unconfigure all functions */
1310 retval = disable_device(slot);
1314 /* power off all functions */
1315 retval = power_off_slot(slot);
1320 mutex_unlock(&slot->crit_sect);
1329 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1331 return (slot->flags & SLOT_POWEREDON);
1339 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1343 sta = get_slot_status(slot);
1345 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1350 * adapter presence : 1
1353 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1357 sta = get_slot_status(slot);
1359 return (sta == 0) ? 0 : 1;