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>
54 static LIST_HEAD(bridge_list
);
55 static LIST_HEAD(ioapic_list
);
56 static DEFINE_SPINLOCK(ioapic_list_lock
);
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
->secondary
) {
104 buses
= (buses
& 0xff000000)
105 | ((unsigned int)(bus
->primary
) << 0)
106 | ((unsigned int)(bus
->secondary
) << 8)
107 | ((unsigned int)(bus
->subordinate
) << 16);
108 pci_write_config_dword(bus
->self
, PCI_PRIMARY_BUS
, buses
);
114 static 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 acpi_evaluate_integer(handle
, "_ADR", NULL
, &adr
);
136 device
= (adr
>> 16) & 0xffff;
137 function
= adr
& 0xffff;
139 newfunc
= kzalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
143 INIT_LIST_HEAD(&newfunc
->sibling
);
144 newfunc
->handle
= handle
;
145 newfunc
->function
= function
;
147 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_EJ0", &tmp
)))
148 newfunc
->flags
= FUNC_HAS_EJ0
;
150 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_STA", &tmp
)))
151 newfunc
->flags
|= FUNC_HAS_STA
;
153 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS0", &tmp
)))
154 newfunc
->flags
|= FUNC_HAS_PS0
;
156 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS3", &tmp
)))
157 newfunc
->flags
|= FUNC_HAS_PS3
;
159 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_DCK", &tmp
)))
160 newfunc
->flags
|= FUNC_HAS_DCK
;
162 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
163 if (ACPI_FAILURE(status
)) {
165 * use the count of the number of slots we've found
166 * for the number of the slot
168 sun
= bridge
->nr_slots
+1;
171 /* search for objects that share the same slot */
172 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
)
173 if (slot
->device
== device
) {
174 if (slot
->sun
!= sun
)
175 warn("sibling found, but _SUN doesn't match!\n");
180 slot
= kzalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
186 slot
->bridge
= bridge
;
187 slot
->device
= device
;
189 INIT_LIST_HEAD(&slot
->funcs
);
190 mutex_init(&slot
->crit_sect
);
192 slot
->next
= bridge
->slots
;
193 bridge
->slots
= slot
;
197 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
198 slot
->sun
, pci_domain_nr(pbus
), pbus
->number
, device
);
199 retval
= acpiphp_register_hotplug_slot(slot
);
201 if (retval
== -EBUSY
)
202 warn("Slot %llu already registered by another "
203 "hotplug driver\n", slot
->sun
);
205 warn("acpiphp_register_hotplug_slot failed "
206 "(err code = 0x%x)\n", retval
);
211 newfunc
->slot
= slot
;
212 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
214 pdev
= pci_get_slot(pbus
, PCI_DEVFN(device
, function
));
216 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
220 if (is_dock_device(handle
)) {
221 /* we don't want to call this device's _EJ0
222 * because we want the dock notify handler
223 * to call it after it calls _DCK
225 newfunc
->flags
&= ~FUNC_HAS_EJ0
;
226 if (register_hotplug_dock_device(handle
,
227 &acpiphp_dock_ops
, newfunc
))
228 dbg("failed to register dock device\n");
230 /* we need to be notified when dock events happen
231 * outside of the hotplug operation, since we may
232 * need to do fixups before we can hotplug.
234 newfunc
->nb
.notifier_call
= post_dock_fixups
;
235 if (register_dock_notifier(&newfunc
->nb
))
236 dbg("failed to register a dock notifier");
239 /* install notify handler */
240 if (!(newfunc
->flags
& FUNC_HAS_DCK
)) {
241 status
= acpi_install_notify_handler(handle
,
243 handle_hotplug_event_func
,
246 if (ACPI_FAILURE(status
))
247 err("failed to register interrupt notify handler\n");
255 bridge
->slots
= slot
->next
;
263 /* see if it's worth looking at this bridge */
264 static int detect_ejectable_slots(acpi_handle handle
)
266 int found
= acpi_pci_detect_ejectable(handle
);
268 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
269 is_pci_dock_device
, (void *)&found
, NULL
);
274 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
275 static void init_bridge_misc(struct acpiphp_bridge
*bridge
)
279 /* must be added to the list prior to calling register_slot */
280 list_add(&bridge
->list
, &bridge_list
);
282 /* register all slot objects under this bridge */
283 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge
->handle
, (u32
)1,
284 register_slot
, bridge
, NULL
);
285 if (ACPI_FAILURE(status
)) {
286 list_del(&bridge
->list
);
290 /* install notify handler */
291 if (bridge
->type
!= BRIDGE_TYPE_HOST
) {
292 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
293 status
= acpi_remove_notify_handler(bridge
->func
->handle
,
295 handle_hotplug_event_func
);
296 if (ACPI_FAILURE(status
))
297 err("failed to remove notify handler\n");
299 status
= acpi_install_notify_handler(bridge
->handle
,
301 handle_hotplug_event_bridge
,
304 if (ACPI_FAILURE(status
)) {
305 err("failed to register interrupt notify handler\n");
311 /* find acpiphp_func from acpiphp_bridge */
312 static struct acpiphp_func
*acpiphp_bridge_handle_to_function(acpi_handle handle
)
314 struct list_head
*node
, *l
;
315 struct acpiphp_bridge
*bridge
;
316 struct acpiphp_slot
*slot
;
317 struct acpiphp_func
*func
;
319 list_for_each(node
, &bridge_list
) {
320 bridge
= list_entry(node
, struct acpiphp_bridge
, list
);
321 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
322 list_for_each(l
, &slot
->funcs
) {
323 func
= list_entry(l
, struct acpiphp_func
,
325 if (func
->handle
== handle
)
335 static inline void config_p2p_bridge_flags(struct acpiphp_bridge
*bridge
)
337 acpi_handle dummy_handle
;
339 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
340 "_STA", &dummy_handle
)))
341 bridge
->flags
|= BRIDGE_HAS_STA
;
343 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
344 "_EJ0", &dummy_handle
)))
345 bridge
->flags
|= BRIDGE_HAS_EJ0
;
347 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
348 "_PS0", &dummy_handle
)))
349 bridge
->flags
|= BRIDGE_HAS_PS0
;
351 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
352 "_PS3", &dummy_handle
)))
353 bridge
->flags
|= BRIDGE_HAS_PS3
;
355 /* is this ejectable p2p bridge? */
356 if (bridge
->flags
& BRIDGE_HAS_EJ0
) {
357 struct acpiphp_func
*func
;
359 dbg("found ejectable p2p bridge\n");
361 /* make link between PCI bridge and PCI function */
362 func
= acpiphp_bridge_handle_to_function(bridge
->handle
);
366 func
->bridge
= bridge
;
371 /* allocate and initialize host bridge data structure */
372 static void add_host_bridge(acpi_handle
*handle
)
374 struct acpiphp_bridge
*bridge
;
375 struct acpi_pci_root
*root
= acpi_pci_find_root(handle
);
377 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
381 bridge
->type
= BRIDGE_TYPE_HOST
;
382 bridge
->handle
= handle
;
384 bridge
->pci_bus
= root
->bus
;
386 spin_lock_init(&bridge
->res_lock
);
388 init_bridge_misc(bridge
);
392 /* allocate and initialize PCI-to-PCI bridge data structure */
393 static void add_p2p_bridge(acpi_handle
*handle
)
395 struct acpiphp_bridge
*bridge
;
397 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
398 if (bridge
== NULL
) {
399 err("out of memory\n");
403 bridge
->type
= BRIDGE_TYPE_P2P
;
404 bridge
->handle
= handle
;
405 config_p2p_bridge_flags(bridge
);
407 bridge
->pci_dev
= acpi_get_pci_dev(handle
);
408 bridge
->pci_bus
= bridge
->pci_dev
->subordinate
;
409 if (!bridge
->pci_bus
) {
410 err("This is not a PCI-to-PCI bridge!\n");
415 * Grab a ref to the subordinate PCI bus in case the bus is
416 * removed via PCI core logical hotplug. The ref pins the bus
417 * (which we access during module unload).
419 get_device(&bridge
->pci_bus
->dev
);
420 spin_lock_init(&bridge
->res_lock
);
422 init_bridge_misc(bridge
);
425 pci_dev_put(bridge
->pci_dev
);
431 /* callback routine to find P2P bridges */
433 find_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
438 dev
= acpi_get_pci_dev(handle
);
439 if (!dev
|| !dev
->subordinate
)
442 /* check if this bridge has ejectable slots */
443 if ((detect_ejectable_slots(handle
) > 0)) {
444 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev
));
445 add_p2p_bridge(handle
);
448 /* search P2P bridges under this p2p bridge */
449 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
450 find_p2p_bridge
, NULL
, NULL
);
451 if (ACPI_FAILURE(status
))
452 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
460 /* find hot-pluggable slots, and then find P2P bridge */
461 static int add_bridge(acpi_handle handle
)
464 unsigned long long tmp
;
465 acpi_handle dummy_handle
;
467 /* if the bridge doesn't have _STA, we assume it is always there */
468 status
= acpi_get_handle(handle
, "_STA", &dummy_handle
);
469 if (ACPI_SUCCESS(status
)) {
470 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &tmp
);
471 if (ACPI_FAILURE(status
)) {
472 dbg("%s: _STA evaluation failure\n", __func__
);
475 if ((tmp
& ACPI_STA_FUNCTIONING
) == 0)
476 /* don't register this object */
480 /* check if this bridge has ejectable slots */
481 if (detect_ejectable_slots(handle
) > 0) {
482 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
483 add_host_bridge(handle
);
486 /* search P2P bridges under this host bridge */
487 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
488 find_p2p_bridge
, NULL
, NULL
);
490 if (ACPI_FAILURE(status
))
491 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
496 static struct acpiphp_bridge
*acpiphp_handle_to_bridge(acpi_handle handle
)
498 struct list_head
*head
;
499 list_for_each(head
, &bridge_list
) {
500 struct acpiphp_bridge
*bridge
= list_entry(head
,
501 struct acpiphp_bridge
, list
);
502 if (bridge
->handle
== handle
)
509 static void cleanup_bridge(struct acpiphp_bridge
*bridge
)
511 struct list_head
*list
, *tmp
;
512 struct acpiphp_slot
*slot
;
514 acpi_handle handle
= bridge
->handle
;
516 status
= acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
517 handle_hotplug_event_bridge
);
518 if (ACPI_FAILURE(status
))
519 err("failed to remove notify handler\n");
521 if ((bridge
->type
!= BRIDGE_TYPE_HOST
) &&
522 ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
)) {
523 status
= acpi_install_notify_handler(bridge
->func
->handle
,
525 handle_hotplug_event_func
,
527 if (ACPI_FAILURE(status
))
528 err("failed to install interrupt notify handler\n");
531 slot
= bridge
->slots
;
533 struct acpiphp_slot
*next
= slot
->next
;
534 list_for_each_safe (list
, tmp
, &slot
->funcs
) {
535 struct acpiphp_func
*func
;
536 func
= list_entry(list
, struct acpiphp_func
, sibling
);
537 if (is_dock_device(func
->handle
)) {
538 unregister_hotplug_dock_device(func
->handle
);
539 unregister_dock_notifier(&func
->nb
);
541 if (!(func
->flags
& FUNC_HAS_DCK
)) {
542 status
= acpi_remove_notify_handler(func
->handle
,
544 handle_hotplug_event_func
);
545 if (ACPI_FAILURE(status
))
546 err("failed to remove notify handler\n");
551 acpiphp_unregister_hotplug_slot(slot
);
552 list_del(&slot
->funcs
);
558 * Only P2P bridges have a pci_dev
561 put_device(&bridge
->pci_bus
->dev
);
563 pci_dev_put(bridge
->pci_dev
);
564 list_del(&bridge
->list
);
569 cleanup_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
571 struct acpiphp_bridge
*bridge
;
573 /* cleanup p2p bridges under this P2P bridge
574 in a depth-first manner */
575 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
576 cleanup_p2p_bridge
, NULL
, NULL
);
578 bridge
= acpiphp_handle_to_bridge(handle
);
580 cleanup_bridge(bridge
);
585 static void remove_bridge(acpi_handle handle
)
587 struct acpiphp_bridge
*bridge
;
589 /* cleanup p2p bridges under this host bridge
590 in a depth-first manner */
591 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
592 (u32
)1, cleanup_p2p_bridge
, NULL
, NULL
);
595 * On root bridges with hotplug slots directly underneath (ie,
596 * no p2p bridge inbetween), we call cleanup_bridge().
598 * The else clause cleans up root bridges that either had no
599 * hotplug slots at all, or had a p2p bridge underneath.
601 bridge
= acpiphp_handle_to_bridge(handle
);
603 cleanup_bridge(bridge
);
605 acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
606 handle_hotplug_event_bridge
);
609 static struct pci_dev
* get_apic_pci_info(acpi_handle handle
)
613 dev
= acpi_get_pci_dev(handle
);
617 if ((dev
->class != PCI_CLASS_SYSTEM_PIC_IOAPIC
) &&
618 (dev
->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC
))
627 static int get_gsi_base(acpi_handle handle
, u32
*gsi_base
)
631 unsigned long long gsb
;
632 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
633 union acpi_object
*obj
;
636 status
= acpi_evaluate_integer(handle
, "_GSB", NULL
, &gsb
);
637 if (ACPI_SUCCESS(status
)) {
638 *gsi_base
= (u32
)gsb
;
642 status
= acpi_evaluate_object(handle
, "_MAT", NULL
, &buffer
);
643 if (ACPI_FAILURE(status
) || !buffer
.length
|| !buffer
.pointer
)
646 obj
= buffer
.pointer
;
647 if (obj
->type
!= ACPI_TYPE_BUFFER
)
650 table
= obj
->buffer
.pointer
;
651 switch (((struct acpi_subtable_header
*)table
)->type
) {
652 case ACPI_MADT_TYPE_IO_SAPIC
:
653 *gsi_base
= ((struct acpi_madt_io_sapic
*)table
)->global_irq_base
;
656 case ACPI_MADT_TYPE_IO_APIC
:
657 *gsi_base
= ((struct acpi_madt_io_apic
*)table
)->global_irq_base
;
664 kfree(buffer
.pointer
);
669 ioapic_add(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
672 unsigned long long sta
;
674 struct pci_dev
*pdev
;
677 struct acpiphp_ioapic
*ioapic
;
679 /* Evaluate _STA if present */
680 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
681 if (ACPI_SUCCESS(status
) && sta
!= ACPI_STA_ALL
)
682 return AE_CTRL_DEPTH
;
684 /* Scan only PCI bus scope */
685 status
= acpi_get_handle(handle
, "_HID", &tmp
);
686 if (ACPI_SUCCESS(status
))
687 return AE_CTRL_DEPTH
;
689 if (get_gsi_base(handle
, &gsi_base
))
692 ioapic
= kmalloc(sizeof(*ioapic
), GFP_KERNEL
);
696 pdev
= get_apic_pci_info(handle
);
700 if (pci_enable_device(pdev
))
701 goto exit_pci_dev_put
;
703 pci_set_master(pdev
);
705 if (pci_request_region(pdev
, 0, "I/O APIC(acpiphp)"))
706 goto exit_pci_disable_device
;
708 phys_addr
= pci_resource_start(pdev
, 0);
709 if (acpi_register_ioapic(handle
, phys_addr
, gsi_base
))
710 goto exit_pci_release_region
;
712 ioapic
->gsi_base
= gsi_base
;
714 spin_lock(&ioapic_list_lock
);
715 list_add_tail(&ioapic
->list
, &ioapic_list
);
716 spin_unlock(&ioapic_list_lock
);
720 exit_pci_release_region
:
721 pci_release_region(pdev
, 0);
722 exit_pci_disable_device
:
723 pci_disable_device(pdev
);
733 ioapic_remove(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
736 unsigned long long sta
;
739 struct acpiphp_ioapic
*pos
, *n
, *ioapic
= NULL
;
741 /* Evaluate _STA if present */
742 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
743 if (ACPI_SUCCESS(status
) && sta
!= ACPI_STA_ALL
)
744 return AE_CTRL_DEPTH
;
746 /* Scan only PCI bus scope */
747 status
= acpi_get_handle(handle
, "_HID", &tmp
);
748 if (ACPI_SUCCESS(status
))
749 return AE_CTRL_DEPTH
;
751 if (get_gsi_base(handle
, &gsi_base
))
754 acpi_unregister_ioapic(handle
, gsi_base
);
756 spin_lock(&ioapic_list_lock
);
757 list_for_each_entry_safe(pos
, n
, &ioapic_list
, list
) {
758 if (pos
->gsi_base
!= gsi_base
)
761 list_del(&ioapic
->list
);
764 spin_unlock(&ioapic_list_lock
);
769 pci_release_region(ioapic
->dev
, 0);
770 pci_disable_device(ioapic
->dev
);
771 pci_dev_put(ioapic
->dev
);
777 static int acpiphp_configure_ioapics(acpi_handle handle
)
779 ioapic_add(handle
, 0, NULL
, NULL
);
780 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
781 ACPI_UINT32_MAX
, ioapic_add
, NULL
, NULL
);
785 static int acpiphp_unconfigure_ioapics(acpi_handle handle
)
787 ioapic_remove(handle
, 0, NULL
, NULL
);
788 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
789 ACPI_UINT32_MAX
, ioapic_remove
, NULL
, NULL
);
793 static int power_on_slot(struct acpiphp_slot
*slot
)
796 struct acpiphp_func
*func
;
800 /* if already enabled, just skip */
801 if (slot
->flags
& SLOT_POWEREDON
)
804 list_for_each (l
, &slot
->funcs
) {
805 func
= list_entry(l
, struct acpiphp_func
, sibling
);
807 if (func
->flags
& FUNC_HAS_PS0
) {
808 dbg("%s: executing _PS0\n", __func__
);
809 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
810 if (ACPI_FAILURE(status
)) {
811 warn("%s: _PS0 failed\n", __func__
);
819 /* TBD: evaluate _STA to check if the slot is enabled */
821 slot
->flags
|= SLOT_POWEREDON
;
828 static int power_off_slot(struct acpiphp_slot
*slot
)
831 struct acpiphp_func
*func
;
836 /* if already disabled, just skip */
837 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
840 list_for_each (l
, &slot
->funcs
) {
841 func
= list_entry(l
, struct acpiphp_func
, sibling
);
843 if (func
->flags
& FUNC_HAS_PS3
) {
844 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
845 if (ACPI_FAILURE(status
)) {
846 warn("%s: _PS3 failed\n", __func__
);
854 /* TBD: evaluate _STA to check if the slot is disabled */
856 slot
->flags
&= (~SLOT_POWEREDON
);
865 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
866 * @bus: bus to start search with
868 static unsigned char acpiphp_max_busnr(struct pci_bus
*bus
)
870 struct list_head
*tmp
;
871 unsigned char max
, n
;
874 * pci_bus_max_busnr will return the highest
875 * reserved busnr for all these children.
876 * that is equivalent to the bus->subordinate
877 * value. We don't want to use the parent's
878 * bus->subordinate value because it could have
881 max
= bus
->secondary
;
883 list_for_each(tmp
, &bus
->children
) {
884 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
893 * acpiphp_bus_add - add a new bus to acpi subsystem
894 * @func: acpiphp_func of the bridge
896 static int acpiphp_bus_add(struct acpiphp_func
*func
)
899 struct acpi_device
*device
, *pdevice
;
902 acpi_get_parent(func
->handle
, &phandle
);
903 if (acpi_bus_get_device(phandle
, &pdevice
)) {
904 dbg("no parent device, assuming NULL\n");
907 if (!acpi_bus_get_device(func
->handle
, &device
)) {
908 dbg("bus exists... trim\n");
909 /* this shouldn't be in here, so remove
910 * the bus then re-add it...
912 ret_val
= acpi_bus_trim(device
, 1);
913 dbg("acpi_bus_trim return %x\n", ret_val
);
916 ret_val
= acpi_bus_add(&device
, pdevice
, func
->handle
,
917 ACPI_BUS_TYPE_DEVICE
);
919 dbg("error adding bus, %x\n",
921 goto acpiphp_bus_add_out
;
924 * try to start anyway. We could have failed to add
925 * simply because this bus had previously been added
926 * on another add. Don't bother with the return value
927 * we just keep going.
929 ret_val
= acpi_bus_start(device
);
937 * acpiphp_bus_trim - trim a bus from acpi subsystem
938 * @handle: handle to acpi namespace
940 static int acpiphp_bus_trim(acpi_handle handle
)
942 struct acpi_device
*device
;
945 retval
= acpi_bus_get_device(handle
, &device
);
947 dbg("acpi_device not found\n");
951 retval
= acpi_bus_trim(device
, 1);
953 err("cannot remove from acpi list\n");
959 * enable_device - enable, configure a slot
960 * @slot: slot to be enabled
962 * This function should be called per *physical slot*,
963 * not per each slot object in ACPI namespace.
965 static int __ref
enable_device(struct acpiphp_slot
*slot
)
968 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
970 struct acpiphp_func
*func
;
975 if (slot
->flags
& SLOT_ENABLED
)
978 /* sanity check: dev should be NULL when hot-plugged in */
979 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
, 0));
981 /* This case shouldn't happen */
982 err("pci_dev structure already exists.\n");
988 num
= pci_scan_slot(bus
, PCI_DEVFN(slot
->device
, 0));
990 err("No new device found\n");
995 max
= acpiphp_max_busnr(bus
);
996 for (pass
= 0; pass
< 2; pass
++) {
997 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
998 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
1000 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
1001 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
1002 max
= pci_scan_bridge(bus
, dev
, max
, pass
);
1003 if (pass
&& dev
->subordinate
)
1004 pci_bus_size_bridges(dev
->subordinate
);
1009 list_for_each (l
, &slot
->funcs
) {
1010 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1011 acpiphp_bus_add(func
);
1014 pci_bus_assign_resources(bus
);
1015 acpiphp_sanitize_bus(bus
);
1016 acpiphp_set_hpp_values(bus
);
1017 list_for_each_entry(func
, &slot
->funcs
, sibling
)
1018 acpiphp_configure_ioapics(func
->handle
);
1019 pci_enable_bridges(bus
);
1020 pci_bus_add_devices(bus
);
1022 list_for_each (l
, &slot
->funcs
) {
1023 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1024 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
1029 if (dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
&&
1030 dev
->hdr_type
!= PCI_HEADER_TYPE_CARDBUS
) {
1035 status
= find_p2p_bridge(func
->handle
, (u32
)1, bus
, NULL
);
1036 if (ACPI_FAILURE(status
))
1037 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1042 slot
->flags
|= SLOT_ENABLED
;
1048 static void disable_bridges(struct pci_bus
*bus
)
1050 struct pci_dev
*dev
;
1051 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1052 if (dev
->subordinate
) {
1053 disable_bridges(dev
->subordinate
);
1054 pci_disable_device(dev
);
1060 * disable_device - disable a slot
1061 * @slot: ACPI PHP slot
1063 static int disable_device(struct acpiphp_slot
*slot
)
1065 struct acpiphp_func
*func
;
1066 struct pci_dev
*pdev
;
1068 /* is this slot already disabled? */
1069 if (!(slot
->flags
& SLOT_ENABLED
))
1072 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
1074 /* cleanup p2p bridges under this P2P bridge */
1075 cleanup_p2p_bridge(func
->bridge
->handle
,
1076 (u32
)1, NULL
, NULL
);
1077 func
->bridge
= NULL
;
1080 pdev
= pci_get_slot(slot
->bridge
->pci_bus
,
1081 PCI_DEVFN(slot
->device
, func
->function
));
1083 pci_stop_bus_device(pdev
);
1084 if (pdev
->subordinate
) {
1085 disable_bridges(pdev
->subordinate
);
1086 pci_disable_device(pdev
);
1088 pci_remove_bus_device(pdev
);
1093 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
1094 acpiphp_unconfigure_ioapics(func
->handle
);
1095 acpiphp_bus_trim(func
->handle
);
1098 slot
->flags
&= (~SLOT_ENABLED
);
1106 * get_slot_status - get ACPI slot status
1107 * @slot: ACPI PHP slot
1109 * If a slot has _STA for each function and if any one of them
1110 * returned non-zero status, return it.
1112 * If a slot doesn't have _STA and if any one of its functions'
1113 * configuration space is configured, return 0x0f as a _STA.
1115 * Otherwise return 0.
1117 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
1120 unsigned long long sta
= 0;
1122 struct list_head
*l
;
1123 struct acpiphp_func
*func
;
1125 list_for_each (l
, &slot
->funcs
) {
1126 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1128 if (func
->flags
& FUNC_HAS_STA
) {
1129 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
1130 if (ACPI_SUCCESS(status
) && sta
)
1133 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
1134 PCI_DEVFN(slot
->device
,
1136 PCI_VENDOR_ID
, &dvid
);
1137 if (dvid
!= 0xffffffff) {
1144 return (unsigned int)sta
;
1148 * acpiphp_eject_slot - physically eject the slot
1149 * @slot: ACPI PHP slot
1151 int acpiphp_eject_slot(struct acpiphp_slot
*slot
)
1154 struct acpiphp_func
*func
;
1155 struct list_head
*l
;
1156 struct acpi_object_list arg_list
;
1157 union acpi_object arg
;
1159 list_for_each (l
, &slot
->funcs
) {
1160 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1162 /* We don't want to call _EJ0 on non-existing functions. */
1163 if ((func
->flags
& FUNC_HAS_EJ0
)) {
1164 /* _EJ0 method take one argument */
1166 arg_list
.pointer
= &arg
;
1167 arg
.type
= ACPI_TYPE_INTEGER
;
1168 arg
.integer
.value
= 1;
1170 status
= acpi_evaluate_object(func
->handle
, "_EJ0", &arg_list
, NULL
);
1171 if (ACPI_FAILURE(status
)) {
1172 warn("%s: _EJ0 failed\n", __func__
);
1182 * acpiphp_check_bridge - re-enumerate devices
1183 * @bridge: where to begin re-enumeration
1185 * Iterate over all slots under this bridge and make sure that if a
1186 * card is present they are enabled, and if not they are disabled.
1188 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
1190 struct acpiphp_slot
*slot
;
1192 int enabled
, disabled
;
1194 enabled
= disabled
= 0;
1196 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1197 unsigned int status
= get_slot_status(slot
);
1198 if (slot
->flags
& SLOT_ENABLED
) {
1199 if (status
== ACPI_STA_ALL
)
1201 retval
= acpiphp_disable_slot(slot
);
1203 err("Error occurred in disabling\n");
1206 acpiphp_eject_slot(slot
);
1210 if (status
!= ACPI_STA_ALL
)
1212 retval
= acpiphp_enable_slot(slot
);
1214 err("Error occurred in enabling\n");
1221 dbg("%s: %d enabled, %d disabled\n", __func__
, enabled
, disabled
);
1227 static void acpiphp_set_hpp_values(struct pci_bus
*bus
)
1229 struct pci_dev
*dev
;
1231 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
1232 pci_configure_slot(dev
);
1236 * Remove devices for which we could not assign resources, call
1237 * arch specific code to fix-up the bus
1239 static void acpiphp_sanitize_bus(struct pci_bus
*bus
)
1241 struct pci_dev
*dev
;
1243 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
;
1245 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1246 for (i
=0; i
<PCI_BRIDGE_RESOURCES
; i
++) {
1247 struct resource
*res
= &dev
->resource
[i
];
1248 if ((res
->flags
& type_mask
) && !res
->start
&&
1250 /* Could not assign a required resources
1251 * for this device, remove it */
1252 pci_remove_bus_device(dev
);
1259 /* Program resources in newly inserted bridge */
1260 static int acpiphp_configure_bridge (acpi_handle handle
)
1262 struct pci_bus
*bus
;
1264 if (acpi_is_root_bridge(handle
)) {
1265 struct acpi_pci_root
*root
= acpi_pci_find_root(handle
);
1268 struct pci_dev
*pdev
= acpi_get_pci_dev(handle
);
1269 bus
= pdev
->subordinate
;
1273 pci_bus_size_bridges(bus
);
1274 pci_bus_assign_resources(bus
);
1275 acpiphp_sanitize_bus(bus
);
1276 acpiphp_set_hpp_values(bus
);
1277 pci_enable_bridges(bus
);
1278 acpiphp_configure_ioapics(handle
);
1282 static void handle_bridge_insertion(acpi_handle handle
, u32 type
)
1284 struct acpi_device
*device
, *pdevice
;
1285 acpi_handle phandle
;
1287 if ((type
!= ACPI_NOTIFY_BUS_CHECK
) &&
1288 (type
!= ACPI_NOTIFY_DEVICE_CHECK
)) {
1289 err("unexpected notification type %d\n", type
);
1293 acpi_get_parent(handle
, &phandle
);
1294 if (acpi_bus_get_device(phandle
, &pdevice
)) {
1295 dbg("no parent device, assuming NULL\n");
1298 if (acpi_bus_add(&device
, pdevice
, handle
, ACPI_BUS_TYPE_DEVICE
)) {
1299 err("cannot add bridge to acpi list\n");
1302 if (!acpiphp_configure_bridge(handle
) &&
1303 !acpi_bus_start(device
))
1306 err("cannot configure and start bridge\n");
1311 * ACPI event handlers
1315 count_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1317 int *count
= (int *)context
;
1318 struct acpiphp_bridge
*bridge
;
1320 bridge
= acpiphp_handle_to_bridge(handle
);
1327 check_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1329 struct acpiphp_bridge
*bridge
;
1331 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1332 .pointer
= objname
};
1334 bridge
= acpiphp_handle_to_bridge(handle
);
1336 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1337 dbg("%s: re-enumerating slots under %s\n",
1339 acpiphp_check_bridge(bridge
);
1345 * handle_hotplug_event_bridge - handle ACPI event on bridges
1346 * @handle: Notify()'ed acpi_handle
1347 * @type: Notify code
1348 * @context: pointer to acpiphp_bridge structure
1350 * Handles ACPI event notification on {host,p2p} bridges.
1352 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
, void *context
)
1354 struct acpiphp_bridge
*bridge
;
1356 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1357 .pointer
= objname
};
1358 struct acpi_device
*device
;
1359 int num_sub_bridges
= 0;
1361 if (acpi_bus_get_device(handle
, &device
)) {
1362 /* This bridge must have just been physically inserted */
1363 handle_bridge_insertion(handle
, type
);
1367 bridge
= acpiphp_handle_to_bridge(handle
);
1368 if (type
== ACPI_NOTIFY_BUS_CHECK
) {
1369 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, ACPI_UINT32_MAX
,
1370 count_sub_bridges
, &num_sub_bridges
, NULL
);
1373 if (!bridge
&& !num_sub_bridges
) {
1374 err("cannot get bridge info\n");
1378 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1381 case ACPI_NOTIFY_BUS_CHECK
:
1382 /* bus re-enumerate */
1383 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
1385 dbg("%s: re-enumerating slots under %s\n",
1387 acpiphp_check_bridge(bridge
);
1389 if (num_sub_bridges
)
1390 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
1391 ACPI_UINT32_MAX
, check_sub_bridges
, NULL
, NULL
);
1394 case ACPI_NOTIFY_DEVICE_CHECK
:
1396 dbg("%s: Device check notify on %s\n", __func__
, objname
);
1397 acpiphp_check_bridge(bridge
);
1400 case ACPI_NOTIFY_DEVICE_WAKE
:
1402 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1405 case ACPI_NOTIFY_EJECT_REQUEST
:
1406 /* request device eject */
1407 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1408 if ((bridge
->type
!= BRIDGE_TYPE_HOST
) &&
1409 (bridge
->flags
& BRIDGE_HAS_EJ0
)) {
1410 struct acpiphp_slot
*slot
;
1411 slot
= bridge
->func
->slot
;
1412 if (!acpiphp_disable_slot(slot
))
1413 acpiphp_eject_slot(slot
);
1417 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
1418 printk(KERN_ERR
"Device %s cannot be configured due"
1419 " to a frequency mismatch\n", objname
);
1422 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
1423 printk(KERN_ERR
"Device %s cannot be configured due"
1424 " to a bus mode mismatch\n", objname
);
1427 case ACPI_NOTIFY_POWER_FAULT
:
1428 printk(KERN_ERR
"Device %s has suffered a power fault\n",
1433 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1439 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1440 * @handle: Notify()'ed acpi_handle
1441 * @type: Notify code
1442 * @context: pointer to acpiphp_func structure
1444 * Handles ACPI event notification on slots.
1446 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
, void *context
)
1448 struct acpiphp_func
*func
;
1450 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1451 .pointer
= objname
};
1453 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1455 func
= (struct acpiphp_func
*)context
;
1458 case ACPI_NOTIFY_BUS_CHECK
:
1459 /* bus re-enumerate */
1460 dbg("%s: Bus check notify on %s\n", __func__
, objname
);
1461 acpiphp_enable_slot(func
->slot
);
1464 case ACPI_NOTIFY_DEVICE_CHECK
:
1465 /* device check : re-enumerate from parent bus */
1466 dbg("%s: Device check notify on %s\n", __func__
, objname
);
1467 acpiphp_check_bridge(func
->slot
->bridge
);
1470 case ACPI_NOTIFY_DEVICE_WAKE
:
1472 dbg("%s: Device wake notify on %s\n", __func__
, objname
);
1475 case ACPI_NOTIFY_EJECT_REQUEST
:
1476 /* request device eject */
1477 dbg("%s: Device eject notify on %s\n", __func__
, objname
);
1478 if (!(acpiphp_disable_slot(func
->slot
)))
1479 acpiphp_eject_slot(func
->slot
);
1483 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1490 find_root_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1492 int *count
= (int *)context
;
1494 if (acpi_is_root_bridge(handle
)) {
1495 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1496 handle_hotplug_event_bridge
, NULL
);
1502 static struct acpi_pci_driver acpi_pci_hp_driver
= {
1504 .remove
= remove_bridge
,
1508 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1510 int __init
acpiphp_glue_init(void)
1514 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
1515 ACPI_UINT32_MAX
, find_root_bridges
, &num
, NULL
);
1520 acpi_pci_register_driver(&acpi_pci_hp_driver
);
1527 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1529 * This function frees all data allocated in acpiphp_glue_init().
1531 void acpiphp_glue_exit(void)
1533 acpi_pci_unregister_driver(&acpi_pci_hp_driver
);
1538 * acpiphp_get_num_slots - count number of slots in a system
1540 int __init
acpiphp_get_num_slots(void)
1542 struct acpiphp_bridge
*bridge
;
1545 list_for_each_entry (bridge
, &bridge_list
, list
) {
1546 dbg("Bus %04x:%02x has %d slot%s\n",
1547 pci_domain_nr(bridge
->pci_bus
),
1548 bridge
->pci_bus
->number
, bridge
->nr_slots
,
1549 bridge
->nr_slots
== 1 ? "" : "s");
1550 num_slots
+= bridge
->nr_slots
;
1553 dbg("Total %d slots\n", num_slots
);
1560 * acpiphp_for_each_slot - call function for each slot
1561 * @fn: callback function
1562 * @data: context to be passed to callback function
1564 static int acpiphp_for_each_slot(acpiphp_callback fn
, void *data
)
1566 struct list_head
*node
;
1567 struct acpiphp_bridge
*bridge
;
1568 struct acpiphp_slot
*slot
;
1571 list_for_each (node
, &bridge_list
) {
1572 bridge
= (struct acpiphp_bridge
*)node
;
1573 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1574 retval
= fn(slot
, data
);
1587 * acpiphp_enable_slot - power on slot
1588 * @slot: ACPI PHP slot
1590 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1594 mutex_lock(&slot
->crit_sect
);
1596 /* wake up all functions */
1597 retval
= power_on_slot(slot
);
1601 if (get_slot_status(slot
) == ACPI_STA_ALL
) {
1602 /* configure all functions */
1603 retval
= enable_device(slot
);
1605 power_off_slot(slot
);
1607 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__
);
1608 power_off_slot(slot
);
1612 mutex_unlock(&slot
->crit_sect
);
1617 * acpiphp_disable_slot - power off slot
1618 * @slot: ACPI PHP slot
1620 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1624 mutex_lock(&slot
->crit_sect
);
1626 /* unconfigure all functions */
1627 retval
= disable_device(slot
);
1631 /* power off all functions */
1632 retval
= power_off_slot(slot
);
1637 mutex_unlock(&slot
->crit_sect
);
1646 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1648 return (slot
->flags
& SLOT_POWEREDON
);
1656 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1660 sta
= get_slot_status(slot
);
1662 return (sta
& ACPI_STA_SHOW_IN_UI
) ? 0 : 1;
1667 * adapter presence : 1
1670 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1674 sta
= get_slot_status(slot
);
1676 return (sta
== 0) ? 0 : 1;