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 handle_hotplug_event_func(acpi_handle handle
, u32 type
, void *context
);
65 static void free_bridge(struct kref
*kref
);
67 /* callback routine to check for the existence of a pci dock device */
69 is_pci_dock_device(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
71 int *count
= (int *)context
;
73 if (is_dock_device(handle
)) {
75 return AE_CTRL_TERMINATE
;
81 static inline void get_bridge(struct acpiphp_bridge
*bridge
)
83 kref_get(&bridge
->ref
);
86 static inline void put_bridge(struct acpiphp_bridge
*bridge
)
88 kref_put(&bridge
->ref
, free_bridge
);
91 static void free_bridge(struct kref
*kref
)
93 struct acpiphp_bridge
*bridge
;
94 struct acpiphp_slot
*slot
, *next
;
95 struct acpiphp_func
*func
, *tmp
;
97 bridge
= container_of(kref
, struct acpiphp_bridge
, ref
);
99 list_for_each_entry_safe(slot
, next
, &bridge
->slots
, node
) {
100 list_for_each_entry_safe(func
, tmp
, &slot
->funcs
, sibling
) {
106 /* Release reference acquired by acpiphp_bridge_handle_to_function() */
107 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
)
108 put_bridge(bridge
->func
->slot
->bridge
);
109 put_device(&bridge
->pci_bus
->dev
);
110 pci_dev_put(bridge
->pci_dev
);
115 * the _DCK method can do funny things... and sometimes not
118 * TBD - figure out a way to only call fixups for
119 * systems that require them.
121 static int post_dock_fixups(struct notifier_block
*nb
, unsigned long val
,
124 struct acpiphp_func
*func
= container_of(nb
, struct acpiphp_func
, nb
);
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
);
149 static const struct acpi_dock_ops acpiphp_dock_ops
= {
150 .handler
= handle_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 /* callback routine to register each ACPI PCI slot object */
184 register_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
186 struct acpiphp_bridge
*bridge
= (struct acpiphp_bridge
*)context
;
187 struct acpiphp_slot
*slot
;
188 struct acpiphp_func
*newfunc
;
190 acpi_status status
= AE_OK
;
191 unsigned long long adr
, sun
;
192 int device
, function
, retval
, found
= 0;
193 struct pci_bus
*pbus
= bridge
->pci_bus
;
194 struct pci_dev
*pdev
;
197 if (!acpi_pci_check_ejectable(pbus
, handle
) && !is_dock_device(handle
))
200 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &adr
);
201 if (ACPI_FAILURE(status
)) {
202 warn("can't evaluate _ADR (%#x)\n", status
);
206 device
= (adr
>> 16) & 0xffff;
207 function
= adr
& 0xffff;
209 pdev
= bridge
->pci_dev
;
210 if (pdev
&& device_is_managed_by_native_pciehp(pdev
))
213 newfunc
= kzalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
217 newfunc
->handle
= handle
;
218 newfunc
->function
= function
;
220 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_EJ0", &tmp
)))
221 newfunc
->flags
= FUNC_HAS_EJ0
;
223 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_STA", &tmp
)))
224 newfunc
->flags
|= FUNC_HAS_STA
;
226 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS0", &tmp
)))
227 newfunc
->flags
|= FUNC_HAS_PS0
;
229 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS3", &tmp
)))
230 newfunc
->flags
|= FUNC_HAS_PS3
;
232 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_DCK", &tmp
)))
233 newfunc
->flags
|= FUNC_HAS_DCK
;
235 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
236 if (ACPI_FAILURE(status
)) {
238 * use the count of the number of slots we've found
239 * for the number of the slot
241 sun
= bridge
->nr_slots
+1;
244 /* search for objects that share the same slot */
245 list_for_each_entry(slot
, &bridge
->slots
, node
)
246 if (slot
->device
== device
) {
247 if (slot
->sun
!= sun
)
248 warn("sibling found, but _SUN doesn't match!\n");
254 slot
= kzalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
260 slot
->bridge
= bridge
;
261 slot
->device
= device
;
263 INIT_LIST_HEAD(&slot
->funcs
);
264 mutex_init(&slot
->crit_sect
);
266 mutex_lock(&bridge_mutex
);
267 list_add_tail(&slot
->node
, &bridge
->slots
);
268 mutex_unlock(&bridge_mutex
);
271 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
272 slot
->sun
, pci_domain_nr(pbus
), pbus
->number
, device
);
273 retval
= acpiphp_register_hotplug_slot(slot
);
275 if (retval
== -EBUSY
)
276 warn("Slot %llu already registered by another "
277 "hotplug driver\n", slot
->sun
);
279 warn("acpiphp_register_hotplug_slot failed "
280 "(err code = 0x%x)\n", retval
);
285 newfunc
->slot
= slot
;
286 mutex_lock(&bridge_mutex
);
287 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
288 mutex_unlock(&bridge_mutex
);
290 if (pci_bus_read_dev_vendor_id(pbus
, PCI_DEVFN(device
, function
),
292 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
294 if (is_dock_device(handle
)) {
295 /* we don't want to call this device's _EJ0
296 * because we want the dock notify handler
297 * to call it after it calls _DCK
299 newfunc
->flags
&= ~FUNC_HAS_EJ0
;
300 if (register_hotplug_dock_device(handle
,
301 &acpiphp_dock_ops
, newfunc
))
302 dbg("failed to register dock device\n");
304 /* we need to be notified when dock events happen
305 * outside of the hotplug operation, since we may
306 * need to do fixups before we can hotplug.
308 newfunc
->nb
.notifier_call
= post_dock_fixups
;
309 if (register_dock_notifier(&newfunc
->nb
))
310 dbg("failed to register a dock notifier");
313 /* install notify handler */
314 if (!(newfunc
->flags
& FUNC_HAS_DCK
)) {
315 status
= acpi_install_notify_handler(handle
,
317 handle_hotplug_event_func
,
320 if (ACPI_FAILURE(status
))
321 err("failed to register interrupt notify handler\n");
329 mutex_lock(&bridge_mutex
);
330 list_del(&slot
->node
);
331 mutex_unlock(&bridge_mutex
);
339 /* see if it's worth looking at this bridge */
340 static int detect_ejectable_slots(acpi_handle handle
)
342 int found
= acpi_pci_detect_ejectable(handle
);
344 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
345 is_pci_dock_device
, NULL
, (void *)&found
, NULL
);
350 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
351 static void init_bridge_misc(struct acpiphp_bridge
*bridge
)
355 /* must be added to the list prior to calling register_slot */
356 mutex_lock(&bridge_mutex
);
357 list_add(&bridge
->list
, &bridge_list
);
358 mutex_unlock(&bridge_mutex
);
360 /* register all slot objects under this bridge */
361 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge
->handle
, (u32
)1,
362 register_slot
, NULL
, bridge
, NULL
);
363 if (ACPI_FAILURE(status
)) {
364 mutex_lock(&bridge_mutex
);
365 list_del(&bridge
->list
);
366 mutex_unlock(&bridge_mutex
);
370 /* install notify handler for P2P bridges */
371 if (!pci_is_root_bus(bridge
->pci_bus
)) {
372 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
373 status
= acpi_remove_notify_handler(bridge
->func
->handle
,
375 handle_hotplug_event_func
);
376 if (ACPI_FAILURE(status
))
377 err("failed to remove notify handler\n");
379 status
= acpi_install_notify_handler(bridge
->handle
,
381 handle_hotplug_event_bridge
,
384 if (ACPI_FAILURE(status
)) {
385 err("failed to register interrupt notify handler\n");
391 /* find acpiphp_func from acpiphp_bridge */
392 static struct acpiphp_func
*acpiphp_bridge_handle_to_function(acpi_handle handle
)
394 struct acpiphp_bridge
*bridge
;
395 struct acpiphp_slot
*slot
;
396 struct acpiphp_func
*func
= NULL
;
398 mutex_lock(&bridge_mutex
);
399 list_for_each_entry(bridge
, &bridge_list
, list
) {
400 list_for_each_entry(slot
, &bridge
->slots
, node
) {
401 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
402 if (func
->handle
== handle
) {
403 get_bridge(func
->slot
->bridge
);
404 mutex_unlock(&bridge_mutex
);
410 mutex_unlock(&bridge_mutex
);
416 static struct acpiphp_bridge
*acpiphp_handle_to_bridge(acpi_handle handle
)
418 struct acpiphp_bridge
*bridge
;
420 mutex_lock(&bridge_mutex
);
421 list_for_each_entry(bridge
, &bridge_list
, list
)
422 if (bridge
->handle
== handle
) {
424 mutex_unlock(&bridge_mutex
);
427 mutex_unlock(&bridge_mutex
);
432 static void cleanup_bridge(struct acpiphp_bridge
*bridge
)
434 struct acpiphp_slot
*slot
;
435 struct acpiphp_func
*func
;
437 acpi_handle handle
= bridge
->handle
;
439 if (!pci_is_root_bus(bridge
->pci_bus
)) {
440 status
= acpi_remove_notify_handler(handle
,
442 handle_hotplug_event_bridge
);
443 if (ACPI_FAILURE(status
))
444 err("failed to remove notify handler\n");
447 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
448 status
= acpi_install_notify_handler(bridge
->func
->handle
,
450 handle_hotplug_event_func
,
452 if (ACPI_FAILURE(status
))
453 err("failed to install interrupt notify handler\n");
456 list_for_each_entry(slot
, &bridge
->slots
, node
) {
457 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
458 if (is_dock_device(func
->handle
)) {
459 unregister_hotplug_dock_device(func
->handle
);
460 unregister_dock_notifier(&func
->nb
);
462 if (!(func
->flags
& FUNC_HAS_DCK
)) {
463 status
= acpi_remove_notify_handler(func
->handle
,
465 handle_hotplug_event_func
);
466 if (ACPI_FAILURE(status
))
467 err("failed to remove notify handler\n");
470 acpiphp_unregister_hotplug_slot(slot
);
473 mutex_lock(&bridge_mutex
);
474 list_del(&bridge
->list
);
475 mutex_unlock(&bridge_mutex
);
478 static int power_on_slot(struct acpiphp_slot
*slot
)
481 struct acpiphp_func
*func
;
484 /* if already enabled, just skip */
485 if (slot
->flags
& SLOT_POWEREDON
)
488 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
489 if (func
->flags
& FUNC_HAS_PS0
) {
490 dbg("%s: executing _PS0\n", __func__
);
491 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
492 if (ACPI_FAILURE(status
)) {
493 warn("%s: _PS0 failed\n", __func__
);
501 /* TBD: evaluate _STA to check if the slot is enabled */
503 slot
->flags
|= SLOT_POWEREDON
;
510 static int power_off_slot(struct acpiphp_slot
*slot
)
513 struct acpiphp_func
*func
;
517 /* if already disabled, just skip */
518 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
521 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
522 if (func
->flags
& FUNC_HAS_PS3
) {
523 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
524 if (ACPI_FAILURE(status
)) {
525 warn("%s: _PS3 failed\n", __func__
);
533 /* TBD: evaluate _STA to check if the slot is disabled */
535 slot
->flags
&= (~SLOT_POWEREDON
);
544 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
545 * @bus: bus to start search with
547 static unsigned char acpiphp_max_busnr(struct pci_bus
*bus
)
549 struct list_head
*tmp
;
550 unsigned char max
, n
;
553 * pci_bus_max_busnr will return the highest
554 * reserved busnr for all these children.
555 * that is equivalent to the bus->subordinate
556 * value. We don't want to use the parent's
557 * bus->subordinate value because it could have
560 max
= bus
->busn_res
.start
;
562 list_for_each(tmp
, &bus
->children
) {
563 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
572 * acpiphp_bus_add - add a new bus to acpi subsystem
573 * @func: acpiphp_func of the bridge
575 static int acpiphp_bus_add(struct acpiphp_func
*func
)
577 struct acpi_device
*device
;
580 if (!acpi_bus_get_device(func
->handle
, &device
)) {
581 dbg("bus exists... trim\n");
582 /* this shouldn't be in here, so remove
583 * the bus then re-add it...
585 acpi_bus_trim(device
);
588 ret_val
= acpi_bus_scan(func
->handle
);
590 ret_val
= acpi_bus_get_device(func
->handle
, &device
);
593 dbg("error adding bus, %x\n", -ret_val
);
600 * acpiphp_bus_trim - trim a bus from acpi subsystem
601 * @handle: handle to acpi namespace
603 static int acpiphp_bus_trim(acpi_handle handle
)
605 struct acpi_device
*device
;
608 retval
= acpi_bus_get_device(handle
, &device
);
610 dbg("acpi_device not found\n");
614 acpi_bus_trim(device
);
618 static void acpiphp_set_acpi_region(struct acpiphp_slot
*slot
)
620 struct acpiphp_func
*func
;
621 union acpi_object params
[2];
622 struct acpi_object_list arg_list
;
624 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
626 arg_list
.pointer
= params
;
627 params
[0].type
= ACPI_TYPE_INTEGER
;
628 params
[0].integer
.value
= ACPI_ADR_SPACE_PCI_CONFIG
;
629 params
[1].type
= ACPI_TYPE_INTEGER
;
630 params
[1].integer
.value
= 1;
631 /* _REG is optional, we don't care about if there is failure */
632 acpi_evaluate_object(func
->handle
, "_REG", &arg_list
, NULL
);
636 static void check_hotplug_bridge(struct acpiphp_slot
*slot
, struct pci_dev
*dev
)
638 struct acpiphp_func
*func
;
640 if (!dev
->subordinate
)
643 /* quirk, or pcie could set it already */
644 if (dev
->is_hotplug_bridge
)
647 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
650 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
651 if (PCI_FUNC(dev
->devfn
) == func
->function
) {
652 /* check if this bridge has ejectable slots */
653 if ((detect_ejectable_slots(func
->handle
) > 0))
654 dev
->is_hotplug_bridge
= 1;
661 * enable_device - enable, configure a slot
662 * @slot: slot to be enabled
664 * This function should be called per *physical slot*,
665 * not per each slot object in ACPI namespace.
667 static int __ref
enable_device(struct acpiphp_slot
*slot
)
670 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
671 struct acpiphp_func
*func
;
674 if (slot
->flags
& SLOT_ENABLED
)
677 list_for_each_entry(func
, &slot
->funcs
, sibling
)
678 acpiphp_bus_add(func
);
680 num
= pci_scan_slot(bus
, PCI_DEVFN(slot
->device
, 0));
682 /* Maybe only part of funcs are added. */
683 dbg("No new device found\n");
687 max
= acpiphp_max_busnr(bus
);
688 for (pass
= 0; pass
< 2; pass
++) {
689 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
690 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
692 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
693 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
694 max
= pci_scan_bridge(bus
, dev
, max
, pass
);
695 if (pass
&& dev
->subordinate
) {
696 check_hotplug_bridge(slot
, dev
);
697 pci_bus_size_bridges(dev
->subordinate
);
703 pci_bus_assign_resources(bus
);
704 acpiphp_sanitize_bus(bus
);
705 acpiphp_set_hpp_values(bus
);
706 acpiphp_set_acpi_region(slot
);
707 pci_enable_bridges(bus
);
709 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
710 /* Assume that newly added devices are powered on already. */
712 dev
->current_state
= PCI_D0
;
715 pci_bus_add_devices(bus
);
717 slot
->flags
|= SLOT_ENABLED
;
718 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
719 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
722 /* Do not set SLOT_ENABLED flag if some funcs
724 slot
->flags
&= (~SLOT_ENABLED
);
734 /* return first device in slot, acquiring a reference on it */
735 static struct pci_dev
*dev_in_slot(struct acpiphp_slot
*slot
)
737 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
739 struct pci_dev
*ret
= NULL
;
741 down_read(&pci_bus_sem
);
742 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
743 if (PCI_SLOT(dev
->devfn
) == slot
->device
) {
744 ret
= pci_dev_get(dev
);
747 up_read(&pci_bus_sem
);
753 * disable_device - disable a slot
754 * @slot: ACPI PHP slot
756 static int disable_device(struct acpiphp_slot
*slot
)
758 struct acpiphp_func
*func
;
759 struct pci_dev
*pdev
;
762 * enable_device() enumerates all functions in this device via
763 * pci_scan_slot(), whether they have associated ACPI hotplug
764 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
767 while ((pdev
= dev_in_slot(slot
))) {
768 pci_stop_and_remove_bus_device(pdev
);
772 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
773 acpiphp_bus_trim(func
->handle
);
776 slot
->flags
&= (~SLOT_ENABLED
);
783 * get_slot_status - get ACPI slot status
784 * @slot: ACPI PHP slot
786 * If a slot has _STA for each function and if any one of them
787 * returned non-zero status, return it.
789 * If a slot doesn't have _STA and if any one of its functions'
790 * configuration space is configured, return 0x0f as a _STA.
792 * Otherwise return 0.
794 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
797 unsigned long long sta
= 0;
799 struct acpiphp_func
*func
;
801 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
802 if (func
->flags
& FUNC_HAS_STA
) {
803 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
804 if (ACPI_SUCCESS(status
) && sta
)
807 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
808 PCI_DEVFN(slot
->device
,
810 PCI_VENDOR_ID
, &dvid
);
811 if (dvid
!= 0xffffffff) {
818 return (unsigned int)sta
;
822 * acpiphp_eject_slot - physically eject the slot
823 * @slot: ACPI PHP slot
825 int acpiphp_eject_slot(struct acpiphp_slot
*slot
)
828 struct acpiphp_func
*func
;
829 struct acpi_object_list arg_list
;
830 union acpi_object arg
;
832 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
833 /* We don't want to call _EJ0 on non-existing functions. */
834 if ((func
->flags
& FUNC_HAS_EJ0
)) {
835 /* _EJ0 method take one argument */
837 arg_list
.pointer
= &arg
;
838 arg
.type
= ACPI_TYPE_INTEGER
;
839 arg
.integer
.value
= 1;
841 status
= acpi_evaluate_object(func
->handle
, "_EJ0", &arg_list
, NULL
);
842 if (ACPI_FAILURE(status
)) {
843 warn("%s: _EJ0 failed\n", __func__
);
853 * acpiphp_check_bridge - re-enumerate devices
854 * @bridge: where to begin re-enumeration
856 * Iterate over all slots under this bridge and make sure that if a
857 * card is present they are enabled, and if not they are disabled.
859 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
861 struct acpiphp_slot
*slot
;
863 int enabled
, disabled
;
865 enabled
= disabled
= 0;
867 list_for_each_entry(slot
, &bridge
->slots
, node
) {
868 unsigned int status
= get_slot_status(slot
);
869 if (slot
->flags
& SLOT_ENABLED
) {
870 if (status
== ACPI_STA_ALL
)
872 retval
= acpiphp_disable_slot(slot
);
874 err("Error occurred in disabling\n");
877 acpiphp_eject_slot(slot
);
881 if (status
!= ACPI_STA_ALL
)
883 retval
= acpiphp_enable_slot(slot
);
885 err("Error occurred in enabling\n");
892 dbg("%s: %d enabled, %d disabled\n", __func__
, enabled
, disabled
);
898 static void acpiphp_set_hpp_values(struct pci_bus
*bus
)
902 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
903 pci_configure_slot(dev
);
907 * Remove devices for which we could not assign resources, call
908 * arch specific code to fix-up the bus
910 static void acpiphp_sanitize_bus(struct pci_bus
*bus
)
912 struct pci_dev
*dev
, *tmp
;
914 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
;
916 list_for_each_entry_safe(dev
, tmp
, &bus
->devices
, bus_list
) {
917 for (i
=0; i
<PCI_BRIDGE_RESOURCES
; i
++) {
918 struct resource
*res
= &dev
->resource
[i
];
919 if ((res
->flags
& type_mask
) && !res
->start
&&
921 /* Could not assign a required resources
922 * for this device, remove it */
923 pci_stop_and_remove_bus_device(dev
);
931 * ACPI event handlers
935 check_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
937 struct acpiphp_bridge
*bridge
;
939 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
940 .pointer
= objname
};
942 bridge
= acpiphp_handle_to_bridge(handle
);
944 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
945 dbg("%s: re-enumerating slots under %s\n",
947 acpiphp_check_bridge(bridge
);
953 void acpiphp_check_host_bridge(acpi_handle handle
)
955 struct acpiphp_bridge
*bridge
;
957 bridge
= acpiphp_handle_to_bridge(handle
);
959 acpiphp_check_bridge(bridge
);
963 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
964 ACPI_UINT32_MAX
, check_sub_bridges
, NULL
, NULL
, NULL
);
967 static void _handle_hotplug_event_bridge(struct work_struct
*work
)
969 struct acpiphp_bridge
*bridge
;
971 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
972 .pointer
= objname
};
973 struct acpi_hp_work
*hp_work
;
977 hp_work
= container_of(work
, struct acpi_hp_work
, work
);
978 handle
= hp_work
->handle
;
979 type
= hp_work
->type
;
980 bridge
= (struct acpiphp_bridge
*)hp_work
->context
;
982 acpi_scan_lock_acquire();
984 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
987 case ACPI_NOTIFY_BUS_CHECK
:
988 /* bus re-enumerate */
989 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
990 dbg("%s: re-enumerating slots under %s\n", __func__
, objname
);
991 acpiphp_check_bridge(bridge
);
992 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
993 ACPI_UINT32_MAX
, check_sub_bridges
, NULL
, NULL
, NULL
);
996 case ACPI_NOTIFY_DEVICE_CHECK
:
998 dbg("%s: Device check notify on %s\n", __func__
, objname
);
999 acpiphp_check_bridge(bridge
);
1002 case ACPI_NOTIFY_DEVICE_WAKE
:
1004 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1007 case ACPI_NOTIFY_EJECT_REQUEST
:
1008 /* request device eject */
1009 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1010 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
1011 struct acpiphp_slot
*slot
;
1012 slot
= bridge
->func
->slot
;
1013 if (!acpiphp_disable_slot(slot
))
1014 acpiphp_eject_slot(slot
);
1018 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
1019 printk(KERN_ERR
"Device %s cannot be configured due"
1020 " to a frequency mismatch\n", objname
);
1023 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
1024 printk(KERN_ERR
"Device %s cannot be configured due"
1025 " to a bus mode mismatch\n", objname
);
1028 case ACPI_NOTIFY_POWER_FAULT
:
1029 printk(KERN_ERR
"Device %s has suffered a power fault\n",
1034 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1038 acpi_scan_lock_release();
1039 kfree(hp_work
); /* allocated in handle_hotplug_event_bridge */
1044 * handle_hotplug_event_bridge - handle ACPI event on bridges
1045 * @handle: Notify()'ed acpi_handle
1046 * @type: Notify code
1047 * @context: pointer to acpiphp_bridge structure
1049 * Handles ACPI event notification on {host,p2p} bridges.
1051 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
,
1054 struct acpiphp_bridge
*bridge
= context
;
1057 * Currently the code adds all hotplug events to the kacpid_wq
1058 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1059 * The proper way to fix this is to reorganize the code so that
1060 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1061 * For now just re-add this work to the kacpi_hotplug_wq so we
1062 * don't deadlock on hotplug actions.
1065 alloc_acpi_hp_work(handle
, type
, context
, _handle_hotplug_event_bridge
);
1068 static void _handle_hotplug_event_func(struct work_struct
*work
)
1070 struct acpiphp_func
*func
;
1072 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1073 .pointer
= objname
};
1074 struct acpi_hp_work
*hp_work
;
1078 hp_work
= container_of(work
, struct acpi_hp_work
, work
);
1079 handle
= hp_work
->handle
;
1080 type
= hp_work
->type
;
1081 func
= (struct acpiphp_func
*)hp_work
->context
;
1083 acpi_scan_lock_acquire();
1085 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1088 case ACPI_NOTIFY_BUS_CHECK
:
1089 /* bus re-enumerate */
1090 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
1091 acpiphp_enable_slot(func
->slot
);
1094 case ACPI_NOTIFY_DEVICE_CHECK
:
1095 /* device check : re-enumerate from parent bus */
1096 dbg("%s: Device check notify on %s\n", __func__
, objname
);
1097 acpiphp_check_bridge(func
->slot
->bridge
);
1100 case ACPI_NOTIFY_DEVICE_WAKE
:
1102 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1105 case ACPI_NOTIFY_EJECT_REQUEST
:
1106 /* request device eject */
1107 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1108 if (!(acpiphp_disable_slot(func
->slot
)))
1109 acpiphp_eject_slot(func
->slot
);
1113 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1117 acpi_scan_lock_release();
1118 kfree(hp_work
); /* allocated in handle_hotplug_event_func */
1119 put_bridge(func
->slot
->bridge
);
1123 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1124 * @handle: Notify()'ed acpi_handle
1125 * @type: Notify code
1126 * @context: pointer to acpiphp_func structure
1128 * Handles ACPI event notification on slots.
1130 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
,
1133 struct acpiphp_func
*func
= context
;
1136 * Currently the code adds all hotplug events to the kacpid_wq
1137 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1138 * The proper way to fix this is to reorganize the code so that
1139 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1140 * For now just re-add this work to the kacpi_hotplug_wq so we
1141 * don't deadlock on hotplug actions.
1143 get_bridge(func
->slot
->bridge
);
1144 alloc_acpi_hp_work(handle
, type
, context
, _handle_hotplug_event_func
);
1148 * Create hotplug slots for the PCI bus.
1149 * It should always return 0 to avoid skipping following notifiers.
1151 void acpiphp_enumerate_slots(struct pci_bus
*bus
, acpi_handle handle
)
1153 acpi_handle dummy_handle
;
1154 struct acpiphp_bridge
*bridge
;
1156 if (acpiphp_disabled
)
1159 if (detect_ejectable_slots(handle
) <= 0)
1162 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
1163 if (bridge
== NULL
) {
1164 err("out of memory\n");
1168 INIT_LIST_HEAD(&bridge
->slots
);
1169 kref_init(&bridge
->ref
);
1170 bridge
->handle
= handle
;
1171 bridge
->pci_dev
= pci_dev_get(bus
->self
);
1172 bridge
->pci_bus
= bus
;
1175 * Grab a ref to the subordinate PCI bus in case the bus is
1176 * removed via PCI core logical hotplug. The ref pins the bus
1177 * (which we access during module unload).
1179 get_device(&bus
->dev
);
1181 if (!pci_is_root_bus(bridge
->pci_bus
) &&
1182 ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
1183 "_EJ0", &dummy_handle
))) {
1184 dbg("found ejectable p2p bridge\n");
1185 bridge
->flags
|= BRIDGE_HAS_EJ0
;
1186 bridge
->func
= acpiphp_bridge_handle_to_function(handle
);
1189 init_bridge_misc(bridge
);
1192 /* Destroy hotplug slots associated with the PCI bus */
1193 void acpiphp_remove_slots(struct pci_bus
*bus
)
1195 struct acpiphp_bridge
*bridge
, *tmp
;
1197 if (acpiphp_disabled
)
1200 list_for_each_entry_safe(bridge
, tmp
, &bridge_list
, list
)
1201 if (bridge
->pci_bus
== bus
) {
1202 cleanup_bridge(bridge
);
1209 * acpiphp_enable_slot - power on slot
1210 * @slot: ACPI PHP slot
1212 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1216 mutex_lock(&slot
->crit_sect
);
1218 /* wake up all functions */
1219 retval
= power_on_slot(slot
);
1223 if (get_slot_status(slot
) == ACPI_STA_ALL
) {
1224 /* configure all functions */
1225 retval
= enable_device(slot
);
1227 power_off_slot(slot
);
1229 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__
);
1230 power_off_slot(slot
);
1234 mutex_unlock(&slot
->crit_sect
);
1239 * acpiphp_disable_slot - power off slot
1240 * @slot: ACPI PHP slot
1242 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1246 mutex_lock(&slot
->crit_sect
);
1248 /* unconfigure all functions */
1249 retval
= disable_device(slot
);
1253 /* power off all functions */
1254 retval
= power_off_slot(slot
);
1259 mutex_unlock(&slot
->crit_sect
);
1268 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1270 return (slot
->flags
& SLOT_POWEREDON
);
1278 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1282 sta
= get_slot_status(slot
);
1284 return (sta
& ACPI_STA_DEVICE_UI
) ? 0 : 1;
1289 * adapter presence : 1
1292 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1296 sta
= get_slot_status(slot
);
1298 return (sta
== 0) ? 0 : 1;