Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / pci / hotplug / acpiphp_glue.c
blobee672b5332701868b203462109abb2e87117c05d
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/pci_hotplug.h>
49 #include <linux/mutex.h>
51 #include "../pci.h"
52 #include "acpiphp.h"
54 static LIST_HEAD(bridge_list);
55 static LIST_HEAD(ioapic_list);
56 static DEFINE_SPINLOCK(ioapic_list_lock);
58 #define MY_NAME "acpiphp_glue"
60 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
61 static void acpiphp_sanitize_bus(struct pci_bus *bus);
62 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
63 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
67 * initialization & terminatation routines
70 /**
71 * is_ejectable - determine if a slot is ejectable
72 * @handle: handle to acpi namespace
74 * Ejectable slot should satisfy at least these conditions:
76 * 1. has _ADR method
77 * 2. has _EJ0 method
79 * optionally
81 * 1. has _STA method
82 * 2. has _PS0 method
83 * 3. has _PS3 method
84 * 4. ..
86 static int is_ejectable(acpi_handle handle)
88 acpi_status status;
89 acpi_handle tmp;
91 status = acpi_get_handle(handle, "_ADR", &tmp);
92 if (ACPI_FAILURE(status)) {
93 return 0;
96 status = acpi_get_handle(handle, "_EJ0", &tmp);
97 if (ACPI_FAILURE(status)) {
98 return 0;
101 return 1;
105 /* callback routine to check for the existence of ejectable slots */
106 static acpi_status
107 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
109 int *count = (int *)context;
111 if (is_ejectable(handle)) {
112 (*count)++;
113 /* only one ejectable slot is enough */
114 return AE_CTRL_TERMINATE;
115 } else {
116 return AE_OK;
120 /* callback routine to check for the existence of a pci dock device */
121 static acpi_status
122 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
124 int *count = (int *)context;
126 if (is_dock_device(handle)) {
127 (*count)++;
128 return AE_CTRL_TERMINATE;
129 } else {
130 return AE_OK;
138 * the _DCK method can do funny things... and sometimes not
139 * hah-hah funny.
141 * TBD - figure out a way to only call fixups for
142 * systems that require them.
144 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
145 void *v)
147 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
148 struct pci_bus *bus = func->slot->bridge->pci_bus;
149 u32 buses;
151 if (!bus->self)
152 return NOTIFY_OK;
154 /* fixup bad _DCK function that rewrites
155 * secondary bridge on slot
157 pci_read_config_dword(bus->self,
158 PCI_PRIMARY_BUS,
159 &buses);
161 if (((buses >> 8) & 0xff) != bus->secondary) {
162 buses = (buses & 0xff000000)
163 | ((unsigned int)(bus->primary) << 0)
164 | ((unsigned int)(bus->secondary) << 8)
165 | ((unsigned int)(bus->subordinate) << 16);
166 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
168 return NOTIFY_OK;
174 /* callback routine to register each ACPI PCI slot object */
175 static acpi_status
176 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
178 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
179 struct acpiphp_slot *slot;
180 struct acpiphp_func *newfunc;
181 acpi_handle tmp;
182 acpi_status status = AE_OK;
183 unsigned long adr, sun;
184 int device, function, retval;
186 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
188 if (ACPI_FAILURE(status))
189 return AE_OK;
191 status = acpi_get_handle(handle, "_EJ0", &tmp);
193 if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
194 return AE_OK;
196 device = (adr >> 16) & 0xffff;
197 function = adr & 0xffff;
199 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
200 if (!newfunc)
201 return AE_NO_MEMORY;
203 INIT_LIST_HEAD(&newfunc->sibling);
204 newfunc->handle = handle;
205 newfunc->function = function;
206 if (ACPI_SUCCESS(status))
207 newfunc->flags = FUNC_HAS_EJ0;
209 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
210 newfunc->flags |= FUNC_HAS_STA;
212 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
213 newfunc->flags |= FUNC_HAS_PS0;
215 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
216 newfunc->flags |= FUNC_HAS_PS3;
218 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
219 newfunc->flags |= FUNC_HAS_DCK;
221 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
222 if (ACPI_FAILURE(status)) {
224 * use the count of the number of slots we've found
225 * for the number of the slot
227 sun = bridge->nr_slots+1;
230 /* search for objects that share the same slot */
231 for (slot = bridge->slots; slot; slot = slot->next)
232 if (slot->device == device) {
233 if (slot->sun != sun)
234 warn("sibling found, but _SUN doesn't match!\n");
235 break;
238 if (!slot) {
239 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
240 if (!slot) {
241 kfree(newfunc);
242 return AE_NO_MEMORY;
245 slot->bridge = bridge;
246 slot->device = device;
247 slot->sun = sun;
248 INIT_LIST_HEAD(&slot->funcs);
249 mutex_init(&slot->crit_sect);
251 slot->next = bridge->slots;
252 bridge->slots = slot;
254 bridge->nr_slots++;
256 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
257 slot->sun, pci_domain_nr(bridge->pci_bus),
258 bridge->pci_bus->number, slot->device);
259 retval = acpiphp_register_hotplug_slot(slot);
260 if (retval) {
261 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
262 goto err_exit;
266 newfunc->slot = slot;
267 list_add_tail(&newfunc->sibling, &slot->funcs);
269 /* associate corresponding pci_dev */
270 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
271 PCI_DEVFN(device, function));
272 if (newfunc->pci_dev) {
273 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
276 if (is_dock_device(handle)) {
277 /* we don't want to call this device's _EJ0
278 * because we want the dock notify handler
279 * to call it after it calls _DCK
281 newfunc->flags &= ~FUNC_HAS_EJ0;
282 if (register_hotplug_dock_device(handle,
283 handle_hotplug_event_func, newfunc))
284 dbg("failed to register dock device\n");
286 /* we need to be notified when dock events happen
287 * outside of the hotplug operation, since we may
288 * need to do fixups before we can hotplug.
290 newfunc->nb.notifier_call = post_dock_fixups;
291 if (register_dock_notifier(&newfunc->nb))
292 dbg("failed to register a dock notifier");
295 /* install notify handler */
296 if (!(newfunc->flags & FUNC_HAS_DCK)) {
297 status = acpi_install_notify_handler(handle,
298 ACPI_SYSTEM_NOTIFY,
299 handle_hotplug_event_func,
300 newfunc);
302 if (ACPI_FAILURE(status))
303 err("failed to register interrupt notify handler\n");
304 } else
305 status = AE_OK;
307 return status;
309 err_exit:
310 bridge->nr_slots--;
311 bridge->slots = slot->next;
312 kfree(slot);
313 kfree(newfunc);
315 return AE_OK;
319 /* see if it's worth looking at this bridge */
320 static int detect_ejectable_slots(acpi_handle *bridge_handle)
322 acpi_status status;
323 int count;
325 count = 0;
327 /* only check slots defined directly below bridge object */
328 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
329 is_ejectable_slot, (void *)&count, NULL);
332 * we also need to add this bridge if there is a dock bridge or
333 * other pci device on a dock station (removable)
335 if (!count)
336 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
337 (u32)1, is_pci_dock_device, (void *)&count,
338 NULL);
340 return count;
344 /* decode ACPI 2.0 _HPP hot plug parameters */
345 static void decode_hpp(struct acpiphp_bridge *bridge)
347 acpi_status status;
349 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
350 if (ACPI_FAILURE(status) ||
351 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
352 /* use default numbers */
353 printk(KERN_WARNING
354 "%s: Could not get hotplug parameters. Use defaults\n",
355 __FUNCTION__);
356 bridge->hpp.t0 = &bridge->hpp.type0_data;
357 bridge->hpp.t0->revision = 0;
358 bridge->hpp.t0->cache_line_size = 0x10;
359 bridge->hpp.t0->latency_timer = 0x40;
360 bridge->hpp.t0->enable_serr = 0;
361 bridge->hpp.t0->enable_perr = 0;
367 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
368 static void init_bridge_misc(struct acpiphp_bridge *bridge)
370 acpi_status status;
372 /* decode ACPI 2.0 _HPP (hot plug parameters) */
373 decode_hpp(bridge);
375 /* must be added to the list prior to calling register_slot */
376 list_add(&bridge->list, &bridge_list);
378 /* register all slot objects under this bridge */
379 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
380 register_slot, bridge, NULL);
381 if (ACPI_FAILURE(status)) {
382 list_del(&bridge->list);
383 return;
386 /* install notify handler */
387 if (bridge->type != BRIDGE_TYPE_HOST) {
388 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
389 status = acpi_remove_notify_handler(bridge->func->handle,
390 ACPI_SYSTEM_NOTIFY,
391 handle_hotplug_event_func);
392 if (ACPI_FAILURE(status))
393 err("failed to remove notify handler\n");
395 status = acpi_install_notify_handler(bridge->handle,
396 ACPI_SYSTEM_NOTIFY,
397 handle_hotplug_event_bridge,
398 bridge);
400 if (ACPI_FAILURE(status)) {
401 err("failed to register interrupt notify handler\n");
407 /* find acpiphp_func from acpiphp_bridge */
408 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
410 struct list_head *node, *l;
411 struct acpiphp_bridge *bridge;
412 struct acpiphp_slot *slot;
413 struct acpiphp_func *func;
415 list_for_each(node, &bridge_list) {
416 bridge = list_entry(node, struct acpiphp_bridge, list);
417 for (slot = bridge->slots; slot; slot = slot->next) {
418 list_for_each(l, &slot->funcs) {
419 func = list_entry(l, struct acpiphp_func,
420 sibling);
421 if (func->handle == handle)
422 return func;
427 return NULL;
431 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
433 acpi_handle dummy_handle;
435 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
436 "_STA", &dummy_handle)))
437 bridge->flags |= BRIDGE_HAS_STA;
439 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
440 "_EJ0", &dummy_handle)))
441 bridge->flags |= BRIDGE_HAS_EJ0;
443 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
444 "_PS0", &dummy_handle)))
445 bridge->flags |= BRIDGE_HAS_PS0;
447 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
448 "_PS3", &dummy_handle)))
449 bridge->flags |= BRIDGE_HAS_PS3;
451 /* is this ejectable p2p bridge? */
452 if (bridge->flags & BRIDGE_HAS_EJ0) {
453 struct acpiphp_func *func;
455 dbg("found ejectable p2p bridge\n");
457 /* make link between PCI bridge and PCI function */
458 func = acpiphp_bridge_handle_to_function(bridge->handle);
459 if (!func)
460 return;
461 bridge->func = func;
462 func->bridge = bridge;
467 /* allocate and initialize host bridge data structure */
468 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
470 struct acpiphp_bridge *bridge;
472 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
473 if (bridge == NULL)
474 return;
476 bridge->type = BRIDGE_TYPE_HOST;
477 bridge->handle = handle;
479 bridge->pci_bus = pci_bus;
481 spin_lock_init(&bridge->res_lock);
483 init_bridge_misc(bridge);
487 /* allocate and initialize PCI-to-PCI bridge data structure */
488 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
490 struct acpiphp_bridge *bridge;
492 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
493 if (bridge == NULL) {
494 err("out of memory\n");
495 return;
498 bridge->type = BRIDGE_TYPE_P2P;
499 bridge->handle = handle;
500 config_p2p_bridge_flags(bridge);
502 bridge->pci_dev = pci_dev_get(pci_dev);
503 bridge->pci_bus = pci_dev->subordinate;
504 if (!bridge->pci_bus) {
505 err("This is not a PCI-to-PCI bridge!\n");
506 goto err;
509 spin_lock_init(&bridge->res_lock);
511 init_bridge_misc(bridge);
512 return;
513 err:
514 pci_dev_put(pci_dev);
515 kfree(bridge);
516 return;
520 /* callback routine to find P2P bridges */
521 static acpi_status
522 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
524 acpi_status status;
525 acpi_handle dummy_handle;
526 unsigned long tmp;
527 int device, function;
528 struct pci_dev *dev;
529 struct pci_bus *pci_bus = context;
531 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
532 if (ACPI_FAILURE(status))
533 return AE_OK; /* continue */
535 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
536 if (ACPI_FAILURE(status)) {
537 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
538 return AE_OK;
541 device = (tmp >> 16) & 0xffff;
542 function = tmp & 0xffff;
544 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
546 if (!dev || !dev->subordinate)
547 goto out;
549 /* check if this bridge has ejectable slots */
550 if ((detect_ejectable_slots(handle) > 0)) {
551 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
552 add_p2p_bridge(handle, dev);
555 /* search P2P bridges under this p2p bridge */
556 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
557 find_p2p_bridge, dev->subordinate, NULL);
558 if (ACPI_FAILURE(status))
559 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
561 out:
562 pci_dev_put(dev);
563 return AE_OK;
567 /* find hot-pluggable slots, and then find P2P bridge */
568 static int add_bridge(acpi_handle handle)
570 acpi_status status;
571 unsigned long tmp;
572 int seg, bus;
573 acpi_handle dummy_handle;
574 struct pci_bus *pci_bus;
576 /* if the bridge doesn't have _STA, we assume it is always there */
577 status = acpi_get_handle(handle, "_STA", &dummy_handle);
578 if (ACPI_SUCCESS(status)) {
579 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
580 if (ACPI_FAILURE(status)) {
581 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
582 return 0;
584 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
585 /* don't register this object */
586 return 0;
589 /* get PCI segment number */
590 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
592 seg = ACPI_SUCCESS(status) ? tmp : 0;
594 /* get PCI bus number */
595 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
597 if (ACPI_SUCCESS(status)) {
598 bus = tmp;
599 } else {
600 warn("can't get bus number, assuming 0\n");
601 bus = 0;
604 pci_bus = pci_find_bus(seg, bus);
605 if (!pci_bus) {
606 err("Can't find bus %04x:%02x\n", seg, bus);
607 return 0;
610 /* check if this bridge has ejectable slots */
611 if (detect_ejectable_slots(handle) > 0) {
612 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
613 add_host_bridge(handle, pci_bus);
616 /* search P2P bridges under this host bridge */
617 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
618 find_p2p_bridge, pci_bus, NULL);
620 if (ACPI_FAILURE(status))
621 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
623 return 0;
626 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
628 struct list_head *head;
629 list_for_each(head, &bridge_list) {
630 struct acpiphp_bridge *bridge = list_entry(head,
631 struct acpiphp_bridge, list);
632 if (bridge->handle == handle)
633 return bridge;
636 return NULL;
639 static void cleanup_bridge(struct acpiphp_bridge *bridge)
641 struct list_head *list, *tmp;
642 struct acpiphp_slot *slot;
643 acpi_status status;
644 acpi_handle handle = bridge->handle;
646 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
647 handle_hotplug_event_bridge);
648 if (ACPI_FAILURE(status))
649 err("failed to remove notify handler\n");
651 if ((bridge->type != BRIDGE_TYPE_HOST) &&
652 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
653 status = acpi_install_notify_handler(bridge->func->handle,
654 ACPI_SYSTEM_NOTIFY,
655 handle_hotplug_event_func,
656 bridge->func);
657 if (ACPI_FAILURE(status))
658 err("failed to install interrupt notify handler\n");
661 slot = bridge->slots;
662 while (slot) {
663 struct acpiphp_slot *next = slot->next;
664 list_for_each_safe (list, tmp, &slot->funcs) {
665 struct acpiphp_func *func;
666 func = list_entry(list, struct acpiphp_func, sibling);
667 if (is_dock_device(func->handle)) {
668 unregister_hotplug_dock_device(func->handle);
669 unregister_dock_notifier(&func->nb);
671 if (!(func->flags & FUNC_HAS_DCK)) {
672 status = acpi_remove_notify_handler(func->handle,
673 ACPI_SYSTEM_NOTIFY,
674 handle_hotplug_event_func);
675 if (ACPI_FAILURE(status))
676 err("failed to remove notify handler\n");
678 pci_dev_put(func->pci_dev);
679 list_del(list);
680 kfree(func);
682 acpiphp_unregister_hotplug_slot(slot);
683 list_del(&slot->funcs);
684 kfree(slot);
685 slot = next;
688 pci_dev_put(bridge->pci_dev);
689 list_del(&bridge->list);
690 kfree(bridge);
693 static acpi_status
694 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
696 struct acpiphp_bridge *bridge;
698 /* cleanup p2p bridges under this P2P bridge
699 in a depth-first manner */
700 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
701 cleanup_p2p_bridge, NULL, NULL);
703 if (!(bridge = acpiphp_handle_to_bridge(handle)))
704 return AE_OK;
705 cleanup_bridge(bridge);
706 return AE_OK;
709 static void remove_bridge(acpi_handle handle)
711 struct acpiphp_bridge *bridge;
713 /* cleanup p2p bridges under this host bridge
714 in a depth-first manner */
715 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
716 (u32)1, cleanup_p2p_bridge, NULL, NULL);
718 bridge = acpiphp_handle_to_bridge(handle);
719 if (bridge)
720 cleanup_bridge(bridge);
723 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
725 struct acpi_pci_id id;
726 struct pci_bus *bus;
727 struct pci_dev *dev;
729 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
730 return NULL;
732 bus = pci_find_bus(id.segment, id.bus);
733 if (!bus)
734 return NULL;
736 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
737 if (!dev)
738 return NULL;
740 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
741 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
743 pci_dev_put(dev);
744 return NULL;
747 return dev;
750 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
752 acpi_status status;
753 int result = -1;
754 unsigned long gsb;
755 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
756 union acpi_object *obj;
757 void *table;
759 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
760 if (ACPI_SUCCESS(status)) {
761 *gsi_base = (u32)gsb;
762 return 0;
765 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
766 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
767 return -1;
769 obj = buffer.pointer;
770 if (obj->type != ACPI_TYPE_BUFFER)
771 goto out;
773 table = obj->buffer.pointer;
774 switch (((struct acpi_subtable_header *)table)->type) {
775 case ACPI_MADT_TYPE_IO_SAPIC:
776 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
777 result = 0;
778 break;
779 case ACPI_MADT_TYPE_IO_APIC:
780 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
781 result = 0;
782 break;
783 default:
784 break;
786 out:
787 kfree(buffer.pointer);
788 return result;
791 static acpi_status
792 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
794 acpi_status status;
795 unsigned long sta;
796 acpi_handle tmp;
797 struct pci_dev *pdev;
798 u32 gsi_base;
799 u64 phys_addr;
800 struct acpiphp_ioapic *ioapic;
802 /* Evaluate _STA if present */
803 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
804 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
805 return AE_CTRL_DEPTH;
807 /* Scan only PCI bus scope */
808 status = acpi_get_handle(handle, "_HID", &tmp);
809 if (ACPI_SUCCESS(status))
810 return AE_CTRL_DEPTH;
812 if (get_gsi_base(handle, &gsi_base))
813 return AE_OK;
815 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
816 if (!ioapic)
817 return AE_NO_MEMORY;
819 pdev = get_apic_pci_info(handle);
820 if (!pdev)
821 goto exit_kfree;
823 if (pci_enable_device(pdev))
824 goto exit_pci_dev_put;
826 pci_set_master(pdev);
828 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
829 goto exit_pci_disable_device;
831 phys_addr = pci_resource_start(pdev, 0);
832 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
833 goto exit_pci_release_region;
835 ioapic->gsi_base = gsi_base;
836 ioapic->dev = pdev;
837 spin_lock(&ioapic_list_lock);
838 list_add_tail(&ioapic->list, &ioapic_list);
839 spin_unlock(&ioapic_list_lock);
841 return AE_OK;
843 exit_pci_release_region:
844 pci_release_region(pdev, 0);
845 exit_pci_disable_device:
846 pci_disable_device(pdev);
847 exit_pci_dev_put:
848 pci_dev_put(pdev);
849 exit_kfree:
850 kfree(ioapic);
852 return AE_OK;
855 static acpi_status
856 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
858 acpi_status status;
859 unsigned long sta;
860 acpi_handle tmp;
861 u32 gsi_base;
862 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
864 /* Evaluate _STA if present */
865 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
866 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
867 return AE_CTRL_DEPTH;
869 /* Scan only PCI bus scope */
870 status = acpi_get_handle(handle, "_HID", &tmp);
871 if (ACPI_SUCCESS(status))
872 return AE_CTRL_DEPTH;
874 if (get_gsi_base(handle, &gsi_base))
875 return AE_OK;
877 acpi_unregister_ioapic(handle, gsi_base);
879 spin_lock(&ioapic_list_lock);
880 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
881 if (pos->gsi_base != gsi_base)
882 continue;
883 ioapic = pos;
884 list_del(&ioapic->list);
885 break;
887 spin_unlock(&ioapic_list_lock);
889 if (!ioapic)
890 return AE_OK;
892 pci_release_region(ioapic->dev, 0);
893 pci_disable_device(ioapic->dev);
894 pci_dev_put(ioapic->dev);
895 kfree(ioapic);
897 return AE_OK;
900 static int acpiphp_configure_ioapics(acpi_handle handle)
902 ioapic_add(handle, 0, NULL, NULL);
903 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
904 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
905 return 0;
908 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
910 ioapic_remove(handle, 0, NULL, NULL);
911 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
912 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
913 return 0;
916 static int power_on_slot(struct acpiphp_slot *slot)
918 acpi_status status;
919 struct acpiphp_func *func;
920 struct list_head *l;
921 int retval = 0;
923 /* if already enabled, just skip */
924 if (slot->flags & SLOT_POWEREDON)
925 goto err_exit;
927 list_for_each (l, &slot->funcs) {
928 func = list_entry(l, struct acpiphp_func, sibling);
930 if (func->flags & FUNC_HAS_PS0) {
931 dbg("%s: executing _PS0\n", __FUNCTION__);
932 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
933 if (ACPI_FAILURE(status)) {
934 warn("%s: _PS0 failed\n", __FUNCTION__);
935 retval = -1;
936 goto err_exit;
937 } else
938 break;
942 /* TBD: evaluate _STA to check if the slot is enabled */
944 slot->flags |= SLOT_POWEREDON;
946 err_exit:
947 return retval;
951 static int power_off_slot(struct acpiphp_slot *slot)
953 acpi_status status;
954 struct acpiphp_func *func;
955 struct list_head *l;
957 int retval = 0;
959 /* if already disabled, just skip */
960 if ((slot->flags & SLOT_POWEREDON) == 0)
961 goto err_exit;
963 list_for_each (l, &slot->funcs) {
964 func = list_entry(l, struct acpiphp_func, sibling);
966 if (func->flags & FUNC_HAS_PS3) {
967 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
968 if (ACPI_FAILURE(status)) {
969 warn("%s: _PS3 failed\n", __FUNCTION__);
970 retval = -1;
971 goto err_exit;
972 } else
973 break;
977 /* TBD: evaluate _STA to check if the slot is disabled */
979 slot->flags &= (~SLOT_POWEREDON);
981 err_exit:
982 return retval;
988 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
989 * @bus: bus to start search with
991 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
993 struct list_head *tmp;
994 unsigned char max, n;
997 * pci_bus_max_busnr will return the highest
998 * reserved busnr for all these children.
999 * that is equivalent to the bus->subordinate
1000 * value. We don't want to use the parent's
1001 * bus->subordinate value because it could have
1002 * padding in it.
1004 max = bus->secondary;
1006 list_for_each(tmp, &bus->children) {
1007 n = pci_bus_max_busnr(pci_bus_b(tmp));
1008 if (n > max)
1009 max = n;
1011 return max;
1016 * acpiphp_bus_add - add a new bus to acpi subsystem
1017 * @func: acpiphp_func of the bridge
1019 static int acpiphp_bus_add(struct acpiphp_func *func)
1021 acpi_handle phandle;
1022 struct acpi_device *device, *pdevice;
1023 int ret_val;
1025 acpi_get_parent(func->handle, &phandle);
1026 if (acpi_bus_get_device(phandle, &pdevice)) {
1027 dbg("no parent device, assuming NULL\n");
1028 pdevice = NULL;
1030 if (!acpi_bus_get_device(func->handle, &device)) {
1031 dbg("bus exists... trim\n");
1032 /* this shouldn't be in here, so remove
1033 * the bus then re-add it...
1035 ret_val = acpi_bus_trim(device, 1);
1036 dbg("acpi_bus_trim return %x\n", ret_val);
1039 ret_val = acpi_bus_add(&device, pdevice, func->handle,
1040 ACPI_BUS_TYPE_DEVICE);
1041 if (ret_val) {
1042 dbg("error adding bus, %x\n",
1043 -ret_val);
1044 goto acpiphp_bus_add_out;
1047 * try to start anyway. We could have failed to add
1048 * simply because this bus had previously been added
1049 * on another add. Don't bother with the return value
1050 * we just keep going.
1052 ret_val = acpi_bus_start(device);
1054 acpiphp_bus_add_out:
1055 return ret_val;
1060 * acpiphp_bus_trim - trim a bus from acpi subsystem
1061 * @handle: handle to acpi namespace
1063 static int acpiphp_bus_trim(acpi_handle handle)
1065 struct acpi_device *device;
1066 int retval;
1068 retval = acpi_bus_get_device(handle, &device);
1069 if (retval) {
1070 dbg("acpi_device not found\n");
1071 return retval;
1074 retval = acpi_bus_trim(device, 1);
1075 if (retval)
1076 err("cannot remove from acpi list\n");
1078 return retval;
1082 * enable_device - enable, configure a slot
1083 * @slot: slot to be enabled
1085 * This function should be called per *physical slot*,
1086 * not per each slot object in ACPI namespace.
1088 <<<<<<< HEAD:drivers/pci/hotplug/acpiphp_glue.c
1089 static int enable_device(struct acpiphp_slot *slot)
1090 =======
1091 static int __ref enable_device(struct acpiphp_slot *slot)
1092 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/pci/hotplug/acpiphp_glue.c
1094 struct pci_dev *dev;
1095 struct pci_bus *bus = slot->bridge->pci_bus;
1096 struct list_head *l;
1097 struct acpiphp_func *func;
1098 int retval = 0;
1099 int num, max, pass;
1100 acpi_status status;
1102 if (slot->flags & SLOT_ENABLED)
1103 goto err_exit;
1105 /* sanity check: dev should be NULL when hot-plugged in */
1106 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1107 if (dev) {
1108 /* This case shouldn't happen */
1109 err("pci_dev structure already exists.\n");
1110 pci_dev_put(dev);
1111 retval = -1;
1112 goto err_exit;
1115 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1116 if (num == 0) {
1117 err("No new device found\n");
1118 retval = -1;
1119 goto err_exit;
1122 max = acpiphp_max_busnr(bus);
1123 for (pass = 0; pass < 2; pass++) {
1124 list_for_each_entry(dev, &bus->devices, bus_list) {
1125 if (PCI_SLOT(dev->devfn) != slot->device)
1126 continue;
1127 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1128 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1129 max = pci_scan_bridge(bus, dev, max, pass);
1130 if (pass && dev->subordinate)
1131 pci_bus_size_bridges(dev->subordinate);
1136 list_for_each (l, &slot->funcs) {
1137 func = list_entry(l, struct acpiphp_func, sibling);
1138 acpiphp_bus_add(func);
1141 pci_bus_assign_resources(bus);
1142 acpiphp_sanitize_bus(bus);
1143 acpiphp_set_hpp_values(slot->bridge->handle, bus);
1144 list_for_each_entry(func, &slot->funcs, sibling)
1145 acpiphp_configure_ioapics(func->handle);
1146 pci_enable_bridges(bus);
1147 pci_bus_add_devices(bus);
1149 /* associate pci_dev to our representation */
1150 list_for_each (l, &slot->funcs) {
1151 func = list_entry(l, struct acpiphp_func, sibling);
1152 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1153 func->function));
1154 if (!func->pci_dev)
1155 continue;
1157 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1158 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1159 continue;
1161 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1162 if (ACPI_FAILURE(status))
1163 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1164 status);
1167 slot->flags |= SLOT_ENABLED;
1169 err_exit:
1170 return retval;
1173 static void disable_bridges(struct pci_bus *bus)
1175 struct pci_dev *dev;
1176 list_for_each_entry(dev, &bus->devices, bus_list) {
1177 if (dev->subordinate) {
1178 disable_bridges(dev->subordinate);
1179 pci_disable_device(dev);
1185 * disable_device - disable a slot
1186 * @slot: ACPI PHP slot
1188 static int disable_device(struct acpiphp_slot *slot)
1190 int retval = 0;
1191 struct acpiphp_func *func;
1192 struct list_head *l;
1194 /* is this slot already disabled? */
1195 if (!(slot->flags & SLOT_ENABLED))
1196 goto err_exit;
1198 list_for_each (l, &slot->funcs) {
1199 func = list_entry(l, struct acpiphp_func, sibling);
1201 if (func->bridge) {
1202 /* cleanup p2p bridges under this P2P bridge */
1203 cleanup_p2p_bridge(func->bridge->handle,
1204 (u32)1, NULL, NULL);
1205 func->bridge = NULL;
1208 if (func->pci_dev) {
1209 pci_stop_bus_device(func->pci_dev);
1210 if (func->pci_dev->subordinate) {
1211 disable_bridges(func->pci_dev->subordinate);
1212 pci_disable_device(func->pci_dev);
1217 list_for_each (l, &slot->funcs) {
1218 func = list_entry(l, struct acpiphp_func, sibling);
1220 acpiphp_unconfigure_ioapics(func->handle);
1221 acpiphp_bus_trim(func->handle);
1222 /* try to remove anyway.
1223 * acpiphp_bus_add might have been failed */
1225 if (!func->pci_dev)
1226 continue;
1228 pci_remove_bus_device(func->pci_dev);
1229 pci_dev_put(func->pci_dev);
1230 func->pci_dev = NULL;
1233 slot->flags &= (~SLOT_ENABLED);
1235 err_exit:
1236 return retval;
1241 * get_slot_status - get ACPI slot status
1242 * @slot: ACPI PHP slot
1244 * If a slot has _STA for each function and if any one of them
1245 * returned non-zero status, return it.
1247 * If a slot doesn't have _STA and if any one of its functions'
1248 * configuration space is configured, return 0x0f as a _STA.
1250 * Otherwise return 0.
1252 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1254 acpi_status status;
1255 unsigned long sta = 0;
1256 u32 dvid;
1257 struct list_head *l;
1258 struct acpiphp_func *func;
1260 list_for_each (l, &slot->funcs) {
1261 func = list_entry(l, struct acpiphp_func, sibling);
1263 if (func->flags & FUNC_HAS_STA) {
1264 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1265 if (ACPI_SUCCESS(status) && sta)
1266 break;
1267 } else {
1268 pci_bus_read_config_dword(slot->bridge->pci_bus,
1269 PCI_DEVFN(slot->device,
1270 func->function),
1271 PCI_VENDOR_ID, &dvid);
1272 if (dvid != 0xffffffff) {
1273 sta = ACPI_STA_ALL;
1274 break;
1279 return (unsigned int)sta;
1283 * acpiphp_eject_slot - physically eject the slot
1284 * @slot: ACPI PHP slot
1286 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
1318 * @bridge: where to begin re-enumeration
1320 * Iterate over all slots under this bridge and make sure that if a
1321 * card is present they are enabled, and if not they are disabled.
1323 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1325 struct acpiphp_slot *slot;
1326 int retval = 0;
1327 int enabled, disabled;
1329 enabled = disabled = 0;
1331 for (slot = bridge->slots; slot; slot = slot->next) {
1332 unsigned int status = get_slot_status(slot);
1333 if (slot->flags & SLOT_ENABLED) {
1334 if (status == ACPI_STA_ALL)
1335 continue;
1336 retval = acpiphp_disable_slot(slot);
1337 if (retval) {
1338 err("Error occurred in disabling\n");
1339 goto err_exit;
1340 } else {
1341 acpiphp_eject_slot(slot);
1343 disabled++;
1344 } else {
1345 if (status != ACPI_STA_ALL)
1346 continue;
1347 retval = acpiphp_enable_slot(slot);
1348 if (retval) {
1349 err("Error occurred in enabling\n");
1350 goto err_exit;
1352 enabled++;
1356 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1358 err_exit:
1359 return retval;
1362 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1364 u16 pci_cmd, pci_bctl;
1365 struct pci_dev *cdev;
1367 /* Program hpp values for this device */
1368 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1369 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1370 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1371 return;
1373 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1374 return;
1376 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1377 bridge->hpp.t0->cache_line_size);
1378 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1379 bridge->hpp.t0->latency_timer);
1380 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1381 if (bridge->hpp.t0->enable_serr)
1382 pci_cmd |= PCI_COMMAND_SERR;
1383 else
1384 pci_cmd &= ~PCI_COMMAND_SERR;
1385 if (bridge->hpp.t0->enable_perr)
1386 pci_cmd |= PCI_COMMAND_PARITY;
1387 else
1388 pci_cmd &= ~PCI_COMMAND_PARITY;
1389 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1391 /* Program bridge control value and child devices */
1392 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1393 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1394 bridge->hpp.t0->latency_timer);
1395 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1396 if (bridge->hpp.t0->enable_serr)
1397 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1398 else
1399 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1400 if (bridge->hpp.t0->enable_perr)
1401 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1402 else
1403 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1404 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1405 if (dev->subordinate) {
1406 list_for_each_entry(cdev, &dev->subordinate->devices,
1407 bus_list)
1408 program_hpp(cdev, bridge);
1413 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1415 struct acpiphp_bridge bridge;
1416 struct pci_dev *dev;
1418 memset(&bridge, 0, sizeof(bridge));
1419 bridge.handle = handle;
1420 bridge.pci_bus = bus;
1421 bridge.pci_dev = bus->self;
1422 decode_hpp(&bridge);
1423 list_for_each_entry(dev, &bus->devices, bus_list)
1424 program_hpp(dev, &bridge);
1429 * Remove devices for which we could not assign resources, call
1430 * arch specific code to fix-up the bus
1432 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1434 struct pci_dev *dev;
1435 int i;
1436 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1438 list_for_each_entry(dev, &bus->devices, bus_list) {
1439 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1440 struct resource *res = &dev->resource[i];
1441 if ((res->flags & type_mask) && !res->start &&
1442 res->end) {
1443 /* Could not assign a required resources
1444 * for this device, remove it */
1445 pci_remove_bus_device(dev);
1446 break;
1452 /* Program resources in newly inserted bridge */
1453 static int acpiphp_configure_bridge (acpi_handle handle)
1455 struct acpi_pci_id pci_id;
1456 struct pci_bus *bus;
1458 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1459 err("cannot get PCI domain and bus number for bridge\n");
1460 return -EINVAL;
1462 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1463 if (!bus) {
1464 err("cannot find bus %d:%d\n",
1465 pci_id.segment, pci_id.bus);
1466 return -EINVAL;
1469 pci_bus_size_bridges(bus);
1470 pci_bus_assign_resources(bus);
1471 acpiphp_sanitize_bus(bus);
1472 acpiphp_set_hpp_values(handle, bus);
1473 pci_enable_bridges(bus);
1474 acpiphp_configure_ioapics(handle);
1475 return 0;
1478 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1480 struct acpi_device *device, *pdevice;
1481 acpi_handle phandle;
1483 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1484 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1485 err("unexpected notification type %d\n", type);
1486 return;
1489 acpi_get_parent(handle, &phandle);
1490 if (acpi_bus_get_device(phandle, &pdevice)) {
1491 dbg("no parent device, assuming NULL\n");
1492 pdevice = NULL;
1494 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1495 err("cannot add bridge to acpi list\n");
1496 return;
1498 if (!acpiphp_configure_bridge(handle) &&
1499 !acpi_bus_start(device))
1500 add_bridge(handle);
1501 else
1502 err("cannot configure and start bridge\n");
1507 * ACPI event handlers
1510 static acpi_status
1511 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1513 int *count = (int *)context;
1514 struct acpiphp_bridge *bridge;
1516 bridge = acpiphp_handle_to_bridge(handle);
1517 if (bridge)
1518 (*count)++;
1519 return AE_OK ;
1522 static acpi_status
1523 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1525 struct acpiphp_bridge *bridge;
1526 char objname[64];
1527 struct acpi_buffer buffer = { .length = sizeof(objname),
1528 .pointer = objname };
1530 bridge = acpiphp_handle_to_bridge(handle);
1531 if (bridge) {
1532 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1533 dbg("%s: re-enumerating slots under %s\n",
1534 __FUNCTION__, objname);
1535 acpiphp_check_bridge(bridge);
1537 return AE_OK ;
1541 * handle_hotplug_event_bridge - handle ACPI event on bridges
1542 * @handle: Notify()'ed acpi_handle
1543 * @type: Notify code
1544 * @context: pointer to acpiphp_bridge structure
1546 * Handles ACPI event notification on {host,p2p} bridges.
1548 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1550 struct acpiphp_bridge *bridge;
1551 char objname[64];
1552 struct acpi_buffer buffer = { .length = sizeof(objname),
1553 .pointer = objname };
1554 struct acpi_device *device;
1555 int num_sub_bridges = 0;
1557 if (acpi_bus_get_device(handle, &device)) {
1558 /* This bridge must have just been physically inserted */
1559 handle_bridge_insertion(handle, type);
1560 return;
1563 bridge = acpiphp_handle_to_bridge(handle);
1564 if (type == ACPI_NOTIFY_BUS_CHECK) {
1565 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1566 count_sub_bridges, &num_sub_bridges, NULL);
1569 if (!bridge && !num_sub_bridges) {
1570 err("cannot get bridge info\n");
1571 return;
1574 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1576 switch (type) {
1577 case ACPI_NOTIFY_BUS_CHECK:
1578 /* bus re-enumerate */
1579 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1580 if (bridge) {
1581 dbg("%s: re-enumerating slots under %s\n",
1582 __FUNCTION__, objname);
1583 acpiphp_check_bridge(bridge);
1585 if (num_sub_bridges)
1586 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1587 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1588 break;
1590 case ACPI_NOTIFY_DEVICE_CHECK:
1591 /* device check */
1592 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1593 acpiphp_check_bridge(bridge);
1594 break;
1596 case ACPI_NOTIFY_DEVICE_WAKE:
1597 /* wake event */
1598 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1599 break;
1601 case ACPI_NOTIFY_EJECT_REQUEST:
1602 /* request device eject */
1603 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1604 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1605 (bridge->flags & BRIDGE_HAS_EJ0)) {
1606 struct acpiphp_slot *slot;
1607 slot = bridge->func->slot;
1608 if (!acpiphp_disable_slot(slot))
1609 acpiphp_eject_slot(slot);
1611 break;
1613 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1614 printk(KERN_ERR "Device %s cannot be configured due"
1615 " to a frequency mismatch\n", objname);
1616 break;
1618 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1619 printk(KERN_ERR "Device %s cannot be configured due"
1620 " to a bus mode mismatch\n", objname);
1621 break;
1623 case ACPI_NOTIFY_POWER_FAULT:
1624 printk(KERN_ERR "Device %s has suffered a power fault\n",
1625 objname);
1626 break;
1628 default:
1629 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1630 break;
1635 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1636 * @handle: Notify()'ed acpi_handle
1637 * @type: Notify code
1638 * @context: pointer to acpiphp_func structure
1640 * Handles ACPI event notification on slots.
1642 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1644 struct acpiphp_func *func;
1645 char objname[64];
1646 struct acpi_buffer buffer = { .length = sizeof(objname),
1647 .pointer = objname };
1649 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1651 func = (struct acpiphp_func *)context;
1653 switch (type) {
1654 case ACPI_NOTIFY_BUS_CHECK:
1655 /* bus re-enumerate */
1656 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1657 acpiphp_enable_slot(func->slot);
1658 break;
1660 case ACPI_NOTIFY_DEVICE_CHECK:
1661 /* device check : re-enumerate from parent bus */
1662 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1663 acpiphp_check_bridge(func->slot->bridge);
1664 break;
1666 case ACPI_NOTIFY_DEVICE_WAKE:
1667 /* wake event */
1668 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1669 break;
1671 case ACPI_NOTIFY_EJECT_REQUEST:
1672 /* request device eject */
1673 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1674 if (!(acpiphp_disable_slot(func->slot)))
1675 acpiphp_eject_slot(func->slot);
1676 break;
1678 default:
1679 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1680 break;
1685 static acpi_status
1686 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1688 int *count = (int *)context;
1690 if (acpi_root_bridge(handle)) {
1691 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1692 handle_hotplug_event_bridge, NULL);
1693 (*count)++;
1695 return AE_OK ;
1698 static struct acpi_pci_driver acpi_pci_hp_driver = {
1699 .add = add_bridge,
1700 .remove = remove_bridge,
1704 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1706 int __init acpiphp_glue_init(void)
1708 int num = 0;
1710 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1711 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1713 if (num <= 0)
1714 return -1;
1715 else
1716 acpi_pci_register_driver(&acpi_pci_hp_driver);
1718 return 0;
1723 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1725 * This function frees all data allocated in acpiphp_glue_init().
1727 void acpiphp_glue_exit(void)
1729 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1734 * acpiphp_get_num_slots - count number of slots in a system
1736 int __init acpiphp_get_num_slots(void)
1738 struct acpiphp_bridge *bridge;
1739 int num_slots = 0;
1741 list_for_each_entry (bridge, &bridge_list, list) {
1742 dbg("Bus %04x:%02x has %d slot%s\n",
1743 pci_domain_nr(bridge->pci_bus),
1744 bridge->pci_bus->number, bridge->nr_slots,
1745 bridge->nr_slots == 1 ? "" : "s");
1746 num_slots += bridge->nr_slots;
1749 dbg("Total %d slots\n", num_slots);
1750 return num_slots;
1754 #if 0
1756 * acpiphp_for_each_slot - call function for each slot
1757 * @fn: callback function
1758 * @data: context to be passed to callback function
1760 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1762 struct list_head *node;
1763 struct acpiphp_bridge *bridge;
1764 struct acpiphp_slot *slot;
1765 int retval = 0;
1767 list_for_each (node, &bridge_list) {
1768 bridge = (struct acpiphp_bridge *)node;
1769 for (slot = bridge->slots; slot; slot = slot->next) {
1770 retval = fn(slot, data);
1771 if (!retval)
1772 goto err_exit;
1776 err_exit:
1777 return retval;
1779 #endif
1783 * acpiphp_enable_slot - power on slot
1784 * @slot: ACPI PHP slot
1786 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1788 int retval;
1790 mutex_lock(&slot->crit_sect);
1792 /* wake up all functions */
1793 retval = power_on_slot(slot);
1794 if (retval)
1795 goto err_exit;
1797 if (get_slot_status(slot) == ACPI_STA_ALL) {
1798 /* configure all functions */
1799 retval = enable_device(slot);
1800 if (retval)
1801 power_off_slot(slot);
1802 } else {
1803 dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__);
1804 power_off_slot(slot);
1807 err_exit:
1808 mutex_unlock(&slot->crit_sect);
1809 return retval;
1813 * acpiphp_disable_slot - power off slot
1814 * @slot: ACPI PHP slot
1816 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1818 int retval = 0;
1820 mutex_lock(&slot->crit_sect);
1822 /* unconfigure all functions */
1823 retval = disable_device(slot);
1824 if (retval)
1825 goto err_exit;
1827 /* power off all functions */
1828 retval = power_off_slot(slot);
1829 if (retval)
1830 goto err_exit;
1832 err_exit:
1833 mutex_unlock(&slot->crit_sect);
1834 return retval;
1839 * slot enabled: 1
1840 * slot disabled: 0
1842 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1844 return (slot->flags & SLOT_POWEREDON);
1849 * latch open: 1
1850 * latch closed: 0
1852 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1854 unsigned int sta;
1856 sta = get_slot_status(slot);
1858 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1863 * adapter presence : 1
1864 * absence : 0
1866 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1868 unsigned int sta;
1870 sta = get_slot_status(slot);
1872 return (sta == 0) ? 0 : 1;
1877 * pci address (seg/bus/dev)
1879 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1881 u32 address;
1882 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1884 address = (pci_domain_nr(pci_bus) << 16) |
1885 (pci_bus->number << 8) |
1886 slot->device;
1888 return address;