PCI Hotplug: acpiphp: grab refcount on p2p subordinate bus
[linux-2.6/mini2440.git] / drivers / pci / hotplug / acpiphp_glue.c
bloba33794d9e0dce60d3ae57948122e1e53efa57aa5
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.
41 * - When a P2P bridge is present, we elevate the refcount on the subordinate
42 * bus. It loses the refcount when the the driver unloads.
45 #include <linux/init.h>
46 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/pci.h>
50 #include <linux/pci_hotplug.h>
51 #include <linux/pci-acpi.h>
52 #include <linux/mutex.h>
54 #include "../pci.h"
55 #include "acpiphp.h"
57 static LIST_HEAD(bridge_list);
58 static LIST_HEAD(ioapic_list);
59 static DEFINE_SPINLOCK(ioapic_list_lock);
61 #define MY_NAME "acpiphp_glue"
63 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
64 static void acpiphp_sanitize_bus(struct pci_bus *bus);
65 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
66 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
68 /* callback routine to check for the existence of a pci dock device */
69 static acpi_status
70 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
72 int *count = (int *)context;
74 if (is_dock_device(handle)) {
75 (*count)++;
76 return AE_CTRL_TERMINATE;
77 } else {
78 return AE_OK;
83 * the _DCK method can do funny things... and sometimes not
84 * hah-hah funny.
86 * TBD - figure out a way to only call fixups for
87 * systems that require them.
89 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
90 void *v)
92 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
93 struct pci_bus *bus = func->slot->bridge->pci_bus;
94 u32 buses;
96 if (!bus->self)
97 return NOTIFY_OK;
99 /* fixup bad _DCK function that rewrites
100 * secondary bridge on slot
102 pci_read_config_dword(bus->self,
103 PCI_PRIMARY_BUS,
104 &buses);
106 if (((buses >> 8) & 0xff) != bus->secondary) {
107 buses = (buses & 0xff000000)
108 | ((unsigned int)(bus->primary) << 0)
109 | ((unsigned int)(bus->secondary) << 8)
110 | ((unsigned int)(bus->subordinate) << 16);
111 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
113 return NOTIFY_OK;
117 static struct acpi_dock_ops acpiphp_dock_ops = {
118 .handler = handle_hotplug_event_func,
121 /* callback routine to register each ACPI PCI slot object */
122 static acpi_status
123 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
125 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
126 struct acpiphp_slot *slot;
127 struct acpiphp_func *newfunc;
128 acpi_handle tmp;
129 acpi_status status = AE_OK;
130 unsigned long long adr, sun;
131 int device, function, retval;
132 struct pci_bus *pbus = bridge->pci_bus;
134 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
135 return AE_OK;
137 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
138 device = (adr >> 16) & 0xffff;
139 function = adr & 0xffff;
141 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
142 if (!newfunc)
143 return AE_NO_MEMORY;
145 INIT_LIST_HEAD(&newfunc->sibling);
146 newfunc->handle = handle;
147 newfunc->function = function;
149 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
150 newfunc->flags = FUNC_HAS_EJ0;
152 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
153 newfunc->flags |= FUNC_HAS_STA;
155 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
156 newfunc->flags |= FUNC_HAS_PS0;
158 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
159 newfunc->flags |= FUNC_HAS_PS3;
161 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
162 newfunc->flags |= FUNC_HAS_DCK;
164 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
165 if (ACPI_FAILURE(status)) {
167 * use the count of the number of slots we've found
168 * for the number of the slot
170 sun = bridge->nr_slots+1;
173 /* search for objects that share the same slot */
174 for (slot = bridge->slots; slot; slot = slot->next)
175 if (slot->device == device) {
176 if (slot->sun != sun)
177 warn("sibling found, but _SUN doesn't match!\n");
178 break;
181 if (!slot) {
182 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
183 if (!slot) {
184 kfree(newfunc);
185 return AE_NO_MEMORY;
188 slot->bridge = bridge;
189 slot->device = device;
190 slot->sun = sun;
191 INIT_LIST_HEAD(&slot->funcs);
192 mutex_init(&slot->crit_sect);
194 slot->next = bridge->slots;
195 bridge->slots = slot;
197 bridge->nr_slots++;
199 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
200 slot->sun, pci_domain_nr(pbus), pbus->number, device);
201 retval = acpiphp_register_hotplug_slot(slot);
202 if (retval) {
203 if (retval == -EBUSY)
204 warn("Slot %llu already registered by another "
205 "hotplug driver\n", slot->sun);
206 else
207 warn("acpiphp_register_hotplug_slot failed "
208 "(err code = 0x%x)\n", retval);
209 goto err_exit;
213 newfunc->slot = slot;
214 list_add_tail(&newfunc->sibling, &slot->funcs);
216 /* associate corresponding pci_dev */
217 newfunc->pci_dev = pci_get_slot(pbus, PCI_DEVFN(device, function));
218 if (newfunc->pci_dev) {
219 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
222 if (is_dock_device(handle)) {
223 /* we don't want to call this device's _EJ0
224 * because we want the dock notify handler
225 * to call it after it calls _DCK
227 newfunc->flags &= ~FUNC_HAS_EJ0;
228 if (register_hotplug_dock_device(handle,
229 &acpiphp_dock_ops, newfunc))
230 dbg("failed to register dock device\n");
232 /* we need to be notified when dock events happen
233 * outside of the hotplug operation, since we may
234 * need to do fixups before we can hotplug.
236 newfunc->nb.notifier_call = post_dock_fixups;
237 if (register_dock_notifier(&newfunc->nb))
238 dbg("failed to register a dock notifier");
241 /* install notify handler */
242 if (!(newfunc->flags & FUNC_HAS_DCK)) {
243 status = acpi_install_notify_handler(handle,
244 ACPI_SYSTEM_NOTIFY,
245 handle_hotplug_event_func,
246 newfunc);
248 if (ACPI_FAILURE(status))
249 err("failed to register interrupt notify handler\n");
250 } else
251 status = AE_OK;
253 return status;
255 err_exit:
256 bridge->nr_slots--;
257 bridge->slots = slot->next;
258 kfree(slot);
259 kfree(newfunc);
261 return AE_OK;
265 /* see if it's worth looking at this bridge */
266 static int detect_ejectable_slots(struct pci_bus *pbus)
268 int found = acpi_pci_detect_ejectable(pbus);
269 if (!found) {
270 acpi_handle bridge_handle = acpi_pci_get_bridge_handle(pbus);
271 if (!bridge_handle)
272 return 0;
273 acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
274 is_pci_dock_device, (void *)&found, NULL);
276 return found;
280 /* decode ACPI 2.0 _HPP hot plug parameters */
281 static void decode_hpp(struct acpiphp_bridge *bridge)
283 acpi_status status;
285 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
286 if (ACPI_FAILURE(status) ||
287 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
288 /* use default numbers */
289 printk(KERN_WARNING
290 "%s: Could not get hotplug parameters. Use defaults\n",
291 __func__);
292 bridge->hpp.t0 = &bridge->hpp.type0_data;
293 bridge->hpp.t0->revision = 0;
294 bridge->hpp.t0->cache_line_size = 0x10;
295 bridge->hpp.t0->latency_timer = 0x40;
296 bridge->hpp.t0->enable_serr = 0;
297 bridge->hpp.t0->enable_perr = 0;
303 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
304 static void init_bridge_misc(struct acpiphp_bridge *bridge)
306 acpi_status status;
308 /* decode ACPI 2.0 _HPP (hot plug parameters) */
309 decode_hpp(bridge);
311 /* must be added to the list prior to calling register_slot */
312 list_add(&bridge->list, &bridge_list);
314 /* register all slot objects under this bridge */
315 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
316 register_slot, bridge, NULL);
317 if (ACPI_FAILURE(status)) {
318 list_del(&bridge->list);
319 return;
322 /* install notify handler */
323 if (bridge->type != BRIDGE_TYPE_HOST) {
324 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
325 status = acpi_remove_notify_handler(bridge->func->handle,
326 ACPI_SYSTEM_NOTIFY,
327 handle_hotplug_event_func);
328 if (ACPI_FAILURE(status))
329 err("failed to remove notify handler\n");
331 status = acpi_install_notify_handler(bridge->handle,
332 ACPI_SYSTEM_NOTIFY,
333 handle_hotplug_event_bridge,
334 bridge);
336 if (ACPI_FAILURE(status)) {
337 err("failed to register interrupt notify handler\n");
343 /* find acpiphp_func from acpiphp_bridge */
344 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
346 struct list_head *node, *l;
347 struct acpiphp_bridge *bridge;
348 struct acpiphp_slot *slot;
349 struct acpiphp_func *func;
351 list_for_each(node, &bridge_list) {
352 bridge = list_entry(node, struct acpiphp_bridge, list);
353 for (slot = bridge->slots; slot; slot = slot->next) {
354 list_for_each(l, &slot->funcs) {
355 func = list_entry(l, struct acpiphp_func,
356 sibling);
357 if (func->handle == handle)
358 return func;
363 return NULL;
367 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
369 acpi_handle dummy_handle;
371 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
372 "_STA", &dummy_handle)))
373 bridge->flags |= BRIDGE_HAS_STA;
375 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
376 "_EJ0", &dummy_handle)))
377 bridge->flags |= BRIDGE_HAS_EJ0;
379 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
380 "_PS0", &dummy_handle)))
381 bridge->flags |= BRIDGE_HAS_PS0;
383 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
384 "_PS3", &dummy_handle)))
385 bridge->flags |= BRIDGE_HAS_PS3;
387 /* is this ejectable p2p bridge? */
388 if (bridge->flags & BRIDGE_HAS_EJ0) {
389 struct acpiphp_func *func;
391 dbg("found ejectable p2p bridge\n");
393 /* make link between PCI bridge and PCI function */
394 func = acpiphp_bridge_handle_to_function(bridge->handle);
395 if (!func)
396 return;
397 bridge->func = func;
398 func->bridge = bridge;
403 /* allocate and initialize host bridge data structure */
404 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
406 struct acpiphp_bridge *bridge;
408 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
409 if (bridge == NULL)
410 return;
412 bridge->type = BRIDGE_TYPE_HOST;
413 bridge->handle = handle;
415 bridge->pci_bus = pci_bus;
417 spin_lock_init(&bridge->res_lock);
419 init_bridge_misc(bridge);
423 /* allocate and initialize PCI-to-PCI bridge data structure */
424 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
426 struct acpiphp_bridge *bridge;
428 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
429 if (bridge == NULL) {
430 err("out of memory\n");
431 return;
434 bridge->type = BRIDGE_TYPE_P2P;
435 bridge->handle = handle;
436 config_p2p_bridge_flags(bridge);
438 bridge->pci_dev = pci_dev_get(pci_dev);
439 bridge->pci_bus = pci_dev->subordinate;
440 if (!bridge->pci_bus) {
441 err("This is not a PCI-to-PCI bridge!\n");
442 goto err;
446 * Grab a ref to the subordinate PCI bus in case the bus is
447 * removed via PCI core logical hotplug. The ref pins the bus
448 * (which we access during module unload).
450 get_device(&bridge->pci_bus->dev);
451 spin_lock_init(&bridge->res_lock);
453 init_bridge_misc(bridge);
454 return;
455 err:
456 pci_dev_put(pci_dev);
457 kfree(bridge);
458 return;
462 /* callback routine to find P2P bridges */
463 static acpi_status
464 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
466 acpi_status status;
467 acpi_handle dummy_handle;
468 unsigned long long tmp;
469 int device, function;
470 struct pci_dev *dev;
471 struct pci_bus *pci_bus = context;
473 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
474 if (ACPI_FAILURE(status))
475 return AE_OK; /* continue */
477 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
478 if (ACPI_FAILURE(status)) {
479 dbg("%s: _ADR evaluation failure\n", __func__);
480 return AE_OK;
483 device = (tmp >> 16) & 0xffff;
484 function = tmp & 0xffff;
486 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
488 if (!dev || !dev->subordinate)
489 goto out;
491 /* check if this bridge has ejectable slots */
492 if ((detect_ejectable_slots(dev->subordinate) > 0)) {
493 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
494 add_p2p_bridge(handle, dev);
497 /* search P2P bridges under this p2p bridge */
498 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
499 find_p2p_bridge, dev->subordinate, NULL);
500 if (ACPI_FAILURE(status))
501 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
503 out:
504 pci_dev_put(dev);
505 return AE_OK;
509 /* find hot-pluggable slots, and then find P2P bridge */
510 static int add_bridge(acpi_handle handle)
512 acpi_status status;
513 unsigned long long tmp;
514 int seg, bus;
515 acpi_handle dummy_handle;
516 struct pci_bus *pci_bus;
518 /* if the bridge doesn't have _STA, we assume it is always there */
519 status = acpi_get_handle(handle, "_STA", &dummy_handle);
520 if (ACPI_SUCCESS(status)) {
521 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
522 if (ACPI_FAILURE(status)) {
523 dbg("%s: _STA evaluation failure\n", __func__);
524 return 0;
526 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
527 /* don't register this object */
528 return 0;
531 /* get PCI segment number */
532 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
534 seg = ACPI_SUCCESS(status) ? tmp : 0;
536 /* get PCI bus number */
537 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
539 if (ACPI_SUCCESS(status)) {
540 bus = tmp;
541 } else {
542 warn("can't get bus number, assuming 0\n");
543 bus = 0;
546 pci_bus = pci_find_bus(seg, bus);
547 if (!pci_bus) {
548 err("Can't find bus %04x:%02x\n", seg, bus);
549 return 0;
552 /* check if this bridge has ejectable slots */
553 if (detect_ejectable_slots(pci_bus) > 0) {
554 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
555 add_host_bridge(handle, pci_bus);
558 /* search P2P bridges under this host bridge */
559 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
560 find_p2p_bridge, pci_bus, NULL);
562 if (ACPI_FAILURE(status))
563 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
565 return 0;
568 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
570 struct list_head *head;
571 list_for_each(head, &bridge_list) {
572 struct acpiphp_bridge *bridge = list_entry(head,
573 struct acpiphp_bridge, list);
574 if (bridge->handle == handle)
575 return bridge;
578 return NULL;
581 static void cleanup_bridge(struct acpiphp_bridge *bridge)
583 struct list_head *list, *tmp;
584 struct acpiphp_slot *slot;
585 acpi_status status;
586 acpi_handle handle = bridge->handle;
588 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
589 handle_hotplug_event_bridge);
590 if (ACPI_FAILURE(status))
591 err("failed to remove notify handler\n");
593 if ((bridge->type != BRIDGE_TYPE_HOST) &&
594 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
595 status = acpi_install_notify_handler(bridge->func->handle,
596 ACPI_SYSTEM_NOTIFY,
597 handle_hotplug_event_func,
598 bridge->func);
599 if (ACPI_FAILURE(status))
600 err("failed to install interrupt notify handler\n");
603 slot = bridge->slots;
604 while (slot) {
605 struct acpiphp_slot *next = slot->next;
606 list_for_each_safe (list, tmp, &slot->funcs) {
607 struct acpiphp_func *func;
608 func = list_entry(list, struct acpiphp_func, sibling);
609 if (is_dock_device(func->handle)) {
610 unregister_hotplug_dock_device(func->handle);
611 unregister_dock_notifier(&func->nb);
613 if (!(func->flags & FUNC_HAS_DCK)) {
614 status = acpi_remove_notify_handler(func->handle,
615 ACPI_SYSTEM_NOTIFY,
616 handle_hotplug_event_func);
617 if (ACPI_FAILURE(status))
618 err("failed to remove notify handler\n");
620 pci_dev_put(func->pci_dev);
621 list_del(list);
622 kfree(func);
624 acpiphp_unregister_hotplug_slot(slot);
625 list_del(&slot->funcs);
626 kfree(slot);
627 slot = next;
631 * Only P2P bridges have a pci_dev
633 if (bridge->pci_dev)
634 put_device(&bridge->pci_bus->dev);
636 pci_dev_put(bridge->pci_dev);
637 list_del(&bridge->list);
638 kfree(bridge);
641 static acpi_status
642 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
644 struct acpiphp_bridge *bridge;
646 /* cleanup p2p bridges under this P2P bridge
647 in a depth-first manner */
648 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
649 cleanup_p2p_bridge, NULL, NULL);
651 bridge = acpiphp_handle_to_bridge(handle);
652 if (bridge)
653 cleanup_bridge(bridge);
655 return AE_OK;
658 static void remove_bridge(acpi_handle handle)
660 struct acpiphp_bridge *bridge;
662 /* cleanup p2p bridges under this host bridge
663 in a depth-first manner */
664 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
665 (u32)1, cleanup_p2p_bridge, NULL, NULL);
668 * On root bridges with hotplug slots directly underneath (ie,
669 * no p2p bridge inbetween), we call cleanup_bridge().
671 * The else clause cleans up root bridges that either had no
672 * hotplug slots at all, or had a p2p bridge underneath.
674 bridge = acpiphp_handle_to_bridge(handle);
675 if (bridge)
676 cleanup_bridge(bridge);
677 else
678 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
679 handle_hotplug_event_bridge);
682 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
684 struct acpi_pci_id id;
685 struct pci_bus *bus;
686 struct pci_dev *dev;
688 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
689 return NULL;
691 bus = pci_find_bus(id.segment, id.bus);
692 if (!bus)
693 return NULL;
695 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
696 if (!dev)
697 return NULL;
699 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
700 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
702 pci_dev_put(dev);
703 return NULL;
706 return dev;
709 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
711 acpi_status status;
712 int result = -1;
713 unsigned long long gsb;
714 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
715 union acpi_object *obj;
716 void *table;
718 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
719 if (ACPI_SUCCESS(status)) {
720 *gsi_base = (u32)gsb;
721 return 0;
724 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
725 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
726 return -1;
728 obj = buffer.pointer;
729 if (obj->type != ACPI_TYPE_BUFFER)
730 goto out;
732 table = obj->buffer.pointer;
733 switch (((struct acpi_subtable_header *)table)->type) {
734 case ACPI_MADT_TYPE_IO_SAPIC:
735 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
736 result = 0;
737 break;
738 case ACPI_MADT_TYPE_IO_APIC:
739 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
740 result = 0;
741 break;
742 default:
743 break;
745 out:
746 kfree(buffer.pointer);
747 return result;
750 static acpi_status
751 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
753 acpi_status status;
754 unsigned long long sta;
755 acpi_handle tmp;
756 struct pci_dev *pdev;
757 u32 gsi_base;
758 u64 phys_addr;
759 struct acpiphp_ioapic *ioapic;
761 /* Evaluate _STA if present */
762 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
763 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
764 return AE_CTRL_DEPTH;
766 /* Scan only PCI bus scope */
767 status = acpi_get_handle(handle, "_HID", &tmp);
768 if (ACPI_SUCCESS(status))
769 return AE_CTRL_DEPTH;
771 if (get_gsi_base(handle, &gsi_base))
772 return AE_OK;
774 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
775 if (!ioapic)
776 return AE_NO_MEMORY;
778 pdev = get_apic_pci_info(handle);
779 if (!pdev)
780 goto exit_kfree;
782 if (pci_enable_device(pdev))
783 goto exit_pci_dev_put;
785 pci_set_master(pdev);
787 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
788 goto exit_pci_disable_device;
790 phys_addr = pci_resource_start(pdev, 0);
791 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
792 goto exit_pci_release_region;
794 ioapic->gsi_base = gsi_base;
795 ioapic->dev = pdev;
796 spin_lock(&ioapic_list_lock);
797 list_add_tail(&ioapic->list, &ioapic_list);
798 spin_unlock(&ioapic_list_lock);
800 return AE_OK;
802 exit_pci_release_region:
803 pci_release_region(pdev, 0);
804 exit_pci_disable_device:
805 pci_disable_device(pdev);
806 exit_pci_dev_put:
807 pci_dev_put(pdev);
808 exit_kfree:
809 kfree(ioapic);
811 return AE_OK;
814 static acpi_status
815 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
817 acpi_status status;
818 unsigned long long sta;
819 acpi_handle tmp;
820 u32 gsi_base;
821 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
823 /* Evaluate _STA if present */
824 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
825 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
826 return AE_CTRL_DEPTH;
828 /* Scan only PCI bus scope */
829 status = acpi_get_handle(handle, "_HID", &tmp);
830 if (ACPI_SUCCESS(status))
831 return AE_CTRL_DEPTH;
833 if (get_gsi_base(handle, &gsi_base))
834 return AE_OK;
836 acpi_unregister_ioapic(handle, gsi_base);
838 spin_lock(&ioapic_list_lock);
839 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
840 if (pos->gsi_base != gsi_base)
841 continue;
842 ioapic = pos;
843 list_del(&ioapic->list);
844 break;
846 spin_unlock(&ioapic_list_lock);
848 if (!ioapic)
849 return AE_OK;
851 pci_release_region(ioapic->dev, 0);
852 pci_disable_device(ioapic->dev);
853 pci_dev_put(ioapic->dev);
854 kfree(ioapic);
856 return AE_OK;
859 static int acpiphp_configure_ioapics(acpi_handle handle)
861 ioapic_add(handle, 0, NULL, NULL);
862 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
863 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
864 return 0;
867 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
869 ioapic_remove(handle, 0, NULL, NULL);
870 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
871 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
872 return 0;
875 static int power_on_slot(struct acpiphp_slot *slot)
877 acpi_status status;
878 struct acpiphp_func *func;
879 struct list_head *l;
880 int retval = 0;
882 /* if already enabled, just skip */
883 if (slot->flags & SLOT_POWEREDON)
884 goto err_exit;
886 list_for_each (l, &slot->funcs) {
887 func = list_entry(l, struct acpiphp_func, sibling);
889 if (func->flags & FUNC_HAS_PS0) {
890 dbg("%s: executing _PS0\n", __func__);
891 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
892 if (ACPI_FAILURE(status)) {
893 warn("%s: _PS0 failed\n", __func__);
894 retval = -1;
895 goto err_exit;
896 } else
897 break;
901 /* TBD: evaluate _STA to check if the slot is enabled */
903 slot->flags |= SLOT_POWEREDON;
905 err_exit:
906 return retval;
910 static int power_off_slot(struct acpiphp_slot *slot)
912 acpi_status status;
913 struct acpiphp_func *func;
914 struct list_head *l;
916 int retval = 0;
918 /* if already disabled, just skip */
919 if ((slot->flags & SLOT_POWEREDON) == 0)
920 goto err_exit;
922 list_for_each (l, &slot->funcs) {
923 func = list_entry(l, struct acpiphp_func, sibling);
925 if (func->flags & FUNC_HAS_PS3) {
926 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
927 if (ACPI_FAILURE(status)) {
928 warn("%s: _PS3 failed\n", __func__);
929 retval = -1;
930 goto err_exit;
931 } else
932 break;
936 /* TBD: evaluate _STA to check if the slot is disabled */
938 slot->flags &= (~SLOT_POWEREDON);
940 err_exit:
941 return retval;
947 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
948 * @bus: bus to start search with
950 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
952 struct list_head *tmp;
953 unsigned char max, n;
956 * pci_bus_max_busnr will return the highest
957 * reserved busnr for all these children.
958 * that is equivalent to the bus->subordinate
959 * value. We don't want to use the parent's
960 * bus->subordinate value because it could have
961 * padding in it.
963 max = bus->secondary;
965 list_for_each(tmp, &bus->children) {
966 n = pci_bus_max_busnr(pci_bus_b(tmp));
967 if (n > max)
968 max = n;
970 return max;
975 * acpiphp_bus_add - add a new bus to acpi subsystem
976 * @func: acpiphp_func of the bridge
978 static int acpiphp_bus_add(struct acpiphp_func *func)
980 acpi_handle phandle;
981 struct acpi_device *device, *pdevice;
982 int ret_val;
984 acpi_get_parent(func->handle, &phandle);
985 if (acpi_bus_get_device(phandle, &pdevice)) {
986 dbg("no parent device, assuming NULL\n");
987 pdevice = NULL;
989 if (!acpi_bus_get_device(func->handle, &device)) {
990 dbg("bus exists... trim\n");
991 /* this shouldn't be in here, so remove
992 * the bus then re-add it...
994 ret_val = acpi_bus_trim(device, 1);
995 dbg("acpi_bus_trim return %x\n", ret_val);
998 ret_val = acpi_bus_add(&device, pdevice, func->handle,
999 ACPI_BUS_TYPE_DEVICE);
1000 if (ret_val) {
1001 dbg("error adding bus, %x\n",
1002 -ret_val);
1003 goto acpiphp_bus_add_out;
1006 * try to start anyway. We could have failed to add
1007 * simply because this bus had previously been added
1008 * on another add. Don't bother with the return value
1009 * we just keep going.
1011 ret_val = acpi_bus_start(device);
1013 acpiphp_bus_add_out:
1014 return ret_val;
1019 * acpiphp_bus_trim - trim a bus from acpi subsystem
1020 * @handle: handle to acpi namespace
1022 static int acpiphp_bus_trim(acpi_handle handle)
1024 struct acpi_device *device;
1025 int retval;
1027 retval = acpi_bus_get_device(handle, &device);
1028 if (retval) {
1029 dbg("acpi_device not found\n");
1030 return retval;
1033 retval = acpi_bus_trim(device, 1);
1034 if (retval)
1035 err("cannot remove from acpi list\n");
1037 return retval;
1041 * enable_device - enable, configure a slot
1042 * @slot: slot to be enabled
1044 * This function should be called per *physical slot*,
1045 * not per each slot object in ACPI namespace.
1047 static int __ref enable_device(struct acpiphp_slot *slot)
1049 struct pci_dev *dev;
1050 struct pci_bus *bus = slot->bridge->pci_bus;
1051 struct list_head *l;
1052 struct acpiphp_func *func;
1053 int retval = 0;
1054 int num, max, pass;
1055 acpi_status status;
1057 if (slot->flags & SLOT_ENABLED)
1058 goto err_exit;
1060 /* sanity check: dev should be NULL when hot-plugged in */
1061 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1062 if (dev) {
1063 /* This case shouldn't happen */
1064 err("pci_dev structure already exists.\n");
1065 pci_dev_put(dev);
1066 retval = -1;
1067 goto err_exit;
1070 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1071 if (num == 0) {
1072 err("No new device found\n");
1073 retval = -1;
1074 goto err_exit;
1077 max = acpiphp_max_busnr(bus);
1078 for (pass = 0; pass < 2; pass++) {
1079 list_for_each_entry(dev, &bus->devices, bus_list) {
1080 if (PCI_SLOT(dev->devfn) != slot->device)
1081 continue;
1082 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1083 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1084 max = pci_scan_bridge(bus, dev, max, pass);
1085 if (pass && dev->subordinate)
1086 pci_bus_size_bridges(dev->subordinate);
1091 list_for_each (l, &slot->funcs) {
1092 func = list_entry(l, struct acpiphp_func, sibling);
1093 acpiphp_bus_add(func);
1096 pci_bus_assign_resources(bus);
1097 acpiphp_sanitize_bus(bus);
1098 acpiphp_set_hpp_values(slot->bridge->handle, bus);
1099 list_for_each_entry(func, &slot->funcs, sibling)
1100 acpiphp_configure_ioapics(func->handle);
1101 pci_enable_bridges(bus);
1102 pci_bus_add_devices(bus);
1104 /* associate pci_dev to our representation */
1105 list_for_each (l, &slot->funcs) {
1106 func = list_entry(l, struct acpiphp_func, sibling);
1107 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1108 func->function));
1109 if (!func->pci_dev)
1110 continue;
1112 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1113 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1114 continue;
1116 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1117 if (ACPI_FAILURE(status))
1118 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1119 status);
1122 slot->flags |= SLOT_ENABLED;
1124 err_exit:
1125 return retval;
1128 static void disable_bridges(struct pci_bus *bus)
1130 struct pci_dev *dev;
1131 list_for_each_entry(dev, &bus->devices, bus_list) {
1132 if (dev->subordinate) {
1133 disable_bridges(dev->subordinate);
1134 pci_disable_device(dev);
1140 * disable_device - disable a slot
1141 * @slot: ACPI PHP slot
1143 static int disable_device(struct acpiphp_slot *slot)
1145 int retval = 0;
1146 struct acpiphp_func *func;
1147 struct list_head *l;
1149 /* is this slot already disabled? */
1150 if (!(slot->flags & SLOT_ENABLED))
1151 goto err_exit;
1153 list_for_each (l, &slot->funcs) {
1154 func = list_entry(l, struct acpiphp_func, sibling);
1156 if (func->bridge) {
1157 /* cleanup p2p bridges under this P2P bridge */
1158 cleanup_p2p_bridge(func->bridge->handle,
1159 (u32)1, NULL, NULL);
1160 func->bridge = NULL;
1163 if (func->pci_dev) {
1164 pci_stop_bus_device(func->pci_dev);
1165 if (func->pci_dev->subordinate) {
1166 disable_bridges(func->pci_dev->subordinate);
1167 pci_disable_device(func->pci_dev);
1172 list_for_each (l, &slot->funcs) {
1173 func = list_entry(l, struct acpiphp_func, sibling);
1175 acpiphp_unconfigure_ioapics(func->handle);
1176 acpiphp_bus_trim(func->handle);
1177 /* try to remove anyway.
1178 * acpiphp_bus_add might have been failed */
1180 if (!func->pci_dev)
1181 continue;
1183 pci_remove_bus_device(func->pci_dev);
1184 pci_dev_put(func->pci_dev);
1185 func->pci_dev = NULL;
1188 slot->flags &= (~SLOT_ENABLED);
1190 err_exit:
1191 return retval;
1196 * get_slot_status - get ACPI slot status
1197 * @slot: ACPI PHP slot
1199 * If a slot has _STA for each function and if any one of them
1200 * returned non-zero status, return it.
1202 * If a slot doesn't have _STA and if any one of its functions'
1203 * configuration space is configured, return 0x0f as a _STA.
1205 * Otherwise return 0.
1207 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1209 acpi_status status;
1210 unsigned long long sta = 0;
1211 u32 dvid;
1212 struct list_head *l;
1213 struct acpiphp_func *func;
1215 list_for_each (l, &slot->funcs) {
1216 func = list_entry(l, struct acpiphp_func, sibling);
1218 if (func->flags & FUNC_HAS_STA) {
1219 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1220 if (ACPI_SUCCESS(status) && sta)
1221 break;
1222 } else {
1223 pci_bus_read_config_dword(slot->bridge->pci_bus,
1224 PCI_DEVFN(slot->device,
1225 func->function),
1226 PCI_VENDOR_ID, &dvid);
1227 if (dvid != 0xffffffff) {
1228 sta = ACPI_STA_ALL;
1229 break;
1234 return (unsigned int)sta;
1238 * acpiphp_eject_slot - physically eject the slot
1239 * @slot: ACPI PHP slot
1241 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1243 acpi_status status;
1244 struct acpiphp_func *func;
1245 struct list_head *l;
1246 struct acpi_object_list arg_list;
1247 union acpi_object arg;
1249 list_for_each (l, &slot->funcs) {
1250 func = list_entry(l, struct acpiphp_func, sibling);
1252 /* We don't want to call _EJ0 on non-existing functions. */
1253 if ((func->flags & FUNC_HAS_EJ0)) {
1254 /* _EJ0 method take one argument */
1255 arg_list.count = 1;
1256 arg_list.pointer = &arg;
1257 arg.type = ACPI_TYPE_INTEGER;
1258 arg.integer.value = 1;
1260 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1261 if (ACPI_FAILURE(status)) {
1262 warn("%s: _EJ0 failed\n", __func__);
1263 return -1;
1264 } else
1265 break;
1268 return 0;
1272 * acpiphp_check_bridge - re-enumerate devices
1273 * @bridge: where to begin re-enumeration
1275 * Iterate over all slots under this bridge and make sure that if a
1276 * card is present they are enabled, and if not they are disabled.
1278 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1280 struct acpiphp_slot *slot;
1281 int retval = 0;
1282 int enabled, disabled;
1284 enabled = disabled = 0;
1286 for (slot = bridge->slots; slot; slot = slot->next) {
1287 unsigned int status = get_slot_status(slot);
1288 if (slot->flags & SLOT_ENABLED) {
1289 if (status == ACPI_STA_ALL)
1290 continue;
1291 retval = acpiphp_disable_slot(slot);
1292 if (retval) {
1293 err("Error occurred in disabling\n");
1294 goto err_exit;
1295 } else {
1296 acpiphp_eject_slot(slot);
1298 disabled++;
1299 } else {
1300 if (status != ACPI_STA_ALL)
1301 continue;
1302 retval = acpiphp_enable_slot(slot);
1303 if (retval) {
1304 err("Error occurred in enabling\n");
1305 goto err_exit;
1307 enabled++;
1311 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1313 err_exit:
1314 return retval;
1317 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1319 u16 pci_cmd, pci_bctl;
1320 struct pci_dev *cdev;
1322 /* Program hpp values for this device */
1323 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1324 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1325 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1326 return;
1328 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1329 return;
1331 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1332 bridge->hpp.t0->cache_line_size);
1333 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1334 bridge->hpp.t0->latency_timer);
1335 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1336 if (bridge->hpp.t0->enable_serr)
1337 pci_cmd |= PCI_COMMAND_SERR;
1338 else
1339 pci_cmd &= ~PCI_COMMAND_SERR;
1340 if (bridge->hpp.t0->enable_perr)
1341 pci_cmd |= PCI_COMMAND_PARITY;
1342 else
1343 pci_cmd &= ~PCI_COMMAND_PARITY;
1344 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1346 /* Program bridge control value and child devices */
1347 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1348 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1349 bridge->hpp.t0->latency_timer);
1350 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1351 if (bridge->hpp.t0->enable_serr)
1352 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1353 else
1354 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1355 if (bridge->hpp.t0->enable_perr)
1356 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1357 else
1358 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1359 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1360 if (dev->subordinate) {
1361 list_for_each_entry(cdev, &dev->subordinate->devices,
1362 bus_list)
1363 program_hpp(cdev, bridge);
1368 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1370 struct acpiphp_bridge bridge;
1371 struct pci_dev *dev;
1373 memset(&bridge, 0, sizeof(bridge));
1374 bridge.handle = handle;
1375 bridge.pci_bus = bus;
1376 bridge.pci_dev = bus->self;
1377 decode_hpp(&bridge);
1378 list_for_each_entry(dev, &bus->devices, bus_list)
1379 program_hpp(dev, &bridge);
1384 * Remove devices for which we could not assign resources, call
1385 * arch specific code to fix-up the bus
1387 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1389 struct pci_dev *dev;
1390 int i;
1391 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1393 list_for_each_entry(dev, &bus->devices, bus_list) {
1394 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1395 struct resource *res = &dev->resource[i];
1396 if ((res->flags & type_mask) && !res->start &&
1397 res->end) {
1398 /* Could not assign a required resources
1399 * for this device, remove it */
1400 pci_remove_bus_device(dev);
1401 break;
1407 /* Program resources in newly inserted bridge */
1408 static int acpiphp_configure_bridge (acpi_handle handle)
1410 struct acpi_pci_id pci_id;
1411 struct pci_bus *bus;
1413 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1414 err("cannot get PCI domain and bus number for bridge\n");
1415 return -EINVAL;
1417 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1418 if (!bus) {
1419 err("cannot find bus %d:%d\n",
1420 pci_id.segment, pci_id.bus);
1421 return -EINVAL;
1424 pci_bus_size_bridges(bus);
1425 pci_bus_assign_resources(bus);
1426 acpiphp_sanitize_bus(bus);
1427 acpiphp_set_hpp_values(handle, bus);
1428 pci_enable_bridges(bus);
1429 acpiphp_configure_ioapics(handle);
1430 return 0;
1433 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1435 struct acpi_device *device, *pdevice;
1436 acpi_handle phandle;
1438 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1439 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1440 err("unexpected notification type %d\n", type);
1441 return;
1444 acpi_get_parent(handle, &phandle);
1445 if (acpi_bus_get_device(phandle, &pdevice)) {
1446 dbg("no parent device, assuming NULL\n");
1447 pdevice = NULL;
1449 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1450 err("cannot add bridge to acpi list\n");
1451 return;
1453 if (!acpiphp_configure_bridge(handle) &&
1454 !acpi_bus_start(device))
1455 add_bridge(handle);
1456 else
1457 err("cannot configure and start bridge\n");
1462 * ACPI event handlers
1465 static acpi_status
1466 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1468 int *count = (int *)context;
1469 struct acpiphp_bridge *bridge;
1471 bridge = acpiphp_handle_to_bridge(handle);
1472 if (bridge)
1473 (*count)++;
1474 return AE_OK ;
1477 static acpi_status
1478 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1480 struct acpiphp_bridge *bridge;
1481 char objname[64];
1482 struct acpi_buffer buffer = { .length = sizeof(objname),
1483 .pointer = objname };
1485 bridge = acpiphp_handle_to_bridge(handle);
1486 if (bridge) {
1487 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1488 dbg("%s: re-enumerating slots under %s\n",
1489 __func__, objname);
1490 acpiphp_check_bridge(bridge);
1492 return AE_OK ;
1496 * handle_hotplug_event_bridge - handle ACPI event on bridges
1497 * @handle: Notify()'ed acpi_handle
1498 * @type: Notify code
1499 * @context: pointer to acpiphp_bridge structure
1501 * Handles ACPI event notification on {host,p2p} bridges.
1503 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1505 struct acpiphp_bridge *bridge;
1506 char objname[64];
1507 struct acpi_buffer buffer = { .length = sizeof(objname),
1508 .pointer = objname };
1509 struct acpi_device *device;
1510 int num_sub_bridges = 0;
1512 if (acpi_bus_get_device(handle, &device)) {
1513 /* This bridge must have just been physically inserted */
1514 handle_bridge_insertion(handle, type);
1515 return;
1518 bridge = acpiphp_handle_to_bridge(handle);
1519 if (type == ACPI_NOTIFY_BUS_CHECK) {
1520 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1521 count_sub_bridges, &num_sub_bridges, NULL);
1524 if (!bridge && !num_sub_bridges) {
1525 err("cannot get bridge info\n");
1526 return;
1529 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1531 switch (type) {
1532 case ACPI_NOTIFY_BUS_CHECK:
1533 /* bus re-enumerate */
1534 dbg("%s: Bus check notify on %s\n", __func__, objname);
1535 if (bridge) {
1536 dbg("%s: re-enumerating slots under %s\n",
1537 __func__, objname);
1538 acpiphp_check_bridge(bridge);
1540 if (num_sub_bridges)
1541 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1542 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1543 break;
1545 case ACPI_NOTIFY_DEVICE_CHECK:
1546 /* device check */
1547 dbg("%s: Device check notify on %s\n", __func__, 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", __func__, objname);
1554 break;
1556 case ACPI_NOTIFY_EJECT_REQUEST:
1557 /* request device eject */
1558 dbg("%s: Device eject notify on %s\n", __func__, 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)
1591 * @handle: Notify()'ed acpi_handle
1592 * @type: Notify code
1593 * @context: pointer to acpiphp_func structure
1595 * Handles ACPI event notification on slots.
1597 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1599 struct acpiphp_func *func;
1600 char objname[64];
1601 struct acpi_buffer buffer = { .length = sizeof(objname),
1602 .pointer = objname };
1604 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1606 func = (struct acpiphp_func *)context;
1608 switch (type) {
1609 case ACPI_NOTIFY_BUS_CHECK:
1610 /* bus re-enumerate */
1611 dbg("%s: Bus check notify on %s\n", __func__, objname);
1612 acpiphp_enable_slot(func->slot);
1613 break;
1615 case ACPI_NOTIFY_DEVICE_CHECK:
1616 /* device check : re-enumerate from parent bus */
1617 dbg("%s: Device check notify on %s\n", __func__, objname);
1618 acpiphp_check_bridge(func->slot->bridge);
1619 break;
1621 case ACPI_NOTIFY_DEVICE_WAKE:
1622 /* wake event */
1623 dbg("%s: Device wake notify on %s\n", __func__, objname);
1624 break;
1626 case ACPI_NOTIFY_EJECT_REQUEST:
1627 /* request device eject */
1628 dbg("%s: Device eject notify on %s\n", __func__, objname);
1629 if (!(acpiphp_disable_slot(func->slot)))
1630 acpiphp_eject_slot(func->slot);
1631 break;
1633 default:
1634 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1635 break;
1640 static acpi_status
1641 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1643 int *count = (int *)context;
1645 if (acpi_root_bridge(handle)) {
1646 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1647 handle_hotplug_event_bridge, NULL);
1648 (*count)++;
1650 return AE_OK ;
1653 static struct acpi_pci_driver acpi_pci_hp_driver = {
1654 .add = add_bridge,
1655 .remove = remove_bridge,
1659 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1661 int __init acpiphp_glue_init(void)
1663 int num = 0;
1665 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1666 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1668 if (num <= 0)
1669 return -1;
1670 else
1671 acpi_pci_register_driver(&acpi_pci_hp_driver);
1673 return 0;
1678 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1680 * This function frees all data allocated in acpiphp_glue_init().
1682 void acpiphp_glue_exit(void)
1684 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1689 * acpiphp_get_num_slots - count number of slots in a system
1691 int __init acpiphp_get_num_slots(void)
1693 struct acpiphp_bridge *bridge;
1694 int num_slots = 0;
1696 list_for_each_entry (bridge, &bridge_list, list) {
1697 dbg("Bus %04x:%02x has %d slot%s\n",
1698 pci_domain_nr(bridge->pci_bus),
1699 bridge->pci_bus->number, bridge->nr_slots,
1700 bridge->nr_slots == 1 ? "" : "s");
1701 num_slots += bridge->nr_slots;
1704 dbg("Total %d slots\n", num_slots);
1705 return num_slots;
1709 #if 0
1711 * acpiphp_for_each_slot - call function for each slot
1712 * @fn: callback function
1713 * @data: context to be passed to callback function
1715 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1717 struct list_head *node;
1718 struct acpiphp_bridge *bridge;
1719 struct acpiphp_slot *slot;
1720 int retval = 0;
1722 list_for_each (node, &bridge_list) {
1723 bridge = (struct acpiphp_bridge *)node;
1724 for (slot = bridge->slots; slot; slot = slot->next) {
1725 retval = fn(slot, data);
1726 if (!retval)
1727 goto err_exit;
1731 err_exit:
1732 return retval;
1734 #endif
1738 * acpiphp_enable_slot - power on slot
1739 * @slot: ACPI PHP slot
1741 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1743 int retval;
1745 mutex_lock(&slot->crit_sect);
1747 /* wake up all functions */
1748 retval = power_on_slot(slot);
1749 if (retval)
1750 goto err_exit;
1752 if (get_slot_status(slot) == ACPI_STA_ALL) {
1753 /* configure all functions */
1754 retval = enable_device(slot);
1755 if (retval)
1756 power_off_slot(slot);
1757 } else {
1758 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1759 power_off_slot(slot);
1762 err_exit:
1763 mutex_unlock(&slot->crit_sect);
1764 return retval;
1768 * acpiphp_disable_slot - power off slot
1769 * @slot: ACPI PHP slot
1771 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1773 int retval = 0;
1775 mutex_lock(&slot->crit_sect);
1777 /* unconfigure all functions */
1778 retval = disable_device(slot);
1779 if (retval)
1780 goto err_exit;
1782 /* power off all functions */
1783 retval = power_off_slot(slot);
1784 if (retval)
1785 goto err_exit;
1787 err_exit:
1788 mutex_unlock(&slot->crit_sect);
1789 return retval;
1794 * slot enabled: 1
1795 * slot disabled: 0
1797 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1799 return (slot->flags & SLOT_POWEREDON);
1804 * latch open: 1
1805 * latch closed: 0
1807 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1809 unsigned int sta;
1811 sta = get_slot_status(slot);
1813 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1818 * adapter presence : 1
1819 * absence : 0
1821 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1823 unsigned int sta;
1825 sta = get_slot_status(slot);
1827 return (sta == 0) ? 0 : 1;