2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
4 * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5 * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6 * Copyright (C) 2002,2003 NEC Corporation
7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
12 * All rights reserved.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22 * NON INFRINGEMENT. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Send feedback to <t-kochi@bq.jp.nec.com>
34 * Lifetime rules for pci_dev:
35 * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
36 * when the driver is loaded or when an insertion event occurs. It loses
37 * a refcount when its ejected or the driver unloads.
38 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
39 * when the bridge is scanned and it loses a refcount when the bridge
43 #include <linux/init.h>
44 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/pci.h>
48 #include <linux/smp_lock.h>
49 #include <linux/mutex.h>
52 #include "pci_hotplug.h"
55 static LIST_HEAD(bridge_list);
57 #define MY_NAME "acpiphp_glue"
59 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
60 static void acpiphp_sanitize_bus(struct pci_bus *bus);
61 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
65 * initialization & terminatation routines
69 * is_ejectable - determine if a slot is ejectable
70 * @handle: handle to acpi namespace
72 * Ejectable slot should satisfy at least these conditions:
85 static int is_ejectable(acpi_handle handle)
90 status = acpi_get_handle(handle, "_ADR", &tmp);
91 if (ACPI_FAILURE(status)) {
95 status = acpi_get_handle(handle, "_EJ0", &tmp);
96 if (ACPI_FAILURE(status)) {
104 /* callback routine to check the existence of ejectable slots */
106 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
108 int *count = (int *)context;
110 if (is_ejectable(handle)) {
112 /* only one ejectable slot is enough */
113 return AE_CTRL_TERMINATE;
120 /* callback routine to register each ACPI PCI slot object */
122 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
124 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
125 struct acpiphp_slot *slot;
126 struct acpiphp_func *newfunc;
127 struct dependent_device *dd;
129 acpi_status status = AE_OK;
130 unsigned long adr, sun;
131 int device, function, retval;
133 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
135 if (ACPI_FAILURE(status))
138 status = acpi_get_handle(handle, "_EJ0", &tmp);
140 if (ACPI_FAILURE(status) && !(is_dependent_device(handle)))
143 device = (adr >> 16) & 0xffff;
144 function = adr & 0xffff;
146 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
150 INIT_LIST_HEAD(&newfunc->sibling);
151 newfunc->handle = handle;
152 newfunc->function = function;
153 if (ACPI_SUCCESS(status))
154 newfunc->flags = FUNC_HAS_EJ0;
156 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
157 newfunc->flags |= FUNC_HAS_STA;
159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
160 newfunc->flags |= FUNC_HAS_PS0;
162 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
163 newfunc->flags |= FUNC_HAS_PS3;
165 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) {
166 newfunc->flags |= FUNC_HAS_DCK;
167 /* add to devices dependent on dock station,
168 * because this may actually be the dock bridge
170 dd = alloc_dependent_device(handle);
172 err("Can't allocate memory for "
173 "new dependent device!\n");
175 add_dependent_device(dd);
178 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
179 if (ACPI_FAILURE(status))
182 /* search for objects that share the same slot */
183 for (slot = bridge->slots; slot; slot = slot->next)
184 if (slot->device == device) {
185 if (slot->sun != sun)
186 warn("sibling found, but _SUN doesn't match!\n");
191 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
197 slot->bridge = bridge;
198 slot->device = device;
200 INIT_LIST_HEAD(&slot->funcs);
201 mutex_init(&slot->crit_sect);
203 slot->next = bridge->slots;
204 bridge->slots = slot;
208 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
209 slot->sun, pci_domain_nr(bridge->pci_bus),
210 bridge->pci_bus->number, slot->device);
211 retval = acpiphp_register_hotplug_slot(slot);
213 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
218 newfunc->slot = slot;
219 list_add_tail(&newfunc->sibling, &slot->funcs);
221 /* associate corresponding pci_dev */
222 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
223 PCI_DEVFN(device, function));
224 if (newfunc->pci_dev) {
225 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
228 /* if this is a device dependent on a dock station,
229 * associate the acpiphp_func to the dependent_device
232 if ((dd = get_dependent_device(handle))) {
233 newfunc->flags |= FUNC_IS_DD;
235 * we don't want any devices which is dependent
236 * on the dock to have it's _EJ0 method executed.
237 * because we need to run _DCK first.
239 newfunc->flags &= ~FUNC_HAS_EJ0;
241 add_pci_dependent_device(dd);
244 /* install notify handler */
245 if (!(newfunc->flags & FUNC_HAS_DCK)) {
246 status = acpi_install_notify_handler(handle,
248 handle_hotplug_event_func,
251 if (ACPI_FAILURE(status))
252 err("failed to register interrupt notify handler\n");
260 bridge->slots = slot->next;
268 /* see if it's worth looking at this bridge */
269 static int detect_ejectable_slots(acpi_handle *bridge_handle)
276 /* only check slots defined directly below bridge object */
277 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
278 is_ejectable_slot, (void *)&count, NULL);
284 /* decode ACPI 2.0 _HPP hot plug parameters */
285 static void decode_hpp(struct acpiphp_bridge *bridge)
288 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
290 union acpi_object *package;
293 /* default numbers */
294 bridge->hpp.cache_line_size = 0x10;
295 bridge->hpp.latency_timer = 0x40;
296 bridge->hpp.enable_SERR = 0;
297 bridge->hpp.enable_PERR = 0;
299 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
301 if (ACPI_FAILURE(status)) {
302 dbg("_HPP evaluation failed\n");
306 package = (union acpi_object *) buffer.pointer;
308 if (!package || package->type != ACPI_TYPE_PACKAGE ||
309 package->package.count != 4 || !package->package.elements) {
310 err("invalid _HPP object; ignoring\n");
314 for (i = 0; i < 4; i++) {
315 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
316 err("invalid _HPP parameter type; ignoring\n");
321 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
322 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
323 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
324 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
326 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
327 bridge->hpp.cache_line_size,
328 bridge->hpp.latency_timer,
329 bridge->hpp.enable_SERR,
330 bridge->hpp.enable_PERR);
332 bridge->flags |= BRIDGE_HAS_HPP;
335 kfree(buffer.pointer);
339 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
340 static void init_bridge_misc(struct acpiphp_bridge *bridge)
344 /* decode ACPI 2.0 _HPP (hot plug parameters) */
347 /* must be added to the list prior to calling register_slot */
348 list_add(&bridge->list, &bridge_list);
350 /* register all slot objects under this bridge */
351 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
352 register_slot, bridge, NULL);
353 if (ACPI_FAILURE(status)) {
354 list_del(&bridge->list);
358 /* install notify handler */
359 if (bridge->type != BRIDGE_TYPE_HOST) {
360 status = acpi_install_notify_handler(bridge->handle,
362 handle_hotplug_event_bridge,
365 if (ACPI_FAILURE(status)) {
366 err("failed to register interrupt notify handler\n");
372 /* allocate and initialize host bridge data structure */
373 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
375 struct acpiphp_bridge *bridge;
377 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
381 bridge->type = BRIDGE_TYPE_HOST;
382 bridge->handle = handle;
384 bridge->pci_bus = pci_bus;
386 spin_lock_init(&bridge->res_lock);
388 init_bridge_misc(bridge);
392 /* allocate and initialize PCI-to-PCI bridge data structure */
393 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
395 struct acpiphp_bridge *bridge;
397 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
398 if (bridge == NULL) {
399 err("out of memory\n");
403 bridge->type = BRIDGE_TYPE_P2P;
404 bridge->handle = handle;
406 bridge->pci_dev = pci_dev_get(pci_dev);
407 bridge->pci_bus = pci_dev->subordinate;
408 if (!bridge->pci_bus) {
409 err("This is not a PCI-to-PCI bridge!\n");
413 spin_lock_init(&bridge->res_lock);
415 init_bridge_misc(bridge);
418 pci_dev_put(pci_dev);
424 /* callback routine to find P2P bridges */
426 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
429 acpi_handle dummy_handle;
431 int device, function;
433 struct pci_bus *pci_bus = context;
435 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
436 if (ACPI_FAILURE(status))
437 return AE_OK; /* continue */
439 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
440 if (ACPI_FAILURE(status)) {
441 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
445 device = (tmp >> 16) & 0xffff;
446 function = tmp & 0xffff;
448 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
450 if (!dev || !dev->subordinate)
453 /* check if this bridge has ejectable slots */
454 if ((detect_ejectable_slots(handle) > 0) ||
455 (detect_dependent_devices(handle) > 0)) {
456 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
457 add_p2p_bridge(handle, dev);
460 /* search P2P bridges under this p2p bridge */
461 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
462 find_p2p_bridge, dev->subordinate, NULL);
463 if (ACPI_FAILURE(status))
464 warn("find_p2p_bridge faied (error code = 0x%x)\n", status);
472 /* find hot-pluggable slots, and then find P2P bridge */
473 static int add_bridge(acpi_handle handle)
478 acpi_handle dummy_handle;
479 struct pci_bus *pci_bus;
481 /* if the bridge doesn't have _STA, we assume it is always there */
482 status = acpi_get_handle(handle, "_STA", &dummy_handle);
483 if (ACPI_SUCCESS(status)) {
484 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
485 if (ACPI_FAILURE(status)) {
486 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
489 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
490 /* don't register this object */
494 /* get PCI segment number */
495 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
497 seg = ACPI_SUCCESS(status) ? tmp : 0;
499 /* get PCI bus number */
500 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
502 if (ACPI_SUCCESS(status)) {
505 warn("can't get bus number, assuming 0\n");
509 pci_bus = pci_find_bus(seg, bus);
511 err("Can't find bus %04x:%02x\n", seg, bus);
515 /* check if this bridge has ejectable slots */
516 if (detect_ejectable_slots(handle) > 0) {
517 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
518 add_host_bridge(handle, pci_bus);
522 /* search P2P bridges under this host bridge */
523 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
524 find_p2p_bridge, pci_bus, NULL);
526 if (ACPI_FAILURE(status))
527 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
532 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
534 struct list_head *head;
535 list_for_each(head, &bridge_list) {
536 struct acpiphp_bridge *bridge = list_entry(head,
537 struct acpiphp_bridge, list);
538 if (bridge->handle == handle)
545 static void cleanup_bridge(struct acpiphp_bridge *bridge)
547 struct list_head *list, *tmp;
548 struct acpiphp_slot *slot;
550 acpi_handle handle = bridge->handle;
552 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
553 handle_hotplug_event_bridge);
554 if (ACPI_FAILURE(status))
555 err("failed to remove notify handler\n");
557 slot = bridge->slots;
559 struct acpiphp_slot *next = slot->next;
560 list_for_each_safe (list, tmp, &slot->funcs) {
561 struct acpiphp_func *func;
562 func = list_entry(list, struct acpiphp_func, sibling);
563 if (!(func->flags & FUNC_HAS_DCK)) {
564 status = acpi_remove_notify_handler(func->handle,
566 handle_hotplug_event_func);
567 if (ACPI_FAILURE(status))
568 err("failed to remove notify handler\n");
570 pci_dev_put(func->pci_dev);
574 acpiphp_unregister_hotplug_slot(slot);
575 list_del(&slot->funcs);
580 pci_dev_put(bridge->pci_dev);
581 list_del(&bridge->list);
586 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
588 struct acpiphp_bridge *bridge;
590 if (!(bridge = acpiphp_handle_to_bridge(handle)))
592 cleanup_bridge(bridge);
596 static void remove_bridge(acpi_handle handle)
598 struct acpiphp_bridge *bridge;
600 bridge = acpiphp_handle_to_bridge(handle);
602 cleanup_bridge(bridge);
604 /* clean-up p2p bridges under this host bridge */
605 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
606 ACPI_UINT32_MAX, cleanup_p2p_bridge,
611 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
613 struct acpi_pci_id id;
617 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
620 bus = pci_find_bus(id.segment, id.bus);
624 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
628 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
629 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
638 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
643 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
644 union acpi_object *obj;
647 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
648 if (ACPI_SUCCESS(status)) {
649 *gsi_base = (u32)gsb;
653 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
654 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
657 obj = buffer.pointer;
658 if (obj->type != ACPI_TYPE_BUFFER)
661 table = obj->buffer.pointer;
662 switch (((acpi_table_entry_header *)table)->type) {
663 case ACPI_MADT_IOSAPIC:
664 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
667 case ACPI_MADT_IOAPIC:
668 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
675 acpi_os_free(buffer.pointer);
680 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
685 struct pci_dev *pdev;
689 /* Evaluate _STA if present */
690 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
691 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
692 return AE_CTRL_DEPTH;
694 /* Scan only PCI bus scope */
695 status = acpi_get_handle(handle, "_HID", &tmp);
696 if (ACPI_SUCCESS(status))
697 return AE_CTRL_DEPTH;
699 if (get_gsi_base(handle, &gsi_base))
702 pdev = get_apic_pci_info(handle);
706 if (pci_enable_device(pdev)) {
711 pci_set_master(pdev);
713 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
714 pci_disable_device(pdev);
719 phys_addr = pci_resource_start(pdev, 0);
720 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
721 pci_release_region(pdev, 0);
722 pci_disable_device(pdev);
730 static int acpiphp_configure_ioapics(acpi_handle handle)
732 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
733 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
737 static int power_on_slot(struct acpiphp_slot *slot)
740 struct acpiphp_func *func;
744 /* if already enabled, just skip */
745 if (slot->flags & SLOT_POWEREDON)
748 list_for_each (l, &slot->funcs) {
749 func = list_entry(l, struct acpiphp_func, sibling);
751 if (func->flags & FUNC_HAS_PS0) {
752 dbg("%s: executing _PS0\n", __FUNCTION__);
753 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
754 if (ACPI_FAILURE(status)) {
755 warn("%s: _PS0 failed\n", __FUNCTION__);
763 /* TBD: evaluate _STA to check if the slot is enabled */
765 slot->flags |= SLOT_POWEREDON;
772 static int power_off_slot(struct acpiphp_slot *slot)
775 struct acpiphp_func *func;
780 /* if already disabled, just skip */
781 if ((slot->flags & SLOT_POWEREDON) == 0)
784 list_for_each (l, &slot->funcs) {
785 func = list_entry(l, struct acpiphp_func, sibling);
787 if (func->flags & FUNC_HAS_PS3) {
788 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
789 if (ACPI_FAILURE(status)) {
790 warn("%s: _PS3 failed\n", __FUNCTION__);
798 /* TBD: evaluate _STA to check if the slot is disabled */
800 slot->flags &= (~SLOT_POWEREDON);
809 * acpiphp_max_busnr - return the highest reserved bus number under
811 * @bus: bus to start search with
814 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
816 struct list_head *tmp;
817 unsigned char max, n;
820 * pci_bus_max_busnr will return the highest
821 * reserved busnr for all these children.
822 * that is equivalent to the bus->subordinate
823 * value. We don't want to use the parent's
824 * bus->subordinate value because it could have
827 max = bus->secondary;
829 list_for_each(tmp, &bus->children) {
830 n = pci_bus_max_busnr(pci_bus_b(tmp));
840 * get_func - get a pointer to acpiphp_func given a slot, device
841 * @slot: slot to search
842 * @dev: pci_dev struct to match.
844 * This function will increase the reference count of pci_dev,
845 * so callers should call pci_dev_put when complete.
848 static struct acpiphp_func *
849 get_func(struct acpiphp_slot *slot, struct pci_dev *dev)
851 struct acpiphp_func *func = NULL;
852 struct pci_bus *bus = slot->bridge->pci_bus;
853 struct pci_dev *pdev;
855 list_for_each_entry(func, &slot->funcs, sibling) {
856 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device,
869 * acpiphp_bus_add - add a new bus to acpi subsystem
870 * @func: acpiphp_func of the bridge
873 static int acpiphp_bus_add(struct acpiphp_func *func)
876 struct acpi_device *device, *pdevice;
879 acpi_get_parent(func->handle, &phandle);
880 if (acpi_bus_get_device(phandle, &pdevice)) {
881 dbg("no parent device, assuming NULL\n");
884 if (!acpi_bus_get_device(func->handle, &device)) {
885 dbg("bus exists... trim\n");
886 /* this shouldn't be in here, so remove
887 * the bus then re-add it...
889 ret_val = acpi_bus_trim(device, 1);
890 dbg("acpi_bus_trim return %x\n", ret_val);
893 ret_val = acpi_bus_add(&device, pdevice, func->handle,
894 ACPI_BUS_TYPE_DEVICE);
896 dbg("error adding bus, %x\n",
898 goto acpiphp_bus_add_out;
901 * try to start anyway. We could have failed to add
902 * simply because this bus had previously been added
903 * on another add. Don't bother with the return value
904 * we just keep going.
906 ret_val = acpi_bus_start(device);
915 * enable_device - enable, configure a slot
916 * @slot: slot to be enabled
918 * This function should be called per *physical slot*,
919 * not per each slot object in ACPI namespace.
922 static int enable_device(struct acpiphp_slot *slot)
925 struct pci_bus *bus = slot->bridge->pci_bus;
927 struct acpiphp_func *func;
931 if (slot->flags & SLOT_ENABLED)
934 /* sanity check: dev should be NULL when hot-plugged in */
935 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
937 /* This case shouldn't happen */
938 err("pci_dev structure already exists.\n");
944 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
946 err("No new device found\n");
951 max = acpiphp_max_busnr(bus);
952 for (pass = 0; pass < 2; pass++) {
953 list_for_each_entry(dev, &bus->devices, bus_list) {
954 if (PCI_SLOT(dev->devfn) != slot->device)
956 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
957 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
958 max = pci_scan_bridge(bus, dev, max, pass);
959 if (pass && dev->subordinate) {
960 pci_bus_size_bridges(dev->subordinate);
961 func = get_func(slot, dev);
963 acpiphp_bus_add(func);
964 /* side effect of get_func */
972 pci_bus_assign_resources(bus);
973 acpiphp_sanitize_bus(bus);
974 pci_enable_bridges(bus);
975 pci_bus_add_devices(bus);
976 acpiphp_set_hpp_values(slot->bridge->handle, bus);
977 acpiphp_configure_ioapics(slot->bridge->handle);
979 /* associate pci_dev to our representation */
980 list_for_each (l, &slot->funcs) {
981 func = list_entry(l, struct acpiphp_func, sibling);
982 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
986 slot->flags |= SLOT_ENABLED;
994 * disable_device - disable a slot
996 static int disable_device(struct acpiphp_slot *slot)
999 struct acpiphp_func *func;
1000 struct list_head *l;
1002 /* is this slot already disabled? */
1003 if (!(slot->flags & SLOT_ENABLED))
1006 list_for_each (l, &slot->funcs) {
1007 func = list_entry(l, struct acpiphp_func, sibling);
1011 pci_remove_bus_device(func->pci_dev);
1012 pci_dev_put(func->pci_dev);
1013 func->pci_dev = NULL;
1016 slot->flags &= (~SLOT_ENABLED);
1024 * get_slot_status - get ACPI slot status
1026 * if a slot has _STA for each function and if any one of them
1027 * returned non-zero status, return it
1029 * if a slot doesn't have _STA and if any one of its functions'
1030 * configuration space is configured, return 0x0f as a _STA
1032 * otherwise return 0
1034 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1037 unsigned long sta = 0;
1039 struct list_head *l;
1040 struct acpiphp_func *func;
1042 list_for_each (l, &slot->funcs) {
1043 func = list_entry(l, struct acpiphp_func, sibling);
1045 if (func->flags & FUNC_HAS_STA) {
1046 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1047 if (ACPI_SUCCESS(status) && sta)
1050 pci_bus_read_config_dword(slot->bridge->pci_bus,
1051 PCI_DEVFN(slot->device,
1053 PCI_VENDOR_ID, &dvid);
1054 if (dvid != 0xffffffff) {
1061 return (unsigned int)sta;
1065 * acpiphp_eject_slot - physically eject the slot
1067 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1070 struct acpiphp_func *func;
1071 struct list_head *l;
1072 struct acpi_object_list arg_list;
1073 union acpi_object arg;
1075 list_for_each (l, &slot->funcs) {
1076 func = list_entry(l, struct acpiphp_func, sibling);
1078 /* We don't want to call _EJ0 on non-existing functions. */
1079 if ((func->flags & FUNC_HAS_EJ0)) {
1080 /* _EJ0 method take one argument */
1082 arg_list.pointer = &arg;
1083 arg.type = ACPI_TYPE_INTEGER;
1084 arg.integer.value = 1;
1086 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1087 if (ACPI_FAILURE(status)) {
1088 warn("%s: _EJ0 failed\n", __FUNCTION__);
1098 * acpiphp_check_bridge - re-enumerate devices
1100 * Iterate over all slots under this bridge and make sure that if a
1101 * card is present they are enabled, and if not they are disabled.
1103 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1105 struct acpiphp_slot *slot;
1107 int enabled, disabled;
1109 enabled = disabled = 0;
1111 for (slot = bridge->slots; slot; slot = slot->next) {
1112 unsigned int status = get_slot_status(slot);
1113 if (slot->flags & SLOT_ENABLED) {
1114 if (status == ACPI_STA_ALL)
1116 retval = acpiphp_disable_slot(slot);
1118 err("Error occurred in disabling\n");
1121 acpiphp_eject_slot(slot);
1125 if (status != ACPI_STA_ALL)
1127 retval = acpiphp_enable_slot(slot);
1129 err("Error occurred in enabling\n");
1136 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1142 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1144 u16 pci_cmd, pci_bctl;
1145 struct pci_dev *cdev;
1147 /* Program hpp values for this device */
1148 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1149 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1150 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1152 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1153 bridge->hpp.cache_line_size);
1154 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1155 bridge->hpp.latency_timer);
1156 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1157 if (bridge->hpp.enable_SERR)
1158 pci_cmd |= PCI_COMMAND_SERR;
1160 pci_cmd &= ~PCI_COMMAND_SERR;
1161 if (bridge->hpp.enable_PERR)
1162 pci_cmd |= PCI_COMMAND_PARITY;
1164 pci_cmd &= ~PCI_COMMAND_PARITY;
1165 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1167 /* Program bridge control value and child devices */
1168 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1169 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1170 bridge->hpp.latency_timer);
1171 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1172 if (bridge->hpp.enable_SERR)
1173 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1175 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1176 if (bridge->hpp.enable_PERR)
1177 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1179 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1180 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1181 if (dev->subordinate) {
1182 list_for_each_entry(cdev, &dev->subordinate->devices,
1184 program_hpp(cdev, bridge);
1189 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1191 struct acpiphp_bridge bridge;
1192 struct pci_dev *dev;
1194 memset(&bridge, 0, sizeof(bridge));
1195 bridge.handle = handle;
1196 decode_hpp(&bridge);
1197 list_for_each_entry(dev, &bus->devices, bus_list)
1198 program_hpp(dev, &bridge);
1203 * Remove devices for which we could not assign resources, call
1204 * arch specific code to fix-up the bus
1206 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1208 struct pci_dev *dev;
1210 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1212 list_for_each_entry(dev, &bus->devices, bus_list) {
1213 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1214 struct resource *res = &dev->resource[i];
1215 if ((res->flags & type_mask) && !res->start &&
1217 /* Could not assign a required resources
1218 * for this device, remove it */
1219 pci_remove_bus_device(dev);
1226 /* Program resources in newly inserted bridge */
1227 static int acpiphp_configure_bridge (acpi_handle handle)
1229 struct acpi_pci_id pci_id;
1230 struct pci_bus *bus;
1232 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1233 err("cannot get PCI domain and bus number for bridge\n");
1236 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1238 err("cannot find bus %d:%d\n",
1239 pci_id.segment, pci_id.bus);
1243 pci_bus_size_bridges(bus);
1244 pci_bus_assign_resources(bus);
1245 acpiphp_sanitize_bus(bus);
1246 acpiphp_set_hpp_values(handle, bus);
1247 pci_enable_bridges(bus);
1248 acpiphp_configure_ioapics(handle);
1252 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1254 struct acpi_device *device, *pdevice;
1255 acpi_handle phandle;
1257 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1258 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1259 err("unexpected notification type %d\n", type);
1263 acpi_get_parent(handle, &phandle);
1264 if (acpi_bus_get_device(phandle, &pdevice)) {
1265 dbg("no parent device, assuming NULL\n");
1268 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1269 err("cannot add bridge to acpi list\n");
1272 if (!acpiphp_configure_bridge(handle) &&
1273 !acpi_bus_start(device))
1276 err("cannot configure and start bridge\n");
1281 * ACPI event handlers
1285 * handle_hotplug_event_bridge - handle ACPI event on bridges
1287 * @handle: Notify()'ed acpi_handle
1288 * @type: Notify code
1289 * @context: pointer to acpiphp_bridge structure
1291 * handles ACPI event notification on {host,p2p} bridges
1294 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1296 struct acpiphp_bridge *bridge;
1298 struct acpi_buffer buffer = { .length = sizeof(objname),
1299 .pointer = objname };
1300 struct acpi_device *device;
1302 if (acpi_bus_get_device(handle, &device)) {
1303 /* This bridge must have just been physically inserted */
1304 handle_bridge_insertion(handle, type);
1308 bridge = acpiphp_handle_to_bridge(handle);
1310 err("cannot get bridge info\n");
1314 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1317 case ACPI_NOTIFY_BUS_CHECK:
1318 /* bus re-enumerate */
1319 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1320 acpiphp_check_bridge(bridge);
1323 case ACPI_NOTIFY_DEVICE_CHECK:
1325 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1326 acpiphp_check_bridge(bridge);
1329 case ACPI_NOTIFY_DEVICE_WAKE:
1331 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1334 case ACPI_NOTIFY_EJECT_REQUEST:
1335 /* request device eject */
1336 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1339 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1340 printk(KERN_ERR "Device %s cannot be configured due"
1341 " to a frequency mismatch\n", objname);
1344 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1345 printk(KERN_ERR "Device %s cannot be configured due"
1346 " to a bus mode mismatch\n", objname);
1349 case ACPI_NOTIFY_POWER_FAULT:
1350 printk(KERN_ERR "Device %s has suffered a power fault\n",
1355 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1361 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1363 * @handle: Notify()'ed acpi_handle
1364 * @type: Notify code
1365 * @context: pointer to acpiphp_func structure
1367 * handles ACPI event notification on slots
1370 void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1372 struct acpiphp_func *func;
1374 struct acpi_buffer buffer = { .length = sizeof(objname),
1375 .pointer = objname };
1377 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1379 func = (struct acpiphp_func *)context;
1382 case ACPI_NOTIFY_BUS_CHECK:
1383 /* bus re-enumerate */
1384 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1385 acpiphp_enable_slot(func->slot);
1388 case ACPI_NOTIFY_DEVICE_CHECK:
1389 /* device check : re-enumerate from parent bus */
1390 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1391 acpiphp_check_bridge(func->slot->bridge);
1394 case ACPI_NOTIFY_DEVICE_WAKE:
1396 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1399 case ACPI_NOTIFY_EJECT_REQUEST:
1400 /* request device eject */
1401 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1402 if (!(acpiphp_disable_slot(func->slot)))
1403 acpiphp_eject_slot(func->slot);
1407 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1412 static int is_root_bridge(acpi_handle handle)
1415 struct acpi_device_info *info;
1416 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1419 status = acpi_get_object_info(handle, &buffer);
1420 if (ACPI_SUCCESS(status)) {
1421 info = buffer.pointer;
1422 if ((info->valid & ACPI_VALID_HID) &&
1423 !strcmp(PCI_ROOT_HID_STRING,
1424 info->hardware_id.value)) {
1425 acpi_os_free(buffer.pointer);
1428 if (info->valid & ACPI_VALID_CID) {
1429 for (i=0; i < info->compatibility_id.count; i++) {
1430 if (!strcmp(PCI_ROOT_HID_STRING,
1431 info->compatibility_id.id[i].value)) {
1432 acpi_os_free(buffer.pointer);
1442 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1444 int *count = (int *)context;
1446 if (is_root_bridge(handle)) {
1447 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1448 handle_hotplug_event_bridge, NULL);
1454 static struct acpi_pci_driver acpi_pci_hp_driver = {
1456 .remove = remove_bridge,
1460 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1463 int __init acpiphp_glue_init(void)
1467 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1468 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1473 acpi_pci_register_driver(&acpi_pci_hp_driver);
1480 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1482 * This function frees all data allocated in acpiphp_glue_init()
1484 void __exit acpiphp_glue_exit(void)
1486 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1491 * acpiphp_get_num_slots - count number of slots in a system
1493 int __init acpiphp_get_num_slots(void)
1495 struct list_head *node;
1496 struct acpiphp_bridge *bridge;
1501 list_for_each (node, &bridge_list) {
1502 bridge = (struct acpiphp_bridge *)node;
1503 dbg("Bus %04x:%02x has %d slot%s\n",
1504 pci_domain_nr(bridge->pci_bus),
1505 bridge->pci_bus->number, bridge->nr_slots,
1506 bridge->nr_slots == 1 ? "" : "s");
1507 num_slots += bridge->nr_slots;
1510 dbg("Total %d slots\n", num_slots);
1517 * acpiphp_for_each_slot - call function for each slot
1518 * @fn: callback function
1519 * @data: context to be passed to callback function
1522 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1524 struct list_head *node;
1525 struct acpiphp_bridge *bridge;
1526 struct acpiphp_slot *slot;
1529 list_for_each (node, &bridge_list) {
1530 bridge = (struct acpiphp_bridge *)node;
1531 for (slot = bridge->slots; slot; slot = slot->next) {
1532 retval = fn(slot, data);
1545 * acpiphp_enable_slot - power on slot
1547 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1551 mutex_lock(&slot->crit_sect);
1553 /* wake up all functions */
1554 retval = power_on_slot(slot);
1558 if (get_slot_status(slot) == ACPI_STA_ALL)
1559 /* configure all functions */
1560 retval = enable_device(slot);
1563 mutex_unlock(&slot->crit_sect);
1568 * acpiphp_disable_slot - power off slot
1570 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1574 mutex_lock(&slot->crit_sect);
1576 /* unconfigure all functions */
1577 retval = disable_device(slot);
1581 /* power off all functions */
1582 retval = power_off_slot(slot);
1587 mutex_unlock(&slot->crit_sect);
1596 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1598 return (slot->flags & SLOT_POWEREDON);
1606 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1610 sta = get_slot_status(slot);
1612 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1617 * adapter presence : 1
1620 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1624 sta = get_slot_status(slot);
1626 return (sta == 0) ? 0 : 1;
1631 * pci address (seg/bus/dev)
1633 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1636 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1638 address = (pci_domain_nr(pci_bus) << 16) |
1639 (pci_bus->number << 8) |