2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
4 * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5 * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6 * Copyright (C) 2002,2003 NEC Corporation
7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
12 * All rights reserved.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22 * NON INFRINGEMENT. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Send feedback to <kristen.c.accardi@intel.com>
34 * Lifetime rules for pci_dev:
35 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
36 * when the bridge is scanned and it loses a refcount when the bridge
38 * - When a P2P bridge is present, we elevate the refcount on the subordinate
39 * bus. It loses the refcount when the the driver unloads.
42 #include <linux/init.h>
43 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/pci.h>
47 #include <linux/pci_hotplug.h>
48 #include <linux/pci-acpi.h>
49 #include <linux/mutex.h>
50 #include <linux/slab.h>
51 #include <linux/acpi.h>
56 static LIST_HEAD(bridge_list
);
58 #define MY_NAME "acpiphp_glue"
60 static void handle_hotplug_event_bridge (acpi_handle
, u32
, void *);
61 static void acpiphp_sanitize_bus(struct pci_bus
*bus
);
62 static void acpiphp_set_hpp_values(struct pci_bus
*bus
);
63 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
, void *context
);
65 /* callback routine to check for the existence of a pci dock device */
67 is_pci_dock_device(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
69 int *count
= (int *)context
;
71 if (is_dock_device(handle
)) {
73 return AE_CTRL_TERMINATE
;
80 * the _DCK method can do funny things... and sometimes not
83 * TBD - figure out a way to only call fixups for
84 * systems that require them.
86 static int post_dock_fixups(struct notifier_block
*nb
, unsigned long val
,
89 struct acpiphp_func
*func
= container_of(nb
, struct acpiphp_func
, nb
);
90 struct pci_bus
*bus
= func
->slot
->bridge
->pci_bus
;
96 /* fixup bad _DCK function that rewrites
97 * secondary bridge on slot
99 pci_read_config_dword(bus
->self
,
103 if (((buses
>> 8) & 0xff) != bus
->busn_res
.start
) {
104 buses
= (buses
& 0xff000000)
105 | ((unsigned int)(bus
->primary
) << 0)
106 | ((unsigned int)(bus
->busn_res
.start
) << 8)
107 | ((unsigned int)(bus
->busn_res
.end
) << 16);
108 pci_write_config_dword(bus
->self
, PCI_PRIMARY_BUS
, buses
);
114 static const struct acpi_dock_ops acpiphp_dock_ops
= {
115 .handler
= handle_hotplug_event_func
,
118 /* Check whether the PCI device is managed by native PCIe hotplug driver */
119 static bool device_is_managed_by_native_pciehp(struct pci_dev
*pdev
)
123 struct acpi_pci_root
*root
;
125 /* Check whether the PCIe port supports native PCIe hotplug */
126 if (pcie_capability_read_dword(pdev
, PCI_EXP_SLTCAP
, ®32
))
128 if (!(reg32
& PCI_EXP_SLTCAP_HPC
))
132 * Check whether native PCIe hotplug has been enabled for
133 * this PCIe hierarchy.
135 tmp
= acpi_find_root_bridge_handle(pdev
);
138 root
= acpi_pci_find_root(tmp
);
141 if (!(root
->osc_control_set
& OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
))
147 /* callback routine to register each ACPI PCI slot object */
149 register_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
151 struct acpiphp_bridge
*bridge
= (struct acpiphp_bridge
*)context
;
152 struct acpiphp_slot
*slot
;
153 struct acpiphp_func
*newfunc
;
155 acpi_status status
= AE_OK
;
156 unsigned long long adr
, sun
;
157 int device
, function
, retval
;
158 struct pci_bus
*pbus
= bridge
->pci_bus
;
159 struct pci_dev
*pdev
;
161 if (!acpi_pci_check_ejectable(pbus
, handle
) && !is_dock_device(handle
))
164 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &adr
);
165 if (ACPI_FAILURE(status
)) {
166 warn("can't evaluate _ADR (%#x)\n", status
);
170 device
= (adr
>> 16) & 0xffff;
171 function
= adr
& 0xffff;
174 if (pdev
&& device_is_managed_by_native_pciehp(pdev
))
177 newfunc
= kzalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
181 INIT_LIST_HEAD(&newfunc
->sibling
);
182 newfunc
->handle
= handle
;
183 newfunc
->function
= function
;
185 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_EJ0", &tmp
)))
186 newfunc
->flags
= FUNC_HAS_EJ0
;
188 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_STA", &tmp
)))
189 newfunc
->flags
|= FUNC_HAS_STA
;
191 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS0", &tmp
)))
192 newfunc
->flags
|= FUNC_HAS_PS0
;
194 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS3", &tmp
)))
195 newfunc
->flags
|= FUNC_HAS_PS3
;
197 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_DCK", &tmp
)))
198 newfunc
->flags
|= FUNC_HAS_DCK
;
200 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
201 if (ACPI_FAILURE(status
)) {
203 * use the count of the number of slots we've found
204 * for the number of the slot
206 sun
= bridge
->nr_slots
+1;
209 /* search for objects that share the same slot */
210 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
)
211 if (slot
->device
== device
) {
212 if (slot
->sun
!= sun
)
213 warn("sibling found, but _SUN doesn't match!\n");
218 slot
= kzalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
224 slot
->bridge
= bridge
;
225 slot
->device
= device
;
227 INIT_LIST_HEAD(&slot
->funcs
);
228 mutex_init(&slot
->crit_sect
);
230 slot
->next
= bridge
->slots
;
231 bridge
->slots
= slot
;
235 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
236 slot
->sun
, pci_domain_nr(pbus
), pbus
->number
, device
);
237 retval
= acpiphp_register_hotplug_slot(slot
);
239 if (retval
== -EBUSY
)
240 warn("Slot %llu already registered by another "
241 "hotplug driver\n", slot
->sun
);
243 warn("acpiphp_register_hotplug_slot failed "
244 "(err code = 0x%x)\n", retval
);
249 newfunc
->slot
= slot
;
250 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
252 pdev
= pci_get_slot(pbus
, PCI_DEVFN(device
, function
));
254 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
258 if (is_dock_device(handle
)) {
259 /* we don't want to call this device's _EJ0
260 * because we want the dock notify handler
261 * to call it after it calls _DCK
263 newfunc
->flags
&= ~FUNC_HAS_EJ0
;
264 if (register_hotplug_dock_device(handle
,
265 &acpiphp_dock_ops
, newfunc
))
266 dbg("failed to register dock device\n");
268 /* we need to be notified when dock events happen
269 * outside of the hotplug operation, since we may
270 * need to do fixups before we can hotplug.
272 newfunc
->nb
.notifier_call
= post_dock_fixups
;
273 if (register_dock_notifier(&newfunc
->nb
))
274 dbg("failed to register a dock notifier");
277 /* install notify handler */
278 if (!(newfunc
->flags
& FUNC_HAS_DCK
)) {
279 status
= acpi_install_notify_handler(handle
,
281 handle_hotplug_event_func
,
284 if (ACPI_FAILURE(status
))
285 err("failed to register interrupt notify handler\n");
293 bridge
->slots
= slot
->next
;
301 /* see if it's worth looking at this bridge */
302 static int detect_ejectable_slots(acpi_handle handle
)
304 int found
= acpi_pci_detect_ejectable(handle
);
306 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
307 is_pci_dock_device
, NULL
, (void *)&found
, NULL
);
312 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
313 static void init_bridge_misc(struct acpiphp_bridge
*bridge
)
317 /* must be added to the list prior to calling register_slot */
318 list_add(&bridge
->list
, &bridge_list
);
320 /* register all slot objects under this bridge */
321 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge
->handle
, (u32
)1,
322 register_slot
, NULL
, bridge
, NULL
);
323 if (ACPI_FAILURE(status
)) {
324 list_del(&bridge
->list
);
328 /* install notify handler for P2P bridges */
329 if (!pci_is_root_bus(bridge
->pci_bus
)) {
330 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
331 status
= acpi_remove_notify_handler(bridge
->func
->handle
,
333 handle_hotplug_event_func
);
334 if (ACPI_FAILURE(status
))
335 err("failed to remove notify handler\n");
337 status
= acpi_install_notify_handler(bridge
->handle
,
339 handle_hotplug_event_bridge
,
342 if (ACPI_FAILURE(status
)) {
343 err("failed to register interrupt notify handler\n");
349 /* find acpiphp_func from acpiphp_bridge */
350 static struct acpiphp_func
*acpiphp_bridge_handle_to_function(acpi_handle handle
)
352 struct acpiphp_bridge
*bridge
;
353 struct acpiphp_slot
*slot
;
354 struct acpiphp_func
*func
;
356 list_for_each_entry(bridge
, &bridge_list
, list
) {
357 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
358 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
359 if (func
->handle
== handle
)
369 static inline void config_p2p_bridge_flags(struct acpiphp_bridge
*bridge
)
371 acpi_handle dummy_handle
;
372 struct acpiphp_func
*func
;
374 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
375 "_EJ0", &dummy_handle
))) {
376 bridge
->flags
|= BRIDGE_HAS_EJ0
;
378 dbg("found ejectable p2p bridge\n");
380 /* make link between PCI bridge and PCI function */
381 func
= acpiphp_bridge_handle_to_function(bridge
->handle
);
385 func
->bridge
= bridge
;
390 /* allocate and initialize host bridge data structure */
391 static void add_host_bridge(struct acpi_pci_root
*root
)
393 struct acpiphp_bridge
*bridge
;
394 acpi_handle handle
= root
->device
->handle
;
396 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
400 bridge
->handle
= handle
;
402 bridge
->pci_bus
= root
->bus
;
404 init_bridge_misc(bridge
);
408 /* allocate and initialize PCI-to-PCI bridge data structure */
409 static void add_p2p_bridge(acpi_handle
*handle
)
411 struct acpiphp_bridge
*bridge
;
413 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
414 if (bridge
== NULL
) {
415 err("out of memory\n");
419 bridge
->handle
= handle
;
420 config_p2p_bridge_flags(bridge
);
422 bridge
->pci_dev
= acpi_get_pci_dev(handle
);
423 bridge
->pci_bus
= bridge
->pci_dev
->subordinate
;
424 if (!bridge
->pci_bus
) {
425 err("This is not a PCI-to-PCI bridge!\n");
430 * Grab a ref to the subordinate PCI bus in case the bus is
431 * removed via PCI core logical hotplug. The ref pins the bus
432 * (which we access during module unload).
434 get_device(&bridge
->pci_bus
->dev
);
436 init_bridge_misc(bridge
);
439 pci_dev_put(bridge
->pci_dev
);
445 /* callback routine to find P2P bridges */
447 find_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
452 dev
= acpi_get_pci_dev(handle
);
453 if (!dev
|| !dev
->subordinate
)
456 /* check if this bridge has ejectable slots */
457 if ((detect_ejectable_slots(handle
) > 0)) {
458 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev
));
459 add_p2p_bridge(handle
);
462 /* search P2P bridges under this p2p bridge */
463 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
464 find_p2p_bridge
, NULL
, NULL
, NULL
);
465 if (ACPI_FAILURE(status
))
466 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
474 /* find hot-pluggable slots, and then find P2P bridge */
475 static int add_bridge(struct acpi_pci_root
*root
)
478 unsigned long long tmp
;
479 acpi_handle dummy_handle
;
480 acpi_handle handle
= root
->device
->handle
;
482 /* if the bridge doesn't have _STA, we assume it is always there */
483 status
= acpi_get_handle(handle
, "_STA", &dummy_handle
);
484 if (ACPI_SUCCESS(status
)) {
485 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &tmp
);
486 if (ACPI_FAILURE(status
)) {
487 dbg("%s: _STA evaluation failure\n", __func__
);
490 if ((tmp
& ACPI_STA_FUNCTIONING
) == 0)
491 /* don't register this object */
495 /* check if this bridge has ejectable slots */
496 if (detect_ejectable_slots(handle
) > 0) {
497 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
498 add_host_bridge(root
);
501 /* search P2P bridges under this host bridge */
502 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
503 find_p2p_bridge
, NULL
, NULL
, NULL
);
505 if (ACPI_FAILURE(status
))
506 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
511 static struct acpiphp_bridge
*acpiphp_handle_to_bridge(acpi_handle handle
)
513 struct acpiphp_bridge
*bridge
;
515 list_for_each_entry(bridge
, &bridge_list
, list
)
516 if (bridge
->handle
== handle
)
522 static void cleanup_bridge(struct acpiphp_bridge
*bridge
)
524 struct acpiphp_slot
*slot
, *next
;
525 struct acpiphp_func
*func
, *tmp
;
527 acpi_handle handle
= bridge
->handle
;
529 if (!pci_is_root_bus(bridge
->pci_bus
)) {
530 status
= acpi_remove_notify_handler(handle
,
532 handle_hotplug_event_bridge
);
533 if (ACPI_FAILURE(status
))
534 err("failed to remove notify handler\n");
537 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
538 status
= acpi_install_notify_handler(bridge
->func
->handle
,
540 handle_hotplug_event_func
,
542 if (ACPI_FAILURE(status
))
543 err("failed to install interrupt notify handler\n");
546 slot
= bridge
->slots
;
549 list_for_each_entry_safe(func
, tmp
, &slot
->funcs
, sibling
) {
550 if (is_dock_device(func
->handle
)) {
551 unregister_hotplug_dock_device(func
->handle
);
552 unregister_dock_notifier(&func
->nb
);
554 if (!(func
->flags
& FUNC_HAS_DCK
)) {
555 status
= acpi_remove_notify_handler(func
->handle
,
557 handle_hotplug_event_func
);
558 if (ACPI_FAILURE(status
))
559 err("failed to remove notify handler\n");
561 list_del(&func
->sibling
);
564 acpiphp_unregister_hotplug_slot(slot
);
565 list_del(&slot
->funcs
);
571 * Only P2P bridges have a pci_dev
574 put_device(&bridge
->pci_bus
->dev
);
576 pci_dev_put(bridge
->pci_dev
);
577 list_del(&bridge
->list
);
582 cleanup_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
584 struct acpiphp_bridge
*bridge
;
586 /* cleanup p2p bridges under this P2P bridge
587 in a depth-first manner */
588 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
589 cleanup_p2p_bridge
, NULL
, NULL
, NULL
);
591 bridge
= acpiphp_handle_to_bridge(handle
);
593 cleanup_bridge(bridge
);
598 static void remove_bridge(struct acpi_pci_root
*root
)
600 struct acpiphp_bridge
*bridge
;
601 acpi_handle handle
= root
->device
->handle
;
603 /* cleanup p2p bridges under this host bridge
604 in a depth-first manner */
605 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
606 (u32
)1, cleanup_p2p_bridge
, NULL
, NULL
, NULL
);
609 * On root bridges with hotplug slots directly underneath (ie,
610 * no p2p bridge between), we call cleanup_bridge().
612 * The else clause cleans up root bridges that either had no
613 * hotplug slots at all, or had a p2p bridge underneath.
615 bridge
= acpiphp_handle_to_bridge(handle
);
617 cleanup_bridge(bridge
);
620 static int power_on_slot(struct acpiphp_slot
*slot
)
623 struct acpiphp_func
*func
;
626 /* if already enabled, just skip */
627 if (slot
->flags
& SLOT_POWEREDON
)
630 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
631 if (func
->flags
& FUNC_HAS_PS0
) {
632 dbg("%s: executing _PS0\n", __func__
);
633 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
634 if (ACPI_FAILURE(status
)) {
635 warn("%s: _PS0 failed\n", __func__
);
643 /* TBD: evaluate _STA to check if the slot is enabled */
645 slot
->flags
|= SLOT_POWEREDON
;
652 static int power_off_slot(struct acpiphp_slot
*slot
)
655 struct acpiphp_func
*func
;
659 /* if already disabled, just skip */
660 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
663 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
664 if (func
->flags
& FUNC_HAS_PS3
) {
665 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
666 if (ACPI_FAILURE(status
)) {
667 warn("%s: _PS3 failed\n", __func__
);
675 /* TBD: evaluate _STA to check if the slot is disabled */
677 slot
->flags
&= (~SLOT_POWEREDON
);
686 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
687 * @bus: bus to start search with
689 static unsigned char acpiphp_max_busnr(struct pci_bus
*bus
)
691 struct list_head
*tmp
;
692 unsigned char max
, n
;
695 * pci_bus_max_busnr will return the highest
696 * reserved busnr for all these children.
697 * that is equivalent to the bus->subordinate
698 * value. We don't want to use the parent's
699 * bus->subordinate value because it could have
702 max
= bus
->busn_res
.start
;
704 list_for_each(tmp
, &bus
->children
) {
705 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
714 * acpiphp_bus_add - add a new bus to acpi subsystem
715 * @func: acpiphp_func of the bridge
717 static int acpiphp_bus_add(struct acpiphp_func
*func
)
719 struct acpi_device
*device
;
722 if (!acpi_bus_get_device(func
->handle
, &device
)) {
723 dbg("bus exists... trim\n");
724 /* this shouldn't be in here, so remove
725 * the bus then re-add it...
727 acpi_bus_trim(device
);
730 ret_val
= acpi_bus_scan(func
->handle
);
732 ret_val
= acpi_bus_get_device(func
->handle
, &device
);
735 dbg("error adding bus, %x\n", -ret_val
);
742 * acpiphp_bus_trim - trim a bus from acpi subsystem
743 * @handle: handle to acpi namespace
745 static int acpiphp_bus_trim(acpi_handle handle
)
747 struct acpi_device
*device
;
750 retval
= acpi_bus_get_device(handle
, &device
);
752 dbg("acpi_device not found\n");
756 acpi_bus_trim(device
);
760 static void acpiphp_set_acpi_region(struct acpiphp_slot
*slot
)
762 struct acpiphp_func
*func
;
763 union acpi_object params
[2];
764 struct acpi_object_list arg_list
;
766 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
768 arg_list
.pointer
= params
;
769 params
[0].type
= ACPI_TYPE_INTEGER
;
770 params
[0].integer
.value
= ACPI_ADR_SPACE_PCI_CONFIG
;
771 params
[1].type
= ACPI_TYPE_INTEGER
;
772 params
[1].integer
.value
= 1;
773 /* _REG is optional, we don't care about if there is failure */
774 acpi_evaluate_object(func
->handle
, "_REG", &arg_list
, NULL
);
778 static void check_hotplug_bridge(struct acpiphp_slot
*slot
, struct pci_dev
*dev
)
780 struct acpiphp_func
*func
;
782 if (!dev
->subordinate
)
785 /* quirk, or pcie could set it already */
786 if (dev
->is_hotplug_bridge
)
789 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
792 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
793 if (PCI_FUNC(dev
->devfn
) == func
->function
) {
794 /* check if this bridge has ejectable slots */
795 if ((detect_ejectable_slots(func
->handle
) > 0))
796 dev
->is_hotplug_bridge
= 1;
802 * enable_device - enable, configure a slot
803 * @slot: slot to be enabled
805 * This function should be called per *physical slot*,
806 * not per each slot object in ACPI namespace.
808 static int __ref
enable_device(struct acpiphp_slot
*slot
)
811 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
812 struct acpiphp_func
*func
;
817 if (slot
->flags
& SLOT_ENABLED
)
820 list_for_each_entry(func
, &slot
->funcs
, sibling
)
821 acpiphp_bus_add(func
);
823 num
= pci_scan_slot(bus
, PCI_DEVFN(slot
->device
, 0));
825 /* Maybe only part of funcs are added. */
826 dbg("No new device found\n");
830 max
= acpiphp_max_busnr(bus
);
831 for (pass
= 0; pass
< 2; pass
++) {
832 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
833 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
835 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
836 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
837 max
= pci_scan_bridge(bus
, dev
, max
, pass
);
838 if (pass
&& dev
->subordinate
) {
839 check_hotplug_bridge(slot
, dev
);
840 pci_bus_size_bridges(dev
->subordinate
);
846 pci_bus_assign_resources(bus
);
847 acpiphp_sanitize_bus(bus
);
848 acpiphp_set_hpp_values(bus
);
849 acpiphp_set_acpi_region(slot
);
850 pci_enable_bridges(bus
);
852 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
853 /* Assume that newly added devices are powered on already. */
855 dev
->current_state
= PCI_D0
;
858 pci_bus_add_devices(bus
);
860 slot
->flags
|= SLOT_ENABLED
;
861 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
862 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
865 /* Do not set SLOT_ENABLED flag if some funcs
867 slot
->flags
&= (~SLOT_ENABLED
);
871 if (dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
&&
872 dev
->hdr_type
!= PCI_HEADER_TYPE_CARDBUS
) {
877 status
= find_p2p_bridge(func
->handle
, (u32
)1, bus
, NULL
);
878 if (ACPI_FAILURE(status
))
879 warn("find_p2p_bridge failed (error code = 0x%x)\n",
889 /* return first device in slot, acquiring a reference on it */
890 static struct pci_dev
*dev_in_slot(struct acpiphp_slot
*slot
)
892 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
894 struct pci_dev
*ret
= NULL
;
896 down_read(&pci_bus_sem
);
897 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
898 if (PCI_SLOT(dev
->devfn
) == slot
->device
) {
899 ret
= pci_dev_get(dev
);
902 up_read(&pci_bus_sem
);
908 * disable_device - disable a slot
909 * @slot: ACPI PHP slot
911 static int disable_device(struct acpiphp_slot
*slot
)
913 struct acpiphp_func
*func
;
914 struct pci_dev
*pdev
;
915 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
917 /* The slot will be enabled when func 0 is added, so check
918 func 0 before disable the slot. */
919 pdev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
, 0));
924 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
926 /* cleanup p2p bridges under this P2P bridge */
927 cleanup_p2p_bridge(func
->bridge
->handle
,
934 * enable_device() enumerates all functions in this device via
935 * pci_scan_slot(), whether they have associated ACPI hotplug
936 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
939 while ((pdev
= dev_in_slot(slot
))) {
940 pci_stop_and_remove_bus_device(pdev
);
944 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
945 acpiphp_bus_trim(func
->handle
);
948 slot
->flags
&= (~SLOT_ENABLED
);
956 * get_slot_status - get ACPI slot status
957 * @slot: ACPI PHP slot
959 * If a slot has _STA for each function and if any one of them
960 * returned non-zero status, return it.
962 * If a slot doesn't have _STA and if any one of its functions'
963 * configuration space is configured, return 0x0f as a _STA.
965 * Otherwise return 0.
967 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
970 unsigned long long sta
= 0;
972 struct acpiphp_func
*func
;
974 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
975 if (func
->flags
& FUNC_HAS_STA
) {
976 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
977 if (ACPI_SUCCESS(status
) && sta
)
980 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
981 PCI_DEVFN(slot
->device
,
983 PCI_VENDOR_ID
, &dvid
);
984 if (dvid
!= 0xffffffff) {
991 return (unsigned int)sta
;
995 * acpiphp_eject_slot - physically eject the slot
996 * @slot: ACPI PHP slot
998 int acpiphp_eject_slot(struct acpiphp_slot
*slot
)
1001 struct acpiphp_func
*func
;
1002 struct acpi_object_list arg_list
;
1003 union acpi_object arg
;
1005 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
1006 /* We don't want to call _EJ0 on non-existing functions. */
1007 if ((func
->flags
& FUNC_HAS_EJ0
)) {
1008 /* _EJ0 method take one argument */
1010 arg_list
.pointer
= &arg
;
1011 arg
.type
= ACPI_TYPE_INTEGER
;
1012 arg
.integer
.value
= 1;
1014 status
= acpi_evaluate_object(func
->handle
, "_EJ0", &arg_list
, NULL
);
1015 if (ACPI_FAILURE(status
)) {
1016 warn("%s: _EJ0 failed\n", __func__
);
1026 * acpiphp_check_bridge - re-enumerate devices
1027 * @bridge: where to begin re-enumeration
1029 * Iterate over all slots under this bridge and make sure that if a
1030 * card is present they are enabled, and if not they are disabled.
1032 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
1034 struct acpiphp_slot
*slot
;
1036 int enabled
, disabled
;
1038 enabled
= disabled
= 0;
1040 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1041 unsigned int status
= get_slot_status(slot
);
1042 if (slot
->flags
& SLOT_ENABLED
) {
1043 if (status
== ACPI_STA_ALL
)
1045 retval
= acpiphp_disable_slot(slot
);
1047 err("Error occurred in disabling\n");
1050 acpiphp_eject_slot(slot
);
1054 if (status
!= ACPI_STA_ALL
)
1056 retval
= acpiphp_enable_slot(slot
);
1058 err("Error occurred in enabling\n");
1065 dbg("%s: %d enabled, %d disabled\n", __func__
, enabled
, disabled
);
1071 static void acpiphp_set_hpp_values(struct pci_bus
*bus
)
1073 struct pci_dev
*dev
;
1075 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
1076 pci_configure_slot(dev
);
1080 * Remove devices for which we could not assign resources, call
1081 * arch specific code to fix-up the bus
1083 static void acpiphp_sanitize_bus(struct pci_bus
*bus
)
1085 struct pci_dev
*dev
;
1087 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
;
1089 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1090 for (i
=0; i
<PCI_BRIDGE_RESOURCES
; i
++) {
1091 struct resource
*res
= &dev
->resource
[i
];
1092 if ((res
->flags
& type_mask
) && !res
->start
&&
1094 /* Could not assign a required resources
1095 * for this device, remove it */
1096 pci_stop_and_remove_bus_device(dev
);
1104 * ACPI event handlers
1108 check_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1110 struct acpiphp_bridge
*bridge
;
1112 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1113 .pointer
= objname
};
1115 bridge
= acpiphp_handle_to_bridge(handle
);
1117 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1118 dbg("%s: re-enumerating slots under %s\n",
1120 acpiphp_check_bridge(bridge
);
1125 static void _handle_hotplug_event_bridge(struct work_struct
*work
)
1127 struct acpiphp_bridge
*bridge
;
1129 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1130 .pointer
= objname
};
1131 struct acpi_hp_work
*hp_work
;
1135 hp_work
= container_of(work
, struct acpi_hp_work
, work
);
1136 handle
= hp_work
->handle
;
1137 type
= hp_work
->type
;
1138 bridge
= (struct acpiphp_bridge
*)hp_work
->context
;
1140 acpi_scan_lock_acquire();
1142 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1145 case ACPI_NOTIFY_BUS_CHECK
:
1146 /* bus re-enumerate */
1147 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
1148 dbg("%s: re-enumerating slots under %s\n", __func__
, objname
);
1149 acpiphp_check_bridge(bridge
);
1150 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
1151 ACPI_UINT32_MAX
, check_sub_bridges
, NULL
, NULL
, NULL
);
1154 case ACPI_NOTIFY_DEVICE_CHECK
:
1156 dbg("%s: Device check notify on %s\n", __func__
, objname
);
1157 acpiphp_check_bridge(bridge
);
1160 case ACPI_NOTIFY_DEVICE_WAKE
:
1162 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1165 case ACPI_NOTIFY_EJECT_REQUEST
:
1166 /* request device eject */
1167 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1168 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
1169 struct acpiphp_slot
*slot
;
1170 slot
= bridge
->func
->slot
;
1171 if (!acpiphp_disable_slot(slot
))
1172 acpiphp_eject_slot(slot
);
1176 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
1177 printk(KERN_ERR
"Device %s cannot be configured due"
1178 " to a frequency mismatch\n", objname
);
1181 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
1182 printk(KERN_ERR
"Device %s cannot be configured due"
1183 " to a bus mode mismatch\n", objname
);
1186 case ACPI_NOTIFY_POWER_FAULT
:
1187 printk(KERN_ERR
"Device %s has suffered a power fault\n",
1192 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1196 acpi_scan_lock_release();
1197 kfree(hp_work
); /* allocated in handle_hotplug_event_bridge */
1201 * handle_hotplug_event_bridge - handle ACPI event on bridges
1202 * @handle: Notify()'ed acpi_handle
1203 * @type: Notify code
1204 * @context: pointer to acpiphp_bridge structure
1206 * Handles ACPI event notification on {host,p2p} bridges.
1208 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
,
1212 * Currently the code adds all hotplug events to the kacpid_wq
1213 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1214 * The proper way to fix this is to reorganize the code so that
1215 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1216 * For now just re-add this work to the kacpi_hotplug_wq so we
1217 * don't deadlock on hotplug actions.
1219 alloc_acpi_hp_work(handle
, type
, context
, _handle_hotplug_event_bridge
);
1222 static void _handle_hotplug_event_func(struct work_struct
*work
)
1224 struct acpiphp_func
*func
;
1226 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1227 .pointer
= objname
};
1228 struct acpi_hp_work
*hp_work
;
1232 hp_work
= container_of(work
, struct acpi_hp_work
, work
);
1233 handle
= hp_work
->handle
;
1234 type
= hp_work
->type
;
1235 func
= (struct acpiphp_func
*)hp_work
->context
;
1237 acpi_scan_lock_acquire();
1239 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1242 case ACPI_NOTIFY_BUS_CHECK
:
1243 /* bus re-enumerate */
1244 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
1245 acpiphp_enable_slot(func
->slot
);
1248 case ACPI_NOTIFY_DEVICE_CHECK
:
1249 /* device check : re-enumerate from parent bus */
1250 dbg("%s: Device check notify on %s\n", __func__
, objname
);
1251 acpiphp_check_bridge(func
->slot
->bridge
);
1254 case ACPI_NOTIFY_DEVICE_WAKE
:
1256 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1259 case ACPI_NOTIFY_EJECT_REQUEST
:
1260 /* request device eject */
1261 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1262 if (!(acpiphp_disable_slot(func
->slot
)))
1263 acpiphp_eject_slot(func
->slot
);
1267 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1271 acpi_scan_lock_release();
1272 kfree(hp_work
); /* allocated in handle_hotplug_event_func */
1276 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1277 * @handle: Notify()'ed acpi_handle
1278 * @type: Notify code
1279 * @context: pointer to acpiphp_func structure
1281 * Handles ACPI event notification on slots.
1283 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
,
1287 * Currently the code adds all hotplug events to the kacpid_wq
1288 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1289 * The proper way to fix this is to reorganize the code so that
1290 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1291 * For now just re-add this work to the kacpi_hotplug_wq so we
1292 * don't deadlock on hotplug actions.
1294 alloc_acpi_hp_work(handle
, type
, context
, _handle_hotplug_event_func
);
1297 static struct acpi_pci_driver acpi_pci_hp_driver
= {
1299 .remove
= remove_bridge
,
1303 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1305 int __init
acpiphp_glue_init(void)
1307 acpi_pci_register_driver(&acpi_pci_hp_driver
);
1314 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1316 * This function frees all data allocated in acpiphp_glue_init().
1318 void acpiphp_glue_exit(void)
1320 acpi_pci_unregister_driver(&acpi_pci_hp_driver
);
1324 * acpiphp_enable_slot - power on slot
1325 * @slot: ACPI PHP slot
1327 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1331 mutex_lock(&slot
->crit_sect
);
1333 /* wake up all functions */
1334 retval
= power_on_slot(slot
);
1338 if (get_slot_status(slot
) == ACPI_STA_ALL
) {
1339 /* configure all functions */
1340 retval
= enable_device(slot
);
1342 power_off_slot(slot
);
1344 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__
);
1345 power_off_slot(slot
);
1349 mutex_unlock(&slot
->crit_sect
);
1354 * acpiphp_disable_slot - power off slot
1355 * @slot: ACPI PHP slot
1357 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1361 mutex_lock(&slot
->crit_sect
);
1363 /* unconfigure all functions */
1364 retval
= disable_device(slot
);
1368 /* power off all functions */
1369 retval
= power_off_slot(slot
);
1374 mutex_unlock(&slot
->crit_sect
);
1383 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1385 return (slot
->flags
& SLOT_POWEREDON
);
1393 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1397 sta
= get_slot_status(slot
);
1399 return (sta
& ACPI_STA_SHOW_IN_UI
) ? 0 : 1;
1404 * adapter presence : 1
1407 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1411 sta
= get_slot_status(slot
);
1413 return (sta
== 0) ? 0 : 1;