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>
37 #include "pci_hotplug.h"
40 static LIST_HEAD(bridge_list);
42 #define MY_NAME "acpiphp_glue"
44 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
45 static void handle_hotplug_event_func (acpi_handle, u32, void *);
48 * initialization & terminatation routines
52 * is_ejectable - determine if a slot is ejectable
53 * @handle: handle to acpi namespace
55 * Ejectable slot should satisfy at least these conditions:
68 static int is_ejectable (acpi_handle handle)
73 status = acpi_get_handle(handle, "_ADR", &tmp);
74 if (ACPI_FAILURE(status)) {
78 status = acpi_get_handle(handle, "_EJ0", &tmp);
79 if (ACPI_FAILURE(status)) {
87 /* callback routine to check the existence of ejectable slots */
89 is_ejectable_slot (acpi_handle handle, u32 lvl, void *context, void **rv)
91 int *count = (int *)context;
93 if (is_ejectable(handle)) {
95 /* only one ejectable slot is enough */
96 return AE_CTRL_TERMINATE;
103 /* callback routine to register each ACPI PCI slot object */
105 register_slot (acpi_handle handle, u32 lvl, void *context, void **rv)
107 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
108 struct acpiphp_slot *slot;
109 struct acpiphp_func *newfunc;
111 acpi_status status = AE_OK;
112 unsigned long adr, sun;
113 int device, function;
114 static int num_slots = 0; /* XXX if we support I/O node hotplug... */
116 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
118 if (ACPI_FAILURE(status))
121 status = acpi_get_handle(handle, "_EJ0", &tmp);
123 if (ACPI_FAILURE(status))
126 device = (adr >> 16) & 0xffff;
127 function = adr & 0xffff;
129 newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
132 memset(newfunc, 0, sizeof(struct acpiphp_func));
134 INIT_LIST_HEAD(&newfunc->sibling);
135 newfunc->handle = handle;
136 newfunc->function = function;
137 newfunc->flags = FUNC_HAS_EJ0;
139 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
140 newfunc->flags |= FUNC_HAS_STA;
142 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
143 newfunc->flags |= FUNC_HAS_PS0;
145 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
146 newfunc->flags |= FUNC_HAS_PS3;
148 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
149 if (ACPI_FAILURE(status))
152 /* search for objects that share the same slot */
153 for (slot = bridge->slots; slot; slot = slot->next)
154 if (slot->device == device) {
155 if (slot->sun != sun)
156 warn("sibling found, but _SUN doesn't match!\n");
161 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
167 memset(slot, 0, sizeof(struct acpiphp_slot));
168 slot->bridge = bridge;
169 slot->id = num_slots++;
170 slot->device = device;
172 INIT_LIST_HEAD(&slot->funcs);
173 init_MUTEX(&slot->crit_sect);
175 slot->next = bridge->slots;
176 bridge->slots = slot;
180 dbg("found ACPI PCI Hotplug slot at PCI %02x:%02x Slot:%d\n",
181 slot->bridge->bus, slot->device, slot->sun);
184 newfunc->slot = slot;
185 list_add_tail(&newfunc->sibling, &slot->funcs);
187 /* associate corresponding pci_dev */
188 newfunc->pci_dev = pci_find_slot(bridge->bus,
189 PCI_DEVFN(device, function));
190 if (newfunc->pci_dev) {
191 if (acpiphp_init_func_resource(newfunc) < 0) {
195 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
198 /* install notify handler */
199 status = acpi_install_notify_handler(handle,
201 handle_hotplug_event_func,
204 if (ACPI_FAILURE(status)) {
205 err("failed to register interrupt notify handler\n");
213 /* see if it's worth looking at this bridge */
214 static int detect_ejectable_slots (acpi_handle *bridge_handle)
221 /* only check slots defined directly below bridge object */
222 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
223 is_ejectable_slot, (void *)&count, NULL);
229 /* decode ACPI _CRS data and convert into our internal resource list
233 decode_acpi_resource (struct acpi_resource *resource, struct acpiphp_bridge *bridge)
235 struct acpi_resource_address16 *address16_data;
236 struct acpi_resource_address32 *address32_data;
237 struct acpi_resource_address64 *address64_data;
238 struct pci_resource *res;
240 u32 resource_type, producer_consumer, address_length;
241 u64 min_address_range, max_address_range;
242 u16 cache_attribute = 0;
247 resource_type = producer_consumer = address_length = 0;
248 min_address_range = max_address_range = 0;
253 switch (resource->id) {
254 case ACPI_RSTYPE_ADDRESS16:
255 address16_data = (struct acpi_resource_address16 *)&resource->data;
256 resource_type = address16_data->resource_type;
257 producer_consumer = address16_data->producer_consumer;
258 min_address_range = address16_data->min_address_range;
259 max_address_range = address16_data->max_address_range;
260 address_length = address16_data->address_length;
261 if (resource_type == ACPI_MEMORY_RANGE)
262 cache_attribute = address16_data->attribute.memory.cache_attribute;
266 case ACPI_RSTYPE_ADDRESS32:
267 address32_data = (struct acpi_resource_address32 *)&resource->data;
268 resource_type = address32_data->resource_type;
269 producer_consumer = address32_data->producer_consumer;
270 min_address_range = address32_data->min_address_range;
271 max_address_range = address32_data->max_address_range;
272 address_length = address32_data->address_length;
273 if (resource_type == ACPI_MEMORY_RANGE)
274 cache_attribute = address32_data->attribute.memory.cache_attribute;
278 case ACPI_RSTYPE_ADDRESS64:
279 address64_data = (struct acpi_resource_address64 *)&resource->data;
280 resource_type = address64_data->resource_type;
281 producer_consumer = address64_data->producer_consumer;
282 min_address_range = address64_data->min_address_range;
283 max_address_range = address64_data->max_address_range;
284 address_length = address64_data->address_length;
285 if (resource_type == ACPI_MEMORY_RANGE)
286 cache_attribute = address64_data->attribute.memory.cache_attribute;
290 case ACPI_RSTYPE_END_TAG:
299 resource = (struct acpi_resource *)((char*)resource + resource->length);
301 if (found && producer_consumer == ACPI_PRODUCER && address_length > 0) {
302 switch (resource_type) {
303 case ACPI_MEMORY_RANGE:
304 if (cache_attribute == ACPI_PREFETCHABLE_MEMORY) {
305 dbg("resource type: prefetchable memory 0x%x - 0x%x\n", (u32)min_address_range, (u32)max_address_range);
306 res = acpiphp_make_resource(min_address_range,
309 err("out of memory\n");
312 res->next = bridge->p_mem_head;
313 bridge->p_mem_head = res;
315 dbg("resource type: memory 0x%x - 0x%x\n", (u32)min_address_range, (u32)max_address_range);
316 res = acpiphp_make_resource(min_address_range,
319 err("out of memory\n");
322 res->next = bridge->mem_head;
323 bridge->mem_head = res;
327 dbg("resource type: io 0x%x - 0x%x\n", (u32)min_address_range, (u32)max_address_range);
328 res = acpiphp_make_resource(min_address_range,
331 err("out of memory\n");
334 res->next = bridge->io_head;
335 bridge->io_head = res;
337 case ACPI_BUS_NUMBER_RANGE:
338 dbg("resource type: bus number %d - %d\n", (u32)min_address_range, (u32)max_address_range);
339 res = acpiphp_make_resource(min_address_range,
342 err("out of memory\n");
345 res->next = bridge->bus_head;
346 bridge->bus_head = res;
355 acpiphp_resource_sort_and_combine(&bridge->io_head);
356 acpiphp_resource_sort_and_combine(&bridge->mem_head);
357 acpiphp_resource_sort_and_combine(&bridge->p_mem_head);
358 acpiphp_resource_sort_and_combine(&bridge->bus_head);
360 dbg("ACPI _CRS resource:\n");
361 acpiphp_dump_resource(bridge);
365 /* find pci_bus structure associated to specific bus number */
366 static struct pci_bus *find_pci_bus(const struct list_head *list, int bus)
368 const struct list_head *l;
370 list_for_each (l, list) {
371 struct pci_bus *b = pci_bus_b(l);
372 if (b->number == bus)
375 if (!list_empty(&b->children)) {
376 /* XXX recursive call */
377 b = find_pci_bus(&b->children, bus);
388 /* decode ACPI 2.0 _HPP hot plug parameters */
389 static void decode_hpp(struct acpiphp_bridge *bridge)
392 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
394 union acpi_object *package;
397 /* default numbers */
398 bridge->hpp.cache_line_size = 0x10;
399 bridge->hpp.latency_timer = 0x40;
400 bridge->hpp.enable_SERR = 0;
401 bridge->hpp.enable_PERR = 0;
403 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
405 if (ACPI_FAILURE(status)) {
406 dbg("_HPP evaluation failed\n");
410 package = (union acpi_object *) buffer.pointer;
412 if (!package || package->type != ACPI_TYPE_PACKAGE ||
413 package->package.count != 4 || !package->package.elements) {
414 err("invalid _HPP object; ignoring\n");
418 for (i = 0; i < 4; i++) {
419 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
420 err("invalid _HPP parameter type; ignoring\n");
425 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
426 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
427 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
428 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
430 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
431 bridge->hpp.cache_line_size,
432 bridge->hpp.latency_timer,
433 bridge->hpp.enable_SERR,
434 bridge->hpp.enable_PERR);
436 bridge->flags |= BRIDGE_HAS_HPP;
439 kfree(buffer.pointer);
443 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
444 static void init_bridge_misc (struct acpiphp_bridge *bridge)
448 /* decode ACPI 2.0 _HPP (hot plug parameters) */
451 /* subtract all resources already allocated */
452 acpiphp_detect_pci_resource(bridge);
454 /* register all slot objects under this bridge */
455 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
456 register_slot, bridge, NULL);
458 /* install notify handler */
459 status = acpi_install_notify_handler(bridge->handle,
461 handle_hotplug_event_bridge,
464 if (ACPI_FAILURE(status)) {
465 err("failed to register interrupt notify handler\n");
468 list_add(&bridge->list, &bridge_list);
470 dbg("Bridge resource:\n");
471 acpiphp_dump_resource(bridge);
475 /* allocate and initialize host bridge data structure */
476 static void add_host_bridge (acpi_handle *handle, int seg, int bus)
479 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
481 struct acpiphp_bridge *bridge;
483 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
487 memset(bridge, 0, sizeof(struct acpiphp_bridge));
489 bridge->type = BRIDGE_TYPE_HOST;
490 bridge->handle = handle;
494 bridge->pci_bus = find_pci_bus(&pci_root_buses, bus);
496 bridge->res_lock = SPIN_LOCK_UNLOCKED;
498 /* to be overridden when we decode _CRS */
499 bridge->sub = bridge->bus;
501 /* decode resources */
503 status = acpi_get_current_resources(handle, &buffer);
505 if (ACPI_FAILURE(status)) {
506 err("failed to decode bridge resources\n");
511 decode_acpi_resource(buffer.pointer, bridge);
512 kfree(buffer.pointer);
514 if (bridge->bus_head) {
515 bridge->bus = bridge->bus_head->base;
516 bridge->sub = bridge->bus_head->base + bridge->bus_head->length - 1;
519 init_bridge_misc(bridge);
523 /* allocate and initialize PCI-to-PCI bridge data structure */
524 static void add_p2p_bridge (acpi_handle *handle, int seg, int bus, int dev, int fn)
526 struct acpiphp_bridge *bridge;
530 u32 base, limit, base32u, limit32u;
532 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
533 if (bridge == NULL) {
534 err("out of memory\n");
538 memset(bridge, 0, sizeof(struct acpiphp_bridge));
540 bridge->type = BRIDGE_TYPE_P2P;
541 bridge->handle = handle;
544 bridge->pci_dev = pci_find_slot(bus, PCI_DEVFN(dev, fn));
545 if (!bridge->pci_dev) {
546 err("Can't get pci_dev\n");
551 bridge->pci_bus = bridge->pci_dev->subordinate;
552 if (!bridge->pci_bus) {
553 err("This is not a PCI-to-PCI bridge!\n");
558 bridge->res_lock = SPIN_LOCK_UNLOCKED;
560 bridge->bus = bridge->pci_bus->number;
561 bridge->sub = bridge->pci_bus->subordinate;
564 * decode resources under this P2P bridge
568 pci_read_config_byte(bridge->pci_dev, PCI_IO_BASE, &tmp8);
570 pci_read_config_byte(bridge->pci_dev, PCI_IO_LIMIT, &tmp8);
573 switch (base & PCI_IO_RANGE_TYPE_MASK) {
574 case PCI_IO_RANGE_TYPE_16:
575 base = (base << 8) & 0xf000;
576 limit = ((limit << 8) & 0xf000) + 0xfff;
577 bridge->io_head = acpiphp_make_resource((u64)base, limit - base + 1);
578 if (!bridge->io_head) {
579 err("out of memory\n");
583 dbg("16bit I/O range: %04x-%04x\n",
584 (u32)bridge->io_head->base,
585 (u32)(bridge->io_head->base + bridge->io_head->length - 1));
587 case PCI_IO_RANGE_TYPE_32:
588 pci_read_config_word(bridge->pci_dev, PCI_IO_BASE_UPPER16, &tmp16);
589 base = ((u32)tmp16 << 16) | ((base << 8) & 0xf000);
590 pci_read_config_word(bridge->pci_dev, PCI_IO_LIMIT_UPPER16, &tmp16);
591 limit = (((u32)tmp16 << 16) | ((limit << 8) & 0xf000)) + 0xfff;
592 bridge->io_head = acpiphp_make_resource((u64)base, limit - base + 1);
593 if (!bridge->io_head) {
594 err("out of memory\n");
598 dbg("32bit I/O range: %08x-%08x\n",
599 (u32)bridge->io_head->base,
600 (u32)(bridge->io_head->base + bridge->io_head->length - 1));
603 dbg("I/O space unsupported\n");
606 warn("Unknown I/O range type\n");
609 /* Memory resources (mandatory for P2P bridge) */
610 pci_read_config_word(bridge->pci_dev, PCI_MEMORY_BASE, &tmp16);
611 base = (tmp16 & 0xfff0) << 16;
612 pci_read_config_word(bridge->pci_dev, PCI_MEMORY_LIMIT, &tmp16);
613 limit = ((tmp16 & 0xfff0) << 16) | 0xfffff;
614 bridge->mem_head = acpiphp_make_resource((u64)base, limit - base + 1);
615 if (!bridge->mem_head) {
616 err("out of memory\n");
620 dbg("32bit Memory range: %08x-%08x\n",
621 (u32)bridge->mem_head->base,
622 (u32)(bridge->mem_head->base + bridge->mem_head->length-1));
624 /* Prefetchable Memory resources (optional) */
625 pci_read_config_word(bridge->pci_dev, PCI_PREF_MEMORY_BASE, &tmp16);
627 pci_read_config_word(bridge->pci_dev, PCI_PREF_MEMORY_LIMIT, &tmp16);
630 switch (base & PCI_MEMORY_RANGE_TYPE_MASK) {
631 case PCI_PREF_RANGE_TYPE_32:
632 base = (base & 0xfff0) << 16;
633 limit = ((limit & 0xfff0) << 16) | 0xfffff;
634 bridge->p_mem_head = acpiphp_make_resource((u64)base, limit - base + 1);
635 if (!bridge->p_mem_head) {
636 err("out of memory\n");
640 dbg("32bit Prefetchable memory range: %08x-%08x\n",
641 (u32)bridge->p_mem_head->base,
642 (u32)(bridge->p_mem_head->base + bridge->p_mem_head->length - 1));
644 case PCI_PREF_RANGE_TYPE_64:
645 pci_read_config_dword(bridge->pci_dev, PCI_PREF_BASE_UPPER32, &base32u);
646 pci_read_config_dword(bridge->pci_dev, PCI_PREF_LIMIT_UPPER32, &limit32u);
647 base64 = ((u64)base32u << 32) | ((base & 0xfff0) << 16);
648 limit64 = (((u64)limit32u << 32) | ((limit & 0xfff0) << 16)) + 0xfffff;
650 bridge->p_mem_head = acpiphp_make_resource(base64, limit64 - base64 + 1);
651 if (!bridge->p_mem_head) {
652 err("out of memory\n");
656 dbg("64bit Prefetchable memory range: %08x%08x-%08x%08x\n",
657 (u32)(bridge->p_mem_head->base >> 32),
658 (u32)(bridge->p_mem_head->base & 0xffffffff),
659 (u32)((bridge->p_mem_head->base + bridge->p_mem_head->length - 1) >> 32),
660 (u32)((bridge->p_mem_head->base + bridge->p_mem_head->length - 1) & 0xffffffff));
665 warn("Unknown prefetchale memory type\n");
668 init_bridge_misc(bridge);
672 /* callback routine to find P2P bridges */
674 find_p2p_bridge (acpi_handle handle, u32 lvl, void *context, void **rv)
677 acpi_handle dummy_handle;
678 unsigned long *segbus = context;
680 int seg, bus, device, function;
683 /* get PCI address */
684 seg = (*segbus >> 8) & 0xff;
685 bus = *segbus & 0xff;
687 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
688 if (ACPI_FAILURE(status))
689 return AE_OK; /* continue */
691 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
692 if (ACPI_FAILURE(status)) {
693 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
697 device = (tmp >> 16) & 0xffff;
698 function = tmp & 0xffff;
700 dev = pci_find_slot(bus, PCI_DEVFN(device, function));
705 if (!dev->subordinate)
708 /* check if this bridge has ejectable slots */
709 if (detect_ejectable_slots(handle) > 0) {
710 dbg("found PCI-to-PCI bridge at PCI %s\n", dev->slot_name);
711 add_p2p_bridge(handle, seg, bus, device, function);
718 /* find hot-pluggable slots, and then find P2P bridge */
719 static int add_bridge (acpi_handle handle)
724 acpi_handle dummy_handle;
726 /* if the bridge doesn't have _STA, we assume it is always there */
727 status = acpi_get_handle(handle, "_STA", &dummy_handle);
728 if (ACPI_SUCCESS(status)) {
729 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
730 if (ACPI_FAILURE(status)) {
731 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
734 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
735 /* don't register this object */
739 /* get PCI segment number */
740 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
742 seg = ACPI_SUCCESS(status) ? tmp : 0;
744 /* get PCI bus number */
745 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
747 if (ACPI_SUCCESS(status)) {
750 warn("can't get bus number, assuming 0\n");
754 /* check if this bridge has ejectable slots */
755 if (detect_ejectable_slots(handle) > 0) {
756 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
757 add_host_bridge(handle, seg, bus);
761 tmp = seg << 8 | bus;
763 /* search P2P bridges under this host bridge */
764 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
765 find_p2p_bridge, &tmp, NULL);
767 if (ACPI_FAILURE(status))
768 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
774 static void remove_bridge (acpi_handle handle)
776 /* No-op for now .. */
779 static int power_on_slot (struct acpiphp_slot *slot)
782 struct acpiphp_func *func;
786 /* if already enabled, just skip */
787 if (slot->flags & SLOT_POWEREDON)
790 list_for_each (l, &slot->funcs) {
791 func = list_entry(l, struct acpiphp_func, sibling);
793 if (func->flags & FUNC_HAS_PS0) {
794 dbg("%s: executing _PS0\n", __FUNCTION__);
795 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
796 if (ACPI_FAILURE(status)) {
797 warn("%s: _PS0 failed\n", __FUNCTION__);
805 /* TBD: evaluate _STA to check if the slot is enabled */
807 slot->flags |= SLOT_POWEREDON;
814 static int power_off_slot (struct acpiphp_slot *slot)
817 struct acpiphp_func *func;
819 struct acpi_object_list arg_list;
820 union acpi_object arg;
824 /* if already disabled, just skip */
825 if ((slot->flags & SLOT_POWEREDON) == 0)
828 list_for_each (l, &slot->funcs) {
829 func = list_entry(l, struct acpiphp_func, sibling);
831 if (func->pci_dev && (func->flags & FUNC_HAS_PS3)) {
832 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
833 if (ACPI_FAILURE(status)) {
834 warn("%s: _PS3 failed\n", __FUNCTION__);
842 list_for_each (l, &slot->funcs) {
843 func = list_entry(l, struct acpiphp_func, sibling);
845 /* We don't want to call _EJ0 on non-existing functions. */
846 if (func->pci_dev && (func->flags & FUNC_HAS_EJ0)) {
847 /* _EJ0 method take one argument */
849 arg_list.pointer = &arg;
850 arg.type = ACPI_TYPE_INTEGER;
851 arg.integer.value = 1;
852 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
853 if (ACPI_FAILURE(status)) {
854 warn("%s: _EJ0 failed\n", __FUNCTION__);
862 /* TBD: evaluate _STA to check if the slot is disabled */
864 slot->flags &= (~SLOT_POWEREDON);
872 * enable_device - enable, configure a slot
873 * @slot: slot to be enabled
875 * This function should be called per *physical slot*,
876 * not per each slot object in ACPI namespace.
879 static int enable_device (struct acpiphp_slot *slot)
882 struct pci_dev dev0, *dev;
883 struct pci_bus *child;
885 struct acpiphp_func *func;
888 if (slot->flags & SLOT_ENABLED)
891 /* sanity check: dev should be NULL when hot-plugged in */
892 dev = pci_find_slot(slot->bridge->bus, PCI_DEVFN(slot->device, 0));
894 /* This case shouldn't happen */
895 err("pci_dev structure already exists.\n");
900 /* allocate resources to device */
901 retval = acpiphp_configure_slot(slot);
905 memset(&dev0, 0, sizeof (struct pci_dev));
907 dev0.bus = slot->bridge->pci_bus;
908 dev0.devfn = PCI_DEVFN(slot->device, 0);
909 dev0.sysdata = dev0.bus->sysdata;
911 /* returned `dev' is the *first function* only! */
912 dev = pci_scan_slot (&dev0);
915 err("No new device found\n");
920 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
921 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &bus);
922 child = (struct pci_bus*) pci_add_new_bus(dev->bus, dev, bus);
923 pci_do_scan_bus(child);
926 /* associate pci_dev to our representation */
927 list_for_each (l, &slot->funcs) {
928 func = list_entry(l, struct acpiphp_func, sibling);
930 func->pci_dev = pci_find_slot(slot->bridge->bus,
931 PCI_DEVFN(slot->device,
936 /* configure device */
937 retval = acpiphp_configure_function(func);
942 slot->flags |= SLOT_ENABLED;
944 dbg("Available resources:\n");
945 acpiphp_dump_resource(slot->bridge);
953 * disable_device - disable a slot
955 static int disable_device (struct acpiphp_slot *slot)
958 struct acpiphp_func *func;
961 /* is this slot already disabled? */
962 if (!(slot->flags & SLOT_ENABLED))
965 list_for_each (l, &slot->funcs) {
966 func = list_entry(l, struct acpiphp_func, sibling);
969 if (acpiphp_unconfigure_function(func)) {
970 err("failed to unconfigure device\n");
976 slot->flags &= (~SLOT_ENABLED);
984 * get_slot_status - get ACPI slot status
986 * if a slot has _STA for each function and if any one of them
987 * returned non-zero status, return it
989 * if a slot doesn't have _STA and if any one of its functions'
990 * configuration space is configured, return 0x0f as a _STA
994 static unsigned int get_slot_status (struct acpiphp_slot *slot)
997 unsigned long sta = 0;
1000 struct acpiphp_func *func;
1002 list_for_each (l, &slot->funcs) {
1003 func = list_entry(l, struct acpiphp_func, sibling);
1005 if (func->flags & FUNC_HAS_STA) {
1006 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1007 if (ACPI_SUCCESS(status) && sta)
1010 pci_bus_read_config_dword(slot->bridge->pci_bus,
1011 PCI_DEVFN(slot->device, func->function),
1012 PCI_VENDOR_ID, &dvid);
1013 if (dvid != 0xffffffff) {
1020 return (unsigned int)sta;
1025 * ACPI event handlers
1029 * handle_hotplug_event_bridge - handle ACPI event on bridges
1031 * @handle: Notify()'ed acpi_handle
1032 * @type: Notify code
1033 * @context: pointer to acpiphp_bridge structure
1035 * handles ACPI event notification on {host,p2p} bridges
1038 static void handle_hotplug_event_bridge (acpi_handle handle, u32 type, void *context)
1040 struct acpiphp_bridge *bridge;
1042 struct acpi_buffer buffer = { .length = sizeof(objname),
1043 .pointer = objname };
1045 bridge = (struct acpiphp_bridge *)context;
1047 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1050 case ACPI_NOTIFY_BUS_CHECK:
1051 /* bus re-enumerate */
1052 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1053 acpiphp_check_bridge(bridge);
1056 case ACPI_NOTIFY_DEVICE_CHECK:
1058 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1059 acpiphp_check_bridge(bridge);
1062 case ACPI_NOTIFY_DEVICE_WAKE:
1064 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1067 case ACPI_NOTIFY_EJECT_REQUEST:
1068 /* request device eject */
1069 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1073 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1080 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1082 * @handle: Notify()'ed acpi_handle
1083 * @type: Notify code
1084 * @context: pointer to acpiphp_func structure
1086 * handles ACPI event notification on slots
1089 static void handle_hotplug_event_func (acpi_handle handle, u32 type, void *context)
1091 struct acpiphp_func *func;
1093 struct acpi_buffer buffer = { .length = sizeof(objname),
1094 .pointer = objname };
1096 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1098 func = (struct acpiphp_func *)context;
1101 case ACPI_NOTIFY_BUS_CHECK:
1102 /* bus re-enumerate */
1103 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1104 acpiphp_enable_slot(func->slot);
1107 case ACPI_NOTIFY_DEVICE_CHECK:
1108 /* device check : re-enumerate from parent bus */
1109 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1110 acpiphp_check_bridge(func->slot->bridge);
1113 case ACPI_NOTIFY_DEVICE_WAKE:
1115 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1118 case ACPI_NOTIFY_EJECT_REQUEST:
1119 /* request device eject */
1120 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1121 acpiphp_disable_slot(func->slot);
1125 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1131 static struct acpi_pci_driver acpi_pci_hp_driver = {
1133 .remove = remove_bridge,
1137 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1140 int acpiphp_glue_init (void)
1144 if (list_empty(&pci_root_buses))
1147 num = acpi_pci_register_driver(&acpi_pci_hp_driver);
1157 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1159 * This function frees all data allocated in acpiphp_glue_init()
1161 void acpiphp_glue_exit (void)
1163 struct list_head *l1, *l2, *n1, *n2;
1164 struct acpiphp_bridge *bridge;
1165 struct acpiphp_slot *slot, *next;
1166 struct acpiphp_func *func;
1169 list_for_each_safe (l1, n1, &bridge_list) {
1170 bridge = (struct acpiphp_bridge *)l1;
1171 slot = bridge->slots;
1174 list_for_each_safe (l2, n2, &slot->funcs) {
1175 func = list_entry(l2, struct acpiphp_func, sibling);
1176 acpiphp_free_resource(&func->io_head);
1177 acpiphp_free_resource(&func->mem_head);
1178 acpiphp_free_resource(&func->p_mem_head);
1179 acpiphp_free_resource(&func->bus_head);
1180 status = acpi_remove_notify_handler(func->handle,
1182 handle_hotplug_event_func);
1183 if (ACPI_FAILURE(status))
1184 err("failed to remove notify handler\n");
1190 status = acpi_remove_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
1191 handle_hotplug_event_bridge);
1192 if (ACPI_FAILURE(status))
1193 err("failed to remove notify handler\n");
1195 acpiphp_free_resource(&bridge->io_head);
1196 acpiphp_free_resource(&bridge->mem_head);
1197 acpiphp_free_resource(&bridge->p_mem_head);
1198 acpiphp_free_resource(&bridge->bus_head);
1206 * acpiphp_get_num_slots - count number of slots in a system
1208 int acpiphp_get_num_slots (void)
1210 struct list_head *node;
1211 struct acpiphp_bridge *bridge;
1216 list_for_each (node, &bridge_list) {
1217 bridge = (struct acpiphp_bridge *)node;
1218 dbg("Bus%d %dslot(s)\n", bridge->bus, bridge->nr_slots);
1219 num_slots += bridge->nr_slots;
1222 dbg("Total %dslots\n", num_slots);
1228 * acpiphp_for_each_slot - call function for each slot
1229 * @fn: callback function
1230 * @data: context to be passed to callback function
1233 int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1235 struct list_head *node;
1236 struct acpiphp_bridge *bridge;
1237 struct acpiphp_slot *slot;
1240 list_for_each (node, &bridge_list) {
1241 bridge = (struct acpiphp_bridge *)node;
1242 for (slot = bridge->slots; slot; slot = slot->next) {
1243 retval = fn(slot, data);
1254 /* search matching slot from id */
1255 struct acpiphp_slot *get_slot_from_id (int id)
1257 struct list_head *node;
1258 struct acpiphp_bridge *bridge;
1259 struct acpiphp_slot *slot;
1261 list_for_each (node, &bridge_list) {
1262 bridge = (struct acpiphp_bridge *)node;
1263 for (slot = bridge->slots; slot; slot = slot->next)
1268 /* should never happen! */
1269 err("%s: no object for id %d\n",__FUNCTION__, id);
1275 * acpiphp_enable_slot - power on slot
1277 int acpiphp_enable_slot (struct acpiphp_slot *slot)
1281 down(&slot->crit_sect);
1283 /* wake up all functions */
1284 retval = power_on_slot(slot);
1288 if (get_slot_status(slot) == ACPI_STA_ALL)
1289 /* configure all functions */
1290 retval = enable_device(slot);
1293 up(&slot->crit_sect);
1299 * acpiphp_disable_slot - power off slot
1301 int acpiphp_disable_slot (struct acpiphp_slot *slot)
1305 down(&slot->crit_sect);
1307 /* unconfigure all functions */
1308 retval = disable_device(slot);
1312 /* power off all functions */
1313 retval = power_off_slot(slot);
1317 acpiphp_resource_sort_and_combine(&slot->bridge->io_head);
1318 acpiphp_resource_sort_and_combine(&slot->bridge->mem_head);
1319 acpiphp_resource_sort_and_combine(&slot->bridge->p_mem_head);
1320 acpiphp_resource_sort_and_combine(&slot->bridge->bus_head);
1321 dbg("Available resources:\n");
1322 acpiphp_dump_resource(slot->bridge);
1325 up(&slot->crit_sect);
1331 * acpiphp_check_bridge - re-enumerate devices
1333 int acpiphp_check_bridge (struct acpiphp_bridge *bridge)
1335 struct acpiphp_slot *slot;
1338 int enabled, disabled;
1340 enabled = disabled = 0;
1342 for (slot = bridge->slots; slot; slot = slot->next) {
1343 sta = get_slot_status(slot);
1344 if (slot->flags & SLOT_ENABLED) {
1345 /* if enabled but not present, disable */
1346 if (sta != ACPI_STA_ALL) {
1347 retval = acpiphp_disable_slot(slot);
1349 err("Error occurred in enabling\n");
1350 up(&slot->crit_sect);
1356 /* if disabled but present, enable */
1357 if (sta == ACPI_STA_ALL) {
1358 retval = acpiphp_enable_slot(slot);
1360 err("Error occurred in enabling\n");
1361 up(&slot->crit_sect);
1369 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1380 u8 acpiphp_get_power_status (struct acpiphp_slot *slot)
1384 sta = get_slot_status(slot);
1386 return (sta & ACPI_STA_ENABLED) ? 1 : 0;
1391 * attention LED ON: 1
1395 * no direct attention led status information via ACPI
1398 u8 acpiphp_get_attention_status (struct acpiphp_slot *slot)
1408 u8 acpiphp_get_latch_status (struct acpiphp_slot *slot)
1412 sta = get_slot_status(slot);
1414 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1419 * adapter presence : 1
1422 u8 acpiphp_get_adapter_status (struct acpiphp_slot *slot)
1426 sta = get_slot_status(slot);
1428 return (sta == 0) ? 0 : 1;
1433 * pci address (seg/bus/dev)
1435 u32 acpiphp_get_address (struct acpiphp_slot *slot)
1439 address = ((slot->bridge->seg) << 16) |
1440 ((slot->bridge->bus) << 8) |