blob: 955aae4071f7a64eeb5cb716aaacf8ebb26c7a11 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
3 *
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
Rajesh Shah42f49a62005-04-28 00:25:53 -07007 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
Rajesh Shah8e7561c2005-04-28 00:25:56 -07009 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * All rights reserved.
13 *
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.
18 *
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.
24 *
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.
28 *
Kristen Carlson Accardi998be202006-07-26 10:52:33 -070029 * Send feedback to <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 */
32
Rajesh Shah42f49a62005-04-28 00:25:53 -070033/*
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 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/init.h>
44#include <linux/module.h>
45
46#include <linux/kernel.h>
47#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070048#include <linux/pci_hotplug.h>
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +010049#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "../pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "acpiphp.h"
53
54static LIST_HEAD(bridge_list);
Satoru Takeuchi600812e2006-09-12 10:22:53 -070055static LIST_HEAD(ioapic_list);
56static DEFINE_SPINLOCK(ioapic_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define MY_NAME "acpiphp_glue"
59
60static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070061static void acpiphp_sanitize_bus(struct pci_bus *bus);
62static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
Len Brown2b85e132006-06-27 01:50:14 -040063static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * initialization & terminatation routines
68 */
69
70/**
71 * is_ejectable - determine if a slot is ejectable
72 * @handle: handle to acpi namespace
73 *
74 * Ejectable slot should satisfy at least these conditions:
75 *
76 * 1. has _ADR method
77 * 2. has _EJ0 method
78 *
79 * optionally
80 *
81 * 1. has _STA method
82 * 2. has _PS0 method
83 * 3. has _PS3 method
84 * 4. ..
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86static int is_ejectable(acpi_handle handle)
87{
88 acpi_status status;
89 acpi_handle tmp;
90
91 status = acpi_get_handle(handle, "_ADR", &tmp);
92 if (ACPI_FAILURE(status)) {
93 return 0;
94 }
95
96 status = acpi_get_handle(handle, "_EJ0", &tmp);
97 if (ACPI_FAILURE(status)) {
98 return 0;
99 }
100
101 return 1;
102}
103
104
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +0900105/* callback routine to check for the existence of ejectable slots */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static acpi_status
107is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
108{
109 int *count = (int *)context;
110
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;
117 }
118}
119
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +0900120/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400121static acpi_status
122is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
123{
124 int *count = (int *)context;
125
126 if (is_dock_device(handle)) {
127 (*count)++;
128 return AE_CTRL_TERMINATE;
129 } else {
130 return AE_OK;
131 }
132}
133
134
135
136
137/*
138 * the _DCK method can do funny things... and sometimes not
139 * hah-hah funny.
140 *
141 * TBD - figure out a way to only call fixups for
142 * systems that require them.
143 */
144static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
145 void *v)
146{
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;
150
151 if (!bus->self)
152 return NOTIFY_OK;
153
154 /* fixup bad _DCK function that rewrites
155 * secondary bridge on slot
156 */
157 pci_read_config_dword(bus->self,
158 PCI_PRIMARY_BUS,
159 &buses);
160
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);
167 }
168 return NOTIFY_OK;
169}
170
171
Shaohua Li1253f7a2008-08-28 10:06:16 +0800172static struct acpi_dock_ops acpiphp_dock_ops = {
173 .handler = handle_hotplug_event_func,
174};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/* callback routine to register each ACPI PCI slot object */
177static acpi_status
178register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
179{
180 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
181 struct acpiphp_slot *slot;
182 struct acpiphp_func *newfunc;
183 acpi_handle tmp;
184 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400185 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800186 int device, function, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
189
190 if (ACPI_FAILURE(status))
191 return AE_OK;
192
193 status = acpi_get_handle(handle, "_EJ0", &tmp);
194
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400195 if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return AE_OK;
197
198 device = (adr >> 16) & 0xffff;
199 function = adr & 0xffff;
200
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100201 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!newfunc)
203 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 INIT_LIST_HEAD(&newfunc->sibling);
206 newfunc->handle = handle;
207 newfunc->function = function;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800208 if (ACPI_SUCCESS(status))
209 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
212 newfunc->flags |= FUNC_HAS_STA;
213
214 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
215 newfunc->flags |= FUNC_HAS_PS0;
216
217 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
218 newfunc->flags |= FUNC_HAS_PS3;
219
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400220 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800221 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400224 if (ACPI_FAILURE(status)) {
225 /*
226 * use the count of the number of slots we've found
227 * for the number of the slot
228 */
229 sun = bridge->nr_slots+1;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* search for objects that share the same slot */
233 for (slot = bridge->slots; slot; slot = slot->next)
234 if (slot->device == device) {
235 if (slot->sun != sun)
236 warn("sibling found, but _SUN doesn't match!\n");
237 break;
238 }
239
240 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100241 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!slot) {
243 kfree(newfunc);
244 return AE_NO_MEMORY;
245 }
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 slot->device = device;
249 slot->sun = sun;
250 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100251 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 slot->next = bridge->slots;
254 bridge->slots = slot;
255
256 bridge->nr_slots++;
257
Rajesh Shah42f49a62005-04-28 00:25:53 -0700258 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
259 slot->sun, pci_domain_nr(bridge->pci_bus),
260 bridge->pci_bus->number, slot->device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800261 retval = acpiphp_register_hotplug_slot(slot);
262 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600263 if (retval == -EBUSY)
264 warn("Slot %d already registered by another "
265 "hotplug driver\n", slot->sun);
266 else
267 warn("acpiphp_register_hotplug_slot failed "
268 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800269 goto err_exit;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 newfunc->slot = slot;
274 list_add_tail(&newfunc->sibling, &slot->funcs);
275
276 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700277 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 PCI_DEVFN(device, function));
279 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
281 }
282
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400283 if (is_dock_device(handle)) {
284 /* we don't want to call this device's _EJ0
285 * because we want the dock notify handler
286 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800287 */
288 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400289 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800290 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400291 dbg("failed to register dock device\n");
292
293 /* we need to be notified when dock events happen
294 * outside of the hotplug operation, since we may
295 * need to do fixups before we can hotplug.
296 */
297 newfunc->nb.notifier_call = post_dock_fixups;
298 if (register_dock_notifier(&newfunc->nb))
299 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800300 }
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800303 if (!(newfunc->flags & FUNC_HAS_DCK)) {
304 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 ACPI_SYSTEM_NOTIFY,
306 handle_hotplug_event_func,
307 newfunc);
308
Kristen Accardi20416ea2006-02-23 17:56:03 -0800309 if (ACPI_FAILURE(status))
310 err("failed to register interrupt notify handler\n");
311 } else
312 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Kristen Accardi20416ea2006-02-23 17:56:03 -0800314 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800315
316 err_exit:
317 bridge->nr_slots--;
318 bridge->slots = slot->next;
319 kfree(slot);
320 kfree(newfunc);
321
322 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325
326/* see if it's worth looking at this bridge */
327static int detect_ejectable_slots(acpi_handle *bridge_handle)
328{
329 acpi_status status;
330 int count;
331
332 count = 0;
333
334 /* only check slots defined directly below bridge object */
335 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
336 is_ejectable_slot, (void *)&count, NULL);
337
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400338 /*
339 * we also need to add this bridge if there is a dock bridge or
340 * other pci device on a dock station (removable)
341 */
342 if (!count)
343 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
344 (u32)1, is_pci_dock_device, (void *)&count,
345 NULL);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return count;
348}
349
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* decode ACPI 2.0 _HPP hot plug parameters */
352static void decode_hpp(struct acpiphp_bridge *bridge)
353{
354 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900356 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900357 if (ACPI_FAILURE(status) ||
358 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800359 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900360 printk(KERN_WARNING
361 "%s: Could not get hotplug parameters. Use defaults\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800362 __func__);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900363 bridge->hpp.t0 = &bridge->hpp.type0_data;
364 bridge->hpp.t0->revision = 0;
365 bridge->hpp.t0->cache_line_size = 0x10;
366 bridge->hpp.t0->latency_timer = 0x40;
367 bridge->hpp.t0->enable_serr = 0;
368 bridge->hpp.t0->enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372
Kristen Accardi783c49f2006-03-03 10:16:05 -0800373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
375static void init_bridge_misc(struct acpiphp_bridge *bridge)
376{
377 acpi_status status;
378
379 /* decode ACPI 2.0 _HPP (hot plug parameters) */
380 decode_hpp(bridge);
381
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800382 /* must be added to the list prior to calling register_slot */
383 list_add(&bridge->list, &bridge_list);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* register all slot objects under this bridge */
386 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
387 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800388 if (ACPI_FAILURE(status)) {
389 list_del(&bridge->list);
390 return;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700394 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900395 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
396 status = acpi_remove_notify_handler(bridge->func->handle,
397 ACPI_SYSTEM_NOTIFY,
398 handle_hotplug_event_func);
399 if (ACPI_FAILURE(status))
400 err("failed to remove notify handler\n");
401 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700402 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ACPI_SYSTEM_NOTIFY,
404 handle_hotplug_event_bridge,
405 bridge);
406
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700407 if (ACPI_FAILURE(status)) {
408 err("failed to register interrupt notify handler\n");
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900414/* find acpiphp_func from acpiphp_bridge */
415static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
416{
417 struct list_head *node, *l;
418 struct acpiphp_bridge *bridge;
419 struct acpiphp_slot *slot;
420 struct acpiphp_func *func;
421
422 list_for_each(node, &bridge_list) {
423 bridge = list_entry(node, struct acpiphp_bridge, list);
424 for (slot = bridge->slots; slot; slot = slot->next) {
425 list_for_each(l, &slot->funcs) {
426 func = list_entry(l, struct acpiphp_func,
427 sibling);
428 if (func->handle == handle)
429 return func;
430 }
431 }
432 }
433
434 return NULL;
435}
436
437
438static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
439{
440 acpi_handle dummy_handle;
441
442 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
443 "_STA", &dummy_handle)))
444 bridge->flags |= BRIDGE_HAS_STA;
445
446 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
447 "_EJ0", &dummy_handle)))
448 bridge->flags |= BRIDGE_HAS_EJ0;
449
450 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
451 "_PS0", &dummy_handle)))
452 bridge->flags |= BRIDGE_HAS_PS0;
453
454 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
455 "_PS3", &dummy_handle)))
456 bridge->flags |= BRIDGE_HAS_PS3;
457
458 /* is this ejectable p2p bridge? */
459 if (bridge->flags & BRIDGE_HAS_EJ0) {
460 struct acpiphp_func *func;
461
462 dbg("found ejectable p2p bridge\n");
463
464 /* make link between PCI bridge and PCI function */
465 func = acpiphp_bridge_handle_to_function(bridge->handle);
466 if (!func)
467 return;
468 bridge->func = func;
469 func->bridge = bridge;
470 }
471}
472
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700475static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct acpiphp_bridge *bridge;
478
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100479 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (bridge == NULL)
481 return;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 bridge->type = BRIDGE_TYPE_HOST;
484 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Rajesh Shah42f49a62005-04-28 00:25:53 -0700486 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 spin_lock_init(&bridge->res_lock);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 init_bridge_misc(bridge);
491}
492
493
494/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700495static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100499 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (bridge == NULL) {
501 err("out of memory\n");
502 return;
503 }
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 bridge->type = BRIDGE_TYPE_P2P;
506 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900507 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Rajesh Shah42f49a62005-04-28 00:25:53 -0700509 bridge->pci_dev = pci_dev_get(pci_dev);
510 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!bridge->pci_bus) {
512 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700513 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 spin_lock_init(&bridge->res_lock);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700519 return;
520 err:
521 pci_dev_put(pci_dev);
522 kfree(bridge);
523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526
527/* callback routine to find P2P bridges */
528static acpi_status
529find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
530{
531 acpi_status status;
532 acpi_handle dummy_handle;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400533 unsigned long long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700534 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700536 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
539 if (ACPI_FAILURE(status))
540 return AE_OK; /* continue */
541
542 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
543 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800544 dbg("%s: _ADR evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return AE_OK;
546 }
547
548 device = (tmp >> 16) & 0xffff;
549 function = tmp & 0xffff;
550
Rajesh Shah42f49a62005-04-28 00:25:53 -0700551 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Rajesh Shah42f49a62005-04-28 00:25:53 -0700553 if (!dev || !dev->subordinate)
554 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /* check if this bridge has ejectable slots */
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400557 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700559 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900562 /* search P2P bridges under this p2p bridge */
563 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
564 find_p2p_bridge, dev->subordinate, NULL);
565 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900566 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900567
Rajesh Shah42f49a62005-04-28 00:25:53 -0700568 out:
569 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return AE_OK;
571}
572
573
574/* find hot-pluggable slots, and then find P2P bridge */
575static int add_bridge(acpi_handle handle)
576{
577 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400578 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int seg, bus;
580 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700581 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* if the bridge doesn't have _STA, we assume it is always there */
584 status = acpi_get_handle(handle, "_STA", &dummy_handle);
585 if (ACPI_SUCCESS(status)) {
586 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
587 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800588 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590 }
591 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
592 /* don't register this object */
593 return 0;
594 }
595
596 /* get PCI segment number */
597 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
598
599 seg = ACPI_SUCCESS(status) ? tmp : 0;
600
601 /* get PCI bus number */
602 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
603
604 if (ACPI_SUCCESS(status)) {
605 bus = tmp;
606 } else {
607 warn("can't get bus number, assuming 0\n");
608 bus = 0;
609 }
610
Rajesh Shah42f49a62005-04-28 00:25:53 -0700611 pci_bus = pci_find_bus(seg, bus);
612 if (!pci_bus) {
613 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 0;
615 }
616
Rajesh Shah42f49a62005-04-28 00:25:53 -0700617 /* check if this bridge has ejectable slots */
618 if (detect_ejectable_slots(handle) > 0) {
619 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
620 add_host_bridge(handle, pci_bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* search P2P bridges under this host bridge */
624 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700625 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900628 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 return 0;
631}
632
Rajesh Shah42f49a62005-04-28 00:25:53 -0700633static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
634{
635 struct list_head *head;
636 list_for_each(head, &bridge_list) {
637 struct acpiphp_bridge *bridge = list_entry(head,
638 struct acpiphp_bridge, list);
639 if (bridge->handle == handle)
640 return bridge;
641 }
642
643 return NULL;
644}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Rajesh Shah364d5092005-04-28 00:25:54 -0700646static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700648 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700649 struct acpiphp_slot *slot;
650 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700651 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700652
653 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
654 handle_hotplug_event_bridge);
655 if (ACPI_FAILURE(status))
656 err("failed to remove notify handler\n");
657
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900658 if ((bridge->type != BRIDGE_TYPE_HOST) &&
659 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
660 status = acpi_install_notify_handler(bridge->func->handle,
661 ACPI_SYSTEM_NOTIFY,
662 handle_hotplug_event_func,
663 bridge->func);
664 if (ACPI_FAILURE(status))
665 err("failed to install interrupt notify handler\n");
666 }
667
Rajesh Shah42f49a62005-04-28 00:25:53 -0700668 slot = bridge->slots;
669 while (slot) {
670 struct acpiphp_slot *next = slot->next;
671 list_for_each_safe (list, tmp, &slot->funcs) {
672 struct acpiphp_func *func;
673 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400674 if (is_dock_device(func->handle)) {
675 unregister_hotplug_dock_device(func->handle);
676 unregister_dock_notifier(&func->nb);
677 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800678 if (!(func->flags & FUNC_HAS_DCK)) {
679 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700680 ACPI_SYSTEM_NOTIFY,
681 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800682 if (ACPI_FAILURE(status))
683 err("failed to remove notify handler\n");
684 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700685 pci_dev_put(func->pci_dev);
686 list_del(list);
687 kfree(func);
688 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800689 acpiphp_unregister_hotplug_slot(slot);
690 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700691 kfree(slot);
692 slot = next;
693 }
694
695 pci_dev_put(bridge->pci_dev);
696 list_del(&bridge->list);
697 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Rajesh Shah364d5092005-04-28 00:25:54 -0700700static acpi_status
701cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
702{
703 struct acpiphp_bridge *bridge;
704
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900705 /* cleanup p2p bridges under this P2P bridge
706 in a depth-first manner */
707 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
708 cleanup_p2p_bridge, NULL, NULL);
709
Alex Chianga13307c2008-07-01 20:02:23 -0600710 bridge = acpiphp_handle_to_bridge(handle);
711 if (bridge)
712 cleanup_bridge(bridge);
713
Rajesh Shah364d5092005-04-28 00:25:54 -0700714 return AE_OK;
715}
716
717static void remove_bridge(acpi_handle handle)
718{
719 struct acpiphp_bridge *bridge;
720
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900721 /* cleanup p2p bridges under this host bridge
722 in a depth-first manner */
723 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
724 (u32)1, cleanup_p2p_bridge, NULL, NULL);
725
Alex Chianga13307c2008-07-01 20:02:23 -0600726 /*
727 * On root bridges with hotplug slots directly underneath (ie,
728 * no p2p bridge inbetween), we call cleanup_bridge().
729 *
730 * The else clause cleans up root bridges that either had no
731 * hotplug slots at all, or had a p2p bridge underneath.
732 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700733 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900734 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700735 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600736 else
737 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
738 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700739}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700741static struct pci_dev * get_apic_pci_info(acpi_handle handle)
742{
743 struct acpi_pci_id id;
744 struct pci_bus *bus;
745 struct pci_dev *dev;
746
747 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
748 return NULL;
749
750 bus = pci_find_bus(id.segment, id.bus);
751 if (!bus)
752 return NULL;
753
754 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
755 if (!dev)
756 return NULL;
757
758 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
759 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
760 {
761 pci_dev_put(dev);
762 return NULL;
763 }
764
765 return dev;
766}
767
768static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
769{
770 acpi_status status;
771 int result = -1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400772 unsigned long long gsb;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700773 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
774 union acpi_object *obj;
775 void *table;
776
777 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
778 if (ACPI_SUCCESS(status)) {
779 *gsi_base = (u32)gsb;
780 return 0;
781 }
782
783 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
784 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
785 return -1;
786
787 obj = buffer.pointer;
788 if (obj->type != ACPI_TYPE_BUFFER)
789 goto out;
790
791 table = obj->buffer.pointer;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300792 switch (((struct acpi_subtable_header *)table)->type) {
793 case ACPI_MADT_TYPE_IO_SAPIC:
794 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700795 result = 0;
796 break;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300797 case ACPI_MADT_TYPE_IO_APIC:
798 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700799 result = 0;
800 break;
801 default:
802 break;
803 }
804 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700805 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700806 return result;
807}
808
809static acpi_status
810ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
811{
812 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400813 unsigned long long sta;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700814 acpi_handle tmp;
815 struct pci_dev *pdev;
816 u32 gsi_base;
817 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700818 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700819
820 /* Evaluate _STA if present */
821 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
822 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
823 return AE_CTRL_DEPTH;
824
825 /* Scan only PCI bus scope */
826 status = acpi_get_handle(handle, "_HID", &tmp);
827 if (ACPI_SUCCESS(status))
828 return AE_CTRL_DEPTH;
829
830 if (get_gsi_base(handle, &gsi_base))
831 return AE_OK;
832
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700833 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
834 if (!ioapic)
835 return AE_NO_MEMORY;
836
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700837 pdev = get_apic_pci_info(handle);
838 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700839 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700840
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700841 if (pci_enable_device(pdev))
842 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700843
844 pci_set_master(pdev);
845
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700846 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
847 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700848
849 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700850 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
851 goto exit_pci_release_region;
852
853 ioapic->gsi_base = gsi_base;
854 ioapic->dev = pdev;
855 spin_lock(&ioapic_list_lock);
856 list_add_tail(&ioapic->list, &ioapic_list);
857 spin_unlock(&ioapic_list_lock);
858
859 return AE_OK;
860
861 exit_pci_release_region:
862 pci_release_region(pdev, 0);
863 exit_pci_disable_device:
864 pci_disable_device(pdev);
865 exit_pci_dev_put:
866 pci_dev_put(pdev);
867 exit_kfree:
868 kfree(ioapic);
869
870 return AE_OK;
871}
872
873static acpi_status
874ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
875{
876 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400877 unsigned long long sta;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700878 acpi_handle tmp;
879 u32 gsi_base;
880 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
881
882 /* Evaluate _STA if present */
883 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
884 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
885 return AE_CTRL_DEPTH;
886
887 /* Scan only PCI bus scope */
888 status = acpi_get_handle(handle, "_HID", &tmp);
889 if (ACPI_SUCCESS(status))
890 return AE_CTRL_DEPTH;
891
892 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700893 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700894
895 acpi_unregister_ioapic(handle, gsi_base);
896
897 spin_lock(&ioapic_list_lock);
898 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
899 if (pos->gsi_base != gsi_base)
900 continue;
901 ioapic = pos;
902 list_del(&ioapic->list);
903 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700904 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700905 spin_unlock(&ioapic_list_lock);
906
907 if (!ioapic)
908 return AE_OK;
909
910 pci_release_region(ioapic->dev, 0);
911 pci_disable_device(ioapic->dev);
912 pci_dev_put(ioapic->dev);
913 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700914
915 return AE_OK;
916}
917
918static int acpiphp_configure_ioapics(acpi_handle handle)
919{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700920 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700921 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
922 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
923 return 0;
924}
925
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700926static int acpiphp_unconfigure_ioapics(acpi_handle handle)
927{
928 ioapic_remove(handle, 0, NULL, NULL);
929 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
930 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
931 return 0;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static int power_on_slot(struct acpiphp_slot *slot)
935{
936 acpi_status status;
937 struct acpiphp_func *func;
938 struct list_head *l;
939 int retval = 0;
940
941 /* if already enabled, just skip */
942 if (slot->flags & SLOT_POWEREDON)
943 goto err_exit;
944
945 list_for_each (l, &slot->funcs) {
946 func = list_entry(l, struct acpiphp_func, sibling);
947
948 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800949 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
951 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800952 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 retval = -1;
954 goto err_exit;
955 } else
956 break;
957 }
958 }
959
960 /* TBD: evaluate _STA to check if the slot is enabled */
961
962 slot->flags |= SLOT_POWEREDON;
963
964 err_exit:
965 return retval;
966}
967
968
969static int power_off_slot(struct acpiphp_slot *slot)
970{
971 acpi_status status;
972 struct acpiphp_func *func;
973 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 int retval = 0;
976
977 /* if already disabled, just skip */
978 if ((slot->flags & SLOT_POWEREDON) == 0)
979 goto err_exit;
980
981 list_for_each (l, &slot->funcs) {
982 func = list_entry(l, struct acpiphp_func, sibling);
983
Rajesh Shah2f523b12005-04-28 00:25:55 -0700984 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
986 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800987 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 retval = -1;
989 goto err_exit;
990 } else
991 break;
992 }
993 }
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* TBD: evaluate _STA to check if the slot is disabled */
996
997 slot->flags &= (~SLOT_POWEREDON);
998
999 err_exit:
1000 return retval;
1001}
1002
1003
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001004
1005/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001006 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001007 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001008 */
1009static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
1010{
1011 struct list_head *tmp;
1012 unsigned char max, n;
1013
1014 /*
1015 * pci_bus_max_busnr will return the highest
1016 * reserved busnr for all these children.
1017 * that is equivalent to the bus->subordinate
1018 * value. We don't want to use the parent's
1019 * bus->subordinate value because it could have
1020 * padding in it.
1021 */
1022 max = bus->secondary;
1023
1024 list_for_each(tmp, &bus->children) {
1025 n = pci_bus_max_busnr(pci_bus_b(tmp));
1026 if (n > max)
1027 max = n;
1028 }
1029 return max;
1030}
1031
1032
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001033/**
1034 * acpiphp_bus_add - add a new bus to acpi subsystem
1035 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001036 */
1037static int acpiphp_bus_add(struct acpiphp_func *func)
1038{
1039 acpi_handle phandle;
1040 struct acpi_device *device, *pdevice;
1041 int ret_val;
1042
1043 acpi_get_parent(func->handle, &phandle);
1044 if (acpi_bus_get_device(phandle, &pdevice)) {
1045 dbg("no parent device, assuming NULL\n");
1046 pdevice = NULL;
1047 }
Kristen Accardi20416ea2006-02-23 17:56:03 -08001048 if (!acpi_bus_get_device(func->handle, &device)) {
1049 dbg("bus exists... trim\n");
1050 /* this shouldn't be in here, so remove
1051 * the bus then re-add it...
1052 */
1053 ret_val = acpi_bus_trim(device, 1);
1054 dbg("acpi_bus_trim return %x\n", ret_val);
1055 }
1056
1057 ret_val = acpi_bus_add(&device, pdevice, func->handle,
1058 ACPI_BUS_TYPE_DEVICE);
1059 if (ret_val) {
1060 dbg("error adding bus, %x\n",
1061 -ret_val);
1062 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001063 }
1064 /*
1065 * try to start anyway. We could have failed to add
1066 * simply because this bus had previously been added
1067 * on another add. Don't bother with the return value
1068 * we just keep going.
1069 */
1070 ret_val = acpi_bus_start(device);
1071
1072acpiphp_bus_add_out:
1073 return ret_val;
1074}
1075
1076
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001077/**
1078 * acpiphp_bus_trim - trim a bus from acpi subsystem
1079 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001080 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -07001081static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001082{
1083 struct acpi_device *device;
1084 int retval;
1085
1086 retval = acpi_bus_get_device(handle, &device);
1087 if (retval) {
1088 dbg("acpi_device not found\n");
1089 return retval;
1090 }
1091
1092 retval = acpi_bus_trim(device, 1);
1093 if (retval)
1094 err("cannot remove from acpi list\n");
1095
1096 return retval;
1097}
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099/**
1100 * enable_device - enable, configure a slot
1101 * @slot: slot to be enabled
1102 *
1103 * This function should be called per *physical slot*,
1104 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +01001106static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001109 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct list_head *l;
1111 struct acpiphp_func *func;
1112 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001113 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001114 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (slot->flags & SLOT_ENABLED)
1117 goto err_exit;
1118
1119 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001120 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (dev) {
1122 /* This case shouldn't happen */
1123 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001124 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 retval = -1;
1126 goto err_exit;
1127 }
1128
Rajesh Shah42f49a62005-04-28 00:25:53 -07001129 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1130 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 err("No new device found\n");
1132 retval = -1;
1133 goto err_exit;
1134 }
1135
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001136 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001137 for (pass = 0; pass < 2; pass++) {
1138 list_for_each_entry(dev, &bus->devices, bus_list) {
1139 if (PCI_SLOT(dev->devfn) != slot->device)
1140 continue;
1141 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001142 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001143 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001144 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001145 pci_bus_size_bridges(dev->subordinate);
1146 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001150 list_for_each (l, &slot->funcs) {
1151 func = list_entry(l, struct acpiphp_func, sibling);
1152 acpiphp_bus_add(func);
1153 }
1154
Rajesh Shah42f49a62005-04-28 00:25:53 -07001155 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001156 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001157 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001158 list_for_each_entry(func, &slot->funcs, sibling)
1159 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001160 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001161 pci_bus_add_devices(bus);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /* associate pci_dev to our representation */
1164 list_for_each (l, &slot->funcs) {
1165 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001166 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 func->function));
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001168 if (!func->pci_dev)
1169 continue;
1170
1171 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1172 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1173 continue;
1174
1175 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1176 if (ACPI_FAILURE(status))
1177 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1178 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
1181 slot->flags |= SLOT_ENABLED;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 err_exit:
1184 return retval;
1185}
1186
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001187static void disable_bridges(struct pci_bus *bus)
1188{
1189 struct pci_dev *dev;
1190 list_for_each_entry(dev, &bus->devices, bus_list) {
1191 if (dev->subordinate) {
1192 disable_bridges(dev->subordinate);
1193 pci_disable_device(dev);
1194 }
1195 }
1196}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198/**
1199 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001200 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
1202static int disable_device(struct acpiphp_slot *slot)
1203{
1204 int retval = 0;
1205 struct acpiphp_func *func;
1206 struct list_head *l;
1207
1208 /* is this slot already disabled? */
1209 if (!(slot->flags & SLOT_ENABLED))
1210 goto err_exit;
1211
1212 list_for_each (l, &slot->funcs) {
1213 func = list_entry(l, struct acpiphp_func, sibling);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001214
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001215 if (func->bridge) {
1216 /* cleanup p2p bridges under this P2P bridge */
1217 cleanup_p2p_bridge(func->bridge->handle,
1218 (u32)1, NULL, NULL);
1219 func->bridge = NULL;
1220 }
1221
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001222 if (func->pci_dev) {
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001223 pci_stop_bus_device(func->pci_dev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001224 if (func->pci_dev->subordinate) {
1225 disable_bridges(func->pci_dev->subordinate);
1226 pci_disable_device(func->pci_dev);
1227 }
1228 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001229 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001230
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001231 list_for_each (l, &slot->funcs) {
1232 func = list_entry(l, struct acpiphp_func, sibling);
1233
1234 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001235 acpiphp_bus_trim(func->handle);
1236 /* try to remove anyway.
1237 * acpiphp_bus_add might have been failed */
1238
Rajesh Shah42f49a62005-04-28 00:25:53 -07001239 if (!func->pci_dev)
1240 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Rajesh Shah42f49a62005-04-28 00:25:53 -07001242 pci_remove_bus_device(func->pci_dev);
1243 pci_dev_put(func->pci_dev);
1244 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246
1247 slot->flags &= (~SLOT_ENABLED);
1248
1249 err_exit:
1250 return retval;
1251}
1252
1253
1254/**
1255 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -08001256 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001258 * If a slot has _STA for each function and if any one of them
1259 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001261 * If a slot doesn't have _STA and if any one of its functions'
1262 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001264 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
1266static unsigned int get_slot_status(struct acpiphp_slot *slot)
1267{
1268 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001269 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 u32 dvid;
1271 struct list_head *l;
1272 struct acpiphp_func *func;
1273
1274 list_for_each (l, &slot->funcs) {
1275 func = list_entry(l, struct acpiphp_func, sibling);
1276
1277 if (func->flags & FUNC_HAS_STA) {
1278 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1279 if (ACPI_SUCCESS(status) && sta)
1280 break;
1281 } else {
1282 pci_bus_read_config_dword(slot->bridge->pci_bus,
1283 PCI_DEVFN(slot->device,
1284 func->function),
1285 PCI_VENDOR_ID, &dvid);
1286 if (dvid != 0xffffffff) {
1287 sta = ACPI_STA_ALL;
1288 break;
1289 }
1290 }
1291 }
1292
1293 return (unsigned int)sta;
1294}
1295
1296/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001297 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001298 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001299 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001300int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001301{
1302 acpi_status status;
1303 struct acpiphp_func *func;
1304 struct list_head *l;
1305 struct acpi_object_list arg_list;
1306 union acpi_object arg;
1307
1308 list_for_each (l, &slot->funcs) {
1309 func = list_entry(l, struct acpiphp_func, sibling);
1310
1311 /* We don't want to call _EJ0 on non-existing functions. */
1312 if ((func->flags & FUNC_HAS_EJ0)) {
1313 /* _EJ0 method take one argument */
1314 arg_list.count = 1;
1315 arg_list.pointer = &arg;
1316 arg.type = ACPI_TYPE_INTEGER;
1317 arg.integer.value = 1;
1318
1319 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1320 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001321 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001322 return -1;
1323 } else
1324 break;
1325 }
1326 }
1327 return 0;
1328}
1329
1330/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001332 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
1334 * Iterate over all slots under this bridge and make sure that if a
1335 * card is present they are enabled, and if not they are disabled.
1336 */
1337static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1338{
1339 struct acpiphp_slot *slot;
1340 int retval = 0;
1341 int enabled, disabled;
1342
1343 enabled = disabled = 0;
1344
1345 for (slot = bridge->slots; slot; slot = slot->next) {
1346 unsigned int status = get_slot_status(slot);
1347 if (slot->flags & SLOT_ENABLED) {
1348 if (status == ACPI_STA_ALL)
1349 continue;
1350 retval = acpiphp_disable_slot(slot);
1351 if (retval) {
1352 err("Error occurred in disabling\n");
1353 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001354 } else {
1355 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 disabled++;
1358 } else {
1359 if (status != ACPI_STA_ALL)
1360 continue;
1361 retval = acpiphp_enable_slot(slot);
1362 if (retval) {
1363 err("Error occurred in enabling\n");
1364 goto err_exit;
1365 }
1366 enabled++;
1367 }
1368 }
1369
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001370 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 err_exit:
1373 return retval;
1374}
1375
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001376static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1377{
1378 u16 pci_cmd, pci_bctl;
1379 struct pci_dev *cdev;
1380
1381 /* Program hpp values for this device */
1382 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1383 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1384 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1385 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001386
Gary Hade9ef22412007-07-05 11:10:47 -07001387 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1388 return;
1389
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001390 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001391 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001392 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001393 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001394 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001395 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001396 pci_cmd |= PCI_COMMAND_SERR;
1397 else
1398 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001399 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001400 pci_cmd |= PCI_COMMAND_PARITY;
1401 else
1402 pci_cmd &= ~PCI_COMMAND_PARITY;
1403 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1404
1405 /* Program bridge control value and child devices */
1406 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1407 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001408 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001409 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001410 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001411 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1412 else
1413 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001414 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001415 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1416 else
1417 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1418 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1419 if (dev->subordinate) {
1420 list_for_each_entry(cdev, &dev->subordinate->devices,
1421 bus_list)
1422 program_hpp(cdev, bridge);
1423 }
1424 }
1425}
1426
1427static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1428{
1429 struct acpiphp_bridge bridge;
1430 struct pci_dev *dev;
1431
1432 memset(&bridge, 0, sizeof(bridge));
1433 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001434 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001435 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001436 decode_hpp(&bridge);
1437 list_for_each_entry(dev, &bus->devices, bus_list)
1438 program_hpp(dev, &bridge);
1439
1440}
1441
1442/*
1443 * Remove devices for which we could not assign resources, call
1444 * arch specific code to fix-up the bus
1445 */
1446static void acpiphp_sanitize_bus(struct pci_bus *bus)
1447{
1448 struct pci_dev *dev;
1449 int i;
1450 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1451
1452 list_for_each_entry(dev, &bus->devices, bus_list) {
1453 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1454 struct resource *res = &dev->resource[i];
1455 if ((res->flags & type_mask) && !res->start &&
1456 res->end) {
1457 /* Could not assign a required resources
1458 * for this device, remove it */
1459 pci_remove_bus_device(dev);
1460 break;
1461 }
1462 }
1463 }
1464}
1465
1466/* Program resources in newly inserted bridge */
1467static int acpiphp_configure_bridge (acpi_handle handle)
1468{
1469 struct acpi_pci_id pci_id;
1470 struct pci_bus *bus;
1471
1472 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1473 err("cannot get PCI domain and bus number for bridge\n");
1474 return -EINVAL;
1475 }
1476 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1477 if (!bus) {
1478 err("cannot find bus %d:%d\n",
1479 pci_id.segment, pci_id.bus);
1480 return -EINVAL;
1481 }
1482
1483 pci_bus_size_bridges(bus);
1484 pci_bus_assign_resources(bus);
1485 acpiphp_sanitize_bus(bus);
1486 acpiphp_set_hpp_values(handle, bus);
1487 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001488 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001489 return 0;
1490}
1491
1492static void handle_bridge_insertion(acpi_handle handle, u32 type)
1493{
1494 struct acpi_device *device, *pdevice;
1495 acpi_handle phandle;
1496
1497 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1498 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1499 err("unexpected notification type %d\n", type);
1500 return;
1501 }
1502
1503 acpi_get_parent(handle, &phandle);
1504 if (acpi_bus_get_device(phandle, &pdevice)) {
1505 dbg("no parent device, assuming NULL\n");
1506 pdevice = NULL;
1507 }
1508 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1509 err("cannot add bridge to acpi list\n");
1510 return;
1511 }
1512 if (!acpiphp_configure_bridge(handle) &&
1513 !acpi_bus_start(device))
1514 add_bridge(handle);
1515 else
1516 err("cannot configure and start bridge\n");
1517
1518}
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520/*
1521 * ACPI event handlers
1522 */
1523
Gary Hade0bbd6422007-07-05 11:10:48 -07001524static acpi_status
1525count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1526{
1527 int *count = (int *)context;
1528 struct acpiphp_bridge *bridge;
1529
1530 bridge = acpiphp_handle_to_bridge(handle);
1531 if (bridge)
1532 (*count)++;
1533 return AE_OK ;
1534}
1535
1536static acpi_status
1537check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1538{
1539 struct acpiphp_bridge *bridge;
1540 char objname[64];
1541 struct acpi_buffer buffer = { .length = sizeof(objname),
1542 .pointer = objname };
1543
1544 bridge = acpiphp_handle_to_bridge(handle);
1545 if (bridge) {
1546 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1547 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001548 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001549 acpiphp_check_bridge(bridge);
1550 }
1551 return AE_OK ;
1552}
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/**
1555 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 * @handle: Notify()'ed acpi_handle
1557 * @type: Notify code
1558 * @context: pointer to acpiphp_bridge structure
1559 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001560 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 */
1562static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1563{
1564 struct acpiphp_bridge *bridge;
1565 char objname[64];
1566 struct acpi_buffer buffer = { .length = sizeof(objname),
1567 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001568 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001569 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001571 if (acpi_bus_get_device(handle, &device)) {
1572 /* This bridge must have just been physically inserted */
1573 handle_bridge_insertion(handle, type);
1574 return;
1575 }
1576
1577 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001578 if (type == ACPI_NOTIFY_BUS_CHECK) {
1579 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1580 count_sub_bridges, &num_sub_bridges, NULL);
1581 }
1582
1583 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001584 err("cannot get bridge info\n");
1585 return;
1586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1589
1590 switch (type) {
1591 case ACPI_NOTIFY_BUS_CHECK:
1592 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001593 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001594 if (bridge) {
1595 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001596 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001597 acpiphp_check_bridge(bridge);
1598 }
1599 if (num_sub_bridges)
1600 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1601 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
1603
1604 case ACPI_NOTIFY_DEVICE_CHECK:
1605 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001606 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 acpiphp_check_bridge(bridge);
1608 break;
1609
1610 case ACPI_NOTIFY_DEVICE_WAKE:
1611 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001612 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
1614
1615 case ACPI_NOTIFY_EJECT_REQUEST:
1616 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001617 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001618 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1619 (bridge->flags & BRIDGE_HAS_EJ0)) {
1620 struct acpiphp_slot *slot;
1621 slot = bridge->func->slot;
1622 if (!acpiphp_disable_slot(slot))
1623 acpiphp_eject_slot(slot);
1624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 break;
1626
1627 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1628 printk(KERN_ERR "Device %s cannot be configured due"
1629 " to a frequency mismatch\n", objname);
1630 break;
1631
1632 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1633 printk(KERN_ERR "Device %s cannot be configured due"
1634 " to a bus mode mismatch\n", objname);
1635 break;
1636
1637 case ACPI_NOTIFY_POWER_FAULT:
1638 printk(KERN_ERR "Device %s has suffered a power fault\n",
1639 objname);
1640 break;
1641
1642 default:
1643 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1644 break;
1645 }
1646}
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648/**
1649 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * @handle: Notify()'ed acpi_handle
1651 * @type: Notify code
1652 * @context: pointer to acpiphp_func structure
1653 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001654 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
Len Brown2b85e132006-06-27 01:50:14 -04001656static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct acpiphp_func *func;
1659 char objname[64];
1660 struct acpi_buffer buffer = { .length = sizeof(objname),
1661 .pointer = objname };
1662
1663 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1664
1665 func = (struct acpiphp_func *)context;
1666
1667 switch (type) {
1668 case ACPI_NOTIFY_BUS_CHECK:
1669 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001670 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 acpiphp_enable_slot(func->slot);
1672 break;
1673
1674 case ACPI_NOTIFY_DEVICE_CHECK:
1675 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001676 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 acpiphp_check_bridge(func->slot->bridge);
1678 break;
1679
1680 case ACPI_NOTIFY_DEVICE_WAKE:
1681 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001682 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 break;
1684
1685 case ACPI_NOTIFY_EJECT_REQUEST:
1686 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001687 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001688 if (!(acpiphp_disable_slot(func->slot)))
1689 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691
1692 default:
1693 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1694 break;
1695 }
1696}
1697
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001698
1699static acpi_status
1700find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1701{
1702 int *count = (int *)context;
1703
Kristen Accardi783c49f2006-03-03 10:16:05 -08001704 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001705 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1706 handle_hotplug_event_bridge, NULL);
1707 (*count)++;
1708 }
1709 return AE_OK ;
1710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712static struct acpi_pci_driver acpi_pci_hp_driver = {
1713 .add = add_bridge,
1714 .remove = remove_bridge,
1715};
1716
1717/**
1718 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 */
1720int __init acpiphp_glue_init(void)
1721{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001722 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001724 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1725 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 if (num <= 0)
1728 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001729 else
1730 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 return 0;
1733}
1734
1735
1736/**
1737 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1738 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001739 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001741void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1744}
1745
1746
1747/**
1748 * acpiphp_get_num_slots - count number of slots in a system
1749 */
1750int __init acpiphp_get_num_slots(void)
1751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001753 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Akinobu Mita467c4422006-10-30 13:07:58 -08001755 list_for_each_entry (bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001756 dbg("Bus %04x:%02x has %d slot%s\n",
1757 pci_domain_nr(bridge->pci_bus),
1758 bridge->pci_bus->number, bridge->nr_slots,
1759 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 num_slots += bridge->nr_slots;
1761 }
1762
Rajesh Shah42f49a62005-04-28 00:25:53 -07001763 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return num_slots;
1765}
1766
1767
1768#if 0
1769/**
1770 * acpiphp_for_each_slot - call function for each slot
1771 * @fn: callback function
1772 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
1774static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1775{
1776 struct list_head *node;
1777 struct acpiphp_bridge *bridge;
1778 struct acpiphp_slot *slot;
1779 int retval = 0;
1780
1781 list_for_each (node, &bridge_list) {
1782 bridge = (struct acpiphp_bridge *)node;
1783 for (slot = bridge->slots; slot; slot = slot->next) {
1784 retval = fn(slot, data);
1785 if (!retval)
1786 goto err_exit;
1787 }
1788 }
1789
1790 err_exit:
1791 return retval;
1792}
1793#endif
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796/**
1797 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001798 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
1800int acpiphp_enable_slot(struct acpiphp_slot *slot)
1801{
1802 int retval;
1803
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001804 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 /* wake up all functions */
1807 retval = power_on_slot(slot);
1808 if (retval)
1809 goto err_exit;
1810
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001811 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* configure all functions */
1813 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001814 if (retval)
1815 power_off_slot(slot);
1816 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001817 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001818 power_off_slot(slot);
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001822 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return retval;
1824}
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826/**
1827 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001828 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 */
1830int acpiphp_disable_slot(struct acpiphp_slot *slot)
1831{
1832 int retval = 0;
1833
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001834 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 /* unconfigure all functions */
1837 retval = disable_device(slot);
1838 if (retval)
1839 goto err_exit;
1840
1841 /* power off all functions */
1842 retval = power_off_slot(slot);
1843 if (retval)
1844 goto err_exit;
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001847 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return retval;
1849}
1850
1851
1852/*
1853 * slot enabled: 1
1854 * slot disabled: 0
1855 */
1856u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1857{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001858 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861
1862/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001863 * latch open: 1
1864 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 */
1866u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1867{
1868 unsigned int sta;
1869
1870 sta = get_slot_status(slot);
1871
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001872 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
1875
1876/*
1877 * adapter presence : 1
1878 * absence : 0
1879 */
1880u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1881{
1882 unsigned int sta;
1883
1884 sta = get_slot_status(slot);
1885
1886 return (sta == 0) ? 0 : 1;
1887}