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 void post_dock_fixups(acpi_handle not_used
, u32 event
, void *data
)
124 struct acpiphp_func
*func
= data
;
125 struct pci_bus
*bus
= func
->slot
->bridge
->pci_bus
;
131 /* fixup bad _DCK function that rewrites
132 * secondary bridge on slot
134 pci_read_config_dword(bus
->self
,
138 if (((buses
>> 8) & 0xff) != bus
->busn_res
.start
) {
139 buses
= (buses
& 0xff000000)
140 | ((unsigned int)(bus
->primary
) << 0)
141 | ((unsigned int)(bus
->busn_res
.start
) << 8)
142 | ((unsigned int)(bus
->busn_res
.end
) << 16);
143 pci_write_config_dword(bus
->self
, PCI_PRIMARY_BUS
, buses
);
148 static const struct acpi_dock_ops acpiphp_dock_ops
= {
149 .fixup
= post_dock_fixups
,
150 .handler
= hotplug_event_func
,
153 /* Check whether the PCI device is managed by native PCIe hotplug driver */
154 static bool device_is_managed_by_native_pciehp(struct pci_dev
*pdev
)
158 struct acpi_pci_root
*root
;
160 /* Check whether the PCIe port supports native PCIe hotplug */
161 if (pcie_capability_read_dword(pdev
, PCI_EXP_SLTCAP
, ®32
))
163 if (!(reg32
& PCI_EXP_SLTCAP_HPC
))
167 * Check whether native PCIe hotplug has been enabled for
168 * this PCIe hierarchy.
170 tmp
= acpi_find_root_bridge_handle(pdev
);
173 root
= acpi_pci_find_root(tmp
);
176 if (!(root
->osc_control_set
& OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
))
182 static void acpiphp_dock_init(void *data
)
184 struct acpiphp_func
*func
= data
;
186 get_bridge(func
->slot
->bridge
);
189 static void acpiphp_dock_release(void *data
)
191 struct acpiphp_func
*func
= data
;
193 put_bridge(func
->slot
->bridge
);
196 /* callback routine to register each ACPI PCI slot object */
198 register_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
200 struct acpiphp_bridge
*bridge
= (struct acpiphp_bridge
*)context
;
201 struct acpiphp_slot
*slot
;
202 struct acpiphp_func
*newfunc
;
203 acpi_status status
= AE_OK
;
204 unsigned long long adr
, sun
;
205 int device
, function
, retval
, found
= 0;
206 struct pci_bus
*pbus
= bridge
->pci_bus
;
207 struct pci_dev
*pdev
;
210 if (!acpi_pci_check_ejectable(pbus
, handle
) && !is_dock_device(handle
))
213 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &adr
);
214 if (ACPI_FAILURE(status
)) {
215 warn("can't evaluate _ADR (%#x)\n", status
);
219 device
= (adr
>> 16) & 0xffff;
220 function
= adr
& 0xffff;
222 pdev
= bridge
->pci_dev
;
223 if (pdev
&& device_is_managed_by_native_pciehp(pdev
))
226 newfunc
= kzalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
230 newfunc
->handle
= handle
;
231 newfunc
->function
= function
;
233 if (acpi_has_method(handle
, "_EJ0"))
234 newfunc
->flags
= FUNC_HAS_EJ0
;
236 if (acpi_has_method(handle
, "_STA"))
237 newfunc
->flags
|= FUNC_HAS_STA
;
239 if (acpi_has_method(handle
, "_PS0"))
240 newfunc
->flags
|= FUNC_HAS_PS0
;
242 if (acpi_has_method(handle
, "_PS3"))
243 newfunc
->flags
|= FUNC_HAS_PS3
;
245 if (acpi_has_method(handle
, "_DCK"))
246 newfunc
->flags
|= FUNC_HAS_DCK
;
248 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
249 if (ACPI_FAILURE(status
)) {
251 * use the count of the number of slots we've found
252 * for the number of the slot
254 sun
= bridge
->nr_slots
+1;
257 /* search for objects that share the same slot */
258 list_for_each_entry(slot
, &bridge
->slots
, node
)
259 if (slot
->device
== device
) {
260 if (slot
->sun
!= sun
)
261 warn("sibling found, but _SUN doesn't match!\n");
267 slot
= kzalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
273 slot
->bridge
= bridge
;
274 slot
->device
= device
;
276 INIT_LIST_HEAD(&slot
->funcs
);
277 mutex_init(&slot
->crit_sect
);
279 mutex_lock(&bridge_mutex
);
280 list_add_tail(&slot
->node
, &bridge
->slots
);
281 mutex_unlock(&bridge_mutex
);
284 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
285 slot
->sun
, pci_domain_nr(pbus
), pbus
->number
, device
);
286 retval
= acpiphp_register_hotplug_slot(slot
);
288 if (retval
== -EBUSY
)
289 warn("Slot %llu already registered by another "
290 "hotplug driver\n", slot
->sun
);
292 warn("acpiphp_register_hotplug_slot failed "
293 "(err code = 0x%x)\n", retval
);
298 newfunc
->slot
= slot
;
299 mutex_lock(&bridge_mutex
);
300 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
301 mutex_unlock(&bridge_mutex
);
303 if (pci_bus_read_dev_vendor_id(pbus
, PCI_DEVFN(device
, function
),
305 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
307 if (is_dock_device(handle
)) {
308 /* we don't want to call this device's _EJ0
309 * because we want the dock notify handler
310 * to call it after it calls _DCK
312 newfunc
->flags
&= ~FUNC_HAS_EJ0
;
313 if (register_hotplug_dock_device(handle
,
314 &acpiphp_dock_ops
, newfunc
,
315 acpiphp_dock_init
, acpiphp_dock_release
))
316 dbg("failed to register dock device\n");
319 /* install notify handler */
320 if (!(newfunc
->flags
& FUNC_HAS_DCK
)) {
321 status
= acpi_install_notify_handler(handle
,
323 handle_hotplug_event_func
,
326 if (ACPI_FAILURE(status
))
327 err("failed to register interrupt notify handler\n");
335 mutex_lock(&bridge_mutex
);
336 list_del(&slot
->node
);
337 mutex_unlock(&bridge_mutex
);
345 /* see if it's worth looking at this bridge */
346 static int detect_ejectable_slots(acpi_handle handle
)
348 int found
= acpi_pci_detect_ejectable(handle
);
350 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
351 is_pci_dock_device
, NULL
, (void *)&found
, NULL
);
357 /* find acpiphp_func from acpiphp_bridge */
358 static struct acpiphp_func
*acpiphp_bridge_handle_to_function(acpi_handle handle
)
360 struct acpiphp_bridge
*bridge
;
361 struct acpiphp_slot
*slot
;
362 struct acpiphp_func
*func
= NULL
;
364 mutex_lock(&bridge_mutex
);
365 list_for_each_entry(bridge
, &bridge_list
, list
) {
366 list_for_each_entry(slot
, &bridge
->slots
, node
) {
367 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
368 if (func
->handle
== handle
) {
369 get_bridge(func
->slot
->bridge
);
370 mutex_unlock(&bridge_mutex
);
376 mutex_unlock(&bridge_mutex
);
382 static struct acpiphp_bridge
*acpiphp_handle_to_bridge(acpi_handle handle
)
384 struct acpiphp_bridge
*bridge
;
386 mutex_lock(&bridge_mutex
);
387 list_for_each_entry(bridge
, &bridge_list
, list
)
388 if (bridge
->handle
== handle
) {
390 mutex_unlock(&bridge_mutex
);
393 mutex_unlock(&bridge_mutex
);
398 static void cleanup_bridge(struct acpiphp_bridge
*bridge
)
400 struct acpiphp_slot
*slot
;
401 struct acpiphp_func
*func
;
403 acpi_handle handle
= bridge
->handle
;
405 if (!pci_is_root_bus(bridge
->pci_bus
)) {
406 status
= acpi_remove_notify_handler(handle
,
408 handle_hotplug_event_bridge
);
409 if (ACPI_FAILURE(status
))
410 err("failed to remove notify handler\n");
413 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
414 status
= acpi_install_notify_handler(bridge
->func
->handle
,
416 handle_hotplug_event_func
,
418 if (ACPI_FAILURE(status
))
419 err("failed to install interrupt notify handler\n");
422 list_for_each_entry(slot
, &bridge
->slots
, node
) {
423 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
424 if (is_dock_device(func
->handle
)) {
425 unregister_hotplug_dock_device(func
->handle
);
427 if (!(func
->flags
& FUNC_HAS_DCK
)) {
428 status
= acpi_remove_notify_handler(func
->handle
,
430 handle_hotplug_event_func
);
431 if (ACPI_FAILURE(status
))
432 err("failed to remove notify handler\n");
435 acpiphp_unregister_hotplug_slot(slot
);
438 mutex_lock(&bridge_mutex
);
439 list_del(&bridge
->list
);
440 mutex_unlock(&bridge_mutex
);
443 static int power_on_slot(struct acpiphp_slot
*slot
)
446 struct acpiphp_func
*func
;
449 /* if already enabled, just skip */
450 if (slot
->flags
& SLOT_POWEREDON
)
453 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
454 if (func
->flags
& FUNC_HAS_PS0
) {
455 dbg("%s: executing _PS0\n", __func__
);
456 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
457 if (ACPI_FAILURE(status
)) {
458 warn("%s: _PS0 failed\n", __func__
);
466 /* TBD: evaluate _STA to check if the slot is enabled */
468 slot
->flags
|= SLOT_POWEREDON
;
475 static int power_off_slot(struct acpiphp_slot
*slot
)
478 struct acpiphp_func
*func
;
482 /* if already disabled, just skip */
483 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
486 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
487 if (func
->flags
& FUNC_HAS_PS3
) {
488 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
489 if (ACPI_FAILURE(status
)) {
490 warn("%s: _PS3 failed\n", __func__
);
498 /* TBD: evaluate _STA to check if the slot is disabled */
500 slot
->flags
&= (~SLOT_POWEREDON
);
509 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
510 * @bus: bus to start search with
512 static unsigned char acpiphp_max_busnr(struct pci_bus
*bus
)
514 struct list_head
*tmp
;
515 unsigned char max
, n
;
518 * pci_bus_max_busnr will return the highest
519 * reserved busnr for all these children.
520 * that is equivalent to the bus->subordinate
521 * value. We don't want to use the parent's
522 * bus->subordinate value because it could have
525 max
= bus
->busn_res
.start
;
527 list_for_each(tmp
, &bus
->children
) {
528 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
537 * acpiphp_bus_add - add a new bus to acpi subsystem
538 * @func: acpiphp_func of the bridge
540 static int acpiphp_bus_add(struct acpiphp_func
*func
)
542 struct acpi_device
*device
;
545 if (!acpi_bus_get_device(func
->handle
, &device
)) {
546 dbg("bus exists... trim\n");
547 /* this shouldn't be in here, so remove
548 * the bus then re-add it...
550 acpi_bus_trim(device
);
553 ret_val
= acpi_bus_scan(func
->handle
);
555 ret_val
= acpi_bus_get_device(func
->handle
, &device
);
558 dbg("error adding bus, %x\n", -ret_val
);
565 * acpiphp_bus_trim - trim a bus from acpi subsystem
566 * @handle: handle to acpi namespace
568 static int acpiphp_bus_trim(acpi_handle handle
)
570 struct acpi_device
*device
;
573 retval
= acpi_bus_get_device(handle
, &device
);
575 dbg("acpi_device not found\n");
579 acpi_bus_trim(device
);
583 static void acpiphp_set_acpi_region(struct acpiphp_slot
*slot
)
585 struct acpiphp_func
*func
;
586 union acpi_object params
[2];
587 struct acpi_object_list arg_list
;
589 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
591 arg_list
.pointer
= params
;
592 params
[0].type
= ACPI_TYPE_INTEGER
;
593 params
[0].integer
.value
= ACPI_ADR_SPACE_PCI_CONFIG
;
594 params
[1].type
= ACPI_TYPE_INTEGER
;
595 params
[1].integer
.value
= 1;
596 /* _REG is optional, we don't care about if there is failure */
597 acpi_evaluate_object(func
->handle
, "_REG", &arg_list
, NULL
);
601 static void check_hotplug_bridge(struct acpiphp_slot
*slot
, struct pci_dev
*dev
)
603 struct acpiphp_func
*func
;
605 if (!dev
->subordinate
)
608 /* quirk, or pcie could set it already */
609 if (dev
->is_hotplug_bridge
)
612 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
615 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
616 if (PCI_FUNC(dev
->devfn
) == func
->function
) {
617 /* check if this bridge has ejectable slots */
618 if ((detect_ejectable_slots(func
->handle
) > 0))
619 dev
->is_hotplug_bridge
= 1;
626 * enable_device - enable, configure a slot
627 * @slot: slot to be enabled
629 * This function should be called per *physical slot*,
630 * not per each slot object in ACPI namespace.
632 static int __ref
enable_device(struct acpiphp_slot
*slot
)
635 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
636 struct acpiphp_func
*func
;
640 if (slot
->flags
& SLOT_ENABLED
)
643 list_for_each_entry(func
, &slot
->funcs
, sibling
)
644 acpiphp_bus_add(func
);
646 num
= pci_scan_slot(bus
, PCI_DEVFN(slot
->device
, 0));
648 /* Maybe only part of funcs are added. */
649 dbg("No new device found\n");
653 max
= acpiphp_max_busnr(bus
);
654 for (pass
= 0; pass
< 2; pass
++) {
655 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
656 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
658 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
659 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
660 max
= pci_scan_bridge(bus
, dev
, max
, pass
);
661 if (pass
&& dev
->subordinate
) {
662 check_hotplug_bridge(slot
, dev
);
663 pcibios_resource_survey_bus(dev
->subordinate
);
664 __pci_bus_size_bridges(dev
->subordinate
,
671 __pci_bus_assign_resources(bus
, &add_list
, NULL
);
672 acpiphp_sanitize_bus(bus
);
673 acpiphp_set_hpp_values(bus
);
674 acpiphp_set_acpi_region(slot
);
675 pci_enable_bridges(bus
);
677 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
678 /* Assume that newly added devices are powered on already. */
680 dev
->current_state
= PCI_D0
;
683 pci_bus_add_devices(bus
);
685 slot
->flags
|= SLOT_ENABLED
;
686 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
687 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
690 /* Do not set SLOT_ENABLED flag if some funcs
692 slot
->flags
&= (~SLOT_ENABLED
);
702 /* return first device in slot, acquiring a reference on it */
703 static struct pci_dev
*dev_in_slot(struct acpiphp_slot
*slot
)
705 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
707 struct pci_dev
*ret
= NULL
;
709 down_read(&pci_bus_sem
);
710 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
711 if (PCI_SLOT(dev
->devfn
) == slot
->device
) {
712 ret
= pci_dev_get(dev
);
715 up_read(&pci_bus_sem
);
721 * disable_device - disable a slot
722 * @slot: ACPI PHP slot
724 static int disable_device(struct acpiphp_slot
*slot
)
726 struct acpiphp_func
*func
;
727 struct pci_dev
*pdev
;
730 * enable_device() enumerates all functions in this device via
731 * pci_scan_slot(), whether they have associated ACPI hotplug
732 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
735 while ((pdev
= dev_in_slot(slot
))) {
736 pci_stop_and_remove_bus_device(pdev
);
740 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
741 acpiphp_bus_trim(func
->handle
);
744 slot
->flags
&= (~SLOT_ENABLED
);
751 * get_slot_status - get ACPI slot status
752 * @slot: ACPI PHP slot
754 * If a slot has _STA for each function and if any one of them
755 * returned non-zero status, return it.
757 * If a slot doesn't have _STA and if any one of its functions'
758 * configuration space is configured, return 0x0f as a _STA.
760 * Otherwise return 0.
762 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
765 unsigned long long sta
= 0;
767 struct acpiphp_func
*func
;
769 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
770 if (func
->flags
& FUNC_HAS_STA
) {
771 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
772 if (ACPI_SUCCESS(status
) && sta
)
775 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
776 PCI_DEVFN(slot
->device
,
778 PCI_VENDOR_ID
, &dvid
);
779 if (dvid
!= 0xffffffff) {
786 return (unsigned int)sta
;
790 * acpiphp_eject_slot - physically eject the slot
791 * @slot: ACPI PHP slot
793 int acpiphp_eject_slot(struct acpiphp_slot
*slot
)
795 struct acpiphp_func
*func
;
797 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
798 /* We don't want to call _EJ0 on non-existing functions. */
799 if ((func
->flags
& FUNC_HAS_EJ0
)) {
800 if (ACPI_FAILURE(acpi_evaluate_ej0(func
->handle
)))
810 * acpiphp_check_bridge - re-enumerate devices
811 * @bridge: where to begin re-enumeration
813 * Iterate over all slots under this bridge and make sure that if a
814 * card is present they are enabled, and if not they are disabled.
816 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
818 struct acpiphp_slot
*slot
;
820 int enabled
, disabled
;
822 enabled
= disabled
= 0;
824 list_for_each_entry(slot
, &bridge
->slots
, node
) {
825 unsigned int status
= get_slot_status(slot
);
826 if (slot
->flags
& SLOT_ENABLED
) {
827 if (status
== ACPI_STA_ALL
)
829 retval
= acpiphp_disable_slot(slot
);
831 err("Error occurred in disabling\n");
834 acpiphp_eject_slot(slot
);
838 if (status
!= ACPI_STA_ALL
)
840 retval
= acpiphp_enable_slot(slot
);
842 err("Error occurred in enabling\n");
849 dbg("%s: %d enabled, %d disabled\n", __func__
, enabled
, disabled
);
855 static void acpiphp_set_hpp_values(struct pci_bus
*bus
)
859 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
860 pci_configure_slot(dev
);
864 * Remove devices for which we could not assign resources, call
865 * arch specific code to fix-up the bus
867 static void acpiphp_sanitize_bus(struct pci_bus
*bus
)
869 struct pci_dev
*dev
, *tmp
;
871 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
;
873 list_for_each_entry_safe(dev
, tmp
, &bus
->devices
, bus_list
) {
874 for (i
=0; i
<PCI_BRIDGE_RESOURCES
; i
++) {
875 struct resource
*res
= &dev
->resource
[i
];
876 if ((res
->flags
& type_mask
) && !res
->start
&&
878 /* Could not assign a required resources
879 * for this device, remove it */
880 pci_stop_and_remove_bus_device(dev
);
888 * ACPI event handlers
892 check_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
894 struct acpiphp_bridge
*bridge
;
896 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
897 .pointer
= objname
};
899 bridge
= acpiphp_handle_to_bridge(handle
);
901 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
902 dbg("%s: re-enumerating slots under %s\n",
904 acpiphp_check_bridge(bridge
);
910 void acpiphp_check_host_bridge(acpi_handle handle
)
912 struct acpiphp_bridge
*bridge
;
914 bridge
= acpiphp_handle_to_bridge(handle
);
916 acpiphp_check_bridge(bridge
);
920 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
921 ACPI_UINT32_MAX
, check_sub_bridges
, NULL
, NULL
, NULL
);
924 static void _handle_hotplug_event_bridge(struct work_struct
*work
)
926 struct acpiphp_bridge
*bridge
;
928 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
929 .pointer
= objname
};
930 struct acpi_hp_work
*hp_work
;
934 hp_work
= container_of(work
, struct acpi_hp_work
, work
);
935 handle
= hp_work
->handle
;
936 type
= hp_work
->type
;
937 bridge
= (struct acpiphp_bridge
*)hp_work
->context
;
939 acpi_scan_lock_acquire();
941 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
944 case ACPI_NOTIFY_BUS_CHECK
:
945 /* bus re-enumerate */
946 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
947 dbg("%s: re-enumerating slots under %s\n", __func__
, objname
);
948 acpiphp_check_bridge(bridge
);
949 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
950 ACPI_UINT32_MAX
, check_sub_bridges
, NULL
, NULL
, NULL
);
953 case ACPI_NOTIFY_DEVICE_CHECK
:
955 dbg("%s: Device check notify on %s\n", __func__
, objname
);
956 acpiphp_check_bridge(bridge
);
959 case ACPI_NOTIFY_DEVICE_WAKE
:
961 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
964 case ACPI_NOTIFY_EJECT_REQUEST
:
965 /* request device eject */
966 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
967 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
968 struct acpiphp_slot
*slot
;
969 slot
= bridge
->func
->slot
;
970 if (!acpiphp_disable_slot(slot
))
971 acpiphp_eject_slot(slot
);
975 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
976 printk(KERN_ERR
"Device %s cannot be configured due"
977 " to a frequency mismatch\n", objname
);
980 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
981 printk(KERN_ERR
"Device %s cannot be configured due"
982 " to a bus mode mismatch\n", objname
);
985 case ACPI_NOTIFY_POWER_FAULT
:
986 printk(KERN_ERR
"Device %s has suffered a power fault\n",
991 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
995 acpi_scan_lock_release();
996 kfree(hp_work
); /* allocated in handle_hotplug_event_bridge */
1001 * handle_hotplug_event_bridge - handle ACPI event on bridges
1002 * @handle: Notify()'ed acpi_handle
1003 * @type: Notify code
1004 * @context: pointer to acpiphp_bridge structure
1006 * Handles ACPI event notification on {host,p2p} bridges.
1008 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
,
1011 struct acpiphp_bridge
*bridge
= context
;
1014 * Currently the code adds all hotplug events to the kacpid_wq
1015 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1016 * The proper way to fix this is to reorganize the code so that
1017 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1018 * For now just re-add this work to the kacpi_hotplug_wq so we
1019 * don't deadlock on hotplug actions.
1022 alloc_acpi_hp_work(handle
, type
, context
, _handle_hotplug_event_bridge
);
1025 static void hotplug_event_func(acpi_handle handle
, u32 type
, void *context
)
1027 struct acpiphp_func
*func
= context
;
1029 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1030 .pointer
= objname
};
1032 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1035 case ACPI_NOTIFY_BUS_CHECK
:
1036 /* bus re-enumerate */
1037 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
1038 acpiphp_enable_slot(func
->slot
);
1041 case ACPI_NOTIFY_DEVICE_CHECK
:
1042 /* device check : re-enumerate from parent bus */
1043 dbg("%s: Device check notify on %s\n", __func__
, objname
);
1044 acpiphp_check_bridge(func
->slot
->bridge
);
1047 case ACPI_NOTIFY_DEVICE_WAKE
:
1049 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1052 case ACPI_NOTIFY_EJECT_REQUEST
:
1053 /* request device eject */
1054 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1055 if (!(acpiphp_disable_slot(func
->slot
)))
1056 acpiphp_eject_slot(func
->slot
);
1060 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1065 static void _handle_hotplug_event_func(struct work_struct
*work
)
1067 struct acpi_hp_work
*hp_work
;
1068 struct acpiphp_func
*func
;
1070 hp_work
= container_of(work
, struct acpi_hp_work
, work
);
1071 func
= hp_work
->context
;
1072 acpi_scan_lock_acquire();
1074 hotplug_event_func(hp_work
->handle
, hp_work
->type
, func
);
1076 acpi_scan_lock_release();
1077 kfree(hp_work
); /* allocated in handle_hotplug_event_func */
1078 put_bridge(func
->slot
->bridge
);
1082 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1083 * @handle: Notify()'ed acpi_handle
1084 * @type: Notify code
1085 * @context: pointer to acpiphp_func structure
1087 * Handles ACPI event notification on slots.
1089 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
,
1092 struct acpiphp_func
*func
= context
;
1095 * Currently the code adds all hotplug events to the kacpid_wq
1096 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1097 * The proper way to fix this is to reorganize the code so that
1098 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1099 * For now just re-add this work to the kacpi_hotplug_wq so we
1100 * don't deadlock on hotplug actions.
1102 get_bridge(func
->slot
->bridge
);
1103 alloc_acpi_hp_work(handle
, type
, context
, _handle_hotplug_event_func
);
1107 * Create hotplug slots for the PCI bus.
1108 * It should always return 0 to avoid skipping following notifiers.
1110 void acpiphp_enumerate_slots(struct pci_bus
*bus
)
1112 struct acpiphp_bridge
*bridge
;
1116 if (acpiphp_disabled
)
1119 handle
= ACPI_HANDLE(bus
->bridge
);
1120 if (!handle
|| detect_ejectable_slots(handle
) <= 0)
1123 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
1124 if (bridge
== NULL
) {
1125 err("out of memory\n");
1129 INIT_LIST_HEAD(&bridge
->slots
);
1130 kref_init(&bridge
->ref
);
1131 bridge
->handle
= handle
;
1132 bridge
->pci_dev
= pci_dev_get(bus
->self
);
1133 bridge
->pci_bus
= bus
;
1136 * Grab a ref to the subordinate PCI bus in case the bus is
1137 * removed via PCI core logical hotplug. The ref pins the bus
1138 * (which we access during module unload).
1140 get_device(&bus
->dev
);
1142 /* must be added to the list prior to calling register_slot */
1143 mutex_lock(&bridge_mutex
);
1144 list_add(&bridge
->list
, &bridge_list
);
1145 mutex_unlock(&bridge_mutex
);
1147 /* register all slot objects under this bridge */
1148 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge
->handle
, 1,
1149 register_slot
, NULL
, bridge
, NULL
);
1150 if (ACPI_FAILURE(status
)) {
1151 acpi_handle_err(bridge
->handle
, "failed to register slots\n");
1155 if (pci_is_root_bus(bridge
->pci_bus
))
1158 /* install notify handler for P2P bridges */
1159 status
= acpi_install_notify_handler(bridge
->handle
, ACPI_SYSTEM_NOTIFY
,
1160 handle_hotplug_event_bridge
,
1162 if (ACPI_FAILURE(status
)) {
1163 acpi_handle_err(bridge
->handle
,
1164 "failed to register notify handler\n");
1168 if (!acpi_has_method(bridge
->handle
, "_EJ0"))
1171 dbg("found ejectable p2p bridge\n");
1172 bridge
->flags
|= BRIDGE_HAS_EJ0
;
1173 bridge
->func
= acpiphp_bridge_handle_to_function(bridge
->handle
);
1175 status
= acpi_remove_notify_handler(bridge
->func
->handle
,
1177 handle_hotplug_event_func
);
1178 if (ACPI_FAILURE(status
))
1179 acpi_handle_err(bridge
->func
->handle
,
1180 "failed to remove notify handler\n");
1185 cleanup_bridge(bridge
);
1189 /* Destroy hotplug slots associated with the PCI bus */
1190 void acpiphp_remove_slots(struct pci_bus
*bus
)
1192 struct acpiphp_bridge
*bridge
, *tmp
;
1194 if (acpiphp_disabled
)
1197 list_for_each_entry_safe(bridge
, tmp
, &bridge_list
, list
)
1198 if (bridge
->pci_bus
== bus
) {
1199 cleanup_bridge(bridge
);
1206 * acpiphp_enable_slot - power on slot
1207 * @slot: ACPI PHP slot
1209 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1213 mutex_lock(&slot
->crit_sect
);
1215 /* wake up all functions */
1216 retval
= power_on_slot(slot
);
1220 if (get_slot_status(slot
) == ACPI_STA_ALL
) {
1221 /* configure all functions */
1222 retval
= enable_device(slot
);
1224 power_off_slot(slot
);
1226 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__
);
1227 power_off_slot(slot
);
1231 mutex_unlock(&slot
->crit_sect
);
1236 * acpiphp_disable_slot - power off slot
1237 * @slot: ACPI PHP slot
1239 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1243 mutex_lock(&slot
->crit_sect
);
1245 /* unconfigure all functions */
1246 retval
= disable_device(slot
);
1250 /* power off all functions */
1251 retval
= power_off_slot(slot
);
1256 mutex_unlock(&slot
->crit_sect
);
1265 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1267 return (slot
->flags
& SLOT_POWEREDON
);
1275 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1279 sta
= get_slot_status(slot
);
1281 return (sta
& ACPI_STA_DEVICE_UI
) ? 0 : 1;
1286 * adapter presence : 1
1289 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1293 sta
= get_slot_status(slot
);
1295 return (sta
== 0) ? 0 : 1;