blob: 16167b016266c0499cd4c9f26900b0f48ba5c76e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/smp_lock.h>
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +010050#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include "../pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "acpiphp.h"
54
55static LIST_HEAD(bridge_list);
Satoru Takeuchi600812e2006-09-12 10:22:53 -070056static LIST_HEAD(ioapic_list);
57static DEFINE_SPINLOCK(ioapic_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define MY_NAME "acpiphp_glue"
60
61static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070062static void acpiphp_sanitize_bus(struct pci_bus *bus);
63static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
Len Brown2b85e132006-06-27 01:50:14 -040064static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * initialization & terminatation routines
69 */
70
71/**
72 * is_ejectable - determine if a slot is ejectable
73 * @handle: handle to acpi namespace
74 *
75 * Ejectable slot should satisfy at least these conditions:
76 *
77 * 1. has _ADR method
78 * 2. has _EJ0 method
79 *
80 * optionally
81 *
82 * 1. has _STA method
83 * 2. has _PS0 method
84 * 3. has _PS3 method
85 * 4. ..
86 *
87 */
88static int is_ejectable(acpi_handle handle)
89{
90 acpi_status status;
91 acpi_handle tmp;
92
93 status = acpi_get_handle(handle, "_ADR", &tmp);
94 if (ACPI_FAILURE(status)) {
95 return 0;
96 }
97
98 status = acpi_get_handle(handle, "_EJ0", &tmp);
99 if (ACPI_FAILURE(status)) {
100 return 0;
101 }
102
103 return 1;
104}
105
106
107/* callback routine to check the existence of ejectable slots */
108static acpi_status
109is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
110{
111 int *count = (int *)context;
112
113 if (is_ejectable(handle)) {
114 (*count)++;
115 /* only one ejectable slot is enough */
116 return AE_CTRL_TERMINATE;
117 } else {
118 return AE_OK;
119 }
120}
121
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400122/* callback routine to check for the existance of a pci dock device */
123static acpi_status
124is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
125{
126 int *count = (int *)context;
127
128 if (is_dock_device(handle)) {
129 (*count)++;
130 return AE_CTRL_TERMINATE;
131 } else {
132 return AE_OK;
133 }
134}
135
136
137
138
139/*
140 * the _DCK method can do funny things... and sometimes not
141 * hah-hah funny.
142 *
143 * TBD - figure out a way to only call fixups for
144 * systems that require them.
145 */
146static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
147 void *v)
148{
149 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
150 struct pci_bus *bus = func->slot->bridge->pci_bus;
151 u32 buses;
152
153 if (!bus->self)
154 return NOTIFY_OK;
155
156 /* fixup bad _DCK function that rewrites
157 * secondary bridge on slot
158 */
159 pci_read_config_dword(bus->self,
160 PCI_PRIMARY_BUS,
161 &buses);
162
163 if (((buses >> 8) & 0xff) != bus->secondary) {
164 buses = (buses & 0xff000000)
165 | ((unsigned int)(bus->primary) << 0)
166 | ((unsigned int)(bus->secondary) << 8)
167 | ((unsigned int)(bus->subordinate) << 16);
168 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
169 }
170 return NOTIFY_OK;
171}
172
173
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/* callback routine to register each ACPI PCI slot object */
177static acpi_status
178register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
179{
180 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
181 struct acpiphp_slot *slot;
182 struct acpiphp_func *newfunc;
183 acpi_handle tmp;
184 acpi_status status = AE_OK;
185 unsigned long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800186 int device, function, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
189
190 if (ACPI_FAILURE(status))
191 return AE_OK;
192
193 status = acpi_get_handle(handle, "_EJ0", &tmp);
194
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400195 if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return AE_OK;
197
198 device = (adr >> 16) & 0xffff;
199 function = adr & 0xffff;
200
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100201 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!newfunc)
203 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 INIT_LIST_HEAD(&newfunc->sibling);
206 newfunc->handle = handle;
207 newfunc->function = function;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800208 if (ACPI_SUCCESS(status))
209 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
212 newfunc->flags |= FUNC_HAS_STA;
213
214 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
215 newfunc->flags |= FUNC_HAS_PS0;
216
217 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
218 newfunc->flags |= FUNC_HAS_PS3;
219
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400220 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800221 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400224 if (ACPI_FAILURE(status)) {
225 /*
226 * use the count of the number of slots we've found
227 * for the number of the slot
228 */
229 sun = bridge->nr_slots+1;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* search for objects that share the same slot */
233 for (slot = bridge->slots; slot; slot = slot->next)
234 if (slot->device == device) {
235 if (slot->sun != sun)
236 warn("sibling found, but _SUN doesn't match!\n");
237 break;
238 }
239
240 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100241 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!slot) {
243 kfree(newfunc);
244 return AE_NO_MEMORY;
245 }
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 slot->device = device;
249 slot->sun = sun;
250 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100251 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 slot->next = bridge->slots;
254 bridge->slots = slot;
255
256 bridge->nr_slots++;
257
Rajesh Shah42f49a62005-04-28 00:25:53 -0700258 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
259 slot->sun, pci_domain_nr(bridge->pci_bus),
260 bridge->pci_bus->number, slot->device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800261 retval = acpiphp_register_hotplug_slot(slot);
262 if (retval) {
263 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
264 goto err_exit;
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
268 newfunc->slot = slot;
269 list_add_tail(&newfunc->sibling, &slot->funcs);
270
271 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700272 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 PCI_DEVFN(device, function));
274 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
276 }
277
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400278 if (is_dock_device(handle)) {
279 /* we don't want to call this device's _EJ0
280 * because we want the dock notify handler
281 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800282 */
283 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400284 if (register_hotplug_dock_device(handle,
285 handle_hotplug_event_func, newfunc))
286 dbg("failed to register dock device\n");
287
288 /* we need to be notified when dock events happen
289 * outside of the hotplug operation, since we may
290 * need to do fixups before we can hotplug.
291 */
292 newfunc->nb.notifier_call = post_dock_fixups;
293 if (register_dock_notifier(&newfunc->nb))
294 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800295 }
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800298 if (!(newfunc->flags & FUNC_HAS_DCK)) {
299 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 ACPI_SYSTEM_NOTIFY,
301 handle_hotplug_event_func,
302 newfunc);
303
Kristen Accardi20416ea2006-02-23 17:56:03 -0800304 if (ACPI_FAILURE(status))
305 err("failed to register interrupt notify handler\n");
306 } else
307 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Kristen Accardi20416ea2006-02-23 17:56:03 -0800309 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800310
311 err_exit:
312 bridge->nr_slots--;
313 bridge->slots = slot->next;
314 kfree(slot);
315 kfree(newfunc);
316
317 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320
321/* see if it's worth looking at this bridge */
322static int detect_ejectable_slots(acpi_handle *bridge_handle)
323{
324 acpi_status status;
325 int count;
326
327 count = 0;
328
329 /* only check slots defined directly below bridge object */
330 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
331 is_ejectable_slot, (void *)&count, NULL);
332
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400333 /*
334 * we also need to add this bridge if there is a dock bridge or
335 * other pci device on a dock station (removable)
336 */
337 if (!count)
338 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
339 (u32)1, is_pci_dock_device, (void *)&count,
340 NULL);
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return count;
343}
344
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/* decode ACPI 2.0 _HPP hot plug parameters */
347static void decode_hpp(struct acpiphp_bridge *bridge)
348{
349 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900351 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900352 if (ACPI_FAILURE(status) ||
353 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800354 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900355 printk(KERN_WARNING
356 "%s: Could not get hotplug parameters. Use defaults\n",
357 __FUNCTION__);
358 bridge->hpp.t0 = &bridge->hpp.type0_data;
359 bridge->hpp.t0->revision = 0;
360 bridge->hpp.t0->cache_line_size = 0x10;
361 bridge->hpp.t0->latency_timer = 0x40;
362 bridge->hpp.t0->enable_serr = 0;
363 bridge->hpp.t0->enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367
Kristen Accardi783c49f2006-03-03 10:16:05 -0800368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
370static void init_bridge_misc(struct acpiphp_bridge *bridge)
371{
372 acpi_status status;
373
374 /* decode ACPI 2.0 _HPP (hot plug parameters) */
375 decode_hpp(bridge);
376
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800377 /* must be added to the list prior to calling register_slot */
378 list_add(&bridge->list, &bridge_list);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* register all slot objects under this bridge */
381 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
382 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800383 if (ACPI_FAILURE(status)) {
384 list_del(&bridge->list);
385 return;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700389 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900390 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
391 status = acpi_remove_notify_handler(bridge->func->handle,
392 ACPI_SYSTEM_NOTIFY,
393 handle_hotplug_event_func);
394 if (ACPI_FAILURE(status))
395 err("failed to remove notify handler\n");
396 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700397 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ACPI_SYSTEM_NOTIFY,
399 handle_hotplug_event_bridge,
400 bridge);
401
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700402 if (ACPI_FAILURE(status)) {
403 err("failed to register interrupt notify handler\n");
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900409/* find acpiphp_func from acpiphp_bridge */
410static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
411{
412 struct list_head *node, *l;
413 struct acpiphp_bridge *bridge;
414 struct acpiphp_slot *slot;
415 struct acpiphp_func *func;
416
417 list_for_each(node, &bridge_list) {
418 bridge = list_entry(node, struct acpiphp_bridge, list);
419 for (slot = bridge->slots; slot; slot = slot->next) {
420 list_for_each(l, &slot->funcs) {
421 func = list_entry(l, struct acpiphp_func,
422 sibling);
423 if (func->handle == handle)
424 return func;
425 }
426 }
427 }
428
429 return NULL;
430}
431
432
433static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
434{
435 acpi_handle dummy_handle;
436
437 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
438 "_STA", &dummy_handle)))
439 bridge->flags |= BRIDGE_HAS_STA;
440
441 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
442 "_EJ0", &dummy_handle)))
443 bridge->flags |= BRIDGE_HAS_EJ0;
444
445 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
446 "_PS0", &dummy_handle)))
447 bridge->flags |= BRIDGE_HAS_PS0;
448
449 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
450 "_PS3", &dummy_handle)))
451 bridge->flags |= BRIDGE_HAS_PS3;
452
453 /* is this ejectable p2p bridge? */
454 if (bridge->flags & BRIDGE_HAS_EJ0) {
455 struct acpiphp_func *func;
456
457 dbg("found ejectable p2p bridge\n");
458
459 /* make link between PCI bridge and PCI function */
460 func = acpiphp_bridge_handle_to_function(bridge->handle);
461 if (!func)
462 return;
463 bridge->func = func;
464 func->bridge = bridge;
465 }
466}
467
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700470static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct acpiphp_bridge *bridge;
473
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100474 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (bridge == NULL)
476 return;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 bridge->type = BRIDGE_TYPE_HOST;
479 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Rajesh Shah42f49a62005-04-28 00:25:53 -0700481 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 spin_lock_init(&bridge->res_lock);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 init_bridge_misc(bridge);
486}
487
488
489/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700490static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100494 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (bridge == NULL) {
496 err("out of memory\n");
497 return;
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 bridge->type = BRIDGE_TYPE_P2P;
501 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900502 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Rajesh Shah42f49a62005-04-28 00:25:53 -0700504 bridge->pci_dev = pci_dev_get(pci_dev);
505 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!bridge->pci_bus) {
507 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700508 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 spin_lock_init(&bridge->res_lock);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700514 return;
515 err:
516 pci_dev_put(pci_dev);
517 kfree(bridge);
518 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521
522/* callback routine to find P2P bridges */
523static acpi_status
524find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
525{
526 acpi_status status;
527 acpi_handle dummy_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 unsigned long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700529 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700531 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
534 if (ACPI_FAILURE(status))
535 return AE_OK; /* continue */
536
537 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
538 if (ACPI_FAILURE(status)) {
539 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
540 return AE_OK;
541 }
542
543 device = (tmp >> 16) & 0xffff;
544 function = tmp & 0xffff;
545
Rajesh Shah42f49a62005-04-28 00:25:53 -0700546 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Rajesh Shah42f49a62005-04-28 00:25:53 -0700548 if (!dev || !dev->subordinate)
549 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* check if this bridge has ejectable slots */
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400552 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700554 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900557 /* search P2P bridges under this p2p bridge */
558 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
559 find_p2p_bridge, dev->subordinate, NULL);
560 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900561 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900562
Rajesh Shah42f49a62005-04-28 00:25:53 -0700563 out:
564 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return AE_OK;
566}
567
568
569/* find hot-pluggable slots, and then find P2P bridge */
570static int add_bridge(acpi_handle handle)
571{
572 acpi_status status;
573 unsigned long tmp;
574 int seg, bus;
575 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700576 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* if the bridge doesn't have _STA, we assume it is always there */
579 status = acpi_get_handle(handle, "_STA", &dummy_handle);
580 if (ACPI_SUCCESS(status)) {
581 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
582 if (ACPI_FAILURE(status)) {
583 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
584 return 0;
585 }
586 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
587 /* don't register this object */
588 return 0;
589 }
590
591 /* get PCI segment number */
592 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
593
594 seg = ACPI_SUCCESS(status) ? tmp : 0;
595
596 /* get PCI bus number */
597 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
598
599 if (ACPI_SUCCESS(status)) {
600 bus = tmp;
601 } else {
602 warn("can't get bus number, assuming 0\n");
603 bus = 0;
604 }
605
Rajesh Shah42f49a62005-04-28 00:25:53 -0700606 pci_bus = pci_find_bus(seg, bus);
607 if (!pci_bus) {
608 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610 }
611
Rajesh Shah42f49a62005-04-28 00:25:53 -0700612 /* check if this bridge has ejectable slots */
613 if (detect_ejectable_slots(handle) > 0) {
614 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
615 add_host_bridge(handle, pci_bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* search P2P bridges under this host bridge */
619 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700620 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900623 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 return 0;
626}
627
Rajesh Shah42f49a62005-04-28 00:25:53 -0700628static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
629{
630 struct list_head *head;
631 list_for_each(head, &bridge_list) {
632 struct acpiphp_bridge *bridge = list_entry(head,
633 struct acpiphp_bridge, list);
634 if (bridge->handle == handle)
635 return bridge;
636 }
637
638 return NULL;
639}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Rajesh Shah364d5092005-04-28 00:25:54 -0700641static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700643 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700644 struct acpiphp_slot *slot;
645 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700646 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700647
648 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
649 handle_hotplug_event_bridge);
650 if (ACPI_FAILURE(status))
651 err("failed to remove notify handler\n");
652
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900653 if ((bridge->type != BRIDGE_TYPE_HOST) &&
654 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
655 status = acpi_install_notify_handler(bridge->func->handle,
656 ACPI_SYSTEM_NOTIFY,
657 handle_hotplug_event_func,
658 bridge->func);
659 if (ACPI_FAILURE(status))
660 err("failed to install interrupt notify handler\n");
661 }
662
Rajesh Shah42f49a62005-04-28 00:25:53 -0700663 slot = bridge->slots;
664 while (slot) {
665 struct acpiphp_slot *next = slot->next;
666 list_for_each_safe (list, tmp, &slot->funcs) {
667 struct acpiphp_func *func;
668 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400669 if (is_dock_device(func->handle)) {
670 unregister_hotplug_dock_device(func->handle);
671 unregister_dock_notifier(&func->nb);
672 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800673 if (!(func->flags & FUNC_HAS_DCK)) {
674 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700675 ACPI_SYSTEM_NOTIFY,
676 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800677 if (ACPI_FAILURE(status))
678 err("failed to remove notify handler\n");
679 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700680 pci_dev_put(func->pci_dev);
681 list_del(list);
682 kfree(func);
683 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800684 acpiphp_unregister_hotplug_slot(slot);
685 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700686 kfree(slot);
687 slot = next;
688 }
689
690 pci_dev_put(bridge->pci_dev);
691 list_del(&bridge->list);
692 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Rajesh Shah364d5092005-04-28 00:25:54 -0700695static acpi_status
696cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
697{
698 struct acpiphp_bridge *bridge;
699
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900700 /* cleanup p2p bridges under this P2P bridge
701 in a depth-first manner */
702 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
703 cleanup_p2p_bridge, NULL, NULL);
704
Rajesh Shah364d5092005-04-28 00:25:54 -0700705 if (!(bridge = acpiphp_handle_to_bridge(handle)))
706 return AE_OK;
707 cleanup_bridge(bridge);
708 return AE_OK;
709}
710
711static void remove_bridge(acpi_handle handle)
712{
713 struct acpiphp_bridge *bridge;
714
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900715 /* cleanup p2p bridges under this host bridge
716 in a depth-first manner */
717 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
718 (u32)1, cleanup_p2p_bridge, NULL, NULL);
719
Rajesh Shah364d5092005-04-28 00:25:54 -0700720 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900721 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700722 cleanup_bridge(bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700723}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700725static struct pci_dev * get_apic_pci_info(acpi_handle handle)
726{
727 struct acpi_pci_id id;
728 struct pci_bus *bus;
729 struct pci_dev *dev;
730
731 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
732 return NULL;
733
734 bus = pci_find_bus(id.segment, id.bus);
735 if (!bus)
736 return NULL;
737
738 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
739 if (!dev)
740 return NULL;
741
742 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
743 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
744 {
745 pci_dev_put(dev);
746 return NULL;
747 }
748
749 return dev;
750}
751
752static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
753{
754 acpi_status status;
755 int result = -1;
756 unsigned long gsb;
757 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
758 union acpi_object *obj;
759 void *table;
760
761 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
762 if (ACPI_SUCCESS(status)) {
763 *gsi_base = (u32)gsb;
764 return 0;
765 }
766
767 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
768 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
769 return -1;
770
771 obj = buffer.pointer;
772 if (obj->type != ACPI_TYPE_BUFFER)
773 goto out;
774
775 table = obj->buffer.pointer;
776 switch (((acpi_table_entry_header *)table)->type) {
777 case ACPI_MADT_IOSAPIC:
778 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
779 result = 0;
780 break;
781 case ACPI_MADT_IOAPIC:
782 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
783 result = 0;
784 break;
785 default:
786 break;
787 }
788 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700789 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700790 return result;
791}
792
793static acpi_status
794ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
795{
796 acpi_status status;
797 unsigned long sta;
798 acpi_handle tmp;
799 struct pci_dev *pdev;
800 u32 gsi_base;
801 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700802 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700803
804 /* Evaluate _STA if present */
805 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
806 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
807 return AE_CTRL_DEPTH;
808
809 /* Scan only PCI bus scope */
810 status = acpi_get_handle(handle, "_HID", &tmp);
811 if (ACPI_SUCCESS(status))
812 return AE_CTRL_DEPTH;
813
814 if (get_gsi_base(handle, &gsi_base))
815 return AE_OK;
816
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700817 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
818 if (!ioapic)
819 return AE_NO_MEMORY;
820
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700821 pdev = get_apic_pci_info(handle);
822 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700823 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700824
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700825 if (pci_enable_device(pdev))
826 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700827
828 pci_set_master(pdev);
829
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700830 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
831 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700832
833 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700834 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
835 goto exit_pci_release_region;
836
837 ioapic->gsi_base = gsi_base;
838 ioapic->dev = pdev;
839 spin_lock(&ioapic_list_lock);
840 list_add_tail(&ioapic->list, &ioapic_list);
841 spin_unlock(&ioapic_list_lock);
842
843 return AE_OK;
844
845 exit_pci_release_region:
846 pci_release_region(pdev, 0);
847 exit_pci_disable_device:
848 pci_disable_device(pdev);
849 exit_pci_dev_put:
850 pci_dev_put(pdev);
851 exit_kfree:
852 kfree(ioapic);
853
854 return AE_OK;
855}
856
857static acpi_status
858ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
859{
860 acpi_status status;
861 unsigned long sta;
862 acpi_handle tmp;
863 u32 gsi_base;
864 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
865
866 /* Evaluate _STA if present */
867 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
868 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
869 return AE_CTRL_DEPTH;
870
871 /* Scan only PCI bus scope */
872 status = acpi_get_handle(handle, "_HID", &tmp);
873 if (ACPI_SUCCESS(status))
874 return AE_CTRL_DEPTH;
875
876 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700877 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700878
879 acpi_unregister_ioapic(handle, gsi_base);
880
881 spin_lock(&ioapic_list_lock);
882 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
883 if (pos->gsi_base != gsi_base)
884 continue;
885 ioapic = pos;
886 list_del(&ioapic->list);
887 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700888 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700889 spin_unlock(&ioapic_list_lock);
890
891 if (!ioapic)
892 return AE_OK;
893
894 pci_release_region(ioapic->dev, 0);
895 pci_disable_device(ioapic->dev);
896 pci_dev_put(ioapic->dev);
897 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700898
899 return AE_OK;
900}
901
902static int acpiphp_configure_ioapics(acpi_handle handle)
903{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700904 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700905 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
906 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
907 return 0;
908}
909
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700910static int acpiphp_unconfigure_ioapics(acpi_handle handle)
911{
912 ioapic_remove(handle, 0, NULL, NULL);
913 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
914 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
915 return 0;
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918static int power_on_slot(struct acpiphp_slot *slot)
919{
920 acpi_status status;
921 struct acpiphp_func *func;
922 struct list_head *l;
923 int retval = 0;
924
925 /* if already enabled, just skip */
926 if (slot->flags & SLOT_POWEREDON)
927 goto err_exit;
928
929 list_for_each (l, &slot->funcs) {
930 func = list_entry(l, struct acpiphp_func, sibling);
931
932 if (func->flags & FUNC_HAS_PS0) {
933 dbg("%s: executing _PS0\n", __FUNCTION__);
934 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
935 if (ACPI_FAILURE(status)) {
936 warn("%s: _PS0 failed\n", __FUNCTION__);
937 retval = -1;
938 goto err_exit;
939 } else
940 break;
941 }
942 }
943
944 /* TBD: evaluate _STA to check if the slot is enabled */
945
946 slot->flags |= SLOT_POWEREDON;
947
948 err_exit:
949 return retval;
950}
951
952
953static int power_off_slot(struct acpiphp_slot *slot)
954{
955 acpi_status status;
956 struct acpiphp_func *func;
957 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 int retval = 0;
960
961 /* if already disabled, just skip */
962 if ((slot->flags & SLOT_POWEREDON) == 0)
963 goto err_exit;
964
965 list_for_each (l, &slot->funcs) {
966 func = list_entry(l, struct acpiphp_func, sibling);
967
Rajesh Shah2f523b12005-04-28 00:25:55 -0700968 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
970 if (ACPI_FAILURE(status)) {
971 warn("%s: _PS3 failed\n", __FUNCTION__);
972 retval = -1;
973 goto err_exit;
974 } else
975 break;
976 }
977 }
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* TBD: evaluate _STA to check if the slot is disabled */
980
981 slot->flags &= (~SLOT_POWEREDON);
982
983 err_exit:
984 return retval;
985}
986
987
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800988
989/**
990 * acpiphp_max_busnr - return the highest reserved bus number under
991 * the given bus.
992 * @bus: bus to start search with
993 *
994 */
995static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
996{
997 struct list_head *tmp;
998 unsigned char max, n;
999
1000 /*
1001 * pci_bus_max_busnr will return the highest
1002 * reserved busnr for all these children.
1003 * that is equivalent to the bus->subordinate
1004 * value. We don't want to use the parent's
1005 * bus->subordinate value because it could have
1006 * padding in it.
1007 */
1008 max = bus->secondary;
1009
1010 list_for_each(tmp, &bus->children) {
1011 n = pci_bus_max_busnr(pci_bus_b(tmp));
1012 if (n > max)
1013 max = n;
1014 }
1015 return max;
1016}
1017
1018
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001019/**
1020 * acpiphp_bus_add - add a new bus to acpi subsystem
1021 * @func: acpiphp_func of the bridge
1022 *
1023 */
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
1067 *
1068 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -07001069static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001070{
1071 struct acpi_device *device;
1072 int retval;
1073
1074 retval = acpi_bus_get_device(handle, &device);
1075 if (retval) {
1076 dbg("acpi_device not found\n");
1077 return retval;
1078 }
1079
1080 retval = acpi_bus_trim(device, 1);
1081 if (retval)
1082 err("cannot remove from acpi list\n");
1083
1084 return retval;
1085}
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/**
1088 * enable_device - enable, configure a slot
1089 * @slot: slot to be enabled
1090 *
1091 * This function should be called per *physical slot*,
1092 * not per each slot object in ACPI namespace.
1093 *
1094 */
1095static int enable_device(struct acpiphp_slot *slot)
1096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001098 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct list_head *l;
1100 struct acpiphp_func *func;
1101 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001102 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001103 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 if (slot->flags & SLOT_ENABLED)
1106 goto err_exit;
1107
1108 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001109 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (dev) {
1111 /* This case shouldn't happen */
1112 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001113 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 retval = -1;
1115 goto err_exit;
1116 }
1117
Rajesh Shah42f49a62005-04-28 00:25:53 -07001118 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1119 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 err("No new device found\n");
1121 retval = -1;
1122 goto err_exit;
1123 }
1124
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001125 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001126 for (pass = 0; pass < 2; pass++) {
1127 list_for_each_entry(dev, &bus->devices, bus_list) {
1128 if (PCI_SLOT(dev->devfn) != slot->device)
1129 continue;
1130 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001131 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001132 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001133 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001134 pci_bus_size_bridges(dev->subordinate);
1135 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001139 list_for_each (l, &slot->funcs) {
1140 func = list_entry(l, struct acpiphp_func, sibling);
1141 acpiphp_bus_add(func);
1142 }
1143
Rajesh Shah42f49a62005-04-28 00:25:53 -07001144 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001145 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001146 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001147 list_for_each_entry(func, &slot->funcs, sibling)
1148 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001149 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001150 pci_bus_add_devices(bus);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* associate pci_dev to our representation */
1153 list_for_each (l, &slot->funcs) {
1154 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001155 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 func->function));
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001157 if (!func->pci_dev)
1158 continue;
1159
1160 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1161 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1162 continue;
1163
1164 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1165 if (ACPI_FAILURE(status))
1166 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1167 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169
1170 slot->flags |= SLOT_ENABLED;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 err_exit:
1173 return retval;
1174}
1175
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001176static void disable_bridges(struct pci_bus *bus)
1177{
1178 struct pci_dev *dev;
1179 list_for_each_entry(dev, &bus->devices, bus_list) {
1180 if (dev->subordinate) {
1181 disable_bridges(dev->subordinate);
1182 pci_disable_device(dev);
1183 }
1184 }
1185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187/**
1188 * disable_device - disable a slot
1189 */
1190static int disable_device(struct acpiphp_slot *slot)
1191{
1192 int retval = 0;
1193 struct acpiphp_func *func;
1194 struct list_head *l;
1195
1196 /* is this slot already disabled? */
1197 if (!(slot->flags & SLOT_ENABLED))
1198 goto err_exit;
1199
1200 list_for_each (l, &slot->funcs) {
1201 func = list_entry(l, struct acpiphp_func, sibling);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001202
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001203 if (func->bridge) {
1204 /* cleanup p2p bridges under this P2P bridge */
1205 cleanup_p2p_bridge(func->bridge->handle,
1206 (u32)1, NULL, NULL);
1207 func->bridge = NULL;
1208 }
1209
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001210 if (func->pci_dev) {
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001211 pci_stop_bus_device(func->pci_dev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001212 if (func->pci_dev->subordinate) {
1213 disable_bridges(func->pci_dev->subordinate);
1214 pci_disable_device(func->pci_dev);
1215 }
1216 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001217 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001218
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001219 list_for_each (l, &slot->funcs) {
1220 func = list_entry(l, struct acpiphp_func, sibling);
1221
1222 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001223 acpiphp_bus_trim(func->handle);
1224 /* try to remove anyway.
1225 * acpiphp_bus_add might have been failed */
1226
Rajesh Shah42f49a62005-04-28 00:25:53 -07001227 if (!func->pci_dev)
1228 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Rajesh Shah42f49a62005-04-28 00:25:53 -07001230 pci_remove_bus_device(func->pci_dev);
1231 pci_dev_put(func->pci_dev);
1232 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 slot->flags &= (~SLOT_ENABLED);
1236
1237 err_exit:
1238 return retval;
1239}
1240
1241
1242/**
1243 * get_slot_status - get ACPI slot status
1244 *
1245 * if a slot has _STA for each function and if any one of them
1246 * returned non-zero status, return it
1247 *
1248 * if a slot doesn't have _STA and if any one of its functions'
1249 * configuration space is configured, return 0x0f as a _STA
1250 *
1251 * otherwise return 0
1252 */
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
1285 */
1286static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1287{
1288 acpi_status status;
1289 struct acpiphp_func *func;
1290 struct list_head *l;
1291 struct acpi_object_list arg_list;
1292 union acpi_object arg;
1293
1294 list_for_each (l, &slot->funcs) {
1295 func = list_entry(l, struct acpiphp_func, sibling);
1296
1297 /* We don't want to call _EJ0 on non-existing functions. */
1298 if ((func->flags & FUNC_HAS_EJ0)) {
1299 /* _EJ0 method take one argument */
1300 arg_list.count = 1;
1301 arg_list.pointer = &arg;
1302 arg.type = ACPI_TYPE_INTEGER;
1303 arg.integer.value = 1;
1304
1305 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1306 if (ACPI_FAILURE(status)) {
1307 warn("%s: _EJ0 failed\n", __FUNCTION__);
1308 return -1;
1309 } else
1310 break;
1311 }
1312 }
1313 return 0;
1314}
1315
1316/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 * acpiphp_check_bridge - re-enumerate devices
1318 *
1319 * Iterate over all slots under this bridge and make sure that if a
1320 * card is present they are enabled, and if not they are disabled.
1321 */
1322static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1323{
1324 struct acpiphp_slot *slot;
1325 int retval = 0;
1326 int enabled, disabled;
1327
1328 enabled = disabled = 0;
1329
1330 for (slot = bridge->slots; slot; slot = slot->next) {
1331 unsigned int status = get_slot_status(slot);
1332 if (slot->flags & SLOT_ENABLED) {
1333 if (status == ACPI_STA_ALL)
1334 continue;
1335 retval = acpiphp_disable_slot(slot);
1336 if (retval) {
1337 err("Error occurred in disabling\n");
1338 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001339 } else {
1340 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 disabled++;
1343 } else {
1344 if (status != ACPI_STA_ALL)
1345 continue;
1346 retval = acpiphp_enable_slot(slot);
1347 if (retval) {
1348 err("Error occurred in enabling\n");
1349 goto err_exit;
1350 }
1351 enabled++;
1352 }
1353 }
1354
1355 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1356
1357 err_exit:
1358 return retval;
1359}
1360
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001361static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1362{
1363 u16 pci_cmd, pci_bctl;
1364 struct pci_dev *cdev;
1365
1366 /* Program hpp values for this device */
1367 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1368 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1369 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1370 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001371
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001372 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001373 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001374 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001375 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001376 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001377 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001378 pci_cmd |= PCI_COMMAND_SERR;
1379 else
1380 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001381 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001382 pci_cmd |= PCI_COMMAND_PARITY;
1383 else
1384 pci_cmd &= ~PCI_COMMAND_PARITY;
1385 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1386
1387 /* Program bridge control value and child devices */
1388 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1389 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001390 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001391 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001392 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001393 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1394 else
1395 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001396 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001397 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1398 else
1399 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1400 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1401 if (dev->subordinate) {
1402 list_for_each_entry(cdev, &dev->subordinate->devices,
1403 bus_list)
1404 program_hpp(cdev, bridge);
1405 }
1406 }
1407}
1408
1409static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1410{
1411 struct acpiphp_bridge bridge;
1412 struct pci_dev *dev;
1413
1414 memset(&bridge, 0, sizeof(bridge));
1415 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001416 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001417 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001418 decode_hpp(&bridge);
1419 list_for_each_entry(dev, &bus->devices, bus_list)
1420 program_hpp(dev, &bridge);
1421
1422}
1423
1424/*
1425 * Remove devices for which we could not assign resources, call
1426 * arch specific code to fix-up the bus
1427 */
1428static void acpiphp_sanitize_bus(struct pci_bus *bus)
1429{
1430 struct pci_dev *dev;
1431 int i;
1432 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1433
1434 list_for_each_entry(dev, &bus->devices, bus_list) {
1435 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1436 struct resource *res = &dev->resource[i];
1437 if ((res->flags & type_mask) && !res->start &&
1438 res->end) {
1439 /* Could not assign a required resources
1440 * for this device, remove it */
1441 pci_remove_bus_device(dev);
1442 break;
1443 }
1444 }
1445 }
1446}
1447
1448/* Program resources in newly inserted bridge */
1449static int acpiphp_configure_bridge (acpi_handle handle)
1450{
1451 struct acpi_pci_id pci_id;
1452 struct pci_bus *bus;
1453
1454 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1455 err("cannot get PCI domain and bus number for bridge\n");
1456 return -EINVAL;
1457 }
1458 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1459 if (!bus) {
1460 err("cannot find bus %d:%d\n",
1461 pci_id.segment, pci_id.bus);
1462 return -EINVAL;
1463 }
1464
1465 pci_bus_size_bridges(bus);
1466 pci_bus_assign_resources(bus);
1467 acpiphp_sanitize_bus(bus);
1468 acpiphp_set_hpp_values(handle, bus);
1469 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001470 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001471 return 0;
1472}
1473
1474static void handle_bridge_insertion(acpi_handle handle, u32 type)
1475{
1476 struct acpi_device *device, *pdevice;
1477 acpi_handle phandle;
1478
1479 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1480 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1481 err("unexpected notification type %d\n", type);
1482 return;
1483 }
1484
1485 acpi_get_parent(handle, &phandle);
1486 if (acpi_bus_get_device(phandle, &pdevice)) {
1487 dbg("no parent device, assuming NULL\n");
1488 pdevice = NULL;
1489 }
1490 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1491 err("cannot add bridge to acpi list\n");
1492 return;
1493 }
1494 if (!acpiphp_configure_bridge(handle) &&
1495 !acpi_bus_start(device))
1496 add_bridge(handle);
1497 else
1498 err("cannot configure and start bridge\n");
1499
1500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/*
1503 * ACPI event handlers
1504 */
1505
1506/**
1507 * handle_hotplug_event_bridge - handle ACPI event on bridges
1508 *
1509 * @handle: Notify()'ed acpi_handle
1510 * @type: Notify code
1511 * @context: pointer to acpiphp_bridge structure
1512 *
1513 * handles ACPI event notification on {host,p2p} bridges
1514 *
1515 */
1516static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1517{
1518 struct acpiphp_bridge *bridge;
1519 char objname[64];
1520 struct acpi_buffer buffer = { .length = sizeof(objname),
1521 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001522 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001524 if (acpi_bus_get_device(handle, &device)) {
1525 /* This bridge must have just been physically inserted */
1526 handle_bridge_insertion(handle, type);
1527 return;
1528 }
1529
1530 bridge = acpiphp_handle_to_bridge(handle);
1531 if (!bridge) {
1532 err("cannot get bridge info\n");
1533 return;
1534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1537
1538 switch (type) {
1539 case ACPI_NOTIFY_BUS_CHECK:
1540 /* bus re-enumerate */
1541 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1542 acpiphp_check_bridge(bridge);
1543 break;
1544
1545 case ACPI_NOTIFY_DEVICE_CHECK:
1546 /* device check */
1547 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1548 acpiphp_check_bridge(bridge);
1549 break;
1550
1551 case ACPI_NOTIFY_DEVICE_WAKE:
1552 /* wake event */
1553 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1554 break;
1555
1556 case ACPI_NOTIFY_EJECT_REQUEST:
1557 /* request device eject */
1558 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001559 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1560 (bridge->flags & BRIDGE_HAS_EJ0)) {
1561 struct acpiphp_slot *slot;
1562 slot = bridge->func->slot;
1563 if (!acpiphp_disable_slot(slot))
1564 acpiphp_eject_slot(slot);
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567
1568 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1569 printk(KERN_ERR "Device %s cannot be configured due"
1570 " to a frequency mismatch\n", objname);
1571 break;
1572
1573 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1574 printk(KERN_ERR "Device %s cannot be configured due"
1575 " to a bus mode mismatch\n", objname);
1576 break;
1577
1578 case ACPI_NOTIFY_POWER_FAULT:
1579 printk(KERN_ERR "Device %s has suffered a power fault\n",
1580 objname);
1581 break;
1582
1583 default:
1584 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1585 break;
1586 }
1587}
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589/**
1590 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1591 *
1592 * @handle: Notify()'ed acpi_handle
1593 * @type: Notify code
1594 * @context: pointer to acpiphp_func structure
1595 *
1596 * handles ACPI event notification on slots
1597 *
1598 */
Len Brown2b85e132006-06-27 01:50:14 -04001599static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct acpiphp_func *func;
1602 char objname[64];
1603 struct acpi_buffer buffer = { .length = sizeof(objname),
1604 .pointer = objname };
1605
1606 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1607
1608 func = (struct acpiphp_func *)context;
1609
1610 switch (type) {
1611 case ACPI_NOTIFY_BUS_CHECK:
1612 /* bus re-enumerate */
1613 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1614 acpiphp_enable_slot(func->slot);
1615 break;
1616
1617 case ACPI_NOTIFY_DEVICE_CHECK:
1618 /* device check : re-enumerate from parent bus */
1619 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1620 acpiphp_check_bridge(func->slot->bridge);
1621 break;
1622
1623 case ACPI_NOTIFY_DEVICE_WAKE:
1624 /* wake event */
1625 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1626 break;
1627
1628 case ACPI_NOTIFY_EJECT_REQUEST:
1629 /* request device eject */
1630 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001631 if (!(acpiphp_disable_slot(func->slot)))
1632 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634
1635 default:
1636 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1637 break;
1638 }
1639}
1640
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001641
1642static acpi_status
1643find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1644{
1645 int *count = (int *)context;
1646
Kristen Accardi783c49f2006-03-03 10:16:05 -08001647 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001648 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1649 handle_hotplug_event_bridge, NULL);
1650 (*count)++;
1651 }
1652 return AE_OK ;
1653}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655static struct acpi_pci_driver acpi_pci_hp_driver = {
1656 .add = add_bridge,
1657 .remove = remove_bridge,
1658};
1659
1660/**
1661 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1662 *
1663 */
1664int __init acpiphp_glue_init(void)
1665{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001666 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001668 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1669 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 if (num <= 0)
1672 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001673 else
1674 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 return 0;
1677}
1678
1679
1680/**
1681 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1682 *
1683 * This function frees all data allocated in acpiphp_glue_init()
1684 */
1685void __exit acpiphp_glue_exit(void)
1686{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1688}
1689
1690
1691/**
1692 * acpiphp_get_num_slots - count number of slots in a system
1693 */
1694int __init acpiphp_get_num_slots(void)
1695{
1696 struct list_head *node;
1697 struct acpiphp_bridge *bridge;
1698 int num_slots;
1699
1700 num_slots = 0;
1701
1702 list_for_each (node, &bridge_list) {
1703 bridge = (struct acpiphp_bridge *)node;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001704 dbg("Bus %04x:%02x has %d slot%s\n",
1705 pci_domain_nr(bridge->pci_bus),
1706 bridge->pci_bus->number, bridge->nr_slots,
1707 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 num_slots += bridge->nr_slots;
1709 }
1710
Rajesh Shah42f49a62005-04-28 00:25:53 -07001711 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return num_slots;
1713}
1714
1715
1716#if 0
1717/**
1718 * acpiphp_for_each_slot - call function for each slot
1719 * @fn: callback function
1720 * @data: context to be passed to callback function
1721 *
1722 */
1723static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1724{
1725 struct list_head *node;
1726 struct acpiphp_bridge *bridge;
1727 struct acpiphp_slot *slot;
1728 int retval = 0;
1729
1730 list_for_each (node, &bridge_list) {
1731 bridge = (struct acpiphp_bridge *)node;
1732 for (slot = bridge->slots; slot; slot = slot->next) {
1733 retval = fn(slot, data);
1734 if (!retval)
1735 goto err_exit;
1736 }
1737 }
1738
1739 err_exit:
1740 return retval;
1741}
1742#endif
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745/**
1746 * acpiphp_enable_slot - power on slot
1747 */
1748int acpiphp_enable_slot(struct acpiphp_slot *slot)
1749{
1750 int retval;
1751
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001752 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /* wake up all functions */
1755 retval = power_on_slot(slot);
1756 if (retval)
1757 goto err_exit;
1758
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001759 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 /* configure all functions */
1761 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001762 if (retval)
1763 power_off_slot(slot);
1764 } else {
1765 dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__);
1766 power_off_slot(slot);
1767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001770 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return retval;
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/**
1775 * acpiphp_disable_slot - power off slot
1776 */
1777int acpiphp_disable_slot(struct acpiphp_slot *slot)
1778{
1779 int retval = 0;
1780
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001781 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* unconfigure all functions */
1784 retval = disable_device(slot);
1785 if (retval)
1786 goto err_exit;
1787
1788 /* power off all functions */
1789 retval = power_off_slot(slot);
1790 if (retval)
1791 goto err_exit;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001794 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return retval;
1796}
1797
1798
1799/*
1800 * slot enabled: 1
1801 * slot disabled: 0
1802 */
1803u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1804{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001805 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
1808
1809/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001810 * latch open: 1
1811 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
1813u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1814{
1815 unsigned int sta;
1816
1817 sta = get_slot_status(slot);
1818
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001819 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
1822
1823/*
1824 * adapter presence : 1
1825 * absence : 0
1826 */
1827u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1828{
1829 unsigned int sta;
1830
1831 sta = get_slot_status(slot);
1832
1833 return (sta == 0) ? 0 : 1;
1834}
1835
1836
1837/*
1838 * pci address (seg/bus/dev)
1839 */
1840u32 acpiphp_get_address(struct acpiphp_slot *slot)
1841{
1842 u32 address;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001843 struct pci_bus *pci_bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Rajesh Shah42f49a62005-04-28 00:25:53 -07001845 address = (pci_domain_nr(pci_bus) << 16) |
1846 (pci_bus->number << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 slot->device;
1848
1849 return address;
1850}