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;
132 static int num_slots = 0; /* XXX if we support I/O node hotplug... */
134 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
136 if (ACPI_FAILURE(status))
139 status = acpi_get_handle(handle, "_EJ0", &tmp);
141 if (ACPI_FAILURE(status) && !(is_dependent_device(handle)))
144 device = (adr >> 16) & 0xffff;
145 function = adr & 0xffff;
147 newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
150 memset(newfunc, 0, sizeof(struct acpiphp_func));
152 INIT_LIST_HEAD(&newfunc->sibling);
153 newfunc->handle = handle;
154 newfunc->function = function;
155 if (ACPI_SUCCESS(status))
156 newfunc->flags = FUNC_HAS_EJ0;
158 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
159 newfunc->flags |= FUNC_HAS_STA;
161 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
162 newfunc->flags |= FUNC_HAS_PS0;
164 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
165 newfunc->flags |= FUNC_HAS_PS3;
167 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) {
168 newfunc->flags |= FUNC_HAS_DCK;
169 /* add to devices dependent on dock station,
170 * because this may actually be the dock bridge
172 dd = alloc_dependent_device(handle);
174 err("Can't allocate memory for "
175 "new dependent device!\n");
177 add_dependent_device(dd);
180 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
181 if (ACPI_FAILURE(status))
184 /* search for objects that share the same slot */
185 for (slot = bridge->slots; slot; slot = slot->next)
186 if (slot->device == device) {
187 if (slot->sun != sun)
188 warn("sibling found, but _SUN doesn't match!\n");
193 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
199 memset(slot, 0, sizeof(struct acpiphp_slot));
200 slot->bridge = bridge;
201 slot->id = num_slots++;
202 slot->device = device;
204 INIT_LIST_HEAD(&slot->funcs);
205 mutex_init(&slot->crit_sect);
207 slot->next = bridge->slots;
208 bridge->slots = slot;
212 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
213 slot->sun, pci_domain_nr(bridge->pci_bus),
214 bridge->pci_bus->number, slot->device);
217 newfunc->slot = slot;
218 list_add_tail(&newfunc->sibling, &slot->funcs);
220 /* associate corresponding pci_dev */
221 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
222 PCI_DEVFN(device, function));
223 if (newfunc->pci_dev) {
224 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
227 /* if this is a device dependent on a dock station,
228 * associate the acpiphp_func to the dependent_device
231 if ((dd = get_dependent_device(handle))) {
232 newfunc->flags |= FUNC_IS_DD;
234 * we don't want any devices which is dependent
235 * on the dock to have it's _EJ0 method executed.
236 * because we need to run _DCK first.
238 newfunc->flags &= ~FUNC_HAS_EJ0;
240 add_pci_dependent_device(dd);
243 /* install notify handler */
244 if (!(newfunc->flags & FUNC_HAS_DCK)) {
245 status = acpi_install_notify_handler(handle,
247 handle_hotplug_event_func,
250 if (ACPI_FAILURE(status))
251 err("failed to register interrupt notify handler\n");
259 /* see if it's worth looking at this bridge */
260 static int detect_ejectable_slots(acpi_handle *bridge_handle)
267 /* only check slots defined directly below bridge object */
268 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
269 is_ejectable_slot, (void *)&count, NULL);
275 /* decode ACPI 2.0 _HPP hot plug parameters */
276 static void decode_hpp(struct acpiphp_bridge *bridge)
279 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
281 union acpi_object *package;
284 /* default numbers */
285 bridge->hpp.cache_line_size = 0x10;
286 bridge->hpp.latency_timer = 0x40;
287 bridge->hpp.enable_SERR = 0;
288 bridge->hpp.enable_PERR = 0;
290 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
292 if (ACPI_FAILURE(status)) {
293 dbg("_HPP evaluation failed\n");
297 package = (union acpi_object *) buffer.pointer;
299 if (!package || package->type != ACPI_TYPE_PACKAGE ||
300 package->package.count != 4 || !package->package.elements) {
301 err("invalid _HPP object; ignoring\n");
305 for (i = 0; i < 4; i++) {
306 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
307 err("invalid _HPP parameter type; ignoring\n");
312 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
313 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
314 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
315 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
317 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
318 bridge->hpp.cache_line_size,
319 bridge->hpp.latency_timer,
320 bridge->hpp.enable_SERR,
321 bridge->hpp.enable_PERR);
323 bridge->flags |= BRIDGE_HAS_HPP;
326 kfree(buffer.pointer);
330 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
331 static void init_bridge_misc(struct acpiphp_bridge *bridge)
335 /* decode ACPI 2.0 _HPP (hot plug parameters) */
338 /* register all slot objects under this bridge */
339 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
340 register_slot, bridge, NULL);
342 /* install notify handler */
343 if (bridge->type != BRIDGE_TYPE_HOST) {
344 status = acpi_install_notify_handler(bridge->handle,
346 handle_hotplug_event_bridge,
349 if (ACPI_FAILURE(status)) {
350 err("failed to register interrupt notify handler\n");
354 list_add(&bridge->list, &bridge_list);
358 /* allocate and initialize host bridge data structure */
359 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
361 struct acpiphp_bridge *bridge;
363 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
367 memset(bridge, 0, sizeof(struct acpiphp_bridge));
369 bridge->type = BRIDGE_TYPE_HOST;
370 bridge->handle = handle;
372 bridge->pci_bus = pci_bus;
374 spin_lock_init(&bridge->res_lock);
376 init_bridge_misc(bridge);
380 /* allocate and initialize PCI-to-PCI bridge data structure */
381 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
383 struct acpiphp_bridge *bridge;
385 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
386 if (bridge == NULL) {
387 err("out of memory\n");
391 memset(bridge, 0, sizeof(struct acpiphp_bridge));
393 bridge->type = BRIDGE_TYPE_P2P;
394 bridge->handle = handle;
396 bridge->pci_dev = pci_dev_get(pci_dev);
397 bridge->pci_bus = pci_dev->subordinate;
398 if (!bridge->pci_bus) {
399 err("This is not a PCI-to-PCI bridge!\n");
403 spin_lock_init(&bridge->res_lock);
405 init_bridge_misc(bridge);
408 pci_dev_put(pci_dev);
414 /* callback routine to find P2P bridges */
416 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
419 acpi_handle dummy_handle;
421 int device, function;
423 struct pci_bus *pci_bus = context;
425 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
426 if (ACPI_FAILURE(status))
427 return AE_OK; /* continue */
429 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
430 if (ACPI_FAILURE(status)) {
431 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
435 device = (tmp >> 16) & 0xffff;
436 function = tmp & 0xffff;
438 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
440 if (!dev || !dev->subordinate)
443 /* check if this bridge has ejectable slots */
444 if ((detect_ejectable_slots(handle) > 0) ||
445 (detect_dependent_devices(handle) > 0)) {
446 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
447 add_p2p_bridge(handle, dev);
456 /* find hot-pluggable slots, and then find P2P bridge */
457 static int add_bridge(acpi_handle handle)
462 acpi_handle dummy_handle;
463 struct pci_bus *pci_bus;
465 /* if the bridge doesn't have _STA, we assume it is always there */
466 status = acpi_get_handle(handle, "_STA", &dummy_handle);
467 if (ACPI_SUCCESS(status)) {
468 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
469 if (ACPI_FAILURE(status)) {
470 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
473 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
474 /* don't register this object */
478 /* get PCI segment number */
479 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
481 seg = ACPI_SUCCESS(status) ? tmp : 0;
483 /* get PCI bus number */
484 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
486 if (ACPI_SUCCESS(status)) {
489 warn("can't get bus number, assuming 0\n");
493 pci_bus = pci_find_bus(seg, bus);
495 err("Can't find bus %04x:%02x\n", seg, bus);
499 /* check if this bridge has ejectable slots */
500 if (detect_ejectable_slots(handle) > 0) {
501 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
502 add_host_bridge(handle, pci_bus);
506 /* search P2P bridges under this host bridge */
507 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
508 find_p2p_bridge, pci_bus, NULL);
510 if (ACPI_FAILURE(status))
511 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
516 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
518 struct list_head *head;
519 list_for_each(head, &bridge_list) {
520 struct acpiphp_bridge *bridge = list_entry(head,
521 struct acpiphp_bridge, list);
522 if (bridge->handle == handle)
529 static void cleanup_bridge(struct acpiphp_bridge *bridge)
531 struct list_head *list, *tmp;
532 struct acpiphp_slot *slot;
534 acpi_handle handle = bridge->handle;
536 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
537 handle_hotplug_event_bridge);
538 if (ACPI_FAILURE(status))
539 err("failed to remove notify handler\n");
541 slot = bridge->slots;
543 struct acpiphp_slot *next = slot->next;
544 list_for_each_safe (list, tmp, &slot->funcs) {
545 struct acpiphp_func *func;
546 func = list_entry(list, struct acpiphp_func, sibling);
547 if (!(func->flags & FUNC_HAS_DCK)) {
548 status = acpi_remove_notify_handler(func->handle,
550 handle_hotplug_event_func);
551 if (ACPI_FAILURE(status))
552 err("failed to remove notify handler\n");
554 pci_dev_put(func->pci_dev);
562 pci_dev_put(bridge->pci_dev);
563 list_del(&bridge->list);
568 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
570 struct acpiphp_bridge *bridge;
572 if (!(bridge = acpiphp_handle_to_bridge(handle)))
574 cleanup_bridge(bridge);
578 static void remove_bridge(acpi_handle handle)
580 struct acpiphp_bridge *bridge;
582 bridge = acpiphp_handle_to_bridge(handle);
584 cleanup_bridge(bridge);
586 /* clean-up p2p bridges under this host bridge */
587 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
588 (u32)1, cleanup_p2p_bridge, NULL, NULL);
592 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
594 struct acpi_pci_id id;
598 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
601 bus = pci_find_bus(id.segment, id.bus);
605 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
609 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
610 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
619 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
624 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
625 union acpi_object *obj;
628 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
629 if (ACPI_SUCCESS(status)) {
630 *gsi_base = (u32)gsb;
634 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
635 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
638 obj = buffer.pointer;
639 if (obj->type != ACPI_TYPE_BUFFER)
642 table = obj->buffer.pointer;
643 switch (((acpi_table_entry_header *)table)->type) {
644 case ACPI_MADT_IOSAPIC:
645 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
648 case ACPI_MADT_IOAPIC:
649 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
656 acpi_os_free(buffer.pointer);
661 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
666 struct pci_dev *pdev;
670 /* Evaluate _STA if present */
671 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
672 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
673 return AE_CTRL_DEPTH;
675 /* Scan only PCI bus scope */
676 status = acpi_get_handle(handle, "_HID", &tmp);
677 if (ACPI_SUCCESS(status))
678 return AE_CTRL_DEPTH;
680 if (get_gsi_base(handle, &gsi_base))
683 pdev = get_apic_pci_info(handle);
687 if (pci_enable_device(pdev)) {
692 pci_set_master(pdev);
694 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
695 pci_disable_device(pdev);
700 phys_addr = pci_resource_start(pdev, 0);
701 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
702 pci_release_region(pdev, 0);
703 pci_disable_device(pdev);
711 static int acpiphp_configure_ioapics(acpi_handle handle)
713 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
714 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
718 static int power_on_slot(struct acpiphp_slot *slot)
721 struct acpiphp_func *func;
725 /* if already enabled, just skip */
726 if (slot->flags & SLOT_POWEREDON)
729 list_for_each (l, &slot->funcs) {
730 func = list_entry(l, struct acpiphp_func, sibling);
732 if (func->flags & FUNC_HAS_PS0) {
733 dbg("%s: executing _PS0\n", __FUNCTION__);
734 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
735 if (ACPI_FAILURE(status)) {
736 warn("%s: _PS0 failed\n", __FUNCTION__);
744 /* TBD: evaluate _STA to check if the slot is enabled */
746 slot->flags |= SLOT_POWEREDON;
753 static int power_off_slot(struct acpiphp_slot *slot)
756 struct acpiphp_func *func;
761 /* if already disabled, just skip */
762 if ((slot->flags & SLOT_POWEREDON) == 0)
765 list_for_each (l, &slot->funcs) {
766 func = list_entry(l, struct acpiphp_func, sibling);
768 if (func->flags & FUNC_HAS_PS3) {
769 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
770 if (ACPI_FAILURE(status)) {
771 warn("%s: _PS3 failed\n", __FUNCTION__);
779 /* TBD: evaluate _STA to check if the slot is disabled */
781 slot->flags &= (~SLOT_POWEREDON);
790 * acpiphp_max_busnr - return the highest reserved bus number under
792 * @bus: bus to start search with
795 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
797 struct list_head *tmp;
798 unsigned char max, n;
801 * pci_bus_max_busnr will return the highest
802 * reserved busnr for all these children.
803 * that is equivalent to the bus->subordinate
804 * value. We don't want to use the parent's
805 * bus->subordinate value because it could have
808 max = bus->secondary;
810 list_for_each(tmp, &bus->children) {
811 n = pci_bus_max_busnr(pci_bus_b(tmp));
821 * get_func - get a pointer to acpiphp_func given a slot, device
822 * @slot: slot to search
823 * @dev: pci_dev struct to match.
825 * This function will increase the reference count of pci_dev,
826 * so callers should call pci_dev_put when complete.
829 static struct acpiphp_func *
830 get_func(struct acpiphp_slot *slot, struct pci_dev *dev)
832 struct acpiphp_func *func = NULL;
833 struct pci_bus *bus = slot->bridge->pci_bus;
834 struct pci_dev *pdev;
836 list_for_each_entry(func, &slot->funcs, sibling) {
837 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device,
850 * acpiphp_bus_add - add a new bus to acpi subsystem
851 * @func: acpiphp_func of the bridge
854 static int acpiphp_bus_add(struct acpiphp_func *func)
857 struct acpi_device *device, *pdevice;
860 acpi_get_parent(func->handle, &phandle);
861 if (acpi_bus_get_device(phandle, &pdevice)) {
862 dbg("no parent device, assuming NULL\n");
865 if (!acpi_bus_get_device(func->handle, &device)) {
866 dbg("bus exists... trim\n");
867 /* this shouldn't be in here, so remove
868 * the bus then re-add it...
870 ret_val = acpi_bus_trim(device, 1);
871 dbg("acpi_bus_trim return %x\n", ret_val);
874 ret_val = acpi_bus_add(&device, pdevice, func->handle,
875 ACPI_BUS_TYPE_DEVICE);
877 dbg("error adding bus, %x\n",
879 goto acpiphp_bus_add_out;
882 * try to start anyway. We could have failed to add
883 * simply because this bus had previously been added
884 * on another add. Don't bother with the return value
885 * we just keep going.
887 ret_val = acpi_bus_start(device);
896 * enable_device - enable, configure a slot
897 * @slot: slot to be enabled
899 * This function should be called per *physical slot*,
900 * not per each slot object in ACPI namespace.
903 static int enable_device(struct acpiphp_slot *slot)
906 struct pci_bus *bus = slot->bridge->pci_bus;
908 struct acpiphp_func *func;
912 if (slot->flags & SLOT_ENABLED)
915 /* sanity check: dev should be NULL when hot-plugged in */
916 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
918 /* This case shouldn't happen */
919 err("pci_dev structure already exists.\n");
925 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
927 err("No new device found\n");
932 max = acpiphp_max_busnr(bus);
933 for (pass = 0; pass < 2; pass++) {
934 list_for_each_entry(dev, &bus->devices, bus_list) {
935 if (PCI_SLOT(dev->devfn) != slot->device)
937 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
938 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
939 max = pci_scan_bridge(bus, dev, max, pass);
940 if (pass && dev->subordinate) {
941 pci_bus_size_bridges(dev->subordinate);
942 func = get_func(slot, dev);
944 acpiphp_bus_add(func);
945 /* side effect of get_func */
953 pci_bus_assign_resources(bus);
954 acpiphp_sanitize_bus(bus);
955 pci_enable_bridges(bus);
956 pci_bus_add_devices(bus);
957 acpiphp_set_hpp_values(DEVICE_ACPI_HANDLE(&bus->self->dev), bus);
958 acpiphp_configure_ioapics(DEVICE_ACPI_HANDLE(&bus->self->dev));
960 /* associate pci_dev to our representation */
961 list_for_each (l, &slot->funcs) {
962 func = list_entry(l, struct acpiphp_func, sibling);
963 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
967 slot->flags |= SLOT_ENABLED;
975 * disable_device - disable a slot
977 static int disable_device(struct acpiphp_slot *slot)
980 struct acpiphp_func *func;
983 /* is this slot already disabled? */
984 if (!(slot->flags & SLOT_ENABLED))
987 list_for_each (l, &slot->funcs) {
988 func = list_entry(l, struct acpiphp_func, sibling);
992 pci_remove_bus_device(func->pci_dev);
993 pci_dev_put(func->pci_dev);
994 func->pci_dev = NULL;
997 slot->flags &= (~SLOT_ENABLED);
1005 * get_slot_status - get ACPI slot status
1007 * if a slot has _STA for each function and if any one of them
1008 * returned non-zero status, return it
1010 * if a slot doesn't have _STA and if any one of its functions'
1011 * configuration space is configured, return 0x0f as a _STA
1013 * otherwise return 0
1015 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1018 unsigned long sta = 0;
1020 struct list_head *l;
1021 struct acpiphp_func *func;
1023 list_for_each (l, &slot->funcs) {
1024 func = list_entry(l, struct acpiphp_func, sibling);
1026 if (func->flags & FUNC_HAS_STA) {
1027 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1028 if (ACPI_SUCCESS(status) && sta)
1031 pci_bus_read_config_dword(slot->bridge->pci_bus,
1032 PCI_DEVFN(slot->device,
1034 PCI_VENDOR_ID, &dvid);
1035 if (dvid != 0xffffffff) {
1042 return (unsigned int)sta;
1046 * acpiphp_eject_slot - physically eject the slot
1048 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1051 struct acpiphp_func *func;
1052 struct list_head *l;
1053 struct acpi_object_list arg_list;
1054 union acpi_object arg;
1056 list_for_each (l, &slot->funcs) {
1057 func = list_entry(l, struct acpiphp_func, sibling);
1059 /* We don't want to call _EJ0 on non-existing functions. */
1060 if ((func->flags & FUNC_HAS_EJ0)) {
1061 /* _EJ0 method take one argument */
1063 arg_list.pointer = &arg;
1064 arg.type = ACPI_TYPE_INTEGER;
1065 arg.integer.value = 1;
1067 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1068 if (ACPI_FAILURE(status)) {
1069 warn("%s: _EJ0 failed\n", __FUNCTION__);
1079 * acpiphp_check_bridge - re-enumerate devices
1081 * Iterate over all slots under this bridge and make sure that if a
1082 * card is present they are enabled, and if not they are disabled.
1084 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1086 struct acpiphp_slot *slot;
1088 int enabled, disabled;
1090 enabled = disabled = 0;
1092 for (slot = bridge->slots; slot; slot = slot->next) {
1093 unsigned int status = get_slot_status(slot);
1094 if (slot->flags & SLOT_ENABLED) {
1095 if (status == ACPI_STA_ALL)
1097 retval = acpiphp_disable_slot(slot);
1099 err("Error occurred in disabling\n");
1102 acpiphp_eject_slot(slot);
1106 if (status != ACPI_STA_ALL)
1108 retval = acpiphp_enable_slot(slot);
1110 err("Error occurred in enabling\n");
1117 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1123 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1125 u16 pci_cmd, pci_bctl;
1126 struct pci_dev *cdev;
1128 /* Program hpp values for this device */
1129 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1130 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1131 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1133 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1134 bridge->hpp.cache_line_size);
1135 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1136 bridge->hpp.latency_timer);
1137 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1138 if (bridge->hpp.enable_SERR)
1139 pci_cmd |= PCI_COMMAND_SERR;
1141 pci_cmd &= ~PCI_COMMAND_SERR;
1142 if (bridge->hpp.enable_PERR)
1143 pci_cmd |= PCI_COMMAND_PARITY;
1145 pci_cmd &= ~PCI_COMMAND_PARITY;
1146 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1148 /* Program bridge control value and child devices */
1149 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1150 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1151 bridge->hpp.latency_timer);
1152 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1153 if (bridge->hpp.enable_SERR)
1154 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1156 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1157 if (bridge->hpp.enable_PERR)
1158 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1160 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1161 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1162 if (dev->subordinate) {
1163 list_for_each_entry(cdev, &dev->subordinate->devices,
1165 program_hpp(cdev, bridge);
1170 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1172 struct acpiphp_bridge bridge;
1173 struct pci_dev *dev;
1175 memset(&bridge, 0, sizeof(bridge));
1176 bridge.handle = handle;
1177 decode_hpp(&bridge);
1178 list_for_each_entry(dev, &bus->devices, bus_list)
1179 program_hpp(dev, &bridge);
1184 * Remove devices for which we could not assign resources, call
1185 * arch specific code to fix-up the bus
1187 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1189 struct pci_dev *dev;
1191 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1193 list_for_each_entry(dev, &bus->devices, bus_list) {
1194 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1195 struct resource *res = &dev->resource[i];
1196 if ((res->flags & type_mask) && !res->start &&
1198 /* Could not assign a required resources
1199 * for this device, remove it */
1200 pci_remove_bus_device(dev);
1207 /* Program resources in newly inserted bridge */
1208 static int acpiphp_configure_bridge (acpi_handle handle)
1210 struct acpi_pci_id pci_id;
1211 struct pci_bus *bus;
1213 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1214 err("cannot get PCI domain and bus number for bridge\n");
1217 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1219 err("cannot find bus %d:%d\n",
1220 pci_id.segment, pci_id.bus);
1224 pci_bus_size_bridges(bus);
1225 pci_bus_assign_resources(bus);
1226 acpiphp_sanitize_bus(bus);
1227 acpiphp_set_hpp_values(handle, bus);
1228 pci_enable_bridges(bus);
1229 acpiphp_configure_ioapics(handle);
1233 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1235 struct acpi_device *device, *pdevice;
1236 acpi_handle phandle;
1238 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1239 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1240 err("unexpected notification type %d\n", type);
1244 acpi_get_parent(handle, &phandle);
1245 if (acpi_bus_get_device(phandle, &pdevice)) {
1246 dbg("no parent device, assuming NULL\n");
1249 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1250 err("cannot add bridge to acpi list\n");
1253 if (!acpiphp_configure_bridge(handle) &&
1254 !acpi_bus_start(device))
1257 err("cannot configure and start bridge\n");
1262 * ACPI event handlers
1266 * handle_hotplug_event_bridge - handle ACPI event on bridges
1268 * @handle: Notify()'ed acpi_handle
1269 * @type: Notify code
1270 * @context: pointer to acpiphp_bridge structure
1272 * handles ACPI event notification on {host,p2p} bridges
1275 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1277 struct acpiphp_bridge *bridge;
1279 struct acpi_buffer buffer = { .length = sizeof(objname),
1280 .pointer = objname };
1281 struct acpi_device *device;
1283 if (acpi_bus_get_device(handle, &device)) {
1284 /* This bridge must have just been physically inserted */
1285 handle_bridge_insertion(handle, type);
1289 bridge = acpiphp_handle_to_bridge(handle);
1291 err("cannot get bridge info\n");
1295 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1298 case ACPI_NOTIFY_BUS_CHECK:
1299 /* bus re-enumerate */
1300 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1301 acpiphp_check_bridge(bridge);
1304 case ACPI_NOTIFY_DEVICE_CHECK:
1306 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1307 acpiphp_check_bridge(bridge);
1310 case ACPI_NOTIFY_DEVICE_WAKE:
1312 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1315 case ACPI_NOTIFY_EJECT_REQUEST:
1316 /* request device eject */
1317 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1320 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1321 printk(KERN_ERR "Device %s cannot be configured due"
1322 " to a frequency mismatch\n", objname);
1325 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1326 printk(KERN_ERR "Device %s cannot be configured due"
1327 " to a bus mode mismatch\n", objname);
1330 case ACPI_NOTIFY_POWER_FAULT:
1331 printk(KERN_ERR "Device %s has suffered a power fault\n",
1336 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1342 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1344 * @handle: Notify()'ed acpi_handle
1345 * @type: Notify code
1346 * @context: pointer to acpiphp_func structure
1348 * handles ACPI event notification on slots
1351 void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1353 struct acpiphp_func *func;
1355 struct acpi_buffer buffer = { .length = sizeof(objname),
1356 .pointer = objname };
1358 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1360 func = (struct acpiphp_func *)context;
1363 case ACPI_NOTIFY_BUS_CHECK:
1364 /* bus re-enumerate */
1365 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1366 acpiphp_enable_slot(func->slot);
1369 case ACPI_NOTIFY_DEVICE_CHECK:
1370 /* device check : re-enumerate from parent bus */
1371 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1372 acpiphp_check_bridge(func->slot->bridge);
1375 case ACPI_NOTIFY_DEVICE_WAKE:
1377 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1380 case ACPI_NOTIFY_EJECT_REQUEST:
1381 /* request device eject */
1382 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1383 if (!(acpiphp_disable_slot(func->slot)))
1384 acpiphp_eject_slot(func->slot);
1388 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1393 static int is_root_bridge(acpi_handle handle)
1396 struct acpi_device_info *info;
1397 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1400 status = acpi_get_object_info(handle, &buffer);
1401 if (ACPI_SUCCESS(status)) {
1402 info = buffer.pointer;
1403 if ((info->valid & ACPI_VALID_HID) &&
1404 !strcmp(PCI_ROOT_HID_STRING,
1405 info->hardware_id.value)) {
1406 acpi_os_free(buffer.pointer);
1409 if (info->valid & ACPI_VALID_CID) {
1410 for (i=0; i < info->compatibility_id.count; i++) {
1411 if (!strcmp(PCI_ROOT_HID_STRING,
1412 info->compatibility_id.id[i].value)) {
1413 acpi_os_free(buffer.pointer);
1423 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1425 int *count = (int *)context;
1427 if (is_root_bridge(handle)) {
1428 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1429 handle_hotplug_event_bridge, NULL);
1435 static struct acpi_pci_driver acpi_pci_hp_driver = {
1437 .remove = remove_bridge,
1441 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1444 int __init acpiphp_glue_init(void)
1448 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1449 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1454 acpi_pci_register_driver(&acpi_pci_hp_driver);
1461 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1463 * This function frees all data allocated in acpiphp_glue_init()
1465 void __exit acpiphp_glue_exit(void)
1467 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1472 * acpiphp_get_num_slots - count number of slots in a system
1474 int __init acpiphp_get_num_slots(void)
1476 struct list_head *node;
1477 struct acpiphp_bridge *bridge;
1482 list_for_each (node, &bridge_list) {
1483 bridge = (struct acpiphp_bridge *)node;
1484 dbg("Bus %04x:%02x has %d slot%s\n",
1485 pci_domain_nr(bridge->pci_bus),
1486 bridge->pci_bus->number, bridge->nr_slots,
1487 bridge->nr_slots == 1 ? "" : "s");
1488 num_slots += bridge->nr_slots;
1491 dbg("Total %d slots\n", num_slots);
1498 * acpiphp_for_each_slot - call function for each slot
1499 * @fn: callback function
1500 * @data: context to be passed to callback function
1503 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1505 struct list_head *node;
1506 struct acpiphp_bridge *bridge;
1507 struct acpiphp_slot *slot;
1510 list_for_each (node, &bridge_list) {
1511 bridge = (struct acpiphp_bridge *)node;
1512 for (slot = bridge->slots; slot; slot = slot->next) {
1513 retval = fn(slot, data);
1524 /* search matching slot from id */
1525 struct acpiphp_slot *get_slot_from_id(int id)
1527 struct list_head *node;
1528 struct acpiphp_bridge *bridge;
1529 struct acpiphp_slot *slot;
1531 list_for_each (node, &bridge_list) {
1532 bridge = (struct acpiphp_bridge *)node;
1533 for (slot = bridge->slots; slot; slot = slot->next)
1538 /* should never happen! */
1539 err("%s: no object for id %d\n", __FUNCTION__, id);
1546 * acpiphp_enable_slot - power on slot
1548 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1552 mutex_lock(&slot->crit_sect);
1554 /* wake up all functions */
1555 retval = power_on_slot(slot);
1559 if (get_slot_status(slot) == ACPI_STA_ALL)
1560 /* configure all functions */
1561 retval = enable_device(slot);
1564 mutex_unlock(&slot->crit_sect);
1569 * acpiphp_disable_slot - power off slot
1571 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1575 mutex_lock(&slot->crit_sect);
1577 /* unconfigure all functions */
1578 retval = disable_device(slot);
1582 /* power off all functions */
1583 retval = power_off_slot(slot);
1588 mutex_unlock(&slot->crit_sect);
1597 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1599 return (slot->flags & SLOT_POWEREDON);
1607 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1611 sta = get_slot_status(slot);
1613 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1618 * adapter presence : 1
1621 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1625 sta = get_slot_status(slot);
1627 return (sta == 0) ? 0 : 1;
1632 * pci address (seg/bus/dev)
1634 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1637 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1639 address = (pci_domain_nr(pci_bus) << 16) |
1640 (pci_bus->number << 8) |