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 /* callback routine to register each ACPI PCI slot object */
120 register_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
122 struct acpiphp_bridge
*bridge
= (struct acpiphp_bridge
*)context
;
123 struct acpiphp_slot
*slot
;
124 struct acpiphp_func
*newfunc
;
126 acpi_status status
= AE_OK
;
127 unsigned long long adr
, sun
;
128 int device
, function
, retval
;
129 struct pci_bus
*pbus
= bridge
->pci_bus
;
130 struct pci_dev
*pdev
;
132 if (!acpi_pci_check_ejectable(pbus
, handle
) && !is_dock_device(handle
))
135 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &adr
);
136 if (ACPI_FAILURE(status
)) {
137 warn("can't evaluate _ADR (%#x)\n", status
);
141 device
= (adr
>> 16) & 0xffff;
142 function
= adr
& 0xffff;
145 if (pdev
&& pci_is_pcie(pdev
)) {
146 tmp
= acpi_find_root_bridge_handle(pdev
);
148 struct acpi_pci_root
*root
= acpi_pci_find_root(tmp
);
150 if (root
&& (root
->osc_control_set
&
151 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
))
156 newfunc
= kzalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
160 INIT_LIST_HEAD(&newfunc
->sibling
);
161 newfunc
->handle
= handle
;
162 newfunc
->function
= function
;
164 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_EJ0", &tmp
)))
165 newfunc
->flags
= FUNC_HAS_EJ0
;
167 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_STA", &tmp
)))
168 newfunc
->flags
|= FUNC_HAS_STA
;
170 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS0", &tmp
)))
171 newfunc
->flags
|= FUNC_HAS_PS0
;
173 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS3", &tmp
)))
174 newfunc
->flags
|= FUNC_HAS_PS3
;
176 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_DCK", &tmp
)))
177 newfunc
->flags
|= FUNC_HAS_DCK
;
179 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
180 if (ACPI_FAILURE(status
)) {
182 * use the count of the number of slots we've found
183 * for the number of the slot
185 sun
= bridge
->nr_slots
+1;
188 /* search for objects that share the same slot */
189 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
)
190 if (slot
->device
== device
) {
191 if (slot
->sun
!= sun
)
192 warn("sibling found, but _SUN doesn't match!\n");
197 slot
= kzalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
203 slot
->bridge
= bridge
;
204 slot
->device
= device
;
206 INIT_LIST_HEAD(&slot
->funcs
);
207 mutex_init(&slot
->crit_sect
);
209 slot
->next
= bridge
->slots
;
210 bridge
->slots
= slot
;
214 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
215 slot
->sun
, pci_domain_nr(pbus
), pbus
->number
, device
);
216 retval
= acpiphp_register_hotplug_slot(slot
);
218 if (retval
== -EBUSY
)
219 warn("Slot %llu already registered by another "
220 "hotplug driver\n", slot
->sun
);
222 warn("acpiphp_register_hotplug_slot failed "
223 "(err code = 0x%x)\n", retval
);
228 newfunc
->slot
= slot
;
229 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
231 pdev
= pci_get_slot(pbus
, PCI_DEVFN(device
, function
));
233 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
237 if (is_dock_device(handle
)) {
238 /* we don't want to call this device's _EJ0
239 * because we want the dock notify handler
240 * to call it after it calls _DCK
242 newfunc
->flags
&= ~FUNC_HAS_EJ0
;
243 if (register_hotplug_dock_device(handle
,
244 &acpiphp_dock_ops
, newfunc
))
245 dbg("failed to register dock device\n");
247 /* we need to be notified when dock events happen
248 * outside of the hotplug operation, since we may
249 * need to do fixups before we can hotplug.
251 newfunc
->nb
.notifier_call
= post_dock_fixups
;
252 if (register_dock_notifier(&newfunc
->nb
))
253 dbg("failed to register a dock notifier");
256 /* install notify handler */
257 if (!(newfunc
->flags
& FUNC_HAS_DCK
)) {
258 status
= acpi_install_notify_handler(handle
,
260 handle_hotplug_event_func
,
263 if (ACPI_FAILURE(status
))
264 err("failed to register interrupt notify handler\n");
272 bridge
->slots
= slot
->next
;
280 /* see if it's worth looking at this bridge */
281 static int detect_ejectable_slots(acpi_handle handle
)
283 int found
= acpi_pci_detect_ejectable(handle
);
285 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
286 is_pci_dock_device
, NULL
, (void *)&found
, NULL
);
291 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
292 static void init_bridge_misc(struct acpiphp_bridge
*bridge
)
296 /* must be added to the list prior to calling register_slot */
297 list_add(&bridge
->list
, &bridge_list
);
299 /* register all slot objects under this bridge */
300 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge
->handle
, (u32
)1,
301 register_slot
, NULL
, bridge
, NULL
);
302 if (ACPI_FAILURE(status
)) {
303 list_del(&bridge
->list
);
307 /* install notify handler */
308 if (bridge
->type
!= BRIDGE_TYPE_HOST
) {
309 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
310 status
= acpi_remove_notify_handler(bridge
->func
->handle
,
312 handle_hotplug_event_func
);
313 if (ACPI_FAILURE(status
))
314 err("failed to remove notify handler\n");
316 status
= acpi_install_notify_handler(bridge
->handle
,
318 handle_hotplug_event_bridge
,
321 if (ACPI_FAILURE(status
)) {
322 err("failed to register interrupt notify handler\n");
328 /* find acpiphp_func from acpiphp_bridge */
329 static struct acpiphp_func
*acpiphp_bridge_handle_to_function(acpi_handle handle
)
331 struct acpiphp_bridge
*bridge
;
332 struct acpiphp_slot
*slot
;
333 struct acpiphp_func
*func
;
335 list_for_each_entry(bridge
, &bridge_list
, list
) {
336 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
337 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
338 if (func
->handle
== handle
)
348 static inline void config_p2p_bridge_flags(struct acpiphp_bridge
*bridge
)
350 acpi_handle dummy_handle
;
352 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
353 "_STA", &dummy_handle
)))
354 bridge
->flags
|= BRIDGE_HAS_STA
;
356 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
357 "_EJ0", &dummy_handle
)))
358 bridge
->flags
|= BRIDGE_HAS_EJ0
;
360 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
361 "_PS0", &dummy_handle
)))
362 bridge
->flags
|= BRIDGE_HAS_PS0
;
364 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
365 "_PS3", &dummy_handle
)))
366 bridge
->flags
|= BRIDGE_HAS_PS3
;
368 /* is this ejectable p2p bridge? */
369 if (bridge
->flags
& BRIDGE_HAS_EJ0
) {
370 struct acpiphp_func
*func
;
372 dbg("found ejectable p2p bridge\n");
374 /* make link between PCI bridge and PCI function */
375 func
= acpiphp_bridge_handle_to_function(bridge
->handle
);
379 func
->bridge
= bridge
;
384 /* allocate and initialize host bridge data structure */
385 static void add_host_bridge(struct acpi_pci_root
*root
)
387 struct acpiphp_bridge
*bridge
;
388 acpi_handle handle
= root
->device
->handle
;
390 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
394 bridge
->type
= BRIDGE_TYPE_HOST
;
395 bridge
->handle
= handle
;
397 bridge
->pci_bus
= root
->bus
;
399 init_bridge_misc(bridge
);
403 /* allocate and initialize PCI-to-PCI bridge data structure */
404 static void add_p2p_bridge(acpi_handle
*handle
)
406 struct acpiphp_bridge
*bridge
;
408 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
409 if (bridge
== NULL
) {
410 err("out of memory\n");
414 bridge
->type
= BRIDGE_TYPE_P2P
;
415 bridge
->handle
= handle
;
416 config_p2p_bridge_flags(bridge
);
418 bridge
->pci_dev
= acpi_get_pci_dev(handle
);
419 bridge
->pci_bus
= bridge
->pci_dev
->subordinate
;
420 if (!bridge
->pci_bus
) {
421 err("This is not a PCI-to-PCI bridge!\n");
426 * Grab a ref to the subordinate PCI bus in case the bus is
427 * removed via PCI core logical hotplug. The ref pins the bus
428 * (which we access during module unload).
430 get_device(&bridge
->pci_bus
->dev
);
432 init_bridge_misc(bridge
);
435 pci_dev_put(bridge
->pci_dev
);
441 /* callback routine to find P2P bridges */
443 find_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
448 dev
= acpi_get_pci_dev(handle
);
449 if (!dev
|| !dev
->subordinate
)
452 /* check if this bridge has ejectable slots */
453 if ((detect_ejectable_slots(handle
) > 0)) {
454 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev
));
455 add_p2p_bridge(handle
);
458 /* search P2P bridges under this p2p bridge */
459 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
460 find_p2p_bridge
, NULL
, NULL
, NULL
);
461 if (ACPI_FAILURE(status
))
462 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
470 /* find hot-pluggable slots, and then find P2P bridge */
471 static int add_bridge(struct acpi_pci_root
*root
)
474 unsigned long long tmp
;
475 acpi_handle dummy_handle
;
476 acpi_handle handle
= root
->device
->handle
;
478 /* if the bridge doesn't have _STA, we assume it is always there */
479 status
= acpi_get_handle(handle
, "_STA", &dummy_handle
);
480 if (ACPI_SUCCESS(status
)) {
481 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &tmp
);
482 if (ACPI_FAILURE(status
)) {
483 dbg("%s: _STA evaluation failure\n", __func__
);
486 if ((tmp
& ACPI_STA_FUNCTIONING
) == 0)
487 /* don't register this object */
491 /* check if this bridge has ejectable slots */
492 if (detect_ejectable_slots(handle
) > 0) {
493 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
494 add_host_bridge(root
);
497 /* search P2P bridges under this host bridge */
498 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
499 find_p2p_bridge
, NULL
, NULL
, NULL
);
501 if (ACPI_FAILURE(status
))
502 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
507 static struct acpiphp_bridge
*acpiphp_handle_to_bridge(acpi_handle handle
)
509 struct acpiphp_bridge
*bridge
;
511 list_for_each_entry(bridge
, &bridge_list
, list
)
512 if (bridge
->handle
== handle
)
518 static void cleanup_bridge(struct acpiphp_bridge
*bridge
)
520 struct acpiphp_slot
*slot
, *next
;
521 struct acpiphp_func
*func
, *tmp
;
523 acpi_handle handle
= bridge
->handle
;
525 status
= acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
526 handle_hotplug_event_bridge
);
527 if (ACPI_FAILURE(status
))
528 err("failed to remove notify handler\n");
530 if ((bridge
->type
!= BRIDGE_TYPE_HOST
) &&
531 ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
)) {
532 status
= acpi_install_notify_handler(bridge
->func
->handle
,
534 handle_hotplug_event_func
,
536 if (ACPI_FAILURE(status
))
537 err("failed to install interrupt notify handler\n");
540 slot
= bridge
->slots
;
543 list_for_each_entry_safe(func
, tmp
, &slot
->funcs
, sibling
) {
544 if (is_dock_device(func
->handle
)) {
545 unregister_hotplug_dock_device(func
->handle
);
546 unregister_dock_notifier(&func
->nb
);
548 if (!(func
->flags
& FUNC_HAS_DCK
)) {
549 status
= acpi_remove_notify_handler(func
->handle
,
551 handle_hotplug_event_func
);
552 if (ACPI_FAILURE(status
))
553 err("failed to remove notify handler\n");
555 list_del(&func
->sibling
);
558 acpiphp_unregister_hotplug_slot(slot
);
559 list_del(&slot
->funcs
);
565 * Only P2P bridges have a pci_dev
568 put_device(&bridge
->pci_bus
->dev
);
570 pci_dev_put(bridge
->pci_dev
);
571 list_del(&bridge
->list
);
576 cleanup_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
578 struct acpiphp_bridge
*bridge
;
580 /* cleanup p2p bridges under this P2P bridge
581 in a depth-first manner */
582 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
583 cleanup_p2p_bridge
, NULL
, NULL
, NULL
);
585 bridge
= acpiphp_handle_to_bridge(handle
);
587 cleanup_bridge(bridge
);
592 static void remove_bridge(struct acpi_pci_root
*root
)
594 struct acpiphp_bridge
*bridge
;
595 acpi_handle handle
= root
->device
->handle
;
597 /* cleanup p2p bridges under this host bridge
598 in a depth-first manner */
599 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
600 (u32
)1, cleanup_p2p_bridge
, NULL
, NULL
, NULL
);
603 * On root bridges with hotplug slots directly underneath (ie,
604 * no p2p bridge between), we call cleanup_bridge().
606 * The else clause cleans up root bridges that either had no
607 * hotplug slots at all, or had a p2p bridge underneath.
609 bridge
= acpiphp_handle_to_bridge(handle
);
611 cleanup_bridge(bridge
);
613 acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
614 handle_hotplug_event_bridge
);
617 static int power_on_slot(struct acpiphp_slot
*slot
)
620 struct acpiphp_func
*func
;
623 /* if already enabled, just skip */
624 if (slot
->flags
& SLOT_POWEREDON
)
627 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
628 if (func
->flags
& FUNC_HAS_PS0
) {
629 dbg("%s: executing _PS0\n", __func__
);
630 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
631 if (ACPI_FAILURE(status
)) {
632 warn("%s: _PS0 failed\n", __func__
);
640 /* TBD: evaluate _STA to check if the slot is enabled */
642 slot
->flags
|= SLOT_POWEREDON
;
649 static int power_off_slot(struct acpiphp_slot
*slot
)
652 struct acpiphp_func
*func
;
656 /* if already disabled, just skip */
657 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
660 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
661 if (func
->flags
& FUNC_HAS_PS3
) {
662 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
663 if (ACPI_FAILURE(status
)) {
664 warn("%s: _PS3 failed\n", __func__
);
672 /* TBD: evaluate _STA to check if the slot is disabled */
674 slot
->flags
&= (~SLOT_POWEREDON
);
683 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
684 * @bus: bus to start search with
686 static unsigned char acpiphp_max_busnr(struct pci_bus
*bus
)
688 struct list_head
*tmp
;
689 unsigned char max
, n
;
692 * pci_bus_max_busnr will return the highest
693 * reserved busnr for all these children.
694 * that is equivalent to the bus->subordinate
695 * value. We don't want to use the parent's
696 * bus->subordinate value because it could have
699 max
= bus
->busn_res
.start
;
701 list_for_each(tmp
, &bus
->children
) {
702 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
711 * acpiphp_bus_add - add a new bus to acpi subsystem
712 * @func: acpiphp_func of the bridge
714 static int acpiphp_bus_add(struct acpiphp_func
*func
)
717 struct acpi_device
*device
, *pdevice
;
720 acpi_get_parent(func
->handle
, &phandle
);
721 if (acpi_bus_get_device(phandle
, &pdevice
)) {
722 dbg("no parent device, assuming NULL\n");
725 if (!acpi_bus_get_device(func
->handle
, &device
)) {
726 dbg("bus exists... trim\n");
727 /* this shouldn't be in here, so remove
728 * the bus then re-add it...
730 ret_val
= acpi_bus_trim(device
, 1);
731 dbg("acpi_bus_trim return %x\n", ret_val
);
734 ret_val
= acpi_bus_add(&device
, pdevice
, func
->handle
,
735 ACPI_BUS_TYPE_DEVICE
);
737 dbg("error adding bus, %x\n",
739 goto acpiphp_bus_add_out
;
741 ret_val
= acpi_bus_start(device
);
749 * acpiphp_bus_trim - trim a bus from acpi subsystem
750 * @handle: handle to acpi namespace
752 static int acpiphp_bus_trim(acpi_handle handle
)
754 struct acpi_device
*device
;
757 retval
= acpi_bus_get_device(handle
, &device
);
759 dbg("acpi_device not found\n");
763 retval
= acpi_bus_trim(device
, 1);
765 err("cannot remove from acpi list\n");
770 static void acpiphp_set_acpi_region(struct acpiphp_slot
*slot
)
772 struct acpiphp_func
*func
;
773 union acpi_object params
[2];
774 struct acpi_object_list arg_list
;
776 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
778 arg_list
.pointer
= params
;
779 params
[0].type
= ACPI_TYPE_INTEGER
;
780 params
[0].integer
.value
= ACPI_ADR_SPACE_PCI_CONFIG
;
781 params
[1].type
= ACPI_TYPE_INTEGER
;
782 params
[1].integer
.value
= 1;
783 /* _REG is optional, we don't care about if there is failure */
784 acpi_evaluate_object(func
->handle
, "_REG", &arg_list
, NULL
);
789 * enable_device - enable, configure a slot
790 * @slot: slot to be enabled
792 * This function should be called per *physical slot*,
793 * not per each slot object in ACPI namespace.
795 static int __ref
enable_device(struct acpiphp_slot
*slot
)
798 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
799 struct acpiphp_func
*func
;
804 if (slot
->flags
& SLOT_ENABLED
)
807 num
= pci_scan_slot(bus
, PCI_DEVFN(slot
->device
, 0));
809 /* Maybe only part of funcs are added. */
810 dbg("No new device found\n");
814 max
= acpiphp_max_busnr(bus
);
815 for (pass
= 0; pass
< 2; pass
++) {
816 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
817 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
819 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
820 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
821 max
= pci_scan_bridge(bus
, dev
, max
, pass
);
822 if (pass
&& dev
->subordinate
)
823 pci_bus_size_bridges(dev
->subordinate
);
828 list_for_each_entry(func
, &slot
->funcs
, sibling
)
829 acpiphp_bus_add(func
);
831 pci_bus_assign_resources(bus
);
832 acpiphp_sanitize_bus(bus
);
833 acpiphp_set_hpp_values(bus
);
834 acpiphp_set_acpi_region(slot
);
835 pci_enable_bridges(bus
);
837 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
838 /* Assume that newly added devices are powered on already. */
840 dev
->current_state
= PCI_D0
;
843 pci_bus_add_devices(bus
);
845 slot
->flags
|= SLOT_ENABLED
;
846 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
847 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
850 /* Do not set SLOT_ENABLED flag if some funcs
852 slot
->flags
&= (~SLOT_ENABLED
);
856 if (dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
&&
857 dev
->hdr_type
!= PCI_HEADER_TYPE_CARDBUS
) {
862 status
= find_p2p_bridge(func
->handle
, (u32
)1, bus
, NULL
);
863 if (ACPI_FAILURE(status
))
864 warn("find_p2p_bridge failed (error code = 0x%x)\n",
874 /* return first device in slot, acquiring a reference on it */
875 static struct pci_dev
*dev_in_slot(struct acpiphp_slot
*slot
)
877 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
879 struct pci_dev
*ret
= NULL
;
881 down_read(&pci_bus_sem
);
882 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
883 if (PCI_SLOT(dev
->devfn
) == slot
->device
) {
884 ret
= pci_dev_get(dev
);
887 up_read(&pci_bus_sem
);
893 * disable_device - disable a slot
894 * @slot: ACPI PHP slot
896 static int disable_device(struct acpiphp_slot
*slot
)
898 struct acpiphp_func
*func
;
899 struct pci_dev
*pdev
;
900 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
902 /* The slot will be enabled when func 0 is added, so check
903 func 0 before disable the slot. */
904 pdev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
, 0));
909 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
911 /* cleanup p2p bridges under this P2P bridge */
912 cleanup_p2p_bridge(func
->bridge
->handle
,
919 * enable_device() enumerates all functions in this device via
920 * pci_scan_slot(), whether they have associated ACPI hotplug
921 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
924 while ((pdev
= dev_in_slot(slot
))) {
925 pci_stop_and_remove_bus_device(pdev
);
929 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
930 acpiphp_bus_trim(func
->handle
);
933 slot
->flags
&= (~SLOT_ENABLED
);
941 * get_slot_status - get ACPI slot status
942 * @slot: ACPI PHP slot
944 * If a slot has _STA for each function and if any one of them
945 * returned non-zero status, return it.
947 * If a slot doesn't have _STA and if any one of its functions'
948 * configuration space is configured, return 0x0f as a _STA.
950 * Otherwise return 0.
952 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
955 unsigned long long sta
= 0;
957 struct acpiphp_func
*func
;
959 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
960 if (func
->flags
& FUNC_HAS_STA
) {
961 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
962 if (ACPI_SUCCESS(status
) && sta
)
965 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
966 PCI_DEVFN(slot
->device
,
968 PCI_VENDOR_ID
, &dvid
);
969 if (dvid
!= 0xffffffff) {
976 return (unsigned int)sta
;
980 * acpiphp_eject_slot - physically eject the slot
981 * @slot: ACPI PHP slot
983 int acpiphp_eject_slot(struct acpiphp_slot
*slot
)
986 struct acpiphp_func
*func
;
987 struct acpi_object_list arg_list
;
988 union acpi_object arg
;
990 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
991 /* We don't want to call _EJ0 on non-existing functions. */
992 if ((func
->flags
& FUNC_HAS_EJ0
)) {
993 /* _EJ0 method take one argument */
995 arg_list
.pointer
= &arg
;
996 arg
.type
= ACPI_TYPE_INTEGER
;
997 arg
.integer
.value
= 1;
999 status
= acpi_evaluate_object(func
->handle
, "_EJ0", &arg_list
, NULL
);
1000 if (ACPI_FAILURE(status
)) {
1001 warn("%s: _EJ0 failed\n", __func__
);
1011 * acpiphp_check_bridge - re-enumerate devices
1012 * @bridge: where to begin re-enumeration
1014 * Iterate over all slots under this bridge and make sure that if a
1015 * card is present they are enabled, and if not they are disabled.
1017 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
1019 struct acpiphp_slot
*slot
;
1021 int enabled
, disabled
;
1023 enabled
= disabled
= 0;
1025 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1026 unsigned int status
= get_slot_status(slot
);
1027 if (slot
->flags
& SLOT_ENABLED
) {
1028 if (status
== ACPI_STA_ALL
)
1030 retval
= acpiphp_disable_slot(slot
);
1032 err("Error occurred in disabling\n");
1035 acpiphp_eject_slot(slot
);
1039 if (status
!= ACPI_STA_ALL
)
1041 retval
= acpiphp_enable_slot(slot
);
1043 err("Error occurred in enabling\n");
1050 dbg("%s: %d enabled, %d disabled\n", __func__
, enabled
, disabled
);
1056 static void acpiphp_set_hpp_values(struct pci_bus
*bus
)
1058 struct pci_dev
*dev
;
1060 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
1061 pci_configure_slot(dev
);
1065 * Remove devices for which we could not assign resources, call
1066 * arch specific code to fix-up the bus
1068 static void acpiphp_sanitize_bus(struct pci_bus
*bus
)
1070 struct pci_dev
*dev
;
1072 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
;
1074 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1075 for (i
=0; i
<PCI_BRIDGE_RESOURCES
; i
++) {
1076 struct resource
*res
= &dev
->resource
[i
];
1077 if ((res
->flags
& type_mask
) && !res
->start
&&
1079 /* Could not assign a required resources
1080 * for this device, remove it */
1081 pci_stop_and_remove_bus_device(dev
);
1088 /* Program resources in newly inserted bridge */
1089 static int acpiphp_configure_bridge (acpi_handle handle
)
1091 struct pci_bus
*bus
;
1093 if (acpi_is_root_bridge(handle
)) {
1094 struct acpi_pci_root
*root
= acpi_pci_find_root(handle
);
1097 struct pci_dev
*pdev
= acpi_get_pci_dev(handle
);
1098 bus
= pdev
->subordinate
;
1102 pci_bus_size_bridges(bus
);
1103 pci_bus_assign_resources(bus
);
1104 acpiphp_sanitize_bus(bus
);
1105 acpiphp_set_hpp_values(bus
);
1106 pci_enable_bridges(bus
);
1110 static void handle_bridge_insertion(acpi_handle handle
, u32 type
)
1112 struct acpi_device
*device
, *pdevice
;
1113 acpi_handle phandle
;
1115 if ((type
!= ACPI_NOTIFY_BUS_CHECK
) &&
1116 (type
!= ACPI_NOTIFY_DEVICE_CHECK
)) {
1117 err("unexpected notification type %d\n", type
);
1121 acpi_get_parent(handle
, &phandle
);
1122 if (acpi_bus_get_device(phandle
, &pdevice
)) {
1123 dbg("no parent device, assuming NULL\n");
1126 if (acpi_bus_add(&device
, pdevice
, handle
, ACPI_BUS_TYPE_DEVICE
)) {
1127 err("cannot add bridge to acpi list\n");
1130 if (!acpiphp_configure_bridge(handle
) &&
1131 !acpi_bus_start(device
))
1134 err("cannot configure and start bridge\n");
1139 * ACPI event handlers
1143 count_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1145 int *count
= (int *)context
;
1146 struct acpiphp_bridge
*bridge
;
1148 bridge
= acpiphp_handle_to_bridge(handle
);
1155 check_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1157 struct acpiphp_bridge
*bridge
;
1159 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1160 .pointer
= objname
};
1162 bridge
= acpiphp_handle_to_bridge(handle
);
1164 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1165 dbg("%s: re-enumerating slots under %s\n",
1167 acpiphp_check_bridge(bridge
);
1172 struct acpiphp_hp_work
{
1173 struct work_struct work
;
1179 static void alloc_acpiphp_hp_work(acpi_handle handle
, u32 type
,
1181 void (*func
)(struct work_struct
*work
))
1183 struct acpiphp_hp_work
*hp_work
;
1186 hp_work
= kmalloc(sizeof(*hp_work
), GFP_KERNEL
);
1190 hp_work
->handle
= handle
;
1191 hp_work
->type
= type
;
1192 hp_work
->context
= context
;
1194 INIT_WORK(&hp_work
->work
, func
);
1195 ret
= queue_work(kacpi_hotplug_wq
, &hp_work
->work
);
1200 static void _handle_hotplug_event_bridge(struct work_struct
*work
)
1202 struct acpiphp_bridge
*bridge
;
1204 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1205 .pointer
= objname
};
1206 struct acpi_device
*device
;
1207 int num_sub_bridges
= 0;
1208 struct acpiphp_hp_work
*hp_work
;
1212 hp_work
= container_of(work
, struct acpiphp_hp_work
, work
);
1213 handle
= hp_work
->handle
;
1214 type
= hp_work
->type
;
1216 if (acpi_bus_get_device(handle
, &device
)) {
1217 /* This bridge must have just been physically inserted */
1218 handle_bridge_insertion(handle
, type
);
1222 bridge
= acpiphp_handle_to_bridge(handle
);
1223 if (type
== ACPI_NOTIFY_BUS_CHECK
) {
1224 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, ACPI_UINT32_MAX
,
1225 count_sub_bridges
, NULL
, &num_sub_bridges
, NULL
);
1228 if (!bridge
&& !num_sub_bridges
) {
1229 err("cannot get bridge info\n");
1233 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1236 case ACPI_NOTIFY_BUS_CHECK
:
1237 /* bus re-enumerate */
1238 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
1240 dbg("%s: re-enumerating slots under %s\n",
1242 acpiphp_check_bridge(bridge
);
1244 if (num_sub_bridges
)
1245 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
1246 ACPI_UINT32_MAX
, check_sub_bridges
, NULL
, NULL
, NULL
);
1249 case ACPI_NOTIFY_DEVICE_CHECK
:
1251 dbg("%s: Device check notify on %s\n", __func__
, objname
);
1252 acpiphp_check_bridge(bridge
);
1255 case ACPI_NOTIFY_DEVICE_WAKE
:
1257 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1260 case ACPI_NOTIFY_EJECT_REQUEST
:
1261 /* request device eject */
1262 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1263 if ((bridge
->type
!= BRIDGE_TYPE_HOST
) &&
1264 (bridge
->flags
& BRIDGE_HAS_EJ0
)) {
1265 struct acpiphp_slot
*slot
;
1266 slot
= bridge
->func
->slot
;
1267 if (!acpiphp_disable_slot(slot
))
1268 acpiphp_eject_slot(slot
);
1272 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
1273 printk(KERN_ERR
"Device %s cannot be configured due"
1274 " to a frequency mismatch\n", objname
);
1277 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
1278 printk(KERN_ERR
"Device %s cannot be configured due"
1279 " to a bus mode mismatch\n", objname
);
1282 case ACPI_NOTIFY_POWER_FAULT
:
1283 printk(KERN_ERR
"Device %s has suffered a power fault\n",
1288 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1293 kfree(hp_work
); /* allocated in handle_hotplug_event_bridge */
1297 * handle_hotplug_event_bridge - handle ACPI event on bridges
1298 * @handle: Notify()'ed acpi_handle
1299 * @type: Notify code
1300 * @context: pointer to acpiphp_bridge structure
1302 * Handles ACPI event notification on {host,p2p} bridges.
1304 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
,
1308 * Currently the code adds all hotplug events to the kacpid_wq
1309 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1310 * The proper way to fix this is to reorganize the code so that
1311 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1312 * For now just re-add this work to the kacpi_hotplug_wq so we
1313 * don't deadlock on hotplug actions.
1315 alloc_acpiphp_hp_work(handle
, type
, context
,
1316 _handle_hotplug_event_bridge
);
1319 static void _handle_hotplug_event_func(struct work_struct
*work
)
1321 struct acpiphp_func
*func
;
1323 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1324 .pointer
= objname
};
1325 struct acpiphp_hp_work
*hp_work
;
1330 hp_work
= container_of(work
, struct acpiphp_hp_work
, work
);
1331 handle
= hp_work
->handle
;
1332 type
= hp_work
->type
;
1333 context
= hp_work
->context
;
1335 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1337 func
= (struct acpiphp_func
*)context
;
1340 case ACPI_NOTIFY_BUS_CHECK
:
1341 /* bus re-enumerate */
1342 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
1343 acpiphp_enable_slot(func
->slot
);
1346 case ACPI_NOTIFY_DEVICE_CHECK
:
1347 /* device check : re-enumerate from parent bus */
1348 dbg("%s: Device check notify on %s\n", __func__
, objname
);
1349 acpiphp_check_bridge(func
->slot
->bridge
);
1352 case ACPI_NOTIFY_DEVICE_WAKE
:
1354 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1357 case ACPI_NOTIFY_EJECT_REQUEST
:
1358 /* request device eject */
1359 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1360 if (!(acpiphp_disable_slot(func
->slot
)))
1361 acpiphp_eject_slot(func
->slot
);
1365 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1369 kfree(hp_work
); /* allocated in handle_hotplug_event_func */
1373 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1374 * @handle: Notify()'ed acpi_handle
1375 * @type: Notify code
1376 * @context: pointer to acpiphp_func structure
1378 * Handles ACPI event notification on slots.
1380 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
,
1384 * Currently the code adds all hotplug events to the kacpid_wq
1385 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1386 * The proper way to fix this is to reorganize the code so that
1387 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1388 * For now just re-add this work to the kacpi_hotplug_wq so we
1389 * don't deadlock on hotplug actions.
1391 alloc_acpiphp_hp_work(handle
, type
, context
,
1392 _handle_hotplug_event_func
);
1396 find_root_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1398 int *count
= (int *)context
;
1400 if (!acpi_is_root_bridge(handle
))
1404 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1405 handle_hotplug_event_bridge
, NULL
);
1410 static struct acpi_pci_driver acpi_pci_hp_driver
= {
1412 .remove
= remove_bridge
,
1416 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1418 int __init
acpiphp_glue_init(void)
1422 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
1423 ACPI_UINT32_MAX
, find_root_bridges
, NULL
, &num
, NULL
);
1428 acpi_pci_register_driver(&acpi_pci_hp_driver
);
1435 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1437 * This function frees all data allocated in acpiphp_glue_init().
1439 void acpiphp_glue_exit(void)
1441 acpi_pci_unregister_driver(&acpi_pci_hp_driver
);
1446 * acpiphp_get_num_slots - count number of slots in a system
1448 int __init
acpiphp_get_num_slots(void)
1450 struct acpiphp_bridge
*bridge
;
1453 list_for_each_entry(bridge
, &bridge_list
, list
) {
1454 dbg("Bus %04x:%02x has %d slot%s\n",
1455 pci_domain_nr(bridge
->pci_bus
),
1456 bridge
->pci_bus
->number
, bridge
->nr_slots
,
1457 bridge
->nr_slots
== 1 ? "" : "s");
1458 num_slots
+= bridge
->nr_slots
;
1461 dbg("Total %d slots\n", num_slots
);
1467 * acpiphp_enable_slot - power on slot
1468 * @slot: ACPI PHP slot
1470 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1474 mutex_lock(&slot
->crit_sect
);
1476 /* wake up all functions */
1477 retval
= power_on_slot(slot
);
1481 if (get_slot_status(slot
) == ACPI_STA_ALL
) {
1482 /* configure all functions */
1483 retval
= enable_device(slot
);
1485 power_off_slot(slot
);
1487 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__
);
1488 power_off_slot(slot
);
1492 mutex_unlock(&slot
->crit_sect
);
1497 * acpiphp_disable_slot - power off slot
1498 * @slot: ACPI PHP slot
1500 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1504 mutex_lock(&slot
->crit_sect
);
1506 /* unconfigure all functions */
1507 retval
= disable_device(slot
);
1511 /* power off all functions */
1512 retval
= power_off_slot(slot
);
1517 mutex_unlock(&slot
->crit_sect
);
1526 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1528 return (slot
->flags
& SLOT_POWEREDON
);
1536 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1540 sta
= get_slot_status(slot
);
1542 return (sta
& ACPI_STA_SHOW_IN_UI
) ? 0 : 1;
1547 * adapter presence : 1
1550 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1554 sta
= get_slot_status(slot
);
1556 return (sta
== 0) ? 0 : 1;