2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
4 * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5 * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6 * Copyright (C) 2002,2003 NEC Corporation
7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
12 * All rights reserved.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22 * NON INFRINGEMENT. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Send feedback to <t-kochi@bq.jp.nec.com>
34 * Lifetime rules for pci_dev:
35 * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
36 * when the driver is loaded or when an insertion event occurs. It loses
37 * a refcount when its ejected or the driver unloads.
38 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
39 * when the bridge is scanned and it loses a refcount when the bridge
43 #include <linux/init.h>
44 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/pci.h>
48 #include <linux/smp_lock.h>
49 #include <linux/mutex.h>
52 #include "pci_hotplug.h"
55 static LIST_HEAD(bridge_list);
57 #define MY_NAME "acpiphp_glue"
59 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
60 static void acpiphp_sanitize_bus(struct pci_bus *bus);
61 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
65 * initialization & terminatation routines
69 * is_ejectable - determine if a slot is ejectable
70 * @handle: handle to acpi namespace
72 * Ejectable slot should satisfy at least these conditions:
85 static int is_ejectable(acpi_handle handle)
90 status = acpi_get_handle(handle, "_ADR", &tmp);
91 if (ACPI_FAILURE(status)) {
95 status = acpi_get_handle(handle, "_EJ0", &tmp);
96 if (ACPI_FAILURE(status)) {
104 /* callback routine to check the existence of ejectable slots */
106 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
108 int *count = (int *)context;
110 if (is_ejectable(handle)) {
112 /* only one ejectable slot is enough */
113 return AE_CTRL_TERMINATE;
120 /* callback routine to register each ACPI PCI slot object */
122 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
124 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
125 struct acpiphp_slot *slot;
126 struct acpiphp_func *newfunc;
127 struct dependent_device *dd;
129 acpi_status status = AE_OK;
130 unsigned long adr, sun;
131 int device, function, retval;
133 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
135 if (ACPI_FAILURE(status))
138 status = acpi_get_handle(handle, "_EJ0", &tmp);
140 if (ACPI_FAILURE(status) && !(is_dependent_device(handle)))
143 device = (adr >> 16) & 0xffff;
144 function = adr & 0xffff;
146 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
150 INIT_LIST_HEAD(&newfunc->sibling);
151 newfunc->handle = handle;
152 newfunc->function = function;
153 if (ACPI_SUCCESS(status))
154 newfunc->flags = FUNC_HAS_EJ0;
156 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
157 newfunc->flags |= FUNC_HAS_STA;
159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
160 newfunc->flags |= FUNC_HAS_PS0;
162 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
163 newfunc->flags |= FUNC_HAS_PS3;
165 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) {
166 newfunc->flags |= FUNC_HAS_DCK;
167 /* add to devices dependent on dock station,
168 * because this may actually be the dock bridge
170 dd = alloc_dependent_device(handle);
172 err("Can't allocate memory for "
173 "new dependent device!\n");
175 add_dependent_device(dd);
178 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
179 if (ACPI_FAILURE(status))
182 /* search for objects that share the same slot */
183 for (slot = bridge->slots; slot; slot = slot->next)
184 if (slot->device == device) {
185 if (slot->sun != sun)
186 warn("sibling found, but _SUN doesn't match!\n");
191 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
197 slot->bridge = bridge;
198 slot->device = device;
200 INIT_LIST_HEAD(&slot->funcs);
201 mutex_init(&slot->crit_sect);
203 slot->next = bridge->slots;
204 bridge->slots = slot;
208 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
209 slot->sun, pci_domain_nr(bridge->pci_bus),
210 bridge->pci_bus->number, slot->device);
211 retval = acpiphp_register_hotplug_slot(slot);
213 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
218 newfunc->slot = slot;
219 list_add_tail(&newfunc->sibling, &slot->funcs);
221 /* associate corresponding pci_dev */
222 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
223 PCI_DEVFN(device, function));
224 if (newfunc->pci_dev) {
225 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
228 /* if this is a device dependent on a dock station,
229 * associate the acpiphp_func to the dependent_device
232 if ((dd = get_dependent_device(handle))) {
233 newfunc->flags |= FUNC_IS_DD;
235 * we don't want any devices which is dependent
236 * on the dock to have it's _EJ0 method executed.
237 * because we need to run _DCK first.
239 newfunc->flags &= ~FUNC_HAS_EJ0;
241 add_pci_dependent_device(dd);
244 /* install notify handler */
245 if (!(newfunc->flags & FUNC_HAS_DCK)) {
246 status = acpi_install_notify_handler(handle,
248 handle_hotplug_event_func,
251 if (ACPI_FAILURE(status))
252 err("failed to register interrupt notify handler\n");
260 bridge->slots = slot->next;
268 /* see if it's worth looking at this bridge */
269 static int detect_ejectable_slots(acpi_handle *bridge_handle)
276 /* only check slots defined directly below bridge object */
277 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
278 is_ejectable_slot, (void *)&count, NULL);
284 /* decode ACPI 2.0 _HPP hot plug parameters */
285 static void decode_hpp(struct acpiphp_bridge *bridge)
289 status = acpi_get_hp_params_from_firmware(bridge->pci_dev, &bridge->hpp);
290 if (ACPI_FAILURE(status)) {
291 /* use default numbers */
292 bridge->hpp.cache_line_size = 0x10;
293 bridge->hpp.latency_timer = 0x40;
294 bridge->hpp.enable_serr = 0;
295 bridge->hpp.enable_perr = 0;
301 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
302 static void init_bridge_misc(struct acpiphp_bridge *bridge)
306 /* decode ACPI 2.0 _HPP (hot plug parameters) */
309 /* must be added to the list prior to calling register_slot */
310 list_add(&bridge->list, &bridge_list);
312 /* register all slot objects under this bridge */
313 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
314 register_slot, bridge, NULL);
315 if (ACPI_FAILURE(status)) {
316 list_del(&bridge->list);
320 /* install notify handler */
321 if (bridge->type != BRIDGE_TYPE_HOST) {
322 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
323 status = acpi_remove_notify_handler(bridge->func->handle,
325 handle_hotplug_event_func);
326 if (ACPI_FAILURE(status))
327 err("failed to remove notify handler\n");
329 status = acpi_install_notify_handler(bridge->handle,
331 handle_hotplug_event_bridge,
334 if (ACPI_FAILURE(status)) {
335 err("failed to register interrupt notify handler\n");
341 /* find acpiphp_func from acpiphp_bridge */
342 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
344 struct list_head *node, *l;
345 struct acpiphp_bridge *bridge;
346 struct acpiphp_slot *slot;
347 struct acpiphp_func *func;
349 list_for_each(node, &bridge_list) {
350 bridge = list_entry(node, struct acpiphp_bridge, list);
351 for (slot = bridge->slots; slot; slot = slot->next) {
352 list_for_each(l, &slot->funcs) {
353 func = list_entry(l, struct acpiphp_func,
355 if (func->handle == handle)
365 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
367 acpi_handle dummy_handle;
369 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
370 "_STA", &dummy_handle)))
371 bridge->flags |= BRIDGE_HAS_STA;
373 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
374 "_EJ0", &dummy_handle)))
375 bridge->flags |= BRIDGE_HAS_EJ0;
377 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
378 "_PS0", &dummy_handle)))
379 bridge->flags |= BRIDGE_HAS_PS0;
381 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
382 "_PS3", &dummy_handle)))
383 bridge->flags |= BRIDGE_HAS_PS3;
385 /* is this ejectable p2p bridge? */
386 if (bridge->flags & BRIDGE_HAS_EJ0) {
387 struct acpiphp_func *func;
389 dbg("found ejectable p2p bridge\n");
391 /* make link between PCI bridge and PCI function */
392 func = acpiphp_bridge_handle_to_function(bridge->handle);
396 func->bridge = bridge;
401 /* allocate and initialize host bridge data structure */
402 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
404 struct acpiphp_bridge *bridge;
406 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
410 bridge->type = BRIDGE_TYPE_HOST;
411 bridge->handle = handle;
413 bridge->pci_bus = pci_bus;
415 spin_lock_init(&bridge->res_lock);
417 init_bridge_misc(bridge);
421 /* allocate and initialize PCI-to-PCI bridge data structure */
422 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
424 struct acpiphp_bridge *bridge;
426 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
427 if (bridge == NULL) {
428 err("out of memory\n");
432 bridge->type = BRIDGE_TYPE_P2P;
433 bridge->handle = handle;
434 config_p2p_bridge_flags(bridge);
436 bridge->pci_dev = pci_dev_get(pci_dev);
437 bridge->pci_bus = pci_dev->subordinate;
438 if (!bridge->pci_bus) {
439 err("This is not a PCI-to-PCI bridge!\n");
443 spin_lock_init(&bridge->res_lock);
445 init_bridge_misc(bridge);
448 pci_dev_put(pci_dev);
454 /* callback routine to find P2P bridges */
456 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
459 acpi_handle dummy_handle;
461 int device, function;
463 struct pci_bus *pci_bus = context;
465 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
466 if (ACPI_FAILURE(status))
467 return AE_OK; /* continue */
469 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
470 if (ACPI_FAILURE(status)) {
471 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
475 device = (tmp >> 16) & 0xffff;
476 function = tmp & 0xffff;
478 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
480 if (!dev || !dev->subordinate)
483 /* check if this bridge has ejectable slots */
484 if ((detect_ejectable_slots(handle) > 0) ||
485 (detect_dependent_devices(handle) > 0)) {
486 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
487 add_p2p_bridge(handle, dev);
490 /* search P2P bridges under this p2p bridge */
491 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
492 find_p2p_bridge, dev->subordinate, NULL);
493 if (ACPI_FAILURE(status))
494 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
502 /* find hot-pluggable slots, and then find P2P bridge */
503 static int add_bridge(acpi_handle handle)
508 acpi_handle dummy_handle;
509 struct pci_bus *pci_bus;
511 /* if the bridge doesn't have _STA, we assume it is always there */
512 status = acpi_get_handle(handle, "_STA", &dummy_handle);
513 if (ACPI_SUCCESS(status)) {
514 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
515 if (ACPI_FAILURE(status)) {
516 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
519 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
520 /* don't register this object */
524 /* get PCI segment number */
525 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
527 seg = ACPI_SUCCESS(status) ? tmp : 0;
529 /* get PCI bus number */
530 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
532 if (ACPI_SUCCESS(status)) {
535 warn("can't get bus number, assuming 0\n");
539 pci_bus = pci_find_bus(seg, bus);
541 err("Can't find bus %04x:%02x\n", seg, bus);
545 /* check if this bridge has ejectable slots */
546 if (detect_ejectable_slots(handle) > 0) {
547 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
548 add_host_bridge(handle, pci_bus);
552 /* search P2P bridges under this host bridge */
553 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
554 find_p2p_bridge, pci_bus, NULL);
556 if (ACPI_FAILURE(status))
557 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
562 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
564 struct list_head *head;
565 list_for_each(head, &bridge_list) {
566 struct acpiphp_bridge *bridge = list_entry(head,
567 struct acpiphp_bridge, list);
568 if (bridge->handle == handle)
575 static void cleanup_bridge(struct acpiphp_bridge *bridge)
577 struct list_head *list, *tmp;
578 struct acpiphp_slot *slot;
580 acpi_handle handle = bridge->handle;
582 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
583 handle_hotplug_event_bridge);
584 if (ACPI_FAILURE(status))
585 err("failed to remove notify handler\n");
587 if ((bridge->type != BRIDGE_TYPE_HOST) &&
588 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
589 status = acpi_install_notify_handler(bridge->func->handle,
591 handle_hotplug_event_func,
593 if (ACPI_FAILURE(status))
594 err("failed to install interrupt notify handler\n");
597 slot = bridge->slots;
599 struct acpiphp_slot *next = slot->next;
600 list_for_each_safe (list, tmp, &slot->funcs) {
601 struct acpiphp_func *func;
602 func = list_entry(list, struct acpiphp_func, sibling);
603 if (!(func->flags & FUNC_HAS_DCK)) {
604 status = acpi_remove_notify_handler(func->handle,
606 handle_hotplug_event_func);
607 if (ACPI_FAILURE(status))
608 err("failed to remove notify handler\n");
610 pci_dev_put(func->pci_dev);
614 acpiphp_unregister_hotplug_slot(slot);
615 list_del(&slot->funcs);
620 pci_dev_put(bridge->pci_dev);
621 list_del(&bridge->list);
626 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
628 struct acpiphp_bridge *bridge;
630 /* cleanup p2p bridges under this P2P bridge
631 in a depth-first manner */
632 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
633 cleanup_p2p_bridge, NULL, NULL);
635 if (!(bridge = acpiphp_handle_to_bridge(handle)))
637 cleanup_bridge(bridge);
641 static void remove_bridge(acpi_handle handle)
643 struct acpiphp_bridge *bridge;
645 /* cleanup p2p bridges under this host bridge
646 in a depth-first manner */
647 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
648 (u32)1, cleanup_p2p_bridge, NULL, NULL);
650 bridge = acpiphp_handle_to_bridge(handle);
652 cleanup_bridge(bridge);
655 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
657 struct acpi_pci_id id;
661 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
664 bus = pci_find_bus(id.segment, id.bus);
668 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
672 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
673 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
682 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
687 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
688 union acpi_object *obj;
691 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
692 if (ACPI_SUCCESS(status)) {
693 *gsi_base = (u32)gsb;
697 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
698 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
701 obj = buffer.pointer;
702 if (obj->type != ACPI_TYPE_BUFFER)
705 table = obj->buffer.pointer;
706 switch (((acpi_table_entry_header *)table)->type) {
707 case ACPI_MADT_IOSAPIC:
708 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
711 case ACPI_MADT_IOAPIC:
712 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
719 acpi_os_free(buffer.pointer);
724 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
729 struct pci_dev *pdev;
733 /* Evaluate _STA if present */
734 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
735 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
736 return AE_CTRL_DEPTH;
738 /* Scan only PCI bus scope */
739 status = acpi_get_handle(handle, "_HID", &tmp);
740 if (ACPI_SUCCESS(status))
741 return AE_CTRL_DEPTH;
743 if (get_gsi_base(handle, &gsi_base))
746 pdev = get_apic_pci_info(handle);
750 if (pci_enable_device(pdev)) {
755 pci_set_master(pdev);
757 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
758 pci_disable_device(pdev);
763 phys_addr = pci_resource_start(pdev, 0);
764 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
765 pci_release_region(pdev, 0);
766 pci_disable_device(pdev);
774 static int acpiphp_configure_ioapics(acpi_handle handle)
776 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
777 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
781 static int power_on_slot(struct acpiphp_slot *slot)
784 struct acpiphp_func *func;
788 /* if already enabled, just skip */
789 if (slot->flags & SLOT_POWEREDON)
792 list_for_each (l, &slot->funcs) {
793 func = list_entry(l, struct acpiphp_func, sibling);
795 if (func->flags & FUNC_HAS_PS0) {
796 dbg("%s: executing _PS0\n", __FUNCTION__);
797 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
798 if (ACPI_FAILURE(status)) {
799 warn("%s: _PS0 failed\n", __FUNCTION__);
807 /* TBD: evaluate _STA to check if the slot is enabled */
809 slot->flags |= SLOT_POWEREDON;
816 static int power_off_slot(struct acpiphp_slot *slot)
819 struct acpiphp_func *func;
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->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 /* TBD: evaluate _STA to check if the slot is disabled */
844 slot->flags &= (~SLOT_POWEREDON);
853 * acpiphp_max_busnr - return the highest reserved bus number under
855 * @bus: bus to start search with
858 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
860 struct list_head *tmp;
861 unsigned char max, n;
864 * pci_bus_max_busnr will return the highest
865 * reserved busnr for all these children.
866 * that is equivalent to the bus->subordinate
867 * value. We don't want to use the parent's
868 * bus->subordinate value because it could have
871 max = bus->secondary;
873 list_for_each(tmp, &bus->children) {
874 n = pci_bus_max_busnr(pci_bus_b(tmp));
883 * acpiphp_bus_add - add a new bus to acpi subsystem
884 * @func: acpiphp_func of the bridge
887 static int acpiphp_bus_add(struct acpiphp_func *func)
890 struct acpi_device *device, *pdevice;
893 acpi_get_parent(func->handle, &phandle);
894 if (acpi_bus_get_device(phandle, &pdevice)) {
895 dbg("no parent device, assuming NULL\n");
898 if (!acpi_bus_get_device(func->handle, &device)) {
899 dbg("bus exists... trim\n");
900 /* this shouldn't be in here, so remove
901 * the bus then re-add it...
903 ret_val = acpi_bus_trim(device, 1);
904 dbg("acpi_bus_trim return %x\n", ret_val);
907 ret_val = acpi_bus_add(&device, pdevice, func->handle,
908 ACPI_BUS_TYPE_DEVICE);
910 dbg("error adding bus, %x\n",
912 goto acpiphp_bus_add_out;
915 * try to start anyway. We could have failed to add
916 * simply because this bus had previously been added
917 * on another add. Don't bother with the return value
918 * we just keep going.
920 ret_val = acpi_bus_start(device);
928 * acpiphp_bus_trim - trim a bus from acpi subsystem
929 * @handle: handle to acpi namespace
932 int acpiphp_bus_trim(acpi_handle handle)
934 struct acpi_device *device;
937 retval = acpi_bus_get_device(handle, &device);
939 dbg("acpi_device not found\n");
943 retval = acpi_bus_trim(device, 1);
945 err("cannot remove from acpi list\n");
951 * enable_device - enable, configure a slot
952 * @slot: slot to be enabled
954 * This function should be called per *physical slot*,
955 * not per each slot object in ACPI namespace.
958 static int enable_device(struct acpiphp_slot *slot)
961 struct pci_bus *bus = slot->bridge->pci_bus;
963 struct acpiphp_func *func;
968 if (slot->flags & SLOT_ENABLED)
971 /* sanity check: dev should be NULL when hot-plugged in */
972 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
974 /* This case shouldn't happen */
975 err("pci_dev structure already exists.\n");
981 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
983 err("No new device found\n");
988 max = acpiphp_max_busnr(bus);
989 for (pass = 0; pass < 2; pass++) {
990 list_for_each_entry(dev, &bus->devices, bus_list) {
991 if (PCI_SLOT(dev->devfn) != slot->device)
993 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
994 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
995 max = pci_scan_bridge(bus, dev, max, pass);
996 if (pass && dev->subordinate)
997 pci_bus_size_bridges(dev->subordinate);
1002 list_for_each (l, &slot->funcs) {
1003 func = list_entry(l, struct acpiphp_func, sibling);
1004 acpiphp_bus_add(func);
1007 pci_bus_assign_resources(bus);
1008 acpiphp_sanitize_bus(bus);
1009 pci_enable_bridges(bus);
1010 pci_bus_add_devices(bus);
1011 acpiphp_set_hpp_values(slot->bridge->handle, bus);
1012 acpiphp_configure_ioapics(slot->bridge->handle);
1014 /* associate pci_dev to our representation */
1015 list_for_each (l, &slot->funcs) {
1016 func = list_entry(l, struct acpiphp_func, sibling);
1017 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1022 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1023 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1026 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1027 if (ACPI_FAILURE(status))
1028 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1032 slot->flags |= SLOT_ENABLED;
1040 * disable_device - disable a slot
1042 static int disable_device(struct acpiphp_slot *slot)
1045 struct acpiphp_func *func;
1046 struct list_head *l;
1048 /* is this slot already disabled? */
1049 if (!(slot->flags & SLOT_ENABLED))
1052 list_for_each (l, &slot->funcs) {
1053 func = list_entry(l, struct acpiphp_func, sibling);
1056 /* cleanup p2p bridges under this P2P bridge */
1057 cleanup_p2p_bridge(func->bridge->handle,
1058 (u32)1, NULL, NULL);
1059 func->bridge = NULL;
1062 acpiphp_bus_trim(func->handle);
1063 /* try to remove anyway.
1064 * acpiphp_bus_add might have been failed */
1069 pci_remove_bus_device(func->pci_dev);
1070 pci_dev_put(func->pci_dev);
1071 func->pci_dev = NULL;
1074 slot->flags &= (~SLOT_ENABLED);
1082 * get_slot_status - get ACPI slot status
1084 * if a slot has _STA for each function and if any one of them
1085 * returned non-zero status, return it
1087 * if a slot doesn't have _STA and if any one of its functions'
1088 * configuration space is configured, return 0x0f as a _STA
1090 * otherwise return 0
1092 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1095 unsigned long sta = 0;
1097 struct list_head *l;
1098 struct acpiphp_func *func;
1100 list_for_each (l, &slot->funcs) {
1101 func = list_entry(l, struct acpiphp_func, sibling);
1103 if (func->flags & FUNC_HAS_STA) {
1104 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1105 if (ACPI_SUCCESS(status) && sta)
1108 pci_bus_read_config_dword(slot->bridge->pci_bus,
1109 PCI_DEVFN(slot->device,
1111 PCI_VENDOR_ID, &dvid);
1112 if (dvid != 0xffffffff) {
1119 return (unsigned int)sta;
1123 * acpiphp_eject_slot - physically eject the slot
1125 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1128 struct acpiphp_func *func;
1129 struct list_head *l;
1130 struct acpi_object_list arg_list;
1131 union acpi_object arg;
1133 list_for_each (l, &slot->funcs) {
1134 func = list_entry(l, struct acpiphp_func, sibling);
1136 /* We don't want to call _EJ0 on non-existing functions. */
1137 if ((func->flags & FUNC_HAS_EJ0)) {
1138 /* _EJ0 method take one argument */
1140 arg_list.pointer = &arg;
1141 arg.type = ACPI_TYPE_INTEGER;
1142 arg.integer.value = 1;
1144 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1145 if (ACPI_FAILURE(status)) {
1146 warn("%s: _EJ0 failed\n", __FUNCTION__);
1156 * acpiphp_check_bridge - re-enumerate devices
1158 * Iterate over all slots under this bridge and make sure that if a
1159 * card is present they are enabled, and if not they are disabled.
1161 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1163 struct acpiphp_slot *slot;
1165 int enabled, disabled;
1167 enabled = disabled = 0;
1169 for (slot = bridge->slots; slot; slot = slot->next) {
1170 unsigned int status = get_slot_status(slot);
1171 if (slot->flags & SLOT_ENABLED) {
1172 if (status == ACPI_STA_ALL)
1174 retval = acpiphp_disable_slot(slot);
1176 err("Error occurred in disabling\n");
1179 acpiphp_eject_slot(slot);
1183 if (status != ACPI_STA_ALL)
1185 retval = acpiphp_enable_slot(slot);
1187 err("Error occurred in enabling\n");
1194 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1200 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1202 u16 pci_cmd, pci_bctl;
1203 struct pci_dev *cdev;
1205 /* Program hpp values for this device */
1206 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1207 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1208 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1210 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1211 bridge->hpp.cache_line_size);
1212 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1213 bridge->hpp.latency_timer);
1214 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1215 if (bridge->hpp.enable_serr)
1216 pci_cmd |= PCI_COMMAND_SERR;
1218 pci_cmd &= ~PCI_COMMAND_SERR;
1219 if (bridge->hpp.enable_perr)
1220 pci_cmd |= PCI_COMMAND_PARITY;
1222 pci_cmd &= ~PCI_COMMAND_PARITY;
1223 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1225 /* Program bridge control value and child devices */
1226 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1227 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1228 bridge->hpp.latency_timer);
1229 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1230 if (bridge->hpp.enable_serr)
1231 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1233 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1234 if (bridge->hpp.enable_perr)
1235 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1237 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1238 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1239 if (dev->subordinate) {
1240 list_for_each_entry(cdev, &dev->subordinate->devices,
1242 program_hpp(cdev, bridge);
1247 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1249 struct acpiphp_bridge bridge;
1250 struct pci_dev *dev;
1252 memset(&bridge, 0, sizeof(bridge));
1253 bridge.handle = handle;
1254 bridge.pci_dev = bus->self;
1255 decode_hpp(&bridge);
1256 list_for_each_entry(dev, &bus->devices, bus_list)
1257 program_hpp(dev, &bridge);
1262 * Remove devices for which we could not assign resources, call
1263 * arch specific code to fix-up the bus
1265 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1267 struct pci_dev *dev;
1269 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1271 list_for_each_entry(dev, &bus->devices, bus_list) {
1272 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1273 struct resource *res = &dev->resource[i];
1274 if ((res->flags & type_mask) && !res->start &&
1276 /* Could not assign a required resources
1277 * for this device, remove it */
1278 pci_remove_bus_device(dev);
1285 /* Program resources in newly inserted bridge */
1286 static int acpiphp_configure_bridge (acpi_handle handle)
1288 struct acpi_pci_id pci_id;
1289 struct pci_bus *bus;
1291 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1292 err("cannot get PCI domain and bus number for bridge\n");
1295 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1297 err("cannot find bus %d:%d\n",
1298 pci_id.segment, pci_id.bus);
1302 pci_bus_size_bridges(bus);
1303 pci_bus_assign_resources(bus);
1304 acpiphp_sanitize_bus(bus);
1305 acpiphp_set_hpp_values(handle, bus);
1306 pci_enable_bridges(bus);
1307 acpiphp_configure_ioapics(handle);
1311 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1313 struct acpi_device *device, *pdevice;
1314 acpi_handle phandle;
1316 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1317 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1318 err("unexpected notification type %d\n", type);
1322 acpi_get_parent(handle, &phandle);
1323 if (acpi_bus_get_device(phandle, &pdevice)) {
1324 dbg("no parent device, assuming NULL\n");
1327 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1328 err("cannot add bridge to acpi list\n");
1331 if (!acpiphp_configure_bridge(handle) &&
1332 !acpi_bus_start(device))
1335 err("cannot configure and start bridge\n");
1340 * ACPI event handlers
1344 * handle_hotplug_event_bridge - handle ACPI event on bridges
1346 * @handle: Notify()'ed acpi_handle
1347 * @type: Notify code
1348 * @context: pointer to acpiphp_bridge structure
1350 * handles ACPI event notification on {host,p2p} bridges
1353 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1355 struct acpiphp_bridge *bridge;
1357 struct acpi_buffer buffer = { .length = sizeof(objname),
1358 .pointer = objname };
1359 struct acpi_device *device;
1361 if (acpi_bus_get_device(handle, &device)) {
1362 /* This bridge must have just been physically inserted */
1363 handle_bridge_insertion(handle, type);
1367 bridge = acpiphp_handle_to_bridge(handle);
1369 err("cannot get bridge info\n");
1373 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1376 case ACPI_NOTIFY_BUS_CHECK:
1377 /* bus re-enumerate */
1378 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1379 acpiphp_check_bridge(bridge);
1382 case ACPI_NOTIFY_DEVICE_CHECK:
1384 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1385 acpiphp_check_bridge(bridge);
1388 case ACPI_NOTIFY_DEVICE_WAKE:
1390 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1393 case ACPI_NOTIFY_EJECT_REQUEST:
1394 /* request device eject */
1395 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1396 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1397 (bridge->flags & BRIDGE_HAS_EJ0)) {
1398 struct acpiphp_slot *slot;
1399 slot = bridge->func->slot;
1400 if (!acpiphp_disable_slot(slot))
1401 acpiphp_eject_slot(slot);
1405 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1406 printk(KERN_ERR "Device %s cannot be configured due"
1407 " to a frequency mismatch\n", objname);
1410 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1411 printk(KERN_ERR "Device %s cannot be configured due"
1412 " to a bus mode mismatch\n", objname);
1415 case ACPI_NOTIFY_POWER_FAULT:
1416 printk(KERN_ERR "Device %s has suffered a power fault\n",
1421 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1427 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1429 * @handle: Notify()'ed acpi_handle
1430 * @type: Notify code
1431 * @context: pointer to acpiphp_func structure
1433 * handles ACPI event notification on slots
1436 void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1438 struct acpiphp_func *func;
1440 struct acpi_buffer buffer = { .length = sizeof(objname),
1441 .pointer = objname };
1443 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1445 func = (struct acpiphp_func *)context;
1448 case ACPI_NOTIFY_BUS_CHECK:
1449 /* bus re-enumerate */
1450 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1451 acpiphp_enable_slot(func->slot);
1454 case ACPI_NOTIFY_DEVICE_CHECK:
1455 /* device check : re-enumerate from parent bus */
1456 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1457 acpiphp_check_bridge(func->slot->bridge);
1460 case ACPI_NOTIFY_DEVICE_WAKE:
1462 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1465 case ACPI_NOTIFY_EJECT_REQUEST:
1466 /* request device eject */
1467 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1468 if (!(acpiphp_disable_slot(func->slot)))
1469 acpiphp_eject_slot(func->slot);
1473 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1480 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1482 int *count = (int *)context;
1484 if (acpi_root_bridge(handle)) {
1485 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1486 handle_hotplug_event_bridge, NULL);
1492 static struct acpi_pci_driver acpi_pci_hp_driver = {
1494 .remove = remove_bridge,
1498 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1501 int __init acpiphp_glue_init(void)
1505 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1506 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1511 acpi_pci_register_driver(&acpi_pci_hp_driver);
1518 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1520 * This function frees all data allocated in acpiphp_glue_init()
1522 void __exit acpiphp_glue_exit(void)
1524 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1529 * acpiphp_get_num_slots - count number of slots in a system
1531 int __init acpiphp_get_num_slots(void)
1533 struct list_head *node;
1534 struct acpiphp_bridge *bridge;
1539 list_for_each (node, &bridge_list) {
1540 bridge = (struct acpiphp_bridge *)node;
1541 dbg("Bus %04x:%02x has %d slot%s\n",
1542 pci_domain_nr(bridge->pci_bus),
1543 bridge->pci_bus->number, bridge->nr_slots,
1544 bridge->nr_slots == 1 ? "" : "s");
1545 num_slots += bridge->nr_slots;
1548 dbg("Total %d slots\n", num_slots);
1555 * acpiphp_for_each_slot - call function for each slot
1556 * @fn: callback function
1557 * @data: context to be passed to callback function
1560 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1562 struct list_head *node;
1563 struct acpiphp_bridge *bridge;
1564 struct acpiphp_slot *slot;
1567 list_for_each (node, &bridge_list) {
1568 bridge = (struct acpiphp_bridge *)node;
1569 for (slot = bridge->slots; slot; slot = slot->next) {
1570 retval = fn(slot, data);
1583 * acpiphp_enable_slot - power on slot
1585 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1589 mutex_lock(&slot->crit_sect);
1591 /* wake up all functions */
1592 retval = power_on_slot(slot);
1596 if (get_slot_status(slot) == ACPI_STA_ALL)
1597 /* configure all functions */
1598 retval = enable_device(slot);
1601 mutex_unlock(&slot->crit_sect);
1606 * acpiphp_disable_slot - power off slot
1608 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1612 mutex_lock(&slot->crit_sect);
1614 /* unconfigure all functions */
1615 retval = disable_device(slot);
1619 /* power off all functions */
1620 retval = power_off_slot(slot);
1625 mutex_unlock(&slot->crit_sect);
1634 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1636 return (slot->flags & SLOT_POWEREDON);
1644 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1648 sta = get_slot_status(slot);
1650 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1655 * adapter presence : 1
1658 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1662 sta = get_slot_status(slot);
1664 return (sta == 0) ? 0 : 1;
1669 * pci address (seg/bus/dev)
1671 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1674 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1676 address = (pci_domain_nr(pci_bus) << 16) |
1677 (pci_bus->number << 8) |