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 <asm/semaphore.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 handle_hotplug_event_func (acpi_handle, u32, void *);
63 * initialization & terminatation routines
67 * is_ejectable - determine if a slot is ejectable
68 * @handle: handle to acpi namespace
70 * Ejectable slot should satisfy at least these conditions:
83 static int is_ejectable(acpi_handle handle)
88 status = acpi_get_handle(handle, "_ADR", &tmp);
89 if (ACPI_FAILURE(status)) {
93 status = acpi_get_handle(handle, "_EJ0", &tmp);
94 if (ACPI_FAILURE(status)) {
102 /* callback routine to check the existence of ejectable slots */
104 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
106 int *count = (int *)context;
108 if (is_ejectable(handle)) {
110 /* only one ejectable slot is enough */
111 return AE_CTRL_TERMINATE;
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 adr, sun;
128 int device, function;
129 static int num_slots = 0; /* XXX if we support I/O node hotplug... */
131 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
133 if (ACPI_FAILURE(status))
136 status = acpi_get_handle(handle, "_EJ0", &tmp);
138 if (ACPI_FAILURE(status))
141 device = (adr >> 16) & 0xffff;
142 function = adr & 0xffff;
144 newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
147 memset(newfunc, 0, sizeof(struct acpiphp_func));
149 INIT_LIST_HEAD(&newfunc->sibling);
150 newfunc->handle = handle;
151 newfunc->function = function;
152 newfunc->flags = FUNC_HAS_EJ0;
154 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
155 newfunc->flags |= FUNC_HAS_STA;
157 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
158 newfunc->flags |= FUNC_HAS_PS0;
160 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
161 newfunc->flags |= FUNC_HAS_PS3;
163 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
164 if (ACPI_FAILURE(status))
167 /* search for objects that share the same slot */
168 for (slot = bridge->slots; slot; slot = slot->next)
169 if (slot->device == device) {
170 if (slot->sun != sun)
171 warn("sibling found, but _SUN doesn't match!\n");
176 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
182 memset(slot, 0, sizeof(struct acpiphp_slot));
183 slot->bridge = bridge;
184 slot->id = num_slots++;
185 slot->device = device;
187 INIT_LIST_HEAD(&slot->funcs);
188 init_MUTEX(&slot->crit_sect);
190 slot->next = bridge->slots;
191 bridge->slots = slot;
195 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
196 slot->sun, pci_domain_nr(bridge->pci_bus),
197 bridge->pci_bus->number, slot->device);
200 newfunc->slot = slot;
201 list_add_tail(&newfunc->sibling, &slot->funcs);
203 /* associate corresponding pci_dev */
204 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
205 PCI_DEVFN(device, function));
206 if (newfunc->pci_dev) {
207 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
210 /* install notify handler */
211 status = acpi_install_notify_handler(handle,
213 handle_hotplug_event_func,
216 if (ACPI_FAILURE(status)) {
217 err("failed to register interrupt notify handler\n");
225 /* see if it's worth looking at this bridge */
226 static int detect_ejectable_slots(acpi_handle *bridge_handle)
233 /* only check slots defined directly below bridge object */
234 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
235 is_ejectable_slot, (void *)&count, NULL);
241 /* decode ACPI 2.0 _HPP hot plug parameters */
242 static void decode_hpp(struct acpiphp_bridge *bridge)
245 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
247 union acpi_object *package;
250 /* default numbers */
251 bridge->hpp.cache_line_size = 0x10;
252 bridge->hpp.latency_timer = 0x40;
253 bridge->hpp.enable_SERR = 0;
254 bridge->hpp.enable_PERR = 0;
256 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
258 if (ACPI_FAILURE(status)) {
259 dbg("_HPP evaluation failed\n");
263 package = (union acpi_object *) buffer.pointer;
265 if (!package || package->type != ACPI_TYPE_PACKAGE ||
266 package->package.count != 4 || !package->package.elements) {
267 err("invalid _HPP object; ignoring\n");
271 for (i = 0; i < 4; i++) {
272 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
273 err("invalid _HPP parameter type; ignoring\n");
278 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
279 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
280 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
281 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
283 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
284 bridge->hpp.cache_line_size,
285 bridge->hpp.latency_timer,
286 bridge->hpp.enable_SERR,
287 bridge->hpp.enable_PERR);
289 bridge->flags |= BRIDGE_HAS_HPP;
292 kfree(buffer.pointer);
296 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
297 static void init_bridge_misc(struct acpiphp_bridge *bridge)
301 /* decode ACPI 2.0 _HPP (hot plug parameters) */
304 /* register all slot objects under this bridge */
305 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
306 register_slot, bridge, NULL);
308 /* install notify handler */
309 if (bridge->type != BRIDGE_TYPE_HOST) {
310 status = acpi_install_notify_handler(bridge->handle,
312 handle_hotplug_event_bridge,
315 if (ACPI_FAILURE(status)) {
316 err("failed to register interrupt notify handler\n");
320 list_add(&bridge->list, &bridge_list);
324 /* allocate and initialize host bridge data structure */
325 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
327 struct acpiphp_bridge *bridge;
329 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
333 memset(bridge, 0, sizeof(struct acpiphp_bridge));
335 bridge->type = BRIDGE_TYPE_HOST;
336 bridge->handle = handle;
338 bridge->pci_bus = pci_bus;
340 spin_lock_init(&bridge->res_lock);
342 init_bridge_misc(bridge);
346 /* allocate and initialize PCI-to-PCI bridge data structure */
347 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
349 struct acpiphp_bridge *bridge;
351 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
352 if (bridge == NULL) {
353 err("out of memory\n");
357 memset(bridge, 0, sizeof(struct acpiphp_bridge));
359 bridge->type = BRIDGE_TYPE_P2P;
360 bridge->handle = handle;
362 bridge->pci_dev = pci_dev_get(pci_dev);
363 bridge->pci_bus = pci_dev->subordinate;
364 if (!bridge->pci_bus) {
365 err("This is not a PCI-to-PCI bridge!\n");
369 spin_lock_init(&bridge->res_lock);
371 init_bridge_misc(bridge);
374 pci_dev_put(pci_dev);
380 /* callback routine to find P2P bridges */
382 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
385 acpi_handle dummy_handle;
387 int device, function;
389 struct pci_bus *pci_bus = context;
391 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
392 if (ACPI_FAILURE(status))
393 return AE_OK; /* continue */
395 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
396 if (ACPI_FAILURE(status)) {
397 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
401 device = (tmp >> 16) & 0xffff;
402 function = tmp & 0xffff;
404 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
406 if (!dev || !dev->subordinate)
409 /* check if this bridge has ejectable slots */
410 if (detect_ejectable_slots(handle) > 0) {
411 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
412 add_p2p_bridge(handle, dev);
421 /* find hot-pluggable slots, and then find P2P bridge */
422 static int add_bridge(acpi_handle handle)
427 acpi_handle dummy_handle;
428 struct pci_bus *pci_bus;
430 /* if the bridge doesn't have _STA, we assume it is always there */
431 status = acpi_get_handle(handle, "_STA", &dummy_handle);
432 if (ACPI_SUCCESS(status)) {
433 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
434 if (ACPI_FAILURE(status)) {
435 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
438 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
439 /* don't register this object */
443 /* get PCI segment number */
444 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
446 seg = ACPI_SUCCESS(status) ? tmp : 0;
448 /* get PCI bus number */
449 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
451 if (ACPI_SUCCESS(status)) {
454 warn("can't get bus number, assuming 0\n");
458 pci_bus = pci_find_bus(seg, bus);
460 err("Can't find bus %04x:%02x\n", seg, bus);
464 /* check if this bridge has ejectable slots */
465 if (detect_ejectable_slots(handle) > 0) {
466 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
467 add_host_bridge(handle, pci_bus);
471 /* search P2P bridges under this host bridge */
472 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
473 find_p2p_bridge, pci_bus, NULL);
475 if (ACPI_FAILURE(status))
476 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
481 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
483 struct list_head *head;
484 list_for_each(head, &bridge_list) {
485 struct acpiphp_bridge *bridge = list_entry(head,
486 struct acpiphp_bridge, list);
487 if (bridge->handle == handle)
494 static void cleanup_bridge(struct acpiphp_bridge *bridge)
496 struct list_head *list, *tmp;
497 struct acpiphp_slot *slot;
499 acpi_handle handle = bridge->handle;
501 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
502 handle_hotplug_event_bridge);
503 if (ACPI_FAILURE(status))
504 err("failed to remove notify handler\n");
506 slot = bridge->slots;
508 struct acpiphp_slot *next = slot->next;
509 list_for_each_safe (list, tmp, &slot->funcs) {
510 struct acpiphp_func *func;
511 func = list_entry(list, struct acpiphp_func, sibling);
512 status = acpi_remove_notify_handler(func->handle,
514 handle_hotplug_event_func);
515 if (ACPI_FAILURE(status))
516 err("failed to remove notify handler\n");
517 pci_dev_put(func->pci_dev);
525 pci_dev_put(bridge->pci_dev);
526 list_del(&bridge->list);
531 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
533 struct acpiphp_bridge *bridge;
535 if (!(bridge = acpiphp_handle_to_bridge(handle)))
537 cleanup_bridge(bridge);
541 static void remove_bridge(acpi_handle handle)
543 struct acpiphp_bridge *bridge;
545 bridge = acpiphp_handle_to_bridge(handle);
547 cleanup_bridge(bridge);
549 /* clean-up p2p bridges under this host bridge */
550 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
551 (u32)1, cleanup_p2p_bridge, NULL, NULL);
555 static int power_on_slot(struct acpiphp_slot *slot)
558 struct acpiphp_func *func;
562 /* if already enabled, just skip */
563 if (slot->flags & SLOT_POWEREDON)
566 list_for_each (l, &slot->funcs) {
567 func = list_entry(l, struct acpiphp_func, sibling);
569 if (func->flags & FUNC_HAS_PS0) {
570 dbg("%s: executing _PS0\n", __FUNCTION__);
571 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
572 if (ACPI_FAILURE(status)) {
573 warn("%s: _PS0 failed\n", __FUNCTION__);
581 /* TBD: evaluate _STA to check if the slot is enabled */
583 slot->flags |= SLOT_POWEREDON;
590 static int power_off_slot(struct acpiphp_slot *slot)
593 struct acpiphp_func *func;
598 /* if already disabled, just skip */
599 if ((slot->flags & SLOT_POWEREDON) == 0)
602 list_for_each (l, &slot->funcs) {
603 func = list_entry(l, struct acpiphp_func, sibling);
605 if (func->flags & FUNC_HAS_PS3) {
606 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
607 if (ACPI_FAILURE(status)) {
608 warn("%s: _PS3 failed\n", __FUNCTION__);
616 /* TBD: evaluate _STA to check if the slot is disabled */
618 slot->flags &= (~SLOT_POWEREDON);
626 * enable_device - enable, configure a slot
627 * @slot: slot to be enabled
629 * This function should be called per *physical slot*,
630 * not per each slot object in ACPI namespace.
633 static int enable_device(struct acpiphp_slot *slot)
636 struct pci_bus *bus = slot->bridge->pci_bus;
638 struct acpiphp_func *func;
642 if (slot->flags & SLOT_ENABLED)
645 /* sanity check: dev should be NULL when hot-plugged in */
646 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
648 /* This case shouldn't happen */
649 err("pci_dev structure already exists.\n");
655 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
657 err("No new device found\n");
662 max = bus->secondary;
663 for (pass = 0; pass < 2; pass++) {
664 list_for_each_entry(dev, &bus->devices, bus_list) {
665 if (PCI_SLOT(dev->devfn) != slot->device)
667 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
668 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
669 max = pci_scan_bridge(bus, dev, max, pass);
673 pci_bus_assign_resources(bus);
674 pci_bus_add_devices(bus);
676 /* associate pci_dev to our representation */
677 list_for_each (l, &slot->funcs) {
678 func = list_entry(l, struct acpiphp_func, sibling);
679 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
683 slot->flags |= SLOT_ENABLED;
691 * disable_device - disable a slot
693 static int disable_device(struct acpiphp_slot *slot)
696 struct acpiphp_func *func;
699 /* is this slot already disabled? */
700 if (!(slot->flags & SLOT_ENABLED))
703 list_for_each (l, &slot->funcs) {
704 func = list_entry(l, struct acpiphp_func, sibling);
708 pci_remove_bus_device(func->pci_dev);
709 pci_dev_put(func->pci_dev);
710 func->pci_dev = NULL;
713 slot->flags &= (~SLOT_ENABLED);
721 * get_slot_status - get ACPI slot status
723 * if a slot has _STA for each function and if any one of them
724 * returned non-zero status, return it
726 * if a slot doesn't have _STA and if any one of its functions'
727 * configuration space is configured, return 0x0f as a _STA
731 static unsigned int get_slot_status(struct acpiphp_slot *slot)
734 unsigned long sta = 0;
737 struct acpiphp_func *func;
739 list_for_each (l, &slot->funcs) {
740 func = list_entry(l, struct acpiphp_func, sibling);
742 if (func->flags & FUNC_HAS_STA) {
743 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
744 if (ACPI_SUCCESS(status) && sta)
747 pci_bus_read_config_dword(slot->bridge->pci_bus,
748 PCI_DEVFN(slot->device,
750 PCI_VENDOR_ID, &dvid);
751 if (dvid != 0xffffffff) {
758 return (unsigned int)sta;
762 * acpiphp_eject_slot - physically eject the slot
764 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
767 struct acpiphp_func *func;
769 struct acpi_object_list arg_list;
770 union acpi_object arg;
772 list_for_each (l, &slot->funcs) {
773 func = list_entry(l, struct acpiphp_func, sibling);
775 /* We don't want to call _EJ0 on non-existing functions. */
776 if ((func->flags & FUNC_HAS_EJ0)) {
777 /* _EJ0 method take one argument */
779 arg_list.pointer = &arg;
780 arg.type = ACPI_TYPE_INTEGER;
781 arg.integer.value = 1;
783 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
784 if (ACPI_FAILURE(status)) {
785 warn("%s: _EJ0 failed\n", __FUNCTION__);
795 * acpiphp_check_bridge - re-enumerate devices
797 * Iterate over all slots under this bridge and make sure that if a
798 * card is present they are enabled, and if not they are disabled.
800 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
802 struct acpiphp_slot *slot;
804 int enabled, disabled;
806 enabled = disabled = 0;
808 for (slot = bridge->slots; slot; slot = slot->next) {
809 unsigned int status = get_slot_status(slot);
810 if (slot->flags & SLOT_ENABLED) {
811 if (status == ACPI_STA_ALL)
813 retval = acpiphp_disable_slot(slot);
815 err("Error occurred in disabling\n");
818 acpiphp_eject_slot(slot);
822 if (status != ACPI_STA_ALL)
824 retval = acpiphp_enable_slot(slot);
826 err("Error occurred in enabling\n");
833 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
839 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
841 u16 pci_cmd, pci_bctl;
842 struct pci_dev *cdev;
844 /* Program hpp values for this device */
845 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
846 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
847 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
849 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
850 bridge->hpp.cache_line_size);
851 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
852 bridge->hpp.latency_timer);
853 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
854 if (bridge->hpp.enable_SERR)
855 pci_cmd |= PCI_COMMAND_SERR;
857 pci_cmd &= ~PCI_COMMAND_SERR;
858 if (bridge->hpp.enable_PERR)
859 pci_cmd |= PCI_COMMAND_PARITY;
861 pci_cmd &= ~PCI_COMMAND_PARITY;
862 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
864 /* Program bridge control value and child devices */
865 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
866 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
867 bridge->hpp.latency_timer);
868 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
869 if (bridge->hpp.enable_SERR)
870 pci_bctl |= PCI_BRIDGE_CTL_SERR;
872 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
873 if (bridge->hpp.enable_PERR)
874 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
876 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
877 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
878 if (dev->subordinate) {
879 list_for_each_entry(cdev, &dev->subordinate->devices,
881 program_hpp(cdev, bridge);
886 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
888 struct acpiphp_bridge bridge;
891 memset(&bridge, 0, sizeof(bridge));
892 bridge.handle = handle;
894 list_for_each_entry(dev, &bus->devices, bus_list)
895 program_hpp(dev, &bridge);
900 * Remove devices for which we could not assign resources, call
901 * arch specific code to fix-up the bus
903 static void acpiphp_sanitize_bus(struct pci_bus *bus)
907 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
909 list_for_each_entry(dev, &bus->devices, bus_list) {
910 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
911 struct resource *res = &dev->resource[i];
912 if ((res->flags & type_mask) && !res->start &&
914 /* Could not assign a required resources
915 * for this device, remove it */
916 pci_remove_bus_device(dev);
923 /* Program resources in newly inserted bridge */
924 static int acpiphp_configure_bridge (acpi_handle handle)
926 struct acpi_pci_id pci_id;
929 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
930 err("cannot get PCI domain and bus number for bridge\n");
933 bus = pci_find_bus(pci_id.segment, pci_id.bus);
935 err("cannot find bus %d:%d\n",
936 pci_id.segment, pci_id.bus);
940 pci_bus_size_bridges(bus);
941 pci_bus_assign_resources(bus);
942 acpiphp_sanitize_bus(bus);
943 acpiphp_set_hpp_values(handle, bus);
944 pci_enable_bridges(bus);
948 static void handle_bridge_insertion(acpi_handle handle, u32 type)
950 struct acpi_device *device, *pdevice;
953 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
954 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
955 err("unexpected notification type %d\n", type);
959 acpi_get_parent(handle, &phandle);
960 if (acpi_bus_get_device(phandle, &pdevice)) {
961 dbg("no parent device, assuming NULL\n");
964 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
965 err("cannot add bridge to acpi list\n");
968 if (!acpiphp_configure_bridge(handle) &&
969 !acpi_bus_start(device))
972 err("cannot configure and start bridge\n");
977 * ACPI event handlers
981 * handle_hotplug_event_bridge - handle ACPI event on bridges
983 * @handle: Notify()'ed acpi_handle
985 * @context: pointer to acpiphp_bridge structure
987 * handles ACPI event notification on {host,p2p} bridges
990 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
992 struct acpiphp_bridge *bridge;
994 struct acpi_buffer buffer = { .length = sizeof(objname),
995 .pointer = objname };
996 struct acpi_device *device;
998 if (acpi_bus_get_device(handle, &device)) {
999 /* This bridge must have just been physically inserted */
1000 handle_bridge_insertion(handle, type);
1004 bridge = acpiphp_handle_to_bridge(handle);
1006 err("cannot get bridge info\n");
1010 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1013 case ACPI_NOTIFY_BUS_CHECK:
1014 /* bus re-enumerate */
1015 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1016 acpiphp_check_bridge(bridge);
1019 case ACPI_NOTIFY_DEVICE_CHECK:
1021 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1022 acpiphp_check_bridge(bridge);
1025 case ACPI_NOTIFY_DEVICE_WAKE:
1027 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1030 case ACPI_NOTIFY_EJECT_REQUEST:
1031 /* request device eject */
1032 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1035 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1036 printk(KERN_ERR "Device %s cannot be configured due"
1037 " to a frequency mismatch\n", objname);
1040 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1041 printk(KERN_ERR "Device %s cannot be configured due"
1042 " to a bus mode mismatch\n", objname);
1045 case ACPI_NOTIFY_POWER_FAULT:
1046 printk(KERN_ERR "Device %s has suffered a power fault\n",
1051 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1057 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1059 * @handle: Notify()'ed acpi_handle
1060 * @type: Notify code
1061 * @context: pointer to acpiphp_func structure
1063 * handles ACPI event notification on slots
1066 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1068 struct acpiphp_func *func;
1070 struct acpi_buffer buffer = { .length = sizeof(objname),
1071 .pointer = objname };
1073 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1075 func = (struct acpiphp_func *)context;
1078 case ACPI_NOTIFY_BUS_CHECK:
1079 /* bus re-enumerate */
1080 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1081 acpiphp_enable_slot(func->slot);
1084 case ACPI_NOTIFY_DEVICE_CHECK:
1085 /* device check : re-enumerate from parent bus */
1086 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1087 acpiphp_check_bridge(func->slot->bridge);
1090 case ACPI_NOTIFY_DEVICE_WAKE:
1092 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1095 case ACPI_NOTIFY_EJECT_REQUEST:
1096 /* request device eject */
1097 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1098 if (!(acpiphp_disable_slot(func->slot)))
1099 acpiphp_eject_slot(func->slot);
1103 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1108 static int is_root_bridge(acpi_handle handle)
1111 struct acpi_device_info *info;
1112 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1115 status = acpi_get_object_info(handle, &buffer);
1116 if (ACPI_SUCCESS(status)) {
1117 info = buffer.pointer;
1118 if ((info->valid & ACPI_VALID_HID) &&
1119 !strcmp(PCI_ROOT_HID_STRING,
1120 info->hardware_id.value)) {
1121 acpi_os_free(buffer.pointer);
1124 if (info->valid & ACPI_VALID_CID) {
1125 for (i=0; i < info->compatibility_id.count; i++) {
1126 if (!strcmp(PCI_ROOT_HID_STRING,
1127 info->compatibility_id.id[i].value)) {
1128 acpi_os_free(buffer.pointer);
1138 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1140 int *count = (int *)context;
1142 if (is_root_bridge(handle)) {
1143 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1144 handle_hotplug_event_bridge, NULL);
1150 static struct acpi_pci_driver acpi_pci_hp_driver = {
1152 .remove = remove_bridge,
1156 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1159 int __init acpiphp_glue_init(void)
1163 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1164 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1169 acpi_pci_register_driver(&acpi_pci_hp_driver);
1176 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1178 * This function frees all data allocated in acpiphp_glue_init()
1180 void __exit acpiphp_glue_exit(void)
1182 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1187 * acpiphp_get_num_slots - count number of slots in a system
1189 int __init acpiphp_get_num_slots(void)
1191 struct list_head *node;
1192 struct acpiphp_bridge *bridge;
1197 list_for_each (node, &bridge_list) {
1198 bridge = (struct acpiphp_bridge *)node;
1199 dbg("Bus %04x:%02x has %d slot%s\n",
1200 pci_domain_nr(bridge->pci_bus),
1201 bridge->pci_bus->number, bridge->nr_slots,
1202 bridge->nr_slots == 1 ? "" : "s");
1203 num_slots += bridge->nr_slots;
1206 dbg("Total %d slots\n", num_slots);
1213 * acpiphp_for_each_slot - call function for each slot
1214 * @fn: callback function
1215 * @data: context to be passed to callback function
1218 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1220 struct list_head *node;
1221 struct acpiphp_bridge *bridge;
1222 struct acpiphp_slot *slot;
1225 list_for_each (node, &bridge_list) {
1226 bridge = (struct acpiphp_bridge *)node;
1227 for (slot = bridge->slots; slot; slot = slot->next) {
1228 retval = fn(slot, data);
1239 /* search matching slot from id */
1240 struct acpiphp_slot *get_slot_from_id(int id)
1242 struct list_head *node;
1243 struct acpiphp_bridge *bridge;
1244 struct acpiphp_slot *slot;
1246 list_for_each (node, &bridge_list) {
1247 bridge = (struct acpiphp_bridge *)node;
1248 for (slot = bridge->slots; slot; slot = slot->next)
1253 /* should never happen! */
1254 err("%s: no object for id %d\n", __FUNCTION__, id);
1261 * acpiphp_enable_slot - power on slot
1263 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1267 down(&slot->crit_sect);
1269 /* wake up all functions */
1270 retval = power_on_slot(slot);
1274 if (get_slot_status(slot) == ACPI_STA_ALL)
1275 /* configure all functions */
1276 retval = enable_device(slot);
1279 up(&slot->crit_sect);
1284 * acpiphp_disable_slot - power off slot
1286 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1290 down(&slot->crit_sect);
1292 /* unconfigure all functions */
1293 retval = disable_device(slot);
1297 /* power off all functions */
1298 retval = power_off_slot(slot);
1303 up(&slot->crit_sect);
1312 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1314 return (slot->flags & SLOT_POWEREDON);
1322 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1326 sta = get_slot_status(slot);
1328 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1333 * adapter presence : 1
1336 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1340 sta = get_slot_status(slot);
1342 return (sta == 0) ? 0 : 1;
1347 * pci address (seg/bus/dev)
1349 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1352 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1354 address = (pci_domain_nr(pci_bus) << 16) |
1355 (pci_bus->number << 8) |