[PATCH] solaris emulation: incorrect tty locking
[linux-2.6.22.y-op.git] / drivers / pci / hotplug / acpiphp_glue.c
blob83e8e4412de5834fff05c3558cf911d94146f48f
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 <kristen.c.accardi@intel.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);
56 static LIST_HEAD(ioapic_list);
57 static DEFINE_SPINLOCK(ioapic_list_lock);
59 #define MY_NAME "acpiphp_glue"
61 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
62 static void acpiphp_sanitize_bus(struct pci_bus *bus);
63 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
64 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
68 * initialization & terminatation routines
71 /**
72 * is_ejectable - determine if a slot is ejectable
73 * @handle: handle to acpi namespace
75 * Ejectable slot should satisfy at least these conditions:
77 * 1. has _ADR method
78 * 2. has _EJ0 method
80 * optionally
82 * 1. has _STA method
83 * 2. has _PS0 method
84 * 3. has _PS3 method
85 * 4. ..
88 static int is_ejectable(acpi_handle handle)
90 acpi_status status;
91 acpi_handle tmp;
93 status = acpi_get_handle(handle, "_ADR", &tmp);
94 if (ACPI_FAILURE(status)) {
95 return 0;
98 status = acpi_get_handle(handle, "_EJ0", &tmp);
99 if (ACPI_FAILURE(status)) {
100 return 0;
103 return 1;
107 /* callback routine to check the existence of ejectable slots */
108 static acpi_status
109 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
111 int *count = (int *)context;
113 if (is_ejectable(handle)) {
114 (*count)++;
115 /* only one ejectable slot is enough */
116 return AE_CTRL_TERMINATE;
117 } else {
118 return AE_OK;
122 /* callback routine to check for the existance of a pci dock device */
123 static acpi_status
124 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
126 int *count = (int *)context;
128 if (is_dock_device(handle)) {
129 (*count)++;
130 return AE_CTRL_TERMINATE;
131 } else {
132 return AE_OK;
140 * the _DCK method can do funny things... and sometimes not
141 * hah-hah funny.
143 * TBD - figure out a way to only call fixups for
144 * systems that require them.
146 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
147 void *v)
149 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
150 struct pci_bus *bus = func->slot->bridge->pci_bus;
151 u32 buses;
153 if (!bus->self)
154 return NOTIFY_OK;
156 /* fixup bad _DCK function that rewrites
157 * secondary bridge on slot
159 pci_read_config_dword(bus->self,
160 PCI_PRIMARY_BUS,
161 &buses);
163 if (((buses >> 8) & 0xff) != bus->secondary) {
164 buses = (buses & 0xff000000)
165 | ((unsigned int)(bus->primary) << 0)
166 | ((unsigned int)(bus->secondary) << 8)
167 | ((unsigned int)(bus->subordinate) << 16);
168 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
170 return NOTIFY_OK;
176 /* callback routine to register each ACPI PCI slot object */
177 static acpi_status
178 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
180 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
181 struct acpiphp_slot *slot;
182 struct acpiphp_func *newfunc;
183 acpi_handle tmp;
184 acpi_status status = AE_OK;
185 unsigned long adr, sun;
186 int device, function, retval;
188 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
190 if (ACPI_FAILURE(status))
191 return AE_OK;
193 status = acpi_get_handle(handle, "_EJ0", &tmp);
195 if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
196 return AE_OK;
198 device = (adr >> 16) & 0xffff;
199 function = adr & 0xffff;
201 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
202 if (!newfunc)
203 return AE_NO_MEMORY;
205 INIT_LIST_HEAD(&newfunc->sibling);
206 newfunc->handle = handle;
207 newfunc->function = function;
208 if (ACPI_SUCCESS(status))
209 newfunc->flags = FUNC_HAS_EJ0;
211 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
212 newfunc->flags |= FUNC_HAS_STA;
214 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
215 newfunc->flags |= FUNC_HAS_PS0;
217 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
218 newfunc->flags |= FUNC_HAS_PS3;
220 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
221 newfunc->flags |= FUNC_HAS_DCK;
223 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
224 if (ACPI_FAILURE(status)) {
226 * use the count of the number of slots we've found
227 * for the number of the slot
229 sun = bridge->nr_slots+1;
232 /* search for objects that share the same slot */
233 for (slot = bridge->slots; slot; slot = slot->next)
234 if (slot->device == device) {
235 if (slot->sun != sun)
236 warn("sibling found, but _SUN doesn't match!\n");
237 break;
240 if (!slot) {
241 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
242 if (!slot) {
243 kfree(newfunc);
244 return AE_NO_MEMORY;
247 slot->bridge = bridge;
248 slot->device = device;
249 slot->sun = sun;
250 INIT_LIST_HEAD(&slot->funcs);
251 mutex_init(&slot->crit_sect);
253 slot->next = bridge->slots;
254 bridge->slots = slot;
256 bridge->nr_slots++;
258 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
259 slot->sun, pci_domain_nr(bridge->pci_bus),
260 bridge->pci_bus->number, slot->device);
261 retval = acpiphp_register_hotplug_slot(slot);
262 if (retval) {
263 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
264 goto err_exit;
268 newfunc->slot = slot;
269 list_add_tail(&newfunc->sibling, &slot->funcs);
271 /* associate corresponding pci_dev */
272 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
273 PCI_DEVFN(device, function));
274 if (newfunc->pci_dev) {
275 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
278 if (is_dock_device(handle)) {
279 /* we don't want to call this device's _EJ0
280 * because we want the dock notify handler
281 * to call it after it calls _DCK
283 newfunc->flags &= ~FUNC_HAS_EJ0;
284 if (register_hotplug_dock_device(handle,
285 handle_hotplug_event_func, newfunc))
286 dbg("failed to register dock device\n");
288 /* we need to be notified when dock events happen
289 * outside of the hotplug operation, since we may
290 * need to do fixups before we can hotplug.
292 newfunc->nb.notifier_call = post_dock_fixups;
293 if (register_dock_notifier(&newfunc->nb))
294 dbg("failed to register a dock notifier");
297 /* install notify handler */
298 if (!(newfunc->flags & FUNC_HAS_DCK)) {
299 status = acpi_install_notify_handler(handle,
300 ACPI_SYSTEM_NOTIFY,
301 handle_hotplug_event_func,
302 newfunc);
304 if (ACPI_FAILURE(status))
305 err("failed to register interrupt notify handler\n");
306 } else
307 status = AE_OK;
309 return status;
311 err_exit:
312 bridge->nr_slots--;
313 bridge->slots = slot->next;
314 kfree(slot);
315 kfree(newfunc);
317 return AE_OK;
321 /* see if it's worth looking at this bridge */
322 static int detect_ejectable_slots(acpi_handle *bridge_handle)
324 acpi_status status;
325 int count;
327 count = 0;
329 /* only check slots defined directly below bridge object */
330 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
331 is_ejectable_slot, (void *)&count, NULL);
334 * we also need to add this bridge if there is a dock bridge or
335 * other pci device on a dock station (removable)
337 if (!count)
338 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
339 (u32)1, is_pci_dock_device, (void *)&count,
340 NULL);
342 return count;
346 /* decode ACPI 2.0 _HPP hot plug parameters */
347 static void decode_hpp(struct acpiphp_bridge *bridge)
349 acpi_status status;
351 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
352 if (ACPI_FAILURE(status) ||
353 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
354 /* use default numbers */
355 printk(KERN_WARNING
356 "%s: Could not get hotplug parameters. Use defaults\n",
357 __FUNCTION__);
358 bridge->hpp.t0 = &bridge->hpp.type0_data;
359 bridge->hpp.t0->revision = 0;
360 bridge->hpp.t0->cache_line_size = 0x10;
361 bridge->hpp.t0->latency_timer = 0x40;
362 bridge->hpp.t0->enable_serr = 0;
363 bridge->hpp.t0->enable_perr = 0;
369 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
370 static void init_bridge_misc(struct acpiphp_bridge *bridge)
372 acpi_status status;
374 /* decode ACPI 2.0 _HPP (hot plug parameters) */
375 decode_hpp(bridge);
377 /* must be added to the list prior to calling register_slot */
378 list_add(&bridge->list, &bridge_list);
380 /* register all slot objects under this bridge */
381 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
382 register_slot, bridge, NULL);
383 if (ACPI_FAILURE(status)) {
384 list_del(&bridge->list);
385 return;
388 /* install notify handler */
389 if (bridge->type != BRIDGE_TYPE_HOST) {
390 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
391 status = acpi_remove_notify_handler(bridge->func->handle,
392 ACPI_SYSTEM_NOTIFY,
393 handle_hotplug_event_func);
394 if (ACPI_FAILURE(status))
395 err("failed to remove notify handler\n");
397 status = acpi_install_notify_handler(bridge->handle,
398 ACPI_SYSTEM_NOTIFY,
399 handle_hotplug_event_bridge,
400 bridge);
402 if (ACPI_FAILURE(status)) {
403 err("failed to register interrupt notify handler\n");
409 /* find acpiphp_func from acpiphp_bridge */
410 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
412 struct list_head *node, *l;
413 struct acpiphp_bridge *bridge;
414 struct acpiphp_slot *slot;
415 struct acpiphp_func *func;
417 list_for_each(node, &bridge_list) {
418 bridge = list_entry(node, struct acpiphp_bridge, list);
419 for (slot = bridge->slots; slot; slot = slot->next) {
420 list_for_each(l, &slot->funcs) {
421 func = list_entry(l, struct acpiphp_func,
422 sibling);
423 if (func->handle == handle)
424 return func;
429 return NULL;
433 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
435 acpi_handle dummy_handle;
437 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
438 "_STA", &dummy_handle)))
439 bridge->flags |= BRIDGE_HAS_STA;
441 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
442 "_EJ0", &dummy_handle)))
443 bridge->flags |= BRIDGE_HAS_EJ0;
445 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
446 "_PS0", &dummy_handle)))
447 bridge->flags |= BRIDGE_HAS_PS0;
449 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
450 "_PS3", &dummy_handle)))
451 bridge->flags |= BRIDGE_HAS_PS3;
453 /* is this ejectable p2p bridge? */
454 if (bridge->flags & BRIDGE_HAS_EJ0) {
455 struct acpiphp_func *func;
457 dbg("found ejectable p2p bridge\n");
459 /* make link between PCI bridge and PCI function */
460 func = acpiphp_bridge_handle_to_function(bridge->handle);
461 if (!func)
462 return;
463 bridge->func = func;
464 func->bridge = bridge;
469 /* allocate and initialize host bridge data structure */
470 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
472 struct acpiphp_bridge *bridge;
474 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
475 if (bridge == NULL)
476 return;
478 bridge->type = BRIDGE_TYPE_HOST;
479 bridge->handle = handle;
481 bridge->pci_bus = pci_bus;
483 spin_lock_init(&bridge->res_lock);
485 init_bridge_misc(bridge);
489 /* allocate and initialize PCI-to-PCI bridge data structure */
490 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
492 struct acpiphp_bridge *bridge;
494 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
495 if (bridge == NULL) {
496 err("out of memory\n");
497 return;
500 bridge->type = BRIDGE_TYPE_P2P;
501 bridge->handle = handle;
502 config_p2p_bridge_flags(bridge);
504 bridge->pci_dev = pci_dev_get(pci_dev);
505 bridge->pci_bus = pci_dev->subordinate;
506 if (!bridge->pci_bus) {
507 err("This is not a PCI-to-PCI bridge!\n");
508 goto err;
511 spin_lock_init(&bridge->res_lock);
513 init_bridge_misc(bridge);
514 return;
515 err:
516 pci_dev_put(pci_dev);
517 kfree(bridge);
518 return;
522 /* callback routine to find P2P bridges */
523 static acpi_status
524 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
526 acpi_status status;
527 acpi_handle dummy_handle;
528 unsigned long tmp;
529 int device, function;
530 struct pci_dev *dev;
531 struct pci_bus *pci_bus = context;
533 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
534 if (ACPI_FAILURE(status))
535 return AE_OK; /* continue */
537 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
538 if (ACPI_FAILURE(status)) {
539 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
540 return AE_OK;
543 device = (tmp >> 16) & 0xffff;
544 function = tmp & 0xffff;
546 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
548 if (!dev || !dev->subordinate)
549 goto out;
551 /* check if this bridge has ejectable slots */
552 if ((detect_ejectable_slots(handle) > 0)) {
553 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
554 add_p2p_bridge(handle, dev);
557 /* search P2P bridges under this p2p bridge */
558 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
559 find_p2p_bridge, dev->subordinate, NULL);
560 if (ACPI_FAILURE(status))
561 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
563 out:
564 pci_dev_put(dev);
565 return AE_OK;
569 /* find hot-pluggable slots, and then find P2P bridge */
570 static int add_bridge(acpi_handle handle)
572 acpi_status status;
573 unsigned long tmp;
574 int seg, bus;
575 acpi_handle dummy_handle;
576 struct pci_bus *pci_bus;
578 /* if the bridge doesn't have _STA, we assume it is always there */
579 status = acpi_get_handle(handle, "_STA", &dummy_handle);
580 if (ACPI_SUCCESS(status)) {
581 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
582 if (ACPI_FAILURE(status)) {
583 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
584 return 0;
586 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
587 /* don't register this object */
588 return 0;
591 /* get PCI segment number */
592 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
594 seg = ACPI_SUCCESS(status) ? tmp : 0;
596 /* get PCI bus number */
597 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
599 if (ACPI_SUCCESS(status)) {
600 bus = tmp;
601 } else {
602 warn("can't get bus number, assuming 0\n");
603 bus = 0;
606 pci_bus = pci_find_bus(seg, bus);
607 if (!pci_bus) {
608 err("Can't find bus %04x:%02x\n", seg, bus);
609 return 0;
612 /* check if this bridge has ejectable slots */
613 if (detect_ejectable_slots(handle) > 0) {
614 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
615 add_host_bridge(handle, pci_bus);
618 /* search P2P bridges under this host bridge */
619 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
620 find_p2p_bridge, pci_bus, NULL);
622 if (ACPI_FAILURE(status))
623 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
625 return 0;
628 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
630 struct list_head *head;
631 list_for_each(head, &bridge_list) {
632 struct acpiphp_bridge *bridge = list_entry(head,
633 struct acpiphp_bridge, list);
634 if (bridge->handle == handle)
635 return bridge;
638 return NULL;
641 static void cleanup_bridge(struct acpiphp_bridge *bridge)
643 struct list_head *list, *tmp;
644 struct acpiphp_slot *slot;
645 acpi_status status;
646 acpi_handle handle = bridge->handle;
648 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
649 handle_hotplug_event_bridge);
650 if (ACPI_FAILURE(status))
651 err("failed to remove notify handler\n");
653 if ((bridge->type != BRIDGE_TYPE_HOST) &&
654 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
655 status = acpi_install_notify_handler(bridge->func->handle,
656 ACPI_SYSTEM_NOTIFY,
657 handle_hotplug_event_func,
658 bridge->func);
659 if (ACPI_FAILURE(status))
660 err("failed to install interrupt notify handler\n");
663 slot = bridge->slots;
664 while (slot) {
665 struct acpiphp_slot *next = slot->next;
666 list_for_each_safe (list, tmp, &slot->funcs) {
667 struct acpiphp_func *func;
668 func = list_entry(list, struct acpiphp_func, sibling);
669 if (is_dock_device(func->handle)) {
670 unregister_hotplug_dock_device(func->handle);
671 unregister_dock_notifier(&func->nb);
673 if (!(func->flags & FUNC_HAS_DCK)) {
674 status = acpi_remove_notify_handler(func->handle,
675 ACPI_SYSTEM_NOTIFY,
676 handle_hotplug_event_func);
677 if (ACPI_FAILURE(status))
678 err("failed to remove notify handler\n");
680 pci_dev_put(func->pci_dev);
681 list_del(list);
682 kfree(func);
684 acpiphp_unregister_hotplug_slot(slot);
685 list_del(&slot->funcs);
686 kfree(slot);
687 slot = next;
690 pci_dev_put(bridge->pci_dev);
691 list_del(&bridge->list);
692 kfree(bridge);
695 static acpi_status
696 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
698 struct acpiphp_bridge *bridge;
700 /* cleanup p2p bridges under this P2P bridge
701 in a depth-first manner */
702 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
703 cleanup_p2p_bridge, NULL, NULL);
705 if (!(bridge = acpiphp_handle_to_bridge(handle)))
706 return AE_OK;
707 cleanup_bridge(bridge);
708 return AE_OK;
711 static void remove_bridge(acpi_handle handle)
713 struct acpiphp_bridge *bridge;
715 /* cleanup p2p bridges under this host bridge
716 in a depth-first manner */
717 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
718 (u32)1, cleanup_p2p_bridge, NULL, NULL);
720 bridge = acpiphp_handle_to_bridge(handle);
721 if (bridge)
722 cleanup_bridge(bridge);
725 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
727 struct acpi_pci_id id;
728 struct pci_bus *bus;
729 struct pci_dev *dev;
731 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
732 return NULL;
734 bus = pci_find_bus(id.segment, id.bus);
735 if (!bus)
736 return NULL;
738 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
739 if (!dev)
740 return NULL;
742 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
743 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
745 pci_dev_put(dev);
746 return NULL;
749 return dev;
752 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
754 acpi_status status;
755 int result = -1;
756 unsigned long gsb;
757 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
758 union acpi_object *obj;
759 void *table;
761 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
762 if (ACPI_SUCCESS(status)) {
763 *gsi_base = (u32)gsb;
764 return 0;
767 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
768 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
769 return -1;
771 obj = buffer.pointer;
772 if (obj->type != ACPI_TYPE_BUFFER)
773 goto out;
775 table = obj->buffer.pointer;
776 switch (((acpi_table_entry_header *)table)->type) {
777 case ACPI_MADT_IOSAPIC:
778 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
779 result = 0;
780 break;
781 case ACPI_MADT_IOAPIC:
782 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
783 result = 0;
784 break;
785 default:
786 break;
788 out:
789 kfree(buffer.pointer);
790 return result;
793 static acpi_status
794 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
796 acpi_status status;
797 unsigned long sta;
798 acpi_handle tmp;
799 struct pci_dev *pdev;
800 u32 gsi_base;
801 u64 phys_addr;
802 struct acpiphp_ioapic *ioapic;
804 /* Evaluate _STA if present */
805 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
806 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
807 return AE_CTRL_DEPTH;
809 /* Scan only PCI bus scope */
810 status = acpi_get_handle(handle, "_HID", &tmp);
811 if (ACPI_SUCCESS(status))
812 return AE_CTRL_DEPTH;
814 if (get_gsi_base(handle, &gsi_base))
815 return AE_OK;
817 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
818 if (!ioapic)
819 return AE_NO_MEMORY;
821 pdev = get_apic_pci_info(handle);
822 if (!pdev)
823 goto exit_kfree;
825 if (pci_enable_device(pdev))
826 goto exit_pci_dev_put;
828 pci_set_master(pdev);
830 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
831 goto exit_pci_disable_device;
833 phys_addr = pci_resource_start(pdev, 0);
834 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
835 goto exit_pci_release_region;
837 ioapic->gsi_base = gsi_base;
838 ioapic->dev = pdev;
839 spin_lock(&ioapic_list_lock);
840 list_add_tail(&ioapic->list, &ioapic_list);
841 spin_unlock(&ioapic_list_lock);
843 return AE_OK;
845 exit_pci_release_region:
846 pci_release_region(pdev, 0);
847 exit_pci_disable_device:
848 pci_disable_device(pdev);
849 exit_pci_dev_put:
850 pci_dev_put(pdev);
851 exit_kfree:
852 kfree(ioapic);
854 return AE_OK;
857 static acpi_status
858 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
860 acpi_status status;
861 unsigned long sta;
862 acpi_handle tmp;
863 u32 gsi_base;
864 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
866 /* Evaluate _STA if present */
867 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
868 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
869 return AE_CTRL_DEPTH;
871 /* Scan only PCI bus scope */
872 status = acpi_get_handle(handle, "_HID", &tmp);
873 if (ACPI_SUCCESS(status))
874 return AE_CTRL_DEPTH;
876 if (get_gsi_base(handle, &gsi_base))
877 return AE_OK;
879 acpi_unregister_ioapic(handle, gsi_base);
881 spin_lock(&ioapic_list_lock);
882 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
883 if (pos->gsi_base != gsi_base)
884 continue;
885 ioapic = pos;
886 list_del(&ioapic->list);
887 break;
889 spin_unlock(&ioapic_list_lock);
891 if (!ioapic)
892 return AE_OK;
894 pci_release_region(ioapic->dev, 0);
895 pci_disable_device(ioapic->dev);
896 pci_dev_put(ioapic->dev);
897 kfree(ioapic);
899 return AE_OK;
902 static int acpiphp_configure_ioapics(acpi_handle handle)
904 ioapic_add(handle, 0, NULL, NULL);
905 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
906 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
907 return 0;
910 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
912 ioapic_remove(handle, 0, NULL, NULL);
913 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
914 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
915 return 0;
918 static int power_on_slot(struct acpiphp_slot *slot)
920 acpi_status status;
921 struct acpiphp_func *func;
922 struct list_head *l;
923 int retval = 0;
925 /* if already enabled, just skip */
926 if (slot->flags & SLOT_POWEREDON)
927 goto err_exit;
929 list_for_each (l, &slot->funcs) {
930 func = list_entry(l, struct acpiphp_func, sibling);
932 if (func->flags & FUNC_HAS_PS0) {
933 dbg("%s: executing _PS0\n", __FUNCTION__);
934 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
935 if (ACPI_FAILURE(status)) {
936 warn("%s: _PS0 failed\n", __FUNCTION__);
937 retval = -1;
938 goto err_exit;
939 } else
940 break;
944 /* TBD: evaluate _STA to check if the slot is enabled */
946 slot->flags |= SLOT_POWEREDON;
948 err_exit:
949 return retval;
953 static int power_off_slot(struct acpiphp_slot *slot)
955 acpi_status status;
956 struct acpiphp_func *func;
957 struct list_head *l;
959 int retval = 0;
961 /* if already disabled, just skip */
962 if ((slot->flags & SLOT_POWEREDON) == 0)
963 goto err_exit;
965 list_for_each (l, &slot->funcs) {
966 func = list_entry(l, struct acpiphp_func, sibling);
968 if (func->flags & FUNC_HAS_PS3) {
969 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
970 if (ACPI_FAILURE(status)) {
971 warn("%s: _PS3 failed\n", __FUNCTION__);
972 retval = -1;
973 goto err_exit;
974 } else
975 break;
979 /* TBD: evaluate _STA to check if the slot is disabled */
981 slot->flags &= (~SLOT_POWEREDON);
983 err_exit:
984 return retval;
990 * acpiphp_max_busnr - return the highest reserved bus number under
991 * the given bus.
992 * @bus: bus to start search with
995 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
997 struct list_head *tmp;
998 unsigned char max, n;
1001 * pci_bus_max_busnr will return the highest
1002 * reserved busnr for all these children.
1003 * that is equivalent to the bus->subordinate
1004 * value. We don't want to use the parent's
1005 * bus->subordinate value because it could have
1006 * padding in it.
1008 max = bus->secondary;
1010 list_for_each(tmp, &bus->children) {
1011 n = pci_bus_max_busnr(pci_bus_b(tmp));
1012 if (n > max)
1013 max = n;
1015 return max;
1020 * acpiphp_bus_add - add a new bus to acpi subsystem
1021 * @func: acpiphp_func of the bridge
1024 static int acpiphp_bus_add(struct acpiphp_func *func)
1026 acpi_handle phandle;
1027 struct acpi_device *device, *pdevice;
1028 int ret_val;
1030 acpi_get_parent(func->handle, &phandle);
1031 if (acpi_bus_get_device(phandle, &pdevice)) {
1032 dbg("no parent device, assuming NULL\n");
1033 pdevice = NULL;
1035 if (!acpi_bus_get_device(func->handle, &device)) {
1036 dbg("bus exists... trim\n");
1037 /* this shouldn't be in here, so remove
1038 * the bus then re-add it...
1040 ret_val = acpi_bus_trim(device, 1);
1041 dbg("acpi_bus_trim return %x\n", ret_val);
1044 ret_val = acpi_bus_add(&device, pdevice, func->handle,
1045 ACPI_BUS_TYPE_DEVICE);
1046 if (ret_val) {
1047 dbg("error adding bus, %x\n",
1048 -ret_val);
1049 goto acpiphp_bus_add_out;
1052 * try to start anyway. We could have failed to add
1053 * simply because this bus had previously been added
1054 * on another add. Don't bother with the return value
1055 * we just keep going.
1057 ret_val = acpi_bus_start(device);
1059 acpiphp_bus_add_out:
1060 return ret_val;
1065 * acpiphp_bus_trim - trim a bus from acpi subsystem
1066 * @handle: handle to acpi namespace
1069 static int acpiphp_bus_trim(acpi_handle handle)
1071 struct acpi_device *device;
1072 int retval;
1074 retval = acpi_bus_get_device(handle, &device);
1075 if (retval) {
1076 dbg("acpi_device not found\n");
1077 return retval;
1080 retval = acpi_bus_trim(device, 1);
1081 if (retval)
1082 err("cannot remove from acpi list\n");
1084 return retval;
1088 * enable_device - enable, configure a slot
1089 * @slot: slot to be enabled
1091 * This function should be called per *physical slot*,
1092 * not per each slot object in ACPI namespace.
1095 static int enable_device(struct acpiphp_slot *slot)
1097 struct pci_dev *dev;
1098 struct pci_bus *bus = slot->bridge->pci_bus;
1099 struct list_head *l;
1100 struct acpiphp_func *func;
1101 int retval = 0;
1102 int num, max, pass;
1103 acpi_status status;
1105 if (slot->flags & SLOT_ENABLED)
1106 goto err_exit;
1108 /* sanity check: dev should be NULL when hot-plugged in */
1109 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1110 if (dev) {
1111 /* This case shouldn't happen */
1112 err("pci_dev structure already exists.\n");
1113 pci_dev_put(dev);
1114 retval = -1;
1115 goto err_exit;
1118 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1119 if (num == 0) {
1120 err("No new device found\n");
1121 retval = -1;
1122 goto err_exit;
1125 max = acpiphp_max_busnr(bus);
1126 for (pass = 0; pass < 2; pass++) {
1127 list_for_each_entry(dev, &bus->devices, bus_list) {
1128 if (PCI_SLOT(dev->devfn) != slot->device)
1129 continue;
1130 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1131 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1132 max = pci_scan_bridge(bus, dev, max, pass);
1133 if (pass && dev->subordinate)
1134 pci_bus_size_bridges(dev->subordinate);
1139 list_for_each (l, &slot->funcs) {
1140 func = list_entry(l, struct acpiphp_func, sibling);
1141 acpiphp_bus_add(func);
1144 pci_bus_assign_resources(bus);
1145 acpiphp_sanitize_bus(bus);
1146 acpiphp_set_hpp_values(slot->bridge->handle, bus);
1147 list_for_each_entry(func, &slot->funcs, sibling)
1148 acpiphp_configure_ioapics(func->handle);
1149 pci_enable_bridges(bus);
1150 pci_bus_add_devices(bus);
1152 /* associate pci_dev to our representation */
1153 list_for_each (l, &slot->funcs) {
1154 func = list_entry(l, struct acpiphp_func, sibling);
1155 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1156 func->function));
1157 if (!func->pci_dev)
1158 continue;
1160 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1161 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1162 continue;
1164 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1165 if (ACPI_FAILURE(status))
1166 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1167 status);
1170 slot->flags |= SLOT_ENABLED;
1172 err_exit:
1173 return retval;
1176 static void disable_bridges(struct pci_bus *bus)
1178 struct pci_dev *dev;
1179 list_for_each_entry(dev, &bus->devices, bus_list) {
1180 if (dev->subordinate) {
1181 disable_bridges(dev->subordinate);
1182 pci_disable_device(dev);
1188 * disable_device - disable a slot
1190 static int disable_device(struct acpiphp_slot *slot)
1192 int retval = 0;
1193 struct acpiphp_func *func;
1194 struct list_head *l;
1196 /* is this slot already disabled? */
1197 if (!(slot->flags & SLOT_ENABLED))
1198 goto err_exit;
1200 list_for_each (l, &slot->funcs) {
1201 func = list_entry(l, struct acpiphp_func, sibling);
1203 if (func->bridge) {
1204 /* cleanup p2p bridges under this P2P bridge */
1205 cleanup_p2p_bridge(func->bridge->handle,
1206 (u32)1, NULL, NULL);
1207 func->bridge = NULL;
1210 if (func->pci_dev) {
1211 pci_stop_bus_device(func->pci_dev);
1212 if (func->pci_dev->subordinate) {
1213 disable_bridges(func->pci_dev->subordinate);
1214 pci_disable_device(func->pci_dev);
1219 list_for_each (l, &slot->funcs) {
1220 func = list_entry(l, struct acpiphp_func, sibling);
1222 acpiphp_unconfigure_ioapics(func->handle);
1223 acpiphp_bus_trim(func->handle);
1224 /* try to remove anyway.
1225 * acpiphp_bus_add might have been failed */
1227 if (!func->pci_dev)
1228 continue;
1230 pci_remove_bus_device(func->pci_dev);
1231 pci_dev_put(func->pci_dev);
1232 func->pci_dev = NULL;
1235 slot->flags &= (~SLOT_ENABLED);
1237 err_exit:
1238 return retval;
1243 * get_slot_status - get ACPI slot status
1245 * if a slot has _STA for each function and if any one of them
1246 * returned non-zero status, return it
1248 * if a slot doesn't have _STA and if any one of its functions'
1249 * configuration space is configured, return 0x0f as a _STA
1251 * otherwise return 0
1253 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1255 acpi_status status;
1256 unsigned long sta = 0;
1257 u32 dvid;
1258 struct list_head *l;
1259 struct acpiphp_func *func;
1261 list_for_each (l, &slot->funcs) {
1262 func = list_entry(l, struct acpiphp_func, sibling);
1264 if (func->flags & FUNC_HAS_STA) {
1265 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1266 if (ACPI_SUCCESS(status) && sta)
1267 break;
1268 } else {
1269 pci_bus_read_config_dword(slot->bridge->pci_bus,
1270 PCI_DEVFN(slot->device,
1271 func->function),
1272 PCI_VENDOR_ID, &dvid);
1273 if (dvid != 0xffffffff) {
1274 sta = ACPI_STA_ALL;
1275 break;
1280 return (unsigned int)sta;
1284 * acpiphp_eject_slot - physically eject the slot
1286 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1288 acpi_status status;
1289 struct acpiphp_func *func;
1290 struct list_head *l;
1291 struct acpi_object_list arg_list;
1292 union acpi_object arg;
1294 list_for_each (l, &slot->funcs) {
1295 func = list_entry(l, struct acpiphp_func, sibling);
1297 /* We don't want to call _EJ0 on non-existing functions. */
1298 if ((func->flags & FUNC_HAS_EJ0)) {
1299 /* _EJ0 method take one argument */
1300 arg_list.count = 1;
1301 arg_list.pointer = &arg;
1302 arg.type = ACPI_TYPE_INTEGER;
1303 arg.integer.value = 1;
1305 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1306 if (ACPI_FAILURE(status)) {
1307 warn("%s: _EJ0 failed\n", __FUNCTION__);
1308 return -1;
1309 } else
1310 break;
1313 return 0;
1317 * acpiphp_check_bridge - re-enumerate devices
1319 * Iterate over all slots under this bridge and make sure that if a
1320 * card is present they are enabled, and if not they are disabled.
1322 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1324 struct acpiphp_slot *slot;
1325 int retval = 0;
1326 int enabled, disabled;
1328 enabled = disabled = 0;
1330 for (slot = bridge->slots; slot; slot = slot->next) {
1331 unsigned int status = get_slot_status(slot);
1332 if (slot->flags & SLOT_ENABLED) {
1333 if (status == ACPI_STA_ALL)
1334 continue;
1335 retval = acpiphp_disable_slot(slot);
1336 if (retval) {
1337 err("Error occurred in disabling\n");
1338 goto err_exit;
1339 } else {
1340 acpiphp_eject_slot(slot);
1342 disabled++;
1343 } else {
1344 if (status != ACPI_STA_ALL)
1345 continue;
1346 retval = acpiphp_enable_slot(slot);
1347 if (retval) {
1348 err("Error occurred in enabling\n");
1349 goto err_exit;
1351 enabled++;
1355 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1357 err_exit:
1358 return retval;
1361 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1363 u16 pci_cmd, pci_bctl;
1364 struct pci_dev *cdev;
1366 /* Program hpp values for this device */
1367 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1368 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1369 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1370 return;
1372 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1373 bridge->hpp.t0->cache_line_size);
1374 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1375 bridge->hpp.t0->latency_timer);
1376 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1377 if (bridge->hpp.t0->enable_serr)
1378 pci_cmd |= PCI_COMMAND_SERR;
1379 else
1380 pci_cmd &= ~PCI_COMMAND_SERR;
1381 if (bridge->hpp.t0->enable_perr)
1382 pci_cmd |= PCI_COMMAND_PARITY;
1383 else
1384 pci_cmd &= ~PCI_COMMAND_PARITY;
1385 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1387 /* Program bridge control value and child devices */
1388 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1389 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1390 bridge->hpp.t0->latency_timer);
1391 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1392 if (bridge->hpp.t0->enable_serr)
1393 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1394 else
1395 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1396 if (bridge->hpp.t0->enable_perr)
1397 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1398 else
1399 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1400 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1401 if (dev->subordinate) {
1402 list_for_each_entry(cdev, &dev->subordinate->devices,
1403 bus_list)
1404 program_hpp(cdev, bridge);
1409 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1411 struct acpiphp_bridge bridge;
1412 struct pci_dev *dev;
1414 memset(&bridge, 0, sizeof(bridge));
1415 bridge.handle = handle;
1416 bridge.pci_bus = bus;
1417 bridge.pci_dev = bus->self;
1418 decode_hpp(&bridge);
1419 list_for_each_entry(dev, &bus->devices, bus_list)
1420 program_hpp(dev, &bridge);
1425 * Remove devices for which we could not assign resources, call
1426 * arch specific code to fix-up the bus
1428 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1430 struct pci_dev *dev;
1431 int i;
1432 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1434 list_for_each_entry(dev, &bus->devices, bus_list) {
1435 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1436 struct resource *res = &dev->resource[i];
1437 if ((res->flags & type_mask) && !res->start &&
1438 res->end) {
1439 /* Could not assign a required resources
1440 * for this device, remove it */
1441 pci_remove_bus_device(dev);
1442 break;
1448 /* Program resources in newly inserted bridge */
1449 static int acpiphp_configure_bridge (acpi_handle handle)
1451 struct acpi_pci_id pci_id;
1452 struct pci_bus *bus;
1454 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1455 err("cannot get PCI domain and bus number for bridge\n");
1456 return -EINVAL;
1458 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1459 if (!bus) {
1460 err("cannot find bus %d:%d\n",
1461 pci_id.segment, pci_id.bus);
1462 return -EINVAL;
1465 pci_bus_size_bridges(bus);
1466 pci_bus_assign_resources(bus);
1467 acpiphp_sanitize_bus(bus);
1468 acpiphp_set_hpp_values(handle, bus);
1469 pci_enable_bridges(bus);
1470 acpiphp_configure_ioapics(handle);
1471 return 0;
1474 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1476 struct acpi_device *device, *pdevice;
1477 acpi_handle phandle;
1479 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1480 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1481 err("unexpected notification type %d\n", type);
1482 return;
1485 acpi_get_parent(handle, &phandle);
1486 if (acpi_bus_get_device(phandle, &pdevice)) {
1487 dbg("no parent device, assuming NULL\n");
1488 pdevice = NULL;
1490 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1491 err("cannot add bridge to acpi list\n");
1492 return;
1494 if (!acpiphp_configure_bridge(handle) &&
1495 !acpi_bus_start(device))
1496 add_bridge(handle);
1497 else
1498 err("cannot configure and start bridge\n");
1503 * ACPI event handlers
1507 * handle_hotplug_event_bridge - handle ACPI event on bridges
1509 * @handle: Notify()'ed acpi_handle
1510 * @type: Notify code
1511 * @context: pointer to acpiphp_bridge structure
1513 * handles ACPI event notification on {host,p2p} bridges
1516 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1518 struct acpiphp_bridge *bridge;
1519 char objname[64];
1520 struct acpi_buffer buffer = { .length = sizeof(objname),
1521 .pointer = objname };
1522 struct acpi_device *device;
1524 if (acpi_bus_get_device(handle, &device)) {
1525 /* This bridge must have just been physically inserted */
1526 handle_bridge_insertion(handle, type);
1527 return;
1530 bridge = acpiphp_handle_to_bridge(handle);
1531 if (!bridge) {
1532 err("cannot get bridge info\n");
1533 return;
1536 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1538 switch (type) {
1539 case ACPI_NOTIFY_BUS_CHECK:
1540 /* bus re-enumerate */
1541 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1542 acpiphp_check_bridge(bridge);
1543 break;
1545 case ACPI_NOTIFY_DEVICE_CHECK:
1546 /* device check */
1547 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1548 acpiphp_check_bridge(bridge);
1549 break;
1551 case ACPI_NOTIFY_DEVICE_WAKE:
1552 /* wake event */
1553 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1554 break;
1556 case ACPI_NOTIFY_EJECT_REQUEST:
1557 /* request device eject */
1558 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1559 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1560 (bridge->flags & BRIDGE_HAS_EJ0)) {
1561 struct acpiphp_slot *slot;
1562 slot = bridge->func->slot;
1563 if (!acpiphp_disable_slot(slot))
1564 acpiphp_eject_slot(slot);
1566 break;
1568 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1569 printk(KERN_ERR "Device %s cannot be configured due"
1570 " to a frequency mismatch\n", objname);
1571 break;
1573 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1574 printk(KERN_ERR "Device %s cannot be configured due"
1575 " to a bus mode mismatch\n", objname);
1576 break;
1578 case ACPI_NOTIFY_POWER_FAULT:
1579 printk(KERN_ERR "Device %s has suffered a power fault\n",
1580 objname);
1581 break;
1583 default:
1584 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1585 break;
1590 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1592 * @handle: Notify()'ed acpi_handle
1593 * @type: Notify code
1594 * @context: pointer to acpiphp_func structure
1596 * handles ACPI event notification on slots
1599 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1601 struct acpiphp_func *func;
1602 char objname[64];
1603 struct acpi_buffer buffer = { .length = sizeof(objname),
1604 .pointer = objname };
1606 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1608 func = (struct acpiphp_func *)context;
1610 switch (type) {
1611 case ACPI_NOTIFY_BUS_CHECK:
1612 /* bus re-enumerate */
1613 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1614 acpiphp_enable_slot(func->slot);
1615 break;
1617 case ACPI_NOTIFY_DEVICE_CHECK:
1618 /* device check : re-enumerate from parent bus */
1619 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1620 acpiphp_check_bridge(func->slot->bridge);
1621 break;
1623 case ACPI_NOTIFY_DEVICE_WAKE:
1624 /* wake event */
1625 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1626 break;
1628 case ACPI_NOTIFY_EJECT_REQUEST:
1629 /* request device eject */
1630 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1631 if (!(acpiphp_disable_slot(func->slot)))
1632 acpiphp_eject_slot(func->slot);
1633 break;
1635 default:
1636 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1637 break;
1642 static acpi_status
1643 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1645 int *count = (int *)context;
1647 if (acpi_root_bridge(handle)) {
1648 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1649 handle_hotplug_event_bridge, NULL);
1650 (*count)++;
1652 return AE_OK ;
1655 static struct acpi_pci_driver acpi_pci_hp_driver = {
1656 .add = add_bridge,
1657 .remove = remove_bridge,
1661 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1664 int __init acpiphp_glue_init(void)
1666 int num = 0;
1668 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1669 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1671 if (num <= 0)
1672 return -1;
1673 else
1674 acpi_pci_register_driver(&acpi_pci_hp_driver);
1676 return 0;
1681 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1683 * This function frees all data allocated in acpiphp_glue_init()
1685 void __exit acpiphp_glue_exit(void)
1687 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1692 * acpiphp_get_num_slots - count number of slots in a system
1694 int __init acpiphp_get_num_slots(void)
1696 struct list_head *node;
1697 struct acpiphp_bridge *bridge;
1698 int num_slots;
1700 num_slots = 0;
1702 list_for_each (node, &bridge_list) {
1703 bridge = (struct acpiphp_bridge *)node;
1704 dbg("Bus %04x:%02x has %d slot%s\n",
1705 pci_domain_nr(bridge->pci_bus),
1706 bridge->pci_bus->number, bridge->nr_slots,
1707 bridge->nr_slots == 1 ? "" : "s");
1708 num_slots += bridge->nr_slots;
1711 dbg("Total %d slots\n", num_slots);
1712 return num_slots;
1716 #if 0
1718 * acpiphp_for_each_slot - call function for each slot
1719 * @fn: callback function
1720 * @data: context to be passed to callback function
1723 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1725 struct list_head *node;
1726 struct acpiphp_bridge *bridge;
1727 struct acpiphp_slot *slot;
1728 int retval = 0;
1730 list_for_each (node, &bridge_list) {
1731 bridge = (struct acpiphp_bridge *)node;
1732 for (slot = bridge->slots; slot; slot = slot->next) {
1733 retval = fn(slot, data);
1734 if (!retval)
1735 goto err_exit;
1739 err_exit:
1740 return retval;
1742 #endif
1746 * acpiphp_enable_slot - power on slot
1748 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1750 int retval;
1752 mutex_lock(&slot->crit_sect);
1754 /* wake up all functions */
1755 retval = power_on_slot(slot);
1756 if (retval)
1757 goto err_exit;
1759 if (get_slot_status(slot) == ACPI_STA_ALL) {
1760 /* configure all functions */
1761 retval = enable_device(slot);
1762 if (retval)
1763 power_off_slot(slot);
1764 } else {
1765 dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__);
1766 power_off_slot(slot);
1769 err_exit:
1770 mutex_unlock(&slot->crit_sect);
1771 return retval;
1775 * acpiphp_disable_slot - power off slot
1777 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1779 int retval = 0;
1781 mutex_lock(&slot->crit_sect);
1783 /* unconfigure all functions */
1784 retval = disable_device(slot);
1785 if (retval)
1786 goto err_exit;
1788 /* power off all functions */
1789 retval = power_off_slot(slot);
1790 if (retval)
1791 goto err_exit;
1793 err_exit:
1794 mutex_unlock(&slot->crit_sect);
1795 return retval;
1800 * slot enabled: 1
1801 * slot disabled: 0
1803 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1805 return (slot->flags & SLOT_POWEREDON);
1810 * latch closed: 1
1811 * latch open: 0
1813 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1815 unsigned int sta;
1817 sta = get_slot_status(slot);
1819 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1824 * adapter presence : 1
1825 * absence : 0
1827 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1829 unsigned int sta;
1831 sta = get_slot_status(slot);
1833 return (sta == 0) ? 0 : 1;
1838 * pci address (seg/bus/dev)
1840 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1842 u32 address;
1843 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1845 address = (pci_domain_nr(pci_bus) << 16) |
1846 (pci_bus->number << 8) |
1847 slot->device;
1849 return address;