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
= kmalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
149 memset(newfunc
, 0, sizeof(struct acpiphp_func
));
151 INIT_LIST_HEAD(&newfunc
->sibling
);
152 newfunc
->handle
= handle
;
153 newfunc
->function
= function
;
154 if (ACPI_SUCCESS(status
))
155 newfunc
->flags
= FUNC_HAS_EJ0
;
157 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_STA", &tmp
)))
158 newfunc
->flags
|= FUNC_HAS_STA
;
160 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS0", &tmp
)))
161 newfunc
->flags
|= FUNC_HAS_PS0
;
163 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS3", &tmp
)))
164 newfunc
->flags
|= FUNC_HAS_PS3
;
166 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_DCK", &tmp
))) {
167 newfunc
->flags
|= FUNC_HAS_DCK
;
168 /* add to devices dependent on dock station,
169 * because this may actually be the dock bridge
171 dd
= alloc_dependent_device(handle
);
173 err("Can't allocate memory for "
174 "new dependent device!\n");
176 add_dependent_device(dd
);
179 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
180 if (ACPI_FAILURE(status
))
183 /* search for objects that share the same slot */
184 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
)
185 if (slot
->device
== device
) {
186 if (slot
->sun
!= sun
)
187 warn("sibling found, but _SUN doesn't match!\n");
192 slot
= kmalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
198 memset(slot
, 0, sizeof(struct acpiphp_slot
));
199 slot
->bridge
= bridge
;
200 slot
->device
= device
;
202 INIT_LIST_HEAD(&slot
->funcs
);
203 mutex_init(&slot
->crit_sect
);
205 slot
->next
= bridge
->slots
;
206 bridge
->slots
= slot
;
210 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
211 slot
->sun
, pci_domain_nr(bridge
->pci_bus
),
212 bridge
->pci_bus
->number
, slot
->device
);
213 retval
= acpiphp_register_hotplug_slot(slot
);
215 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval
);
220 newfunc
->slot
= slot
;
221 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
223 /* associate corresponding pci_dev */
224 newfunc
->pci_dev
= pci_get_slot(bridge
->pci_bus
,
225 PCI_DEVFN(device
, function
));
226 if (newfunc
->pci_dev
) {
227 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
230 /* if this is a device dependent on a dock station,
231 * associate the acpiphp_func to the dependent_device
234 if ((dd
= get_dependent_device(handle
))) {
235 newfunc
->flags
|= FUNC_IS_DD
;
237 * we don't want any devices which is dependent
238 * on the dock to have it's _EJ0 method executed.
239 * because we need to run _DCK first.
241 newfunc
->flags
&= ~FUNC_HAS_EJ0
;
243 add_pci_dependent_device(dd
);
246 /* install notify handler */
247 if (!(newfunc
->flags
& FUNC_HAS_DCK
)) {
248 status
= acpi_install_notify_handler(handle
,
250 handle_hotplug_event_func
,
253 if (ACPI_FAILURE(status
))
254 err("failed to register interrupt notify handler\n");
262 bridge
->slots
= slot
->next
;
270 /* see if it's worth looking at this bridge */
271 static int detect_ejectable_slots(acpi_handle
*bridge_handle
)
278 /* only check slots defined directly below bridge object */
279 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge_handle
, (u32
)1,
280 is_ejectable_slot
, (void *)&count
, NULL
);
286 /* decode ACPI 2.0 _HPP hot plug parameters */
287 static void decode_hpp(struct acpiphp_bridge
*bridge
)
290 struct acpi_buffer buffer
= { .length
= ACPI_ALLOCATE_BUFFER
,
292 union acpi_object
*package
;
295 /* default numbers */
296 bridge
->hpp
.cache_line_size
= 0x10;
297 bridge
->hpp
.latency_timer
= 0x40;
298 bridge
->hpp
.enable_SERR
= 0;
299 bridge
->hpp
.enable_PERR
= 0;
301 status
= acpi_evaluate_object(bridge
->handle
, "_HPP", NULL
, &buffer
);
303 if (ACPI_FAILURE(status
)) {
304 dbg("_HPP evaluation failed\n");
308 package
= (union acpi_object
*) buffer
.pointer
;
310 if (!package
|| package
->type
!= ACPI_TYPE_PACKAGE
||
311 package
->package
.count
!= 4 || !package
->package
.elements
) {
312 err("invalid _HPP object; ignoring\n");
316 for (i
= 0; i
< 4; i
++) {
317 if (package
->package
.elements
[i
].type
!= ACPI_TYPE_INTEGER
) {
318 err("invalid _HPP parameter type; ignoring\n");
323 bridge
->hpp
.cache_line_size
= package
->package
.elements
[0].integer
.value
;
324 bridge
->hpp
.latency_timer
= package
->package
.elements
[1].integer
.value
;
325 bridge
->hpp
.enable_SERR
= package
->package
.elements
[2].integer
.value
;
326 bridge
->hpp
.enable_PERR
= package
->package
.elements
[3].integer
.value
;
328 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
329 bridge
->hpp
.cache_line_size
,
330 bridge
->hpp
.latency_timer
,
331 bridge
->hpp
.enable_SERR
,
332 bridge
->hpp
.enable_PERR
);
334 bridge
->flags
|= BRIDGE_HAS_HPP
;
337 kfree(buffer
.pointer
);
341 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
342 static void init_bridge_misc(struct acpiphp_bridge
*bridge
)
346 /* decode ACPI 2.0 _HPP (hot plug parameters) */
349 /* must be added to the list prior to calling register_slot */
350 list_add(&bridge
->list
, &bridge_list
);
352 /* register all slot objects under this bridge */
353 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge
->handle
, (u32
)1,
354 register_slot
, bridge
, NULL
);
355 if (ACPI_FAILURE(status
)) {
356 list_del(&bridge
->list
);
360 /* install notify handler */
361 if (bridge
->type
!= BRIDGE_TYPE_HOST
) {
362 status
= acpi_install_notify_handler(bridge
->handle
,
364 handle_hotplug_event_bridge
,
367 if (ACPI_FAILURE(status
)) {
368 err("failed to register interrupt notify handler\n");
374 /* allocate and initialize host bridge data structure */
375 static void add_host_bridge(acpi_handle
*handle
, struct pci_bus
*pci_bus
)
377 struct acpiphp_bridge
*bridge
;
379 bridge
= kmalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
383 memset(bridge
, 0, sizeof(struct acpiphp_bridge
));
385 bridge
->type
= BRIDGE_TYPE_HOST
;
386 bridge
->handle
= handle
;
388 bridge
->pci_bus
= pci_bus
;
390 spin_lock_init(&bridge
->res_lock
);
392 init_bridge_misc(bridge
);
396 /* allocate and initialize PCI-to-PCI bridge data structure */
397 static void add_p2p_bridge(acpi_handle
*handle
, struct pci_dev
*pci_dev
)
399 struct acpiphp_bridge
*bridge
;
401 bridge
= kmalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
402 if (bridge
== NULL
) {
403 err("out of memory\n");
407 memset(bridge
, 0, sizeof(struct acpiphp_bridge
));
409 bridge
->type
= BRIDGE_TYPE_P2P
;
410 bridge
->handle
= handle
;
412 bridge
->pci_dev
= pci_dev_get(pci_dev
);
413 bridge
->pci_bus
= pci_dev
->subordinate
;
414 if (!bridge
->pci_bus
) {
415 err("This is not a PCI-to-PCI bridge!\n");
419 spin_lock_init(&bridge
->res_lock
);
421 init_bridge_misc(bridge
);
424 pci_dev_put(pci_dev
);
430 /* callback routine to find P2P bridges */
432 find_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
435 acpi_handle dummy_handle
;
437 int device
, function
;
439 struct pci_bus
*pci_bus
= context
;
441 status
= acpi_get_handle(handle
, "_ADR", &dummy_handle
);
442 if (ACPI_FAILURE(status
))
443 return AE_OK
; /* continue */
445 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &tmp
);
446 if (ACPI_FAILURE(status
)) {
447 dbg("%s: _ADR evaluation failure\n", __FUNCTION__
);
451 device
= (tmp
>> 16) & 0xffff;
452 function
= tmp
& 0xffff;
454 dev
= pci_get_slot(pci_bus
, PCI_DEVFN(device
, function
));
456 if (!dev
|| !dev
->subordinate
)
459 /* check if this bridge has ejectable slots */
460 if ((detect_ejectable_slots(handle
) > 0) ||
461 (detect_dependent_devices(handle
) > 0)) {
462 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev
));
463 add_p2p_bridge(handle
, dev
);
472 /* find hot-pluggable slots, and then find P2P bridge */
473 static int add_bridge(acpi_handle handle
)
478 acpi_handle dummy_handle
;
479 struct pci_bus
*pci_bus
;
481 /* if the bridge doesn't have _STA, we assume it is always there */
482 status
= acpi_get_handle(handle
, "_STA", &dummy_handle
);
483 if (ACPI_SUCCESS(status
)) {
484 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &tmp
);
485 if (ACPI_FAILURE(status
)) {
486 dbg("%s: _STA evaluation failure\n", __FUNCTION__
);
489 if ((tmp
& ACPI_STA_FUNCTIONING
) == 0)
490 /* don't register this object */
494 /* get PCI segment number */
495 status
= acpi_evaluate_integer(handle
, "_SEG", NULL
, &tmp
);
497 seg
= ACPI_SUCCESS(status
) ? tmp
: 0;
499 /* get PCI bus number */
500 status
= acpi_evaluate_integer(handle
, "_BBN", NULL
, &tmp
);
502 if (ACPI_SUCCESS(status
)) {
505 warn("can't get bus number, assuming 0\n");
509 pci_bus
= pci_find_bus(seg
, bus
);
511 err("Can't find bus %04x:%02x\n", seg
, bus
);
515 /* check if this bridge has ejectable slots */
516 if (detect_ejectable_slots(handle
) > 0) {
517 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
518 add_host_bridge(handle
, pci_bus
);
522 /* search P2P bridges under this host bridge */
523 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
524 find_p2p_bridge
, pci_bus
, NULL
);
526 if (ACPI_FAILURE(status
))
527 warn("find_p2p_bridge faied (error code = 0x%x)\n",status
);
532 static struct acpiphp_bridge
*acpiphp_handle_to_bridge(acpi_handle handle
)
534 struct list_head
*head
;
535 list_for_each(head
, &bridge_list
) {
536 struct acpiphp_bridge
*bridge
= list_entry(head
,
537 struct acpiphp_bridge
, list
);
538 if (bridge
->handle
== handle
)
545 static void cleanup_bridge(struct acpiphp_bridge
*bridge
)
547 struct list_head
*list
, *tmp
;
548 struct acpiphp_slot
*slot
;
550 acpi_handle handle
= bridge
->handle
;
552 status
= acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
553 handle_hotplug_event_bridge
);
554 if (ACPI_FAILURE(status
))
555 err("failed to remove notify handler\n");
557 slot
= bridge
->slots
;
559 struct acpiphp_slot
*next
= slot
->next
;
560 list_for_each_safe (list
, tmp
, &slot
->funcs
) {
561 struct acpiphp_func
*func
;
562 func
= list_entry(list
, struct acpiphp_func
, sibling
);
563 if (!(func
->flags
& FUNC_HAS_DCK
)) {
564 status
= acpi_remove_notify_handler(func
->handle
,
566 handle_hotplug_event_func
);
567 if (ACPI_FAILURE(status
))
568 err("failed to remove notify handler\n");
570 pci_dev_put(func
->pci_dev
);
574 acpiphp_unregister_hotplug_slot(slot
);
575 list_del(&slot
->funcs
);
580 pci_dev_put(bridge
->pci_dev
);
581 list_del(&bridge
->list
);
586 cleanup_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
588 struct acpiphp_bridge
*bridge
;
590 if (!(bridge
= acpiphp_handle_to_bridge(handle
)))
592 cleanup_bridge(bridge
);
596 static void remove_bridge(acpi_handle handle
)
598 struct acpiphp_bridge
*bridge
;
600 bridge
= acpiphp_handle_to_bridge(handle
);
602 cleanup_bridge(bridge
);
604 /* clean-up p2p bridges under this host bridge */
605 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
606 (u32
)1, cleanup_p2p_bridge
, NULL
, NULL
);
610 static struct pci_dev
* get_apic_pci_info(acpi_handle handle
)
612 struct acpi_pci_id id
;
616 if (ACPI_FAILURE(acpi_get_pci_id(handle
, &id
)))
619 bus
= pci_find_bus(id
.segment
, id
.bus
);
623 dev
= pci_get_slot(bus
, PCI_DEVFN(id
.device
, id
.function
));
627 if ((dev
->class != PCI_CLASS_SYSTEM_PIC_IOAPIC
) &&
628 (dev
->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC
))
637 static int get_gsi_base(acpi_handle handle
, u32
*gsi_base
)
642 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
643 union acpi_object
*obj
;
646 status
= acpi_evaluate_integer(handle
, "_GSB", NULL
, &gsb
);
647 if (ACPI_SUCCESS(status
)) {
648 *gsi_base
= (u32
)gsb
;
652 status
= acpi_evaluate_object(handle
, "_MAT", NULL
, &buffer
);
653 if (ACPI_FAILURE(status
) || !buffer
.length
|| !buffer
.pointer
)
656 obj
= buffer
.pointer
;
657 if (obj
->type
!= ACPI_TYPE_BUFFER
)
660 table
= obj
->buffer
.pointer
;
661 switch (((acpi_table_entry_header
*)table
)->type
) {
662 case ACPI_MADT_IOSAPIC
:
663 *gsi_base
= ((struct acpi_table_iosapic
*)table
)->global_irq_base
;
666 case ACPI_MADT_IOAPIC
:
667 *gsi_base
= ((struct acpi_table_ioapic
*)table
)->global_irq_base
;
674 acpi_os_free(buffer
.pointer
);
679 ioapic_add(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
684 struct pci_dev
*pdev
;
688 /* Evaluate _STA if present */
689 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
690 if (ACPI_SUCCESS(status
) && sta
!= ACPI_STA_ALL
)
691 return AE_CTRL_DEPTH
;
693 /* Scan only PCI bus scope */
694 status
= acpi_get_handle(handle
, "_HID", &tmp
);
695 if (ACPI_SUCCESS(status
))
696 return AE_CTRL_DEPTH
;
698 if (get_gsi_base(handle
, &gsi_base
))
701 pdev
= get_apic_pci_info(handle
);
705 if (pci_enable_device(pdev
)) {
710 pci_set_master(pdev
);
712 if (pci_request_region(pdev
, 0, "I/O APIC(acpiphp)")) {
713 pci_disable_device(pdev
);
718 phys_addr
= pci_resource_start(pdev
, 0);
719 if (acpi_register_ioapic(handle
, phys_addr
, gsi_base
)) {
720 pci_release_region(pdev
, 0);
721 pci_disable_device(pdev
);
729 static int acpiphp_configure_ioapics(acpi_handle handle
)
731 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
,
732 ACPI_UINT32_MAX
, ioapic_add
, NULL
, NULL
);
736 static int power_on_slot(struct acpiphp_slot
*slot
)
739 struct acpiphp_func
*func
;
743 /* if already enabled, just skip */
744 if (slot
->flags
& SLOT_POWEREDON
)
747 list_for_each (l
, &slot
->funcs
) {
748 func
= list_entry(l
, struct acpiphp_func
, sibling
);
750 if (func
->flags
& FUNC_HAS_PS0
) {
751 dbg("%s: executing _PS0\n", __FUNCTION__
);
752 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
753 if (ACPI_FAILURE(status
)) {
754 warn("%s: _PS0 failed\n", __FUNCTION__
);
762 /* TBD: evaluate _STA to check if the slot is enabled */
764 slot
->flags
|= SLOT_POWEREDON
;
771 static int power_off_slot(struct acpiphp_slot
*slot
)
774 struct acpiphp_func
*func
;
779 /* if already disabled, just skip */
780 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
783 list_for_each (l
, &slot
->funcs
) {
784 func
= list_entry(l
, struct acpiphp_func
, sibling
);
786 if (func
->flags
& FUNC_HAS_PS3
) {
787 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
788 if (ACPI_FAILURE(status
)) {
789 warn("%s: _PS3 failed\n", __FUNCTION__
);
797 /* TBD: evaluate _STA to check if the slot is disabled */
799 slot
->flags
&= (~SLOT_POWEREDON
);
808 * acpiphp_max_busnr - return the highest reserved bus number under
810 * @bus: bus to start search with
813 static unsigned char acpiphp_max_busnr(struct pci_bus
*bus
)
815 struct list_head
*tmp
;
816 unsigned char max
, n
;
819 * pci_bus_max_busnr will return the highest
820 * reserved busnr for all these children.
821 * that is equivalent to the bus->subordinate
822 * value. We don't want to use the parent's
823 * bus->subordinate value because it could have
826 max
= bus
->secondary
;
828 list_for_each(tmp
, &bus
->children
) {
829 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
839 * get_func - get a pointer to acpiphp_func given a slot, device
840 * @slot: slot to search
841 * @dev: pci_dev struct to match.
843 * This function will increase the reference count of pci_dev,
844 * so callers should call pci_dev_put when complete.
847 static struct acpiphp_func
*
848 get_func(struct acpiphp_slot
*slot
, struct pci_dev
*dev
)
850 struct acpiphp_func
*func
= NULL
;
851 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
852 struct pci_dev
*pdev
;
854 list_for_each_entry(func
, &slot
->funcs
, sibling
) {
855 pdev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
868 * acpiphp_bus_add - add a new bus to acpi subsystem
869 * @func: acpiphp_func of the bridge
872 static int acpiphp_bus_add(struct acpiphp_func
*func
)
875 struct acpi_device
*device
, *pdevice
;
878 acpi_get_parent(func
->handle
, &phandle
);
879 if (acpi_bus_get_device(phandle
, &pdevice
)) {
880 dbg("no parent device, assuming NULL\n");
883 if (!acpi_bus_get_device(func
->handle
, &device
)) {
884 dbg("bus exists... trim\n");
885 /* this shouldn't be in here, so remove
886 * the bus then re-add it...
888 ret_val
= acpi_bus_trim(device
, 1);
889 dbg("acpi_bus_trim return %x\n", ret_val
);
892 ret_val
= acpi_bus_add(&device
, pdevice
, func
->handle
,
893 ACPI_BUS_TYPE_DEVICE
);
895 dbg("error adding bus, %x\n",
897 goto acpiphp_bus_add_out
;
900 * try to start anyway. We could have failed to add
901 * simply because this bus had previously been added
902 * on another add. Don't bother with the return value
903 * we just keep going.
905 ret_val
= acpi_bus_start(device
);
914 * enable_device - enable, configure a slot
915 * @slot: slot to be enabled
917 * This function should be called per *physical slot*,
918 * not per each slot object in ACPI namespace.
921 static int enable_device(struct acpiphp_slot
*slot
)
924 struct pci_bus
*bus
= slot
->bridge
->pci_bus
;
926 struct acpiphp_func
*func
;
930 if (slot
->flags
& SLOT_ENABLED
)
933 /* sanity check: dev should be NULL when hot-plugged in */
934 dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
, 0));
936 /* This case shouldn't happen */
937 err("pci_dev structure already exists.\n");
943 num
= pci_scan_slot(bus
, PCI_DEVFN(slot
->device
, 0));
945 err("No new device found\n");
950 max
= acpiphp_max_busnr(bus
);
951 for (pass
= 0; pass
< 2; pass
++) {
952 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
953 if (PCI_SLOT(dev
->devfn
) != slot
->device
)
955 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
956 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
957 max
= pci_scan_bridge(bus
, dev
, max
, pass
);
958 if (pass
&& dev
->subordinate
) {
959 pci_bus_size_bridges(dev
->subordinate
);
960 func
= get_func(slot
, dev
);
962 acpiphp_bus_add(func
);
963 /* side effect of get_func */
971 pci_bus_assign_resources(bus
);
972 acpiphp_sanitize_bus(bus
);
973 pci_enable_bridges(bus
);
974 pci_bus_add_devices(bus
);
975 acpiphp_set_hpp_values(DEVICE_ACPI_HANDLE(&bus
->self
->dev
), bus
);
976 acpiphp_configure_ioapics(DEVICE_ACPI_HANDLE(&bus
->self
->dev
));
978 /* associate pci_dev to our representation */
979 list_for_each (l
, &slot
->funcs
) {
980 func
= list_entry(l
, struct acpiphp_func
, sibling
);
981 func
->pci_dev
= pci_get_slot(bus
, PCI_DEVFN(slot
->device
,
985 slot
->flags
|= SLOT_ENABLED
;
993 * disable_device - disable a slot
995 static int disable_device(struct acpiphp_slot
*slot
)
998 struct acpiphp_func
*func
;
1001 /* is this slot already disabled? */
1002 if (!(slot
->flags
& SLOT_ENABLED
))
1005 list_for_each (l
, &slot
->funcs
) {
1006 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1010 pci_remove_bus_device(func
->pci_dev
);
1011 pci_dev_put(func
->pci_dev
);
1012 func
->pci_dev
= NULL
;
1015 slot
->flags
&= (~SLOT_ENABLED
);
1023 * get_slot_status - get ACPI slot status
1025 * if a slot has _STA for each function and if any one of them
1026 * returned non-zero status, return it
1028 * if a slot doesn't have _STA and if any one of its functions'
1029 * configuration space is configured, return 0x0f as a _STA
1031 * otherwise return 0
1033 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
1036 unsigned long sta
= 0;
1038 struct list_head
*l
;
1039 struct acpiphp_func
*func
;
1041 list_for_each (l
, &slot
->funcs
) {
1042 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1044 if (func
->flags
& FUNC_HAS_STA
) {
1045 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
1046 if (ACPI_SUCCESS(status
) && sta
)
1049 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
1050 PCI_DEVFN(slot
->device
,
1052 PCI_VENDOR_ID
, &dvid
);
1053 if (dvid
!= 0xffffffff) {
1060 return (unsigned int)sta
;
1064 * acpiphp_eject_slot - physically eject the slot
1066 static int acpiphp_eject_slot(struct acpiphp_slot
*slot
)
1069 struct acpiphp_func
*func
;
1070 struct list_head
*l
;
1071 struct acpi_object_list arg_list
;
1072 union acpi_object arg
;
1074 list_for_each (l
, &slot
->funcs
) {
1075 func
= list_entry(l
, struct acpiphp_func
, sibling
);
1077 /* We don't want to call _EJ0 on non-existing functions. */
1078 if ((func
->flags
& FUNC_HAS_EJ0
)) {
1079 /* _EJ0 method take one argument */
1081 arg_list
.pointer
= &arg
;
1082 arg
.type
= ACPI_TYPE_INTEGER
;
1083 arg
.integer
.value
= 1;
1085 status
= acpi_evaluate_object(func
->handle
, "_EJ0", &arg_list
, NULL
);
1086 if (ACPI_FAILURE(status
)) {
1087 warn("%s: _EJ0 failed\n", __FUNCTION__
);
1097 * acpiphp_check_bridge - re-enumerate devices
1099 * Iterate over all slots under this bridge and make sure that if a
1100 * card is present they are enabled, and if not they are disabled.
1102 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
1104 struct acpiphp_slot
*slot
;
1106 int enabled
, disabled
;
1108 enabled
= disabled
= 0;
1110 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1111 unsigned int status
= get_slot_status(slot
);
1112 if (slot
->flags
& SLOT_ENABLED
) {
1113 if (status
== ACPI_STA_ALL
)
1115 retval
= acpiphp_disable_slot(slot
);
1117 err("Error occurred in disabling\n");
1120 acpiphp_eject_slot(slot
);
1124 if (status
!= ACPI_STA_ALL
)
1126 retval
= acpiphp_enable_slot(slot
);
1128 err("Error occurred in enabling\n");
1135 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__
, enabled
, disabled
);
1141 static void program_hpp(struct pci_dev
*dev
, struct acpiphp_bridge
*bridge
)
1143 u16 pci_cmd
, pci_bctl
;
1144 struct pci_dev
*cdev
;
1146 /* Program hpp values for this device */
1147 if (!(dev
->hdr_type
== PCI_HEADER_TYPE_NORMAL
||
1148 (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
&&
1149 (dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
)))
1151 pci_write_config_byte(dev
, PCI_CACHE_LINE_SIZE
,
1152 bridge
->hpp
.cache_line_size
);
1153 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
,
1154 bridge
->hpp
.latency_timer
);
1155 pci_read_config_word(dev
, PCI_COMMAND
, &pci_cmd
);
1156 if (bridge
->hpp
.enable_SERR
)
1157 pci_cmd
|= PCI_COMMAND_SERR
;
1159 pci_cmd
&= ~PCI_COMMAND_SERR
;
1160 if (bridge
->hpp
.enable_PERR
)
1161 pci_cmd
|= PCI_COMMAND_PARITY
;
1163 pci_cmd
&= ~PCI_COMMAND_PARITY
;
1164 pci_write_config_word(dev
, PCI_COMMAND
, pci_cmd
);
1166 /* Program bridge control value and child devices */
1167 if ((dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
) {
1168 pci_write_config_byte(dev
, PCI_SEC_LATENCY_TIMER
,
1169 bridge
->hpp
.latency_timer
);
1170 pci_read_config_word(dev
, PCI_BRIDGE_CONTROL
, &pci_bctl
);
1171 if (bridge
->hpp
.enable_SERR
)
1172 pci_bctl
|= PCI_BRIDGE_CTL_SERR
;
1174 pci_bctl
&= ~PCI_BRIDGE_CTL_SERR
;
1175 if (bridge
->hpp
.enable_PERR
)
1176 pci_bctl
|= PCI_BRIDGE_CTL_PARITY
;
1178 pci_bctl
&= ~PCI_BRIDGE_CTL_PARITY
;
1179 pci_write_config_word(dev
, PCI_BRIDGE_CONTROL
, pci_bctl
);
1180 if (dev
->subordinate
) {
1181 list_for_each_entry(cdev
, &dev
->subordinate
->devices
,
1183 program_hpp(cdev
, bridge
);
1188 static void acpiphp_set_hpp_values(acpi_handle handle
, struct pci_bus
*bus
)
1190 struct acpiphp_bridge bridge
;
1191 struct pci_dev
*dev
;
1193 memset(&bridge
, 0, sizeof(bridge
));
1194 bridge
.handle
= handle
;
1195 decode_hpp(&bridge
);
1196 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
1197 program_hpp(dev
, &bridge
);
1202 * Remove devices for which we could not assign resources, call
1203 * arch specific code to fix-up the bus
1205 static void acpiphp_sanitize_bus(struct pci_bus
*bus
)
1207 struct pci_dev
*dev
;
1209 unsigned long type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
;
1211 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
1212 for (i
=0; i
<PCI_BRIDGE_RESOURCES
; i
++) {
1213 struct resource
*res
= &dev
->resource
[i
];
1214 if ((res
->flags
& type_mask
) && !res
->start
&&
1216 /* Could not assign a required resources
1217 * for this device, remove it */
1218 pci_remove_bus_device(dev
);
1225 /* Program resources in newly inserted bridge */
1226 static int acpiphp_configure_bridge (acpi_handle handle
)
1228 struct acpi_pci_id pci_id
;
1229 struct pci_bus
*bus
;
1231 if (ACPI_FAILURE(acpi_get_pci_id(handle
, &pci_id
))) {
1232 err("cannot get PCI domain and bus number for bridge\n");
1235 bus
= pci_find_bus(pci_id
.segment
, pci_id
.bus
);
1237 err("cannot find bus %d:%d\n",
1238 pci_id
.segment
, pci_id
.bus
);
1242 pci_bus_size_bridges(bus
);
1243 pci_bus_assign_resources(bus
);
1244 acpiphp_sanitize_bus(bus
);
1245 acpiphp_set_hpp_values(handle
, bus
);
1246 pci_enable_bridges(bus
);
1247 acpiphp_configure_ioapics(handle
);
1251 static void handle_bridge_insertion(acpi_handle handle
, u32 type
)
1253 struct acpi_device
*device
, *pdevice
;
1254 acpi_handle phandle
;
1256 if ((type
!= ACPI_NOTIFY_BUS_CHECK
) &&
1257 (type
!= ACPI_NOTIFY_DEVICE_CHECK
)) {
1258 err("unexpected notification type %d\n", type
);
1262 acpi_get_parent(handle
, &phandle
);
1263 if (acpi_bus_get_device(phandle
, &pdevice
)) {
1264 dbg("no parent device, assuming NULL\n");
1267 if (acpi_bus_add(&device
, pdevice
, handle
, ACPI_BUS_TYPE_DEVICE
)) {
1268 err("cannot add bridge to acpi list\n");
1271 if (!acpiphp_configure_bridge(handle
) &&
1272 !acpi_bus_start(device
))
1275 err("cannot configure and start bridge\n");
1280 * ACPI event handlers
1284 * handle_hotplug_event_bridge - handle ACPI event on bridges
1286 * @handle: Notify()'ed acpi_handle
1287 * @type: Notify code
1288 * @context: pointer to acpiphp_bridge structure
1290 * handles ACPI event notification on {host,p2p} bridges
1293 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
, void *context
)
1295 struct acpiphp_bridge
*bridge
;
1297 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1298 .pointer
= objname
};
1299 struct acpi_device
*device
;
1301 if (acpi_bus_get_device(handle
, &device
)) {
1302 /* This bridge must have just been physically inserted */
1303 handle_bridge_insertion(handle
, type
);
1307 bridge
= acpiphp_handle_to_bridge(handle
);
1309 err("cannot get bridge info\n");
1313 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1316 case ACPI_NOTIFY_BUS_CHECK
:
1317 /* bus re-enumerate */
1318 dbg("%s: Bus check notify on %s\n", __FUNCTION__
, objname
);
1319 acpiphp_check_bridge(bridge
);
1322 case ACPI_NOTIFY_DEVICE_CHECK
:
1324 dbg("%s: Device check notify on %s\n", __FUNCTION__
, objname
);
1325 acpiphp_check_bridge(bridge
);
1328 case ACPI_NOTIFY_DEVICE_WAKE
:
1330 dbg("%s: Device wake notify on %s\n", __FUNCTION__
, objname
);
1333 case ACPI_NOTIFY_EJECT_REQUEST
:
1334 /* request device eject */
1335 dbg("%s: Device eject notify on %s\n", __FUNCTION__
, objname
);
1338 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
1339 printk(KERN_ERR
"Device %s cannot be configured due"
1340 " to a frequency mismatch\n", objname
);
1343 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
1344 printk(KERN_ERR
"Device %s cannot be configured due"
1345 " to a bus mode mismatch\n", objname
);
1348 case ACPI_NOTIFY_POWER_FAULT
:
1349 printk(KERN_ERR
"Device %s has suffered a power fault\n",
1354 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1360 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1362 * @handle: Notify()'ed acpi_handle
1363 * @type: Notify code
1364 * @context: pointer to acpiphp_func structure
1366 * handles ACPI event notification on slots
1369 void handle_hotplug_event_func(acpi_handle handle
, u32 type
, void *context
)
1371 struct acpiphp_func
*func
;
1373 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1374 .pointer
= objname
};
1376 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1378 func
= (struct acpiphp_func
*)context
;
1381 case ACPI_NOTIFY_BUS_CHECK
:
1382 /* bus re-enumerate */
1383 dbg("%s: Bus check notify on %s\n", __FUNCTION__
, objname
);
1384 acpiphp_enable_slot(func
->slot
);
1387 case ACPI_NOTIFY_DEVICE_CHECK
:
1388 /* device check : re-enumerate from parent bus */
1389 dbg("%s: Device check notify on %s\n", __FUNCTION__
, objname
);
1390 acpiphp_check_bridge(func
->slot
->bridge
);
1393 case ACPI_NOTIFY_DEVICE_WAKE
:
1395 dbg("%s: Device wake notify on %s\n", __FUNCTION__
, objname
);
1398 case ACPI_NOTIFY_EJECT_REQUEST
:
1399 /* request device eject */
1400 dbg("%s: Device eject notify on %s\n", __FUNCTION__
, objname
);
1401 if (!(acpiphp_disable_slot(func
->slot
)))
1402 acpiphp_eject_slot(func
->slot
);
1406 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1411 static int is_root_bridge(acpi_handle handle
)
1414 struct acpi_device_info
*info
;
1415 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1418 status
= acpi_get_object_info(handle
, &buffer
);
1419 if (ACPI_SUCCESS(status
)) {
1420 info
= buffer
.pointer
;
1421 if ((info
->valid
& ACPI_VALID_HID
) &&
1422 !strcmp(PCI_ROOT_HID_STRING
,
1423 info
->hardware_id
.value
)) {
1424 acpi_os_free(buffer
.pointer
);
1427 if (info
->valid
& ACPI_VALID_CID
) {
1428 for (i
=0; i
< info
->compatibility_id
.count
; i
++) {
1429 if (!strcmp(PCI_ROOT_HID_STRING
,
1430 info
->compatibility_id
.id
[i
].value
)) {
1431 acpi_os_free(buffer
.pointer
);
1441 find_root_bridges(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1443 int *count
= (int *)context
;
1445 if (is_root_bridge(handle
)) {
1446 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1447 handle_hotplug_event_bridge
, NULL
);
1453 static struct acpi_pci_driver acpi_pci_hp_driver
= {
1455 .remove
= remove_bridge
,
1459 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1462 int __init
acpiphp_glue_init(void)
1466 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
1467 ACPI_UINT32_MAX
, find_root_bridges
, &num
, NULL
);
1472 acpi_pci_register_driver(&acpi_pci_hp_driver
);
1479 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1481 * This function frees all data allocated in acpiphp_glue_init()
1483 void __exit
acpiphp_glue_exit(void)
1485 acpi_pci_unregister_driver(&acpi_pci_hp_driver
);
1490 * acpiphp_get_num_slots - count number of slots in a system
1492 int __init
acpiphp_get_num_slots(void)
1494 struct list_head
*node
;
1495 struct acpiphp_bridge
*bridge
;
1500 list_for_each (node
, &bridge_list
) {
1501 bridge
= (struct acpiphp_bridge
*)node
;
1502 dbg("Bus %04x:%02x has %d slot%s\n",
1503 pci_domain_nr(bridge
->pci_bus
),
1504 bridge
->pci_bus
->number
, bridge
->nr_slots
,
1505 bridge
->nr_slots
== 1 ? "" : "s");
1506 num_slots
+= bridge
->nr_slots
;
1509 dbg("Total %d slots\n", num_slots
);
1516 * acpiphp_for_each_slot - call function for each slot
1517 * @fn: callback function
1518 * @data: context to be passed to callback function
1521 static int acpiphp_for_each_slot(acpiphp_callback fn
, void *data
)
1523 struct list_head
*node
;
1524 struct acpiphp_bridge
*bridge
;
1525 struct acpiphp_slot
*slot
;
1528 list_for_each (node
, &bridge_list
) {
1529 bridge
= (struct acpiphp_bridge
*)node
;
1530 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1531 retval
= fn(slot
, data
);
1544 * acpiphp_enable_slot - power on slot
1546 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1550 mutex_lock(&slot
->crit_sect
);
1552 /* wake up all functions */
1553 retval
= power_on_slot(slot
);
1557 if (get_slot_status(slot
) == ACPI_STA_ALL
)
1558 /* configure all functions */
1559 retval
= enable_device(slot
);
1562 mutex_unlock(&slot
->crit_sect
);
1567 * acpiphp_disable_slot - power off slot
1569 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1573 mutex_lock(&slot
->crit_sect
);
1575 /* unconfigure all functions */
1576 retval
= disable_device(slot
);
1580 /* power off all functions */
1581 retval
= power_off_slot(slot
);
1586 mutex_unlock(&slot
->crit_sect
);
1595 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1597 return (slot
->flags
& SLOT_POWEREDON
);
1605 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1609 sta
= get_slot_status(slot
);
1611 return (sta
& ACPI_STA_SHOW_IN_UI
) ? 1 : 0;
1616 * adapter presence : 1
1619 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1623 sta
= get_slot_status(slot
);
1625 return (sta
== 0) ? 0 : 1;
1630 * pci address (seg/bus/dev)
1632 u32
acpiphp_get_address(struct acpiphp_slot
*slot
)
1635 struct pci_bus
*pci_bus
= slot
->bridge
->pci_bus
;
1637 address
= (pci_domain_nr(pci_bus
) << 16) |
1638 (pci_bus
->number
<< 8) |