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_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/pci_hotplug.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(acpi_handle handle
, struct pci_bus
*bus
);
63 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
, void *context
);
67 * initialization & terminatation routines
71 * is_ejectable - determine if a slot is ejectable
72 * @handle: handle to acpi namespace
74 * Ejectable slot should satisfy at least these conditions:
87 static int is_ejectable(acpi_handle handle
)
92 status
= acpi_get_handle(handle
, "_ADR", &tmp
);
93 if (ACPI_FAILURE(status
)) {
97 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
98 if (ACPI_FAILURE(status
)) {
106 /* callback routine to check the existence of ejectable slots */
108 is_ejectable_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
110 int *count
= (int *)context
;
112 if (is_ejectable(handle
)) {
114 /* only one ejectable slot is enough */
115 return AE_CTRL_TERMINATE
;
121 /* callback routine to check for the existance of a pci dock device */
123 is_pci_dock_device(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
125 int *count
= (int *)context
;
127 if (is_dock_device(handle
)) {
129 return AE_CTRL_TERMINATE
;
139 * the _DCK method can do funny things... and sometimes not
142 * TBD - figure out a way to only call fixups for
143 * systems that require them.
145 static int post_dock_fixups(struct notifier_block
*nb
, unsigned long val
,
148 struct acpiphp_func
*func
= container_of(nb
, struct acpiphp_func
, nb
);
149 struct pci_bus
*bus
= func
->slot
->bridge
->pci_bus
;
155 /* fixup bad _DCK function that rewrites
156 * secondary bridge on slot
158 pci_read_config_dword(bus
->self
,
162 if (((buses
>> 8) & 0xff) != bus
->secondary
) {
163 buses
= (buses
& 0xff000000)
164 | ((unsigned int)(bus
->primary
) << 0)
165 | ((unsigned int)(bus
->secondary
) << 8)
166 | ((unsigned int)(bus
->subordinate
) << 16);
167 pci_write_config_dword(bus
->self
, PCI_PRIMARY_BUS
, buses
);
175 /* callback routine to register each ACPI PCI slot object */
177 register_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
179 struct acpiphp_bridge
*bridge
= (struct acpiphp_bridge
*)context
;
180 struct acpiphp_slot
*slot
;
181 struct acpiphp_func
*newfunc
;
183 acpi_status status
= AE_OK
;
184 unsigned long adr
, sun
;
185 int device
, function
, retval
;
187 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &adr
);
189 if (ACPI_FAILURE(status
))
192 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
194 if (ACPI_FAILURE(status
) && !(is_dock_device(handle
)))
197 device
= (adr
>> 16) & 0xffff;
198 function
= adr
& 0xffff;
200 newfunc
= kzalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
204 INIT_LIST_HEAD(&newfunc
->sibling
);
205 newfunc
->handle
= handle
;
206 newfunc
->function
= function
;
207 if (ACPI_SUCCESS(status
))
208 newfunc
->flags
= FUNC_HAS_EJ0
;
210 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_STA", &tmp
)))
211 newfunc
->flags
|= FUNC_HAS_STA
;
213 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS0", &tmp
)))
214 newfunc
->flags
|= FUNC_HAS_PS0
;
216 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS3", &tmp
)))
217 newfunc
->flags
|= FUNC_HAS_PS3
;
219 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_DCK", &tmp
)))
220 newfunc
->flags
|= FUNC_HAS_DCK
;
222 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
223 if (ACPI_FAILURE(status
)) {
225 * use the count of the number of slots we've found
226 * for the number of the slot
228 sun
= bridge
->nr_slots
+1;
231 /* search for objects that share the same slot */
232 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
)
233 if (slot
->device
== device
) {
234 if (slot
->sun
!= sun
)
235 warn("sibling found, but _SUN doesn't match!\n");
240 slot
= kzalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
246 slot
->bridge
= bridge
;
247 slot
->device
= device
;
249 INIT_LIST_HEAD(&slot
->funcs
);
250 mutex_init(&slot
->crit_sect
);
252 slot
->next
= bridge
->slots
;
253 bridge
->slots
= slot
;
257 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
258 slot
->sun
, pci_domain_nr(bridge
->pci_bus
),
259 bridge
->pci_bus
->number
, slot
->device
);
260 retval
= acpiphp_register_hotplug_slot(slot
);
262 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval
);
267 newfunc
->slot
= slot
;
268 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
270 /* associate corresponding pci_dev */
271 newfunc
->pci_dev
= pci_get_slot(bridge
->pci_bus
,
272 PCI_DEVFN(device
, function
));
273 if (newfunc
->pci_dev
) {
274 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
277 if (is_dock_device(handle
)) {
278 /* we don't want to call this device's _EJ0
279 * because we want the dock notify handler
280 * to call it after it calls _DCK
282 newfunc
->flags
&= ~FUNC_HAS_EJ0
;
283 if (register_hotplug_dock_device(handle
,
284 handle_hotplug_event_func
, newfunc
))
285 dbg("failed to register dock device\n");
287 /* we need to be notified when dock events happen
288 * outside of the hotplug operation, since we may
289 * need to do fixups before we can hotplug.
291 newfunc
->nb
.notifier_call
= post_dock_fixups
;
292 if (register_dock_notifier(&newfunc
->nb
))
293 dbg("failed to register a dock notifier");
296 /* install notify handler */
297 if (!(newfunc
->flags
& FUNC_HAS_DCK
)) {
298 status
= acpi_install_notify_handler(handle
,
300 handle_hotplug_event_func
,
303 if (ACPI_FAILURE(status
))
304 err("failed to register interrupt notify handler\n");
312 bridge
->slots
= slot
->next
;
320 /* see if it's worth looking at this bridge */
321 static int detect_ejectable_slots(acpi_handle
*bridge_handle
)
328 /* only check slots defined directly below bridge object */
329 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge_handle
, (u32
)1,
330 is_ejectable_slot
, (void *)&count
, NULL
);
333 * we also need to add this bridge if there is a dock bridge or
334 * other pci device on a dock station (removable)
337 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge_handle
,
338 (u32
)1, is_pci_dock_device
, (void *)&count
,
345 /* decode ACPI 2.0 _HPP hot plug parameters */
346 static void decode_hpp(struct acpiphp_bridge
*bridge
)
350 status
= acpi_get_hp_params_from_firmware(bridge
->pci_bus
, &bridge
->hpp
);
351 if (ACPI_FAILURE(status
) ||
352 !bridge
->hpp
.t0
|| (bridge
->hpp
.t0
->revision
> 1)) {
353 /* use default numbers */
355 "%s: Could not get hotplug parameters. Use defaults\n",
357 bridge
->hpp
.t0
= &bridge
->hpp
.type0_data
;
358 bridge
->hpp
.t0
->revision
= 0;
359 bridge
->hpp
.t0
->cache_line_size
= 0x10;
360 bridge
->hpp
.t0
->latency_timer
= 0x40;
361 bridge
->hpp
.t0
->enable_serr
= 0;
362 bridge
->hpp
.t0
->enable_perr
= 0;
368 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
369 static void init_bridge_misc(struct acpiphp_bridge
*bridge
)
373 /* decode ACPI 2.0 _HPP (hot plug parameters) */
376 /* must be added to the list prior to calling register_slot */
377 list_add(&bridge
->list
, &bridge_list
);
379 /* register all slot objects under this bridge */
380 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge
->handle
, (u32
)1,
381 register_slot
, bridge
, NULL
);
382 if (ACPI_FAILURE(status
)) {
383 list_del(&bridge
->list
);
387 /* install notify handler */
388 if (bridge
->type
!= BRIDGE_TYPE_HOST
) {
389 if ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
) {
390 status
= acpi_remove_notify_handler(bridge
->func
->handle
,
392 handle_hotplug_event_func
);
393 if (ACPI_FAILURE(status
))
394 err("failed to remove notify handler\n");
396 status
= acpi_install_notify_handler(bridge
->handle
,
398 handle_hotplug_event_bridge
,
401 if (ACPI_FAILURE(status
)) {
402 err("failed to register interrupt notify handler\n");
408 /* find acpiphp_func from acpiphp_bridge */
409 static struct acpiphp_func
*acpiphp_bridge_handle_to_function(acpi_handle handle
)
411 struct list_head
*node
, *l
;
412 struct acpiphp_bridge
*bridge
;
413 struct acpiphp_slot
*slot
;
414 struct acpiphp_func
*func
;
416 list_for_each(node
, &bridge_list
) {
417 bridge
= list_entry(node
, struct acpiphp_bridge
, list
);
418 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
419 list_for_each(l
, &slot
->funcs
) {
420 func
= list_entry(l
, struct acpiphp_func
,
422 if (func
->handle
== handle
)
432 static inline void config_p2p_bridge_flags(struct acpiphp_bridge
*bridge
)
434 acpi_handle dummy_handle
;
436 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
437 "_STA", &dummy_handle
)))
438 bridge
->flags
|= BRIDGE_HAS_STA
;
440 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
441 "_EJ0", &dummy_handle
)))
442 bridge
->flags
|= BRIDGE_HAS_EJ0
;
444 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
445 "_PS0", &dummy_handle
)))
446 bridge
->flags
|= BRIDGE_HAS_PS0
;
448 if (ACPI_SUCCESS(acpi_get_handle(bridge
->handle
,
449 "_PS3", &dummy_handle
)))
450 bridge
->flags
|= BRIDGE_HAS_PS3
;
452 /* is this ejectable p2p bridge? */
453 if (bridge
->flags
& BRIDGE_HAS_EJ0
) {
454 struct acpiphp_func
*func
;
456 dbg("found ejectable p2p bridge\n");
458 /* make link between PCI bridge and PCI function */
459 func
= acpiphp_bridge_handle_to_function(bridge
->handle
);
463 func
->bridge
= bridge
;
468 /* allocate and initialize host bridge data structure */
469 static void add_host_bridge(acpi_handle
*handle
, struct pci_bus
*pci_bus
)
471 struct acpiphp_bridge
*bridge
;
473 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
477 bridge
->type
= BRIDGE_TYPE_HOST
;
478 bridge
->handle
= handle
;
480 bridge
->pci_bus
= pci_bus
;
482 spin_lock_init(&bridge
->res_lock
);
484 init_bridge_misc(bridge
);
488 /* allocate and initialize PCI-to-PCI bridge data structure */
489 static void add_p2p_bridge(acpi_handle
*handle
, struct pci_dev
*pci_dev
)
491 struct acpiphp_bridge
*bridge
;
493 bridge
= kzalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
494 if (bridge
== NULL
) {
495 err("out of memory\n");
499 bridge
->type
= BRIDGE_TYPE_P2P
;
500 bridge
->handle
= handle
;
501 config_p2p_bridge_flags(bridge
);
503 bridge
->pci_dev
= pci_dev_get(pci_dev
);
504 bridge
->pci_bus
= pci_dev
->subordinate
;
505 if (!bridge
->pci_bus
) {
506 err("This is not a PCI-to-PCI bridge!\n");
510 spin_lock_init(&bridge
->res_lock
);
512 init_bridge_misc(bridge
);
515 pci_dev_put(pci_dev
);
521 /* callback routine to find P2P bridges */
523 find_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
526 acpi_handle dummy_handle
;
528 int device
, function
;
530 struct pci_bus
*pci_bus
= context
;
532 status
= acpi_get_handle(handle
, "_ADR", &dummy_handle
);
533 if (ACPI_FAILURE(status
))
534 return AE_OK
; /* continue */
536 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &tmp
);
537 if (ACPI_FAILURE(status
)) {
538 dbg("%s: _ADR evaluation failure\n", __FUNCTION__
);
542 device
= (tmp
>> 16) & 0xffff;
543 function
= tmp
& 0xffff;
545 dev
= pci_get_slot(pci_bus
, PCI_DEVFN(device
, function
));
547 if (!dev
|| !dev
->subordinate
)
550 /* check if this bridge has ejectable slots */
551 if ((detect_ejectable_slots(handle
) > 0)) {
552 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev
));
553 add_p2p_bridge(handle
, dev
);
556 /* search P2P bridges under this p2p bridge */
557 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
558 find_p2p_bridge
, dev
->subordinate
, NULL
);
559 if (ACPI_FAILURE(status
))
560 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
568 /* find hot-pluggable slots, and then find P2P bridge */
569 static int add_bridge(acpi_handle handle
)
574 acpi_handle dummy_handle
;
575 struct pci_bus
*pci_bus
;
577 /* if the bridge doesn't have _STA, we assume it is always there */
578 status
= acpi_get_handle(handle
, "_STA", &dummy_handle
);
579 if (ACPI_SUCCESS(status
)) {
580 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &tmp
);
581 if (ACPI_FAILURE(status
)) {
582 dbg("%s: _STA evaluation failure\n", __FUNCTION__
);
585 if ((tmp
& ACPI_STA_FUNCTIONING
) == 0)
586 /* don't register this object */
590 /* get PCI segment number */
591 status
= acpi_evaluate_integer(handle
, "_SEG", NULL
, &tmp
);
593 seg
= ACPI_SUCCESS(status
) ? tmp
: 0;
595 /* get PCI bus number */
596 status
= acpi_evaluate_integer(handle
, "_BBN", NULL
, &tmp
);
598 if (ACPI_SUCCESS(status
)) {
601 warn("can't get bus number, assuming 0\n");
605 pci_bus
= pci_find_bus(seg
, bus
);
607 err("Can't find bus %04x:%02x\n", seg
, bus
);
611 /* check if this bridge has ejectable slots */
612 if (detect_ejectable_slots(handle
) > 0) {
613 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
614 add_host_bridge(handle
, pci_bus
);
617 /* search P2P bridges under this host bridge */
618 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
619 find_p2p_bridge
, pci_bus
, NULL
);
621 if (ACPI_FAILURE(status
))
622 warn("find_p2p_bridge failed (error code = 0x%x)\n", status
);
627 static struct acpiphp_bridge
*acpiphp_handle_to_bridge(acpi_handle handle
)
629 struct list_head
*head
;
630 list_for_each(head
, &bridge_list
) {
631 struct acpiphp_bridge
*bridge
= list_entry(head
,
632 struct acpiphp_bridge
, list
);
633 if (bridge
->handle
== handle
)
640 static void cleanup_bridge(struct acpiphp_bridge
*bridge
)
642 struct list_head
*list
, *tmp
;
643 struct acpiphp_slot
*slot
;
645 acpi_handle handle
= bridge
->handle
;
647 status
= acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
648 handle_hotplug_event_bridge
);
649 if (ACPI_FAILURE(status
))
650 err("failed to remove notify handler\n");
652 if ((bridge
->type
!= BRIDGE_TYPE_HOST
) &&
653 ((bridge
->flags
& BRIDGE_HAS_EJ0
) && bridge
->func
)) {
654 status
= acpi_install_notify_handler(bridge
->func
->handle
,
656 handle_hotplug_event_func
,
658 if (ACPI_FAILURE(status
))
659 err("failed to install interrupt notify handler\n");
662 slot
= bridge
->slots
;
664 struct acpiphp_slot
*next
= slot
->next
;
665 list_for_each_safe (list
, tmp
, &slot
->funcs
) {
666 struct acpiphp_func
*func
;
667 func
= list_entry(list
, struct acpiphp_func
, sibling
);
668 if (is_dock_device(func
->handle
)) {
669 unregister_hotplug_dock_device(func
->handle
);
670 unregister_dock_notifier(&func
->nb
);
672 if (!(func
->flags
& FUNC_HAS_DCK
)) {
673 status
= acpi_remove_notify_handler(func
->handle
,
675 handle_hotplug_event_func
);
676 if (ACPI_FAILURE(status
))
677 err("failed to remove notify handler\n");
679 pci_dev_put(func
->pci_dev
);
683 acpiphp_unregister_hotplug_slot(slot
);
684 list_del(&slot
->funcs
);
689 pci_dev_put(bridge
->pci_dev
);
690 list_del(&bridge
->list
);
695 cleanup_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
697 struct acpiphp_bridge
*bridge
;
699 /* cleanup p2p bridges under this P2P bridge
700 in a depth-first manner */
701 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
702 cleanup_p2p_bridge
, NULL
, NULL
);
704 if (!(bridge
= acpiphp_handle_to_bridge(handle
)))
706 cleanup_bridge(bridge
);
710 static void remove_bridge(acpi_handle handle
)
712 struct acpiphp_bridge
*bridge
;
714 /* cleanup p2p bridges under this host bridge
715 in a depth-first manner */
716 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
717 (u32
)1, cleanup_p2p_bridge
, NULL
, NULL
);
719 bridge
= acpiphp_handle_to_bridge(handle
);
721 cleanup_bridge(bridge
);
724 static struct pci_dev
* get_apic_pci_info(acpi_handle handle
)
726 struct acpi_pci_id id
;
730 if (ACPI_FAILURE(acpi_get_pci_id(handle
, &id
)))
733 bus
= pci_find_bus(id
.segment
, id
.bus
);
737 dev
= pci_get_slot(bus
, PCI_DEVFN(id
.device
, id
.function
));
741 if ((dev
->class != PCI_CLASS_SYSTEM_PIC_IOAPIC
) &&
742 (dev
->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC
))
751 static int get_gsi_base(acpi_handle handle
, u32
*gsi_base
)
756 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
757 union acpi_object
*obj
;
760 status
= acpi_evaluate_integer(handle
, "_GSB", NULL
, &gsb
);
761 if (ACPI_SUCCESS(status
)) {
762 *gsi_base
= (u32
)gsb
;
766 status
= acpi_evaluate_object(handle
, "_MAT", NULL
, &buffer
);
767 if (ACPI_FAILURE(status
) || !buffer
.length
|| !buffer
.pointer
)
770 obj
= buffer
.pointer
;
771 if (obj
->type
!= ACPI_TYPE_BUFFER
)
774 table
= obj
->buffer
.pointer
;
775 switch (((struct acpi_subtable_header
*)table
)->type
) {
776 case ACPI_MADT_TYPE_IO_SAPIC
:
777 *gsi_base
= ((struct acpi_madt_io_sapic
*)table
)->global_irq_base
;
780 case ACPI_MADT_TYPE_IO_APIC
:
781 *gsi_base
= ((struct acpi_madt_io_apic
*)table
)->global_irq_base
;
788 kfree(buffer
.pointer
);
793 ioapic_add(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
798 struct pci_dev
*pdev
;
801 struct acpiphp_ioapic
*ioapic
;
803 /* Evaluate _STA if present */
804 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
805 if (ACPI_SUCCESS(status
) && sta
!= ACPI_STA_ALL
)
806 return AE_CTRL_DEPTH
;
808 /* Scan only PCI bus scope */
809 status
= acpi_get_handle(handle
, "_HID", &tmp
);
810 if (ACPI_SUCCESS(status
))
811 return AE_CTRL_DEPTH
;
813 if (get_gsi_base(handle
, &gsi_base
))
816 ioapic
= kmalloc(sizeof(*ioapic
), GFP_KERNEL
);
820 pdev
= get_apic_pci_info(handle
);
824 if (pci_enable_device(pdev
))
825 goto exit_pci_dev_put
;
827 pci_set_master(pdev
);
829 if (pci_request_region(pdev
, 0, "I/O APIC(acpiphp)"))
830 goto exit_pci_disable_device
;
832 phys_addr
= pci_resource_start(pdev
, 0);
833 if (acpi_register_ioapic(handle
, phys_addr
, gsi_base
))
834 goto exit_pci_release_region
;
836 ioapic
->gsi_base
= gsi_base
;
838 spin_lock(&ioapic_list_lock
);
839 list_add_tail(&ioapic
->list
, &ioapic_list
);
840 spin_unlock(&ioapic_list_lock
);
844 exit_pci_release_region
:
845 pci_release_region(pdev
, 0);
846 exit_pci_disable_device
:
847 pci_disable_device(pdev
);
857 ioapic_remove(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
863 struct acpiphp_ioapic
*pos
, *n
, *ioapic
= NULL
;
865 /* Evaluate _STA if present */
866 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
867 if (ACPI_SUCCESS(status
) && sta
!= ACPI_STA_ALL
)
868 return AE_CTRL_DEPTH
;
870 /* Scan only PCI bus scope */
871 status
= acpi_get_handle(handle
, "_HID", &tmp
);
872 if (ACPI_SUCCESS(status
))
873 return AE_CTRL_DEPTH
;
875 if (get_gsi_base(handle
, &gsi_base
))
878 acpi_unregister_ioapic(handle
, gsi_base
);
880 spin_lock(&ioapic_list_lock
);
881 list_for_each_entry_safe(pos
, n
, &ioapic_list
, list
) {
882 if (pos
->gsi_base
!= gsi_base
)
885 list_del(&ioapic
->list
);
888 spin_unlock(&ioapic_list_lock
);
893 pci_release_region(ioapic
->dev
, 0);
894 pci_disable_device(ioapic
->dev
);
895 pci_dev_put(ioapic
->dev
);
901 static int acpiphp_configure_ioapics(acpi_handle handle
)
903 ioapic_add(handle
, 0, NULL
, NULL
);
904 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
905 ACPI_UINT32_MAX
, ioapic_add
, NULL
, NULL
);
909 static int acpiphp_unconfigure_ioapics(acpi_handle handle
)
911 ioapic_remove(handle
, 0, NULL
, NULL
);
912 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
913 ACPI_UINT32_MAX
, ioapic_remove
, NULL
, NULL
);
917 static int power_on_slot(struct acpiphp_slot
*slot
)
920 struct acpiphp_func
*func
;
924 /* if already enabled, just skip */
925 if (slot
->flags
& SLOT_POWEREDON
)
928 list_for_each (l
, &slot
->funcs
) {
929 func
= list_entry(l
, struct acpiphp_func
, sibling
);
931 if (func
->flags
& FUNC_HAS_PS0
) {
932 dbg("%s: executing _PS0\n", __FUNCTION__
);
933 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
934 if (ACPI_FAILURE(status
)) {
935 warn("%s: _PS0 failed\n", __FUNCTION__
);
943 /* TBD: evaluate _STA to check if the slot is enabled */
945 slot
->flags
|= SLOT_POWEREDON
;
952 static int power_off_slot(struct acpiphp_slot
*slot
)
955 struct acpiphp_func
*func
;
960 /* if already disabled, just skip */
961 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
964 list_for_each (l
, &slot
->funcs
) {
965 func
= list_entry(l
, struct acpiphp_func
, sibling
);
967 if (func
->flags
& FUNC_HAS_PS3
) {
968 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
969 if (ACPI_FAILURE(status
)) {
970 warn("%s: _PS3 failed\n", __FUNCTION__
);
978 /* TBD: evaluate _STA to check if the slot is disabled */
980 slot
->flags
&= (~SLOT_POWEREDON
);
989 * acpiphp_max_busnr - return the highest reserved bus number under
991 * @bus: bus to start search with
994 static unsigned char acpiphp_max_busnr(struct pci_bus
*bus
)
996 struct list_head
*tmp
;
997 unsigned char max
, n
;
1000 * pci_bus_max_busnr will return the highest
1001 * reserved busnr for all these children.
1002 * that is equivalent to the bus->subordinate
1003 * value. We don't want to use the parent's
1004 * bus->subordinate value because it could have
1007 max
= bus
->secondary
;
1009 list_for_each(tmp
, &bus
->children
) {
1010 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
1019 * acpiphp_bus_add - add a new bus to acpi subsystem
1020 * @func: acpiphp_func of the bridge
1023 static int acpiphp_bus_add(struct acpiphp_func
*func
)
1025 acpi_handle phandle
;
1026 struct acpi_device
*device
, *pdevice
;
1029 acpi_get_parent(func
->handle
, &phandle
);
1030 if (acpi_bus_get_device(phandle
, &pdevice
)) {
1031 dbg("no parent device, assuming NULL\n");
1034 if (!acpi_bus_get_device(func
->handle
, &device
)) {
1035 dbg("bus exists... trim\n");
1036 /* this shouldn't be in here, so remove
1037 * the bus then re-add it...
1039 ret_val
= acpi_bus_trim(device
, 1);
1040 dbg("acpi_bus_trim return %x\n", ret_val
);
1043 ret_val
= acpi_bus_add(&device
, pdevice
, func
->handle
,
1044 ACPI_BUS_TYPE_DEVICE
);
1046 dbg("error adding bus, %x\n",
1048 goto acpiphp_bus_add_out
;
1051 * try to start anyway. We could have failed to add
1052 * simply because this bus had previously been added
1053 * on another add. Don't bother with the return value
1054 * we just keep going.
1056 ret_val
= acpi_bus_start(device
);
1058 acpiphp_bus_add_out
:
1064 * acpiphp_bus_trim - trim a bus from acpi subsystem
1065 * @handle: handle to acpi namespace
1068 static int acpiphp_bus_trim(acpi_handle handle
)
1070 struct acpi_device
*device
;
1073 retval
= acpi_bus_get_device(handle
, &device
);
1075 dbg("acpi_device not found\n");
1079 retval
= acpi_bus_trim(device
, 1);
1081 err("cannot remove from acpi list\n");
1087 * enable_device - enable, configure a slot
1088 * @slot: slot to be enabled
1090 * This function should be called per *physical slot*,
1091 * not per each slot object in ACPI namespace.
1094 static int enable_device(struct acpiphp_slot
*slot
)
1096 struct pci_dev
*dev
;
1097 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
1098 struct list_head
*l
;
1099 struct acpiphp_func
*func
;
1104 if (slot
->flags
& SLOT_ENABLED
)
1107 /* sanity check: dev should be NULL when hot-plugged in */
1108 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
, 0));
1110 /* This case shouldn't happen */
1111 err("pci_dev structure already exists.\n");
1117 num
= pci_scan_slot(bus
, PCI_DEVFN(slot
->device
, 0));
1119 err("No new device found\n");
1124 max
= acpiphp_max_busnr(bus
);
1125 for (pass
= 0; pass
< 2; pass
++) {
1126 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1127 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
1129 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
1130 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
1131 max
= pci_scan_bridge(bus
, dev
, max
, pass
);
1132 if (pass
&& dev
->subordinate
)
1133 pci_bus_size_bridges(dev
->subordinate
);
1138 list_for_each (l
, &slot
->funcs
) {
1139 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1140 acpiphp_bus_add(func
);
1143 pci_bus_assign_resources(bus
);
1144 acpiphp_sanitize_bus(bus
);
1145 acpiphp_set_hpp_values(slot
->bridge
->handle
, bus
);
1146 list_for_each_entry(func
, &slot
->funcs
, sibling
)
1147 acpiphp_configure_ioapics(func
->handle
);
1148 pci_enable_bridges(bus
);
1149 pci_bus_add_devices(bus
);
1151 /* associate pci_dev to our representation */
1152 list_for_each (l
, &slot
->funcs
) {
1153 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1154 func
->pci_dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
1159 if (func
->pci_dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
&&
1160 func
->pci_dev
->hdr_type
!= PCI_HEADER_TYPE_CARDBUS
)
1163 status
= find_p2p_bridge(func
->handle
, (u32
)1, bus
, NULL
);
1164 if (ACPI_FAILURE(status
))
1165 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1169 slot
->flags
|= SLOT_ENABLED
;
1175 static void disable_bridges(struct pci_bus
*bus
)
1177 struct pci_dev
*dev
;
1178 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1179 if (dev
->subordinate
) {
1180 disable_bridges(dev
->subordinate
);
1181 pci_disable_device(dev
);
1187 * disable_device - disable a slot
1189 static int disable_device(struct acpiphp_slot
*slot
)
1192 struct acpiphp_func
*func
;
1193 struct list_head
*l
;
1195 /* is this slot already disabled? */
1196 if (!(slot
->flags
& SLOT_ENABLED
))
1199 list_for_each (l
, &slot
->funcs
) {
1200 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1203 /* cleanup p2p bridges under this P2P bridge */
1204 cleanup_p2p_bridge(func
->bridge
->handle
,
1205 (u32
)1, NULL
, NULL
);
1206 func
->bridge
= NULL
;
1209 if (func
->pci_dev
) {
1210 pci_stop_bus_device(func
->pci_dev
);
1211 if (func
->pci_dev
->subordinate
) {
1212 disable_bridges(func
->pci_dev
->subordinate
);
1213 pci_disable_device(func
->pci_dev
);
1218 list_for_each (l
, &slot
->funcs
) {
1219 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1221 acpiphp_unconfigure_ioapics(func
->handle
);
1222 acpiphp_bus_trim(func
->handle
);
1223 /* try to remove anyway.
1224 * acpiphp_bus_add might have been failed */
1229 pci_remove_bus_device(func
->pci_dev
);
1230 pci_dev_put(func
->pci_dev
);
1231 func
->pci_dev
= NULL
;
1234 slot
->flags
&= (~SLOT_ENABLED
);
1242 * get_slot_status - get ACPI slot status
1244 * if a slot has _STA for each function and if any one of them
1245 * returned non-zero status, return it
1247 * if a slot doesn't have _STA and if any one of its functions'
1248 * configuration space is configured, return 0x0f as a _STA
1250 * otherwise return 0
1252 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
1255 unsigned long sta
= 0;
1257 struct list_head
*l
;
1258 struct acpiphp_func
*func
;
1260 list_for_each (l
, &slot
->funcs
) {
1261 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1263 if (func
->flags
& FUNC_HAS_STA
) {
1264 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
1265 if (ACPI_SUCCESS(status
) && sta
)
1268 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
1269 PCI_DEVFN(slot
->device
,
1271 PCI_VENDOR_ID
, &dvid
);
1272 if (dvid
!= 0xffffffff) {
1279 return (unsigned int)sta
;
1283 * acpiphp_eject_slot - physically eject the slot
1285 int acpiphp_eject_slot(struct acpiphp_slot
*slot
)
1288 struct acpiphp_func
*func
;
1289 struct list_head
*l
;
1290 struct acpi_object_list arg_list
;
1291 union acpi_object arg
;
1293 list_for_each (l
, &slot
->funcs
) {
1294 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1296 /* We don't want to call _EJ0 on non-existing functions. */
1297 if ((func
->flags
& FUNC_HAS_EJ0
)) {
1298 /* _EJ0 method take one argument */
1300 arg_list
.pointer
= &arg
;
1301 arg
.type
= ACPI_TYPE_INTEGER
;
1302 arg
.integer
.value
= 1;
1304 status
= acpi_evaluate_object(func
->handle
, "_EJ0", &arg_list
, NULL
);
1305 if (ACPI_FAILURE(status
)) {
1306 warn("%s: _EJ0 failed\n", __FUNCTION__
);
1316 * acpiphp_check_bridge - re-enumerate devices
1318 * Iterate over all slots under this bridge and make sure that if a
1319 * card is present they are enabled, and if not they are disabled.
1321 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
1323 struct acpiphp_slot
*slot
;
1325 int enabled
, disabled
;
1327 enabled
= disabled
= 0;
1329 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1330 unsigned int status
= get_slot_status(slot
);
1331 if (slot
->flags
& SLOT_ENABLED
) {
1332 if (status
== ACPI_STA_ALL
)
1334 retval
= acpiphp_disable_slot(slot
);
1336 err("Error occurred in disabling\n");
1339 acpiphp_eject_slot(slot
);
1343 if (status
!= ACPI_STA_ALL
)
1345 retval
= acpiphp_enable_slot(slot
);
1347 err("Error occurred in enabling\n");
1354 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__
, enabled
, disabled
);
1360 static void program_hpp(struct pci_dev
*dev
, struct acpiphp_bridge
*bridge
)
1362 u16 pci_cmd
, pci_bctl
;
1363 struct pci_dev
*cdev
;
1365 /* Program hpp values for this device */
1366 if (!(dev
->hdr_type
== PCI_HEADER_TYPE_NORMAL
||
1367 (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
&&
1368 (dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
)))
1371 if ((dev
->class >> 8) == PCI_CLASS_BRIDGE_HOST
)
1374 pci_write_config_byte(dev
, PCI_CACHE_LINE_SIZE
,
1375 bridge
->hpp
.t0
->cache_line_size
);
1376 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
,
1377 bridge
->hpp
.t0
->latency_timer
);
1378 pci_read_config_word(dev
, PCI_COMMAND
, &pci_cmd
);
1379 if (bridge
->hpp
.t0
->enable_serr
)
1380 pci_cmd
|= PCI_COMMAND_SERR
;
1382 pci_cmd
&= ~PCI_COMMAND_SERR
;
1383 if (bridge
->hpp
.t0
->enable_perr
)
1384 pci_cmd
|= PCI_COMMAND_PARITY
;
1386 pci_cmd
&= ~PCI_COMMAND_PARITY
;
1387 pci_write_config_word(dev
, PCI_COMMAND
, pci_cmd
);
1389 /* Program bridge control value and child devices */
1390 if ((dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
) {
1391 pci_write_config_byte(dev
, PCI_SEC_LATENCY_TIMER
,
1392 bridge
->hpp
.t0
->latency_timer
);
1393 pci_read_config_word(dev
, PCI_BRIDGE_CONTROL
, &pci_bctl
);
1394 if (bridge
->hpp
.t0
->enable_serr
)
1395 pci_bctl
|= PCI_BRIDGE_CTL_SERR
;
1397 pci_bctl
&= ~PCI_BRIDGE_CTL_SERR
;
1398 if (bridge
->hpp
.t0
->enable_perr
)
1399 pci_bctl
|= PCI_BRIDGE_CTL_PARITY
;
1401 pci_bctl
&= ~PCI_BRIDGE_CTL_PARITY
;
1402 pci_write_config_word(dev
, PCI_BRIDGE_CONTROL
, pci_bctl
);
1403 if (dev
->subordinate
) {
1404 list_for_each_entry(cdev
, &dev
->subordinate
->devices
,
1406 program_hpp(cdev
, bridge
);
1411 static void acpiphp_set_hpp_values(acpi_handle handle
, struct pci_bus
*bus
)
1413 struct acpiphp_bridge bridge
;
1414 struct pci_dev
*dev
;
1416 memset(&bridge
, 0, sizeof(bridge
));
1417 bridge
.handle
= handle
;
1418 bridge
.pci_bus
= bus
;
1419 bridge
.pci_dev
= bus
->self
;
1420 decode_hpp(&bridge
);
1421 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
1422 program_hpp(dev
, &bridge
);
1427 * Remove devices for which we could not assign resources, call
1428 * arch specific code to fix-up the bus
1430 static void acpiphp_sanitize_bus(struct pci_bus
*bus
)
1432 struct pci_dev
*dev
;
1434 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
;
1436 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1437 for (i
=0; i
<PCI_BRIDGE_RESOURCES
; i
++) {
1438 struct resource
*res
= &dev
->resource
[i
];
1439 if ((res
->flags
& type_mask
) && !res
->start
&&
1441 /* Could not assign a required resources
1442 * for this device, remove it */
1443 pci_remove_bus_device(dev
);
1450 /* Program resources in newly inserted bridge */
1451 static int acpiphp_configure_bridge (acpi_handle handle
)
1453 struct acpi_pci_id pci_id
;
1454 struct pci_bus
*bus
;
1456 if (ACPI_FAILURE(acpi_get_pci_id(handle
, &pci_id
))) {
1457 err("cannot get PCI domain and bus number for bridge\n");
1460 bus
= pci_find_bus(pci_id
.segment
, pci_id
.bus
);
1462 err("cannot find bus %d:%d\n",
1463 pci_id
.segment
, pci_id
.bus
);
1467 pci_bus_size_bridges(bus
);
1468 pci_bus_assign_resources(bus
);
1469 acpiphp_sanitize_bus(bus
);
1470 acpiphp_set_hpp_values(handle
, bus
);
1471 pci_enable_bridges(bus
);
1472 acpiphp_configure_ioapics(handle
);
1476 static void handle_bridge_insertion(acpi_handle handle
, u32 type
)
1478 struct acpi_device
*device
, *pdevice
;
1479 acpi_handle phandle
;
1481 if ((type
!= ACPI_NOTIFY_BUS_CHECK
) &&
1482 (type
!= ACPI_NOTIFY_DEVICE_CHECK
)) {
1483 err("unexpected notification type %d\n", type
);
1487 acpi_get_parent(handle
, &phandle
);
1488 if (acpi_bus_get_device(phandle
, &pdevice
)) {
1489 dbg("no parent device, assuming NULL\n");
1492 if (acpi_bus_add(&device
, pdevice
, handle
, ACPI_BUS_TYPE_DEVICE
)) {
1493 err("cannot add bridge to acpi list\n");
1496 if (!acpiphp_configure_bridge(handle
) &&
1497 !acpi_bus_start(device
))
1500 err("cannot configure and start bridge\n");
1505 * ACPI event handlers
1509 count_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1511 int *count
= (int *)context
;
1512 struct acpiphp_bridge
*bridge
;
1514 bridge
= acpiphp_handle_to_bridge(handle
);
1521 check_sub_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1523 struct acpiphp_bridge
*bridge
;
1525 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1526 .pointer
= objname
};
1528 bridge
= acpiphp_handle_to_bridge(handle
);
1530 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1531 dbg("%s: re-enumerating slots under %s\n",
1532 __FUNCTION__
, objname
);
1533 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1534 acpiphp_check_bridge(bridge
);
1540 * handle_hotplug_event_bridge - handle ACPI event on bridges
1542 * @handle: Notify()'ed acpi_handle
1543 * @type: Notify code
1544 * @context: pointer to acpiphp_bridge structure
1546 * handles ACPI event notification on {host,p2p} bridges
1549 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
, void *context
)
1551 struct acpiphp_bridge
*bridge
;
1553 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1554 .pointer
= objname
};
1555 struct acpi_device
*device
;
1556 int num_sub_bridges
= 0;
1558 if (acpi_bus_get_device(handle
, &device
)) {
1559 /* This bridge must have just been physically inserted */
1560 handle_bridge_insertion(handle
, type
);
1564 bridge
= acpiphp_handle_to_bridge(handle
);
1565 if (type
== ACPI_NOTIFY_BUS_CHECK
) {
1566 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, ACPI_UINT32_MAX
,
1567 count_sub_bridges
, &num_sub_bridges
, NULL
);
1570 if (!bridge
&& !num_sub_bridges
) {
1571 err("cannot get bridge info\n");
1575 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1578 case ACPI_NOTIFY_BUS_CHECK
:
1579 /* bus re-enumerate */
1580 dbg("%s: Bus check notify on %s\n", __FUNCTION__
, objname
);
1582 dbg("%s: re-enumerating slots under %s\n",
1583 __FUNCTION__
, objname
);
1584 acpiphp_check_bridge(bridge
);
1586 if (num_sub_bridges
)
1587 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
1588 ACPI_UINT32_MAX
, check_sub_bridges
, NULL
, NULL
);
1591 case ACPI_NOTIFY_DEVICE_CHECK
:
1593 dbg("%s: Device check notify on %s\n", __FUNCTION__
, objname
);
1594 acpiphp_check_bridge(bridge
);
1597 case ACPI_NOTIFY_DEVICE_WAKE
:
1599 dbg("%s: Device wake notify on %s\n", __FUNCTION__
, objname
);
1602 case ACPI_NOTIFY_EJECT_REQUEST
:
1603 /* request device eject */
1604 dbg("%s: Device eject notify on %s\n", __FUNCTION__
, objname
);
1605 if ((bridge
->type
!= BRIDGE_TYPE_HOST
) &&
1606 (bridge
->flags
& BRIDGE_HAS_EJ0
)) {
1607 struct acpiphp_slot
*slot
;
1608 slot
= bridge
->func
->slot
;
1609 if (!acpiphp_disable_slot(slot
))
1610 acpiphp_eject_slot(slot
);
1614 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
1615 printk(KERN_ERR
"Device %s cannot be configured due"
1616 " to a frequency mismatch\n", objname
);
1619 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
1620 printk(KERN_ERR
"Device %s cannot be configured due"
1621 " to a bus mode mismatch\n", objname
);
1624 case ACPI_NOTIFY_POWER_FAULT
:
1625 printk(KERN_ERR
"Device %s has suffered a power fault\n",
1630 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1636 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1638 * @handle: Notify()'ed acpi_handle
1639 * @type: Notify code
1640 * @context: pointer to acpiphp_func structure
1642 * handles ACPI event notification on slots
1645 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
, void *context
)
1647 struct acpiphp_func
*func
;
1649 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1650 .pointer
= objname
};
1652 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1654 func
= (struct acpiphp_func
*)context
;
1657 case ACPI_NOTIFY_BUS_CHECK
:
1658 /* bus re-enumerate */
1659 dbg("%s: Bus check notify on %s\n", __FUNCTION__
, objname
);
1660 acpiphp_enable_slot(func
->slot
);
1663 case ACPI_NOTIFY_DEVICE_CHECK
:
1664 /* device check : re-enumerate from parent bus */
1665 dbg("%s: Device check notify on %s\n", __FUNCTION__
, objname
);
1666 acpiphp_check_bridge(func
->slot
->bridge
);
1669 case ACPI_NOTIFY_DEVICE_WAKE
:
1671 dbg("%s: Device wake notify on %s\n", __FUNCTION__
, objname
);
1674 case ACPI_NOTIFY_EJECT_REQUEST
:
1675 /* request device eject */
1676 dbg("%s: Device eject notify on %s\n", __FUNCTION__
, objname
);
1677 if (!(acpiphp_disable_slot(func
->slot
)))
1678 acpiphp_eject_slot(func
->slot
);
1682 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1689 find_root_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1691 int *count
= (int *)context
;
1693 if (acpi_root_bridge(handle
)) {
1694 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1695 handle_hotplug_event_bridge
, NULL
);
1701 static struct acpi_pci_driver acpi_pci_hp_driver
= {
1703 .remove
= remove_bridge
,
1707 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1710 int __init
acpiphp_glue_init(void)
1714 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
1715 ACPI_UINT32_MAX
, find_root_bridges
, &num
, NULL
);
1720 acpi_pci_register_driver(&acpi_pci_hp_driver
);
1727 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1729 * This function frees all data allocated in acpiphp_glue_init()
1731 void acpiphp_glue_exit(void)
1733 acpi_pci_unregister_driver(&acpi_pci_hp_driver
);
1738 * acpiphp_get_num_slots - count number of slots in a system
1740 int __init
acpiphp_get_num_slots(void)
1742 struct acpiphp_bridge
*bridge
;
1745 list_for_each_entry (bridge
, &bridge_list
, list
) {
1746 dbg("Bus %04x:%02x has %d slot%s\n",
1747 pci_domain_nr(bridge
->pci_bus
),
1748 bridge
->pci_bus
->number
, bridge
->nr_slots
,
1749 bridge
->nr_slots
== 1 ? "" : "s");
1750 num_slots
+= bridge
->nr_slots
;
1753 dbg("Total %d slots\n", num_slots
);
1760 * acpiphp_for_each_slot - call function for each slot
1761 * @fn: callback function
1762 * @data: context to be passed to callback function
1765 static int acpiphp_for_each_slot(acpiphp_callback fn
, void *data
)
1767 struct list_head
*node
;
1768 struct acpiphp_bridge
*bridge
;
1769 struct acpiphp_slot
*slot
;
1772 list_for_each (node
, &bridge_list
) {
1773 bridge
= (struct acpiphp_bridge
*)node
;
1774 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1775 retval
= fn(slot
, data
);
1788 * acpiphp_enable_slot - power on slot
1790 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1794 mutex_lock(&slot
->crit_sect
);
1796 /* wake up all functions */
1797 retval
= power_on_slot(slot
);
1801 if (get_slot_status(slot
) == ACPI_STA_ALL
) {
1802 /* configure all functions */
1803 retval
= enable_device(slot
);
1805 power_off_slot(slot
);
1807 dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__
);
1808 power_off_slot(slot
);
1812 mutex_unlock(&slot
->crit_sect
);
1817 * acpiphp_disable_slot - power off slot
1819 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1823 mutex_lock(&slot
->crit_sect
);
1825 /* unconfigure all functions */
1826 retval
= disable_device(slot
);
1830 /* power off all functions */
1831 retval
= power_off_slot(slot
);
1836 mutex_unlock(&slot
->crit_sect
);
1845 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1847 return (slot
->flags
& SLOT_POWEREDON
);
1855 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1859 sta
= get_slot_status(slot
);
1861 return (sta
& ACPI_STA_SHOW_IN_UI
) ? 0 : 1;
1866 * adapter presence : 1
1869 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1873 sta
= get_slot_status(slot
);
1875 return (sta
== 0) ? 0 : 1;
1880 * pci address (seg/bus/dev)
1882 u32
acpiphp_get_address(struct acpiphp_slot
*slot
)
1885 struct pci_bus
*pci_bus
= slot
->bridge
->pci_bus
;
1887 address
= (pci_domain_nr(pci_bus
) << 16) |
1888 (pci_bus
->number
<< 8) |