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 = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
149 memset(newfunc, 0, sizeof(struct acpiphp_func));
151 INIT_LIST_HEAD(&newfunc->sibling);
152 newfunc->handle = handle;
153 newfunc->function = function;
154 if (ACPI_SUCCESS(status))
155 newfunc->flags = FUNC_HAS_EJ0;
157 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
158 newfunc->flags |= FUNC_HAS_STA;
160 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
161 newfunc->flags |= FUNC_HAS_PS0;
163 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
164 newfunc->flags |= FUNC_HAS_PS3;
166 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) {
167 newfunc->flags |= FUNC_HAS_DCK;
168 /* add to devices dependent on dock station,
169 * because this may actually be the dock bridge
171 dd = alloc_dependent_device(handle);
173 err("Can't allocate memory for "
174 "new dependent device!\n");
176 add_dependent_device(dd);
179 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
180 if (ACPI_FAILURE(status))
183 /* search for objects that share the same slot */
184 for (slot = bridge->slots; slot; slot = slot->next)
185 if (slot->device == device) {
186 if (slot->sun != sun)
187 warn("sibling found, but _SUN doesn't match!\n");
192 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
198 memset(slot, 0, sizeof(struct acpiphp_slot));
199 slot->bridge = bridge;
200 slot->device = device;
202 INIT_LIST_HEAD(&slot->funcs);
203 mutex_init(&slot->crit_sect);
205 slot->next = bridge->slots;
206 bridge->slots = slot;
210 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
211 slot->sun, pci_domain_nr(bridge->pci_bus),
212 bridge->pci_bus->number, slot->device);
213 retval = acpiphp_register_hotplug_slot(slot);
215 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
220 newfunc->slot = slot;
221 list_add_tail(&newfunc->sibling, &slot->funcs);
223 /* associate corresponding pci_dev */
224 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
225 PCI_DEVFN(device, function));
226 if (newfunc->pci_dev) {
227 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
230 /* if this is a device dependent on a dock station,
231 * associate the acpiphp_func to the dependent_device
234 if ((dd = get_dependent_device(handle))) {
235 newfunc->flags |= FUNC_IS_DD;
237 * we don't want any devices which is dependent
238 * on the dock to have it's _EJ0 method executed.
239 * because we need to run _DCK first.
241 newfunc->flags &= ~FUNC_HAS_EJ0;
243 add_pci_dependent_device(dd);
246 /* install notify handler */
247 if (!(newfunc->flags & FUNC_HAS_DCK)) {
248 status = acpi_install_notify_handler(handle,
250 handle_hotplug_event_func,
253 if (ACPI_FAILURE(status))
254 err("failed to register interrupt notify handler\n");
262 bridge->slots = slot->next;
270 /* see if it's worth looking at this bridge */
271 static int detect_ejectable_slots(acpi_handle *bridge_handle)
278 /* only check slots defined directly below bridge object */
279 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
280 is_ejectable_slot, (void *)&count, NULL);
286 /* decode ACPI 2.0 _HPP hot plug parameters */
287 static void decode_hpp(struct acpiphp_bridge *bridge)
290 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
292 union acpi_object *package;
295 /* default numbers */
296 bridge->hpp.cache_line_size = 0x10;
297 bridge->hpp.latency_timer = 0x40;
298 bridge->hpp.enable_SERR = 0;
299 bridge->hpp.enable_PERR = 0;
301 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
303 if (ACPI_FAILURE(status)) {
304 dbg("_HPP evaluation failed\n");
308 package = (union acpi_object *) buffer.pointer;
310 if (!package || package->type != ACPI_TYPE_PACKAGE ||
311 package->package.count != 4 || !package->package.elements) {
312 err("invalid _HPP object; ignoring\n");
316 for (i = 0; i < 4; i++) {
317 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
318 err("invalid _HPP parameter type; ignoring\n");
323 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
324 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
325 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
326 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
328 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
329 bridge->hpp.cache_line_size,
330 bridge->hpp.latency_timer,
331 bridge->hpp.enable_SERR,
332 bridge->hpp.enable_PERR);
334 bridge->flags |= BRIDGE_HAS_HPP;
337 kfree(buffer.pointer);
341 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
342 static void init_bridge_misc(struct acpiphp_bridge *bridge)
346 /* decode ACPI 2.0 _HPP (hot plug parameters) */
349 /* must be added to the list prior to calling register_slot */
350 list_add(&bridge->list, &bridge_list);
352 /* register all slot objects under this bridge */
353 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
354 register_slot, bridge, NULL);
355 if (ACPI_FAILURE(status)) {
356 list_del(&bridge->list);
360 /* install notify handler */
361 if (bridge->type != BRIDGE_TYPE_HOST) {
362 status = acpi_install_notify_handler(bridge->handle,
364 handle_hotplug_event_bridge,
367 if (ACPI_FAILURE(status)) {
368 err("failed to register interrupt notify handler\n");
374 /* allocate and initialize host bridge data structure */
375 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
377 struct acpiphp_bridge *bridge;
379 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
383 memset(bridge, 0, sizeof(struct acpiphp_bridge));
385 bridge->type = BRIDGE_TYPE_HOST;
386 bridge->handle = handle;
388 bridge->pci_bus = pci_bus;
390 spin_lock_init(&bridge->res_lock);
392 init_bridge_misc(bridge);
396 /* allocate and initialize PCI-to-PCI bridge data structure */
397 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
399 struct acpiphp_bridge *bridge;
401 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
402 if (bridge == NULL) {
403 err("out of memory\n");
407 memset(bridge, 0, sizeof(struct acpiphp_bridge));
409 bridge->type = BRIDGE_TYPE_P2P;
410 bridge->handle = handle;
412 bridge->pci_dev = pci_dev_get(pci_dev);
413 bridge->pci_bus = pci_dev->subordinate;
414 if (!bridge->pci_bus) {
415 err("This is not a PCI-to-PCI bridge!\n");
419 spin_lock_init(&bridge->res_lock);
421 init_bridge_misc(bridge);
424 pci_dev_put(pci_dev);
430 /* callback routine to find P2P bridges */
432 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
435 acpi_handle dummy_handle;
437 int device, function;
439 struct pci_bus *pci_bus = context;
441 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
442 if (ACPI_FAILURE(status))
443 return AE_OK; /* continue */
445 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
446 if (ACPI_FAILURE(status)) {
447 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
451 device = (tmp >> 16) & 0xffff;
452 function = tmp & 0xffff;
454 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
456 if (!dev || !dev->subordinate)
459 /* check if this bridge has ejectable slots */
460 if ((detect_ejectable_slots(handle) > 0) ||
461 (detect_dependent_devices(handle) > 0)) {
462 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
463 add_p2p_bridge(handle, dev);
466 /* search P2P bridges under this p2p bridge */
467 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
468 find_p2p_bridge, dev->subordinate, NULL);
469 if (ACPI_FAILURE(status))
470 warn("find_p2p_bridge faied (error code = 0x%x)\n", status);
478 /* find hot-pluggable slots, and then find P2P bridge */
479 static int add_bridge(acpi_handle handle)
484 acpi_handle dummy_handle;
485 struct pci_bus *pci_bus;
487 /* if the bridge doesn't have _STA, we assume it is always there */
488 status = acpi_get_handle(handle, "_STA", &dummy_handle);
489 if (ACPI_SUCCESS(status)) {
490 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
491 if (ACPI_FAILURE(status)) {
492 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
495 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
496 /* don't register this object */
500 /* get PCI segment number */
501 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
503 seg = ACPI_SUCCESS(status) ? tmp : 0;
505 /* get PCI bus number */
506 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
508 if (ACPI_SUCCESS(status)) {
511 warn("can't get bus number, assuming 0\n");
515 pci_bus = pci_find_bus(seg, bus);
517 err("Can't find bus %04x:%02x\n", seg, bus);
521 /* check if this bridge has ejectable slots */
522 if (detect_ejectable_slots(handle) > 0) {
523 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
524 add_host_bridge(handle, pci_bus);
528 /* search P2P bridges under this host bridge */
529 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
530 find_p2p_bridge, pci_bus, NULL);
532 if (ACPI_FAILURE(status))
533 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
538 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
540 struct list_head *head;
541 list_for_each(head, &bridge_list) {
542 struct acpiphp_bridge *bridge = list_entry(head,
543 struct acpiphp_bridge, list);
544 if (bridge->handle == handle)
551 static void cleanup_bridge(struct acpiphp_bridge *bridge)
553 struct list_head *list, *tmp;
554 struct acpiphp_slot *slot;
556 acpi_handle handle = bridge->handle;
558 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
559 handle_hotplug_event_bridge);
560 if (ACPI_FAILURE(status))
561 err("failed to remove notify handler\n");
563 slot = bridge->slots;
565 struct acpiphp_slot *next = slot->next;
566 list_for_each_safe (list, tmp, &slot->funcs) {
567 struct acpiphp_func *func;
568 func = list_entry(list, struct acpiphp_func, sibling);
569 if (!(func->flags & FUNC_HAS_DCK)) {
570 status = acpi_remove_notify_handler(func->handle,
572 handle_hotplug_event_func);
573 if (ACPI_FAILURE(status))
574 err("failed to remove notify handler\n");
576 pci_dev_put(func->pci_dev);
580 acpiphp_unregister_hotplug_slot(slot);
581 list_del(&slot->funcs);
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 if (!(bridge = acpiphp_handle_to_bridge(handle)))
598 cleanup_bridge(bridge);
602 static void remove_bridge(acpi_handle handle)
604 struct acpiphp_bridge *bridge;
606 bridge = acpiphp_handle_to_bridge(handle);
608 cleanup_bridge(bridge);
610 /* clean-up p2p bridges under this host bridge */
611 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
612 ACPI_UINT32_MAX, cleanup_p2p_bridge,
617 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
619 struct acpi_pci_id id;
623 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
626 bus = pci_find_bus(id.segment, id.bus);
630 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
634 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
635 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
644 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
649 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
650 union acpi_object *obj;
653 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
654 if (ACPI_SUCCESS(status)) {
655 *gsi_base = (u32)gsb;
659 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
660 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
663 obj = buffer.pointer;
664 if (obj->type != ACPI_TYPE_BUFFER)
667 table = obj->buffer.pointer;
668 switch (((acpi_table_entry_header *)table)->type) {
669 case ACPI_MADT_IOSAPIC:
670 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
673 case ACPI_MADT_IOAPIC:
674 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
681 acpi_os_free(buffer.pointer);
686 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
691 struct pci_dev *pdev;
695 /* Evaluate _STA if present */
696 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
697 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
698 return AE_CTRL_DEPTH;
700 /* Scan only PCI bus scope */
701 status = acpi_get_handle(handle, "_HID", &tmp);
702 if (ACPI_SUCCESS(status))
703 return AE_CTRL_DEPTH;
705 if (get_gsi_base(handle, &gsi_base))
708 pdev = get_apic_pci_info(handle);
712 if (pci_enable_device(pdev)) {
717 pci_set_master(pdev);
719 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
720 pci_disable_device(pdev);
725 phys_addr = pci_resource_start(pdev, 0);
726 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
727 pci_release_region(pdev, 0);
728 pci_disable_device(pdev);
736 static int acpiphp_configure_ioapics(acpi_handle handle)
738 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
739 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
743 static int power_on_slot(struct acpiphp_slot *slot)
746 struct acpiphp_func *func;
750 /* if already enabled, just skip */
751 if (slot->flags & SLOT_POWEREDON)
754 list_for_each (l, &slot->funcs) {
755 func = list_entry(l, struct acpiphp_func, sibling);
757 if (func->flags & FUNC_HAS_PS0) {
758 dbg("%s: executing _PS0\n", __FUNCTION__);
759 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
760 if (ACPI_FAILURE(status)) {
761 warn("%s: _PS0 failed\n", __FUNCTION__);
769 /* TBD: evaluate _STA to check if the slot is enabled */
771 slot->flags |= SLOT_POWEREDON;
778 static int power_off_slot(struct acpiphp_slot *slot)
781 struct acpiphp_func *func;
786 /* if already disabled, just skip */
787 if ((slot->flags & SLOT_POWEREDON) == 0)
790 list_for_each (l, &slot->funcs) {
791 func = list_entry(l, struct acpiphp_func, sibling);
793 if (func->flags & FUNC_HAS_PS3) {
794 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
795 if (ACPI_FAILURE(status)) {
796 warn("%s: _PS3 failed\n", __FUNCTION__);
804 /* TBD: evaluate _STA to check if the slot is disabled */
806 slot->flags &= (~SLOT_POWEREDON);
815 * acpiphp_max_busnr - return the highest reserved bus number under
817 * @bus: bus to start search with
820 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
822 struct list_head *tmp;
823 unsigned char max, n;
826 * pci_bus_max_busnr will return the highest
827 * reserved busnr for all these children.
828 * that is equivalent to the bus->subordinate
829 * value. We don't want to use the parent's
830 * bus->subordinate value because it could have
833 max = bus->secondary;
835 list_for_each(tmp, &bus->children) {
836 n = pci_bus_max_busnr(pci_bus_b(tmp));
846 * get_func - get a pointer to acpiphp_func given a slot, device
847 * @slot: slot to search
848 * @dev: pci_dev struct to match.
850 * This function will increase the reference count of pci_dev,
851 * so callers should call pci_dev_put when complete.
854 static struct acpiphp_func *
855 get_func(struct acpiphp_slot *slot, struct pci_dev *dev)
857 struct acpiphp_func *func = NULL;
858 struct pci_bus *bus = slot->bridge->pci_bus;
859 struct pci_dev *pdev;
861 list_for_each_entry(func, &slot->funcs, sibling) {
862 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device,
875 * acpiphp_bus_add - add a new bus to acpi subsystem
876 * @func: acpiphp_func of the bridge
879 static int acpiphp_bus_add(struct acpiphp_func *func)
882 struct acpi_device *device, *pdevice;
885 acpi_get_parent(func->handle, &phandle);
886 if (acpi_bus_get_device(phandle, &pdevice)) {
887 dbg("no parent device, assuming NULL\n");
890 if (!acpi_bus_get_device(func->handle, &device)) {
891 dbg("bus exists... trim\n");
892 /* this shouldn't be in here, so remove
893 * the bus then re-add it...
895 ret_val = acpi_bus_trim(device, 1);
896 dbg("acpi_bus_trim return %x\n", ret_val);
899 ret_val = acpi_bus_add(&device, pdevice, func->handle,
900 ACPI_BUS_TYPE_DEVICE);
902 dbg("error adding bus, %x\n",
904 goto acpiphp_bus_add_out;
907 * try to start anyway. We could have failed to add
908 * simply because this bus had previously been added
909 * on another add. Don't bother with the return value
910 * we just keep going.
912 ret_val = acpi_bus_start(device);
921 * enable_device - enable, configure a slot
922 * @slot: slot to be enabled
924 * This function should be called per *physical slot*,
925 * not per each slot object in ACPI namespace.
928 static int enable_device(struct acpiphp_slot *slot)
931 struct pci_bus *bus = slot->bridge->pci_bus;
933 struct acpiphp_func *func;
937 if (slot->flags & SLOT_ENABLED)
940 /* sanity check: dev should be NULL when hot-plugged in */
941 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
943 /* This case shouldn't happen */
944 err("pci_dev structure already exists.\n");
950 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
952 err("No new device found\n");
957 max = acpiphp_max_busnr(bus);
958 for (pass = 0; pass < 2; pass++) {
959 list_for_each_entry(dev, &bus->devices, bus_list) {
960 if (PCI_SLOT(dev->devfn) != slot->device)
962 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
963 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
964 max = pci_scan_bridge(bus, dev, max, pass);
965 if (pass && dev->subordinate) {
966 pci_bus_size_bridges(dev->subordinate);
967 func = get_func(slot, dev);
969 acpiphp_bus_add(func);
970 /* side effect of get_func */
978 pci_bus_assign_resources(bus);
979 acpiphp_sanitize_bus(bus);
980 pci_enable_bridges(bus);
981 pci_bus_add_devices(bus);
982 acpiphp_set_hpp_values(slot->bridge->handle, bus);
983 acpiphp_configure_ioapics(slot->bridge->handle);
985 /* associate pci_dev to our representation */
986 list_for_each (l, &slot->funcs) {
987 func = list_entry(l, struct acpiphp_func, sibling);
988 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
992 slot->flags |= SLOT_ENABLED;
1000 * disable_device - disable a slot
1002 static int disable_device(struct acpiphp_slot *slot)
1005 struct acpiphp_func *func;
1006 struct list_head *l;
1008 /* is this slot already disabled? */
1009 if (!(slot->flags & SLOT_ENABLED))
1012 list_for_each (l, &slot->funcs) {
1013 func = list_entry(l, struct acpiphp_func, sibling);
1017 pci_remove_bus_device(func->pci_dev);
1018 pci_dev_put(func->pci_dev);
1019 func->pci_dev = NULL;
1022 slot->flags &= (~SLOT_ENABLED);
1030 * get_slot_status - get ACPI slot status
1032 * if a slot has _STA for each function and if any one of them
1033 * returned non-zero status, return it
1035 * if a slot doesn't have _STA and if any one of its functions'
1036 * configuration space is configured, return 0x0f as a _STA
1038 * otherwise return 0
1040 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1043 unsigned long sta = 0;
1045 struct list_head *l;
1046 struct acpiphp_func *func;
1048 list_for_each (l, &slot->funcs) {
1049 func = list_entry(l, struct acpiphp_func, sibling);
1051 if (func->flags & FUNC_HAS_STA) {
1052 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1053 if (ACPI_SUCCESS(status) && sta)
1056 pci_bus_read_config_dword(slot->bridge->pci_bus,
1057 PCI_DEVFN(slot->device,
1059 PCI_VENDOR_ID, &dvid);
1060 if (dvid != 0xffffffff) {
1067 return (unsigned int)sta;
1071 * acpiphp_eject_slot - physically eject the slot
1073 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1076 struct acpiphp_func *func;
1077 struct list_head *l;
1078 struct acpi_object_list arg_list;
1079 union acpi_object arg;
1081 list_for_each (l, &slot->funcs) {
1082 func = list_entry(l, struct acpiphp_func, sibling);
1084 /* We don't want to call _EJ0 on non-existing functions. */
1085 if ((func->flags & FUNC_HAS_EJ0)) {
1086 /* _EJ0 method take one argument */
1088 arg_list.pointer = &arg;
1089 arg.type = ACPI_TYPE_INTEGER;
1090 arg.integer.value = 1;
1092 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1093 if (ACPI_FAILURE(status)) {
1094 warn("%s: _EJ0 failed\n", __FUNCTION__);
1104 * acpiphp_check_bridge - re-enumerate devices
1106 * Iterate over all slots under this bridge and make sure that if a
1107 * card is present they are enabled, and if not they are disabled.
1109 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1111 struct acpiphp_slot *slot;
1113 int enabled, disabled;
1115 enabled = disabled = 0;
1117 for (slot = bridge->slots; slot; slot = slot->next) {
1118 unsigned int status = get_slot_status(slot);
1119 if (slot->flags & SLOT_ENABLED) {
1120 if (status == ACPI_STA_ALL)
1122 retval = acpiphp_disable_slot(slot);
1124 err("Error occurred in disabling\n");
1127 acpiphp_eject_slot(slot);
1131 if (status != ACPI_STA_ALL)
1133 retval = acpiphp_enable_slot(slot);
1135 err("Error occurred in enabling\n");
1142 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1148 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1150 u16 pci_cmd, pci_bctl;
1151 struct pci_dev *cdev;
1153 /* Program hpp values for this device */
1154 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1155 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1156 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1158 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1159 bridge->hpp.cache_line_size);
1160 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1161 bridge->hpp.latency_timer);
1162 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1163 if (bridge->hpp.enable_SERR)
1164 pci_cmd |= PCI_COMMAND_SERR;
1166 pci_cmd &= ~PCI_COMMAND_SERR;
1167 if (bridge->hpp.enable_PERR)
1168 pci_cmd |= PCI_COMMAND_PARITY;
1170 pci_cmd &= ~PCI_COMMAND_PARITY;
1171 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1173 /* Program bridge control value and child devices */
1174 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1175 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1176 bridge->hpp.latency_timer);
1177 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1178 if (bridge->hpp.enable_SERR)
1179 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1181 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1182 if (bridge->hpp.enable_PERR)
1183 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1185 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1186 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1187 if (dev->subordinate) {
1188 list_for_each_entry(cdev, &dev->subordinate->devices,
1190 program_hpp(cdev, bridge);
1195 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1197 struct acpiphp_bridge bridge;
1198 struct pci_dev *dev;
1200 memset(&bridge, 0, sizeof(bridge));
1201 bridge.handle = handle;
1202 decode_hpp(&bridge);
1203 list_for_each_entry(dev, &bus->devices, bus_list)
1204 program_hpp(dev, &bridge);
1209 * Remove devices for which we could not assign resources, call
1210 * arch specific code to fix-up the bus
1212 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1214 struct pci_dev *dev;
1216 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1218 list_for_each_entry(dev, &bus->devices, bus_list) {
1219 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1220 struct resource *res = &dev->resource[i];
1221 if ((res->flags & type_mask) && !res->start &&
1223 /* Could not assign a required resources
1224 * for this device, remove it */
1225 pci_remove_bus_device(dev);
1232 /* Program resources in newly inserted bridge */
1233 static int acpiphp_configure_bridge (acpi_handle handle)
1235 struct acpi_pci_id pci_id;
1236 struct pci_bus *bus;
1238 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1239 err("cannot get PCI domain and bus number for bridge\n");
1242 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1244 err("cannot find bus %d:%d\n",
1245 pci_id.segment, pci_id.bus);
1249 pci_bus_size_bridges(bus);
1250 pci_bus_assign_resources(bus);
1251 acpiphp_sanitize_bus(bus);
1252 acpiphp_set_hpp_values(handle, bus);
1253 pci_enable_bridges(bus);
1254 acpiphp_configure_ioapics(handle);
1258 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1260 struct acpi_device *device, *pdevice;
1261 acpi_handle phandle;
1263 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1264 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1265 err("unexpected notification type %d\n", type);
1269 acpi_get_parent(handle, &phandle);
1270 if (acpi_bus_get_device(phandle, &pdevice)) {
1271 dbg("no parent device, assuming NULL\n");
1274 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1275 err("cannot add bridge to acpi list\n");
1278 if (!acpiphp_configure_bridge(handle) &&
1279 !acpi_bus_start(device))
1282 err("cannot configure and start bridge\n");
1287 * ACPI event handlers
1291 * handle_hotplug_event_bridge - handle ACPI event on bridges
1293 * @handle: Notify()'ed acpi_handle
1294 * @type: Notify code
1295 * @context: pointer to acpiphp_bridge structure
1297 * handles ACPI event notification on {host,p2p} bridges
1300 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1302 struct acpiphp_bridge *bridge;
1304 struct acpi_buffer buffer = { .length = sizeof(objname),
1305 .pointer = objname };
1306 struct acpi_device *device;
1308 if (acpi_bus_get_device(handle, &device)) {
1309 /* This bridge must have just been physically inserted */
1310 handle_bridge_insertion(handle, type);
1314 bridge = acpiphp_handle_to_bridge(handle);
1316 err("cannot get bridge info\n");
1320 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1323 case ACPI_NOTIFY_BUS_CHECK:
1324 /* bus re-enumerate */
1325 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1326 acpiphp_check_bridge(bridge);
1329 case ACPI_NOTIFY_DEVICE_CHECK:
1331 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1332 acpiphp_check_bridge(bridge);
1335 case ACPI_NOTIFY_DEVICE_WAKE:
1337 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1340 case ACPI_NOTIFY_EJECT_REQUEST:
1341 /* request device eject */
1342 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1345 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1346 printk(KERN_ERR "Device %s cannot be configured due"
1347 " to a frequency mismatch\n", objname);
1350 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1351 printk(KERN_ERR "Device %s cannot be configured due"
1352 " to a bus mode mismatch\n", objname);
1355 case ACPI_NOTIFY_POWER_FAULT:
1356 printk(KERN_ERR "Device %s has suffered a power fault\n",
1361 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1367 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1369 * @handle: Notify()'ed acpi_handle
1370 * @type: Notify code
1371 * @context: pointer to acpiphp_func structure
1373 * handles ACPI event notification on slots
1376 void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1378 struct acpiphp_func *func;
1380 struct acpi_buffer buffer = { .length = sizeof(objname),
1381 .pointer = objname };
1383 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1385 func = (struct acpiphp_func *)context;
1388 case ACPI_NOTIFY_BUS_CHECK:
1389 /* bus re-enumerate */
1390 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1391 acpiphp_enable_slot(func->slot);
1394 case ACPI_NOTIFY_DEVICE_CHECK:
1395 /* device check : re-enumerate from parent bus */
1396 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1397 acpiphp_check_bridge(func->slot->bridge);
1400 case ACPI_NOTIFY_DEVICE_WAKE:
1402 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1405 case ACPI_NOTIFY_EJECT_REQUEST:
1406 /* request device eject */
1407 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1408 if (!(acpiphp_disable_slot(func->slot)))
1409 acpiphp_eject_slot(func->slot);
1413 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1418 static int is_root_bridge(acpi_handle handle)
1421 struct acpi_device_info *info;
1422 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1425 status = acpi_get_object_info(handle, &buffer);
1426 if (ACPI_SUCCESS(status)) {
1427 info = buffer.pointer;
1428 if ((info->valid & ACPI_VALID_HID) &&
1429 !strcmp(PCI_ROOT_HID_STRING,
1430 info->hardware_id.value)) {
1431 acpi_os_free(buffer.pointer);
1434 if (info->valid & ACPI_VALID_CID) {
1435 for (i=0; i < info->compatibility_id.count; i++) {
1436 if (!strcmp(PCI_ROOT_HID_STRING,
1437 info->compatibility_id.id[i].value)) {
1438 acpi_os_free(buffer.pointer);
1448 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1450 int *count = (int *)context;
1452 if (is_root_bridge(handle)) {
1453 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1454 handle_hotplug_event_bridge, NULL);
1460 static struct acpi_pci_driver acpi_pci_hp_driver = {
1462 .remove = remove_bridge,
1466 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1469 int __init acpiphp_glue_init(void)
1473 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1474 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1479 acpi_pci_register_driver(&acpi_pci_hp_driver);
1486 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1488 * This function frees all data allocated in acpiphp_glue_init()
1490 void __exit acpiphp_glue_exit(void)
1492 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1497 * acpiphp_get_num_slots - count number of slots in a system
1499 int __init acpiphp_get_num_slots(void)
1501 struct list_head *node;
1502 struct acpiphp_bridge *bridge;
1507 list_for_each (node, &bridge_list) {
1508 bridge = (struct acpiphp_bridge *)node;
1509 dbg("Bus %04x:%02x has %d slot%s\n",
1510 pci_domain_nr(bridge->pci_bus),
1511 bridge->pci_bus->number, bridge->nr_slots,
1512 bridge->nr_slots == 1 ? "" : "s");
1513 num_slots += bridge->nr_slots;
1516 dbg("Total %d slots\n", num_slots);
1523 * acpiphp_for_each_slot - call function for each slot
1524 * @fn: callback function
1525 * @data: context to be passed to callback function
1528 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1530 struct list_head *node;
1531 struct acpiphp_bridge *bridge;
1532 struct acpiphp_slot *slot;
1535 list_for_each (node, &bridge_list) {
1536 bridge = (struct acpiphp_bridge *)node;
1537 for (slot = bridge->slots; slot; slot = slot->next) {
1538 retval = fn(slot, data);
1551 * acpiphp_enable_slot - power on slot
1553 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1557 mutex_lock(&slot->crit_sect);
1559 /* wake up all functions */
1560 retval = power_on_slot(slot);
1564 if (get_slot_status(slot) == ACPI_STA_ALL)
1565 /* configure all functions */
1566 retval = enable_device(slot);
1569 mutex_unlock(&slot->crit_sect);
1574 * acpiphp_disable_slot - power off slot
1576 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1580 mutex_lock(&slot->crit_sect);
1582 /* unconfigure all functions */
1583 retval = disable_device(slot);
1587 /* power off all functions */
1588 retval = power_off_slot(slot);
1593 mutex_unlock(&slot->crit_sect);
1602 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1604 return (slot->flags & SLOT_POWEREDON);
1612 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1616 sta = get_slot_status(slot);
1618 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1623 * adapter presence : 1
1626 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1630 sta = get_slot_status(slot);
1632 return (sta == 0) ? 0 : 1;
1637 * pci address (seg/bus/dev)
1639 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1642 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1644 address = (pci_domain_nr(pci_bus) << 16) |
1645 (pci_bus->number << 8) |