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/config.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/pci.h>
33 #include <linux/smp_lock.h>
34 #include <linux/init.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, address.min_address_range, address.max_address_range);
249 res = acpiphp_make_resource(address.min_address_range,
250 address.address_length);
252 err("out of memory\n");
256 switch (address.resource_type) {
257 case ACPI_MEMORY_RANGE:
258 if (address.attribute.memory.cache_attribute == ACPI_PREFETCHABLE_MEMORY) {
259 res->next = bridge->p_mem_head;
260 bridge->p_mem_head = res;
262 res->next = bridge->mem_head;
263 bridge->mem_head = res;
267 res->next = bridge->io_head;
268 bridge->io_head = res;
270 case ACPI_BUS_NUMBER_RANGE:
271 res->next = bridge->bus_head;
272 bridge->bus_head = res;
284 /* decode ACPI 2.0 _HPP hot plug parameters */
285 static void decode_hpp(struct acpiphp_bridge *bridge)
288 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
290 union acpi_object *package;
293 /* default numbers */
294 bridge->hpp.cache_line_size = 0x10;
295 bridge->hpp.latency_timer = 0x40;
296 bridge->hpp.enable_SERR = 0;
297 bridge->hpp.enable_PERR = 0;
299 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
301 if (ACPI_FAILURE(status)) {
302 dbg("_HPP evaluation failed\n");
306 package = (union acpi_object *) buffer.pointer;
308 if (!package || package->type != ACPI_TYPE_PACKAGE ||
309 package->package.count != 4 || !package->package.elements) {
310 err("invalid _HPP object; ignoring\n");
314 for (i = 0; i < 4; i++) {
315 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
316 err("invalid _HPP parameter type; ignoring\n");
321 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
322 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
323 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
324 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
326 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
327 bridge->hpp.cache_line_size,
328 bridge->hpp.latency_timer,
329 bridge->hpp.enable_SERR,
330 bridge->hpp.enable_PERR);
332 bridge->flags |= BRIDGE_HAS_HPP;
335 kfree(buffer.pointer);
339 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
340 static void init_bridge_misc (struct acpiphp_bridge *bridge)
344 /* decode ACPI 2.0 _HPP (hot plug parameters) */
347 /* subtract all resources already allocated */
348 acpiphp_detect_pci_resource(bridge);
350 /* register all slot objects under this bridge */
351 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
352 register_slot, bridge, NULL);
354 /* install notify handler */
355 status = acpi_install_notify_handler(bridge->handle,
357 handle_hotplug_event_bridge,
360 if (ACPI_FAILURE(status)) {
361 err("failed to register interrupt notify handler\n");
364 list_add(&bridge->list, &bridge_list);
366 dbg("Bridge resource:\n");
367 acpiphp_dump_resource(bridge);
371 /* allocate and initialize host bridge data structure */
372 static void add_host_bridge (acpi_handle *handle, int seg, int bus)
375 struct acpiphp_bridge *bridge;
377 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
381 memset(bridge, 0, sizeof(struct acpiphp_bridge));
383 bridge->type = BRIDGE_TYPE_HOST;
384 bridge->handle = handle;
388 bridge->pci_bus = pci_find_bus(seg, bus);
390 bridge->res_lock = SPIN_LOCK_UNLOCKED;
392 /* to be overridden when we decode _CRS */
393 bridge->sub = bridge->bus;
395 /* decode resources */
397 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
398 decode_acpi_resource, bridge);
400 if (ACPI_FAILURE(status)) {
401 err("failed to decode bridge resources\n");
406 acpiphp_resource_sort_and_combine(&bridge->io_head);
407 acpiphp_resource_sort_and_combine(&bridge->mem_head);
408 acpiphp_resource_sort_and_combine(&bridge->p_mem_head);
409 acpiphp_resource_sort_and_combine(&bridge->bus_head);
411 dbg("ACPI _CRS resource:\n");
412 acpiphp_dump_resource(bridge);
414 if (bridge->bus_head) {
415 bridge->bus = bridge->bus_head->base;
416 bridge->sub = bridge->bus_head->base + bridge->bus_head->length - 1;
419 init_bridge_misc(bridge);
423 /* allocate and initialize PCI-to-PCI bridge data structure */
424 static void add_p2p_bridge (acpi_handle *handle, int seg, int bus, int dev, int fn)
426 struct acpiphp_bridge *bridge;
430 u32 base, limit, base32u, limit32u;
432 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
433 if (bridge == NULL) {
434 err("out of memory\n");
438 memset(bridge, 0, sizeof(struct acpiphp_bridge));
440 bridge->type = BRIDGE_TYPE_P2P;
441 bridge->handle = handle;
444 bridge->pci_dev = pci_find_slot(bus, PCI_DEVFN(dev, fn));
445 if (!bridge->pci_dev) {
446 err("Can't get pci_dev\n");
451 bridge->pci_bus = bridge->pci_dev->subordinate;
452 if (!bridge->pci_bus) {
453 err("This is not a PCI-to-PCI bridge!\n");
458 bridge->res_lock = SPIN_LOCK_UNLOCKED;
460 bridge->bus = bridge->pci_bus->number;
461 bridge->sub = bridge->pci_bus->subordinate;
464 * decode resources under this P2P bridge
468 pci_read_config_byte(bridge->pci_dev, PCI_IO_BASE, &tmp8);
470 pci_read_config_byte(bridge->pci_dev, PCI_IO_LIMIT, &tmp8);
473 switch (base & PCI_IO_RANGE_TYPE_MASK) {
474 case PCI_IO_RANGE_TYPE_16:
475 base = (base << 8) & 0xf000;
476 limit = ((limit << 8) & 0xf000) + 0xfff;
477 bridge->io_head = acpiphp_make_resource((u64)base, limit - base + 1);
478 if (!bridge->io_head) {
479 err("out of memory\n");
483 dbg("16bit I/O range: %04x-%04x\n",
484 (u32)bridge->io_head->base,
485 (u32)(bridge->io_head->base + bridge->io_head->length - 1));
487 case PCI_IO_RANGE_TYPE_32:
488 pci_read_config_word(bridge->pci_dev, PCI_IO_BASE_UPPER16, &tmp16);
489 base = ((u32)tmp16 << 16) | ((base << 8) & 0xf000);
490 pci_read_config_word(bridge->pci_dev, PCI_IO_LIMIT_UPPER16, &tmp16);
491 limit = (((u32)tmp16 << 16) | ((limit << 8) & 0xf000)) + 0xfff;
492 bridge->io_head = acpiphp_make_resource((u64)base, limit - base + 1);
493 if (!bridge->io_head) {
494 err("out of memory\n");
498 dbg("32bit I/O range: %08x-%08x\n",
499 (u32)bridge->io_head->base,
500 (u32)(bridge->io_head->base + bridge->io_head->length - 1));
503 dbg("I/O space unsupported\n");
506 warn("Unknown I/O range type\n");
509 /* Memory resources (mandatory for P2P bridge) */
510 pci_read_config_word(bridge->pci_dev, PCI_MEMORY_BASE, &tmp16);
511 base = (tmp16 & 0xfff0) << 16;
512 pci_read_config_word(bridge->pci_dev, PCI_MEMORY_LIMIT, &tmp16);
513 limit = ((tmp16 & 0xfff0) << 16) | 0xfffff;
514 bridge->mem_head = acpiphp_make_resource((u64)base, limit - base + 1);
515 if (!bridge->mem_head) {
516 err("out of memory\n");
520 dbg("32bit Memory range: %08x-%08x\n",
521 (u32)bridge->mem_head->base,
522 (u32)(bridge->mem_head->base + bridge->mem_head->length-1));
524 /* Prefetchable Memory resources (optional) */
525 pci_read_config_word(bridge->pci_dev, PCI_PREF_MEMORY_BASE, &tmp16);
527 pci_read_config_word(bridge->pci_dev, PCI_PREF_MEMORY_LIMIT, &tmp16);
530 switch (base & PCI_MEMORY_RANGE_TYPE_MASK) {
531 case PCI_PREF_RANGE_TYPE_32:
532 base = (base & 0xfff0) << 16;
533 limit = ((limit & 0xfff0) << 16) | 0xfffff;
534 bridge->p_mem_head = acpiphp_make_resource((u64)base, limit - base + 1);
535 if (!bridge->p_mem_head) {
536 err("out of memory\n");
540 dbg("32bit Prefetchable memory range: %08x-%08x\n",
541 (u32)bridge->p_mem_head->base,
542 (u32)(bridge->p_mem_head->base + bridge->p_mem_head->length - 1));
544 case PCI_PREF_RANGE_TYPE_64:
545 pci_read_config_dword(bridge->pci_dev, PCI_PREF_BASE_UPPER32, &base32u);
546 pci_read_config_dword(bridge->pci_dev, PCI_PREF_LIMIT_UPPER32, &limit32u);
547 base64 = ((u64)base32u << 32) | ((base & 0xfff0) << 16);
548 limit64 = (((u64)limit32u << 32) | ((limit & 0xfff0) << 16)) + 0xfffff;
550 bridge->p_mem_head = acpiphp_make_resource(base64, limit64 - base64 + 1);
551 if (!bridge->p_mem_head) {
552 err("out of memory\n");
556 dbg("64bit Prefetchable memory range: %08x%08x-%08x%08x\n",
557 (u32)(bridge->p_mem_head->base >> 32),
558 (u32)(bridge->p_mem_head->base & 0xffffffff),
559 (u32)((bridge->p_mem_head->base + bridge->p_mem_head->length - 1) >> 32),
560 (u32)((bridge->p_mem_head->base + bridge->p_mem_head->length - 1) & 0xffffffff));
565 warn("Unknown prefetchale memory type\n");
568 init_bridge_misc(bridge);
572 /* callback routine to find P2P bridges */
574 find_p2p_bridge (acpi_handle handle, u32 lvl, void *context, void **rv)
577 acpi_handle dummy_handle;
578 unsigned long *segbus = context;
580 int seg, bus, device, function;
583 /* get PCI address */
584 seg = (*segbus >> 8) & 0xff;
585 bus = *segbus & 0xff;
587 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
588 if (ACPI_FAILURE(status))
589 return AE_OK; /* continue */
591 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
592 if (ACPI_FAILURE(status)) {
593 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
597 device = (tmp >> 16) & 0xffff;
598 function = tmp & 0xffff;
600 dev = pci_find_slot(bus, PCI_DEVFN(device, function));
605 if (!dev->subordinate)
608 /* check if this bridge has ejectable slots */
609 if (detect_ejectable_slots(handle) > 0) {
610 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
611 add_p2p_bridge(handle, seg, bus, device, function);
618 /* find hot-pluggable slots, and then find P2P bridge */
619 static int add_bridge(acpi_handle handle)
624 acpi_handle dummy_handle;
626 /* if the bridge doesn't have _STA, we assume it is always there */
627 status = acpi_get_handle(handle, "_STA", &dummy_handle);
628 if (ACPI_SUCCESS(status)) {
629 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
630 if (ACPI_FAILURE(status)) {
631 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
634 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
635 /* don't register this object */
639 /* get PCI segment number */
640 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
642 seg = ACPI_SUCCESS(status) ? tmp : 0;
644 /* get PCI bus number */
645 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
647 if (ACPI_SUCCESS(status)) {
650 warn("can't get bus number, assuming 0\n");
654 /* check if this bridge has ejectable slots */
655 if (detect_ejectable_slots(handle) > 0) {
656 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
657 add_host_bridge(handle, seg, bus);
661 tmp = seg << 8 | bus;
663 /* search P2P bridges under this host bridge */
664 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
665 find_p2p_bridge, &tmp, NULL);
667 if (ACPI_FAILURE(status))
668 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
674 static void remove_bridge (acpi_handle handle)
676 /* No-op for now .. */
680 static int power_on_slot (struct acpiphp_slot *slot)
683 struct acpiphp_func *func;
687 /* is this already enabled? */
688 if (slot->flags & SLOT_POWEREDON)
691 list_for_each (l, &slot->funcs) {
692 func = list_entry(l, struct acpiphp_func, sibling);
694 if (func->flags & FUNC_HAS_PS0) {
695 dbg("%s: executing _PS0 on %s\n", __FUNCTION__,
696 pci_name(func->pci_dev));
697 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
698 if (ACPI_FAILURE(status)) {
699 warn("%s: _PS0 failed\n", __FUNCTION__);
706 /* TBD: evaluate _STA to check if the slot is enabled */
708 slot->flags |= SLOT_POWEREDON;
715 static int power_off_slot (struct acpiphp_slot *slot)
718 struct acpiphp_func *func;
720 struct acpi_object_list arg_list;
721 union acpi_object arg;
725 /* is this already enabled? */
726 if ((slot->flags & SLOT_POWEREDON) == 0)
729 list_for_each (l, &slot->funcs) {
730 func = list_entry(l, struct acpiphp_func, sibling);
732 if (func->flags & (FUNC_HAS_PS3 | FUNC_EXISTS)) {
733 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
734 if (ACPI_FAILURE(status)) {
735 warn("%s: _PS3 failed\n", __FUNCTION__);
742 list_for_each (l, &slot->funcs) {
743 func = list_entry(l, struct acpiphp_func, sibling);
745 /* We don't want to call _EJ0 on non-existing functions. */
746 if (func->flags & (FUNC_HAS_EJ0 | FUNC_EXISTS)) {
747 /* _EJ0 method take one argument */
749 arg_list.pointer = &arg;
750 arg.type = ACPI_TYPE_INTEGER;
751 arg.integer.value = 1;
753 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
754 if (ACPI_FAILURE(status)) {
755 warn("%s: _EJ0 failed\n", __FUNCTION__);
759 func->flags &= (~FUNC_EXISTS);
763 /* TBD: evaluate _STA to check if the slot is disabled */
765 slot->flags &= (~SLOT_POWEREDON);
773 * enable_device - enable, configure a slot
774 * @slot: slot to be enabled
776 * This function should be called per *physical slot*,
777 * not per each slot object in ACPI namespace.
780 static int enable_device (struct acpiphp_slot *slot)
784 struct pci_bus *child;
786 struct acpiphp_func *func;
790 if (slot->flags & SLOT_ENABLED)
793 /* sanity check: dev should be NULL when hot-plugged in */
794 dev = pci_find_slot(slot->bridge->bus, PCI_DEVFN(slot->device, 0));
796 /* This case shouldn't happen */
797 err("pci_dev structure already exists.\n");
802 /* allocate resources to device */
803 retval = acpiphp_configure_slot(slot);
807 /* returned `dev' is the *first function* only! */
808 num = pci_scan_slot(slot->bridge->pci_bus, PCI_DEVFN(slot->device, 0));
810 pci_bus_add_devices(slot->bridge->pci_bus);
811 dev = pci_find_slot(slot->bridge->bus, PCI_DEVFN(slot->device, 0));
814 err("No new device found\n");
819 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
820 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &bus);
821 child = (struct pci_bus*) pci_add_new_bus(dev->bus, dev, bus);
822 pci_do_scan_bus(child);
825 /* associate pci_dev to our representation */
826 list_for_each (l, &slot->funcs) {
827 func = list_entry(l, struct acpiphp_func, sibling);
829 func->pci_dev = pci_find_slot(slot->bridge->bus,
830 PCI_DEVFN(slot->device,
835 /* configure device */
836 retval = acpiphp_configure_function(func);
840 func->flags |= FUNC_EXISTS;
843 slot->flags |= SLOT_ENABLED;
845 dbg("Available resources:\n");
846 acpiphp_dump_resource(slot->bridge);
854 * disable_device - disable a slot
856 static int disable_device (struct acpiphp_slot *slot)
859 struct acpiphp_func *func;
862 /* is this slot already disabled? */
863 if (!(slot->flags & SLOT_ENABLED))
866 list_for_each (l, &slot->funcs) {
867 func = list_entry(l, struct acpiphp_func, sibling);
870 if (acpiphp_unconfigure_function(func) == 0) {
871 func->pci_dev = NULL;
873 err("failed to unconfigure device\n");
880 slot->flags &= (~SLOT_ENABLED);
888 * get_slot_status - get ACPI slot status
890 * if a slot has _STA for each function and if any one of them
891 * returned non-zero status, return it
893 * if a slot doesn't have _STA and if any one of its functions'
894 * configuration space is configured, return 0x0f as a _STA
898 static unsigned int get_slot_status (struct acpiphp_slot *slot)
901 unsigned long sta = 0;
904 struct acpiphp_func *func;
906 list_for_each (l, &slot->funcs) {
907 func = list_entry(l, struct acpiphp_func, sibling);
909 if (func->flags & FUNC_HAS_STA) {
910 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
911 if (ACPI_SUCCESS(status) && sta)
914 pci_bus_read_config_dword(slot->bridge->pci_bus,
915 PCI_DEVFN(slot->device,
917 PCI_VENDOR_ID, &dvid);
918 if (dvid != 0xffffffff) {
925 return (unsigned int)sta;
930 * ACPI event handlers
934 * handle_hotplug_event_bridge - handle ACPI event on bridges
936 * @handle: Notify()'ed acpi_handle
938 * @context: pointer to acpiphp_bridge structure
940 * handles ACPI event notification on {host,p2p} bridges
943 static void handle_hotplug_event_bridge (acpi_handle handle, u32 type, void *context)
945 struct acpiphp_bridge *bridge;
947 struct acpi_buffer buffer = { .length = sizeof(objname),
948 .pointer = objname };
950 bridge = (struct acpiphp_bridge *)context;
952 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
955 case ACPI_NOTIFY_BUS_CHECK:
956 /* bus re-enumerate */
957 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
958 acpiphp_check_bridge(bridge);
961 case ACPI_NOTIFY_DEVICE_CHECK:
963 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
964 acpiphp_check_bridge(bridge);
967 case ACPI_NOTIFY_DEVICE_WAKE:
969 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
972 case ACPI_NOTIFY_EJECT_REQUEST:
973 /* request device eject */
974 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
977 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
978 printk(KERN_ERR "Device %s cannot be configured due"
979 " to a frequency mismatch\n", objname);
982 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
983 printk(KERN_ERR "Device %s cannot be configured due"
984 " to a bus mode mismatch\n", objname);
987 case ACPI_NOTIFY_POWER_FAULT:
988 printk(KERN_ERR "Device %s has suffered a power fault\n",
993 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1000 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1002 * @handle: Notify()'ed acpi_handle
1003 * @type: Notify code
1004 * @context: pointer to acpiphp_func structure
1006 * handles ACPI event notification on slots
1009 static void handle_hotplug_event_func (acpi_handle handle, u32 type, void *context)
1011 struct acpiphp_func *func;
1013 struct acpi_buffer buffer = { .length = sizeof(objname),
1014 .pointer = objname };
1016 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1018 func = (struct acpiphp_func *)context;
1021 case ACPI_NOTIFY_BUS_CHECK:
1022 /* bus re-enumerate */
1023 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1024 acpiphp_enable_slot(func->slot);
1027 case ACPI_NOTIFY_DEVICE_CHECK:
1028 /* device check : re-enumerate from parent bus */
1029 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1030 acpiphp_check_bridge(func->slot->bridge);
1033 case ACPI_NOTIFY_DEVICE_WAKE:
1035 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1038 case ACPI_NOTIFY_EJECT_REQUEST:
1039 /* request device eject */
1040 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1041 acpiphp_disable_slot(func->slot);
1045 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1051 static struct acpi_pci_driver acpi_pci_hp_driver = {
1053 .remove = remove_bridge,
1057 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1060 int acpiphp_glue_init (void)
1064 if (list_empty(&pci_root_buses))
1067 num = acpi_pci_register_driver(&acpi_pci_hp_driver);
1077 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1079 * This function frees all data allocated in acpiphp_glue_init()
1081 void acpiphp_glue_exit (void)
1083 struct list_head *l1, *l2, *n1, *n2;
1084 struct acpiphp_bridge *bridge;
1085 struct acpiphp_slot *slot, *next;
1086 struct acpiphp_func *func;
1089 list_for_each_safe (l1, n1, &bridge_list) {
1090 bridge = (struct acpiphp_bridge *)l1;
1091 slot = bridge->slots;
1094 list_for_each_safe (l2, n2, &slot->funcs) {
1095 func = list_entry(l2, struct acpiphp_func, sibling);
1096 acpiphp_free_resource(&func->io_head);
1097 acpiphp_free_resource(&func->mem_head);
1098 acpiphp_free_resource(&func->p_mem_head);
1099 acpiphp_free_resource(&func->bus_head);
1100 status = acpi_remove_notify_handler(func->handle,
1102 handle_hotplug_event_func);
1103 if (ACPI_FAILURE(status))
1104 err("failed to remove notify handler\n");
1110 status = acpi_remove_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
1111 handle_hotplug_event_bridge);
1112 if (ACPI_FAILURE(status))
1113 err("failed to remove notify handler\n");
1115 acpiphp_free_resource(&bridge->io_head);
1116 acpiphp_free_resource(&bridge->mem_head);
1117 acpiphp_free_resource(&bridge->p_mem_head);
1118 acpiphp_free_resource(&bridge->bus_head);
1126 * acpiphp_get_num_slots - count number of slots in a system
1128 int acpiphp_get_num_slots (void)
1130 struct list_head *node;
1131 struct acpiphp_bridge *bridge;
1136 list_for_each (node, &bridge_list) {
1137 bridge = (struct acpiphp_bridge *)node;
1138 dbg("Bus%d %dslot(s)\n", bridge->bus, bridge->nr_slots);
1139 num_slots += bridge->nr_slots;
1142 dbg("Total %dslots\n", num_slots);
1148 * acpiphp_for_each_slot - call function for each slot
1149 * @fn: callback function
1150 * @data: context to be passed to callback function
1153 int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1155 struct list_head *node;
1156 struct acpiphp_bridge *bridge;
1157 struct acpiphp_slot *slot;
1160 list_for_each (node, &bridge_list) {
1161 bridge = (struct acpiphp_bridge *)node;
1162 for (slot = bridge->slots; slot; slot = slot->next) {
1163 retval = fn(slot, data);
1174 /* search matching slot from id */
1175 struct acpiphp_slot *get_slot_from_id (int id)
1177 struct list_head *node;
1178 struct acpiphp_bridge *bridge;
1179 struct acpiphp_slot *slot;
1181 list_for_each (node, &bridge_list) {
1182 bridge = (struct acpiphp_bridge *)node;
1183 for (slot = bridge->slots; slot; slot = slot->next)
1188 /* should never happen! */
1189 err("%s: no object for id %d\n",__FUNCTION__, id);
1195 * acpiphp_enable_slot - power on slot
1197 int acpiphp_enable_slot (struct acpiphp_slot *slot)
1201 down(&slot->crit_sect);
1203 /* wake up all functions */
1204 retval = power_on_slot(slot);
1208 if (get_slot_status(slot) == ACPI_STA_ALL)
1209 /* configure all functions */
1210 retval = enable_device(slot);
1213 up(&slot->crit_sect);
1219 * acpiphp_disable_slot - power off slot
1221 int acpiphp_disable_slot (struct acpiphp_slot *slot)
1225 down(&slot->crit_sect);
1227 /* unconfigure all functions */
1228 retval = disable_device(slot);
1232 /* power off all functions */
1233 retval = power_off_slot(slot);
1237 acpiphp_resource_sort_and_combine(&slot->bridge->io_head);
1238 acpiphp_resource_sort_and_combine(&slot->bridge->mem_head);
1239 acpiphp_resource_sort_and_combine(&slot->bridge->p_mem_head);
1240 acpiphp_resource_sort_and_combine(&slot->bridge->bus_head);
1241 dbg("Available resources:\n");
1242 acpiphp_dump_resource(slot->bridge);
1245 up(&slot->crit_sect);
1251 * acpiphp_check_bridge - re-enumerate devices
1253 int acpiphp_check_bridge (struct acpiphp_bridge *bridge)
1255 struct acpiphp_slot *slot;
1258 int enabled, disabled;
1260 enabled = disabled = 0;
1262 for (slot = bridge->slots; slot; slot = slot->next) {
1263 sta = get_slot_status(slot);
1264 if (slot->flags & SLOT_ENABLED) {
1265 /* if enabled but not present, disable */
1266 if (sta != ACPI_STA_ALL) {
1267 retval = acpiphp_disable_slot(slot);
1269 err("Error occurred in enabling\n");
1270 up(&slot->crit_sect);
1276 /* if disabled but present, enable */
1277 if (sta == ACPI_STA_ALL) {
1278 retval = acpiphp_enable_slot(slot);
1280 err("Error occurred in enabling\n");
1281 up(&slot->crit_sect);
1289 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1300 u8 acpiphp_get_power_status (struct acpiphp_slot *slot)
1304 sta = get_slot_status(slot);
1306 return (sta & ACPI_STA_ENABLED) ? 1 : 0;
1311 * attention LED ON: 1
1315 * no direct attention led status information via ACPI
1318 u8 acpiphp_get_attention_status (struct acpiphp_slot *slot)
1328 u8 acpiphp_get_latch_status (struct acpiphp_slot *slot)
1332 sta = get_slot_status(slot);
1334 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1339 * adapter presence : 1
1342 u8 acpiphp_get_adapter_status (struct acpiphp_slot *slot)
1346 sta = get_slot_status(slot);
1348 return (sta == 0) ? 0 : 1;