blob: 9342c848db29f8888b82000c6defcf07a435b968 [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
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/* callback routine to register each ACPI PCI slot object */
175static acpi_status
176register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
177{
178 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
179 struct acpiphp_slot *slot;
180 struct acpiphp_func *newfunc;
181 acpi_handle tmp;
182 acpi_status status = AE_OK;
183 unsigned long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800184 int device, function, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
187
188 if (ACPI_FAILURE(status))
189 return AE_OK;
190
191 status = acpi_get_handle(handle, "_EJ0", &tmp);
192
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400193 if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return AE_OK;
195
196 device = (adr >> 16) & 0xffff;
197 function = adr & 0xffff;
198
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100199 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!newfunc)
201 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 INIT_LIST_HEAD(&newfunc->sibling);
204 newfunc->handle = handle;
205 newfunc->function = function;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800206 if (ACPI_SUCCESS(status))
207 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
210 newfunc->flags |= FUNC_HAS_STA;
211
212 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
213 newfunc->flags |= FUNC_HAS_PS0;
214
215 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
216 newfunc->flags |= FUNC_HAS_PS3;
217
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400218 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800219 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400222 if (ACPI_FAILURE(status)) {
223 /*
224 * use the count of the number of slots we've found
225 * for the number of the slot
226 */
227 sun = bridge->nr_slots+1;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* search for objects that share the same slot */
231 for (slot = bridge->slots; slot; slot = slot->next)
232 if (slot->device == device) {
233 if (slot->sun != sun)
234 warn("sibling found, but _SUN doesn't match!\n");
235 break;
236 }
237
238 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100239 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!slot) {
241 kfree(newfunc);
242 return AE_NO_MEMORY;
243 }
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 slot->device = device;
247 slot->sun = sun;
248 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100249 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 slot->next = bridge->slots;
252 bridge->slots = slot;
253
254 bridge->nr_slots++;
255
Rajesh Shah42f49a62005-04-28 00:25:53 -0700256 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
257 slot->sun, pci_domain_nr(bridge->pci_bus),
258 bridge->pci_bus->number, slot->device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800259 retval = acpiphp_register_hotplug_slot(slot);
260 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600261 if (retval == -EBUSY)
262 warn("Slot %d already registered by another "
263 "hotplug driver\n", slot->sun);
264 else
265 warn("acpiphp_register_hotplug_slot failed "
266 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800267 goto err_exit;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 newfunc->slot = slot;
272 list_add_tail(&newfunc->sibling, &slot->funcs);
273
274 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700275 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 PCI_DEVFN(device, function));
277 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
279 }
280
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400281 if (is_dock_device(handle)) {
282 /* we don't want to call this device's _EJ0
283 * because we want the dock notify handler
284 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800285 */
286 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400287 if (register_hotplug_dock_device(handle,
288 handle_hotplug_event_func, newfunc))
289 dbg("failed to register dock device\n");
290
291 /* we need to be notified when dock events happen
292 * outside of the hotplug operation, since we may
293 * need to do fixups before we can hotplug.
294 */
295 newfunc->nb.notifier_call = post_dock_fixups;
296 if (register_dock_notifier(&newfunc->nb))
297 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800301 if (!(newfunc->flags & FUNC_HAS_DCK)) {
302 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 ACPI_SYSTEM_NOTIFY,
304 handle_hotplug_event_func,
305 newfunc);
306
Kristen Accardi20416ea2006-02-23 17:56:03 -0800307 if (ACPI_FAILURE(status))
308 err("failed to register interrupt notify handler\n");
309 } else
310 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Kristen Accardi20416ea2006-02-23 17:56:03 -0800312 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800313
314 err_exit:
315 bridge->nr_slots--;
316 bridge->slots = slot->next;
317 kfree(slot);
318 kfree(newfunc);
319
320 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323
324/* see if it's worth looking at this bridge */
325static int detect_ejectable_slots(acpi_handle *bridge_handle)
326{
327 acpi_status status;
328 int count;
329
330 count = 0;
331
332 /* only check slots defined directly below bridge object */
333 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
334 is_ejectable_slot, (void *)&count, NULL);
335
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400336 /*
337 * we also need to add this bridge if there is a dock bridge or
338 * other pci device on a dock station (removable)
339 */
340 if (!count)
341 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
342 (u32)1, is_pci_dock_device, (void *)&count,
343 NULL);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return count;
346}
347
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* decode ACPI 2.0 _HPP hot plug parameters */
350static void decode_hpp(struct acpiphp_bridge *bridge)
351{
352 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900354 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900355 if (ACPI_FAILURE(status) ||
356 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800357 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900358 printk(KERN_WARNING
359 "%s: Could not get hotplug parameters. Use defaults\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800360 __func__);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900361 bridge->hpp.t0 = &bridge->hpp.type0_data;
362 bridge->hpp.t0->revision = 0;
363 bridge->hpp.t0->cache_line_size = 0x10;
364 bridge->hpp.t0->latency_timer = 0x40;
365 bridge->hpp.t0->enable_serr = 0;
366 bridge->hpp.t0->enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370
Kristen Accardi783c49f2006-03-03 10:16:05 -0800371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
373static void init_bridge_misc(struct acpiphp_bridge *bridge)
374{
375 acpi_status status;
376
377 /* decode ACPI 2.0 _HPP (hot plug parameters) */
378 decode_hpp(bridge);
379
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800380 /* must be added to the list prior to calling register_slot */
381 list_add(&bridge->list, &bridge_list);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* register all slot objects under this bridge */
384 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
385 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800386 if (ACPI_FAILURE(status)) {
387 list_del(&bridge->list);
388 return;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700392 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900393 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
394 status = acpi_remove_notify_handler(bridge->func->handle,
395 ACPI_SYSTEM_NOTIFY,
396 handle_hotplug_event_func);
397 if (ACPI_FAILURE(status))
398 err("failed to remove notify handler\n");
399 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700400 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ACPI_SYSTEM_NOTIFY,
402 handle_hotplug_event_bridge,
403 bridge);
404
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700405 if (ACPI_FAILURE(status)) {
406 err("failed to register interrupt notify handler\n");
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900412/* find acpiphp_func from acpiphp_bridge */
413static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
414{
415 struct list_head *node, *l;
416 struct acpiphp_bridge *bridge;
417 struct acpiphp_slot *slot;
418 struct acpiphp_func *func;
419
420 list_for_each(node, &bridge_list) {
421 bridge = list_entry(node, struct acpiphp_bridge, list);
422 for (slot = bridge->slots; slot; slot = slot->next) {
423 list_for_each(l, &slot->funcs) {
424 func = list_entry(l, struct acpiphp_func,
425 sibling);
426 if (func->handle == handle)
427 return func;
428 }
429 }
430 }
431
432 return NULL;
433}
434
435
436static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
437{
438 acpi_handle dummy_handle;
439
440 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
441 "_STA", &dummy_handle)))
442 bridge->flags |= BRIDGE_HAS_STA;
443
444 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
445 "_EJ0", &dummy_handle)))
446 bridge->flags |= BRIDGE_HAS_EJ0;
447
448 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
449 "_PS0", &dummy_handle)))
450 bridge->flags |= BRIDGE_HAS_PS0;
451
452 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
453 "_PS3", &dummy_handle)))
454 bridge->flags |= BRIDGE_HAS_PS3;
455
456 /* is this ejectable p2p bridge? */
457 if (bridge->flags & BRIDGE_HAS_EJ0) {
458 struct acpiphp_func *func;
459
460 dbg("found ejectable p2p bridge\n");
461
462 /* make link between PCI bridge and PCI function */
463 func = acpiphp_bridge_handle_to_function(bridge->handle);
464 if (!func)
465 return;
466 bridge->func = func;
467 func->bridge = bridge;
468 }
469}
470
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700473static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct acpiphp_bridge *bridge;
476
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100477 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (bridge == NULL)
479 return;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 bridge->type = BRIDGE_TYPE_HOST;
482 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Rajesh Shah42f49a62005-04-28 00:25:53 -0700484 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 spin_lock_init(&bridge->res_lock);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 init_bridge_misc(bridge);
489}
490
491
492/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700493static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100497 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (bridge == NULL) {
499 err("out of memory\n");
500 return;
501 }
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 bridge->type = BRIDGE_TYPE_P2P;
504 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900505 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Rajesh Shah42f49a62005-04-28 00:25:53 -0700507 bridge->pci_dev = pci_dev_get(pci_dev);
508 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!bridge->pci_bus) {
510 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700511 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 spin_lock_init(&bridge->res_lock);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700517 return;
518 err:
519 pci_dev_put(pci_dev);
520 kfree(bridge);
521 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524
525/* callback routine to find P2P bridges */
526static acpi_status
527find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
528{
529 acpi_status status;
530 acpi_handle dummy_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 unsigned long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700532 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700534 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
537 if (ACPI_FAILURE(status))
538 return AE_OK; /* continue */
539
540 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
541 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800542 dbg("%s: _ADR evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return AE_OK;
544 }
545
546 device = (tmp >> 16) & 0xffff;
547 function = tmp & 0xffff;
548
Rajesh Shah42f49a62005-04-28 00:25:53 -0700549 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Rajesh Shah42f49a62005-04-28 00:25:53 -0700551 if (!dev || !dev->subordinate)
552 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* check if this bridge has ejectable slots */
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400555 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700557 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900560 /* search P2P bridges under this p2p bridge */
561 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
562 find_p2p_bridge, dev->subordinate, NULL);
563 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900564 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900565
Rajesh Shah42f49a62005-04-28 00:25:53 -0700566 out:
567 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return AE_OK;
569}
570
571
572/* find hot-pluggable slots, and then find P2P bridge */
573static int add_bridge(acpi_handle handle)
574{
575 acpi_status status;
576 unsigned long tmp;
577 int seg, bus;
578 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700579 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* if the bridge doesn't have _STA, we assume it is always there */
582 status = acpi_get_handle(handle, "_STA", &dummy_handle);
583 if (ACPI_SUCCESS(status)) {
584 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
585 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800586 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
588 }
589 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
590 /* don't register this object */
591 return 0;
592 }
593
594 /* get PCI segment number */
595 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
596
597 seg = ACPI_SUCCESS(status) ? tmp : 0;
598
599 /* get PCI bus number */
600 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
601
602 if (ACPI_SUCCESS(status)) {
603 bus = tmp;
604 } else {
605 warn("can't get bus number, assuming 0\n");
606 bus = 0;
607 }
608
Rajesh Shah42f49a62005-04-28 00:25:53 -0700609 pci_bus = pci_find_bus(seg, bus);
610 if (!pci_bus) {
611 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return 0;
613 }
614
Rajesh Shah42f49a62005-04-28 00:25:53 -0700615 /* check if this bridge has ejectable slots */
616 if (detect_ejectable_slots(handle) > 0) {
617 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
618 add_host_bridge(handle, pci_bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* search P2P bridges under this host bridge */
622 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700623 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900626 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 return 0;
629}
630
Rajesh Shah42f49a62005-04-28 00:25:53 -0700631static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
632{
633 struct list_head *head;
634 list_for_each(head, &bridge_list) {
635 struct acpiphp_bridge *bridge = list_entry(head,
636 struct acpiphp_bridge, list);
637 if (bridge->handle == handle)
638 return bridge;
639 }
640
641 return NULL;
642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Rajesh Shah364d5092005-04-28 00:25:54 -0700644static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700646 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700647 struct acpiphp_slot *slot;
648 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700649 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700650
651 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
652 handle_hotplug_event_bridge);
653 if (ACPI_FAILURE(status))
654 err("failed to remove notify handler\n");
655
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900656 if ((bridge->type != BRIDGE_TYPE_HOST) &&
657 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
658 status = acpi_install_notify_handler(bridge->func->handle,
659 ACPI_SYSTEM_NOTIFY,
660 handle_hotplug_event_func,
661 bridge->func);
662 if (ACPI_FAILURE(status))
663 err("failed to install interrupt notify handler\n");
664 }
665
Rajesh Shah42f49a62005-04-28 00:25:53 -0700666 slot = bridge->slots;
667 while (slot) {
668 struct acpiphp_slot *next = slot->next;
669 list_for_each_safe (list, tmp, &slot->funcs) {
670 struct acpiphp_func *func;
671 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400672 if (is_dock_device(func->handle)) {
673 unregister_hotplug_dock_device(func->handle);
674 unregister_dock_notifier(&func->nb);
675 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800676 if (!(func->flags & FUNC_HAS_DCK)) {
677 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700678 ACPI_SYSTEM_NOTIFY,
679 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800680 if (ACPI_FAILURE(status))
681 err("failed to remove notify handler\n");
682 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700683 pci_dev_put(func->pci_dev);
684 list_del(list);
685 kfree(func);
686 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800687 acpiphp_unregister_hotplug_slot(slot);
688 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700689 kfree(slot);
690 slot = next;
691 }
692
693 pci_dev_put(bridge->pci_dev);
694 list_del(&bridge->list);
695 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Rajesh Shah364d5092005-04-28 00:25:54 -0700698static acpi_status
699cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
700{
701 struct acpiphp_bridge *bridge;
702
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900703 /* cleanup p2p bridges under this P2P bridge
704 in a depth-first manner */
705 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
706 cleanup_p2p_bridge, NULL, NULL);
707
Rajesh Shah364d5092005-04-28 00:25:54 -0700708 if (!(bridge = acpiphp_handle_to_bridge(handle)))
709 return AE_OK;
710 cleanup_bridge(bridge);
711 return AE_OK;
712}
713
714static void remove_bridge(acpi_handle handle)
715{
716 struct acpiphp_bridge *bridge;
717
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900718 /* cleanup p2p bridges under this host bridge
719 in a depth-first manner */
720 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
721 (u32)1, cleanup_p2p_bridge, NULL, NULL);
722
Rajesh Shah364d5092005-04-28 00:25:54 -0700723 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900724 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700725 cleanup_bridge(bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700726}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700728static struct pci_dev * get_apic_pci_info(acpi_handle handle)
729{
730 struct acpi_pci_id id;
731 struct pci_bus *bus;
732 struct pci_dev *dev;
733
734 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
735 return NULL;
736
737 bus = pci_find_bus(id.segment, id.bus);
738 if (!bus)
739 return NULL;
740
741 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
742 if (!dev)
743 return NULL;
744
745 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
746 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
747 {
748 pci_dev_put(dev);
749 return NULL;
750 }
751
752 return dev;
753}
754
755static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
756{
757 acpi_status status;
758 int result = -1;
759 unsigned long gsb;
760 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
761 union acpi_object *obj;
762 void *table;
763
764 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
765 if (ACPI_SUCCESS(status)) {
766 *gsi_base = (u32)gsb;
767 return 0;
768 }
769
770 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
771 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
772 return -1;
773
774 obj = buffer.pointer;
775 if (obj->type != ACPI_TYPE_BUFFER)
776 goto out;
777
778 table = obj->buffer.pointer;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300779 switch (((struct acpi_subtable_header *)table)->type) {
780 case ACPI_MADT_TYPE_IO_SAPIC:
781 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700782 result = 0;
783 break;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300784 case ACPI_MADT_TYPE_IO_APIC:
785 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700786 result = 0;
787 break;
788 default:
789 break;
790 }
791 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700792 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700793 return result;
794}
795
796static acpi_status
797ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
798{
799 acpi_status status;
800 unsigned long sta;
801 acpi_handle tmp;
802 struct pci_dev *pdev;
803 u32 gsi_base;
804 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700805 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700806
807 /* Evaluate _STA if present */
808 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
809 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
810 return AE_CTRL_DEPTH;
811
812 /* Scan only PCI bus scope */
813 status = acpi_get_handle(handle, "_HID", &tmp);
814 if (ACPI_SUCCESS(status))
815 return AE_CTRL_DEPTH;
816
817 if (get_gsi_base(handle, &gsi_base))
818 return AE_OK;
819
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700820 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
821 if (!ioapic)
822 return AE_NO_MEMORY;
823
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700824 pdev = get_apic_pci_info(handle);
825 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700826 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700827
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700828 if (pci_enable_device(pdev))
829 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700830
831 pci_set_master(pdev);
832
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700833 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
834 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700835
836 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700837 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
838 goto exit_pci_release_region;
839
840 ioapic->gsi_base = gsi_base;
841 ioapic->dev = pdev;
842 spin_lock(&ioapic_list_lock);
843 list_add_tail(&ioapic->list, &ioapic_list);
844 spin_unlock(&ioapic_list_lock);
845
846 return AE_OK;
847
848 exit_pci_release_region:
849 pci_release_region(pdev, 0);
850 exit_pci_disable_device:
851 pci_disable_device(pdev);
852 exit_pci_dev_put:
853 pci_dev_put(pdev);
854 exit_kfree:
855 kfree(ioapic);
856
857 return AE_OK;
858}
859
860static acpi_status
861ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
862{
863 acpi_status status;
864 unsigned long sta;
865 acpi_handle tmp;
866 u32 gsi_base;
867 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
868
869 /* Evaluate _STA if present */
870 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
871 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
872 return AE_CTRL_DEPTH;
873
874 /* Scan only PCI bus scope */
875 status = acpi_get_handle(handle, "_HID", &tmp);
876 if (ACPI_SUCCESS(status))
877 return AE_CTRL_DEPTH;
878
879 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700880 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700881
882 acpi_unregister_ioapic(handle, gsi_base);
883
884 spin_lock(&ioapic_list_lock);
885 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
886 if (pos->gsi_base != gsi_base)
887 continue;
888 ioapic = pos;
889 list_del(&ioapic->list);
890 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700891 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700892 spin_unlock(&ioapic_list_lock);
893
894 if (!ioapic)
895 return AE_OK;
896
897 pci_release_region(ioapic->dev, 0);
898 pci_disable_device(ioapic->dev);
899 pci_dev_put(ioapic->dev);
900 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700901
902 return AE_OK;
903}
904
905static int acpiphp_configure_ioapics(acpi_handle handle)
906{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700907 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700908 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
909 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
910 return 0;
911}
912
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700913static int acpiphp_unconfigure_ioapics(acpi_handle handle)
914{
915 ioapic_remove(handle, 0, NULL, NULL);
916 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
917 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
918 return 0;
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921static int power_on_slot(struct acpiphp_slot *slot)
922{
923 acpi_status status;
924 struct acpiphp_func *func;
925 struct list_head *l;
926 int retval = 0;
927
928 /* if already enabled, just skip */
929 if (slot->flags & SLOT_POWEREDON)
930 goto err_exit;
931
932 list_for_each (l, &slot->funcs) {
933 func = list_entry(l, struct acpiphp_func, sibling);
934
935 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800936 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
938 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800939 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 retval = -1;
941 goto err_exit;
942 } else
943 break;
944 }
945 }
946
947 /* TBD: evaluate _STA to check if the slot is enabled */
948
949 slot->flags |= SLOT_POWEREDON;
950
951 err_exit:
952 return retval;
953}
954
955
956static int power_off_slot(struct acpiphp_slot *slot)
957{
958 acpi_status status;
959 struct acpiphp_func *func;
960 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 int retval = 0;
963
964 /* if already disabled, just skip */
965 if ((slot->flags & SLOT_POWEREDON) == 0)
966 goto err_exit;
967
968 list_for_each (l, &slot->funcs) {
969 func = list_entry(l, struct acpiphp_func, sibling);
970
Rajesh Shah2f523b12005-04-28 00:25:55 -0700971 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
973 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800974 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 retval = -1;
976 goto err_exit;
977 } else
978 break;
979 }
980 }
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* TBD: evaluate _STA to check if the slot is disabled */
983
984 slot->flags &= (~SLOT_POWEREDON);
985
986 err_exit:
987 return retval;
988}
989
990
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800991
992/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800993 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800994 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800995 */
996static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
997{
998 struct list_head *tmp;
999 unsigned char max, n;
1000
1001 /*
1002 * pci_bus_max_busnr will return the highest
1003 * reserved busnr for all these children.
1004 * that is equivalent to the bus->subordinate
1005 * value. We don't want to use the parent's
1006 * bus->subordinate value because it could have
1007 * padding in it.
1008 */
1009 max = bus->secondary;
1010
1011 list_for_each(tmp, &bus->children) {
1012 n = pci_bus_max_busnr(pci_bus_b(tmp));
1013 if (n > max)
1014 max = n;
1015 }
1016 return max;
1017}
1018
1019
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001020/**
1021 * acpiphp_bus_add - add a new bus to acpi subsystem
1022 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001023 */
1024static int acpiphp_bus_add(struct acpiphp_func *func)
1025{
1026 acpi_handle phandle;
1027 struct acpi_device *device, *pdevice;
1028 int ret_val;
1029
1030 acpi_get_parent(func->handle, &phandle);
1031 if (acpi_bus_get_device(phandle, &pdevice)) {
1032 dbg("no parent device, assuming NULL\n");
1033 pdevice = NULL;
1034 }
Kristen Accardi20416ea2006-02-23 17:56:03 -08001035 if (!acpi_bus_get_device(func->handle, &device)) {
1036 dbg("bus exists... trim\n");
1037 /* this shouldn't be in here, so remove
1038 * the bus then re-add it...
1039 */
1040 ret_val = acpi_bus_trim(device, 1);
1041 dbg("acpi_bus_trim return %x\n", ret_val);
1042 }
1043
1044 ret_val = acpi_bus_add(&device, pdevice, func->handle,
1045 ACPI_BUS_TYPE_DEVICE);
1046 if (ret_val) {
1047 dbg("error adding bus, %x\n",
1048 -ret_val);
1049 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001050 }
1051 /*
1052 * try to start anyway. We could have failed to add
1053 * simply because this bus had previously been added
1054 * on another add. Don't bother with the return value
1055 * we just keep going.
1056 */
1057 ret_val = acpi_bus_start(device);
1058
1059acpiphp_bus_add_out:
1060 return ret_val;
1061}
1062
1063
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001064/**
1065 * acpiphp_bus_trim - trim a bus from acpi subsystem
1066 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001067 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -07001068static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001069{
1070 struct acpi_device *device;
1071 int retval;
1072
1073 retval = acpi_bus_get_device(handle, &device);
1074 if (retval) {
1075 dbg("acpi_device not found\n");
1076 return retval;
1077 }
1078
1079 retval = acpi_bus_trim(device, 1);
1080 if (retval)
1081 err("cannot remove from acpi list\n");
1082
1083 return retval;
1084}
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086/**
1087 * enable_device - enable, configure a slot
1088 * @slot: slot to be enabled
1089 *
1090 * This function should be called per *physical slot*,
1091 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +01001093static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001096 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct list_head *l;
1098 struct acpiphp_func *func;
1099 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001100 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001101 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (slot->flags & SLOT_ENABLED)
1104 goto err_exit;
1105
1106 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001107 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (dev) {
1109 /* This case shouldn't happen */
1110 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001111 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 retval = -1;
1113 goto err_exit;
1114 }
1115
Rajesh Shah42f49a62005-04-28 00:25:53 -07001116 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1117 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 err("No new device found\n");
1119 retval = -1;
1120 goto err_exit;
1121 }
1122
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001123 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001124 for (pass = 0; pass < 2; pass++) {
1125 list_for_each_entry(dev, &bus->devices, bus_list) {
1126 if (PCI_SLOT(dev->devfn) != slot->device)
1127 continue;
1128 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001129 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001130 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001131 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001132 pci_bus_size_bridges(dev->subordinate);
1133 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001137 list_for_each (l, &slot->funcs) {
1138 func = list_entry(l, struct acpiphp_func, sibling);
1139 acpiphp_bus_add(func);
1140 }
1141
Rajesh Shah42f49a62005-04-28 00:25:53 -07001142 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001143 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001144 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001145 list_for_each_entry(func, &slot->funcs, sibling)
1146 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001147 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001148 pci_bus_add_devices(bus);
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* associate pci_dev to our representation */
1151 list_for_each (l, &slot->funcs) {
1152 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001153 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 func->function));
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001155 if (!func->pci_dev)
1156 continue;
1157
1158 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1159 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1160 continue;
1161
1162 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1163 if (ACPI_FAILURE(status))
1164 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1165 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
1168 slot->flags |= SLOT_ENABLED;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 err_exit:
1171 return retval;
1172}
1173
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001174static void disable_bridges(struct pci_bus *bus)
1175{
1176 struct pci_dev *dev;
1177 list_for_each_entry(dev, &bus->devices, bus_list) {
1178 if (dev->subordinate) {
1179 disable_bridges(dev->subordinate);
1180 pci_disable_device(dev);
1181 }
1182 }
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185/**
1186 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001187 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 */
1189static int disable_device(struct acpiphp_slot *slot)
1190{
1191 int retval = 0;
1192 struct acpiphp_func *func;
1193 struct list_head *l;
1194
1195 /* is this slot already disabled? */
1196 if (!(slot->flags & SLOT_ENABLED))
1197 goto err_exit;
1198
1199 list_for_each (l, &slot->funcs) {
1200 func = list_entry(l, struct acpiphp_func, sibling);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001201
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001202 if (func->bridge) {
1203 /* cleanup p2p bridges under this P2P bridge */
1204 cleanup_p2p_bridge(func->bridge->handle,
1205 (u32)1, NULL, NULL);
1206 func->bridge = NULL;
1207 }
1208
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001209 if (func->pci_dev) {
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001210 pci_stop_bus_device(func->pci_dev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001211 if (func->pci_dev->subordinate) {
1212 disable_bridges(func->pci_dev->subordinate);
1213 pci_disable_device(func->pci_dev);
1214 }
1215 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001216 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001217
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001218 list_for_each (l, &slot->funcs) {
1219 func = list_entry(l, struct acpiphp_func, sibling);
1220
1221 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001222 acpiphp_bus_trim(func->handle);
1223 /* try to remove anyway.
1224 * acpiphp_bus_add might have been failed */
1225
Rajesh Shah42f49a62005-04-28 00:25:53 -07001226 if (!func->pci_dev)
1227 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Rajesh Shah42f49a62005-04-28 00:25:53 -07001229 pci_remove_bus_device(func->pci_dev);
1230 pci_dev_put(func->pci_dev);
1231 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
1234 slot->flags &= (~SLOT_ENABLED);
1235
1236 err_exit:
1237 return retval;
1238}
1239
1240
1241/**
1242 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -08001243 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001245 * If a slot has _STA for each function and if any one of them
1246 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001248 * If a slot doesn't have _STA and if any one of its functions'
1249 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001251 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
1253static unsigned int get_slot_status(struct acpiphp_slot *slot)
1254{
1255 acpi_status status;
1256 unsigned long sta = 0;
1257 u32 dvid;
1258 struct list_head *l;
1259 struct acpiphp_func *func;
1260
1261 list_for_each (l, &slot->funcs) {
1262 func = list_entry(l, struct acpiphp_func, sibling);
1263
1264 if (func->flags & FUNC_HAS_STA) {
1265 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1266 if (ACPI_SUCCESS(status) && sta)
1267 break;
1268 } else {
1269 pci_bus_read_config_dword(slot->bridge->pci_bus,
1270 PCI_DEVFN(slot->device,
1271 func->function),
1272 PCI_VENDOR_ID, &dvid);
1273 if (dvid != 0xffffffff) {
1274 sta = ACPI_STA_ALL;
1275 break;
1276 }
1277 }
1278 }
1279
1280 return (unsigned int)sta;
1281}
1282
1283/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001284 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001285 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001286 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001287int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001288{
1289 acpi_status status;
1290 struct acpiphp_func *func;
1291 struct list_head *l;
1292 struct acpi_object_list arg_list;
1293 union acpi_object arg;
1294
1295 list_for_each (l, &slot->funcs) {
1296 func = list_entry(l, struct acpiphp_func, sibling);
1297
1298 /* We don't want to call _EJ0 on non-existing functions. */
1299 if ((func->flags & FUNC_HAS_EJ0)) {
1300 /* _EJ0 method take one argument */
1301 arg_list.count = 1;
1302 arg_list.pointer = &arg;
1303 arg.type = ACPI_TYPE_INTEGER;
1304 arg.integer.value = 1;
1305
1306 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1307 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001308 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001309 return -1;
1310 } else
1311 break;
1312 }
1313 }
1314 return 0;
1315}
1316
1317/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001319 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 *
1321 * Iterate over all slots under this bridge and make sure that if a
1322 * card is present they are enabled, and if not they are disabled.
1323 */
1324static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1325{
1326 struct acpiphp_slot *slot;
1327 int retval = 0;
1328 int enabled, disabled;
1329
1330 enabled = disabled = 0;
1331
1332 for (slot = bridge->slots; slot; slot = slot->next) {
1333 unsigned int status = get_slot_status(slot);
1334 if (slot->flags & SLOT_ENABLED) {
1335 if (status == ACPI_STA_ALL)
1336 continue;
1337 retval = acpiphp_disable_slot(slot);
1338 if (retval) {
1339 err("Error occurred in disabling\n");
1340 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001341 } else {
1342 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 disabled++;
1345 } else {
1346 if (status != ACPI_STA_ALL)
1347 continue;
1348 retval = acpiphp_enable_slot(slot);
1349 if (retval) {
1350 err("Error occurred in enabling\n");
1351 goto err_exit;
1352 }
1353 enabled++;
1354 }
1355 }
1356
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001357 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 err_exit:
1360 return retval;
1361}
1362
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001363static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1364{
1365 u16 pci_cmd, pci_bctl;
1366 struct pci_dev *cdev;
1367
1368 /* Program hpp values for this device */
1369 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1370 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1371 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1372 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001373
Gary Hade9ef22412007-07-05 11:10:47 -07001374 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1375 return;
1376
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001377 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001378 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001379 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001380 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001381 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001382 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001383 pci_cmd |= PCI_COMMAND_SERR;
1384 else
1385 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001386 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001387 pci_cmd |= PCI_COMMAND_PARITY;
1388 else
1389 pci_cmd &= ~PCI_COMMAND_PARITY;
1390 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1391
1392 /* Program bridge control value and child devices */
1393 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1394 pci_write_config_byte(dev, PCI_SEC_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_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001397 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001398 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1399 else
1400 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001401 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001402 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1403 else
1404 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1405 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1406 if (dev->subordinate) {
1407 list_for_each_entry(cdev, &dev->subordinate->devices,
1408 bus_list)
1409 program_hpp(cdev, bridge);
1410 }
1411 }
1412}
1413
1414static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1415{
1416 struct acpiphp_bridge bridge;
1417 struct pci_dev *dev;
1418
1419 memset(&bridge, 0, sizeof(bridge));
1420 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001421 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001422 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001423 decode_hpp(&bridge);
1424 list_for_each_entry(dev, &bus->devices, bus_list)
1425 program_hpp(dev, &bridge);
1426
1427}
1428
1429/*
1430 * Remove devices for which we could not assign resources, call
1431 * arch specific code to fix-up the bus
1432 */
1433static void acpiphp_sanitize_bus(struct pci_bus *bus)
1434{
1435 struct pci_dev *dev;
1436 int i;
1437 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1438
1439 list_for_each_entry(dev, &bus->devices, bus_list) {
1440 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1441 struct resource *res = &dev->resource[i];
1442 if ((res->flags & type_mask) && !res->start &&
1443 res->end) {
1444 /* Could not assign a required resources
1445 * for this device, remove it */
1446 pci_remove_bus_device(dev);
1447 break;
1448 }
1449 }
1450 }
1451}
1452
1453/* Program resources in newly inserted bridge */
1454static int acpiphp_configure_bridge (acpi_handle handle)
1455{
1456 struct acpi_pci_id pci_id;
1457 struct pci_bus *bus;
1458
1459 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1460 err("cannot get PCI domain and bus number for bridge\n");
1461 return -EINVAL;
1462 }
1463 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1464 if (!bus) {
1465 err("cannot find bus %d:%d\n",
1466 pci_id.segment, pci_id.bus);
1467 return -EINVAL;
1468 }
1469
1470 pci_bus_size_bridges(bus);
1471 pci_bus_assign_resources(bus);
1472 acpiphp_sanitize_bus(bus);
1473 acpiphp_set_hpp_values(handle, bus);
1474 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001475 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001476 return 0;
1477}
1478
1479static void handle_bridge_insertion(acpi_handle handle, u32 type)
1480{
1481 struct acpi_device *device, *pdevice;
1482 acpi_handle phandle;
1483
1484 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1485 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1486 err("unexpected notification type %d\n", type);
1487 return;
1488 }
1489
1490 acpi_get_parent(handle, &phandle);
1491 if (acpi_bus_get_device(phandle, &pdevice)) {
1492 dbg("no parent device, assuming NULL\n");
1493 pdevice = NULL;
1494 }
1495 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1496 err("cannot add bridge to acpi list\n");
1497 return;
1498 }
1499 if (!acpiphp_configure_bridge(handle) &&
1500 !acpi_bus_start(device))
1501 add_bridge(handle);
1502 else
1503 err("cannot configure and start bridge\n");
1504
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/*
1508 * ACPI event handlers
1509 */
1510
Gary Hade0bbd6422007-07-05 11:10:48 -07001511static acpi_status
1512count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1513{
1514 int *count = (int *)context;
1515 struct acpiphp_bridge *bridge;
1516
1517 bridge = acpiphp_handle_to_bridge(handle);
1518 if (bridge)
1519 (*count)++;
1520 return AE_OK ;
1521}
1522
1523static acpi_status
1524check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1525{
1526 struct acpiphp_bridge *bridge;
1527 char objname[64];
1528 struct acpi_buffer buffer = { .length = sizeof(objname),
1529 .pointer = objname };
1530
1531 bridge = acpiphp_handle_to_bridge(handle);
1532 if (bridge) {
1533 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1534 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001535 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001536 acpiphp_check_bridge(bridge);
1537 }
1538 return AE_OK ;
1539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/**
1542 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * @handle: Notify()'ed acpi_handle
1544 * @type: Notify code
1545 * @context: pointer to acpiphp_bridge structure
1546 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001547 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
1549static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1550{
1551 struct acpiphp_bridge *bridge;
1552 char objname[64];
1553 struct acpi_buffer buffer = { .length = sizeof(objname),
1554 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001555 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001556 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001558 if (acpi_bus_get_device(handle, &device)) {
1559 /* This bridge must have just been physically inserted */
1560 handle_bridge_insertion(handle, type);
1561 return;
1562 }
1563
1564 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001565 if (type == ACPI_NOTIFY_BUS_CHECK) {
1566 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1567 count_sub_bridges, &num_sub_bridges, NULL);
1568 }
1569
1570 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001571 err("cannot get bridge info\n");
1572 return;
1573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1576
1577 switch (type) {
1578 case ACPI_NOTIFY_BUS_CHECK:
1579 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001580 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001581 if (bridge) {
1582 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001583 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001584 acpiphp_check_bridge(bridge);
1585 }
1586 if (num_sub_bridges)
1587 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1588 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 break;
1590
1591 case ACPI_NOTIFY_DEVICE_CHECK:
1592 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001593 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 acpiphp_check_bridge(bridge);
1595 break;
1596
1597 case ACPI_NOTIFY_DEVICE_WAKE:
1598 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001599 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 break;
1601
1602 case ACPI_NOTIFY_EJECT_REQUEST:
1603 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001604 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001605 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1606 (bridge->flags & BRIDGE_HAS_EJ0)) {
1607 struct acpiphp_slot *slot;
1608 slot = bridge->func->slot;
1609 if (!acpiphp_disable_slot(slot))
1610 acpiphp_eject_slot(slot);
1611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 break;
1613
1614 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1615 printk(KERN_ERR "Device %s cannot be configured due"
1616 " to a frequency mismatch\n", objname);
1617 break;
1618
1619 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1620 printk(KERN_ERR "Device %s cannot be configured due"
1621 " to a bus mode mismatch\n", objname);
1622 break;
1623
1624 case ACPI_NOTIFY_POWER_FAULT:
1625 printk(KERN_ERR "Device %s has suffered a power fault\n",
1626 objname);
1627 break;
1628
1629 default:
1630 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1631 break;
1632 }
1633}
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635/**
1636 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * @handle: Notify()'ed acpi_handle
1638 * @type: Notify code
1639 * @context: pointer to acpiphp_func structure
1640 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001641 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
Len Brown2b85e132006-06-27 01:50:14 -04001643static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 struct acpiphp_func *func;
1646 char objname[64];
1647 struct acpi_buffer buffer = { .length = sizeof(objname),
1648 .pointer = objname };
1649
1650 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1651
1652 func = (struct acpiphp_func *)context;
1653
1654 switch (type) {
1655 case ACPI_NOTIFY_BUS_CHECK:
1656 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001657 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 acpiphp_enable_slot(func->slot);
1659 break;
1660
1661 case ACPI_NOTIFY_DEVICE_CHECK:
1662 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001663 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 acpiphp_check_bridge(func->slot->bridge);
1665 break;
1666
1667 case ACPI_NOTIFY_DEVICE_WAKE:
1668 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001669 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 break;
1671
1672 case ACPI_NOTIFY_EJECT_REQUEST:
1673 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001674 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001675 if (!(acpiphp_disable_slot(func->slot)))
1676 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 break;
1678
1679 default:
1680 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1681 break;
1682 }
1683}
1684
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001685
1686static acpi_status
1687find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1688{
1689 int *count = (int *)context;
1690
Kristen Accardi783c49f2006-03-03 10:16:05 -08001691 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001692 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1693 handle_hotplug_event_bridge, NULL);
1694 (*count)++;
1695 }
1696 return AE_OK ;
1697}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699static struct acpi_pci_driver acpi_pci_hp_driver = {
1700 .add = add_bridge,
1701 .remove = remove_bridge,
1702};
1703
1704/**
1705 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
1707int __init acpiphp_glue_init(void)
1708{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001709 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001711 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1712 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 if (num <= 0)
1715 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001716 else
1717 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 return 0;
1720}
1721
1722
1723/**
1724 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1725 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001726 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001728void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1731}
1732
1733
1734/**
1735 * acpiphp_get_num_slots - count number of slots in a system
1736 */
1737int __init acpiphp_get_num_slots(void)
1738{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001740 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Akinobu Mita467c4422006-10-30 13:07:58 -08001742 list_for_each_entry (bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001743 dbg("Bus %04x:%02x has %d slot%s\n",
1744 pci_domain_nr(bridge->pci_bus),
1745 bridge->pci_bus->number, bridge->nr_slots,
1746 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 num_slots += bridge->nr_slots;
1748 }
1749
Rajesh Shah42f49a62005-04-28 00:25:53 -07001750 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return num_slots;
1752}
1753
1754
1755#if 0
1756/**
1757 * acpiphp_for_each_slot - call function for each slot
1758 * @fn: callback function
1759 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
1761static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1762{
1763 struct list_head *node;
1764 struct acpiphp_bridge *bridge;
1765 struct acpiphp_slot *slot;
1766 int retval = 0;
1767
1768 list_for_each (node, &bridge_list) {
1769 bridge = (struct acpiphp_bridge *)node;
1770 for (slot = bridge->slots; slot; slot = slot->next) {
1771 retval = fn(slot, data);
1772 if (!retval)
1773 goto err_exit;
1774 }
1775 }
1776
1777 err_exit:
1778 return retval;
1779}
1780#endif
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783/**
1784 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001785 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
1787int acpiphp_enable_slot(struct acpiphp_slot *slot)
1788{
1789 int retval;
1790
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001791 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 /* wake up all functions */
1794 retval = power_on_slot(slot);
1795 if (retval)
1796 goto err_exit;
1797
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001798 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* configure all functions */
1800 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001801 if (retval)
1802 power_off_slot(slot);
1803 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001804 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001805 power_off_slot(slot);
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001809 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return retval;
1811}
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/**
1814 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001815 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 */
1817int acpiphp_disable_slot(struct acpiphp_slot *slot)
1818{
1819 int retval = 0;
1820
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001821 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 /* unconfigure all functions */
1824 retval = disable_device(slot);
1825 if (retval)
1826 goto err_exit;
1827
1828 /* power off all functions */
1829 retval = power_off_slot(slot);
1830 if (retval)
1831 goto err_exit;
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001834 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return retval;
1836}
1837
1838
1839/*
1840 * slot enabled: 1
1841 * slot disabled: 0
1842 */
1843u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1844{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001845 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848
1849/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001850 * latch open: 1
1851 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
1853u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1854{
1855 unsigned int sta;
1856
1857 sta = get_slot_status(slot);
1858
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001859 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
1862
1863/*
1864 * adapter presence : 1
1865 * absence : 0
1866 */
1867u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1868{
1869 unsigned int sta;
1870
1871 sta = get_slot_status(slot);
1872
1873 return (sta == 0) ? 0 : 1;
1874}