Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pci / hotplug / acpiphp_glue.c
blob053ee843863c1891bc1398b968456cffd6fc31c0
1 /*
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
23 * details.
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
40 * is removed.
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>
51 #include "../pci.h"
52 #include "pci_hotplug.h"
53 #include "acpiphp.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
68 /**
69 * is_ejectable - determine if a slot is ejectable
70 * @handle: handle to acpi namespace
72 * Ejectable slot should satisfy at least these conditions:
74 * 1. has _ADR method
75 * 2. has _EJ0 method
77 * optionally
79 * 1. has _STA method
80 * 2. has _PS0 method
81 * 3. has _PS3 method
82 * 4. ..
85 static int is_ejectable(acpi_handle handle)
87 acpi_status status;
88 acpi_handle tmp;
90 status = acpi_get_handle(handle, "_ADR", &tmp);
91 if (ACPI_FAILURE(status)) {
92 return 0;
95 status = acpi_get_handle(handle, "_EJ0", &tmp);
96 if (ACPI_FAILURE(status)) {
97 return 0;
100 return 1;
104 /* callback routine to check the existence of ejectable slots */
105 static acpi_status
106 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
108 int *count = (int *)context;
110 if (is_ejectable(handle)) {
111 (*count)++;
112 /* only one ejectable slot is enough */
113 return AE_CTRL_TERMINATE;
114 } else {
115 return AE_OK;
120 /* callback routine to register each ACPI PCI slot object */
121 static acpi_status
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;
128 acpi_handle tmp;
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))
136 return AE_OK;
138 status = acpi_get_handle(handle, "_EJ0", &tmp);
140 if (ACPI_FAILURE(status) && !(is_dependent_device(handle)))
141 return AE_OK;
143 device = (adr >> 16) & 0xffff;
144 function = adr & 0xffff;
146 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
147 if (!newfunc)
148 return AE_NO_MEMORY;
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);
171 if (!dd)
172 err("Can't allocate memory for "
173 "new dependent device!\n");
174 else
175 add_dependent_device(dd);
178 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
179 if (ACPI_FAILURE(status))
180 sun = -1;
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");
187 break;
190 if (!slot) {
191 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
192 if (!slot) {
193 kfree(newfunc);
194 return AE_NO_MEMORY;
197 slot->bridge = bridge;
198 slot->device = device;
199 slot->sun = sun;
200 INIT_LIST_HEAD(&slot->funcs);
201 mutex_init(&slot->crit_sect);
203 slot->next = bridge->slots;
204 bridge->slots = slot;
206 bridge->nr_slots++;
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);
212 if (retval) {
213 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
214 goto err_exit;
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
230 * struct.
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;
240 dd->func = newfunc;
241 add_pci_dependent_device(dd);
244 /* install notify handler */
245 if (!(newfunc->flags & FUNC_HAS_DCK)) {
246 status = acpi_install_notify_handler(handle,
247 ACPI_SYSTEM_NOTIFY,
248 handle_hotplug_event_func,
249 newfunc);
251 if (ACPI_FAILURE(status))
252 err("failed to register interrupt notify handler\n");
253 } else
254 status = AE_OK;
256 return status;
258 err_exit:
259 bridge->nr_slots--;
260 bridge->slots = slot->next;
261 kfree(slot);
262 kfree(newfunc);
264 return AE_OK;
268 /* see if it's worth looking at this bridge */
269 static int detect_ejectable_slots(acpi_handle *bridge_handle)
271 acpi_status status;
272 int count;
274 count = 0;
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);
280 return count;
284 /* decode ACPI 2.0 _HPP hot plug parameters */
285 static void decode_hpp(struct acpiphp_bridge *bridge)
287 acpi_status status;
289 status = acpi_get_hp_params_from_firmware(bridge->pci_dev, &bridge->hpp);
290 if (ACPI_FAILURE(status)) {
291 /* use default numbers */
292 bridge->hpp.cache_line_size = 0x10;
293 bridge->hpp.latency_timer = 0x40;
294 bridge->hpp.enable_serr = 0;
295 bridge->hpp.enable_perr = 0;
301 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
302 static void init_bridge_misc(struct acpiphp_bridge *bridge)
304 acpi_status status;
306 /* decode ACPI 2.0 _HPP (hot plug parameters) */
307 decode_hpp(bridge);
309 /* must be added to the list prior to calling register_slot */
310 list_add(&bridge->list, &bridge_list);
312 /* register all slot objects under this bridge */
313 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
314 register_slot, bridge, NULL);
315 if (ACPI_FAILURE(status)) {
316 list_del(&bridge->list);
317 return;
320 /* install notify handler */
321 if (bridge->type != BRIDGE_TYPE_HOST) {
322 status = acpi_install_notify_handler(bridge->handle,
323 ACPI_SYSTEM_NOTIFY,
324 handle_hotplug_event_bridge,
325 bridge);
327 if (ACPI_FAILURE(status)) {
328 err("failed to register interrupt notify handler\n");
334 /* allocate and initialize host bridge data structure */
335 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
337 struct acpiphp_bridge *bridge;
339 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
340 if (bridge == NULL)
341 return;
343 bridge->type = BRIDGE_TYPE_HOST;
344 bridge->handle = handle;
346 bridge->pci_bus = pci_bus;
348 spin_lock_init(&bridge->res_lock);
350 init_bridge_misc(bridge);
354 /* allocate and initialize PCI-to-PCI bridge data structure */
355 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
357 struct acpiphp_bridge *bridge;
359 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
360 if (bridge == NULL) {
361 err("out of memory\n");
362 return;
365 bridge->type = BRIDGE_TYPE_P2P;
366 bridge->handle = handle;
368 bridge->pci_dev = pci_dev_get(pci_dev);
369 bridge->pci_bus = pci_dev->subordinate;
370 if (!bridge->pci_bus) {
371 err("This is not a PCI-to-PCI bridge!\n");
372 goto err;
375 spin_lock_init(&bridge->res_lock);
377 init_bridge_misc(bridge);
378 return;
379 err:
380 pci_dev_put(pci_dev);
381 kfree(bridge);
382 return;
386 /* callback routine to find P2P bridges */
387 static acpi_status
388 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
390 acpi_status status;
391 acpi_handle dummy_handle;
392 unsigned long tmp;
393 int device, function;
394 struct pci_dev *dev;
395 struct pci_bus *pci_bus = context;
397 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
398 if (ACPI_FAILURE(status))
399 return AE_OK; /* continue */
401 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
402 if (ACPI_FAILURE(status)) {
403 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
404 return AE_OK;
407 device = (tmp >> 16) & 0xffff;
408 function = tmp & 0xffff;
410 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
412 if (!dev || !dev->subordinate)
413 goto out;
415 /* check if this bridge has ejectable slots */
416 if ((detect_ejectable_slots(handle) > 0) ||
417 (detect_dependent_devices(handle) > 0)) {
418 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
419 add_p2p_bridge(handle, dev);
422 /* search P2P bridges under this p2p bridge */
423 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
424 find_p2p_bridge, dev->subordinate, NULL);
425 if (ACPI_FAILURE(status))
426 warn("find_p2p_bridge faied (error code = 0x%x)\n", status);
428 out:
429 pci_dev_put(dev);
430 return AE_OK;
434 /* find hot-pluggable slots, and then find P2P bridge */
435 static int add_bridge(acpi_handle handle)
437 acpi_status status;
438 unsigned long tmp;
439 int seg, bus;
440 acpi_handle dummy_handle;
441 struct pci_bus *pci_bus;
443 /* if the bridge doesn't have _STA, we assume it is always there */
444 status = acpi_get_handle(handle, "_STA", &dummy_handle);
445 if (ACPI_SUCCESS(status)) {
446 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
447 if (ACPI_FAILURE(status)) {
448 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
449 return 0;
451 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
452 /* don't register this object */
453 return 0;
456 /* get PCI segment number */
457 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
459 seg = ACPI_SUCCESS(status) ? tmp : 0;
461 /* get PCI bus number */
462 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
464 if (ACPI_SUCCESS(status)) {
465 bus = tmp;
466 } else {
467 warn("can't get bus number, assuming 0\n");
468 bus = 0;
471 pci_bus = pci_find_bus(seg, bus);
472 if (!pci_bus) {
473 err("Can't find bus %04x:%02x\n", seg, bus);
474 return 0;
477 /* check if this bridge has ejectable slots */
478 if (detect_ejectable_slots(handle) > 0) {
479 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
480 add_host_bridge(handle, pci_bus);
481 return 0;
484 /* search P2P bridges under this host bridge */
485 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
486 find_p2p_bridge, pci_bus, NULL);
488 if (ACPI_FAILURE(status))
489 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
491 return 0;
494 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
496 struct list_head *head;
497 list_for_each(head, &bridge_list) {
498 struct acpiphp_bridge *bridge = list_entry(head,
499 struct acpiphp_bridge, list);
500 if (bridge->handle == handle)
501 return bridge;
504 return NULL;
507 static void cleanup_bridge(struct acpiphp_bridge *bridge)
509 struct list_head *list, *tmp;
510 struct acpiphp_slot *slot;
511 acpi_status status;
512 acpi_handle handle = bridge->handle;
514 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
515 handle_hotplug_event_bridge);
516 if (ACPI_FAILURE(status))
517 err("failed to remove notify handler\n");
519 slot = bridge->slots;
520 while (slot) {
521 struct acpiphp_slot *next = slot->next;
522 list_for_each_safe (list, tmp, &slot->funcs) {
523 struct acpiphp_func *func;
524 func = list_entry(list, struct acpiphp_func, sibling);
525 if (!(func->flags & FUNC_HAS_DCK)) {
526 status = acpi_remove_notify_handler(func->handle,
527 ACPI_SYSTEM_NOTIFY,
528 handle_hotplug_event_func);
529 if (ACPI_FAILURE(status))
530 err("failed to remove notify handler\n");
532 pci_dev_put(func->pci_dev);
533 list_del(list);
534 kfree(func);
536 acpiphp_unregister_hotplug_slot(slot);
537 list_del(&slot->funcs);
538 kfree(slot);
539 slot = next;
542 pci_dev_put(bridge->pci_dev);
543 list_del(&bridge->list);
544 kfree(bridge);
547 static acpi_status
548 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
550 struct acpiphp_bridge *bridge;
552 if (!(bridge = acpiphp_handle_to_bridge(handle)))
553 return AE_OK;
554 cleanup_bridge(bridge);
555 return AE_OK;
558 static void remove_bridge(acpi_handle handle)
560 struct acpiphp_bridge *bridge;
562 bridge = acpiphp_handle_to_bridge(handle);
563 if (bridge) {
564 cleanup_bridge(bridge);
565 } else {
566 /* clean-up p2p bridges under this host bridge */
567 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
568 ACPI_UINT32_MAX, cleanup_p2p_bridge,
569 NULL, NULL);
573 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
575 struct acpi_pci_id id;
576 struct pci_bus *bus;
577 struct pci_dev *dev;
579 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
580 return NULL;
582 bus = pci_find_bus(id.segment, id.bus);
583 if (!bus)
584 return NULL;
586 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
587 if (!dev)
588 return NULL;
590 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
591 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
593 pci_dev_put(dev);
594 return NULL;
597 return dev;
600 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
602 acpi_status status;
603 int result = -1;
604 unsigned long gsb;
605 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
606 union acpi_object *obj;
607 void *table;
609 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
610 if (ACPI_SUCCESS(status)) {
611 *gsi_base = (u32)gsb;
612 return 0;
615 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
616 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
617 return -1;
619 obj = buffer.pointer;
620 if (obj->type != ACPI_TYPE_BUFFER)
621 goto out;
623 table = obj->buffer.pointer;
624 switch (((acpi_table_entry_header *)table)->type) {
625 case ACPI_MADT_IOSAPIC:
626 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
627 result = 0;
628 break;
629 case ACPI_MADT_IOAPIC:
630 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
631 result = 0;
632 break;
633 default:
634 break;
636 out:
637 acpi_os_free(buffer.pointer);
638 return result;
641 static acpi_status
642 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
644 acpi_status status;
645 unsigned long sta;
646 acpi_handle tmp;
647 struct pci_dev *pdev;
648 u32 gsi_base;
649 u64 phys_addr;
651 /* Evaluate _STA if present */
652 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
653 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
654 return AE_CTRL_DEPTH;
656 /* Scan only PCI bus scope */
657 status = acpi_get_handle(handle, "_HID", &tmp);
658 if (ACPI_SUCCESS(status))
659 return AE_CTRL_DEPTH;
661 if (get_gsi_base(handle, &gsi_base))
662 return AE_OK;
664 pdev = get_apic_pci_info(handle);
665 if (!pdev)
666 return AE_OK;
668 if (pci_enable_device(pdev)) {
669 pci_dev_put(pdev);
670 return AE_OK;
673 pci_set_master(pdev);
675 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
676 pci_disable_device(pdev);
677 pci_dev_put(pdev);
678 return AE_OK;
681 phys_addr = pci_resource_start(pdev, 0);
682 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
683 pci_release_region(pdev, 0);
684 pci_disable_device(pdev);
685 pci_dev_put(pdev);
686 return AE_OK;
689 return AE_OK;
692 static int acpiphp_configure_ioapics(acpi_handle handle)
694 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
695 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
696 return 0;
699 static int power_on_slot(struct acpiphp_slot *slot)
701 acpi_status status;
702 struct acpiphp_func *func;
703 struct list_head *l;
704 int retval = 0;
706 /* if already enabled, just skip */
707 if (slot->flags & SLOT_POWEREDON)
708 goto err_exit;
710 list_for_each (l, &slot->funcs) {
711 func = list_entry(l, struct acpiphp_func, sibling);
713 if (func->flags & FUNC_HAS_PS0) {
714 dbg("%s: executing _PS0\n", __FUNCTION__);
715 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
716 if (ACPI_FAILURE(status)) {
717 warn("%s: _PS0 failed\n", __FUNCTION__);
718 retval = -1;
719 goto err_exit;
720 } else
721 break;
725 /* TBD: evaluate _STA to check if the slot is enabled */
727 slot->flags |= SLOT_POWEREDON;
729 err_exit:
730 return retval;
734 static int power_off_slot(struct acpiphp_slot *slot)
736 acpi_status status;
737 struct acpiphp_func *func;
738 struct list_head *l;
740 int retval = 0;
742 /* if already disabled, just skip */
743 if ((slot->flags & SLOT_POWEREDON) == 0)
744 goto err_exit;
746 list_for_each (l, &slot->funcs) {
747 func = list_entry(l, struct acpiphp_func, sibling);
749 if (func->flags & FUNC_HAS_PS3) {
750 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
751 if (ACPI_FAILURE(status)) {
752 warn("%s: _PS3 failed\n", __FUNCTION__);
753 retval = -1;
754 goto err_exit;
755 } else
756 break;
760 /* TBD: evaluate _STA to check if the slot is disabled */
762 slot->flags &= (~SLOT_POWEREDON);
764 err_exit:
765 return retval;
771 * acpiphp_max_busnr - return the highest reserved bus number under
772 * the given bus.
773 * @bus: bus to start search with
776 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
778 struct list_head *tmp;
779 unsigned char max, n;
782 * pci_bus_max_busnr will return the highest
783 * reserved busnr for all these children.
784 * that is equivalent to the bus->subordinate
785 * value. We don't want to use the parent's
786 * bus->subordinate value because it could have
787 * padding in it.
789 max = bus->secondary;
791 list_for_each(tmp, &bus->children) {
792 n = pci_bus_max_busnr(pci_bus_b(tmp));
793 if (n > max)
794 max = n;
796 return max;
802 * get_func - get a pointer to acpiphp_func given a slot, device
803 * @slot: slot to search
804 * @dev: pci_dev struct to match.
806 * This function will increase the reference count of pci_dev,
807 * so callers should call pci_dev_put when complete.
810 static struct acpiphp_func *
811 get_func(struct acpiphp_slot *slot, struct pci_dev *dev)
813 struct acpiphp_func *func = NULL;
814 struct pci_bus *bus = slot->bridge->pci_bus;
815 struct pci_dev *pdev;
817 list_for_each_entry(func, &slot->funcs, sibling) {
818 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device,
819 func->function));
820 if (pdev) {
821 if (pdev == dev)
822 break;
823 pci_dev_put(pdev);
826 return func;
831 * acpiphp_bus_add - add a new bus to acpi subsystem
832 * @func: acpiphp_func of the bridge
835 static int acpiphp_bus_add(struct acpiphp_func *func)
837 acpi_handle phandle;
838 struct acpi_device *device, *pdevice;
839 int ret_val;
841 acpi_get_parent(func->handle, &phandle);
842 if (acpi_bus_get_device(phandle, &pdevice)) {
843 dbg("no parent device, assuming NULL\n");
844 pdevice = NULL;
846 if (!acpi_bus_get_device(func->handle, &device)) {
847 dbg("bus exists... trim\n");
848 /* this shouldn't be in here, so remove
849 * the bus then re-add it...
851 ret_val = acpi_bus_trim(device, 1);
852 dbg("acpi_bus_trim return %x\n", ret_val);
855 ret_val = acpi_bus_add(&device, pdevice, func->handle,
856 ACPI_BUS_TYPE_DEVICE);
857 if (ret_val) {
858 dbg("error adding bus, %x\n",
859 -ret_val);
860 goto acpiphp_bus_add_out;
863 * try to start anyway. We could have failed to add
864 * simply because this bus had previously been added
865 * on another add. Don't bother with the return value
866 * we just keep going.
868 ret_val = acpi_bus_start(device);
870 acpiphp_bus_add_out:
871 return ret_val;
877 * enable_device - enable, configure a slot
878 * @slot: slot to be enabled
880 * This function should be called per *physical slot*,
881 * not per each slot object in ACPI namespace.
884 static int enable_device(struct acpiphp_slot *slot)
886 struct pci_dev *dev;
887 struct pci_bus *bus = slot->bridge->pci_bus;
888 struct list_head *l;
889 struct acpiphp_func *func;
890 int retval = 0;
891 int num, max, pass;
893 if (slot->flags & SLOT_ENABLED)
894 goto err_exit;
896 /* sanity check: dev should be NULL when hot-plugged in */
897 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
898 if (dev) {
899 /* This case shouldn't happen */
900 err("pci_dev structure already exists.\n");
901 pci_dev_put(dev);
902 retval = -1;
903 goto err_exit;
906 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
907 if (num == 0) {
908 err("No new device found\n");
909 retval = -1;
910 goto err_exit;
913 max = acpiphp_max_busnr(bus);
914 for (pass = 0; pass < 2; pass++) {
915 list_for_each_entry(dev, &bus->devices, bus_list) {
916 if (PCI_SLOT(dev->devfn) != slot->device)
917 continue;
918 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
919 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
920 max = pci_scan_bridge(bus, dev, max, pass);
921 if (pass && dev->subordinate) {
922 pci_bus_size_bridges(dev->subordinate);
923 func = get_func(slot, dev);
924 if (func) {
925 acpiphp_bus_add(func);
926 /* side effect of get_func */
927 pci_dev_put(dev);
934 pci_bus_assign_resources(bus);
935 acpiphp_sanitize_bus(bus);
936 pci_enable_bridges(bus);
937 pci_bus_add_devices(bus);
938 acpiphp_set_hpp_values(slot->bridge->handle, bus);
939 acpiphp_configure_ioapics(slot->bridge->handle);
941 /* associate pci_dev to our representation */
942 list_for_each (l, &slot->funcs) {
943 func = list_entry(l, struct acpiphp_func, sibling);
944 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
945 func->function));
948 slot->flags |= SLOT_ENABLED;
950 err_exit:
951 return retval;
956 * disable_device - disable a slot
958 static int disable_device(struct acpiphp_slot *slot)
960 int retval = 0;
961 struct acpiphp_func *func;
962 struct list_head *l;
964 /* is this slot already disabled? */
965 if (!(slot->flags & SLOT_ENABLED))
966 goto err_exit;
968 list_for_each (l, &slot->funcs) {
969 func = list_entry(l, struct acpiphp_func, sibling);
970 if (!func->pci_dev)
971 continue;
973 pci_remove_bus_device(func->pci_dev);
974 pci_dev_put(func->pci_dev);
975 func->pci_dev = NULL;
978 slot->flags &= (~SLOT_ENABLED);
980 err_exit:
981 return retval;
986 * get_slot_status - get ACPI slot status
988 * if a slot has _STA for each function and if any one of them
989 * returned non-zero status, return it
991 * if a slot doesn't have _STA and if any one of its functions'
992 * configuration space is configured, return 0x0f as a _STA
994 * otherwise return 0
996 static unsigned int get_slot_status(struct acpiphp_slot *slot)
998 acpi_status status;
999 unsigned long sta = 0;
1000 u32 dvid;
1001 struct list_head *l;
1002 struct acpiphp_func *func;
1004 list_for_each (l, &slot->funcs) {
1005 func = list_entry(l, struct acpiphp_func, sibling);
1007 if (func->flags & FUNC_HAS_STA) {
1008 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1009 if (ACPI_SUCCESS(status) && sta)
1010 break;
1011 } else {
1012 pci_bus_read_config_dword(slot->bridge->pci_bus,
1013 PCI_DEVFN(slot->device,
1014 func->function),
1015 PCI_VENDOR_ID, &dvid);
1016 if (dvid != 0xffffffff) {
1017 sta = ACPI_STA_ALL;
1018 break;
1023 return (unsigned int)sta;
1027 * acpiphp_eject_slot - physically eject the slot
1029 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1031 acpi_status status;
1032 struct acpiphp_func *func;
1033 struct list_head *l;
1034 struct acpi_object_list arg_list;
1035 union acpi_object arg;
1037 list_for_each (l, &slot->funcs) {
1038 func = list_entry(l, struct acpiphp_func, sibling);
1040 /* We don't want to call _EJ0 on non-existing functions. */
1041 if ((func->flags & FUNC_HAS_EJ0)) {
1042 /* _EJ0 method take one argument */
1043 arg_list.count = 1;
1044 arg_list.pointer = &arg;
1045 arg.type = ACPI_TYPE_INTEGER;
1046 arg.integer.value = 1;
1048 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1049 if (ACPI_FAILURE(status)) {
1050 warn("%s: _EJ0 failed\n", __FUNCTION__);
1051 return -1;
1052 } else
1053 break;
1056 return 0;
1060 * acpiphp_check_bridge - re-enumerate devices
1062 * Iterate over all slots under this bridge and make sure that if a
1063 * card is present they are enabled, and if not they are disabled.
1065 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1067 struct acpiphp_slot *slot;
1068 int retval = 0;
1069 int enabled, disabled;
1071 enabled = disabled = 0;
1073 for (slot = bridge->slots; slot; slot = slot->next) {
1074 unsigned int status = get_slot_status(slot);
1075 if (slot->flags & SLOT_ENABLED) {
1076 if (status == ACPI_STA_ALL)
1077 continue;
1078 retval = acpiphp_disable_slot(slot);
1079 if (retval) {
1080 err("Error occurred in disabling\n");
1081 goto err_exit;
1082 } else {
1083 acpiphp_eject_slot(slot);
1085 disabled++;
1086 } else {
1087 if (status != ACPI_STA_ALL)
1088 continue;
1089 retval = acpiphp_enable_slot(slot);
1090 if (retval) {
1091 err("Error occurred in enabling\n");
1092 goto err_exit;
1094 enabled++;
1098 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1100 err_exit:
1101 return retval;
1104 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1106 u16 pci_cmd, pci_bctl;
1107 struct pci_dev *cdev;
1109 /* Program hpp values for this device */
1110 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1111 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1112 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1113 return;
1114 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1115 bridge->hpp.cache_line_size);
1116 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1117 bridge->hpp.latency_timer);
1118 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1119 if (bridge->hpp.enable_serr)
1120 pci_cmd |= PCI_COMMAND_SERR;
1121 else
1122 pci_cmd &= ~PCI_COMMAND_SERR;
1123 if (bridge->hpp.enable_perr)
1124 pci_cmd |= PCI_COMMAND_PARITY;
1125 else
1126 pci_cmd &= ~PCI_COMMAND_PARITY;
1127 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1129 /* Program bridge control value and child devices */
1130 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1131 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1132 bridge->hpp.latency_timer);
1133 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1134 if (bridge->hpp.enable_serr)
1135 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1136 else
1137 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1138 if (bridge->hpp.enable_perr)
1139 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1140 else
1141 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1142 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1143 if (dev->subordinate) {
1144 list_for_each_entry(cdev, &dev->subordinate->devices,
1145 bus_list)
1146 program_hpp(cdev, bridge);
1151 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1153 struct acpiphp_bridge bridge;
1154 struct pci_dev *dev;
1156 memset(&bridge, 0, sizeof(bridge));
1157 bridge.handle = handle;
1158 bridge.pci_dev = bus->self;
1159 decode_hpp(&bridge);
1160 list_for_each_entry(dev, &bus->devices, bus_list)
1161 program_hpp(dev, &bridge);
1166 * Remove devices for which we could not assign resources, call
1167 * arch specific code to fix-up the bus
1169 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1171 struct pci_dev *dev;
1172 int i;
1173 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1175 list_for_each_entry(dev, &bus->devices, bus_list) {
1176 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1177 struct resource *res = &dev->resource[i];
1178 if ((res->flags & type_mask) && !res->start &&
1179 res->end) {
1180 /* Could not assign a required resources
1181 * for this device, remove it */
1182 pci_remove_bus_device(dev);
1183 break;
1189 /* Program resources in newly inserted bridge */
1190 static int acpiphp_configure_bridge (acpi_handle handle)
1192 struct acpi_pci_id pci_id;
1193 struct pci_bus *bus;
1195 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1196 err("cannot get PCI domain and bus number for bridge\n");
1197 return -EINVAL;
1199 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1200 if (!bus) {
1201 err("cannot find bus %d:%d\n",
1202 pci_id.segment, pci_id.bus);
1203 return -EINVAL;
1206 pci_bus_size_bridges(bus);
1207 pci_bus_assign_resources(bus);
1208 acpiphp_sanitize_bus(bus);
1209 acpiphp_set_hpp_values(handle, bus);
1210 pci_enable_bridges(bus);
1211 acpiphp_configure_ioapics(handle);
1212 return 0;
1215 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1217 struct acpi_device *device, *pdevice;
1218 acpi_handle phandle;
1220 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1221 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1222 err("unexpected notification type %d\n", type);
1223 return;
1226 acpi_get_parent(handle, &phandle);
1227 if (acpi_bus_get_device(phandle, &pdevice)) {
1228 dbg("no parent device, assuming NULL\n");
1229 pdevice = NULL;
1231 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1232 err("cannot add bridge to acpi list\n");
1233 return;
1235 if (!acpiphp_configure_bridge(handle) &&
1236 !acpi_bus_start(device))
1237 add_bridge(handle);
1238 else
1239 err("cannot configure and start bridge\n");
1244 * ACPI event handlers
1248 * handle_hotplug_event_bridge - handle ACPI event on bridges
1250 * @handle: Notify()'ed acpi_handle
1251 * @type: Notify code
1252 * @context: pointer to acpiphp_bridge structure
1254 * handles ACPI event notification on {host,p2p} bridges
1257 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1259 struct acpiphp_bridge *bridge;
1260 char objname[64];
1261 struct acpi_buffer buffer = { .length = sizeof(objname),
1262 .pointer = objname };
1263 struct acpi_device *device;
1265 if (acpi_bus_get_device(handle, &device)) {
1266 /* This bridge must have just been physically inserted */
1267 handle_bridge_insertion(handle, type);
1268 return;
1271 bridge = acpiphp_handle_to_bridge(handle);
1272 if (!bridge) {
1273 err("cannot get bridge info\n");
1274 return;
1277 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1279 switch (type) {
1280 case ACPI_NOTIFY_BUS_CHECK:
1281 /* bus re-enumerate */
1282 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1283 acpiphp_check_bridge(bridge);
1284 break;
1286 case ACPI_NOTIFY_DEVICE_CHECK:
1287 /* device check */
1288 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1289 acpiphp_check_bridge(bridge);
1290 break;
1292 case ACPI_NOTIFY_DEVICE_WAKE:
1293 /* wake event */
1294 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1295 break;
1297 case ACPI_NOTIFY_EJECT_REQUEST:
1298 /* request device eject */
1299 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1300 break;
1302 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1303 printk(KERN_ERR "Device %s cannot be configured due"
1304 " to a frequency mismatch\n", objname);
1305 break;
1307 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1308 printk(KERN_ERR "Device %s cannot be configured due"
1309 " to a bus mode mismatch\n", objname);
1310 break;
1312 case ACPI_NOTIFY_POWER_FAULT:
1313 printk(KERN_ERR "Device %s has suffered a power fault\n",
1314 objname);
1315 break;
1317 default:
1318 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1319 break;
1324 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1326 * @handle: Notify()'ed acpi_handle
1327 * @type: Notify code
1328 * @context: pointer to acpiphp_func structure
1330 * handles ACPI event notification on slots
1333 void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1335 struct acpiphp_func *func;
1336 char objname[64];
1337 struct acpi_buffer buffer = { .length = sizeof(objname),
1338 .pointer = objname };
1340 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1342 func = (struct acpiphp_func *)context;
1344 switch (type) {
1345 case ACPI_NOTIFY_BUS_CHECK:
1346 /* bus re-enumerate */
1347 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1348 acpiphp_enable_slot(func->slot);
1349 break;
1351 case ACPI_NOTIFY_DEVICE_CHECK:
1352 /* device check : re-enumerate from parent bus */
1353 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1354 acpiphp_check_bridge(func->slot->bridge);
1355 break;
1357 case ACPI_NOTIFY_DEVICE_WAKE:
1358 /* wake event */
1359 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1360 break;
1362 case ACPI_NOTIFY_EJECT_REQUEST:
1363 /* request device eject */
1364 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1365 if (!(acpiphp_disable_slot(func->slot)))
1366 acpiphp_eject_slot(func->slot);
1367 break;
1369 default:
1370 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1371 break;
1376 static acpi_status
1377 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1379 int *count = (int *)context;
1381 if (acpi_root_bridge(handle)) {
1382 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1383 handle_hotplug_event_bridge, NULL);
1384 (*count)++;
1386 return AE_OK ;
1389 static struct acpi_pci_driver acpi_pci_hp_driver = {
1390 .add = add_bridge,
1391 .remove = remove_bridge,
1395 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1398 int __init acpiphp_glue_init(void)
1400 int num = 0;
1402 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1403 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1405 if (num <= 0)
1406 return -1;
1407 else
1408 acpi_pci_register_driver(&acpi_pci_hp_driver);
1410 return 0;
1415 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1417 * This function frees all data allocated in acpiphp_glue_init()
1419 void __exit acpiphp_glue_exit(void)
1421 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1426 * acpiphp_get_num_slots - count number of slots in a system
1428 int __init acpiphp_get_num_slots(void)
1430 struct list_head *node;
1431 struct acpiphp_bridge *bridge;
1432 int num_slots;
1434 num_slots = 0;
1436 list_for_each (node, &bridge_list) {
1437 bridge = (struct acpiphp_bridge *)node;
1438 dbg("Bus %04x:%02x has %d slot%s\n",
1439 pci_domain_nr(bridge->pci_bus),
1440 bridge->pci_bus->number, bridge->nr_slots,
1441 bridge->nr_slots == 1 ? "" : "s");
1442 num_slots += bridge->nr_slots;
1445 dbg("Total %d slots\n", num_slots);
1446 return num_slots;
1450 #if 0
1452 * acpiphp_for_each_slot - call function for each slot
1453 * @fn: callback function
1454 * @data: context to be passed to callback function
1457 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1459 struct list_head *node;
1460 struct acpiphp_bridge *bridge;
1461 struct acpiphp_slot *slot;
1462 int retval = 0;
1464 list_for_each (node, &bridge_list) {
1465 bridge = (struct acpiphp_bridge *)node;
1466 for (slot = bridge->slots; slot; slot = slot->next) {
1467 retval = fn(slot, data);
1468 if (!retval)
1469 goto err_exit;
1473 err_exit:
1474 return retval;
1476 #endif
1480 * acpiphp_enable_slot - power on slot
1482 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1484 int retval;
1486 mutex_lock(&slot->crit_sect);
1488 /* wake up all functions */
1489 retval = power_on_slot(slot);
1490 if (retval)
1491 goto err_exit;
1493 if (get_slot_status(slot) == ACPI_STA_ALL)
1494 /* configure all functions */
1495 retval = enable_device(slot);
1497 err_exit:
1498 mutex_unlock(&slot->crit_sect);
1499 return retval;
1503 * acpiphp_disable_slot - power off slot
1505 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1507 int retval = 0;
1509 mutex_lock(&slot->crit_sect);
1511 /* unconfigure all functions */
1512 retval = disable_device(slot);
1513 if (retval)
1514 goto err_exit;
1516 /* power off all functions */
1517 retval = power_off_slot(slot);
1518 if (retval)
1519 goto err_exit;
1521 err_exit:
1522 mutex_unlock(&slot->crit_sect);
1523 return retval;
1528 * slot enabled: 1
1529 * slot disabled: 0
1531 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1533 return (slot->flags & SLOT_POWEREDON);
1538 * latch closed: 1
1539 * latch open: 0
1541 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1543 unsigned int sta;
1545 sta = get_slot_status(slot);
1547 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1552 * adapter presence : 1
1553 * absence : 0
1555 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1557 unsigned int sta;
1559 sta = get_slot_status(slot);
1561 return (sta == 0) ? 0 : 1;
1566 * pci address (seg/bus/dev)
1568 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1570 u32 address;
1571 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1573 address = (pci_domain_nr(pci_bus) << 16) |
1574 (pci_bus->number << 8) |
1575 slot->device;
1577 return address;