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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Send feedback to <t-kochi@bq.jp.nec.com>
29 #include <linux/init.h>
30 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/pci.h>
34 #include <linux/smp_lock.h>
35 #include <asm/semaphore.h>
38 #include "pci_hotplug.h"
41 static LIST_HEAD(bridge_list
);
43 #define MY_NAME "acpiphp_glue"
45 static void handle_hotplug_event_bridge (acpi_handle
, u32
, void *);
46 static void handle_hotplug_event_func (acpi_handle
, u32
, void *);
49 * initialization & terminatation routines
53 * is_ejectable - determine if a slot is ejectable
54 * @handle: handle to acpi namespace
56 * Ejectable slot should satisfy at least these conditions:
69 static int is_ejectable(acpi_handle handle
)
74 status
= acpi_get_handle(handle
, "_ADR", &tmp
);
75 if (ACPI_FAILURE(status
)) {
79 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
80 if (ACPI_FAILURE(status
)) {
88 /* callback routine to check the existence of ejectable slots */
90 is_ejectable_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
92 int *count
= (int *)context
;
94 if (is_ejectable(handle
)) {
96 /* only one ejectable slot is enough */
97 return AE_CTRL_TERMINATE
;
104 /* callback routine to register each ACPI PCI slot object */
106 register_slot(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
108 struct acpiphp_bridge
*bridge
= (struct acpiphp_bridge
*)context
;
109 struct acpiphp_slot
*slot
;
110 struct acpiphp_func
*newfunc
;
112 acpi_status status
= AE_OK
;
113 unsigned long adr
, sun
;
114 int device
, function
;
115 static int num_slots
= 0; /* XXX if we support I/O node hotplug... */
117 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &adr
);
119 if (ACPI_FAILURE(status
))
122 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
124 if (ACPI_FAILURE(status
))
127 device
= (adr
>> 16) & 0xffff;
128 function
= adr
& 0xffff;
130 newfunc
= kmalloc(sizeof(struct acpiphp_func
), GFP_KERNEL
);
133 memset(newfunc
, 0, sizeof(struct acpiphp_func
));
135 INIT_LIST_HEAD(&newfunc
->sibling
);
136 newfunc
->handle
= handle
;
137 newfunc
->function
= function
;
138 newfunc
->flags
= FUNC_HAS_EJ0
;
140 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_STA", &tmp
)))
141 newfunc
->flags
|= FUNC_HAS_STA
;
143 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS0", &tmp
)))
144 newfunc
->flags
|= FUNC_HAS_PS0
;
146 if (ACPI_SUCCESS(acpi_get_handle(handle
, "_PS3", &tmp
)))
147 newfunc
->flags
|= FUNC_HAS_PS3
;
149 status
= acpi_evaluate_integer(handle
, "_SUN", NULL
, &sun
);
150 if (ACPI_FAILURE(status
))
153 /* search for objects that share the same slot */
154 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
)
155 if (slot
->device
== device
) {
156 if (slot
->sun
!= sun
)
157 warn("sibling found, but _SUN doesn't match!\n");
162 slot
= kmalloc(sizeof(struct acpiphp_slot
), GFP_KERNEL
);
168 memset(slot
, 0, sizeof(struct acpiphp_slot
));
169 slot
->bridge
= bridge
;
170 slot
->id
= num_slots
++;
171 slot
->device
= device
;
173 INIT_LIST_HEAD(&slot
->funcs
);
174 init_MUTEX(&slot
->crit_sect
);
176 slot
->next
= bridge
->slots
;
177 bridge
->slots
= slot
;
181 dbg("found ACPI PCI Hotplug slot at PCI %02x:%02x Slot:%d\n",
182 slot
->bridge
->bus
, slot
->device
, slot
->sun
);
185 newfunc
->slot
= slot
;
186 list_add_tail(&newfunc
->sibling
, &slot
->funcs
);
188 /* associate corresponding pci_dev */
189 newfunc
->pci_dev
= pci_find_slot(bridge
->bus
,
190 PCI_DEVFN(device
, function
));
191 if (newfunc
->pci_dev
) {
192 if (acpiphp_init_func_resource(newfunc
) < 0) {
196 slot
->flags
|= (SLOT_ENABLED
| SLOT_POWEREDON
);
199 /* install notify handler */
200 status
= acpi_install_notify_handler(handle
,
202 handle_hotplug_event_func
,
205 if (ACPI_FAILURE(status
)) {
206 err("failed to register interrupt notify handler\n");
214 /* see if it's worth looking at this bridge */
215 static int detect_ejectable_slots(acpi_handle
*bridge_handle
)
222 /* only check slots defined directly below bridge object */
223 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, bridge_handle
, (u32
)1,
224 is_ejectable_slot
, (void *)&count
, NULL
);
230 /* decode ACPI _CRS data and convert into our internal resource list
234 decode_acpi_resource(struct acpi_resource
*resource
, void *context
)
236 struct acpiphp_bridge
*bridge
= (struct acpiphp_bridge
*) context
;
237 struct acpi_resource_address64 address
;
238 struct pci_resource
*res
;
240 if (resource
->id
!= ACPI_RSTYPE_ADDRESS16
&&
241 resource
->id
!= ACPI_RSTYPE_ADDRESS32
&&
242 resource
->id
!= ACPI_RSTYPE_ADDRESS64
)
245 acpi_resource_to_address64(resource
, &address
);
247 if (address
.producer_consumer
== ACPI_PRODUCER
&& address
.address_length
> 0) {
248 dbg("resource type: %d: 0x%llx - 0x%llx\n", address
.resource_type
,
249 (unsigned long long)address
.min_address_range
,
250 (unsigned long long)address
.max_address_range
);
251 res
= acpiphp_make_resource(address
.min_address_range
,
252 address
.address_length
);
254 err("out of memory\n");
258 switch (address
.resource_type
) {
259 case ACPI_MEMORY_RANGE
:
260 if (address
.attribute
.memory
.cache_attribute
== ACPI_PREFETCHABLE_MEMORY
) {
261 res
->next
= bridge
->p_mem_head
;
262 bridge
->p_mem_head
= res
;
264 res
->next
= bridge
->mem_head
;
265 bridge
->mem_head
= res
;
269 res
->next
= bridge
->io_head
;
270 bridge
->io_head
= res
;
272 case ACPI_BUS_NUMBER_RANGE
:
273 res
->next
= bridge
->bus_head
;
274 bridge
->bus_head
= res
;
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 /* subtract all resources already allocated */
350 acpiphp_detect_pci_resource(bridge
);
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
);
356 /* install notify handler */
357 status
= acpi_install_notify_handler(bridge
->handle
,
359 handle_hotplug_event_bridge
,
362 if (ACPI_FAILURE(status
)) {
363 err("failed to register interrupt notify handler\n");
366 list_add(&bridge
->list
, &bridge_list
);
368 dbg("Bridge resource:\n");
369 acpiphp_dump_resource(bridge
);
373 /* allocate and initialize host bridge data structure */
374 static void add_host_bridge(acpi_handle
*handle
, int seg
, int 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
;
390 bridge
->pci_bus
= pci_find_bus(seg
, bus
);
392 spin_lock_init(&bridge
->res_lock
);
394 /* to be overridden when we decode _CRS */
395 bridge
->sub
= bridge
->bus
;
397 /* decode resources */
399 status
= acpi_walk_resources(handle
, METHOD_NAME__CRS
,
400 decode_acpi_resource
, bridge
);
402 if (ACPI_FAILURE(status
)) {
403 err("failed to decode bridge resources\n");
408 acpiphp_resource_sort_and_combine(&bridge
->io_head
);
409 acpiphp_resource_sort_and_combine(&bridge
->mem_head
);
410 acpiphp_resource_sort_and_combine(&bridge
->p_mem_head
);
411 acpiphp_resource_sort_and_combine(&bridge
->bus_head
);
413 dbg("ACPI _CRS resource:\n");
414 acpiphp_dump_resource(bridge
);
416 if (bridge
->bus_head
) {
417 bridge
->bus
= bridge
->bus_head
->base
;
418 bridge
->sub
= bridge
->bus_head
->base
+ bridge
->bus_head
->length
- 1;
421 init_bridge_misc(bridge
);
425 /* allocate and initialize PCI-to-PCI bridge data structure */
426 static void add_p2p_bridge(acpi_handle
*handle
, int seg
, int bus
, int dev
, int fn
)
428 struct acpiphp_bridge
*bridge
;
432 u32 base
, limit
, base32u
, limit32u
;
434 bridge
= kmalloc(sizeof(struct acpiphp_bridge
), GFP_KERNEL
);
435 if (bridge
== NULL
) {
436 err("out of memory\n");
440 memset(bridge
, 0, sizeof(struct acpiphp_bridge
));
442 bridge
->type
= BRIDGE_TYPE_P2P
;
443 bridge
->handle
= handle
;
446 bridge
->pci_dev
= pci_find_slot(bus
, PCI_DEVFN(dev
, fn
));
447 if (!bridge
->pci_dev
) {
448 err("Can't get pci_dev\n");
453 bridge
->pci_bus
= bridge
->pci_dev
->subordinate
;
454 if (!bridge
->pci_bus
) {
455 err("This is not a PCI-to-PCI bridge!\n");
460 spin_lock_init(&bridge
->res_lock
);
462 bridge
->bus
= bridge
->pci_bus
->number
;
463 bridge
->sub
= bridge
->pci_bus
->subordinate
;
466 * decode resources under this P2P bridge
470 pci_read_config_byte(bridge
->pci_dev
, PCI_IO_BASE
, &tmp8
);
472 pci_read_config_byte(bridge
->pci_dev
, PCI_IO_LIMIT
, &tmp8
);
475 switch (base
& PCI_IO_RANGE_TYPE_MASK
) {
476 case PCI_IO_RANGE_TYPE_16
:
477 base
= (base
<< 8) & 0xf000;
478 limit
= ((limit
<< 8) & 0xf000) + 0xfff;
479 bridge
->io_head
= acpiphp_make_resource((u64
)base
, limit
- base
+ 1);
480 if (!bridge
->io_head
) {
481 err("out of memory\n");
485 dbg("16bit I/O range: %04x-%04x\n",
486 (u32
)bridge
->io_head
->base
,
487 (u32
)(bridge
->io_head
->base
+ bridge
->io_head
->length
- 1));
489 case PCI_IO_RANGE_TYPE_32
:
490 pci_read_config_word(bridge
->pci_dev
, PCI_IO_BASE_UPPER16
, &tmp16
);
491 base
= ((u32
)tmp16
<< 16) | ((base
<< 8) & 0xf000);
492 pci_read_config_word(bridge
->pci_dev
, PCI_IO_LIMIT_UPPER16
, &tmp16
);
493 limit
= (((u32
)tmp16
<< 16) | ((limit
<< 8) & 0xf000)) + 0xfff;
494 bridge
->io_head
= acpiphp_make_resource((u64
)base
, limit
- base
+ 1);
495 if (!bridge
->io_head
) {
496 err("out of memory\n");
500 dbg("32bit I/O range: %08x-%08x\n",
501 (u32
)bridge
->io_head
->base
,
502 (u32
)(bridge
->io_head
->base
+ bridge
->io_head
->length
- 1));
505 dbg("I/O space unsupported\n");
508 warn("Unknown I/O range type\n");
511 /* Memory resources (mandatory for P2P bridge) */
512 pci_read_config_word(bridge
->pci_dev
, PCI_MEMORY_BASE
, &tmp16
);
513 base
= (tmp16
& 0xfff0) << 16;
514 pci_read_config_word(bridge
->pci_dev
, PCI_MEMORY_LIMIT
, &tmp16
);
515 limit
= ((tmp16
& 0xfff0) << 16) | 0xfffff;
516 bridge
->mem_head
= acpiphp_make_resource((u64
)base
, limit
- base
+ 1);
517 if (!bridge
->mem_head
) {
518 err("out of memory\n");
522 dbg("32bit Memory range: %08x-%08x\n",
523 (u32
)bridge
->mem_head
->base
,
524 (u32
)(bridge
->mem_head
->base
+ bridge
->mem_head
->length
-1));
526 /* Prefetchable Memory resources (optional) */
527 pci_read_config_word(bridge
->pci_dev
, PCI_PREF_MEMORY_BASE
, &tmp16
);
529 pci_read_config_word(bridge
->pci_dev
, PCI_PREF_MEMORY_LIMIT
, &tmp16
);
532 switch (base
& PCI_MEMORY_RANGE_TYPE_MASK
) {
533 case PCI_PREF_RANGE_TYPE_32
:
534 base
= (base
& 0xfff0) << 16;
535 limit
= ((limit
& 0xfff0) << 16) | 0xfffff;
536 bridge
->p_mem_head
= acpiphp_make_resource((u64
)base
, limit
- base
+ 1);
537 if (!bridge
->p_mem_head
) {
538 err("out of memory\n");
542 dbg("32bit Prefetchable memory range: %08x-%08x\n",
543 (u32
)bridge
->p_mem_head
->base
,
544 (u32
)(bridge
->p_mem_head
->base
+ bridge
->p_mem_head
->length
- 1));
546 case PCI_PREF_RANGE_TYPE_64
:
547 pci_read_config_dword(bridge
->pci_dev
, PCI_PREF_BASE_UPPER32
, &base32u
);
548 pci_read_config_dword(bridge
->pci_dev
, PCI_PREF_LIMIT_UPPER32
, &limit32u
);
549 base64
= ((u64
)base32u
<< 32) | ((base
& 0xfff0) << 16);
550 limit64
= (((u64
)limit32u
<< 32) | ((limit
& 0xfff0) << 16)) + 0xfffff;
552 bridge
->p_mem_head
= acpiphp_make_resource(base64
, limit64
- base64
+ 1);
553 if (!bridge
->p_mem_head
) {
554 err("out of memory\n");
558 dbg("64bit Prefetchable memory range: %08x%08x-%08x%08x\n",
559 (u32
)(bridge
->p_mem_head
->base
>> 32),
560 (u32
)(bridge
->p_mem_head
->base
& 0xffffffff),
561 (u32
)((bridge
->p_mem_head
->base
+ bridge
->p_mem_head
->length
- 1) >> 32),
562 (u32
)((bridge
->p_mem_head
->base
+ bridge
->p_mem_head
->length
- 1) & 0xffffffff));
567 warn("Unknown prefetchale memory type\n");
570 init_bridge_misc(bridge
);
574 /* callback routine to find P2P bridges */
576 find_p2p_bridge(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
579 acpi_handle dummy_handle
;
580 unsigned long *segbus
= context
;
582 int seg
, bus
, device
, function
;
585 /* get PCI address */
586 seg
= (*segbus
>> 8) & 0xff;
587 bus
= *segbus
& 0xff;
589 status
= acpi_get_handle(handle
, "_ADR", &dummy_handle
);
590 if (ACPI_FAILURE(status
))
591 return AE_OK
; /* continue */
593 status
= acpi_evaluate_integer(handle
, "_ADR", NULL
, &tmp
);
594 if (ACPI_FAILURE(status
)) {
595 dbg("%s: _ADR evaluation failure\n", __FUNCTION__
);
599 device
= (tmp
>> 16) & 0xffff;
600 function
= tmp
& 0xffff;
602 dev
= pci_find_slot(bus
, PCI_DEVFN(device
, function
));
607 if (!dev
->subordinate
)
610 /* check if this bridge has ejectable slots */
611 if (detect_ejectable_slots(handle
) > 0) {
612 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev
));
613 add_p2p_bridge(handle
, seg
, bus
, device
, function
);
620 /* find hot-pluggable slots, and then find P2P bridge */
621 static int add_bridge(acpi_handle handle
)
626 acpi_handle dummy_handle
;
628 /* if the bridge doesn't have _STA, we assume it is always there */
629 status
= acpi_get_handle(handle
, "_STA", &dummy_handle
);
630 if (ACPI_SUCCESS(status
)) {
631 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &tmp
);
632 if (ACPI_FAILURE(status
)) {
633 dbg("%s: _STA evaluation failure\n", __FUNCTION__
);
636 if ((tmp
& ACPI_STA_FUNCTIONING
) == 0)
637 /* don't register this object */
641 /* get PCI segment number */
642 status
= acpi_evaluate_integer(handle
, "_SEG", NULL
, &tmp
);
644 seg
= ACPI_SUCCESS(status
) ? tmp
: 0;
646 /* get PCI bus number */
647 status
= acpi_evaluate_integer(handle
, "_BBN", NULL
, &tmp
);
649 if (ACPI_SUCCESS(status
)) {
652 warn("can't get bus number, assuming 0\n");
656 /* check if this bridge has ejectable slots */
657 if (detect_ejectable_slots(handle
) > 0) {
658 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
659 add_host_bridge(handle
, seg
, bus
);
663 tmp
= seg
<< 8 | bus
;
665 /* search P2P bridges under this host bridge */
666 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, (u32
)1,
667 find_p2p_bridge
, &tmp
, NULL
);
669 if (ACPI_FAILURE(status
))
670 warn("find_p2p_bridge faied (error code = 0x%x)\n",status
);
676 static void remove_bridge(acpi_handle handle
)
678 /* No-op for now .. */
682 static int power_on_slot(struct acpiphp_slot
*slot
)
685 struct acpiphp_func
*func
;
689 /* if already enabled, just skip */
690 if (slot
->flags
& SLOT_POWEREDON
)
693 list_for_each (l
, &slot
->funcs
) {
694 func
= list_entry(l
, struct acpiphp_func
, sibling
);
696 if (func
->flags
& FUNC_HAS_PS0
) {
697 dbg("%s: executing _PS0\n", __FUNCTION__
);
698 status
= acpi_evaluate_object(func
->handle
, "_PS0", NULL
, NULL
);
699 if (ACPI_FAILURE(status
)) {
700 warn("%s: _PS0 failed\n", __FUNCTION__
);
708 /* TBD: evaluate _STA to check if the slot is enabled */
710 slot
->flags
|= SLOT_POWEREDON
;
717 static int power_off_slot(struct acpiphp_slot
*slot
)
720 struct acpiphp_func
*func
;
722 struct acpi_object_list arg_list
;
723 union acpi_object arg
;
727 /* if already disabled, just skip */
728 if ((slot
->flags
& SLOT_POWEREDON
) == 0)
731 list_for_each (l
, &slot
->funcs
) {
732 func
= list_entry(l
, struct acpiphp_func
, sibling
);
734 if (func
->pci_dev
&& (func
->flags
& FUNC_HAS_PS3
)) {
735 status
= acpi_evaluate_object(func
->handle
, "_PS3", NULL
, NULL
);
736 if (ACPI_FAILURE(status
)) {
737 warn("%s: _PS3 failed\n", __FUNCTION__
);
745 list_for_each (l
, &slot
->funcs
) {
746 func
= list_entry(l
, struct acpiphp_func
, sibling
);
748 /* We don't want to call _EJ0 on non-existing functions. */
749 if (func
->pci_dev
&& (func
->flags
& FUNC_HAS_EJ0
)) {
750 /* _EJ0 method take one argument */
752 arg_list
.pointer
= &arg
;
753 arg
.type
= ACPI_TYPE_INTEGER
;
754 arg
.integer
.value
= 1;
756 status
= acpi_evaluate_object(func
->handle
, "_EJ0", &arg_list
, NULL
);
757 if (ACPI_FAILURE(status
)) {
758 warn("%s: _EJ0 failed\n", __FUNCTION__
);
766 /* TBD: evaluate _STA to check if the slot is disabled */
768 slot
->flags
&= (~SLOT_POWEREDON
);
776 * enable_device - enable, configure a slot
777 * @slot: slot to be enabled
779 * This function should be called per *physical slot*,
780 * not per each slot object in ACPI namespace.
783 static int enable_device(struct acpiphp_slot
*slot
)
787 struct pci_bus
*child
;
789 struct acpiphp_func
*func
;
793 if (slot
->flags
& SLOT_ENABLED
)
796 /* sanity check: dev should be NULL when hot-plugged in */
797 dev
= pci_find_slot(slot
->bridge
->bus
, PCI_DEVFN(slot
->device
, 0));
799 /* This case shouldn't happen */
800 err("pci_dev structure already exists.\n");
805 /* allocate resources to device */
806 retval
= acpiphp_configure_slot(slot
);
810 /* returned `dev' is the *first function* only! */
811 num
= pci_scan_slot(slot
->bridge
->pci_bus
, PCI_DEVFN(slot
->device
, 0));
813 pci_bus_add_devices(slot
->bridge
->pci_bus
);
814 dev
= pci_find_slot(slot
->bridge
->bus
, PCI_DEVFN(slot
->device
, 0));
817 err("No new device found\n");
822 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
) {
823 pci_read_config_byte(dev
, PCI_SECONDARY_BUS
, &bus
);
824 child
= (struct pci_bus
*) pci_add_new_bus(dev
->bus
, dev
, bus
);
825 pci_do_scan_bus(child
);
828 /* associate pci_dev to our representation */
829 list_for_each (l
, &slot
->funcs
) {
830 func
= list_entry(l
, struct acpiphp_func
, sibling
);
832 func
->pci_dev
= pci_find_slot(slot
->bridge
->bus
,
833 PCI_DEVFN(slot
->device
,
838 /* configure device */
839 retval
= acpiphp_configure_function(func
);
844 slot
->flags
|= SLOT_ENABLED
;
846 dbg("Available resources:\n");
847 acpiphp_dump_resource(slot
->bridge
);
855 * disable_device - disable a slot
857 static int disable_device(struct acpiphp_slot
*slot
)
860 struct acpiphp_func
*func
;
863 /* is this slot already disabled? */
864 if (!(slot
->flags
& SLOT_ENABLED
))
867 list_for_each (l
, &slot
->funcs
) {
868 func
= list_entry(l
, struct acpiphp_func
, sibling
);
871 acpiphp_unconfigure_function(func
);
874 slot
->flags
&= (~SLOT_ENABLED
);
882 * get_slot_status - get ACPI slot status
884 * if a slot has _STA for each function and if any one of them
885 * returned non-zero status, return it
887 * if a slot doesn't have _STA and if any one of its functions'
888 * configuration space is configured, return 0x0f as a _STA
892 static unsigned int get_slot_status(struct acpiphp_slot
*slot
)
895 unsigned long sta
= 0;
898 struct acpiphp_func
*func
;
900 list_for_each (l
, &slot
->funcs
) {
901 func
= list_entry(l
, struct acpiphp_func
, sibling
);
903 if (func
->flags
& FUNC_HAS_STA
) {
904 status
= acpi_evaluate_integer(func
->handle
, "_STA", NULL
, &sta
);
905 if (ACPI_SUCCESS(status
) && sta
)
908 pci_bus_read_config_dword(slot
->bridge
->pci_bus
,
909 PCI_DEVFN(slot
->device
,
911 PCI_VENDOR_ID
, &dvid
);
912 if (dvid
!= 0xffffffff) {
919 return (unsigned int)sta
;
923 * acpiphp_check_bridge - re-enumerate devices
925 * Iterate over all slots under this bridge and make sure that if a
926 * card is present they are enabled, and if not they are disabled.
928 static int acpiphp_check_bridge(struct acpiphp_bridge
*bridge
)
930 struct acpiphp_slot
*slot
;
932 int enabled
, disabled
;
934 enabled
= disabled
= 0;
936 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
937 unsigned int status
= get_slot_status(slot
);
938 if (slot
->flags
& SLOT_ENABLED
) {
939 if (status
== ACPI_STA_ALL
)
941 retval
= acpiphp_disable_slot(slot
);
943 err("Error occurred in disabling\n");
948 if (status
!= ACPI_STA_ALL
)
950 retval
= acpiphp_enable_slot(slot
);
952 err("Error occurred in enabling\n");
959 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__
, enabled
, disabled
);
966 * ACPI event handlers
970 * handle_hotplug_event_bridge - handle ACPI event on bridges
972 * @handle: Notify()'ed acpi_handle
974 * @context: pointer to acpiphp_bridge structure
976 * handles ACPI event notification on {host,p2p} bridges
979 static void handle_hotplug_event_bridge(acpi_handle handle
, u32 type
, void *context
)
981 struct acpiphp_bridge
*bridge
;
983 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
984 .pointer
= objname
};
986 bridge
= (struct acpiphp_bridge
*)context
;
988 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
991 case ACPI_NOTIFY_BUS_CHECK
:
992 /* bus re-enumerate */
993 dbg("%s: Bus check notify on %s\n", __FUNCTION__
, objname
);
994 acpiphp_check_bridge(bridge
);
997 case ACPI_NOTIFY_DEVICE_CHECK
:
999 dbg("%s: Device check notify on %s\n", __FUNCTION__
, objname
);
1000 acpiphp_check_bridge(bridge
);
1003 case ACPI_NOTIFY_DEVICE_WAKE
:
1005 dbg("%s: Device wake notify on %s\n", __FUNCTION__
, objname
);
1008 case ACPI_NOTIFY_EJECT_REQUEST
:
1009 /* request device eject */
1010 dbg("%s: Device eject notify on %s\n", __FUNCTION__
, objname
);
1013 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
1014 printk(KERN_ERR
"Device %s cannot be configured due"
1015 " to a frequency mismatch\n", objname
);
1018 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
1019 printk(KERN_ERR
"Device %s cannot be configured due"
1020 " to a bus mode mismatch\n", objname
);
1023 case ACPI_NOTIFY_POWER_FAULT
:
1024 printk(KERN_ERR
"Device %s has suffered a power fault\n",
1029 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1036 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1038 * @handle: Notify()'ed acpi_handle
1039 * @type: Notify code
1040 * @context: pointer to acpiphp_func structure
1042 * handles ACPI event notification on slots
1045 static void handle_hotplug_event_func(acpi_handle handle
, u32 type
, void *context
)
1047 struct acpiphp_func
*func
;
1049 struct acpi_buffer buffer
= { .length
= sizeof(objname
),
1050 .pointer
= objname
};
1052 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1054 func
= (struct acpiphp_func
*)context
;
1057 case ACPI_NOTIFY_BUS_CHECK
:
1058 /* bus re-enumerate */
1059 dbg("%s: Bus check notify on %s\n", __FUNCTION__
, objname
);
1060 acpiphp_enable_slot(func
->slot
);
1063 case ACPI_NOTIFY_DEVICE_CHECK
:
1064 /* device check : re-enumerate from parent bus */
1065 dbg("%s: Device check notify on %s\n", __FUNCTION__
, objname
);
1066 acpiphp_check_bridge(func
->slot
->bridge
);
1069 case ACPI_NOTIFY_DEVICE_WAKE
:
1071 dbg("%s: Device wake notify on %s\n", __FUNCTION__
, objname
);
1074 case ACPI_NOTIFY_EJECT_REQUEST
:
1075 /* request device eject */
1076 dbg("%s: Device eject notify on %s\n", __FUNCTION__
, objname
);
1077 acpiphp_disable_slot(func
->slot
);
1081 warn("notify_handler: unknown event type 0x%x for %s\n", type
, objname
);
1087 static struct acpi_pci_driver acpi_pci_hp_driver
= {
1089 .remove
= remove_bridge
,
1093 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1096 int __init
acpiphp_glue_init(void)
1100 if (list_empty(&pci_root_buses
))
1103 num
= acpi_pci_register_driver(&acpi_pci_hp_driver
);
1113 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1115 * This function frees all data allocated in acpiphp_glue_init()
1117 void __exit
acpiphp_glue_exit(void)
1119 struct list_head
*l1
, *l2
, *n1
, *n2
;
1120 struct acpiphp_bridge
*bridge
;
1121 struct acpiphp_slot
*slot
, *next
;
1122 struct acpiphp_func
*func
;
1125 list_for_each_safe (l1
, n1
, &bridge_list
) {
1126 bridge
= (struct acpiphp_bridge
*)l1
;
1127 slot
= bridge
->slots
;
1130 list_for_each_safe (l2
, n2
, &slot
->funcs
) {
1131 func
= list_entry(l2
, struct acpiphp_func
, sibling
);
1132 acpiphp_free_resource(&func
->io_head
);
1133 acpiphp_free_resource(&func
->mem_head
);
1134 acpiphp_free_resource(&func
->p_mem_head
);
1135 acpiphp_free_resource(&func
->bus_head
);
1136 status
= acpi_remove_notify_handler(func
->handle
,
1138 handle_hotplug_event_func
);
1139 if (ACPI_FAILURE(status
))
1140 err("failed to remove notify handler\n");
1146 status
= acpi_remove_notify_handler(bridge
->handle
, ACPI_SYSTEM_NOTIFY
,
1147 handle_hotplug_event_bridge
);
1148 if (ACPI_FAILURE(status
))
1149 err("failed to remove notify handler\n");
1151 acpiphp_free_resource(&bridge
->io_head
);
1152 acpiphp_free_resource(&bridge
->mem_head
);
1153 acpiphp_free_resource(&bridge
->p_mem_head
);
1154 acpiphp_free_resource(&bridge
->bus_head
);
1159 acpi_pci_unregister_driver(&acpi_pci_hp_driver
);
1164 * acpiphp_get_num_slots - count number of slots in a system
1166 int __init
acpiphp_get_num_slots(void)
1168 struct list_head
*node
;
1169 struct acpiphp_bridge
*bridge
;
1174 list_for_each (node
, &bridge_list
) {
1175 bridge
= (struct acpiphp_bridge
*)node
;
1176 dbg("Bus%d %dslot(s)\n", bridge
->bus
, bridge
->nr_slots
);
1177 num_slots
+= bridge
->nr_slots
;
1180 dbg("Total %dslots\n", num_slots
);
1187 * acpiphp_for_each_slot - call function for each slot
1188 * @fn: callback function
1189 * @data: context to be passed to callback function
1192 static int acpiphp_for_each_slot(acpiphp_callback fn
, void *data
)
1194 struct list_head
*node
;
1195 struct acpiphp_bridge
*bridge
;
1196 struct acpiphp_slot
*slot
;
1199 list_for_each (node
, &bridge_list
) {
1200 bridge
= (struct acpiphp_bridge
*)node
;
1201 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
) {
1202 retval
= fn(slot
, data
);
1213 /* search matching slot from id */
1214 struct acpiphp_slot
*get_slot_from_id(int id
)
1216 struct list_head
*node
;
1217 struct acpiphp_bridge
*bridge
;
1218 struct acpiphp_slot
*slot
;
1220 list_for_each (node
, &bridge_list
) {
1221 bridge
= (struct acpiphp_bridge
*)node
;
1222 for (slot
= bridge
->slots
; slot
; slot
= slot
->next
)
1227 /* should never happen! */
1228 err("%s: no object for id %d\n", __FUNCTION__
, id
);
1235 * acpiphp_enable_slot - power on slot
1237 int acpiphp_enable_slot(struct acpiphp_slot
*slot
)
1241 down(&slot
->crit_sect
);
1243 /* wake up all functions */
1244 retval
= power_on_slot(slot
);
1248 if (get_slot_status(slot
) == ACPI_STA_ALL
)
1249 /* configure all functions */
1250 retval
= enable_device(slot
);
1253 up(&slot
->crit_sect
);
1259 * acpiphp_disable_slot - power off slot
1261 int acpiphp_disable_slot(struct acpiphp_slot
*slot
)
1265 down(&slot
->crit_sect
);
1267 /* unconfigure all functions */
1268 retval
= disable_device(slot
);
1272 /* power off all functions */
1273 retval
= power_off_slot(slot
);
1277 acpiphp_resource_sort_and_combine(&slot
->bridge
->io_head
);
1278 acpiphp_resource_sort_and_combine(&slot
->bridge
->mem_head
);
1279 acpiphp_resource_sort_and_combine(&slot
->bridge
->p_mem_head
);
1280 acpiphp_resource_sort_and_combine(&slot
->bridge
->bus_head
);
1281 dbg("Available resources:\n");
1282 acpiphp_dump_resource(slot
->bridge
);
1285 up(&slot
->crit_sect
);
1294 u8
acpiphp_get_power_status(struct acpiphp_slot
*slot
)
1298 sta
= get_slot_status(slot
);
1300 return (sta
& ACPI_STA_ENABLED
) ? 1 : 0;
1308 u8
acpiphp_get_latch_status(struct acpiphp_slot
*slot
)
1312 sta
= get_slot_status(slot
);
1314 return (sta
& ACPI_STA_SHOW_IN_UI
) ? 1 : 0;
1319 * adapter presence : 1
1322 u8
acpiphp_get_adapter_status(struct acpiphp_slot
*slot
)
1326 sta
= get_slot_status(slot
);
1328 return (sta
== 0) ? 0 : 1;
1333 * pci address (seg/bus/dev)
1335 u32
acpiphp_get_address(struct acpiphp_slot
*slot
)
1339 address
= ((slot
->bridge
->seg
) << 16) |
1340 ((slot
->bridge
->bus
) << 8) |