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 <kristen.c.accardi@intel.com>
34 * Lifetime rules for pci_dev:
35 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
36 * when the bridge is scanned and it loses a refcount when the bridge
38 * - When a P2P bridge is present, we elevate the refcount on the subordinate
39 * bus. It loses the refcount when the the driver unloads.
42 #include <linux/init.h>
43 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/pci.h>
47 #include <linux/pci_hotplug.h>
48 #include <linux/pci-acpi.h>
49 #include <linux/mutex.h>
54 static LIST_HEAD(bridge_list);
55 static LIST_HEAD(ioapic_list);
56 static DEFINE_SPINLOCK(ioapic_list_lock);
58 #define MY_NAME "acpiphp_glue"
60 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
61 static void acpiphp_sanitize_bus(struct pci_bus *bus);
62 static void acpiphp_set_hpp_values(struct pci_bus *bus);
63 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
65 /* callback routine to check for the existence of a pci dock device */
67 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
69 int *count = (int *)context;
71 if (is_dock_device(handle)) {
73 return AE_CTRL_TERMINATE;
80 * the _DCK method can do funny things... and sometimes not
83 * TBD - figure out a way to only call fixups for
84 * systems that require them.
86 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
89 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90 struct pci_bus *bus = func->slot->bridge->pci_bus;
96 /* fixup bad _DCK function that rewrites
97 * secondary bridge on slot
99 pci_read_config_dword(bus->self,
103 if (((buses >> 8) & 0xff) != bus->secondary) {
104 buses = (buses & 0xff000000)
105 | ((unsigned int)(bus->primary) << 0)
106 | ((unsigned int)(bus->secondary) << 8)
107 | ((unsigned int)(bus->subordinate) << 16);
108 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
114 static struct acpi_dock_ops acpiphp_dock_ops = {
115 .handler = handle_hotplug_event_func,
118 /* callback routine to register each ACPI PCI slot object */
120 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
122 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
123 struct acpiphp_slot *slot;
124 struct acpiphp_func *newfunc;
126 acpi_status status = AE_OK;
127 unsigned long long adr, sun;
128 int device, function, retval;
129 struct pci_bus *pbus = bridge->pci_bus;
130 struct pci_dev *pdev;
132 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
135 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
136 device = (adr >> 16) & 0xffff;
137 function = adr & 0xffff;
139 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
143 INIT_LIST_HEAD(&newfunc->sibling);
144 newfunc->handle = handle;
145 newfunc->function = function;
147 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
148 newfunc->flags = FUNC_HAS_EJ0;
150 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
151 newfunc->flags |= FUNC_HAS_STA;
153 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
154 newfunc->flags |= FUNC_HAS_PS0;
156 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
157 newfunc->flags |= FUNC_HAS_PS3;
159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
160 newfunc->flags |= FUNC_HAS_DCK;
162 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
163 if (ACPI_FAILURE(status)) {
165 * use the count of the number of slots we've found
166 * for the number of the slot
168 sun = bridge->nr_slots+1;
171 /* search for objects that share the same slot */
172 for (slot = bridge->slots; slot; slot = slot->next)
173 if (slot->device == device) {
174 if (slot->sun != sun)
175 warn("sibling found, but _SUN doesn't match!\n");
180 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
186 slot->bridge = bridge;
187 slot->device = device;
189 INIT_LIST_HEAD(&slot->funcs);
190 mutex_init(&slot->crit_sect);
192 slot->next = bridge->slots;
193 bridge->slots = slot;
197 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
198 slot->sun, pci_domain_nr(pbus), pbus->number, device);
199 retval = acpiphp_register_hotplug_slot(slot);
201 if (retval == -EBUSY)
202 warn("Slot %llu already registered by another "
203 "hotplug driver\n", slot->sun);
205 warn("acpiphp_register_hotplug_slot failed "
206 "(err code = 0x%x)\n", retval);
211 newfunc->slot = slot;
212 list_add_tail(&newfunc->sibling, &slot->funcs);
214 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
216 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
220 if (is_dock_device(handle)) {
221 /* we don't want to call this device's _EJ0
222 * because we want the dock notify handler
223 * to call it after it calls _DCK
225 newfunc->flags &= ~FUNC_HAS_EJ0;
226 if (register_hotplug_dock_device(handle,
227 &acpiphp_dock_ops, newfunc))
228 dbg("failed to register dock device\n");
230 /* we need to be notified when dock events happen
231 * outside of the hotplug operation, since we may
232 * need to do fixups before we can hotplug.
234 newfunc->nb.notifier_call = post_dock_fixups;
235 if (register_dock_notifier(&newfunc->nb))
236 dbg("failed to register a dock notifier");
239 /* install notify handler */
240 if (!(newfunc->flags & FUNC_HAS_DCK)) {
241 status = acpi_install_notify_handler(handle,
243 handle_hotplug_event_func,
246 if (ACPI_FAILURE(status))
247 err("failed to register interrupt notify handler\n");
255 bridge->slots = slot->next;
263 /* see if it's worth looking at this bridge */
264 static int detect_ejectable_slots(acpi_handle handle)
266 int found = acpi_pci_detect_ejectable(handle);
268 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
269 is_pci_dock_device, (void *)&found, NULL);
275 /* decode ACPI 2.0 _HPP hot plug parameters */
276 static void decode_hpp(struct pci_dev *dev, struct hotplug_params *hpp)
280 ret = acpi_get_hp_params_from_firmware(dev, hpp);
281 if (ret || !hpp->t0 || (hpp->t0->revision > 1)) {
282 /* use default numbers */
284 "%s: Could not get hotplug parameters. Use defaults\n",
286 hpp->t0 = &hpp->type0_data;
287 hpp->t0->revision = 0;
288 hpp->t0->cache_line_size = 0x10;
289 hpp->t0->latency_timer = 0x40;
290 hpp->t0->enable_serr = 0;
291 hpp->t0->enable_perr = 0;
297 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
298 static void init_bridge_misc(struct acpiphp_bridge *bridge)
302 /* must be added to the list prior to calling register_slot */
303 list_add(&bridge->list, &bridge_list);
305 /* register all slot objects under this bridge */
306 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
307 register_slot, bridge, NULL);
308 if (ACPI_FAILURE(status)) {
309 list_del(&bridge->list);
313 /* install notify handler */
314 if (bridge->type != BRIDGE_TYPE_HOST) {
315 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
316 status = acpi_remove_notify_handler(bridge->func->handle,
318 handle_hotplug_event_func);
319 if (ACPI_FAILURE(status))
320 err("failed to remove notify handler\n");
322 status = acpi_install_notify_handler(bridge->handle,
324 handle_hotplug_event_bridge,
327 if (ACPI_FAILURE(status)) {
328 err("failed to register interrupt notify handler\n");
334 /* find acpiphp_func from acpiphp_bridge */
335 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
337 struct list_head *node, *l;
338 struct acpiphp_bridge *bridge;
339 struct acpiphp_slot *slot;
340 struct acpiphp_func *func;
342 list_for_each(node, &bridge_list) {
343 bridge = list_entry(node, struct acpiphp_bridge, list);
344 for (slot = bridge->slots; slot; slot = slot->next) {
345 list_for_each(l, &slot->funcs) {
346 func = list_entry(l, struct acpiphp_func,
348 if (func->handle == handle)
358 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
360 acpi_handle dummy_handle;
362 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
363 "_STA", &dummy_handle)))
364 bridge->flags |= BRIDGE_HAS_STA;
366 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
367 "_EJ0", &dummy_handle)))
368 bridge->flags |= BRIDGE_HAS_EJ0;
370 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
371 "_PS0", &dummy_handle)))
372 bridge->flags |= BRIDGE_HAS_PS0;
374 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
375 "_PS3", &dummy_handle)))
376 bridge->flags |= BRIDGE_HAS_PS3;
378 /* is this ejectable p2p bridge? */
379 if (bridge->flags & BRIDGE_HAS_EJ0) {
380 struct acpiphp_func *func;
382 dbg("found ejectable p2p bridge\n");
384 /* make link between PCI bridge and PCI function */
385 func = acpiphp_bridge_handle_to_function(bridge->handle);
389 func->bridge = bridge;
394 /* allocate and initialize host bridge data structure */
395 static void add_host_bridge(acpi_handle *handle)
397 struct acpiphp_bridge *bridge;
398 struct acpi_pci_root *root = acpi_pci_find_root(handle);
400 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
404 bridge->type = BRIDGE_TYPE_HOST;
405 bridge->handle = handle;
407 bridge->pci_bus = root->bus;
409 spin_lock_init(&bridge->res_lock);
411 init_bridge_misc(bridge);
415 /* allocate and initialize PCI-to-PCI bridge data structure */
416 static void add_p2p_bridge(acpi_handle *handle)
418 struct acpiphp_bridge *bridge;
420 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
421 if (bridge == NULL) {
422 err("out of memory\n");
426 bridge->type = BRIDGE_TYPE_P2P;
427 bridge->handle = handle;
428 config_p2p_bridge_flags(bridge);
430 bridge->pci_dev = acpi_get_pci_dev(handle);
431 bridge->pci_bus = bridge->pci_dev->subordinate;
432 if (!bridge->pci_bus) {
433 err("This is not a PCI-to-PCI bridge!\n");
438 * Grab a ref to the subordinate PCI bus in case the bus is
439 * removed via PCI core logical hotplug. The ref pins the bus
440 * (which we access during module unload).
442 get_device(&bridge->pci_bus->dev);
443 spin_lock_init(&bridge->res_lock);
445 init_bridge_misc(bridge);
448 pci_dev_put(bridge->pci_dev);
454 /* callback routine to find P2P bridges */
456 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
461 dev = acpi_get_pci_dev(handle);
462 if (!dev || !dev->subordinate)
465 /* check if this bridge has ejectable slots */
466 if ((detect_ejectable_slots(handle) > 0)) {
467 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
468 add_p2p_bridge(handle);
471 /* search P2P bridges under this p2p bridge */
472 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
473 find_p2p_bridge, NULL, NULL);
474 if (ACPI_FAILURE(status))
475 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
483 /* find hot-pluggable slots, and then find P2P bridge */
484 static int add_bridge(acpi_handle handle)
487 unsigned long long tmp;
488 acpi_handle dummy_handle;
490 /* if the bridge doesn't have _STA, we assume it is always there */
491 status = acpi_get_handle(handle, "_STA", &dummy_handle);
492 if (ACPI_SUCCESS(status)) {
493 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
494 if (ACPI_FAILURE(status)) {
495 dbg("%s: _STA evaluation failure\n", __func__);
498 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
499 /* don't register this object */
503 /* check if this bridge has ejectable slots */
504 if (detect_ejectable_slots(handle) > 0) {
505 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
506 add_host_bridge(handle);
509 /* search P2P bridges under this host bridge */
510 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
511 find_p2p_bridge, NULL, NULL);
513 if (ACPI_FAILURE(status))
514 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
519 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
521 struct list_head *head;
522 list_for_each(head, &bridge_list) {
523 struct acpiphp_bridge *bridge = list_entry(head,
524 struct acpiphp_bridge, list);
525 if (bridge->handle == handle)
532 static void cleanup_bridge(struct acpiphp_bridge *bridge)
534 struct list_head *list, *tmp;
535 struct acpiphp_slot *slot;
537 acpi_handle handle = bridge->handle;
539 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
540 handle_hotplug_event_bridge);
541 if (ACPI_FAILURE(status))
542 err("failed to remove notify handler\n");
544 if ((bridge->type != BRIDGE_TYPE_HOST) &&
545 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
546 status = acpi_install_notify_handler(bridge->func->handle,
548 handle_hotplug_event_func,
550 if (ACPI_FAILURE(status))
551 err("failed to install interrupt notify handler\n");
554 slot = bridge->slots;
556 struct acpiphp_slot *next = slot->next;
557 list_for_each_safe (list, tmp, &slot->funcs) {
558 struct acpiphp_func *func;
559 func = list_entry(list, struct acpiphp_func, sibling);
560 if (is_dock_device(func->handle)) {
561 unregister_hotplug_dock_device(func->handle);
562 unregister_dock_notifier(&func->nb);
564 if (!(func->flags & FUNC_HAS_DCK)) {
565 status = acpi_remove_notify_handler(func->handle,
567 handle_hotplug_event_func);
568 if (ACPI_FAILURE(status))
569 err("failed to remove notify handler\n");
574 acpiphp_unregister_hotplug_slot(slot);
575 list_del(&slot->funcs);
581 * Only P2P bridges have a pci_dev
584 put_device(&bridge->pci_bus->dev);
586 pci_dev_put(bridge->pci_dev);
587 list_del(&bridge->list);
592 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
594 struct acpiphp_bridge *bridge;
596 /* cleanup p2p bridges under this P2P bridge
597 in a depth-first manner */
598 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
599 cleanup_p2p_bridge, NULL, NULL);
601 bridge = acpiphp_handle_to_bridge(handle);
603 cleanup_bridge(bridge);
608 static void remove_bridge(acpi_handle handle)
610 struct acpiphp_bridge *bridge;
612 /* cleanup p2p bridges under this host bridge
613 in a depth-first manner */
614 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
615 (u32)1, cleanup_p2p_bridge, NULL, NULL);
618 * On root bridges with hotplug slots directly underneath (ie,
619 * no p2p bridge inbetween), we call cleanup_bridge().
621 * The else clause cleans up root bridges that either had no
622 * hotplug slots at all, or had a p2p bridge underneath.
624 bridge = acpiphp_handle_to_bridge(handle);
626 cleanup_bridge(bridge);
628 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
629 handle_hotplug_event_bridge);
632 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
636 dev = acpi_get_pci_dev(handle);
640 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
641 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
650 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
654 unsigned long long gsb;
655 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
656 union acpi_object *obj;
659 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
660 if (ACPI_SUCCESS(status)) {
661 *gsi_base = (u32)gsb;
665 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
666 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
669 obj = buffer.pointer;
670 if (obj->type != ACPI_TYPE_BUFFER)
673 table = obj->buffer.pointer;
674 switch (((struct acpi_subtable_header *)table)->type) {
675 case ACPI_MADT_TYPE_IO_SAPIC:
676 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
679 case ACPI_MADT_TYPE_IO_APIC:
680 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
687 kfree(buffer.pointer);
692 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
695 unsigned long long sta;
697 struct pci_dev *pdev;
700 struct acpiphp_ioapic *ioapic;
702 /* Evaluate _STA if present */
703 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
704 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
705 return AE_CTRL_DEPTH;
707 /* Scan only PCI bus scope */
708 status = acpi_get_handle(handle, "_HID", &tmp);
709 if (ACPI_SUCCESS(status))
710 return AE_CTRL_DEPTH;
712 if (get_gsi_base(handle, &gsi_base))
715 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
719 pdev = get_apic_pci_info(handle);
723 if (pci_enable_device(pdev))
724 goto exit_pci_dev_put;
726 pci_set_master(pdev);
728 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
729 goto exit_pci_disable_device;
731 phys_addr = pci_resource_start(pdev, 0);
732 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
733 goto exit_pci_release_region;
735 ioapic->gsi_base = gsi_base;
737 spin_lock(&ioapic_list_lock);
738 list_add_tail(&ioapic->list, &ioapic_list);
739 spin_unlock(&ioapic_list_lock);
743 exit_pci_release_region:
744 pci_release_region(pdev, 0);
745 exit_pci_disable_device:
746 pci_disable_device(pdev);
756 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
759 unsigned long long sta;
762 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
764 /* Evaluate _STA if present */
765 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
766 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
767 return AE_CTRL_DEPTH;
769 /* Scan only PCI bus scope */
770 status = acpi_get_handle(handle, "_HID", &tmp);
771 if (ACPI_SUCCESS(status))
772 return AE_CTRL_DEPTH;
774 if (get_gsi_base(handle, &gsi_base))
777 acpi_unregister_ioapic(handle, gsi_base);
779 spin_lock(&ioapic_list_lock);
780 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
781 if (pos->gsi_base != gsi_base)
784 list_del(&ioapic->list);
787 spin_unlock(&ioapic_list_lock);
792 pci_release_region(ioapic->dev, 0);
793 pci_disable_device(ioapic->dev);
794 pci_dev_put(ioapic->dev);
800 static int acpiphp_configure_ioapics(acpi_handle handle)
802 ioapic_add(handle, 0, NULL, NULL);
803 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
804 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
808 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
810 ioapic_remove(handle, 0, NULL, NULL);
811 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
812 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
816 static int power_on_slot(struct acpiphp_slot *slot)
819 struct acpiphp_func *func;
823 /* if already enabled, just skip */
824 if (slot->flags & SLOT_POWEREDON)
827 list_for_each (l, &slot->funcs) {
828 func = list_entry(l, struct acpiphp_func, sibling);
830 if (func->flags & FUNC_HAS_PS0) {
831 dbg("%s: executing _PS0\n", __func__);
832 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
833 if (ACPI_FAILURE(status)) {
834 warn("%s: _PS0 failed\n", __func__);
842 /* TBD: evaluate _STA to check if the slot is enabled */
844 slot->flags |= SLOT_POWEREDON;
851 static int power_off_slot(struct acpiphp_slot *slot)
854 struct acpiphp_func *func;
859 /* if already disabled, just skip */
860 if ((slot->flags & SLOT_POWEREDON) == 0)
863 list_for_each (l, &slot->funcs) {
864 func = list_entry(l, struct acpiphp_func, sibling);
866 if (func->flags & FUNC_HAS_PS3) {
867 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
868 if (ACPI_FAILURE(status)) {
869 warn("%s: _PS3 failed\n", __func__);
877 /* TBD: evaluate _STA to check if the slot is disabled */
879 slot->flags &= (~SLOT_POWEREDON);
888 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
889 * @bus: bus to start search with
891 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
893 struct list_head *tmp;
894 unsigned char max, n;
897 * pci_bus_max_busnr will return the highest
898 * reserved busnr for all these children.
899 * that is equivalent to the bus->subordinate
900 * value. We don't want to use the parent's
901 * bus->subordinate value because it could have
904 max = bus->secondary;
906 list_for_each(tmp, &bus->children) {
907 n = pci_bus_max_busnr(pci_bus_b(tmp));
916 * acpiphp_bus_add - add a new bus to acpi subsystem
917 * @func: acpiphp_func of the bridge
919 static int acpiphp_bus_add(struct acpiphp_func *func)
922 struct acpi_device *device, *pdevice;
925 acpi_get_parent(func->handle, &phandle);
926 if (acpi_bus_get_device(phandle, &pdevice)) {
927 dbg("no parent device, assuming NULL\n");
930 if (!acpi_bus_get_device(func->handle, &device)) {
931 dbg("bus exists... trim\n");
932 /* this shouldn't be in here, so remove
933 * the bus then re-add it...
935 ret_val = acpi_bus_trim(device, 1);
936 dbg("acpi_bus_trim return %x\n", ret_val);
939 ret_val = acpi_bus_add(&device, pdevice, func->handle,
940 ACPI_BUS_TYPE_DEVICE);
942 dbg("error adding bus, %x\n",
944 goto acpiphp_bus_add_out;
947 * try to start anyway. We could have failed to add
948 * simply because this bus had previously been added
949 * on another add. Don't bother with the return value
950 * we just keep going.
952 ret_val = acpi_bus_start(device);
960 * acpiphp_bus_trim - trim a bus from acpi subsystem
961 * @handle: handle to acpi namespace
963 static int acpiphp_bus_trim(acpi_handle handle)
965 struct acpi_device *device;
968 retval = acpi_bus_get_device(handle, &device);
970 dbg("acpi_device not found\n");
974 retval = acpi_bus_trim(device, 1);
976 err("cannot remove from acpi list\n");
982 * enable_device - enable, configure a slot
983 * @slot: slot to be enabled
985 * This function should be called per *physical slot*,
986 * not per each slot object in ACPI namespace.
988 static int __ref enable_device(struct acpiphp_slot *slot)
991 struct pci_bus *bus = slot->bridge->pci_bus;
993 struct acpiphp_func *func;
998 if (slot->flags & SLOT_ENABLED)
1001 /* sanity check: dev should be NULL when hot-plugged in */
1002 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1004 /* This case shouldn't happen */
1005 err("pci_dev structure already exists.\n");
1011 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1013 err("No new device found\n");
1018 max = acpiphp_max_busnr(bus);
1019 for (pass = 0; pass < 2; pass++) {
1020 list_for_each_entry(dev, &bus->devices, bus_list) {
1021 if (PCI_SLOT(dev->devfn) != slot->device)
1023 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1024 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1025 max = pci_scan_bridge(bus, dev, max, pass);
1026 if (pass && dev->subordinate)
1027 pci_bus_size_bridges(dev->subordinate);
1032 list_for_each (l, &slot->funcs) {
1033 func = list_entry(l, struct acpiphp_func, sibling);
1034 acpiphp_bus_add(func);
1037 pci_bus_assign_resources(bus);
1038 acpiphp_sanitize_bus(bus);
1039 acpiphp_set_hpp_values(bus);
1040 list_for_each_entry(func, &slot->funcs, sibling)
1041 acpiphp_configure_ioapics(func->handle);
1042 pci_enable_bridges(bus);
1043 pci_bus_add_devices(bus);
1045 list_for_each (l, &slot->funcs) {
1046 func = list_entry(l, struct acpiphp_func, sibling);
1047 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1052 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1053 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1058 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1059 if (ACPI_FAILURE(status))
1060 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1065 slot->flags |= SLOT_ENABLED;
1071 static void disable_bridges(struct pci_bus *bus)
1073 struct pci_dev *dev;
1074 list_for_each_entry(dev, &bus->devices, bus_list) {
1075 if (dev->subordinate) {
1076 disable_bridges(dev->subordinate);
1077 pci_disable_device(dev);
1083 * disable_device - disable a slot
1084 * @slot: ACPI PHP slot
1086 static int disable_device(struct acpiphp_slot *slot)
1088 struct acpiphp_func *func;
1089 struct pci_dev *pdev;
1091 /* is this slot already disabled? */
1092 if (!(slot->flags & SLOT_ENABLED))
1095 list_for_each_entry(func, &slot->funcs, sibling) {
1097 /* cleanup p2p bridges under this P2P bridge */
1098 cleanup_p2p_bridge(func->bridge->handle,
1099 (u32)1, NULL, NULL);
1100 func->bridge = NULL;
1103 pdev = pci_get_slot(slot->bridge->pci_bus,
1104 PCI_DEVFN(slot->device, func->function));
1106 pci_stop_bus_device(pdev);
1107 if (pdev->subordinate) {
1108 disable_bridges(pdev->subordinate);
1109 pci_disable_device(pdev);
1111 pci_remove_bus_device(pdev);
1116 list_for_each_entry(func, &slot->funcs, sibling) {
1117 acpiphp_unconfigure_ioapics(func->handle);
1118 acpiphp_bus_trim(func->handle);
1121 slot->flags &= (~SLOT_ENABLED);
1129 * get_slot_status - get ACPI slot status
1130 * @slot: ACPI PHP slot
1132 * If a slot has _STA for each function and if any one of them
1133 * returned non-zero status, return it.
1135 * If a slot doesn't have _STA and if any one of its functions'
1136 * configuration space is configured, return 0x0f as a _STA.
1138 * Otherwise return 0.
1140 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1143 unsigned long long sta = 0;
1145 struct list_head *l;
1146 struct acpiphp_func *func;
1148 list_for_each (l, &slot->funcs) {
1149 func = list_entry(l, struct acpiphp_func, sibling);
1151 if (func->flags & FUNC_HAS_STA) {
1152 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1153 if (ACPI_SUCCESS(status) && sta)
1156 pci_bus_read_config_dword(slot->bridge->pci_bus,
1157 PCI_DEVFN(slot->device,
1159 PCI_VENDOR_ID, &dvid);
1160 if (dvid != 0xffffffff) {
1167 return (unsigned int)sta;
1171 * acpiphp_eject_slot - physically eject the slot
1172 * @slot: ACPI PHP slot
1174 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1177 struct acpiphp_func *func;
1178 struct list_head *l;
1179 struct acpi_object_list arg_list;
1180 union acpi_object arg;
1182 list_for_each (l, &slot->funcs) {
1183 func = list_entry(l, struct acpiphp_func, sibling);
1185 /* We don't want to call _EJ0 on non-existing functions. */
1186 if ((func->flags & FUNC_HAS_EJ0)) {
1187 /* _EJ0 method take one argument */
1189 arg_list.pointer = &arg;
1190 arg.type = ACPI_TYPE_INTEGER;
1191 arg.integer.value = 1;
1193 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1194 if (ACPI_FAILURE(status)) {
1195 warn("%s: _EJ0 failed\n", __func__);
1205 * acpiphp_check_bridge - re-enumerate devices
1206 * @bridge: where to begin re-enumeration
1208 * Iterate over all slots under this bridge and make sure that if a
1209 * card is present they are enabled, and if not they are disabled.
1211 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1213 struct acpiphp_slot *slot;
1215 int enabled, disabled;
1217 enabled = disabled = 0;
1219 for (slot = bridge->slots; slot; slot = slot->next) {
1220 unsigned int status = get_slot_status(slot);
1221 if (slot->flags & SLOT_ENABLED) {
1222 if (status == ACPI_STA_ALL)
1224 retval = acpiphp_disable_slot(slot);
1226 err("Error occurred in disabling\n");
1229 acpiphp_eject_slot(slot);
1233 if (status != ACPI_STA_ALL)
1235 retval = acpiphp_enable_slot(slot);
1237 err("Error occurred in enabling\n");
1244 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1250 static void program_hpp(struct pci_dev *dev, struct hotplug_params *hpp)
1252 u16 pci_cmd, pci_bctl;
1253 struct pci_dev *cdev;
1255 /* Program hpp values for this device */
1256 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1257 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1258 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1261 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1264 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1265 hpp->t0->cache_line_size);
1266 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1267 hpp->t0->latency_timer);
1268 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1269 if (hpp->t0->enable_serr)
1270 pci_cmd |= PCI_COMMAND_SERR;
1272 pci_cmd &= ~PCI_COMMAND_SERR;
1273 if (hpp->t0->enable_perr)
1274 pci_cmd |= PCI_COMMAND_PARITY;
1276 pci_cmd &= ~PCI_COMMAND_PARITY;
1277 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1279 /* Program bridge control value and child devices */
1280 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1281 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1282 hpp->t0->latency_timer);
1283 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1284 if (hpp->t0->enable_serr)
1285 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1287 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1288 if (hpp->t0->enable_perr)
1289 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1291 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1292 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1293 if (dev->subordinate) {
1294 list_for_each_entry(cdev, &dev->subordinate->devices,
1296 program_hpp(cdev, hpp);
1301 static void acpiphp_set_hpp_values(struct pci_bus *bus)
1303 struct pci_dev *dev;
1304 struct hotplug_params hpp;
1306 list_for_each_entry(dev, &bus->devices, bus_list) {
1307 decode_hpp(dev, &hpp);
1308 program_hpp(dev, &hpp);
1313 * Remove devices for which we could not assign resources, call
1314 * arch specific code to fix-up the bus
1316 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1318 struct pci_dev *dev;
1320 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1322 list_for_each_entry(dev, &bus->devices, bus_list) {
1323 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1324 struct resource *res = &dev->resource[i];
1325 if ((res->flags & type_mask) && !res->start &&
1327 /* Could not assign a required resources
1328 * for this device, remove it */
1329 pci_remove_bus_device(dev);
1336 /* Program resources in newly inserted bridge */
1337 static int acpiphp_configure_bridge (acpi_handle handle)
1339 struct pci_bus *bus;
1341 if (acpi_is_root_bridge(handle)) {
1342 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1345 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1346 bus = pdev->subordinate;
1350 pci_bus_size_bridges(bus);
1351 pci_bus_assign_resources(bus);
1352 acpiphp_sanitize_bus(bus);
1353 acpiphp_set_hpp_values(bus);
1354 pci_enable_bridges(bus);
1355 acpiphp_configure_ioapics(handle);
1359 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1361 struct acpi_device *device, *pdevice;
1362 acpi_handle phandle;
1364 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1365 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1366 err("unexpected notification type %d\n", type);
1370 acpi_get_parent(handle, &phandle);
1371 if (acpi_bus_get_device(phandle, &pdevice)) {
1372 dbg("no parent device, assuming NULL\n");
1375 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1376 err("cannot add bridge to acpi list\n");
1379 if (!acpiphp_configure_bridge(handle) &&
1380 !acpi_bus_start(device))
1383 err("cannot configure and start bridge\n");
1388 * ACPI event handlers
1392 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1394 int *count = (int *)context;
1395 struct acpiphp_bridge *bridge;
1397 bridge = acpiphp_handle_to_bridge(handle);
1404 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1406 struct acpiphp_bridge *bridge;
1408 struct acpi_buffer buffer = { .length = sizeof(objname),
1409 .pointer = objname };
1411 bridge = acpiphp_handle_to_bridge(handle);
1413 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1414 dbg("%s: re-enumerating slots under %s\n",
1416 acpiphp_check_bridge(bridge);
1422 * handle_hotplug_event_bridge - handle ACPI event on bridges
1423 * @handle: Notify()'ed acpi_handle
1424 * @type: Notify code
1425 * @context: pointer to acpiphp_bridge structure
1427 * Handles ACPI event notification on {host,p2p} bridges.
1429 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1431 struct acpiphp_bridge *bridge;
1433 struct acpi_buffer buffer = { .length = sizeof(objname),
1434 .pointer = objname };
1435 struct acpi_device *device;
1436 int num_sub_bridges = 0;
1438 if (acpi_bus_get_device(handle, &device)) {
1439 /* This bridge must have just been physically inserted */
1440 handle_bridge_insertion(handle, type);
1444 bridge = acpiphp_handle_to_bridge(handle);
1445 if (type == ACPI_NOTIFY_BUS_CHECK) {
1446 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1447 count_sub_bridges, &num_sub_bridges, NULL);
1450 if (!bridge && !num_sub_bridges) {
1451 err("cannot get bridge info\n");
1455 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1458 case ACPI_NOTIFY_BUS_CHECK:
1459 /* bus re-enumerate */
1460 dbg("%s: Bus check notify on %s\n", __func__, objname);
1462 dbg("%s: re-enumerating slots under %s\n",
1464 acpiphp_check_bridge(bridge);
1466 if (num_sub_bridges)
1467 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1468 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1471 case ACPI_NOTIFY_DEVICE_CHECK:
1473 dbg("%s: Device check notify on %s\n", __func__, objname);
1474 acpiphp_check_bridge(bridge);
1477 case ACPI_NOTIFY_DEVICE_WAKE:
1479 dbg("%s: Device wake notify on %s\n", __func__, objname);
1482 case ACPI_NOTIFY_EJECT_REQUEST:
1483 /* request device eject */
1484 dbg("%s: Device eject notify on %s\n", __func__, objname);
1485 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1486 (bridge->flags & BRIDGE_HAS_EJ0)) {
1487 struct acpiphp_slot *slot;
1488 slot = bridge->func->slot;
1489 if (!acpiphp_disable_slot(slot))
1490 acpiphp_eject_slot(slot);
1494 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1495 printk(KERN_ERR "Device %s cannot be configured due"
1496 " to a frequency mismatch\n", objname);
1499 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1500 printk(KERN_ERR "Device %s cannot be configured due"
1501 " to a bus mode mismatch\n", objname);
1504 case ACPI_NOTIFY_POWER_FAULT:
1505 printk(KERN_ERR "Device %s has suffered a power fault\n",
1510 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1516 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1517 * @handle: Notify()'ed acpi_handle
1518 * @type: Notify code
1519 * @context: pointer to acpiphp_func structure
1521 * Handles ACPI event notification on slots.
1523 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1525 struct acpiphp_func *func;
1527 struct acpi_buffer buffer = { .length = sizeof(objname),
1528 .pointer = objname };
1530 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1532 func = (struct acpiphp_func *)context;
1535 case ACPI_NOTIFY_BUS_CHECK:
1536 /* bus re-enumerate */
1537 dbg("%s: Bus check notify on %s\n", __func__, objname);
1538 acpiphp_enable_slot(func->slot);
1541 case ACPI_NOTIFY_DEVICE_CHECK:
1542 /* device check : re-enumerate from parent bus */
1543 dbg("%s: Device check notify on %s\n", __func__, objname);
1544 acpiphp_check_bridge(func->slot->bridge);
1547 case ACPI_NOTIFY_DEVICE_WAKE:
1549 dbg("%s: Device wake notify on %s\n", __func__, objname);
1552 case ACPI_NOTIFY_EJECT_REQUEST:
1553 /* request device eject */
1554 dbg("%s: Device eject notify on %s\n", __func__, objname);
1555 if (!(acpiphp_disable_slot(func->slot)))
1556 acpiphp_eject_slot(func->slot);
1560 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1567 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1569 int *count = (int *)context;
1571 if (acpi_is_root_bridge(handle)) {
1572 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1573 handle_hotplug_event_bridge, NULL);
1579 static struct acpi_pci_driver acpi_pci_hp_driver = {
1581 .remove = remove_bridge,
1585 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1587 int __init acpiphp_glue_init(void)
1591 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1592 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1597 acpi_pci_register_driver(&acpi_pci_hp_driver);
1604 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1606 * This function frees all data allocated in acpiphp_glue_init().
1608 void acpiphp_glue_exit(void)
1610 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1615 * acpiphp_get_num_slots - count number of slots in a system
1617 int __init acpiphp_get_num_slots(void)
1619 struct acpiphp_bridge *bridge;
1622 list_for_each_entry (bridge, &bridge_list, list) {
1623 dbg("Bus %04x:%02x has %d slot%s\n",
1624 pci_domain_nr(bridge->pci_bus),
1625 bridge->pci_bus->number, bridge->nr_slots,
1626 bridge->nr_slots == 1 ? "" : "s");
1627 num_slots += bridge->nr_slots;
1630 dbg("Total %d slots\n", num_slots);
1637 * acpiphp_for_each_slot - call function for each slot
1638 * @fn: callback function
1639 * @data: context to be passed to callback function
1641 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1643 struct list_head *node;
1644 struct acpiphp_bridge *bridge;
1645 struct acpiphp_slot *slot;
1648 list_for_each (node, &bridge_list) {
1649 bridge = (struct acpiphp_bridge *)node;
1650 for (slot = bridge->slots; slot; slot = slot->next) {
1651 retval = fn(slot, data);
1664 * acpiphp_enable_slot - power on slot
1665 * @slot: ACPI PHP slot
1667 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1671 mutex_lock(&slot->crit_sect);
1673 /* wake up all functions */
1674 retval = power_on_slot(slot);
1678 if (get_slot_status(slot) == ACPI_STA_ALL) {
1679 /* configure all functions */
1680 retval = enable_device(slot);
1682 power_off_slot(slot);
1684 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1685 power_off_slot(slot);
1689 mutex_unlock(&slot->crit_sect);
1694 * acpiphp_disable_slot - power off slot
1695 * @slot: ACPI PHP slot
1697 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1701 mutex_lock(&slot->crit_sect);
1703 /* unconfigure all functions */
1704 retval = disable_device(slot);
1708 /* power off all functions */
1709 retval = power_off_slot(slot);
1714 mutex_unlock(&slot->crit_sect);
1723 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1725 return (slot->flags & SLOT_POWEREDON);
1733 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1737 sta = get_slot_status(slot);
1739 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1744 * adapter presence : 1
1747 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1751 sta = get_slot_status(slot);
1753 return (sta == 0) ? 0 : 1;