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 <t-kochi@bq.jp.nec.com>
34 * Lifetime rules for pci_dev:
35 * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
36 * when the driver is loaded or when an insertion event occurs. It loses
37 * a refcount when its ejected or the driver unloads.
38 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
39 * when the bridge is scanned and it loses a refcount when the bridge
43 #include <linux/init.h>
44 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/pci.h>
48 #include <linux/smp_lock.h>
49 #include <linux/mutex.h>
52 #include "pci_hotplug.h"
55 static LIST_HEAD(bridge_list
);
57 #define MY_NAME "acpiphp_glue"
59 static void handle_hotplug_event_bridge (acpi_handle
, u32
, void *);
60 static void acpiphp_sanitize_bus(struct pci_bus
*bus
);
61 static void acpiphp_set_hpp_values(acpi_handle handle
, struct pci_bus
*bus
);
65 * initialization & terminatation routines
69 * is_ejectable - determine if a slot is ejectable
70 * @handle: handle to acpi namespace
72 * Ejectable slot should satisfy at least these conditions:
85 static int is_ejectable(acpi_handle handle
)
90 status
= acpi_get_handle(handle
, "_ADR", &tmp
);
91 if (ACPI_FAILURE(status
)) {
95 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
96 if (ACPI_FAILURE(status
)) {
104 /* callback routine to check the existence of ejectable slots */
106 is_ejectable_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
108 int *count
= (int *)context
;
110 if (is_ejectable(handle
)) {
112 /* only one ejectable slot is enough */
113 return AE_CTRL_TERMINATE
;
120 /* callback routine to register each ACPI PCI slot object */
122 register_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
124 struct acpiphp_bridge
*bridge
= (struct acpiphp_bridge
*)context
;
125 struct acpiphp_slot
*slot
;
126 struct acpiphp_func
*newfunc
;
127 struct dependent_device
*dd
;
129 acpi_status status
= AE_OK
;
130 unsigned long adr
, sun
;
131 int device
, function
, retval
;
133 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &adr
);
135 if (ACPI_FAILURE(status
))
138 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
140 if (ACPI_FAILURE(status
) && !(is_dependent_device(handle
)))
143 device
= (adr
>> 16) & 0xffff;
144 function
= adr
& 0xffff;
146 newfunc
= kzalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
150 INIT_LIST_HEAD(&newfunc
->sibling
);
151 newfunc
->handle
= handle
;
152 newfunc
->function
= function
;
153 if (ACPI_SUCCESS(status
))
154 newfunc
->flags
= FUNC_HAS_EJ0
;
156 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_STA", &tmp
)))
157 newfunc
->flags
|= FUNC_HAS_STA
;
159 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS0", &tmp
)))
160 newfunc
->flags
|= FUNC_HAS_PS0
;
162 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS3", &tmp
)))
163 newfunc
->flags
|= FUNC_HAS_PS3
;
165 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_DCK", &tmp
))) {
166 newfunc
->flags
|= FUNC_HAS_DCK
;
167 /* add to devices dependent on dock station,
168 * because this may actually be the dock bridge
170 dd
= alloc_dependent_device(handle
);
172 err("Can't allocate memory for "
173 "new dependent device!\n");
175 add_dependent_device(dd
);
178 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
179 if (ACPI_FAILURE(status
))
182 /* search for objects that share the same slot */
183 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
)
184 if (slot
->device
== device
) {
185 if (slot
->sun
!= sun
)
186 warn("sibling found, but _SUN doesn't match!\n");
191 slot
= kzalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
197 slot
->bridge
= bridge
;
198 slot
->device
= device
;
200 INIT_LIST_HEAD(&slot
->funcs
);
201 mutex_init(&slot
->crit_sect
);
203 slot
->next
= bridge
->slots
;
204 bridge
->slots
= slot
;
208 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
209 slot
->sun
, pci_domain_nr(bridge
->pci_bus
),
210 bridge
->pci_bus
->number
, slot
->device
);
211 retval
= acpiphp_register_hotplug_slot(slot
);
213 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval
);
218 newfunc
->slot
= slot
;
219 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
221 /* associate corresponding pci_dev */
222 newfunc
->pci_dev
= pci_get_slot(bridge
->pci_bus
,
223 PCI_DEVFN(device
, function
));
224 if (newfunc
->pci_dev
) {
225 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
228 /* if this is a device dependent on a dock station,
229 * associate the acpiphp_func to the dependent_device
232 if ((dd
= get_dependent_device(handle
))) {
233 newfunc
->flags
|= FUNC_IS_DD
;
235 * we don't want any devices which is dependent
236 * on the dock to have it's _EJ0 method executed.
237 * because we need to run _DCK first.
239 newfunc
->flags
&= ~FUNC_HAS_EJ0
;
241 add_pci_dependent_device(dd
);
244 /* install notify handler */
245 if (!(newfunc
->flags
& FUNC_HAS_DCK
)) {
246 status
= acpi_install_notify_handler(handle
,
248 handle_hotplug_event_func
,
251 if (ACPI_FAILURE(status
))
252 err("failed to register interrupt notify handler\n");
260 bridge
->slots
= slot
->next
;
268 /* see if it's worth looking at this bridge */
269 static int detect_ejectable_slots(acpi_handle
*bridge_handle
)
276 /* only check slots defined directly below bridge object */
277 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge_handle
, (u32
)1,
278 is_ejectable_slot
, (void *)&count
, NULL
);
284 /* decode ACPI 2.0 _HPP hot plug parameters */
285 static void decode_hpp(struct acpiphp_bridge
*bridge
)
289 status
= acpi_get_hp_params_from_firmware(bridge
->pci_bus
, &bridge
->hpp
);
290 if (ACPI_FAILURE(status
) ||
291 !bridge
->hpp
.t0
|| (bridge
->hpp
.t0
->revision
> 1)) {
292 /* use default numbers */
294 "%s: Could not get hotplug parameters. Use defaults\n",
296 bridge
->hpp
.t0
= &bridge
->hpp
.type0_data
;
297 bridge
->hpp
.t0
->revision
= 0;
298 bridge
->hpp
.t0
->cache_line_size
= 0x10;
299 bridge
->hpp
.t0
->latency_timer
= 0x40;
300 bridge
->hpp
.t0
->enable_serr
= 0;
301 bridge
->hpp
.t0
->enable_perr
= 0;
307 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
308 static void init_bridge_misc(struct acpiphp_bridge
*bridge
)
312 /* decode ACPI 2.0 _HPP (hot plug parameters) */
315 /* must be added to the list prior to calling register_slot */
316 list_add(&bridge
->list
, &bridge_list
);
318 /* register all slot objects under this bridge */
319 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge
->handle
, (u32
)1,
320 register_slot
, bridge
, NULL
);
321 if (ACPI_FAILURE(status
)) {
322 list_del(&bridge
->list
);
326 /* install notify handler */
327 if (bridge
->type
!= BRIDGE_TYPE_HOST
) {
328 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
329 status
= acpi_remove_notify_handler(bridge
->func
->handle
,
331 handle_hotplug_event_func
);
332 if (ACPI_FAILURE(status
))
333 err("failed to remove notify handler\n");
335 status
= acpi_install_notify_handler(bridge
->handle
,
337 handle_hotplug_event_bridge
,
340 if (ACPI_FAILURE(status
)) {
341 err("failed to register interrupt notify handler\n");
347 /* find acpiphp_func from acpiphp_bridge */
348 static struct acpiphp_func
*acpiphp_bridge_handle_to_function(acpi_handle handle
)
350 struct list_head
*node
, *l
;
351 struct acpiphp_bridge
*bridge
;
352 struct acpiphp_slot
*slot
;
353 struct acpiphp_func
*func
;
355 list_for_each(node
, &bridge_list
) {
356 bridge
= list_entry(node
, struct acpiphp_bridge
, list
);
357 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
358 list_for_each(l
, &slot
->funcs
) {
359 func
= list_entry(l
, struct acpiphp_func
,
361 if (func
->handle
== handle
)
371 static inline void config_p2p_bridge_flags(struct acpiphp_bridge
*bridge
)
373 acpi_handle dummy_handle
;
375 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
376 "_STA", &dummy_handle
)))
377 bridge
->flags
|= BRIDGE_HAS_STA
;
379 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
380 "_EJ0", &dummy_handle
)))
381 bridge
->flags
|= BRIDGE_HAS_EJ0
;
383 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
384 "_PS0", &dummy_handle
)))
385 bridge
->flags
|= BRIDGE_HAS_PS0
;
387 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
388 "_PS3", &dummy_handle
)))
389 bridge
->flags
|= BRIDGE_HAS_PS3
;
391 /* is this ejectable p2p bridge? */
392 if (bridge
->flags
& BRIDGE_HAS_EJ0
) {
393 struct acpiphp_func
*func
;
395 dbg("found ejectable p2p bridge\n");
397 /* make link between PCI bridge and PCI function */
398 func
= acpiphp_bridge_handle_to_function(bridge
->handle
);
402 func
->bridge
= bridge
;
407 /* allocate and initialize host bridge data structure */
408 static void add_host_bridge(acpi_handle
*handle
, struct pci_bus
*pci_bus
)
410 struct acpiphp_bridge
*bridge
;
412 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
416 bridge
->type
= BRIDGE_TYPE_HOST
;
417 bridge
->handle
= handle
;
419 bridge
->pci_bus
= pci_bus
;
421 spin_lock_init(&bridge
->res_lock
);
423 init_bridge_misc(bridge
);
427 /* allocate and initialize PCI-to-PCI bridge data structure */
428 static void add_p2p_bridge(acpi_handle
*handle
, struct pci_dev
*pci_dev
)
430 struct acpiphp_bridge
*bridge
;
432 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
433 if (bridge
== NULL
) {
434 err("out of memory\n");
438 bridge
->type
= BRIDGE_TYPE_P2P
;
439 bridge
->handle
= handle
;
440 config_p2p_bridge_flags(bridge
);
442 bridge
->pci_dev
= pci_dev_get(pci_dev
);
443 bridge
->pci_bus
= pci_dev
->subordinate
;
444 if (!bridge
->pci_bus
) {
445 err("This is not a PCI-to-PCI bridge!\n");
449 spin_lock_init(&bridge
->res_lock
);
451 init_bridge_misc(bridge
);
454 pci_dev_put(pci_dev
);
460 /* callback routine to find P2P bridges */
462 find_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
465 acpi_handle dummy_handle
;
467 int device
, function
;
469 struct pci_bus
*pci_bus
= context
;
471 status
= acpi_get_handle(handle
, "_ADR", &dummy_handle
);
472 if (ACPI_FAILURE(status
))
473 return AE_OK
; /* continue */
475 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &tmp
);
476 if (ACPI_FAILURE(status
)) {
477 dbg("%s: _ADR evaluation failure\n", __FUNCTION__
);
481 device
= (tmp
>> 16) & 0xffff;
482 function
= tmp
& 0xffff;
484 dev
= pci_get_slot(pci_bus
, PCI_DEVFN(device
, function
));
486 if (!dev
|| !dev
->subordinate
)
489 /* check if this bridge has ejectable slots */
490 if ((detect_ejectable_slots(handle
) > 0) ||
491 (detect_dependent_devices(handle
) > 0)) {
492 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev
));
493 add_p2p_bridge(handle
, dev
);
496 /* search P2P bridges under this p2p bridge */
497 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
498 find_p2p_bridge
, dev
->subordinate
, NULL
);
499 if (ACPI_FAILURE(status
))
500 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
508 /* find hot-pluggable slots, and then find P2P bridge */
509 static int add_bridge(acpi_handle handle
)
514 acpi_handle dummy_handle
;
515 struct pci_bus
*pci_bus
;
517 /* if the bridge doesn't have _STA, we assume it is always there */
518 status
= acpi_get_handle(handle
, "_STA", &dummy_handle
);
519 if (ACPI_SUCCESS(status
)) {
520 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &tmp
);
521 if (ACPI_FAILURE(status
)) {
522 dbg("%s: _STA evaluation failure\n", __FUNCTION__
);
525 if ((tmp
& ACPI_STA_FUNCTIONING
) == 0)
526 /* don't register this object */
530 /* get PCI segment number */
531 status
= acpi_evaluate_integer(handle
, "_SEG", NULL
, &tmp
);
533 seg
= ACPI_SUCCESS(status
) ? tmp
: 0;
535 /* get PCI bus number */
536 status
= acpi_evaluate_integer(handle
, "_BBN", NULL
, &tmp
);
538 if (ACPI_SUCCESS(status
)) {
541 warn("can't get bus number, assuming 0\n");
545 pci_bus
= pci_find_bus(seg
, bus
);
547 err("Can't find bus %04x:%02x\n", seg
, bus
);
551 /* check if this bridge has ejectable slots */
552 if (detect_ejectable_slots(handle
) > 0) {
553 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
554 add_host_bridge(handle
, pci_bus
);
557 /* search P2P bridges under this host bridge */
558 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
559 find_p2p_bridge
, pci_bus
, NULL
);
561 if (ACPI_FAILURE(status
))
562 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
567 static struct acpiphp_bridge
*acpiphp_handle_to_bridge(acpi_handle handle
)
569 struct list_head
*head
;
570 list_for_each(head
, &bridge_list
) {
571 struct acpiphp_bridge
*bridge
= list_entry(head
,
572 struct acpiphp_bridge
, list
);
573 if (bridge
->handle
== handle
)
580 static void cleanup_bridge(struct acpiphp_bridge
*bridge
)
582 struct list_head
*list
, *tmp
;
583 struct acpiphp_slot
*slot
;
585 acpi_handle handle
= bridge
->handle
;
587 status
= acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
588 handle_hotplug_event_bridge
);
589 if (ACPI_FAILURE(status
))
590 err("failed to remove notify handler\n");
592 if ((bridge
->type
!= BRIDGE_TYPE_HOST
) &&
593 ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
)) {
594 status
= acpi_install_notify_handler(bridge
->func
->handle
,
596 handle_hotplug_event_func
,
598 if (ACPI_FAILURE(status
))
599 err("failed to install interrupt notify handler\n");
602 slot
= bridge
->slots
;
604 struct acpiphp_slot
*next
= slot
->next
;
605 list_for_each_safe (list
, tmp
, &slot
->funcs
) {
606 struct acpiphp_func
*func
;
607 func
= list_entry(list
, struct acpiphp_func
, sibling
);
608 if (!(func
->flags
& FUNC_HAS_DCK
)) {
609 status
= acpi_remove_notify_handler(func
->handle
,
611 handle_hotplug_event_func
);
612 if (ACPI_FAILURE(status
))
613 err("failed to remove notify handler\n");
615 pci_dev_put(func
->pci_dev
);
619 acpiphp_unregister_hotplug_slot(slot
);
620 list_del(&slot
->funcs
);
625 pci_dev_put(bridge
->pci_dev
);
626 list_del(&bridge
->list
);
631 cleanup_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
633 struct acpiphp_bridge
*bridge
;
635 /* cleanup p2p bridges under this P2P bridge
636 in a depth-first manner */
637 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
638 cleanup_p2p_bridge
, NULL
, NULL
);
640 if (!(bridge
= acpiphp_handle_to_bridge(handle
)))
642 cleanup_bridge(bridge
);
646 static void remove_bridge(acpi_handle handle
)
648 struct acpiphp_bridge
*bridge
;
650 /* cleanup p2p bridges under this host bridge
651 in a depth-first manner */
652 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
653 (u32
)1, cleanup_p2p_bridge
, NULL
, NULL
);
655 bridge
= acpiphp_handle_to_bridge(handle
);
657 cleanup_bridge(bridge
);
660 static struct pci_dev
* get_apic_pci_info(acpi_handle handle
)
662 struct acpi_pci_id id
;
666 if (ACPI_FAILURE(acpi_get_pci_id(handle
, &id
)))
669 bus
= pci_find_bus(id
.segment
, id
.bus
);
673 dev
= pci_get_slot(bus
, PCI_DEVFN(id
.device
, id
.function
));
677 if ((dev
->class != PCI_CLASS_SYSTEM_PIC_IOAPIC
) &&
678 (dev
->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC
))
687 static int get_gsi_base(acpi_handle handle
, u32
*gsi_base
)
692 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
693 union acpi_object
*obj
;
696 status
= acpi_evaluate_integer(handle
, "_GSB", NULL
, &gsb
);
697 if (ACPI_SUCCESS(status
)) {
698 *gsi_base
= (u32
)gsb
;
702 status
= acpi_evaluate_object(handle
, "_MAT", NULL
, &buffer
);
703 if (ACPI_FAILURE(status
) || !buffer
.length
|| !buffer
.pointer
)
706 obj
= buffer
.pointer
;
707 if (obj
->type
!= ACPI_TYPE_BUFFER
)
710 table
= obj
->buffer
.pointer
;
711 switch (((acpi_table_entry_header
*)table
)->type
) {
712 case ACPI_MADT_IOSAPIC
:
713 *gsi_base
= ((struct acpi_table_iosapic
*)table
)->global_irq_base
;
716 case ACPI_MADT_IOAPIC
:
717 *gsi_base
= ((struct acpi_table_ioapic
*)table
)->global_irq_base
;
724 kfree(buffer
.pointer
);
729 ioapic_add(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
734 struct pci_dev
*pdev
;
738 /* Evaluate _STA if present */
739 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
740 if (ACPI_SUCCESS(status
) && sta
!= ACPI_STA_ALL
)
741 return AE_CTRL_DEPTH
;
743 /* Scan only PCI bus scope */
744 status
= acpi_get_handle(handle
, "_HID", &tmp
);
745 if (ACPI_SUCCESS(status
))
746 return AE_CTRL_DEPTH
;
748 if (get_gsi_base(handle
, &gsi_base
))
751 pdev
= get_apic_pci_info(handle
);
755 if (pci_enable_device(pdev
)) {
760 pci_set_master(pdev
);
762 if (pci_request_region(pdev
, 0, "I/O APIC(acpiphp)")) {
763 pci_disable_device(pdev
);
768 phys_addr
= pci_resource_start(pdev
, 0);
769 if (acpi_register_ioapic(handle
, phys_addr
, gsi_base
)) {
770 pci_release_region(pdev
, 0);
771 pci_disable_device(pdev
);
779 static int acpiphp_configure_ioapics(acpi_handle handle
)
781 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
782 ACPI_UINT32_MAX
, ioapic_add
, NULL
, NULL
);
786 static int power_on_slot(struct acpiphp_slot
*slot
)
789 struct acpiphp_func
*func
;
793 /* if already enabled, just skip */
794 if (slot
->flags
& SLOT_POWEREDON
)
797 list_for_each (l
, &slot
->funcs
) {
798 func
= list_entry(l
, struct acpiphp_func
, sibling
);
800 if (func
->flags
& FUNC_HAS_PS0
) {
801 dbg("%s: executing _PS0\n", __FUNCTION__
);
802 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
803 if (ACPI_FAILURE(status
)) {
804 warn("%s: _PS0 failed\n", __FUNCTION__
);
812 /* TBD: evaluate _STA to check if the slot is enabled */
814 slot
->flags
|= SLOT_POWEREDON
;
821 static int power_off_slot(struct acpiphp_slot
*slot
)
824 struct acpiphp_func
*func
;
829 /* if already disabled, just skip */
830 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
833 list_for_each (l
, &slot
->funcs
) {
834 func
= list_entry(l
, struct acpiphp_func
, sibling
);
836 if (func
->flags
& FUNC_HAS_PS3
) {
837 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
838 if (ACPI_FAILURE(status
)) {
839 warn("%s: _PS3 failed\n", __FUNCTION__
);
847 /* TBD: evaluate _STA to check if the slot is disabled */
849 slot
->flags
&= (~SLOT_POWEREDON
);
858 * acpiphp_max_busnr - return the highest reserved bus number under
860 * @bus: bus to start search with
863 static unsigned char acpiphp_max_busnr(struct pci_bus
*bus
)
865 struct list_head
*tmp
;
866 unsigned char max
, n
;
869 * pci_bus_max_busnr will return the highest
870 * reserved busnr for all these children.
871 * that is equivalent to the bus->subordinate
872 * value. We don't want to use the parent's
873 * bus->subordinate value because it could have
876 max
= bus
->secondary
;
878 list_for_each(tmp
, &bus
->children
) {
879 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
888 * acpiphp_bus_add - add a new bus to acpi subsystem
889 * @func: acpiphp_func of the bridge
892 static int acpiphp_bus_add(struct acpiphp_func
*func
)
895 struct acpi_device
*device
, *pdevice
;
898 acpi_get_parent(func
->handle
, &phandle
);
899 if (acpi_bus_get_device(phandle
, &pdevice
)) {
900 dbg("no parent device, assuming NULL\n");
903 if (!acpi_bus_get_device(func
->handle
, &device
)) {
904 dbg("bus exists... trim\n");
905 /* this shouldn't be in here, so remove
906 * the bus then re-add it...
908 ret_val
= acpi_bus_trim(device
, 1);
909 dbg("acpi_bus_trim return %x\n", ret_val
);
912 ret_val
= acpi_bus_add(&device
, pdevice
, func
->handle
,
913 ACPI_BUS_TYPE_DEVICE
);
915 dbg("error adding bus, %x\n",
917 goto acpiphp_bus_add_out
;
920 * try to start anyway. We could have failed to add
921 * simply because this bus had previously been added
922 * on another add. Don't bother with the return value
923 * we just keep going.
925 ret_val
= acpi_bus_start(device
);
933 * acpiphp_bus_trim - trim a bus from acpi subsystem
934 * @handle: handle to acpi namespace
937 int acpiphp_bus_trim(acpi_handle handle
)
939 struct acpi_device
*device
;
942 retval
= acpi_bus_get_device(handle
, &device
);
944 dbg("acpi_device not found\n");
948 retval
= acpi_bus_trim(device
, 1);
950 err("cannot remove from acpi list\n");
956 * enable_device - enable, configure a slot
957 * @slot: slot to be enabled
959 * This function should be called per *physical slot*,
960 * not per each slot object in ACPI namespace.
963 static int enable_device(struct acpiphp_slot
*slot
)
966 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
968 struct acpiphp_func
*func
;
973 if (slot
->flags
& SLOT_ENABLED
)
976 /* sanity check: dev should be NULL when hot-plugged in */
977 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
, 0));
979 /* This case shouldn't happen */
980 err("pci_dev structure already exists.\n");
986 num
= pci_scan_slot(bus
, PCI_DEVFN(slot
->device
, 0));
988 err("No new device found\n");
993 max
= acpiphp_max_busnr(bus
);
994 for (pass
= 0; pass
< 2; pass
++) {
995 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
996 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
998 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
999 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
1000 max
= pci_scan_bridge(bus
, dev
, max
, pass
);
1001 if (pass
&& dev
->subordinate
)
1002 pci_bus_size_bridges(dev
->subordinate
);
1007 list_for_each (l
, &slot
->funcs
) {
1008 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1009 acpiphp_bus_add(func
);
1012 pci_bus_assign_resources(bus
);
1013 acpiphp_sanitize_bus(bus
);
1014 pci_enable_bridges(bus
);
1015 pci_bus_add_devices(bus
);
1016 acpiphp_set_hpp_values(slot
->bridge
->handle
, bus
);
1017 acpiphp_configure_ioapics(slot
->bridge
->handle
);
1019 /* associate pci_dev to our representation */
1020 list_for_each (l
, &slot
->funcs
) {
1021 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1022 func
->pci_dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
1027 if (func
->pci_dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
&&
1028 func
->pci_dev
->hdr_type
!= PCI_HEADER_TYPE_CARDBUS
)
1031 status
= find_p2p_bridge(func
->handle
, (u32
)1, bus
, NULL
);
1032 if (ACPI_FAILURE(status
))
1033 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1037 slot
->flags
|= SLOT_ENABLED
;
1045 * disable_device - disable a slot
1047 static int disable_device(struct acpiphp_slot
*slot
)
1050 struct acpiphp_func
*func
;
1051 struct list_head
*l
;
1053 /* is this slot already disabled? */
1054 if (!(slot
->flags
& SLOT_ENABLED
))
1057 list_for_each (l
, &slot
->funcs
) {
1058 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1061 /* cleanup p2p bridges under this P2P bridge */
1062 cleanup_p2p_bridge(func
->bridge
->handle
,
1063 (u32
)1, NULL
, NULL
);
1064 func
->bridge
= NULL
;
1067 acpiphp_bus_trim(func
->handle
);
1068 /* try to remove anyway.
1069 * acpiphp_bus_add might have been failed */
1074 pci_remove_bus_device(func
->pci_dev
);
1075 pci_dev_put(func
->pci_dev
);
1076 func
->pci_dev
= NULL
;
1079 slot
->flags
&= (~SLOT_ENABLED
);
1087 * get_slot_status - get ACPI slot status
1089 * if a slot has _STA for each function and if any one of them
1090 * returned non-zero status, return it
1092 * if a slot doesn't have _STA and if any one of its functions'
1093 * configuration space is configured, return 0x0f as a _STA
1095 * otherwise return 0
1097 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
1100 unsigned long sta
= 0;
1102 struct list_head
*l
;
1103 struct acpiphp_func
*func
;
1105 list_for_each (l
, &slot
->funcs
) {
1106 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1108 if (func
->flags
& FUNC_HAS_STA
) {
1109 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
1110 if (ACPI_SUCCESS(status
) && sta
)
1113 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
1114 PCI_DEVFN(slot
->device
,
1116 PCI_VENDOR_ID
, &dvid
);
1117 if (dvid
!= 0xffffffff) {
1124 return (unsigned int)sta
;
1128 * acpiphp_eject_slot - physically eject the slot
1130 static int acpiphp_eject_slot(struct acpiphp_slot
*slot
)
1133 struct acpiphp_func
*func
;
1134 struct list_head
*l
;
1135 struct acpi_object_list arg_list
;
1136 union acpi_object arg
;
1138 list_for_each (l
, &slot
->funcs
) {
1139 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1141 /* We don't want to call _EJ0 on non-existing functions. */
1142 if ((func
->flags
& FUNC_HAS_EJ0
)) {
1143 /* _EJ0 method take one argument */
1145 arg_list
.pointer
= &arg
;
1146 arg
.type
= ACPI_TYPE_INTEGER
;
1147 arg
.integer
.value
= 1;
1149 status
= acpi_evaluate_object(func
->handle
, "_EJ0", &arg_list
, NULL
);
1150 if (ACPI_FAILURE(status
)) {
1151 warn("%s: _EJ0 failed\n", __FUNCTION__
);
1161 * acpiphp_check_bridge - re-enumerate devices
1163 * Iterate over all slots under this bridge and make sure that if a
1164 * card is present they are enabled, and if not they are disabled.
1166 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
1168 struct acpiphp_slot
*slot
;
1170 int enabled
, disabled
;
1172 enabled
= disabled
= 0;
1174 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1175 unsigned int status
= get_slot_status(slot
);
1176 if (slot
->flags
& SLOT_ENABLED
) {
1177 if (status
== ACPI_STA_ALL
)
1179 retval
= acpiphp_disable_slot(slot
);
1181 err("Error occurred in disabling\n");
1184 acpiphp_eject_slot(slot
);
1188 if (status
!= ACPI_STA_ALL
)
1190 retval
= acpiphp_enable_slot(slot
);
1192 err("Error occurred in enabling\n");
1199 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__
, enabled
, disabled
);
1205 static void program_hpp(struct pci_dev
*dev
, struct acpiphp_bridge
*bridge
)
1207 u16 pci_cmd
, pci_bctl
;
1208 struct pci_dev
*cdev
;
1210 /* Program hpp values for this device */
1211 if (!(dev
->hdr_type
== PCI_HEADER_TYPE_NORMAL
||
1212 (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
&&
1213 (dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
)))
1216 pci_write_config_byte(dev
, PCI_CACHE_LINE_SIZE
,
1217 bridge
->hpp
.t0
->cache_line_size
);
1218 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
,
1219 bridge
->hpp
.t0
->latency_timer
);
1220 pci_read_config_word(dev
, PCI_COMMAND
, &pci_cmd
);
1221 if (bridge
->hpp
.t0
->enable_serr
)
1222 pci_cmd
|= PCI_COMMAND_SERR
;
1224 pci_cmd
&= ~PCI_COMMAND_SERR
;
1225 if (bridge
->hpp
.t0
->enable_perr
)
1226 pci_cmd
|= PCI_COMMAND_PARITY
;
1228 pci_cmd
&= ~PCI_COMMAND_PARITY
;
1229 pci_write_config_word(dev
, PCI_COMMAND
, pci_cmd
);
1231 /* Program bridge control value and child devices */
1232 if ((dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
) {
1233 pci_write_config_byte(dev
, PCI_SEC_LATENCY_TIMER
,
1234 bridge
->hpp
.t0
->latency_timer
);
1235 pci_read_config_word(dev
, PCI_BRIDGE_CONTROL
, &pci_bctl
);
1236 if (bridge
->hpp
.t0
->enable_serr
)
1237 pci_bctl
|= PCI_BRIDGE_CTL_SERR
;
1239 pci_bctl
&= ~PCI_BRIDGE_CTL_SERR
;
1240 if (bridge
->hpp
.t0
->enable_perr
)
1241 pci_bctl
|= PCI_BRIDGE_CTL_PARITY
;
1243 pci_bctl
&= ~PCI_BRIDGE_CTL_PARITY
;
1244 pci_write_config_word(dev
, PCI_BRIDGE_CONTROL
, pci_bctl
);
1245 if (dev
->subordinate
) {
1246 list_for_each_entry(cdev
, &dev
->subordinate
->devices
,
1248 program_hpp(cdev
, bridge
);
1253 static void acpiphp_set_hpp_values(acpi_handle handle
, struct pci_bus
*bus
)
1255 struct acpiphp_bridge bridge
;
1256 struct pci_dev
*dev
;
1258 memset(&bridge
, 0, sizeof(bridge
));
1259 bridge
.handle
= handle
;
1260 bridge
.pci_bus
= bus
;
1261 bridge
.pci_dev
= bus
->self
;
1262 decode_hpp(&bridge
);
1263 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
1264 program_hpp(dev
, &bridge
);
1269 * Remove devices for which we could not assign resources, call
1270 * arch specific code to fix-up the bus
1272 static void acpiphp_sanitize_bus(struct pci_bus
*bus
)
1274 struct pci_dev
*dev
;
1276 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
;
1278 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1279 for (i
=0; i
<PCI_BRIDGE_RESOURCES
; i
++) {
1280 struct resource
*res
= &dev
->resource
[i
];
1281 if ((res
->flags
& type_mask
) && !res
->start
&&
1283 /* Could not assign a required resources
1284 * for this device, remove it */
1285 pci_remove_bus_device(dev
);
1292 /* Program resources in newly inserted bridge */
1293 static int acpiphp_configure_bridge (acpi_handle handle
)
1295 struct acpi_pci_id pci_id
;
1296 struct pci_bus
*bus
;
1298 if (ACPI_FAILURE(acpi_get_pci_id(handle
, &pci_id
))) {
1299 err("cannot get PCI domain and bus number for bridge\n");
1302 bus
= pci_find_bus(pci_id
.segment
, pci_id
.bus
);
1304 err("cannot find bus %d:%d\n",
1305 pci_id
.segment
, pci_id
.bus
);
1309 pci_bus_size_bridges(bus
);
1310 pci_bus_assign_resources(bus
);
1311 acpiphp_sanitize_bus(bus
);
1312 acpiphp_set_hpp_values(handle
, bus
);
1313 pci_enable_bridges(bus
);
1314 acpiphp_configure_ioapics(handle
);
1318 static void handle_bridge_insertion(acpi_handle handle
, u32 type
)
1320 struct acpi_device
*device
, *pdevice
;
1321 acpi_handle phandle
;
1323 if ((type
!= ACPI_NOTIFY_BUS_CHECK
) &&
1324 (type
!= ACPI_NOTIFY_DEVICE_CHECK
)) {
1325 err("unexpected notification type %d\n", type
);
1329 acpi_get_parent(handle
, &phandle
);
1330 if (acpi_bus_get_device(phandle
, &pdevice
)) {
1331 dbg("no parent device, assuming NULL\n");
1334 if (acpi_bus_add(&device
, pdevice
, handle
, ACPI_BUS_TYPE_DEVICE
)) {
1335 err("cannot add bridge to acpi list\n");
1338 if (!acpiphp_configure_bridge(handle
) &&
1339 !acpi_bus_start(device
))
1342 err("cannot configure and start bridge\n");
1347 * ACPI event handlers
1351 * handle_hotplug_event_bridge - handle ACPI event on bridges
1353 * @handle: Notify()'ed acpi_handle
1354 * @type: Notify code
1355 * @context: pointer to acpiphp_bridge structure
1357 * handles ACPI event notification on {host,p2p} bridges
1360 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
, void *context
)
1362 struct acpiphp_bridge
*bridge
;
1364 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1365 .pointer
= objname
};
1366 struct acpi_device
*device
;
1368 if (acpi_bus_get_device(handle
, &device
)) {
1369 /* This bridge must have just been physically inserted */
1370 handle_bridge_insertion(handle
, type
);
1374 bridge
= acpiphp_handle_to_bridge(handle
);
1376 err("cannot get bridge info\n");
1380 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1383 case ACPI_NOTIFY_BUS_CHECK
:
1384 /* bus re-enumerate */
1385 dbg("%s: Bus check notify on %s\n", __FUNCTION__
, objname
);
1386 acpiphp_check_bridge(bridge
);
1389 case ACPI_NOTIFY_DEVICE_CHECK
:
1391 dbg("%s: Device check notify on %s\n", __FUNCTION__
, objname
);
1392 acpiphp_check_bridge(bridge
);
1395 case ACPI_NOTIFY_DEVICE_WAKE
:
1397 dbg("%s: Device wake notify on %s\n", __FUNCTION__
, objname
);
1400 case ACPI_NOTIFY_EJECT_REQUEST
:
1401 /* request device eject */
1402 dbg("%s: Device eject notify on %s\n", __FUNCTION__
, objname
);
1403 if ((bridge
->type
!= BRIDGE_TYPE_HOST
) &&
1404 (bridge
->flags
& BRIDGE_HAS_EJ0
)) {
1405 struct acpiphp_slot
*slot
;
1406 slot
= bridge
->func
->slot
;
1407 if (!acpiphp_disable_slot(slot
))
1408 acpiphp_eject_slot(slot
);
1412 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
1413 printk(KERN_ERR
"Device %s cannot be configured due"
1414 " to a frequency mismatch\n", objname
);
1417 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
1418 printk(KERN_ERR
"Device %s cannot be configured due"
1419 " to a bus mode mismatch\n", objname
);
1422 case ACPI_NOTIFY_POWER_FAULT
:
1423 printk(KERN_ERR
"Device %s has suffered a power fault\n",
1428 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1434 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1436 * @handle: Notify()'ed acpi_handle
1437 * @type: Notify code
1438 * @context: pointer to acpiphp_func structure
1440 * handles ACPI event notification on slots
1443 void handle_hotplug_event_func(acpi_handle handle
, u32 type
, void *context
)
1445 struct acpiphp_func
*func
;
1447 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1448 .pointer
= objname
};
1450 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1452 func
= (struct acpiphp_func
*)context
;
1455 case ACPI_NOTIFY_BUS_CHECK
:
1456 /* bus re-enumerate */
1457 dbg("%s: Bus check notify on %s\n", __FUNCTION__
, objname
);
1458 acpiphp_enable_slot(func
->slot
);
1461 case ACPI_NOTIFY_DEVICE_CHECK
:
1462 /* device check : re-enumerate from parent bus */
1463 dbg("%s: Device check notify on %s\n", __FUNCTION__
, objname
);
1464 acpiphp_check_bridge(func
->slot
->bridge
);
1467 case ACPI_NOTIFY_DEVICE_WAKE
:
1469 dbg("%s: Device wake notify on %s\n", __FUNCTION__
, objname
);
1472 case ACPI_NOTIFY_EJECT_REQUEST
:
1473 /* request device eject */
1474 dbg("%s: Device eject notify on %s\n", __FUNCTION__
, objname
);
1475 if (!(acpiphp_disable_slot(func
->slot
)))
1476 acpiphp_eject_slot(func
->slot
);
1480 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1487 find_root_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1489 int *count
= (int *)context
;
1491 if (acpi_root_bridge(handle
)) {
1492 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1493 handle_hotplug_event_bridge
, NULL
);
1499 static struct acpi_pci_driver acpi_pci_hp_driver
= {
1501 .remove
= remove_bridge
,
1505 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1508 int __init
acpiphp_glue_init(void)
1512 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
1513 ACPI_UINT32_MAX
, find_root_bridges
, &num
, NULL
);
1518 acpi_pci_register_driver(&acpi_pci_hp_driver
);
1525 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1527 * This function frees all data allocated in acpiphp_glue_init()
1529 void __exit
acpiphp_glue_exit(void)
1531 acpi_pci_unregister_driver(&acpi_pci_hp_driver
);
1536 * acpiphp_get_num_slots - count number of slots in a system
1538 int __init
acpiphp_get_num_slots(void)
1540 struct list_head
*node
;
1541 struct acpiphp_bridge
*bridge
;
1546 list_for_each (node
, &bridge_list
) {
1547 bridge
= (struct acpiphp_bridge
*)node
;
1548 dbg("Bus %04x:%02x has %d slot%s\n",
1549 pci_domain_nr(bridge
->pci_bus
),
1550 bridge
->pci_bus
->number
, bridge
->nr_slots
,
1551 bridge
->nr_slots
== 1 ? "" : "s");
1552 num_slots
+= bridge
->nr_slots
;
1555 dbg("Total %d slots\n", num_slots
);
1562 * acpiphp_for_each_slot - call function for each slot
1563 * @fn: callback function
1564 * @data: context to be passed to callback function
1567 static int acpiphp_for_each_slot(acpiphp_callback fn
, void *data
)
1569 struct list_head
*node
;
1570 struct acpiphp_bridge
*bridge
;
1571 struct acpiphp_slot
*slot
;
1574 list_for_each (node
, &bridge_list
) {
1575 bridge
= (struct acpiphp_bridge
*)node
;
1576 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1577 retval
= fn(slot
, data
);
1590 * acpiphp_enable_slot - power on slot
1592 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1596 mutex_lock(&slot
->crit_sect
);
1598 /* wake up all functions */
1599 retval
= power_on_slot(slot
);
1603 if (get_slot_status(slot
) == ACPI_STA_ALL
) {
1604 /* configure all functions */
1605 retval
= enable_device(slot
);
1607 power_off_slot(slot
);
1609 dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__
);
1610 power_off_slot(slot
);
1614 mutex_unlock(&slot
->crit_sect
);
1619 * acpiphp_disable_slot - power off slot
1621 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1625 mutex_lock(&slot
->crit_sect
);
1627 /* unconfigure all functions */
1628 retval
= disable_device(slot
);
1632 /* power off all functions */
1633 retval
= power_off_slot(slot
);
1638 mutex_unlock(&slot
->crit_sect
);
1647 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1649 return (slot
->flags
& SLOT_POWEREDON
);
1657 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1661 sta
= get_slot_status(slot
);
1663 return (sta
& ACPI_STA_SHOW_IN_UI
) ? 1 : 0;
1668 * adapter presence : 1
1671 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1675 sta
= get_slot_status(slot
);
1677 return (sta
== 0) ? 0 : 1;
1682 * pci address (seg/bus/dev)
1684 u32
acpiphp_get_address(struct acpiphp_slot
*slot
)
1687 struct pci_bus
*pci_bus
= slot
->bridge
->pci_bus
;
1689 address
= (pci_domain_nr(pci_bus
) << 16) |
1690 (pci_bus
->number
<< 8) |