blob: 1b19b7ec3e8c442c28562ddcddfc2884dec0ccf1 [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
Matthew Garrett56ee3252008-11-25 21:48:14 +000077 * 2. has _EJ0 method or _RMV method
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
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;
Matthew Garrett56ee3252008-11-25 21:48:14 +000090 unsigned long long removable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 status = acpi_get_handle(handle, "_ADR", &tmp);
Matthew Garrett56ee3252008-11-25 21:48:14 +000093 if (ACPI_FAILURE(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 status = acpi_get_handle(handle, "_EJ0", &tmp);
Matthew Garrett56ee3252008-11-25 21:48:14 +000097 if (ACPI_SUCCESS(status))
98 return 1;
99
100 status = acpi_get_handle(handle, "_RMV", &tmp);
101 if (ACPI_SUCCESS(status)) {
102 status = acpi_evaluate_integer(handle, "_RMV", NULL,
103 &removable);
104 if (ACPI_SUCCESS(status) && removable)
105 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
Matthew Garrett56ee3252008-11-25 21:48:14 +0000108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +0900112/* callback routine to check for the existence of ejectable slots */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static acpi_status
114is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
115{
116 int *count = (int *)context;
117
118 if (is_ejectable(handle)) {
119 (*count)++;
120 /* only one ejectable slot is enough */
121 return AE_CTRL_TERMINATE;
122 } else {
123 return AE_OK;
124 }
125}
126
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +0900127/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400128static acpi_status
129is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
130{
131 int *count = (int *)context;
132
133 if (is_dock_device(handle)) {
134 (*count)++;
135 return AE_CTRL_TERMINATE;
136 } else {
137 return AE_OK;
138 }
139}
140
141
142
143
144/*
145 * the _DCK method can do funny things... and sometimes not
146 * hah-hah funny.
147 *
148 * TBD - figure out a way to only call fixups for
149 * systems that require them.
150 */
151static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
152 void *v)
153{
154 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
155 struct pci_bus *bus = func->slot->bridge->pci_bus;
156 u32 buses;
157
158 if (!bus->self)
159 return NOTIFY_OK;
160
161 /* fixup bad _DCK function that rewrites
162 * secondary bridge on slot
163 */
164 pci_read_config_dword(bus->self,
165 PCI_PRIMARY_BUS,
166 &buses);
167
168 if (((buses >> 8) & 0xff) != bus->secondary) {
169 buses = (buses & 0xff000000)
170 | ((unsigned int)(bus->primary) << 0)
171 | ((unsigned int)(bus->secondary) << 8)
172 | ((unsigned int)(bus->subordinate) << 16);
173 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
174 }
175 return NOTIFY_OK;
176}
177
178
Shaohua Li1253f7a2008-08-28 10:06:16 +0800179static struct acpi_dock_ops acpiphp_dock_ops = {
180 .handler = handle_hotplug_event_func,
181};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* callback routine to register each ACPI PCI slot object */
184static acpi_status
185register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
186{
187 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
188 struct acpiphp_slot *slot;
189 struct acpiphp_func *newfunc;
190 acpi_handle tmp;
191 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400192 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800193 int device, function, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Matthew Garrett56ee3252008-11-25 21:48:14 +0000195 if (!is_ejectable(handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return AE_OK;
197
Matthew Garrett56ee3252008-11-25 21:48:14 +0000198 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 device = (adr >> 16) & 0xffff;
200 function = adr & 0xffff;
201
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100202 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (!newfunc)
204 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 INIT_LIST_HEAD(&newfunc->sibling);
207 newfunc->handle = handle;
208 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000209
210 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800211 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
214 newfunc->flags |= FUNC_HAS_STA;
215
216 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
217 newfunc->flags |= FUNC_HAS_PS0;
218
219 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
220 newfunc->flags |= FUNC_HAS_PS3;
221
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400222 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800223 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400226 if (ACPI_FAILURE(status)) {
227 /*
228 * use the count of the number of slots we've found
229 * for the number of the slot
230 */
231 sun = bridge->nr_slots+1;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* search for objects that share the same slot */
235 for (slot = bridge->slots; slot; slot = slot->next)
236 if (slot->device == device) {
237 if (slot->sun != sun)
238 warn("sibling found, but _SUN doesn't match!\n");
239 break;
240 }
241
242 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100243 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!slot) {
245 kfree(newfunc);
246 return AE_NO_MEMORY;
247 }
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 slot->device = device;
251 slot->sun = sun;
252 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100253 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 slot->next = bridge->slots;
256 bridge->slots = slot;
257
258 bridge->nr_slots++;
259
Justin Chenb6adc192008-12-11 11:16:44 -0700260 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Rajesh Shah42f49a62005-04-28 00:25:53 -0700261 slot->sun, pci_domain_nr(bridge->pci_bus),
262 bridge->pci_bus->number, slot->device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800263 retval = acpiphp_register_hotplug_slot(slot);
264 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600265 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700266 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600267 "hotplug driver\n", slot->sun);
268 else
269 warn("acpiphp_register_hotplug_slot failed "
270 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800271 goto err_exit;
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
275 newfunc->slot = slot;
276 list_add_tail(&newfunc->sibling, &slot->funcs);
277
278 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700279 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 PCI_DEVFN(device, function));
281 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
283 }
284
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400285 if (is_dock_device(handle)) {
286 /* we don't want to call this device's _EJ0
287 * because we want the dock notify handler
288 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800289 */
290 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400291 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800292 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400293 dbg("failed to register dock device\n");
294
295 /* we need to be notified when dock events happen
296 * outside of the hotplug operation, since we may
297 * need to do fixups before we can hotplug.
298 */
299 newfunc->nb.notifier_call = post_dock_fixups;
300 if (register_dock_notifier(&newfunc->nb))
301 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800305 if (!(newfunc->flags & FUNC_HAS_DCK)) {
306 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ACPI_SYSTEM_NOTIFY,
308 handle_hotplug_event_func,
309 newfunc);
310
Kristen Accardi20416ea2006-02-23 17:56:03 -0800311 if (ACPI_FAILURE(status))
312 err("failed to register interrupt notify handler\n");
313 } else
314 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Kristen Accardi20416ea2006-02-23 17:56:03 -0800316 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800317
318 err_exit:
319 bridge->nr_slots--;
320 bridge->slots = slot->next;
321 kfree(slot);
322 kfree(newfunc);
323
324 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327
328/* see if it's worth looking at this bridge */
329static int detect_ejectable_slots(acpi_handle *bridge_handle)
330{
331 acpi_status status;
332 int count;
333
334 count = 0;
335
336 /* only check slots defined directly below bridge object */
337 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
338 is_ejectable_slot, (void *)&count, NULL);
339
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400340 /*
341 * we also need to add this bridge if there is a dock bridge or
342 * other pci device on a dock station (removable)
343 */
344 if (!count)
345 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
346 (u32)1, is_pci_dock_device, (void *)&count,
347 NULL);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return count;
350}
351
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/* decode ACPI 2.0 _HPP hot plug parameters */
354static void decode_hpp(struct acpiphp_bridge *bridge)
355{
356 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900358 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900359 if (ACPI_FAILURE(status) ||
360 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800361 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900362 printk(KERN_WARNING
363 "%s: Could not get hotplug parameters. Use defaults\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800364 __func__);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900365 bridge->hpp.t0 = &bridge->hpp.type0_data;
366 bridge->hpp.t0->revision = 0;
367 bridge->hpp.t0->cache_line_size = 0x10;
368 bridge->hpp.t0->latency_timer = 0x40;
369 bridge->hpp.t0->enable_serr = 0;
370 bridge->hpp.t0->enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374
Kristen Accardi783c49f2006-03-03 10:16:05 -0800375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
377static void init_bridge_misc(struct acpiphp_bridge *bridge)
378{
379 acpi_status status;
380
381 /* decode ACPI 2.0 _HPP (hot plug parameters) */
382 decode_hpp(bridge);
383
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800384 /* must be added to the list prior to calling register_slot */
385 list_add(&bridge->list, &bridge_list);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* register all slot objects under this bridge */
388 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
389 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800390 if (ACPI_FAILURE(status)) {
391 list_del(&bridge->list);
392 return;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700396 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900397 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
398 status = acpi_remove_notify_handler(bridge->func->handle,
399 ACPI_SYSTEM_NOTIFY,
400 handle_hotplug_event_func);
401 if (ACPI_FAILURE(status))
402 err("failed to remove notify handler\n");
403 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700404 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ACPI_SYSTEM_NOTIFY,
406 handle_hotplug_event_bridge,
407 bridge);
408
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700409 if (ACPI_FAILURE(status)) {
410 err("failed to register interrupt notify handler\n");
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900416/* find acpiphp_func from acpiphp_bridge */
417static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
418{
419 struct list_head *node, *l;
420 struct acpiphp_bridge *bridge;
421 struct acpiphp_slot *slot;
422 struct acpiphp_func *func;
423
424 list_for_each(node, &bridge_list) {
425 bridge = list_entry(node, struct acpiphp_bridge, list);
426 for (slot = bridge->slots; slot; slot = slot->next) {
427 list_for_each(l, &slot->funcs) {
428 func = list_entry(l, struct acpiphp_func,
429 sibling);
430 if (func->handle == handle)
431 return func;
432 }
433 }
434 }
435
436 return NULL;
437}
438
439
440static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
441{
442 acpi_handle dummy_handle;
443
444 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
445 "_STA", &dummy_handle)))
446 bridge->flags |= BRIDGE_HAS_STA;
447
448 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
449 "_EJ0", &dummy_handle)))
450 bridge->flags |= BRIDGE_HAS_EJ0;
451
452 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
453 "_PS0", &dummy_handle)))
454 bridge->flags |= BRIDGE_HAS_PS0;
455
456 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
457 "_PS3", &dummy_handle)))
458 bridge->flags |= BRIDGE_HAS_PS3;
459
460 /* is this ejectable p2p bridge? */
461 if (bridge->flags & BRIDGE_HAS_EJ0) {
462 struct acpiphp_func *func;
463
464 dbg("found ejectable p2p bridge\n");
465
466 /* make link between PCI bridge and PCI function */
467 func = acpiphp_bridge_handle_to_function(bridge->handle);
468 if (!func)
469 return;
470 bridge->func = func;
471 func->bridge = bridge;
472 }
473}
474
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700477static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 struct acpiphp_bridge *bridge;
480
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100481 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (bridge == NULL)
483 return;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 bridge->type = BRIDGE_TYPE_HOST;
486 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Rajesh Shah42f49a62005-04-28 00:25:53 -0700488 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 spin_lock_init(&bridge->res_lock);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 init_bridge_misc(bridge);
493}
494
495
496/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700497static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100501 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (bridge == NULL) {
503 err("out of memory\n");
504 return;
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 bridge->type = BRIDGE_TYPE_P2P;
508 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900509 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Rajesh Shah42f49a62005-04-28 00:25:53 -0700511 bridge->pci_dev = pci_dev_get(pci_dev);
512 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!bridge->pci_bus) {
514 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700515 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 spin_lock_init(&bridge->res_lock);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700521 return;
522 err:
523 pci_dev_put(pci_dev);
524 kfree(bridge);
525 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528
529/* callback routine to find P2P bridges */
530static acpi_status
531find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
532{
533 acpi_status status;
534 acpi_handle dummy_handle;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400535 unsigned long long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700536 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700538 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
541 if (ACPI_FAILURE(status))
542 return AE_OK; /* continue */
543
544 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
545 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800546 dbg("%s: _ADR evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return AE_OK;
548 }
549
550 device = (tmp >> 16) & 0xffff;
551 function = tmp & 0xffff;
552
Rajesh Shah42f49a62005-04-28 00:25:53 -0700553 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Rajesh Shah42f49a62005-04-28 00:25:53 -0700555 if (!dev || !dev->subordinate)
556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* check if this bridge has ejectable slots */
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400559 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700561 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900564 /* search P2P bridges under this p2p bridge */
565 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
566 find_p2p_bridge, dev->subordinate, NULL);
567 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900568 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900569
Rajesh Shah42f49a62005-04-28 00:25:53 -0700570 out:
571 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return AE_OK;
573}
574
575
576/* find hot-pluggable slots, and then find P2P bridge */
577static int add_bridge(acpi_handle handle)
578{
579 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400580 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int seg, bus;
582 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700583 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* if the bridge doesn't have _STA, we assume it is always there */
586 status = acpi_get_handle(handle, "_STA", &dummy_handle);
587 if (ACPI_SUCCESS(status)) {
588 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
589 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800590 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
592 }
593 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
594 /* don't register this object */
595 return 0;
596 }
597
598 /* get PCI segment number */
599 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
600
601 seg = ACPI_SUCCESS(status) ? tmp : 0;
602
603 /* get PCI bus number */
604 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
605
606 if (ACPI_SUCCESS(status)) {
607 bus = tmp;
608 } else {
609 warn("can't get bus number, assuming 0\n");
610 bus = 0;
611 }
612
Rajesh Shah42f49a62005-04-28 00:25:53 -0700613 pci_bus = pci_find_bus(seg, bus);
614 if (!pci_bus) {
615 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
617 }
618
Rajesh Shah42f49a62005-04-28 00:25:53 -0700619 /* check if this bridge has ejectable slots */
620 if (detect_ejectable_slots(handle) > 0) {
621 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
622 add_host_bridge(handle, pci_bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* search P2P bridges under this host bridge */
626 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700627 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900630 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 return 0;
633}
634
Rajesh Shah42f49a62005-04-28 00:25:53 -0700635static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
636{
637 struct list_head *head;
638 list_for_each(head, &bridge_list) {
639 struct acpiphp_bridge *bridge = list_entry(head,
640 struct acpiphp_bridge, list);
641 if (bridge->handle == handle)
642 return bridge;
643 }
644
645 return NULL;
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Rajesh Shah364d5092005-04-28 00:25:54 -0700648static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700650 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700651 struct acpiphp_slot *slot;
652 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700653 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700654
655 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
656 handle_hotplug_event_bridge);
657 if (ACPI_FAILURE(status))
658 err("failed to remove notify handler\n");
659
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900660 if ((bridge->type != BRIDGE_TYPE_HOST) &&
661 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
662 status = acpi_install_notify_handler(bridge->func->handle,
663 ACPI_SYSTEM_NOTIFY,
664 handle_hotplug_event_func,
665 bridge->func);
666 if (ACPI_FAILURE(status))
667 err("failed to install interrupt notify handler\n");
668 }
669
Rajesh Shah42f49a62005-04-28 00:25:53 -0700670 slot = bridge->slots;
671 while (slot) {
672 struct acpiphp_slot *next = slot->next;
673 list_for_each_safe (list, tmp, &slot->funcs) {
674 struct acpiphp_func *func;
675 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400676 if (is_dock_device(func->handle)) {
677 unregister_hotplug_dock_device(func->handle);
678 unregister_dock_notifier(&func->nb);
679 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800680 if (!(func->flags & FUNC_HAS_DCK)) {
681 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700682 ACPI_SYSTEM_NOTIFY,
683 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800684 if (ACPI_FAILURE(status))
685 err("failed to remove notify handler\n");
686 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700687 pci_dev_put(func->pci_dev);
688 list_del(list);
689 kfree(func);
690 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800691 acpiphp_unregister_hotplug_slot(slot);
692 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700693 kfree(slot);
694 slot = next;
695 }
696
697 pci_dev_put(bridge->pci_dev);
698 list_del(&bridge->list);
699 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Rajesh Shah364d5092005-04-28 00:25:54 -0700702static acpi_status
703cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
704{
705 struct acpiphp_bridge *bridge;
706
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900707 /* cleanup p2p bridges under this P2P bridge
708 in a depth-first manner */
709 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
710 cleanup_p2p_bridge, NULL, NULL);
711
Alex Chianga13307c2008-07-01 20:02:23 -0600712 bridge = acpiphp_handle_to_bridge(handle);
713 if (bridge)
714 cleanup_bridge(bridge);
715
Rajesh Shah364d5092005-04-28 00:25:54 -0700716 return AE_OK;
717}
718
719static void remove_bridge(acpi_handle handle)
720{
721 struct acpiphp_bridge *bridge;
722
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900723 /* cleanup p2p bridges under this host bridge
724 in a depth-first manner */
725 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
726 (u32)1, cleanup_p2p_bridge, NULL, NULL);
727
Alex Chianga13307c2008-07-01 20:02:23 -0600728 /*
729 * On root bridges with hotplug slots directly underneath (ie,
730 * no p2p bridge inbetween), we call cleanup_bridge().
731 *
732 * The else clause cleans up root bridges that either had no
733 * hotplug slots at all, or had a p2p bridge underneath.
734 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700735 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900736 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700737 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600738 else
739 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
740 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700741}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700743static struct pci_dev * get_apic_pci_info(acpi_handle handle)
744{
745 struct acpi_pci_id id;
746 struct pci_bus *bus;
747 struct pci_dev *dev;
748
749 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
750 return NULL;
751
752 bus = pci_find_bus(id.segment, id.bus);
753 if (!bus)
754 return NULL;
755
756 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
757 if (!dev)
758 return NULL;
759
760 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
761 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
762 {
763 pci_dev_put(dev);
764 return NULL;
765 }
766
767 return dev;
768}
769
770static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
771{
772 acpi_status status;
773 int result = -1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400774 unsigned long long gsb;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700775 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
776 union acpi_object *obj;
777 void *table;
778
779 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
780 if (ACPI_SUCCESS(status)) {
781 *gsi_base = (u32)gsb;
782 return 0;
783 }
784
785 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
786 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
787 return -1;
788
789 obj = buffer.pointer;
790 if (obj->type != ACPI_TYPE_BUFFER)
791 goto out;
792
793 table = obj->buffer.pointer;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300794 switch (((struct acpi_subtable_header *)table)->type) {
795 case ACPI_MADT_TYPE_IO_SAPIC:
796 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700797 result = 0;
798 break;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300799 case ACPI_MADT_TYPE_IO_APIC:
800 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700801 result = 0;
802 break;
803 default:
804 break;
805 }
806 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700807 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700808 return result;
809}
810
811static acpi_status
812ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
813{
814 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400815 unsigned long long sta;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700816 acpi_handle tmp;
817 struct pci_dev *pdev;
818 u32 gsi_base;
819 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700820 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700821
822 /* Evaluate _STA if present */
823 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
824 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
825 return AE_CTRL_DEPTH;
826
827 /* Scan only PCI bus scope */
828 status = acpi_get_handle(handle, "_HID", &tmp);
829 if (ACPI_SUCCESS(status))
830 return AE_CTRL_DEPTH;
831
832 if (get_gsi_base(handle, &gsi_base))
833 return AE_OK;
834
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700835 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
836 if (!ioapic)
837 return AE_NO_MEMORY;
838
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700839 pdev = get_apic_pci_info(handle);
840 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700841 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700842
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700843 if (pci_enable_device(pdev))
844 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700845
846 pci_set_master(pdev);
847
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700848 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
849 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700850
851 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700852 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
853 goto exit_pci_release_region;
854
855 ioapic->gsi_base = gsi_base;
856 ioapic->dev = pdev;
857 spin_lock(&ioapic_list_lock);
858 list_add_tail(&ioapic->list, &ioapic_list);
859 spin_unlock(&ioapic_list_lock);
860
861 return AE_OK;
862
863 exit_pci_release_region:
864 pci_release_region(pdev, 0);
865 exit_pci_disable_device:
866 pci_disable_device(pdev);
867 exit_pci_dev_put:
868 pci_dev_put(pdev);
869 exit_kfree:
870 kfree(ioapic);
871
872 return AE_OK;
873}
874
875static acpi_status
876ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
877{
878 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400879 unsigned long long sta;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700880 acpi_handle tmp;
881 u32 gsi_base;
882 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
883
884 /* Evaluate _STA if present */
885 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
886 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
887 return AE_CTRL_DEPTH;
888
889 /* Scan only PCI bus scope */
890 status = acpi_get_handle(handle, "_HID", &tmp);
891 if (ACPI_SUCCESS(status))
892 return AE_CTRL_DEPTH;
893
894 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700895 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700896
897 acpi_unregister_ioapic(handle, gsi_base);
898
899 spin_lock(&ioapic_list_lock);
900 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
901 if (pos->gsi_base != gsi_base)
902 continue;
903 ioapic = pos;
904 list_del(&ioapic->list);
905 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700906 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700907 spin_unlock(&ioapic_list_lock);
908
909 if (!ioapic)
910 return AE_OK;
911
912 pci_release_region(ioapic->dev, 0);
913 pci_disable_device(ioapic->dev);
914 pci_dev_put(ioapic->dev);
915 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700916
917 return AE_OK;
918}
919
920static int acpiphp_configure_ioapics(acpi_handle handle)
921{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700922 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700923 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
924 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
925 return 0;
926}
927
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700928static int acpiphp_unconfigure_ioapics(acpi_handle handle)
929{
930 ioapic_remove(handle, 0, NULL, NULL);
931 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
932 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
933 return 0;
934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936static int power_on_slot(struct acpiphp_slot *slot)
937{
938 acpi_status status;
939 struct acpiphp_func *func;
940 struct list_head *l;
941 int retval = 0;
942
943 /* if already enabled, just skip */
944 if (slot->flags & SLOT_POWEREDON)
945 goto err_exit;
946
947 list_for_each (l, &slot->funcs) {
948 func = list_entry(l, struct acpiphp_func, sibling);
949
950 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800951 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
953 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800954 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 retval = -1;
956 goto err_exit;
957 } else
958 break;
959 }
960 }
961
962 /* TBD: evaluate _STA to check if the slot is enabled */
963
964 slot->flags |= SLOT_POWEREDON;
965
966 err_exit:
967 return retval;
968}
969
970
971static int power_off_slot(struct acpiphp_slot *slot)
972{
973 acpi_status status;
974 struct acpiphp_func *func;
975 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 int retval = 0;
978
979 /* if already disabled, just skip */
980 if ((slot->flags & SLOT_POWEREDON) == 0)
981 goto err_exit;
982
983 list_for_each (l, &slot->funcs) {
984 func = list_entry(l, struct acpiphp_func, sibling);
985
Rajesh Shah2f523b12005-04-28 00:25:55 -0700986 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
988 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800989 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 retval = -1;
991 goto err_exit;
992 } else
993 break;
994 }
995 }
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /* TBD: evaluate _STA to check if the slot is disabled */
998
999 slot->flags &= (~SLOT_POWEREDON);
1000
1001 err_exit:
1002 return retval;
1003}
1004
1005
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001006
1007/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001008 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001009 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001010 */
1011static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
1012{
1013 struct list_head *tmp;
1014 unsigned char max, n;
1015
1016 /*
1017 * pci_bus_max_busnr will return the highest
1018 * reserved busnr for all these children.
1019 * that is equivalent to the bus->subordinate
1020 * value. We don't want to use the parent's
1021 * bus->subordinate value because it could have
1022 * padding in it.
1023 */
1024 max = bus->secondary;
1025
1026 list_for_each(tmp, &bus->children) {
1027 n = pci_bus_max_busnr(pci_bus_b(tmp));
1028 if (n > max)
1029 max = n;
1030 }
1031 return max;
1032}
1033
1034
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001035/**
1036 * acpiphp_bus_add - add a new bus to acpi subsystem
1037 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001038 */
1039static int acpiphp_bus_add(struct acpiphp_func *func)
1040{
1041 acpi_handle phandle;
1042 struct acpi_device *device, *pdevice;
1043 int ret_val;
1044
1045 acpi_get_parent(func->handle, &phandle);
1046 if (acpi_bus_get_device(phandle, &pdevice)) {
1047 dbg("no parent device, assuming NULL\n");
1048 pdevice = NULL;
1049 }
Kristen Accardi20416ea2006-02-23 17:56:03 -08001050 if (!acpi_bus_get_device(func->handle, &device)) {
1051 dbg("bus exists... trim\n");
1052 /* this shouldn't be in here, so remove
1053 * the bus then re-add it...
1054 */
1055 ret_val = acpi_bus_trim(device, 1);
1056 dbg("acpi_bus_trim return %x\n", ret_val);
1057 }
1058
1059 ret_val = acpi_bus_add(&device, pdevice, func->handle,
1060 ACPI_BUS_TYPE_DEVICE);
1061 if (ret_val) {
1062 dbg("error adding bus, %x\n",
1063 -ret_val);
1064 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001065 }
1066 /*
1067 * try to start anyway. We could have failed to add
1068 * simply because this bus had previously been added
1069 * on another add. Don't bother with the return value
1070 * we just keep going.
1071 */
1072 ret_val = acpi_bus_start(device);
1073
1074acpiphp_bus_add_out:
1075 return ret_val;
1076}
1077
1078
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001079/**
1080 * acpiphp_bus_trim - trim a bus from acpi subsystem
1081 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001082 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -07001083static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001084{
1085 struct acpi_device *device;
1086 int retval;
1087
1088 retval = acpi_bus_get_device(handle, &device);
1089 if (retval) {
1090 dbg("acpi_device not found\n");
1091 return retval;
1092 }
1093
1094 retval = acpi_bus_trim(device, 1);
1095 if (retval)
1096 err("cannot remove from acpi list\n");
1097
1098 return retval;
1099}
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/**
1102 * enable_device - enable, configure a slot
1103 * @slot: slot to be enabled
1104 *
1105 * This function should be called per *physical slot*,
1106 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +01001108static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001111 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct list_head *l;
1113 struct acpiphp_func *func;
1114 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001115 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001116 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (slot->flags & SLOT_ENABLED)
1119 goto err_exit;
1120
1121 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001122 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (dev) {
1124 /* This case shouldn't happen */
1125 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001126 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 retval = -1;
1128 goto err_exit;
1129 }
1130
Rajesh Shah42f49a62005-04-28 00:25:53 -07001131 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1132 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 err("No new device found\n");
1134 retval = -1;
1135 goto err_exit;
1136 }
1137
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001138 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001139 for (pass = 0; pass < 2; pass++) {
1140 list_for_each_entry(dev, &bus->devices, bus_list) {
1141 if (PCI_SLOT(dev->devfn) != slot->device)
1142 continue;
1143 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001144 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001145 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001146 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001147 pci_bus_size_bridges(dev->subordinate);
1148 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001152 list_for_each (l, &slot->funcs) {
1153 func = list_entry(l, struct acpiphp_func, sibling);
1154 acpiphp_bus_add(func);
1155 }
1156
Rajesh Shah42f49a62005-04-28 00:25:53 -07001157 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001158 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001159 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001160 list_for_each_entry(func, &slot->funcs, sibling)
1161 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001162 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001163 pci_bus_add_devices(bus);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* associate pci_dev to our representation */
1166 list_for_each (l, &slot->funcs) {
1167 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001168 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 func->function));
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001170 if (!func->pci_dev)
1171 continue;
1172
1173 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1174 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1175 continue;
1176
1177 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1178 if (ACPI_FAILURE(status))
1179 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1180 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
1183 slot->flags |= SLOT_ENABLED;
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 err_exit:
1186 return retval;
1187}
1188
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001189static void disable_bridges(struct pci_bus *bus)
1190{
1191 struct pci_dev *dev;
1192 list_for_each_entry(dev, &bus->devices, bus_list) {
1193 if (dev->subordinate) {
1194 disable_bridges(dev->subordinate);
1195 pci_disable_device(dev);
1196 }
1197 }
1198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200/**
1201 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001202 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
1204static int disable_device(struct acpiphp_slot *slot)
1205{
1206 int retval = 0;
1207 struct acpiphp_func *func;
1208 struct list_head *l;
1209
1210 /* is this slot already disabled? */
1211 if (!(slot->flags & SLOT_ENABLED))
1212 goto err_exit;
1213
1214 list_for_each (l, &slot->funcs) {
1215 func = list_entry(l, struct acpiphp_func, sibling);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001216
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001217 if (func->bridge) {
1218 /* cleanup p2p bridges under this P2P bridge */
1219 cleanup_p2p_bridge(func->bridge->handle,
1220 (u32)1, NULL, NULL);
1221 func->bridge = NULL;
1222 }
1223
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001224 if (func->pci_dev) {
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001225 pci_stop_bus_device(func->pci_dev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001226 if (func->pci_dev->subordinate) {
1227 disable_bridges(func->pci_dev->subordinate);
1228 pci_disable_device(func->pci_dev);
1229 }
1230 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001231 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001232
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001233 list_for_each (l, &slot->funcs) {
1234 func = list_entry(l, struct acpiphp_func, sibling);
1235
1236 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001237 acpiphp_bus_trim(func->handle);
1238 /* try to remove anyway.
1239 * acpiphp_bus_add might have been failed */
1240
Rajesh Shah42f49a62005-04-28 00:25:53 -07001241 if (!func->pci_dev)
1242 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Rajesh Shah42f49a62005-04-28 00:25:53 -07001244 pci_remove_bus_device(func->pci_dev);
1245 pci_dev_put(func->pci_dev);
1246 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
1249 slot->flags &= (~SLOT_ENABLED);
1250
1251 err_exit:
1252 return retval;
1253}
1254
1255
1256/**
1257 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -08001258 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001260 * If a slot has _STA for each function and if any one of them
1261 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001263 * If a slot doesn't have _STA and if any one of its functions'
1264 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001266 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
1268static unsigned int get_slot_status(struct acpiphp_slot *slot)
1269{
1270 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001271 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 u32 dvid;
1273 struct list_head *l;
1274 struct acpiphp_func *func;
1275
1276 list_for_each (l, &slot->funcs) {
1277 func = list_entry(l, struct acpiphp_func, sibling);
1278
1279 if (func->flags & FUNC_HAS_STA) {
1280 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1281 if (ACPI_SUCCESS(status) && sta)
1282 break;
1283 } else {
1284 pci_bus_read_config_dword(slot->bridge->pci_bus,
1285 PCI_DEVFN(slot->device,
1286 func->function),
1287 PCI_VENDOR_ID, &dvid);
1288 if (dvid != 0xffffffff) {
1289 sta = ACPI_STA_ALL;
1290 break;
1291 }
1292 }
1293 }
1294
1295 return (unsigned int)sta;
1296}
1297
1298/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001299 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001300 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001301 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001302int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001303{
1304 acpi_status status;
1305 struct acpiphp_func *func;
1306 struct list_head *l;
1307 struct acpi_object_list arg_list;
1308 union acpi_object arg;
1309
1310 list_for_each (l, &slot->funcs) {
1311 func = list_entry(l, struct acpiphp_func, sibling);
1312
1313 /* We don't want to call _EJ0 on non-existing functions. */
1314 if ((func->flags & FUNC_HAS_EJ0)) {
1315 /* _EJ0 method take one argument */
1316 arg_list.count = 1;
1317 arg_list.pointer = &arg;
1318 arg.type = ACPI_TYPE_INTEGER;
1319 arg.integer.value = 1;
1320
1321 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1322 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001323 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001324 return -1;
1325 } else
1326 break;
1327 }
1328 }
1329 return 0;
1330}
1331
1332/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001334 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 *
1336 * Iterate over all slots under this bridge and make sure that if a
1337 * card is present they are enabled, and if not they are disabled.
1338 */
1339static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1340{
1341 struct acpiphp_slot *slot;
1342 int retval = 0;
1343 int enabled, disabled;
1344
1345 enabled = disabled = 0;
1346
1347 for (slot = bridge->slots; slot; slot = slot->next) {
1348 unsigned int status = get_slot_status(slot);
1349 if (slot->flags & SLOT_ENABLED) {
1350 if (status == ACPI_STA_ALL)
1351 continue;
1352 retval = acpiphp_disable_slot(slot);
1353 if (retval) {
1354 err("Error occurred in disabling\n");
1355 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001356 } else {
1357 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 disabled++;
1360 } else {
1361 if (status != ACPI_STA_ALL)
1362 continue;
1363 retval = acpiphp_enable_slot(slot);
1364 if (retval) {
1365 err("Error occurred in enabling\n");
1366 goto err_exit;
1367 }
1368 enabled++;
1369 }
1370 }
1371
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001372 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 err_exit:
1375 return retval;
1376}
1377
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001378static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1379{
1380 u16 pci_cmd, pci_bctl;
1381 struct pci_dev *cdev;
1382
1383 /* Program hpp values for this device */
1384 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1385 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1386 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1387 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001388
Gary Hade9ef22412007-07-05 11:10:47 -07001389 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1390 return;
1391
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001392 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001393 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001394 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001395 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001396 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001397 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001398 pci_cmd |= PCI_COMMAND_SERR;
1399 else
1400 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001401 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001402 pci_cmd |= PCI_COMMAND_PARITY;
1403 else
1404 pci_cmd &= ~PCI_COMMAND_PARITY;
1405 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1406
1407 /* Program bridge control value and child devices */
1408 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1409 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001410 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001411 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001412 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001413 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1414 else
1415 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001416 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001417 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1418 else
1419 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1420 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1421 if (dev->subordinate) {
1422 list_for_each_entry(cdev, &dev->subordinate->devices,
1423 bus_list)
1424 program_hpp(cdev, bridge);
1425 }
1426 }
1427}
1428
1429static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1430{
1431 struct acpiphp_bridge bridge;
1432 struct pci_dev *dev;
1433
1434 memset(&bridge, 0, sizeof(bridge));
1435 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001436 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001437 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001438 decode_hpp(&bridge);
1439 list_for_each_entry(dev, &bus->devices, bus_list)
1440 program_hpp(dev, &bridge);
1441
1442}
1443
1444/*
1445 * Remove devices for which we could not assign resources, call
1446 * arch specific code to fix-up the bus
1447 */
1448static void acpiphp_sanitize_bus(struct pci_bus *bus)
1449{
1450 struct pci_dev *dev;
1451 int i;
1452 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1453
1454 list_for_each_entry(dev, &bus->devices, bus_list) {
1455 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1456 struct resource *res = &dev->resource[i];
1457 if ((res->flags & type_mask) && !res->start &&
1458 res->end) {
1459 /* Could not assign a required resources
1460 * for this device, remove it */
1461 pci_remove_bus_device(dev);
1462 break;
1463 }
1464 }
1465 }
1466}
1467
1468/* Program resources in newly inserted bridge */
1469static int acpiphp_configure_bridge (acpi_handle handle)
1470{
1471 struct acpi_pci_id pci_id;
1472 struct pci_bus *bus;
1473
1474 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1475 err("cannot get PCI domain and bus number for bridge\n");
1476 return -EINVAL;
1477 }
1478 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1479 if (!bus) {
1480 err("cannot find bus %d:%d\n",
1481 pci_id.segment, pci_id.bus);
1482 return -EINVAL;
1483 }
1484
1485 pci_bus_size_bridges(bus);
1486 pci_bus_assign_resources(bus);
1487 acpiphp_sanitize_bus(bus);
1488 acpiphp_set_hpp_values(handle, bus);
1489 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001490 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001491 return 0;
1492}
1493
1494static void handle_bridge_insertion(acpi_handle handle, u32 type)
1495{
1496 struct acpi_device *device, *pdevice;
1497 acpi_handle phandle;
1498
1499 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1500 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1501 err("unexpected notification type %d\n", type);
1502 return;
1503 }
1504
1505 acpi_get_parent(handle, &phandle);
1506 if (acpi_bus_get_device(phandle, &pdevice)) {
1507 dbg("no parent device, assuming NULL\n");
1508 pdevice = NULL;
1509 }
1510 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1511 err("cannot add bridge to acpi list\n");
1512 return;
1513 }
1514 if (!acpiphp_configure_bridge(handle) &&
1515 !acpi_bus_start(device))
1516 add_bridge(handle);
1517 else
1518 err("cannot configure and start bridge\n");
1519
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/*
1523 * ACPI event handlers
1524 */
1525
Gary Hade0bbd6422007-07-05 11:10:48 -07001526static acpi_status
1527count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1528{
1529 int *count = (int *)context;
1530 struct acpiphp_bridge *bridge;
1531
1532 bridge = acpiphp_handle_to_bridge(handle);
1533 if (bridge)
1534 (*count)++;
1535 return AE_OK ;
1536}
1537
1538static acpi_status
1539check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1540{
1541 struct acpiphp_bridge *bridge;
1542 char objname[64];
1543 struct acpi_buffer buffer = { .length = sizeof(objname),
1544 .pointer = objname };
1545
1546 bridge = acpiphp_handle_to_bridge(handle);
1547 if (bridge) {
1548 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1549 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001550 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001551 acpiphp_check_bridge(bridge);
1552 }
1553 return AE_OK ;
1554}
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556/**
1557 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * @handle: Notify()'ed acpi_handle
1559 * @type: Notify code
1560 * @context: pointer to acpiphp_bridge structure
1561 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001562 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 */
1564static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1565{
1566 struct acpiphp_bridge *bridge;
1567 char objname[64];
1568 struct acpi_buffer buffer = { .length = sizeof(objname),
1569 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001570 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001571 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001573 if (acpi_bus_get_device(handle, &device)) {
1574 /* This bridge must have just been physically inserted */
1575 handle_bridge_insertion(handle, type);
1576 return;
1577 }
1578
1579 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001580 if (type == ACPI_NOTIFY_BUS_CHECK) {
1581 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1582 count_sub_bridges, &num_sub_bridges, NULL);
1583 }
1584
1585 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001586 err("cannot get bridge info\n");
1587 return;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1591
1592 switch (type) {
1593 case ACPI_NOTIFY_BUS_CHECK:
1594 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001595 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001596 if (bridge) {
1597 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001598 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001599 acpiphp_check_bridge(bridge);
1600 }
1601 if (num_sub_bridges)
1602 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1603 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 break;
1605
1606 case ACPI_NOTIFY_DEVICE_CHECK:
1607 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001608 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 acpiphp_check_bridge(bridge);
1610 break;
1611
1612 case ACPI_NOTIFY_DEVICE_WAKE:
1613 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001614 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 break;
1616
1617 case ACPI_NOTIFY_EJECT_REQUEST:
1618 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001619 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001620 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1621 (bridge->flags & BRIDGE_HAS_EJ0)) {
1622 struct acpiphp_slot *slot;
1623 slot = bridge->func->slot;
1624 if (!acpiphp_disable_slot(slot))
1625 acpiphp_eject_slot(slot);
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 break;
1628
1629 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1630 printk(KERN_ERR "Device %s cannot be configured due"
1631 " to a frequency mismatch\n", objname);
1632 break;
1633
1634 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1635 printk(KERN_ERR "Device %s cannot be configured due"
1636 " to a bus mode mismatch\n", objname);
1637 break;
1638
1639 case ACPI_NOTIFY_POWER_FAULT:
1640 printk(KERN_ERR "Device %s has suffered a power fault\n",
1641 objname);
1642 break;
1643
1644 default:
1645 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1646 break;
1647 }
1648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650/**
1651 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * @handle: Notify()'ed acpi_handle
1653 * @type: Notify code
1654 * @context: pointer to acpiphp_func structure
1655 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001656 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 */
Len Brown2b85e132006-06-27 01:50:14 -04001658static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 struct acpiphp_func *func;
1661 char objname[64];
1662 struct acpi_buffer buffer = { .length = sizeof(objname),
1663 .pointer = objname };
1664
1665 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1666
1667 func = (struct acpiphp_func *)context;
1668
1669 switch (type) {
1670 case ACPI_NOTIFY_BUS_CHECK:
1671 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001672 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 acpiphp_enable_slot(func->slot);
1674 break;
1675
1676 case ACPI_NOTIFY_DEVICE_CHECK:
1677 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001678 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 acpiphp_check_bridge(func->slot->bridge);
1680 break;
1681
1682 case ACPI_NOTIFY_DEVICE_WAKE:
1683 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001684 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 break;
1686
1687 case ACPI_NOTIFY_EJECT_REQUEST:
1688 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001689 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001690 if (!(acpiphp_disable_slot(func->slot)))
1691 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693
1694 default:
1695 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1696 break;
1697 }
1698}
1699
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001700
1701static acpi_status
1702find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1703{
1704 int *count = (int *)context;
1705
Kristen Accardi783c49f2006-03-03 10:16:05 -08001706 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001707 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1708 handle_hotplug_event_bridge, NULL);
1709 (*count)++;
1710 }
1711 return AE_OK ;
1712}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714static struct acpi_pci_driver acpi_pci_hp_driver = {
1715 .add = add_bridge,
1716 .remove = remove_bridge,
1717};
1718
1719/**
1720 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
1722int __init acpiphp_glue_init(void)
1723{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001724 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001726 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1727 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 if (num <= 0)
1730 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001731 else
1732 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 return 0;
1735}
1736
1737
1738/**
1739 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1740 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001741 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001743void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1746}
1747
1748
1749/**
1750 * acpiphp_get_num_slots - count number of slots in a system
1751 */
1752int __init acpiphp_get_num_slots(void)
1753{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001755 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Akinobu Mita467c4422006-10-30 13:07:58 -08001757 list_for_each_entry (bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001758 dbg("Bus %04x:%02x has %d slot%s\n",
1759 pci_domain_nr(bridge->pci_bus),
1760 bridge->pci_bus->number, bridge->nr_slots,
1761 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 num_slots += bridge->nr_slots;
1763 }
1764
Rajesh Shah42f49a62005-04-28 00:25:53 -07001765 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return num_slots;
1767}
1768
1769
1770#if 0
1771/**
1772 * acpiphp_for_each_slot - call function for each slot
1773 * @fn: callback function
1774 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 */
1776static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1777{
1778 struct list_head *node;
1779 struct acpiphp_bridge *bridge;
1780 struct acpiphp_slot *slot;
1781 int retval = 0;
1782
1783 list_for_each (node, &bridge_list) {
1784 bridge = (struct acpiphp_bridge *)node;
1785 for (slot = bridge->slots; slot; slot = slot->next) {
1786 retval = fn(slot, data);
1787 if (!retval)
1788 goto err_exit;
1789 }
1790 }
1791
1792 err_exit:
1793 return retval;
1794}
1795#endif
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798/**
1799 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001800 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
1802int acpiphp_enable_slot(struct acpiphp_slot *slot)
1803{
1804 int retval;
1805
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001806 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 /* wake up all functions */
1809 retval = power_on_slot(slot);
1810 if (retval)
1811 goto err_exit;
1812
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001813 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* configure all functions */
1815 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001816 if (retval)
1817 power_off_slot(slot);
1818 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001819 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001820 power_off_slot(slot);
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001824 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return retval;
1826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/**
1829 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001830 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 */
1832int acpiphp_disable_slot(struct acpiphp_slot *slot)
1833{
1834 int retval = 0;
1835
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001836 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /* unconfigure all functions */
1839 retval = disable_device(slot);
1840 if (retval)
1841 goto err_exit;
1842
1843 /* power off all functions */
1844 retval = power_off_slot(slot);
1845 if (retval)
1846 goto err_exit;
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001849 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return retval;
1851}
1852
1853
1854/*
1855 * slot enabled: 1
1856 * slot disabled: 0
1857 */
1858u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1859{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001860 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
1863
1864/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001865 * latch open: 1
1866 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1869{
1870 unsigned int sta;
1871
1872 sta = get_slot_status(slot);
1873
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001874 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
1877
1878/*
1879 * adapter presence : 1
1880 * absence : 0
1881 */
1882u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1883{
1884 unsigned int sta;
1885
1886 sta = get_slot_status(slot);
1887
1888 return (sta == 0) ? 0 : 1;
1889}