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(acpi_handle handle, 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 acpiphp_bridge *bridge)
280 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
281 if (ACPI_FAILURE(status) ||
282 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
283 /* use default numbers */
285 "%s: Could not get hotplug parameters. Use defaults\n",
287 bridge->hpp.t0 = &bridge->hpp.type0_data;
288 bridge->hpp.t0->revision = 0;
289 bridge->hpp.t0->cache_line_size = 0x10;
290 bridge->hpp.t0->latency_timer = 0x40;
291 bridge->hpp.t0->enable_serr = 0;
292 bridge->hpp.t0->enable_perr = 0;
298 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
299 static void init_bridge_misc(struct acpiphp_bridge *bridge)
303 /* must be added to the list prior to calling register_slot */
304 list_add(&bridge->list, &bridge_list);
306 /* register all slot objects under this bridge */
307 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
308 register_slot, bridge, NULL);
309 if (ACPI_FAILURE(status)) {
310 list_del(&bridge->list);
314 /* install notify handler */
315 if (bridge->type != BRIDGE_TYPE_HOST) {
316 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
317 status = acpi_remove_notify_handler(bridge->func->handle,
319 handle_hotplug_event_func);
320 if (ACPI_FAILURE(status))
321 err("failed to remove notify handler\n");
323 status = acpi_install_notify_handler(bridge->handle,
325 handle_hotplug_event_bridge,
328 if (ACPI_FAILURE(status)) {
329 err("failed to register interrupt notify handler\n");
335 /* find acpiphp_func from acpiphp_bridge */
336 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
338 struct list_head *node, *l;
339 struct acpiphp_bridge *bridge;
340 struct acpiphp_slot *slot;
341 struct acpiphp_func *func;
343 list_for_each(node, &bridge_list) {
344 bridge = list_entry(node, struct acpiphp_bridge, list);
345 for (slot = bridge->slots; slot; slot = slot->next) {
346 list_for_each(l, &slot->funcs) {
347 func = list_entry(l, struct acpiphp_func,
349 if (func->handle == handle)
359 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
361 acpi_handle dummy_handle;
363 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
364 "_STA", &dummy_handle)))
365 bridge->flags |= BRIDGE_HAS_STA;
367 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
368 "_EJ0", &dummy_handle)))
369 bridge->flags |= BRIDGE_HAS_EJ0;
371 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
372 "_PS0", &dummy_handle)))
373 bridge->flags |= BRIDGE_HAS_PS0;
375 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
376 "_PS3", &dummy_handle)))
377 bridge->flags |= BRIDGE_HAS_PS3;
379 /* is this ejectable p2p bridge? */
380 if (bridge->flags & BRIDGE_HAS_EJ0) {
381 struct acpiphp_func *func;
383 dbg("found ejectable p2p bridge\n");
385 /* make link between PCI bridge and PCI function */
386 func = acpiphp_bridge_handle_to_function(bridge->handle);
390 func->bridge = bridge;
395 /* allocate and initialize host bridge data structure */
396 static void add_host_bridge(acpi_handle *handle)
398 struct acpiphp_bridge *bridge;
399 struct acpi_pci_root *root = acpi_pci_find_root(handle);
401 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
405 bridge->type = BRIDGE_TYPE_HOST;
406 bridge->handle = handle;
408 bridge->pci_bus = root->bus;
410 spin_lock_init(&bridge->res_lock);
412 init_bridge_misc(bridge);
416 /* allocate and initialize PCI-to-PCI bridge data structure */
417 static void add_p2p_bridge(acpi_handle *handle)
419 struct acpiphp_bridge *bridge;
421 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
422 if (bridge == NULL) {
423 err("out of memory\n");
427 bridge->type = BRIDGE_TYPE_P2P;
428 bridge->handle = handle;
429 config_p2p_bridge_flags(bridge);
431 bridge->pci_dev = acpi_get_pci_dev(handle);
432 bridge->pci_bus = bridge->pci_dev->subordinate;
433 if (!bridge->pci_bus) {
434 err("This is not a PCI-to-PCI bridge!\n");
439 * Grab a ref to the subordinate PCI bus in case the bus is
440 * removed via PCI core logical hotplug. The ref pins the bus
441 * (which we access during module unload).
443 get_device(&bridge->pci_bus->dev);
444 spin_lock_init(&bridge->res_lock);
446 init_bridge_misc(bridge);
449 pci_dev_put(bridge->pci_dev);
455 /* callback routine to find P2P bridges */
457 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
462 dev = acpi_get_pci_dev(handle);
463 if (!dev || !dev->subordinate)
466 /* check if this bridge has ejectable slots */
467 if ((detect_ejectable_slots(handle) > 0)) {
468 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
469 add_p2p_bridge(handle);
472 /* search P2P bridges under this p2p bridge */
473 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
474 find_p2p_bridge, NULL, NULL);
475 if (ACPI_FAILURE(status))
476 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
484 /* find hot-pluggable slots, and then find P2P bridge */
485 static int add_bridge(acpi_handle handle)
488 unsigned long long tmp;
489 acpi_handle dummy_handle;
491 /* if the bridge doesn't have _STA, we assume it is always there */
492 status = acpi_get_handle(handle, "_STA", &dummy_handle);
493 if (ACPI_SUCCESS(status)) {
494 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
495 if (ACPI_FAILURE(status)) {
496 dbg("%s: _STA evaluation failure\n", __func__);
499 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
500 /* don't register this object */
504 /* check if this bridge has ejectable slots */
505 if (detect_ejectable_slots(handle) > 0) {
506 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
507 add_host_bridge(handle);
510 /* search P2P bridges under this host bridge */
511 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
512 find_p2p_bridge, NULL, NULL);
514 if (ACPI_FAILURE(status))
515 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
520 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
522 struct list_head *head;
523 list_for_each(head, &bridge_list) {
524 struct acpiphp_bridge *bridge = list_entry(head,
525 struct acpiphp_bridge, list);
526 if (bridge->handle == handle)
533 static void cleanup_bridge(struct acpiphp_bridge *bridge)
535 struct list_head *list, *tmp;
536 struct acpiphp_slot *slot;
538 acpi_handle handle = bridge->handle;
540 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
541 handle_hotplug_event_bridge);
542 if (ACPI_FAILURE(status))
543 err("failed to remove notify handler\n");
545 if ((bridge->type != BRIDGE_TYPE_HOST) &&
546 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
547 status = acpi_install_notify_handler(bridge->func->handle,
549 handle_hotplug_event_func,
551 if (ACPI_FAILURE(status))
552 err("failed to install interrupt notify handler\n");
555 slot = bridge->slots;
557 struct acpiphp_slot *next = slot->next;
558 list_for_each_safe (list, tmp, &slot->funcs) {
559 struct acpiphp_func *func;
560 func = list_entry(list, struct acpiphp_func, sibling);
561 if (is_dock_device(func->handle)) {
562 unregister_hotplug_dock_device(func->handle);
563 unregister_dock_notifier(&func->nb);
565 if (!(func->flags & FUNC_HAS_DCK)) {
566 status = acpi_remove_notify_handler(func->handle,
568 handle_hotplug_event_func);
569 if (ACPI_FAILURE(status))
570 err("failed to remove notify handler\n");
575 acpiphp_unregister_hotplug_slot(slot);
576 list_del(&slot->funcs);
582 * Only P2P bridges have a pci_dev
585 put_device(&bridge->pci_bus->dev);
587 pci_dev_put(bridge->pci_dev);
588 list_del(&bridge->list);
593 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
595 struct acpiphp_bridge *bridge;
597 /* cleanup p2p bridges under this P2P bridge
598 in a depth-first manner */
599 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
600 cleanup_p2p_bridge, NULL, NULL);
602 bridge = acpiphp_handle_to_bridge(handle);
604 cleanup_bridge(bridge);
609 static void remove_bridge(acpi_handle handle)
611 struct acpiphp_bridge *bridge;
613 /* cleanup p2p bridges under this host bridge
614 in a depth-first manner */
615 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
616 (u32)1, cleanup_p2p_bridge, NULL, NULL);
619 * On root bridges with hotplug slots directly underneath (ie,
620 * no p2p bridge inbetween), we call cleanup_bridge().
622 * The else clause cleans up root bridges that either had no
623 * hotplug slots at all, or had a p2p bridge underneath.
625 bridge = acpiphp_handle_to_bridge(handle);
627 cleanup_bridge(bridge);
629 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
630 handle_hotplug_event_bridge);
633 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
637 dev = acpi_get_pci_dev(handle);
641 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
642 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
651 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
655 unsigned long long gsb;
656 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
657 union acpi_object *obj;
660 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
661 if (ACPI_SUCCESS(status)) {
662 *gsi_base = (u32)gsb;
666 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
667 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
670 obj = buffer.pointer;
671 if (obj->type != ACPI_TYPE_BUFFER)
674 table = obj->buffer.pointer;
675 switch (((struct acpi_subtable_header *)table)->type) {
676 case ACPI_MADT_TYPE_IO_SAPIC:
677 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
680 case ACPI_MADT_TYPE_IO_APIC:
681 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
688 kfree(buffer.pointer);
693 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
696 unsigned long long sta;
698 struct pci_dev *pdev;
701 struct acpiphp_ioapic *ioapic;
703 /* Evaluate _STA if present */
704 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
705 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
706 return AE_CTRL_DEPTH;
708 /* Scan only PCI bus scope */
709 status = acpi_get_handle(handle, "_HID", &tmp);
710 if (ACPI_SUCCESS(status))
711 return AE_CTRL_DEPTH;
713 if (get_gsi_base(handle, &gsi_base))
716 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
720 pdev = get_apic_pci_info(handle);
724 if (pci_enable_device(pdev))
725 goto exit_pci_dev_put;
727 pci_set_master(pdev);
729 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
730 goto exit_pci_disable_device;
732 phys_addr = pci_resource_start(pdev, 0);
733 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
734 goto exit_pci_release_region;
736 ioapic->gsi_base = gsi_base;
738 spin_lock(&ioapic_list_lock);
739 list_add_tail(&ioapic->list, &ioapic_list);
740 spin_unlock(&ioapic_list_lock);
744 exit_pci_release_region:
745 pci_release_region(pdev, 0);
746 exit_pci_disable_device:
747 pci_disable_device(pdev);
757 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
760 unsigned long long sta;
763 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
765 /* Evaluate _STA if present */
766 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
767 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
768 return AE_CTRL_DEPTH;
770 /* Scan only PCI bus scope */
771 status = acpi_get_handle(handle, "_HID", &tmp);
772 if (ACPI_SUCCESS(status))
773 return AE_CTRL_DEPTH;
775 if (get_gsi_base(handle, &gsi_base))
778 acpi_unregister_ioapic(handle, gsi_base);
780 spin_lock(&ioapic_list_lock);
781 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
782 if (pos->gsi_base != gsi_base)
785 list_del(&ioapic->list);
788 spin_unlock(&ioapic_list_lock);
793 pci_release_region(ioapic->dev, 0);
794 pci_disable_device(ioapic->dev);
795 pci_dev_put(ioapic->dev);
801 static int acpiphp_configure_ioapics(acpi_handle handle)
803 ioapic_add(handle, 0, NULL, NULL);
804 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
805 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
809 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
811 ioapic_remove(handle, 0, NULL, NULL);
812 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
813 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
817 static int power_on_slot(struct acpiphp_slot *slot)
820 struct acpiphp_func *func;
824 /* if already enabled, just skip */
825 if (slot->flags & SLOT_POWEREDON)
828 list_for_each (l, &slot->funcs) {
829 func = list_entry(l, struct acpiphp_func, sibling);
831 if (func->flags & FUNC_HAS_PS0) {
832 dbg("%s: executing _PS0\n", __func__);
833 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
834 if (ACPI_FAILURE(status)) {
835 warn("%s: _PS0 failed\n", __func__);
843 /* TBD: evaluate _STA to check if the slot is enabled */
845 slot->flags |= SLOT_POWEREDON;
852 static int power_off_slot(struct acpiphp_slot *slot)
855 struct acpiphp_func *func;
860 /* if already disabled, just skip */
861 if ((slot->flags & SLOT_POWEREDON) == 0)
864 list_for_each (l, &slot->funcs) {
865 func = list_entry(l, struct acpiphp_func, sibling);
867 if (func->flags & FUNC_HAS_PS3) {
868 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
869 if (ACPI_FAILURE(status)) {
870 warn("%s: _PS3 failed\n", __func__);
878 /* TBD: evaluate _STA to check if the slot is disabled */
880 slot->flags &= (~SLOT_POWEREDON);
889 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
890 * @bus: bus to start search with
892 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
894 struct list_head *tmp;
895 unsigned char max, n;
898 * pci_bus_max_busnr will return the highest
899 * reserved busnr for all these children.
900 * that is equivalent to the bus->subordinate
901 * value. We don't want to use the parent's
902 * bus->subordinate value because it could have
905 max = bus->secondary;
907 list_for_each(tmp, &bus->children) {
908 n = pci_bus_max_busnr(pci_bus_b(tmp));
917 * acpiphp_bus_add - add a new bus to acpi subsystem
918 * @func: acpiphp_func of the bridge
920 static int acpiphp_bus_add(struct acpiphp_func *func)
923 struct acpi_device *device, *pdevice;
926 acpi_get_parent(func->handle, &phandle);
927 if (acpi_bus_get_device(phandle, &pdevice)) {
928 dbg("no parent device, assuming NULL\n");
931 if (!acpi_bus_get_device(func->handle, &device)) {
932 dbg("bus exists... trim\n");
933 /* this shouldn't be in here, so remove
934 * the bus then re-add it...
936 ret_val = acpi_bus_trim(device, 1);
937 dbg("acpi_bus_trim return %x\n", ret_val);
940 ret_val = acpi_bus_add(&device, pdevice, func->handle,
941 ACPI_BUS_TYPE_DEVICE);
943 dbg("error adding bus, %x\n",
945 goto acpiphp_bus_add_out;
948 * try to start anyway. We could have failed to add
949 * simply because this bus had previously been added
950 * on another add. Don't bother with the return value
951 * we just keep going.
953 ret_val = acpi_bus_start(device);
961 * acpiphp_bus_trim - trim a bus from acpi subsystem
962 * @handle: handle to acpi namespace
964 static int acpiphp_bus_trim(acpi_handle handle)
966 struct acpi_device *device;
969 retval = acpi_bus_get_device(handle, &device);
971 dbg("acpi_device not found\n");
975 retval = acpi_bus_trim(device, 1);
977 err("cannot remove from acpi list\n");
983 * enable_device - enable, configure a slot
984 * @slot: slot to be enabled
986 * This function should be called per *physical slot*,
987 * not per each slot object in ACPI namespace.
989 static int __ref enable_device(struct acpiphp_slot *slot)
992 struct pci_bus *bus = slot->bridge->pci_bus;
994 struct acpiphp_func *func;
999 if (slot->flags & SLOT_ENABLED)
1002 /* sanity check: dev should be NULL when hot-plugged in */
1003 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1005 /* This case shouldn't happen */
1006 err("pci_dev structure already exists.\n");
1012 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1014 err("No new device found\n");
1019 max = acpiphp_max_busnr(bus);
1020 for (pass = 0; pass < 2; pass++) {
1021 list_for_each_entry(dev, &bus->devices, bus_list) {
1022 if (PCI_SLOT(dev->devfn) != slot->device)
1024 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1025 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1026 max = pci_scan_bridge(bus, dev, max, pass);
1027 if (pass && dev->subordinate)
1028 pci_bus_size_bridges(dev->subordinate);
1033 list_for_each (l, &slot->funcs) {
1034 func = list_entry(l, struct acpiphp_func, sibling);
1035 acpiphp_bus_add(func);
1038 pci_bus_assign_resources(bus);
1039 acpiphp_sanitize_bus(bus);
1040 acpiphp_set_hpp_values(slot->bridge->handle, bus);
1041 list_for_each_entry(func, &slot->funcs, sibling)
1042 acpiphp_configure_ioapics(func->handle);
1043 pci_enable_bridges(bus);
1044 pci_bus_add_devices(bus);
1046 list_for_each (l, &slot->funcs) {
1047 func = list_entry(l, struct acpiphp_func, sibling);
1048 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1053 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1054 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1059 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1060 if (ACPI_FAILURE(status))
1061 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1066 slot->flags |= SLOT_ENABLED;
1072 static void disable_bridges(struct pci_bus *bus)
1074 struct pci_dev *dev;
1075 list_for_each_entry(dev, &bus->devices, bus_list) {
1076 if (dev->subordinate) {
1077 disable_bridges(dev->subordinate);
1078 pci_disable_device(dev);
1084 * disable_device - disable a slot
1085 * @slot: ACPI PHP slot
1087 static int disable_device(struct acpiphp_slot *slot)
1089 struct acpiphp_func *func;
1090 struct pci_dev *pdev;
1092 /* is this slot already disabled? */
1093 if (!(slot->flags & SLOT_ENABLED))
1096 list_for_each_entry(func, &slot->funcs, sibling) {
1098 /* cleanup p2p bridges under this P2P bridge */
1099 cleanup_p2p_bridge(func->bridge->handle,
1100 (u32)1, NULL, NULL);
1101 func->bridge = NULL;
1104 pdev = pci_get_slot(slot->bridge->pci_bus,
1105 PCI_DEVFN(slot->device, func->function));
1107 pci_stop_bus_device(pdev);
1108 if (pdev->subordinate) {
1109 disable_bridges(pdev->subordinate);
1110 pci_disable_device(pdev);
1112 pci_remove_bus_device(pdev);
1117 list_for_each_entry(func, &slot->funcs, sibling) {
1118 acpiphp_unconfigure_ioapics(func->handle);
1119 acpiphp_bus_trim(func->handle);
1122 slot->flags &= (~SLOT_ENABLED);
1130 * get_slot_status - get ACPI slot status
1131 * @slot: ACPI PHP slot
1133 * If a slot has _STA for each function and if any one of them
1134 * returned non-zero status, return it.
1136 * If a slot doesn't have _STA and if any one of its functions'
1137 * configuration space is configured, return 0x0f as a _STA.
1139 * Otherwise return 0.
1141 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1144 unsigned long long sta = 0;
1146 struct list_head *l;
1147 struct acpiphp_func *func;
1149 list_for_each (l, &slot->funcs) {
1150 func = list_entry(l, struct acpiphp_func, sibling);
1152 if (func->flags & FUNC_HAS_STA) {
1153 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1154 if (ACPI_SUCCESS(status) && sta)
1157 pci_bus_read_config_dword(slot->bridge->pci_bus,
1158 PCI_DEVFN(slot->device,
1160 PCI_VENDOR_ID, &dvid);
1161 if (dvid != 0xffffffff) {
1168 return (unsigned int)sta;
1172 * acpiphp_eject_slot - physically eject the slot
1173 * @slot: ACPI PHP slot
1175 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1178 struct acpiphp_func *func;
1179 struct list_head *l;
1180 struct acpi_object_list arg_list;
1181 union acpi_object arg;
1183 list_for_each (l, &slot->funcs) {
1184 func = list_entry(l, struct acpiphp_func, sibling);
1186 /* We don't want to call _EJ0 on non-existing functions. */
1187 if ((func->flags & FUNC_HAS_EJ0)) {
1188 /* _EJ0 method take one argument */
1190 arg_list.pointer = &arg;
1191 arg.type = ACPI_TYPE_INTEGER;
1192 arg.integer.value = 1;
1194 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1195 if (ACPI_FAILURE(status)) {
1196 warn("%s: _EJ0 failed\n", __func__);
1206 * acpiphp_check_bridge - re-enumerate devices
1207 * @bridge: where to begin re-enumeration
1209 * Iterate over all slots under this bridge and make sure that if a
1210 * card is present they are enabled, and if not they are disabled.
1212 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1214 struct acpiphp_slot *slot;
1216 int enabled, disabled;
1218 enabled = disabled = 0;
1220 for (slot = bridge->slots; slot; slot = slot->next) {
1221 unsigned int status = get_slot_status(slot);
1222 if (slot->flags & SLOT_ENABLED) {
1223 if (status == ACPI_STA_ALL)
1225 retval = acpiphp_disable_slot(slot);
1227 err("Error occurred in disabling\n");
1230 acpiphp_eject_slot(slot);
1234 if (status != ACPI_STA_ALL)
1236 retval = acpiphp_enable_slot(slot);
1238 err("Error occurred in enabling\n");
1245 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1251 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1253 u16 pci_cmd, pci_bctl;
1254 struct pci_dev *cdev;
1256 /* Program hpp values for this device */
1257 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1258 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1259 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1262 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1265 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1266 bridge->hpp.t0->cache_line_size);
1267 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1268 bridge->hpp.t0->latency_timer);
1269 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1270 if (bridge->hpp.t0->enable_serr)
1271 pci_cmd |= PCI_COMMAND_SERR;
1273 pci_cmd &= ~PCI_COMMAND_SERR;
1274 if (bridge->hpp.t0->enable_perr)
1275 pci_cmd |= PCI_COMMAND_PARITY;
1277 pci_cmd &= ~PCI_COMMAND_PARITY;
1278 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1280 /* Program bridge control value and child devices */
1281 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1282 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1283 bridge->hpp.t0->latency_timer);
1284 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1285 if (bridge->hpp.t0->enable_serr)
1286 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1288 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1289 if (bridge->hpp.t0->enable_perr)
1290 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1292 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1293 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1294 if (dev->subordinate) {
1295 list_for_each_entry(cdev, &dev->subordinate->devices,
1297 program_hpp(cdev, bridge);
1302 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1304 struct acpiphp_bridge bridge;
1305 struct pci_dev *dev;
1307 memset(&bridge, 0, sizeof(bridge));
1308 bridge.handle = handle;
1309 bridge.pci_bus = bus;
1310 bridge.pci_dev = bus->self;
1311 decode_hpp(&bridge);
1312 list_for_each_entry(dev, &bus->devices, bus_list)
1313 program_hpp(dev, &bridge);
1318 * Remove devices for which we could not assign resources, call
1319 * arch specific code to fix-up the bus
1321 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1323 struct pci_dev *dev;
1325 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1327 list_for_each_entry(dev, &bus->devices, bus_list) {
1328 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1329 struct resource *res = &dev->resource[i];
1330 if ((res->flags & type_mask) && !res->start &&
1332 /* Could not assign a required resources
1333 * for this device, remove it */
1334 pci_remove_bus_device(dev);
1341 /* Program resources in newly inserted bridge */
1342 static int acpiphp_configure_bridge (acpi_handle handle)
1344 struct pci_bus *bus;
1346 if (acpi_is_root_bridge(handle)) {
1347 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1350 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1351 bus = pdev->subordinate;
1355 pci_bus_size_bridges(bus);
1356 pci_bus_assign_resources(bus);
1357 acpiphp_sanitize_bus(bus);
1358 acpiphp_set_hpp_values(handle, bus);
1359 pci_enable_bridges(bus);
1360 acpiphp_configure_ioapics(handle);
1364 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1366 struct acpi_device *device, *pdevice;
1367 acpi_handle phandle;
1369 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1370 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1371 err("unexpected notification type %d\n", type);
1375 acpi_get_parent(handle, &phandle);
1376 if (acpi_bus_get_device(phandle, &pdevice)) {
1377 dbg("no parent device, assuming NULL\n");
1380 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1381 err("cannot add bridge to acpi list\n");
1384 if (!acpiphp_configure_bridge(handle) &&
1385 !acpi_bus_start(device))
1388 err("cannot configure and start bridge\n");
1393 * ACPI event handlers
1397 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1399 int *count = (int *)context;
1400 struct acpiphp_bridge *bridge;
1402 bridge = acpiphp_handle_to_bridge(handle);
1409 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1411 struct acpiphp_bridge *bridge;
1413 struct acpi_buffer buffer = { .length = sizeof(objname),
1414 .pointer = objname };
1416 bridge = acpiphp_handle_to_bridge(handle);
1418 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1419 dbg("%s: re-enumerating slots under %s\n",
1421 acpiphp_check_bridge(bridge);
1427 * handle_hotplug_event_bridge - handle ACPI event on bridges
1428 * @handle: Notify()'ed acpi_handle
1429 * @type: Notify code
1430 * @context: pointer to acpiphp_bridge structure
1432 * Handles ACPI event notification on {host,p2p} bridges.
1434 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1436 struct acpiphp_bridge *bridge;
1438 struct acpi_buffer buffer = { .length = sizeof(objname),
1439 .pointer = objname };
1440 struct acpi_device *device;
1441 int num_sub_bridges = 0;
1443 if (acpi_bus_get_device(handle, &device)) {
1444 /* This bridge must have just been physically inserted */
1445 handle_bridge_insertion(handle, type);
1449 bridge = acpiphp_handle_to_bridge(handle);
1450 if (type == ACPI_NOTIFY_BUS_CHECK) {
1451 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1452 count_sub_bridges, &num_sub_bridges, NULL);
1455 if (!bridge && !num_sub_bridges) {
1456 err("cannot get bridge info\n");
1460 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1463 case ACPI_NOTIFY_BUS_CHECK:
1464 /* bus re-enumerate */
1465 dbg("%s: Bus check notify on %s\n", __func__, objname);
1467 dbg("%s: re-enumerating slots under %s\n",
1469 acpiphp_check_bridge(bridge);
1471 if (num_sub_bridges)
1472 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1473 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1476 case ACPI_NOTIFY_DEVICE_CHECK:
1478 dbg("%s: Device check notify on %s\n", __func__, objname);
1479 acpiphp_check_bridge(bridge);
1482 case ACPI_NOTIFY_DEVICE_WAKE:
1484 dbg("%s: Device wake notify on %s\n", __func__, objname);
1487 case ACPI_NOTIFY_EJECT_REQUEST:
1488 /* request device eject */
1489 dbg("%s: Device eject notify on %s\n", __func__, objname);
1490 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1491 (bridge->flags & BRIDGE_HAS_EJ0)) {
1492 struct acpiphp_slot *slot;
1493 slot = bridge->func->slot;
1494 if (!acpiphp_disable_slot(slot))
1495 acpiphp_eject_slot(slot);
1499 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1500 printk(KERN_ERR "Device %s cannot be configured due"
1501 " to a frequency mismatch\n", objname);
1504 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1505 printk(KERN_ERR "Device %s cannot be configured due"
1506 " to a bus mode mismatch\n", objname);
1509 case ACPI_NOTIFY_POWER_FAULT:
1510 printk(KERN_ERR "Device %s has suffered a power fault\n",
1515 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1521 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1522 * @handle: Notify()'ed acpi_handle
1523 * @type: Notify code
1524 * @context: pointer to acpiphp_func structure
1526 * Handles ACPI event notification on slots.
1528 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1530 struct acpiphp_func *func;
1532 struct acpi_buffer buffer = { .length = sizeof(objname),
1533 .pointer = objname };
1535 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1537 func = (struct acpiphp_func *)context;
1540 case ACPI_NOTIFY_BUS_CHECK:
1541 /* bus re-enumerate */
1542 dbg("%s: Bus check notify on %s\n", __func__, objname);
1543 acpiphp_enable_slot(func->slot);
1546 case ACPI_NOTIFY_DEVICE_CHECK:
1547 /* device check : re-enumerate from parent bus */
1548 dbg("%s: Device check notify on %s\n", __func__, objname);
1549 acpiphp_check_bridge(func->slot->bridge);
1552 case ACPI_NOTIFY_DEVICE_WAKE:
1554 dbg("%s: Device wake notify on %s\n", __func__, objname);
1557 case ACPI_NOTIFY_EJECT_REQUEST:
1558 /* request device eject */
1559 dbg("%s: Device eject notify on %s\n", __func__, objname);
1560 if (!(acpiphp_disable_slot(func->slot)))
1561 acpiphp_eject_slot(func->slot);
1565 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1572 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1574 int *count = (int *)context;
1576 if (acpi_is_root_bridge(handle)) {
1577 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1578 handle_hotplug_event_bridge, NULL);
1584 static struct acpi_pci_driver acpi_pci_hp_driver = {
1586 .remove = remove_bridge,
1590 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1592 int __init acpiphp_glue_init(void)
1596 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1597 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1602 acpi_pci_register_driver(&acpi_pci_hp_driver);
1609 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1611 * This function frees all data allocated in acpiphp_glue_init().
1613 void acpiphp_glue_exit(void)
1615 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1620 * acpiphp_get_num_slots - count number of slots in a system
1622 int __init acpiphp_get_num_slots(void)
1624 struct acpiphp_bridge *bridge;
1627 list_for_each_entry (bridge, &bridge_list, list) {
1628 dbg("Bus %04x:%02x has %d slot%s\n",
1629 pci_domain_nr(bridge->pci_bus),
1630 bridge->pci_bus->number, bridge->nr_slots,
1631 bridge->nr_slots == 1 ? "" : "s");
1632 num_slots += bridge->nr_slots;
1635 dbg("Total %d slots\n", num_slots);
1642 * acpiphp_for_each_slot - call function for each slot
1643 * @fn: callback function
1644 * @data: context to be passed to callback function
1646 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1648 struct list_head *node;
1649 struct acpiphp_bridge *bridge;
1650 struct acpiphp_slot *slot;
1653 list_for_each (node, &bridge_list) {
1654 bridge = (struct acpiphp_bridge *)node;
1655 for (slot = bridge->slots; slot; slot = slot->next) {
1656 retval = fn(slot, data);
1669 * acpiphp_enable_slot - power on slot
1670 * @slot: ACPI PHP slot
1672 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1676 mutex_lock(&slot->crit_sect);
1678 /* wake up all functions */
1679 retval = power_on_slot(slot);
1683 if (get_slot_status(slot) == ACPI_STA_ALL) {
1684 /* configure all functions */
1685 retval = enable_device(slot);
1687 power_off_slot(slot);
1689 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1690 power_off_slot(slot);
1694 mutex_unlock(&slot->crit_sect);
1699 * acpiphp_disable_slot - power off slot
1700 * @slot: ACPI PHP slot
1702 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1706 mutex_lock(&slot->crit_sect);
1708 /* unconfigure all functions */
1709 retval = disable_device(slot);
1713 /* power off all functions */
1714 retval = power_off_slot(slot);
1719 mutex_unlock(&slot->crit_sect);
1728 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1730 return (slot->flags & SLOT_POWEREDON);
1738 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1742 sta = get_slot_status(slot);
1744 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1749 * adapter presence : 1
1752 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1756 sta = get_slot_status(slot);
1758 return (sta == 0) ? 0 : 1;