[PATCH] s390: dasd open counter
[linux-2.6.22.y-op.git] / drivers / pci / hotplug / acpiphp_glue.c
blob509a5b3ae998f5dd7160448e539657dbeb9bed41
1 /*
2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
4 * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5 * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6 * Copyright (C) 2002,2003 NEC Corporation
7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
12 * All rights reserved.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22 * NON INFRINGEMENT. See the GNU General Public License for more
23 * details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Send feedback to <t-kochi@bq.jp.nec.com>
34 * Lifetime rules for pci_dev:
35 * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
36 * when the driver is loaded or when an insertion event occurs. It loses
37 * a refcount when its ejected or the driver unloads.
38 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
39 * when the bridge is scanned and it loses a refcount when the bridge
40 * is removed.
43 #include <linux/init.h>
44 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/pci.h>
48 #include <linux/smp_lock.h>
49 #include <asm/semaphore.h>
51 #include "../pci.h"
52 #include "pci_hotplug.h"
53 #include "acpiphp.h"
55 static LIST_HEAD(bridge_list);
57 #define MY_NAME "acpiphp_glue"
59 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
60 static void handle_hotplug_event_func (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);
66 * initialization & terminatation routines
69 /**
70 * is_ejectable - determine if a slot is ejectable
71 * @handle: handle to acpi namespace
73 * Ejectable slot should satisfy at least these conditions:
75 * 1. has _ADR method
76 * 2. has _EJ0 method
78 * optionally
80 * 1. has _STA method
81 * 2. has _PS0 method
82 * 3. has _PS3 method
83 * 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 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;
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 adr, sun;
131 int device, function;
132 static int num_slots = 0; /* XXX if we support I/O node hotplug... */
134 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
136 if (ACPI_FAILURE(status))
137 return AE_OK;
139 status = acpi_get_handle(handle, "_EJ0", &tmp);
141 if (ACPI_FAILURE(status))
142 return AE_OK;
144 device = (adr >> 16) & 0xffff;
145 function = adr & 0xffff;
147 newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
148 if (!newfunc)
149 return AE_NO_MEMORY;
150 memset(newfunc, 0, sizeof(struct acpiphp_func));
152 INIT_LIST_HEAD(&newfunc->sibling);
153 newfunc->handle = handle;
154 newfunc->function = function;
155 newfunc->flags = FUNC_HAS_EJ0;
157 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
158 newfunc->flags |= FUNC_HAS_STA;
160 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
161 newfunc->flags |= FUNC_HAS_PS0;
163 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
164 newfunc->flags |= FUNC_HAS_PS3;
166 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
167 if (ACPI_FAILURE(status))
168 sun = -1;
170 /* search for objects that share the same slot */
171 for (slot = bridge->slots; slot; slot = slot->next)
172 if (slot->device == device) {
173 if (slot->sun != sun)
174 warn("sibling found, but _SUN doesn't match!\n");
175 break;
178 if (!slot) {
179 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
180 if (!slot) {
181 kfree(newfunc);
182 return AE_NO_MEMORY;
185 memset(slot, 0, sizeof(struct acpiphp_slot));
186 slot->bridge = bridge;
187 slot->id = num_slots++;
188 slot->device = device;
189 slot->sun = sun;
190 INIT_LIST_HEAD(&slot->funcs);
191 init_MUTEX(&slot->crit_sect);
193 slot->next = bridge->slots;
194 bridge->slots = slot;
196 bridge->nr_slots++;
198 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
199 slot->sun, pci_domain_nr(bridge->pci_bus),
200 bridge->pci_bus->number, slot->device);
203 newfunc->slot = slot;
204 list_add_tail(&newfunc->sibling, &slot->funcs);
206 /* associate corresponding pci_dev */
207 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
208 PCI_DEVFN(device, function));
209 if (newfunc->pci_dev) {
210 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
213 /* install notify handler */
214 status = acpi_install_notify_handler(handle,
215 ACPI_SYSTEM_NOTIFY,
216 handle_hotplug_event_func,
217 newfunc);
219 if (ACPI_FAILURE(status)) {
220 err("failed to register interrupt notify handler\n");
221 return status;
224 return AE_OK;
228 /* see if it's worth looking at this bridge */
229 static int detect_ejectable_slots(acpi_handle *bridge_handle)
231 acpi_status status;
232 int count;
234 count = 0;
236 /* only check slots defined directly below bridge object */
237 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
238 is_ejectable_slot, (void *)&count, NULL);
240 return count;
244 /* decode ACPI 2.0 _HPP hot plug parameters */
245 static void decode_hpp(struct acpiphp_bridge *bridge)
247 acpi_status status;
248 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
249 .pointer = NULL};
250 union acpi_object *package;
251 int i;
253 /* default numbers */
254 bridge->hpp.cache_line_size = 0x10;
255 bridge->hpp.latency_timer = 0x40;
256 bridge->hpp.enable_SERR = 0;
257 bridge->hpp.enable_PERR = 0;
259 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
261 if (ACPI_FAILURE(status)) {
262 dbg("_HPP evaluation failed\n");
263 return;
266 package = (union acpi_object *) buffer.pointer;
268 if (!package || package->type != ACPI_TYPE_PACKAGE ||
269 package->package.count != 4 || !package->package.elements) {
270 err("invalid _HPP object; ignoring\n");
271 goto err_exit;
274 for (i = 0; i < 4; i++) {
275 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
276 err("invalid _HPP parameter type; ignoring\n");
277 goto err_exit;
281 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
282 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
283 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
284 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
286 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
287 bridge->hpp.cache_line_size,
288 bridge->hpp.latency_timer,
289 bridge->hpp.enable_SERR,
290 bridge->hpp.enable_PERR);
292 bridge->flags |= BRIDGE_HAS_HPP;
294 err_exit:
295 kfree(buffer.pointer);
299 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
300 static void init_bridge_misc(struct acpiphp_bridge *bridge)
302 acpi_status status;
304 /* decode ACPI 2.0 _HPP (hot plug parameters) */
305 decode_hpp(bridge);
307 /* register all slot objects under this bridge */
308 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
309 register_slot, bridge, NULL);
311 /* install notify handler */
312 if (bridge->type != BRIDGE_TYPE_HOST) {
313 status = acpi_install_notify_handler(bridge->handle,
314 ACPI_SYSTEM_NOTIFY,
315 handle_hotplug_event_bridge,
316 bridge);
318 if (ACPI_FAILURE(status)) {
319 err("failed to register interrupt notify handler\n");
323 list_add(&bridge->list, &bridge_list);
327 /* allocate and initialize host bridge data structure */
328 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
330 struct acpiphp_bridge *bridge;
332 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
333 if (bridge == NULL)
334 return;
336 memset(bridge, 0, sizeof(struct acpiphp_bridge));
338 bridge->type = BRIDGE_TYPE_HOST;
339 bridge->handle = handle;
341 bridge->pci_bus = pci_bus;
343 spin_lock_init(&bridge->res_lock);
345 init_bridge_misc(bridge);
349 /* allocate and initialize PCI-to-PCI bridge data structure */
350 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
352 struct acpiphp_bridge *bridge;
354 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
355 if (bridge == NULL) {
356 err("out of memory\n");
357 return;
360 memset(bridge, 0, sizeof(struct acpiphp_bridge));
362 bridge->type = BRIDGE_TYPE_P2P;
363 bridge->handle = handle;
365 bridge->pci_dev = pci_dev_get(pci_dev);
366 bridge->pci_bus = pci_dev->subordinate;
367 if (!bridge->pci_bus) {
368 err("This is not a PCI-to-PCI bridge!\n");
369 goto err;
372 spin_lock_init(&bridge->res_lock);
374 init_bridge_misc(bridge);
375 return;
376 err:
377 pci_dev_put(pci_dev);
378 kfree(bridge);
379 return;
383 /* callback routine to find P2P bridges */
384 static acpi_status
385 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
387 acpi_status status;
388 acpi_handle dummy_handle;
389 unsigned long tmp;
390 int device, function;
391 struct pci_dev *dev;
392 struct pci_bus *pci_bus = context;
394 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
395 if (ACPI_FAILURE(status))
396 return AE_OK; /* continue */
398 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
399 if (ACPI_FAILURE(status)) {
400 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
401 return AE_OK;
404 device = (tmp >> 16) & 0xffff;
405 function = tmp & 0xffff;
407 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
409 if (!dev || !dev->subordinate)
410 goto out;
412 /* check if this bridge has ejectable slots */
413 if (detect_ejectable_slots(handle) > 0) {
414 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
415 add_p2p_bridge(handle, dev);
418 out:
419 pci_dev_put(dev);
420 return AE_OK;
424 /* find hot-pluggable slots, and then find P2P bridge */
425 static int add_bridge(acpi_handle handle)
427 acpi_status status;
428 unsigned long tmp;
429 int seg, bus;
430 acpi_handle dummy_handle;
431 struct pci_bus *pci_bus;
433 /* if the bridge doesn't have _STA, we assume it is always there */
434 status = acpi_get_handle(handle, "_STA", &dummy_handle);
435 if (ACPI_SUCCESS(status)) {
436 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
437 if (ACPI_FAILURE(status)) {
438 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
439 return 0;
441 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
442 /* don't register this object */
443 return 0;
446 /* get PCI segment number */
447 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
449 seg = ACPI_SUCCESS(status) ? tmp : 0;
451 /* get PCI bus number */
452 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
454 if (ACPI_SUCCESS(status)) {
455 bus = tmp;
456 } else {
457 warn("can't get bus number, assuming 0\n");
458 bus = 0;
461 pci_bus = pci_find_bus(seg, bus);
462 if (!pci_bus) {
463 err("Can't find bus %04x:%02x\n", seg, bus);
464 return 0;
467 /* check if this bridge has ejectable slots */
468 if (detect_ejectable_slots(handle) > 0) {
469 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
470 add_host_bridge(handle, pci_bus);
471 return 0;
474 /* search P2P bridges under this host bridge */
475 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
476 find_p2p_bridge, pci_bus, NULL);
478 if (ACPI_FAILURE(status))
479 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
481 return 0;
484 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
486 struct list_head *head;
487 list_for_each(head, &bridge_list) {
488 struct acpiphp_bridge *bridge = list_entry(head,
489 struct acpiphp_bridge, list);
490 if (bridge->handle == handle)
491 return bridge;
494 return NULL;
497 static void cleanup_bridge(struct acpiphp_bridge *bridge)
499 struct list_head *list, *tmp;
500 struct acpiphp_slot *slot;
501 acpi_status status;
502 acpi_handle handle = bridge->handle;
504 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
505 handle_hotplug_event_bridge);
506 if (ACPI_FAILURE(status))
507 err("failed to remove notify handler\n");
509 slot = bridge->slots;
510 while (slot) {
511 struct acpiphp_slot *next = slot->next;
512 list_for_each_safe (list, tmp, &slot->funcs) {
513 struct acpiphp_func *func;
514 func = list_entry(list, struct acpiphp_func, sibling);
515 status = acpi_remove_notify_handler(func->handle,
516 ACPI_SYSTEM_NOTIFY,
517 handle_hotplug_event_func);
518 if (ACPI_FAILURE(status))
519 err("failed to remove notify handler\n");
520 pci_dev_put(func->pci_dev);
521 list_del(list);
522 kfree(func);
524 kfree(slot);
525 slot = next;
528 pci_dev_put(bridge->pci_dev);
529 list_del(&bridge->list);
530 kfree(bridge);
533 static acpi_status
534 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
536 struct acpiphp_bridge *bridge;
538 if (!(bridge = acpiphp_handle_to_bridge(handle)))
539 return AE_OK;
540 cleanup_bridge(bridge);
541 return AE_OK;
544 static void remove_bridge(acpi_handle handle)
546 struct acpiphp_bridge *bridge;
548 bridge = acpiphp_handle_to_bridge(handle);
549 if (bridge) {
550 cleanup_bridge(bridge);
551 } else {
552 /* clean-up p2p bridges under this host bridge */
553 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
554 (u32)1, cleanup_p2p_bridge, NULL, NULL);
558 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
560 struct acpi_pci_id id;
561 struct pci_bus *bus;
562 struct pci_dev *dev;
564 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
565 return NULL;
567 bus = pci_find_bus(id.segment, id.bus);
568 if (!bus)
569 return NULL;
571 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
572 if (!dev)
573 return NULL;
575 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
576 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
578 pci_dev_put(dev);
579 return NULL;
582 return dev;
585 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
587 acpi_status status;
588 int result = -1;
589 unsigned long gsb;
590 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
591 union acpi_object *obj;
592 void *table;
594 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
595 if (ACPI_SUCCESS(status)) {
596 *gsi_base = (u32)gsb;
597 return 0;
600 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
601 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
602 return -1;
604 obj = buffer.pointer;
605 if (obj->type != ACPI_TYPE_BUFFER)
606 goto out;
608 table = obj->buffer.pointer;
609 switch (((acpi_table_entry_header *)table)->type) {
610 case ACPI_MADT_IOSAPIC:
611 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
612 result = 0;
613 break;
614 case ACPI_MADT_IOAPIC:
615 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
616 result = 0;
617 break;
618 default:
619 break;
621 out:
622 acpi_os_free(buffer.pointer);
623 return result;
626 static acpi_status
627 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
629 acpi_status status;
630 unsigned long sta;
631 acpi_handle tmp;
632 struct pci_dev *pdev;
633 u32 gsi_base;
634 u64 phys_addr;
636 /* Evaluate _STA if present */
637 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
638 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
639 return AE_CTRL_DEPTH;
641 /* Scan only PCI bus scope */
642 status = acpi_get_handle(handle, "_HID", &tmp);
643 if (ACPI_SUCCESS(status))
644 return AE_CTRL_DEPTH;
646 if (get_gsi_base(handle, &gsi_base))
647 return AE_OK;
649 pdev = get_apic_pci_info(handle);
650 if (!pdev)
651 return AE_OK;
653 if (pci_enable_device(pdev)) {
654 pci_dev_put(pdev);
655 return AE_OK;
658 pci_set_master(pdev);
660 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
661 pci_disable_device(pdev);
662 pci_dev_put(pdev);
663 return AE_OK;
666 phys_addr = pci_resource_start(pdev, 0);
667 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
668 pci_release_region(pdev, 0);
669 pci_disable_device(pdev);
670 pci_dev_put(pdev);
671 return AE_OK;
674 return AE_OK;
677 static int acpiphp_configure_ioapics(acpi_handle handle)
679 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
680 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
681 return 0;
684 static int power_on_slot(struct acpiphp_slot *slot)
686 acpi_status status;
687 struct acpiphp_func *func;
688 struct list_head *l;
689 int retval = 0;
691 /* if already enabled, just skip */
692 if (slot->flags & SLOT_POWEREDON)
693 goto err_exit;
695 list_for_each (l, &slot->funcs) {
696 func = list_entry(l, struct acpiphp_func, sibling);
698 if (func->flags & FUNC_HAS_PS0) {
699 dbg("%s: executing _PS0\n", __FUNCTION__);
700 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
701 if (ACPI_FAILURE(status)) {
702 warn("%s: _PS0 failed\n", __FUNCTION__);
703 retval = -1;
704 goto err_exit;
705 } else
706 break;
710 /* TBD: evaluate _STA to check if the slot is enabled */
712 slot->flags |= SLOT_POWEREDON;
714 err_exit:
715 return retval;
719 static int power_off_slot(struct acpiphp_slot *slot)
721 acpi_status status;
722 struct acpiphp_func *func;
723 struct list_head *l;
725 int retval = 0;
727 /* if already disabled, just skip */
728 if ((slot->flags & SLOT_POWEREDON) == 0)
729 goto err_exit;
731 list_for_each (l, &slot->funcs) {
732 func = list_entry(l, struct acpiphp_func, sibling);
734 if (func->flags & FUNC_HAS_PS3) {
735 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
736 if (ACPI_FAILURE(status)) {
737 warn("%s: _PS3 failed\n", __FUNCTION__);
738 retval = -1;
739 goto err_exit;
740 } else
741 break;
745 /* TBD: evaluate _STA to check if the slot is disabled */
747 slot->flags &= (~SLOT_POWEREDON);
749 err_exit:
750 return retval;
755 * enable_device - enable, configure a slot
756 * @slot: slot to be enabled
758 * This function should be called per *physical slot*,
759 * not per each slot object in ACPI namespace.
762 static int enable_device(struct acpiphp_slot *slot)
764 struct pci_dev *dev;
765 struct pci_bus *bus = slot->bridge->pci_bus;
766 struct list_head *l;
767 struct acpiphp_func *func;
768 int retval = 0;
769 int num, max, pass;
771 if (slot->flags & SLOT_ENABLED)
772 goto err_exit;
774 /* sanity check: dev should be NULL when hot-plugged in */
775 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
776 if (dev) {
777 /* This case shouldn't happen */
778 err("pci_dev structure already exists.\n");
779 pci_dev_put(dev);
780 retval = -1;
781 goto err_exit;
784 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
785 if (num == 0) {
786 err("No new device found\n");
787 retval = -1;
788 goto err_exit;
791 max = bus->secondary;
792 for (pass = 0; pass < 2; pass++) {
793 list_for_each_entry(dev, &bus->devices, bus_list) {
794 if (PCI_SLOT(dev->devfn) != slot->device)
795 continue;
796 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
797 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
798 max = pci_scan_bridge(bus, dev, max, pass);
799 if (pass && dev->subordinate)
800 pci_bus_size_bridges(dev->subordinate);
805 pci_bus_assign_resources(bus);
806 acpiphp_sanitize_bus(bus);
807 pci_enable_bridges(bus);
808 pci_bus_add_devices(bus);
809 acpiphp_set_hpp_values(DEVICE_ACPI_HANDLE(&bus->self->dev), bus);
810 acpiphp_configure_ioapics(DEVICE_ACPI_HANDLE(&bus->self->dev));
812 /* associate pci_dev to our representation */
813 list_for_each (l, &slot->funcs) {
814 func = list_entry(l, struct acpiphp_func, sibling);
815 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
816 func->function));
819 slot->flags |= SLOT_ENABLED;
821 err_exit:
822 return retval;
827 * disable_device - disable a slot
829 static int disable_device(struct acpiphp_slot *slot)
831 int retval = 0;
832 struct acpiphp_func *func;
833 struct list_head *l;
835 /* is this slot already disabled? */
836 if (!(slot->flags & SLOT_ENABLED))
837 goto err_exit;
839 list_for_each (l, &slot->funcs) {
840 func = list_entry(l, struct acpiphp_func, sibling);
841 if (!func->pci_dev)
842 continue;
844 pci_remove_bus_device(func->pci_dev);
845 pci_dev_put(func->pci_dev);
846 func->pci_dev = NULL;
849 slot->flags &= (~SLOT_ENABLED);
851 err_exit:
852 return retval;
857 * get_slot_status - get ACPI slot status
859 * if a slot has _STA for each function and if any one of them
860 * returned non-zero status, return it
862 * if a slot doesn't have _STA and if any one of its functions'
863 * configuration space is configured, return 0x0f as a _STA
865 * otherwise return 0
867 static unsigned int get_slot_status(struct acpiphp_slot *slot)
869 acpi_status status;
870 unsigned long sta = 0;
871 u32 dvid;
872 struct list_head *l;
873 struct acpiphp_func *func;
875 list_for_each (l, &slot->funcs) {
876 func = list_entry(l, struct acpiphp_func, sibling);
878 if (func->flags & FUNC_HAS_STA) {
879 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
880 if (ACPI_SUCCESS(status) && sta)
881 break;
882 } else {
883 pci_bus_read_config_dword(slot->bridge->pci_bus,
884 PCI_DEVFN(slot->device,
885 func->function),
886 PCI_VENDOR_ID, &dvid);
887 if (dvid != 0xffffffff) {
888 sta = ACPI_STA_ALL;
889 break;
894 return (unsigned int)sta;
898 * acpiphp_eject_slot - physically eject the slot
900 static int acpiphp_eject_slot(struct acpiphp_slot *slot)
902 acpi_status status;
903 struct acpiphp_func *func;
904 struct list_head *l;
905 struct acpi_object_list arg_list;
906 union acpi_object arg;
908 list_for_each (l, &slot->funcs) {
909 func = list_entry(l, struct acpiphp_func, sibling);
911 /* We don't want to call _EJ0 on non-existing functions. */
912 if ((func->flags & FUNC_HAS_EJ0)) {
913 /* _EJ0 method take one argument */
914 arg_list.count = 1;
915 arg_list.pointer = &arg;
916 arg.type = ACPI_TYPE_INTEGER;
917 arg.integer.value = 1;
919 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
920 if (ACPI_FAILURE(status)) {
921 warn("%s: _EJ0 failed\n", __FUNCTION__);
922 return -1;
923 } else
924 break;
927 return 0;
931 * acpiphp_check_bridge - re-enumerate devices
933 * Iterate over all slots under this bridge and make sure that if a
934 * card is present they are enabled, and if not they are disabled.
936 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
938 struct acpiphp_slot *slot;
939 int retval = 0;
940 int enabled, disabled;
942 enabled = disabled = 0;
944 for (slot = bridge->slots; slot; slot = slot->next) {
945 unsigned int status = get_slot_status(slot);
946 if (slot->flags & SLOT_ENABLED) {
947 if (status == ACPI_STA_ALL)
948 continue;
949 retval = acpiphp_disable_slot(slot);
950 if (retval) {
951 err("Error occurred in disabling\n");
952 goto err_exit;
953 } else {
954 acpiphp_eject_slot(slot);
956 disabled++;
957 } else {
958 if (status != ACPI_STA_ALL)
959 continue;
960 retval = acpiphp_enable_slot(slot);
961 if (retval) {
962 err("Error occurred in enabling\n");
963 goto err_exit;
965 enabled++;
969 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
971 err_exit:
972 return retval;
975 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
977 u16 pci_cmd, pci_bctl;
978 struct pci_dev *cdev;
980 /* Program hpp values for this device */
981 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
982 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
983 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
984 return;
985 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
986 bridge->hpp.cache_line_size);
987 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
988 bridge->hpp.latency_timer);
989 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
990 if (bridge->hpp.enable_SERR)
991 pci_cmd |= PCI_COMMAND_SERR;
992 else
993 pci_cmd &= ~PCI_COMMAND_SERR;
994 if (bridge->hpp.enable_PERR)
995 pci_cmd |= PCI_COMMAND_PARITY;
996 else
997 pci_cmd &= ~PCI_COMMAND_PARITY;
998 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1000 /* Program bridge control value and child devices */
1001 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1002 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1003 bridge->hpp.latency_timer);
1004 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1005 if (bridge->hpp.enable_SERR)
1006 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1007 else
1008 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1009 if (bridge->hpp.enable_PERR)
1010 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1011 else
1012 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1013 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1014 if (dev->subordinate) {
1015 list_for_each_entry(cdev, &dev->subordinate->devices,
1016 bus_list)
1017 program_hpp(cdev, bridge);
1022 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1024 struct acpiphp_bridge bridge;
1025 struct pci_dev *dev;
1027 memset(&bridge, 0, sizeof(bridge));
1028 bridge.handle = handle;
1029 decode_hpp(&bridge);
1030 list_for_each_entry(dev, &bus->devices, bus_list)
1031 program_hpp(dev, &bridge);
1036 * Remove devices for which we could not assign resources, call
1037 * arch specific code to fix-up the bus
1039 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1041 struct pci_dev *dev;
1042 int i;
1043 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1045 list_for_each_entry(dev, &bus->devices, bus_list) {
1046 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1047 struct resource *res = &dev->resource[i];
1048 if ((res->flags & type_mask) && !res->start &&
1049 res->end) {
1050 /* Could not assign a required resources
1051 * for this device, remove it */
1052 pci_remove_bus_device(dev);
1053 break;
1059 /* Program resources in newly inserted bridge */
1060 static int acpiphp_configure_bridge (acpi_handle handle)
1062 struct acpi_pci_id pci_id;
1063 struct pci_bus *bus;
1065 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1066 err("cannot get PCI domain and bus number for bridge\n");
1067 return -EINVAL;
1069 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1070 if (!bus) {
1071 err("cannot find bus %d:%d\n",
1072 pci_id.segment, pci_id.bus);
1073 return -EINVAL;
1076 pci_bus_size_bridges(bus);
1077 pci_bus_assign_resources(bus);
1078 acpiphp_sanitize_bus(bus);
1079 acpiphp_set_hpp_values(handle, bus);
1080 pci_enable_bridges(bus);
1081 acpiphp_configure_ioapics(handle);
1082 return 0;
1085 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1087 struct acpi_device *device, *pdevice;
1088 acpi_handle phandle;
1090 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1091 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1092 err("unexpected notification type %d\n", type);
1093 return;
1096 acpi_get_parent(handle, &phandle);
1097 if (acpi_bus_get_device(phandle, &pdevice)) {
1098 dbg("no parent device, assuming NULL\n");
1099 pdevice = NULL;
1101 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1102 err("cannot add bridge to acpi list\n");
1103 return;
1105 if (!acpiphp_configure_bridge(handle) &&
1106 !acpi_bus_start(device))
1107 add_bridge(handle);
1108 else
1109 err("cannot configure and start bridge\n");
1114 * ACPI event handlers
1118 * handle_hotplug_event_bridge - handle ACPI event on bridges
1120 * @handle: Notify()'ed acpi_handle
1121 * @type: Notify code
1122 * @context: pointer to acpiphp_bridge structure
1124 * handles ACPI event notification on {host,p2p} bridges
1127 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1129 struct acpiphp_bridge *bridge;
1130 char objname[64];
1131 struct acpi_buffer buffer = { .length = sizeof(objname),
1132 .pointer = objname };
1133 struct acpi_device *device;
1135 if (acpi_bus_get_device(handle, &device)) {
1136 /* This bridge must have just been physically inserted */
1137 handle_bridge_insertion(handle, type);
1138 return;
1141 bridge = acpiphp_handle_to_bridge(handle);
1142 if (!bridge) {
1143 err("cannot get bridge info\n");
1144 return;
1147 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1149 switch (type) {
1150 case ACPI_NOTIFY_BUS_CHECK:
1151 /* bus re-enumerate */
1152 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1153 acpiphp_check_bridge(bridge);
1154 break;
1156 case ACPI_NOTIFY_DEVICE_CHECK:
1157 /* device check */
1158 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1159 acpiphp_check_bridge(bridge);
1160 break;
1162 case ACPI_NOTIFY_DEVICE_WAKE:
1163 /* wake event */
1164 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1165 break;
1167 case ACPI_NOTIFY_EJECT_REQUEST:
1168 /* request device eject */
1169 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1170 break;
1172 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1173 printk(KERN_ERR "Device %s cannot be configured due"
1174 " to a frequency mismatch\n", objname);
1175 break;
1177 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1178 printk(KERN_ERR "Device %s cannot be configured due"
1179 " to a bus mode mismatch\n", objname);
1180 break;
1182 case ACPI_NOTIFY_POWER_FAULT:
1183 printk(KERN_ERR "Device %s has suffered a power fault\n",
1184 objname);
1185 break;
1187 default:
1188 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1189 break;
1194 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1196 * @handle: Notify()'ed acpi_handle
1197 * @type: Notify code
1198 * @context: pointer to acpiphp_func structure
1200 * handles ACPI event notification on slots
1203 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1205 struct acpiphp_func *func;
1206 char objname[64];
1207 struct acpi_buffer buffer = { .length = sizeof(objname),
1208 .pointer = objname };
1210 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1212 func = (struct acpiphp_func *)context;
1214 switch (type) {
1215 case ACPI_NOTIFY_BUS_CHECK:
1216 /* bus re-enumerate */
1217 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1218 acpiphp_enable_slot(func->slot);
1219 break;
1221 case ACPI_NOTIFY_DEVICE_CHECK:
1222 /* device check : re-enumerate from parent bus */
1223 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1224 acpiphp_check_bridge(func->slot->bridge);
1225 break;
1227 case ACPI_NOTIFY_DEVICE_WAKE:
1228 /* wake event */
1229 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1230 break;
1232 case ACPI_NOTIFY_EJECT_REQUEST:
1233 /* request device eject */
1234 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1235 if (!(acpiphp_disable_slot(func->slot)))
1236 acpiphp_eject_slot(func->slot);
1237 break;
1239 default:
1240 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1241 break;
1245 static int is_root_bridge(acpi_handle handle)
1247 acpi_status status;
1248 struct acpi_device_info *info;
1249 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1250 int i;
1252 status = acpi_get_object_info(handle, &buffer);
1253 if (ACPI_SUCCESS(status)) {
1254 info = buffer.pointer;
1255 if ((info->valid & ACPI_VALID_HID) &&
1256 !strcmp(PCI_ROOT_HID_STRING,
1257 info->hardware_id.value)) {
1258 acpi_os_free(buffer.pointer);
1259 return 1;
1261 if (info->valid & ACPI_VALID_CID) {
1262 for (i=0; i < info->compatibility_id.count; i++) {
1263 if (!strcmp(PCI_ROOT_HID_STRING,
1264 info->compatibility_id.id[i].value)) {
1265 acpi_os_free(buffer.pointer);
1266 return 1;
1271 return 0;
1274 static acpi_status
1275 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1277 int *count = (int *)context;
1279 if (is_root_bridge(handle)) {
1280 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1281 handle_hotplug_event_bridge, NULL);
1282 (*count)++;
1284 return AE_OK ;
1287 static struct acpi_pci_driver acpi_pci_hp_driver = {
1288 .add = add_bridge,
1289 .remove = remove_bridge,
1293 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1296 int __init acpiphp_glue_init(void)
1298 int num = 0;
1300 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1301 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1303 if (num <= 0)
1304 return -1;
1305 else
1306 acpi_pci_register_driver(&acpi_pci_hp_driver);
1308 return 0;
1313 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1315 * This function frees all data allocated in acpiphp_glue_init()
1317 void __exit acpiphp_glue_exit(void)
1319 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1324 * acpiphp_get_num_slots - count number of slots in a system
1326 int __init acpiphp_get_num_slots(void)
1328 struct list_head *node;
1329 struct acpiphp_bridge *bridge;
1330 int num_slots;
1332 num_slots = 0;
1334 list_for_each (node, &bridge_list) {
1335 bridge = (struct acpiphp_bridge *)node;
1336 dbg("Bus %04x:%02x has %d slot%s\n",
1337 pci_domain_nr(bridge->pci_bus),
1338 bridge->pci_bus->number, bridge->nr_slots,
1339 bridge->nr_slots == 1 ? "" : "s");
1340 num_slots += bridge->nr_slots;
1343 dbg("Total %d slots\n", num_slots);
1344 return num_slots;
1348 #if 0
1350 * acpiphp_for_each_slot - call function for each slot
1351 * @fn: callback function
1352 * @data: context to be passed to callback function
1355 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1357 struct list_head *node;
1358 struct acpiphp_bridge *bridge;
1359 struct acpiphp_slot *slot;
1360 int retval = 0;
1362 list_for_each (node, &bridge_list) {
1363 bridge = (struct acpiphp_bridge *)node;
1364 for (slot = bridge->slots; slot; slot = slot->next) {
1365 retval = fn(slot, data);
1366 if (!retval)
1367 goto err_exit;
1371 err_exit:
1372 return retval;
1374 #endif
1376 /* search matching slot from id */
1377 struct acpiphp_slot *get_slot_from_id(int id)
1379 struct list_head *node;
1380 struct acpiphp_bridge *bridge;
1381 struct acpiphp_slot *slot;
1383 list_for_each (node, &bridge_list) {
1384 bridge = (struct acpiphp_bridge *)node;
1385 for (slot = bridge->slots; slot; slot = slot->next)
1386 if (slot->id == id)
1387 return slot;
1390 /* should never happen! */
1391 err("%s: no object for id %d\n", __FUNCTION__, id);
1392 WARN_ON(1);
1393 return NULL;
1398 * acpiphp_enable_slot - power on slot
1400 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1402 int retval;
1404 down(&slot->crit_sect);
1406 /* wake up all functions */
1407 retval = power_on_slot(slot);
1408 if (retval)
1409 goto err_exit;
1411 if (get_slot_status(slot) == ACPI_STA_ALL)
1412 /* configure all functions */
1413 retval = enable_device(slot);
1415 err_exit:
1416 up(&slot->crit_sect);
1417 return retval;
1421 * acpiphp_disable_slot - power off slot
1423 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1425 int retval = 0;
1427 down(&slot->crit_sect);
1429 /* unconfigure all functions */
1430 retval = disable_device(slot);
1431 if (retval)
1432 goto err_exit;
1434 /* power off all functions */
1435 retval = power_off_slot(slot);
1436 if (retval)
1437 goto err_exit;
1439 err_exit:
1440 up(&slot->crit_sect);
1441 return retval;
1446 * slot enabled: 1
1447 * slot disabled: 0
1449 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1451 return (slot->flags & SLOT_POWEREDON);
1456 * latch closed: 1
1457 * latch open: 0
1459 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1461 unsigned int sta;
1463 sta = get_slot_status(slot);
1465 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1470 * adapter presence : 1
1471 * absence : 0
1473 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1475 unsigned int sta;
1477 sta = get_slot_status(slot);
1479 return (sta == 0) ? 0 : 1;
1484 * pci address (seg/bus/dev)
1486 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1488 u32 address;
1489 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1491 address = (pci_domain_nr(pci_bus) << 16) |
1492 (pci_bus->number << 8) |
1493 slot->device;
1495 return address;