blob: 631efce3a6ce98832f87f180996bf3f7e8dd6d26 [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 *
29 * Send feedback to <t-kochi@bq.jp.nec.com>
30 *
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>
48#include <linux/smp_lock.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"
52#include "pci_hotplug.h"
53#include "acpiphp.h"
54
55static LIST_HEAD(bridge_list);
56
57#define MY_NAME "acpiphp_glue"
58
59static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070060static void acpiphp_sanitize_bus(struct pci_bus *bus);
61static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * initialization & terminatation routines
66 */
67
68/**
69 * is_ejectable - determine if a slot is ejectable
70 * @handle: handle to acpi namespace
71 *
72 * Ejectable slot should satisfy at least these conditions:
73 *
74 * 1. has _ADR method
75 * 2. has _EJ0 method
76 *
77 * optionally
78 *
79 * 1. has _STA method
80 * 2. has _PS0 method
81 * 3. has _PS3 method
82 * 4. ..
83 *
84 */
85static int is_ejectable(acpi_handle handle)
86{
87 acpi_status status;
88 acpi_handle tmp;
89
90 status = acpi_get_handle(handle, "_ADR", &tmp);
91 if (ACPI_FAILURE(status)) {
92 return 0;
93 }
94
95 status = acpi_get_handle(handle, "_EJ0", &tmp);
96 if (ACPI_FAILURE(status)) {
97 return 0;
98 }
99
100 return 1;
101}
102
103
104/* callback routine to check the existence of ejectable slots */
105static acpi_status
106is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
107{
108 int *count = (int *)context;
109
110 if (is_ejectable(handle)) {
111 (*count)++;
112 /* only one ejectable slot is enough */
113 return AE_CTRL_TERMINATE;
114 } else {
115 return AE_OK;
116 }
117}
118
119
120/* callback routine to register each ACPI PCI slot object */
121static acpi_status
122register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
123{
124 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
125 struct acpiphp_slot *slot;
126 struct acpiphp_func *newfunc;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800127 struct dependent_device *dd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 acpi_handle tmp;
129 acpi_status status = AE_OK;
130 unsigned long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800131 int device, function, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
134
135 if (ACPI_FAILURE(status))
136 return AE_OK;
137
138 status = acpi_get_handle(handle, "_EJ0", &tmp);
139
Kristen Accardi20416ea2006-02-23 17:56:03 -0800140 if (ACPI_FAILURE(status) && !(is_dependent_device(handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return AE_OK;
142
143 device = (adr >> 16) & 0xffff;
144 function = adr & 0xffff;
145
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100146 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!newfunc)
148 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 INIT_LIST_HEAD(&newfunc->sibling);
151 newfunc->handle = handle;
152 newfunc->function = function;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800153 if (ACPI_SUCCESS(status))
154 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
157 newfunc->flags |= FUNC_HAS_STA;
158
159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
160 newfunc->flags |= FUNC_HAS_PS0;
161
162 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
163 newfunc->flags |= FUNC_HAS_PS3;
164
Kristen Accardi20416ea2006-02-23 17:56:03 -0800165 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) {
166 newfunc->flags |= FUNC_HAS_DCK;
167 /* add to devices dependent on dock station,
168 * because this may actually be the dock bridge
169 */
170 dd = alloc_dependent_device(handle);
171 if (!dd)
172 err("Can't allocate memory for "
173 "new dependent device!\n");
174 else
175 add_dependent_device(dd);
176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
179 if (ACPI_FAILURE(status))
180 sun = -1;
181
182 /* search for objects that share the same slot */
183 for (slot = bridge->slots; slot; slot = slot->next)
184 if (slot->device == device) {
185 if (slot->sun != sun)
186 warn("sibling found, but _SUN doesn't match!\n");
187 break;
188 }
189
190 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100191 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!slot) {
193 kfree(newfunc);
194 return AE_NO_MEMORY;
195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 slot->device = device;
199 slot->sun = sun;
200 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100201 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 slot->next = bridge->slots;
204 bridge->slots = slot;
205
206 bridge->nr_slots++;
207
Rajesh Shah42f49a62005-04-28 00:25:53 -0700208 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
209 slot->sun, pci_domain_nr(bridge->pci_bus),
210 bridge->pci_bus->number, slot->device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800211 retval = acpiphp_register_hotplug_slot(slot);
212 if (retval) {
213 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
214 goto err_exit;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 newfunc->slot = slot;
219 list_add_tail(&newfunc->sibling, &slot->funcs);
220
221 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700222 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 PCI_DEVFN(device, function));
224 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
226 }
227
Kristen Accardi20416ea2006-02-23 17:56:03 -0800228 /* if this is a device dependent on a dock station,
229 * associate the acpiphp_func to the dependent_device
230 * struct.
231 */
232 if ((dd = get_dependent_device(handle))) {
233 newfunc->flags |= FUNC_IS_DD;
234 /*
235 * we don't want any devices which is dependent
236 * on the dock to have it's _EJ0 method executed.
237 * because we need to run _DCK first.
238 */
239 newfunc->flags &= ~FUNC_HAS_EJ0;
240 dd->func = newfunc;
241 add_pci_dependent_device(dd);
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800245 if (!(newfunc->flags & FUNC_HAS_DCK)) {
246 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ACPI_SYSTEM_NOTIFY,
248 handle_hotplug_event_func,
249 newfunc);
250
Kristen Accardi20416ea2006-02-23 17:56:03 -0800251 if (ACPI_FAILURE(status))
252 err("failed to register interrupt notify handler\n");
253 } else
254 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Kristen Accardi20416ea2006-02-23 17:56:03 -0800256 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800257
258 err_exit:
259 bridge->nr_slots--;
260 bridge->slots = slot->next;
261 kfree(slot);
262 kfree(newfunc);
263
264 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267
268/* see if it's worth looking at this bridge */
269static int detect_ejectable_slots(acpi_handle *bridge_handle)
270{
271 acpi_status status;
272 int count;
273
274 count = 0;
275
276 /* only check slots defined directly below bridge object */
277 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
278 is_ejectable_slot, (void *)&count, NULL);
279
280 return count;
281}
282
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* decode ACPI 2.0 _HPP hot plug parameters */
285static void decode_hpp(struct acpiphp_bridge *bridge)
286{
287 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Kristen Accardi783c49f2006-03-03 10:16:05 -0800289 status = acpi_get_hp_params_from_firmware(bridge->pci_dev, &bridge->hpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (ACPI_FAILURE(status)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800291 /* use default numbers */
292 bridge->hpp.cache_line_size = 0x10;
293 bridge->hpp.latency_timer = 0x40;
294 bridge->hpp.enable_serr = 0;
295 bridge->hpp.enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299
Kristen Accardi783c49f2006-03-03 10:16:05 -0800300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
302static void init_bridge_misc(struct acpiphp_bridge *bridge)
303{
304 acpi_status status;
305
306 /* decode ACPI 2.0 _HPP (hot plug parameters) */
307 decode_hpp(bridge);
308
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800309 /* must be added to the list prior to calling register_slot */
310 list_add(&bridge->list, &bridge_list);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* register all slot objects under this bridge */
313 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
314 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800315 if (ACPI_FAILURE(status)) {
316 list_del(&bridge->list);
317 return;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700321 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900322 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
323 status = acpi_remove_notify_handler(bridge->func->handle,
324 ACPI_SYSTEM_NOTIFY,
325 handle_hotplug_event_func);
326 if (ACPI_FAILURE(status))
327 err("failed to remove notify handler\n");
328 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700329 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ACPI_SYSTEM_NOTIFY,
331 handle_hotplug_event_bridge,
332 bridge);
333
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700334 if (ACPI_FAILURE(status)) {
335 err("failed to register interrupt notify handler\n");
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900341/* find acpiphp_func from acpiphp_bridge */
342static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
343{
344 struct list_head *node, *l;
345 struct acpiphp_bridge *bridge;
346 struct acpiphp_slot *slot;
347 struct acpiphp_func *func;
348
349 list_for_each(node, &bridge_list) {
350 bridge = list_entry(node, struct acpiphp_bridge, list);
351 for (slot = bridge->slots; slot; slot = slot->next) {
352 list_for_each(l, &slot->funcs) {
353 func = list_entry(l, struct acpiphp_func,
354 sibling);
355 if (func->handle == handle)
356 return func;
357 }
358 }
359 }
360
361 return NULL;
362}
363
364
365static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
366{
367 acpi_handle dummy_handle;
368
369 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
370 "_STA", &dummy_handle)))
371 bridge->flags |= BRIDGE_HAS_STA;
372
373 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
374 "_EJ0", &dummy_handle)))
375 bridge->flags |= BRIDGE_HAS_EJ0;
376
377 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
378 "_PS0", &dummy_handle)))
379 bridge->flags |= BRIDGE_HAS_PS0;
380
381 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
382 "_PS3", &dummy_handle)))
383 bridge->flags |= BRIDGE_HAS_PS3;
384
385 /* is this ejectable p2p bridge? */
386 if (bridge->flags & BRIDGE_HAS_EJ0) {
387 struct acpiphp_func *func;
388
389 dbg("found ejectable p2p bridge\n");
390
391 /* make link between PCI bridge and PCI function */
392 func = acpiphp_bridge_handle_to_function(bridge->handle);
393 if (!func)
394 return;
395 bridge->func = func;
396 func->bridge = bridge;
397 }
398}
399
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700402static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct acpiphp_bridge *bridge;
405
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100406 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (bridge == NULL)
408 return;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 bridge->type = BRIDGE_TYPE_HOST;
411 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Rajesh Shah42f49a62005-04-28 00:25:53 -0700413 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 spin_lock_init(&bridge->res_lock);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 init_bridge_misc(bridge);
418}
419
420
421/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700422static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100426 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (bridge == NULL) {
428 err("out of memory\n");
429 return;
430 }
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 bridge->type = BRIDGE_TYPE_P2P;
433 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900434 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Rajesh Shah42f49a62005-04-28 00:25:53 -0700436 bridge->pci_dev = pci_dev_get(pci_dev);
437 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!bridge->pci_bus) {
439 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700440 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 spin_lock_init(&bridge->res_lock);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700446 return;
447 err:
448 pci_dev_put(pci_dev);
449 kfree(bridge);
450 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453
454/* callback routine to find P2P bridges */
455static acpi_status
456find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
457{
458 acpi_status status;
459 acpi_handle dummy_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 unsigned long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700461 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700463 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
466 if (ACPI_FAILURE(status))
467 return AE_OK; /* continue */
468
469 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
470 if (ACPI_FAILURE(status)) {
471 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
472 return AE_OK;
473 }
474
475 device = (tmp >> 16) & 0xffff;
476 function = tmp & 0xffff;
477
Rajesh Shah42f49a62005-04-28 00:25:53 -0700478 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Rajesh Shah42f49a62005-04-28 00:25:53 -0700480 if (!dev || !dev->subordinate)
481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* check if this bridge has ejectable slots */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800484 if ((detect_ejectable_slots(handle) > 0) ||
485 (detect_dependent_devices(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700487 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900490 /* search P2P bridges under this p2p bridge */
491 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
492 find_p2p_bridge, dev->subordinate, NULL);
493 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900494 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900495
Rajesh Shah42f49a62005-04-28 00:25:53 -0700496 out:
497 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return AE_OK;
499}
500
501
502/* find hot-pluggable slots, and then find P2P bridge */
503static int add_bridge(acpi_handle handle)
504{
505 acpi_status status;
506 unsigned long tmp;
507 int seg, bus;
508 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700509 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* if the bridge doesn't have _STA, we assume it is always there */
512 status = acpi_get_handle(handle, "_STA", &dummy_handle);
513 if (ACPI_SUCCESS(status)) {
514 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
515 if (ACPI_FAILURE(status)) {
516 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
517 return 0;
518 }
519 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
520 /* don't register this object */
521 return 0;
522 }
523
524 /* get PCI segment number */
525 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
526
527 seg = ACPI_SUCCESS(status) ? tmp : 0;
528
529 /* get PCI bus number */
530 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
531
532 if (ACPI_SUCCESS(status)) {
533 bus = tmp;
534 } else {
535 warn("can't get bus number, assuming 0\n");
536 bus = 0;
537 }
538
Rajesh Shah42f49a62005-04-28 00:25:53 -0700539 pci_bus = pci_find_bus(seg, bus);
540 if (!pci_bus) {
541 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
543 }
544
Rajesh Shah42f49a62005-04-28 00:25:53 -0700545 /* check if this bridge has ejectable slots */
546 if (detect_ejectable_slots(handle) > 0) {
547 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
548 add_host_bridge(handle, pci_bus);
549 return 0;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* search P2P bridges under this host bridge */
553 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700554 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900557 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 return 0;
560}
561
Rajesh Shah42f49a62005-04-28 00:25:53 -0700562static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
563{
564 struct list_head *head;
565 list_for_each(head, &bridge_list) {
566 struct acpiphp_bridge *bridge = list_entry(head,
567 struct acpiphp_bridge, list);
568 if (bridge->handle == handle)
569 return bridge;
570 }
571
572 return NULL;
573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Rajesh Shah364d5092005-04-28 00:25:54 -0700575static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700577 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700578 struct acpiphp_slot *slot;
579 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700580 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700581
582 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
583 handle_hotplug_event_bridge);
584 if (ACPI_FAILURE(status))
585 err("failed to remove notify handler\n");
586
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900587 if ((bridge->type != BRIDGE_TYPE_HOST) &&
588 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
589 status = acpi_install_notify_handler(bridge->func->handle,
590 ACPI_SYSTEM_NOTIFY,
591 handle_hotplug_event_func,
592 bridge->func);
593 if (ACPI_FAILURE(status))
594 err("failed to install interrupt notify handler\n");
595 }
596
Rajesh Shah42f49a62005-04-28 00:25:53 -0700597 slot = bridge->slots;
598 while (slot) {
599 struct acpiphp_slot *next = slot->next;
600 list_for_each_safe (list, tmp, &slot->funcs) {
601 struct acpiphp_func *func;
602 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800603 if (!(func->flags & FUNC_HAS_DCK)) {
604 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700605 ACPI_SYSTEM_NOTIFY,
606 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800607 if (ACPI_FAILURE(status))
608 err("failed to remove notify handler\n");
609 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700610 pci_dev_put(func->pci_dev);
611 list_del(list);
612 kfree(func);
613 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800614 acpiphp_unregister_hotplug_slot(slot);
615 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700616 kfree(slot);
617 slot = next;
618 }
619
620 pci_dev_put(bridge->pci_dev);
621 list_del(&bridge->list);
622 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Rajesh Shah364d5092005-04-28 00:25:54 -0700625static acpi_status
626cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
627{
628 struct acpiphp_bridge *bridge;
629
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900630 /* cleanup p2p bridges under this P2P bridge
631 in a depth-first manner */
632 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
633 cleanup_p2p_bridge, NULL, NULL);
634
Rajesh Shah364d5092005-04-28 00:25:54 -0700635 if (!(bridge = acpiphp_handle_to_bridge(handle)))
636 return AE_OK;
637 cleanup_bridge(bridge);
638 return AE_OK;
639}
640
641static void remove_bridge(acpi_handle handle)
642{
643 struct acpiphp_bridge *bridge;
644
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900645 /* cleanup p2p bridges under this host bridge
646 in a depth-first manner */
647 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
648 (u32)1, cleanup_p2p_bridge, NULL, NULL);
649
Rajesh Shah364d5092005-04-28 00:25:54 -0700650 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900651 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700652 cleanup_bridge(bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700653}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700655static struct pci_dev * get_apic_pci_info(acpi_handle handle)
656{
657 struct acpi_pci_id id;
658 struct pci_bus *bus;
659 struct pci_dev *dev;
660
661 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
662 return NULL;
663
664 bus = pci_find_bus(id.segment, id.bus);
665 if (!bus)
666 return NULL;
667
668 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
669 if (!dev)
670 return NULL;
671
672 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
673 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
674 {
675 pci_dev_put(dev);
676 return NULL;
677 }
678
679 return dev;
680}
681
682static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
683{
684 acpi_status status;
685 int result = -1;
686 unsigned long gsb;
687 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
688 union acpi_object *obj;
689 void *table;
690
691 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
692 if (ACPI_SUCCESS(status)) {
693 *gsi_base = (u32)gsb;
694 return 0;
695 }
696
697 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
698 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
699 return -1;
700
701 obj = buffer.pointer;
702 if (obj->type != ACPI_TYPE_BUFFER)
703 goto out;
704
705 table = obj->buffer.pointer;
706 switch (((acpi_table_entry_header *)table)->type) {
707 case ACPI_MADT_IOSAPIC:
708 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
709 result = 0;
710 break;
711 case ACPI_MADT_IOAPIC:
712 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
713 result = 0;
714 break;
715 default:
716 break;
717 }
718 out:
719 acpi_os_free(buffer.pointer);
720 return result;
721}
722
723static acpi_status
724ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
725{
726 acpi_status status;
727 unsigned long sta;
728 acpi_handle tmp;
729 struct pci_dev *pdev;
730 u32 gsi_base;
731 u64 phys_addr;
732
733 /* Evaluate _STA if present */
734 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
735 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
736 return AE_CTRL_DEPTH;
737
738 /* Scan only PCI bus scope */
739 status = acpi_get_handle(handle, "_HID", &tmp);
740 if (ACPI_SUCCESS(status))
741 return AE_CTRL_DEPTH;
742
743 if (get_gsi_base(handle, &gsi_base))
744 return AE_OK;
745
746 pdev = get_apic_pci_info(handle);
747 if (!pdev)
748 return AE_OK;
749
750 if (pci_enable_device(pdev)) {
751 pci_dev_put(pdev);
752 return AE_OK;
753 }
754
755 pci_set_master(pdev);
756
757 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
758 pci_disable_device(pdev);
759 pci_dev_put(pdev);
760 return AE_OK;
761 }
762
763 phys_addr = pci_resource_start(pdev, 0);
764 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
765 pci_release_region(pdev, 0);
766 pci_disable_device(pdev);
767 pci_dev_put(pdev);
768 return AE_OK;
769 }
770
771 return AE_OK;
772}
773
774static int acpiphp_configure_ioapics(acpi_handle handle)
775{
776 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
777 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
778 return 0;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int power_on_slot(struct acpiphp_slot *slot)
782{
783 acpi_status status;
784 struct acpiphp_func *func;
785 struct list_head *l;
786 int retval = 0;
787
788 /* if already enabled, just skip */
789 if (slot->flags & SLOT_POWEREDON)
790 goto err_exit;
791
792 list_for_each (l, &slot->funcs) {
793 func = list_entry(l, struct acpiphp_func, sibling);
794
795 if (func->flags & FUNC_HAS_PS0) {
796 dbg("%s: executing _PS0\n", __FUNCTION__);
797 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
798 if (ACPI_FAILURE(status)) {
799 warn("%s: _PS0 failed\n", __FUNCTION__);
800 retval = -1;
801 goto err_exit;
802 } else
803 break;
804 }
805 }
806
807 /* TBD: evaluate _STA to check if the slot is enabled */
808
809 slot->flags |= SLOT_POWEREDON;
810
811 err_exit:
812 return retval;
813}
814
815
816static int power_off_slot(struct acpiphp_slot *slot)
817{
818 acpi_status status;
819 struct acpiphp_func *func;
820 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 int retval = 0;
823
824 /* if already disabled, just skip */
825 if ((slot->flags & SLOT_POWEREDON) == 0)
826 goto err_exit;
827
828 list_for_each (l, &slot->funcs) {
829 func = list_entry(l, struct acpiphp_func, sibling);
830
Rajesh Shah2f523b12005-04-28 00:25:55 -0700831 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
833 if (ACPI_FAILURE(status)) {
834 warn("%s: _PS3 failed\n", __FUNCTION__);
835 retval = -1;
836 goto err_exit;
837 } else
838 break;
839 }
840 }
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* TBD: evaluate _STA to check if the slot is disabled */
843
844 slot->flags &= (~SLOT_POWEREDON);
845
846 err_exit:
847 return retval;
848}
849
850
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800851
852/**
853 * acpiphp_max_busnr - return the highest reserved bus number under
854 * the given bus.
855 * @bus: bus to start search with
856 *
857 */
858static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
859{
860 struct list_head *tmp;
861 unsigned char max, n;
862
863 /*
864 * pci_bus_max_busnr will return the highest
865 * reserved busnr for all these children.
866 * that is equivalent to the bus->subordinate
867 * value. We don't want to use the parent's
868 * bus->subordinate value because it could have
869 * padding in it.
870 */
871 max = bus->secondary;
872
873 list_for_each(tmp, &bus->children) {
874 n = pci_bus_max_busnr(pci_bus_b(tmp));
875 if (n > max)
876 max = n;
877 }
878 return max;
879}
880
881
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800882/**
883 * acpiphp_bus_add - add a new bus to acpi subsystem
884 * @func: acpiphp_func of the bridge
885 *
886 */
887static int acpiphp_bus_add(struct acpiphp_func *func)
888{
889 acpi_handle phandle;
890 struct acpi_device *device, *pdevice;
891 int ret_val;
892
893 acpi_get_parent(func->handle, &phandle);
894 if (acpi_bus_get_device(phandle, &pdevice)) {
895 dbg("no parent device, assuming NULL\n");
896 pdevice = NULL;
897 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800898 if (!acpi_bus_get_device(func->handle, &device)) {
899 dbg("bus exists... trim\n");
900 /* this shouldn't be in here, so remove
901 * the bus then re-add it...
902 */
903 ret_val = acpi_bus_trim(device, 1);
904 dbg("acpi_bus_trim return %x\n", ret_val);
905 }
906
907 ret_val = acpi_bus_add(&device, pdevice, func->handle,
908 ACPI_BUS_TYPE_DEVICE);
909 if (ret_val) {
910 dbg("error adding bus, %x\n",
911 -ret_val);
912 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800913 }
914 /*
915 * try to start anyway. We could have failed to add
916 * simply because this bus had previously been added
917 * on another add. Don't bother with the return value
918 * we just keep going.
919 */
920 ret_val = acpi_bus_start(device);
921
922acpiphp_bus_add_out:
923 return ret_val;
924}
925
926
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900927/**
928 * acpiphp_bus_trim - trim a bus from acpi subsystem
929 * @handle: handle to acpi namespace
930 *
931 */
932int acpiphp_bus_trim(acpi_handle handle)
933{
934 struct acpi_device *device;
935 int retval;
936
937 retval = acpi_bus_get_device(handle, &device);
938 if (retval) {
939 dbg("acpi_device not found\n");
940 return retval;
941 }
942
943 retval = acpi_bus_trim(device, 1);
944 if (retval)
945 err("cannot remove from acpi list\n");
946
947 return retval;
948}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/**
951 * enable_device - enable, configure a slot
952 * @slot: slot to be enabled
953 *
954 * This function should be called per *physical slot*,
955 * not per each slot object in ACPI namespace.
956 *
957 */
958static int enable_device(struct acpiphp_slot *slot)
959{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700961 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct list_head *l;
963 struct acpiphp_func *func;
964 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700965 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900966 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (slot->flags & SLOT_ENABLED)
969 goto err_exit;
970
971 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700972 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (dev) {
974 /* This case shouldn't happen */
975 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700976 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 retval = -1;
978 goto err_exit;
979 }
980
Rajesh Shah42f49a62005-04-28 00:25:53 -0700981 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
982 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 err("No new device found\n");
984 retval = -1;
985 goto err_exit;
986 }
987
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800988 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700989 for (pass = 0; pass < 2; pass++) {
990 list_for_each_entry(dev, &bus->devices, bus_list) {
991 if (PCI_SLOT(dev->devfn) != slot->device)
992 continue;
993 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800994 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700995 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900996 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800997 pci_bus_size_bridges(dev->subordinate);
998 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001002 list_for_each (l, &slot->funcs) {
1003 func = list_entry(l, struct acpiphp_func, sibling);
1004 acpiphp_bus_add(func);
1005 }
1006
Rajesh Shah42f49a62005-04-28 00:25:53 -07001007 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001008 acpiphp_sanitize_bus(bus);
1009 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001010 pci_bus_add_devices(bus);
MUNEDA Takahiro0cccd0c2006-02-24 17:46:04 +09001011 acpiphp_set_hpp_values(slot->bridge->handle, bus);
1012 acpiphp_configure_ioapics(slot->bridge->handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* associate pci_dev to our representation */
1015 list_for_each (l, &slot->funcs) {
1016 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001017 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 func->function));
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001019 if (!func->pci_dev)
1020 continue;
1021
1022 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1023 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1024 continue;
1025
1026 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1027 if (ACPI_FAILURE(status))
1028 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1029 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032 slot->flags |= SLOT_ENABLED;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 err_exit:
1035 return retval;
1036}
1037
1038
1039/**
1040 * disable_device - disable a slot
1041 */
1042static int disable_device(struct acpiphp_slot *slot)
1043{
1044 int retval = 0;
1045 struct acpiphp_func *func;
1046 struct list_head *l;
1047
1048 /* is this slot already disabled? */
1049 if (!(slot->flags & SLOT_ENABLED))
1050 goto err_exit;
1051
1052 list_for_each (l, &slot->funcs) {
1053 func = list_entry(l, struct acpiphp_func, sibling);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001054
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001055 if (func->bridge) {
1056 /* cleanup p2p bridges under this P2P bridge */
1057 cleanup_p2p_bridge(func->bridge->handle,
1058 (u32)1, NULL, NULL);
1059 func->bridge = NULL;
1060 }
1061
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001062 acpiphp_bus_trim(func->handle);
1063 /* try to remove anyway.
1064 * acpiphp_bus_add might have been failed */
1065
Rajesh Shah42f49a62005-04-28 00:25:53 -07001066 if (!func->pci_dev)
1067 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Rajesh Shah42f49a62005-04-28 00:25:53 -07001069 pci_remove_bus_device(func->pci_dev);
1070 pci_dev_put(func->pci_dev);
1071 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
1074 slot->flags &= (~SLOT_ENABLED);
1075
1076 err_exit:
1077 return retval;
1078}
1079
1080
1081/**
1082 * get_slot_status - get ACPI slot status
1083 *
1084 * if a slot has _STA for each function and if any one of them
1085 * returned non-zero status, return it
1086 *
1087 * if a slot doesn't have _STA and if any one of its functions'
1088 * configuration space is configured, return 0x0f as a _STA
1089 *
1090 * otherwise return 0
1091 */
1092static unsigned int get_slot_status(struct acpiphp_slot *slot)
1093{
1094 acpi_status status;
1095 unsigned long sta = 0;
1096 u32 dvid;
1097 struct list_head *l;
1098 struct acpiphp_func *func;
1099
1100 list_for_each (l, &slot->funcs) {
1101 func = list_entry(l, struct acpiphp_func, sibling);
1102
1103 if (func->flags & FUNC_HAS_STA) {
1104 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1105 if (ACPI_SUCCESS(status) && sta)
1106 break;
1107 } else {
1108 pci_bus_read_config_dword(slot->bridge->pci_bus,
1109 PCI_DEVFN(slot->device,
1110 func->function),
1111 PCI_VENDOR_ID, &dvid);
1112 if (dvid != 0xffffffff) {
1113 sta = ACPI_STA_ALL;
1114 break;
1115 }
1116 }
1117 }
1118
1119 return (unsigned int)sta;
1120}
1121
1122/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001123 * acpiphp_eject_slot - physically eject the slot
1124 */
1125static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1126{
1127 acpi_status status;
1128 struct acpiphp_func *func;
1129 struct list_head *l;
1130 struct acpi_object_list arg_list;
1131 union acpi_object arg;
1132
1133 list_for_each (l, &slot->funcs) {
1134 func = list_entry(l, struct acpiphp_func, sibling);
1135
1136 /* We don't want to call _EJ0 on non-existing functions. */
1137 if ((func->flags & FUNC_HAS_EJ0)) {
1138 /* _EJ0 method take one argument */
1139 arg_list.count = 1;
1140 arg_list.pointer = &arg;
1141 arg.type = ACPI_TYPE_INTEGER;
1142 arg.integer.value = 1;
1143
1144 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1145 if (ACPI_FAILURE(status)) {
1146 warn("%s: _EJ0 failed\n", __FUNCTION__);
1147 return -1;
1148 } else
1149 break;
1150 }
1151 }
1152 return 0;
1153}
1154
1155/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 * acpiphp_check_bridge - re-enumerate devices
1157 *
1158 * Iterate over all slots under this bridge and make sure that if a
1159 * card is present they are enabled, and if not they are disabled.
1160 */
1161static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1162{
1163 struct acpiphp_slot *slot;
1164 int retval = 0;
1165 int enabled, disabled;
1166
1167 enabled = disabled = 0;
1168
1169 for (slot = bridge->slots; slot; slot = slot->next) {
1170 unsigned int status = get_slot_status(slot);
1171 if (slot->flags & SLOT_ENABLED) {
1172 if (status == ACPI_STA_ALL)
1173 continue;
1174 retval = acpiphp_disable_slot(slot);
1175 if (retval) {
1176 err("Error occurred in disabling\n");
1177 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001178 } else {
1179 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 disabled++;
1182 } else {
1183 if (status != ACPI_STA_ALL)
1184 continue;
1185 retval = acpiphp_enable_slot(slot);
1186 if (retval) {
1187 err("Error occurred in enabling\n");
1188 goto err_exit;
1189 }
1190 enabled++;
1191 }
1192 }
1193
1194 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1195
1196 err_exit:
1197 return retval;
1198}
1199
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001200static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1201{
1202 u16 pci_cmd, pci_bctl;
1203 struct pci_dev *cdev;
1204
1205 /* Program hpp values for this device */
1206 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1207 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1208 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1209 return;
1210 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1211 bridge->hpp.cache_line_size);
1212 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1213 bridge->hpp.latency_timer);
1214 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001215 if (bridge->hpp.enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001216 pci_cmd |= PCI_COMMAND_SERR;
1217 else
1218 pci_cmd &= ~PCI_COMMAND_SERR;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001219 if (bridge->hpp.enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001220 pci_cmd |= PCI_COMMAND_PARITY;
1221 else
1222 pci_cmd &= ~PCI_COMMAND_PARITY;
1223 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1224
1225 /* Program bridge control value and child devices */
1226 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1227 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1228 bridge->hpp.latency_timer);
1229 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001230 if (bridge->hpp.enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001231 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1232 else
1233 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001234 if (bridge->hpp.enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001235 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1236 else
1237 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1238 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1239 if (dev->subordinate) {
1240 list_for_each_entry(cdev, &dev->subordinate->devices,
1241 bus_list)
1242 program_hpp(cdev, bridge);
1243 }
1244 }
1245}
1246
1247static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1248{
1249 struct acpiphp_bridge bridge;
1250 struct pci_dev *dev;
1251
1252 memset(&bridge, 0, sizeof(bridge));
1253 bridge.handle = handle;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001254 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001255 decode_hpp(&bridge);
1256 list_for_each_entry(dev, &bus->devices, bus_list)
1257 program_hpp(dev, &bridge);
1258
1259}
1260
1261/*
1262 * Remove devices for which we could not assign resources, call
1263 * arch specific code to fix-up the bus
1264 */
1265static void acpiphp_sanitize_bus(struct pci_bus *bus)
1266{
1267 struct pci_dev *dev;
1268 int i;
1269 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1270
1271 list_for_each_entry(dev, &bus->devices, bus_list) {
1272 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1273 struct resource *res = &dev->resource[i];
1274 if ((res->flags & type_mask) && !res->start &&
1275 res->end) {
1276 /* Could not assign a required resources
1277 * for this device, remove it */
1278 pci_remove_bus_device(dev);
1279 break;
1280 }
1281 }
1282 }
1283}
1284
1285/* Program resources in newly inserted bridge */
1286static int acpiphp_configure_bridge (acpi_handle handle)
1287{
1288 struct acpi_pci_id pci_id;
1289 struct pci_bus *bus;
1290
1291 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1292 err("cannot get PCI domain and bus number for bridge\n");
1293 return -EINVAL;
1294 }
1295 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1296 if (!bus) {
1297 err("cannot find bus %d:%d\n",
1298 pci_id.segment, pci_id.bus);
1299 return -EINVAL;
1300 }
1301
1302 pci_bus_size_bridges(bus);
1303 pci_bus_assign_resources(bus);
1304 acpiphp_sanitize_bus(bus);
1305 acpiphp_set_hpp_values(handle, bus);
1306 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001307 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001308 return 0;
1309}
1310
1311static void handle_bridge_insertion(acpi_handle handle, u32 type)
1312{
1313 struct acpi_device *device, *pdevice;
1314 acpi_handle phandle;
1315
1316 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1317 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1318 err("unexpected notification type %d\n", type);
1319 return;
1320 }
1321
1322 acpi_get_parent(handle, &phandle);
1323 if (acpi_bus_get_device(phandle, &pdevice)) {
1324 dbg("no parent device, assuming NULL\n");
1325 pdevice = NULL;
1326 }
1327 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1328 err("cannot add bridge to acpi list\n");
1329 return;
1330 }
1331 if (!acpiphp_configure_bridge(handle) &&
1332 !acpi_bus_start(device))
1333 add_bridge(handle);
1334 else
1335 err("cannot configure and start bridge\n");
1336
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/*
1340 * ACPI event handlers
1341 */
1342
1343/**
1344 * handle_hotplug_event_bridge - handle ACPI event on bridges
1345 *
1346 * @handle: Notify()'ed acpi_handle
1347 * @type: Notify code
1348 * @context: pointer to acpiphp_bridge structure
1349 *
1350 * handles ACPI event notification on {host,p2p} bridges
1351 *
1352 */
1353static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1354{
1355 struct acpiphp_bridge *bridge;
1356 char objname[64];
1357 struct acpi_buffer buffer = { .length = sizeof(objname),
1358 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001359 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001361 if (acpi_bus_get_device(handle, &device)) {
1362 /* This bridge must have just been physically inserted */
1363 handle_bridge_insertion(handle, type);
1364 return;
1365 }
1366
1367 bridge = acpiphp_handle_to_bridge(handle);
1368 if (!bridge) {
1369 err("cannot get bridge info\n");
1370 return;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1374
1375 switch (type) {
1376 case ACPI_NOTIFY_BUS_CHECK:
1377 /* bus re-enumerate */
1378 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1379 acpiphp_check_bridge(bridge);
1380 break;
1381
1382 case ACPI_NOTIFY_DEVICE_CHECK:
1383 /* device check */
1384 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1385 acpiphp_check_bridge(bridge);
1386 break;
1387
1388 case ACPI_NOTIFY_DEVICE_WAKE:
1389 /* wake event */
1390 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1391 break;
1392
1393 case ACPI_NOTIFY_EJECT_REQUEST:
1394 /* request device eject */
1395 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001396 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1397 (bridge->flags & BRIDGE_HAS_EJ0)) {
1398 struct acpiphp_slot *slot;
1399 slot = bridge->func->slot;
1400 if (!acpiphp_disable_slot(slot))
1401 acpiphp_eject_slot(slot);
1402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
1404
1405 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1406 printk(KERN_ERR "Device %s cannot be configured due"
1407 " to a frequency mismatch\n", objname);
1408 break;
1409
1410 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1411 printk(KERN_ERR "Device %s cannot be configured due"
1412 " to a bus mode mismatch\n", objname);
1413 break;
1414
1415 case ACPI_NOTIFY_POWER_FAULT:
1416 printk(KERN_ERR "Device %s has suffered a power fault\n",
1417 objname);
1418 break;
1419
1420 default:
1421 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1422 break;
1423 }
1424}
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426/**
1427 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1428 *
1429 * @handle: Notify()'ed acpi_handle
1430 * @type: Notify code
1431 * @context: pointer to acpiphp_func structure
1432 *
1433 * handles ACPI event notification on slots
1434 *
1435 */
Kristen Accardi20416ea2006-02-23 17:56:03 -08001436void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct acpiphp_func *func;
1439 char objname[64];
1440 struct acpi_buffer buffer = { .length = sizeof(objname),
1441 .pointer = objname };
1442
1443 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1444
1445 func = (struct acpiphp_func *)context;
1446
1447 switch (type) {
1448 case ACPI_NOTIFY_BUS_CHECK:
1449 /* bus re-enumerate */
1450 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1451 acpiphp_enable_slot(func->slot);
1452 break;
1453
1454 case ACPI_NOTIFY_DEVICE_CHECK:
1455 /* device check : re-enumerate from parent bus */
1456 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1457 acpiphp_check_bridge(func->slot->bridge);
1458 break;
1459
1460 case ACPI_NOTIFY_DEVICE_WAKE:
1461 /* wake event */
1462 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1463 break;
1464
1465 case ACPI_NOTIFY_EJECT_REQUEST:
1466 /* request device eject */
1467 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001468 if (!(acpiphp_disable_slot(func->slot)))
1469 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471
1472 default:
1473 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1474 break;
1475 }
1476}
1477
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001478
1479static acpi_status
1480find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1481{
1482 int *count = (int *)context;
1483
Kristen Accardi783c49f2006-03-03 10:16:05 -08001484 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001485 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1486 handle_hotplug_event_bridge, NULL);
1487 (*count)++;
1488 }
1489 return AE_OK ;
1490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492static struct acpi_pci_driver acpi_pci_hp_driver = {
1493 .add = add_bridge,
1494 .remove = remove_bridge,
1495};
1496
1497/**
1498 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1499 *
1500 */
1501int __init acpiphp_glue_init(void)
1502{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001503 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001505 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1506 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 if (num <= 0)
1509 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001510 else
1511 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 return 0;
1514}
1515
1516
1517/**
1518 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1519 *
1520 * This function frees all data allocated in acpiphp_glue_init()
1521 */
1522void __exit acpiphp_glue_exit(void)
1523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1525}
1526
1527
1528/**
1529 * acpiphp_get_num_slots - count number of slots in a system
1530 */
1531int __init acpiphp_get_num_slots(void)
1532{
1533 struct list_head *node;
1534 struct acpiphp_bridge *bridge;
1535 int num_slots;
1536
1537 num_slots = 0;
1538
1539 list_for_each (node, &bridge_list) {
1540 bridge = (struct acpiphp_bridge *)node;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001541 dbg("Bus %04x:%02x has %d slot%s\n",
1542 pci_domain_nr(bridge->pci_bus),
1543 bridge->pci_bus->number, bridge->nr_slots,
1544 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 num_slots += bridge->nr_slots;
1546 }
1547
Rajesh Shah42f49a62005-04-28 00:25:53 -07001548 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return num_slots;
1550}
1551
1552
1553#if 0
1554/**
1555 * acpiphp_for_each_slot - call function for each slot
1556 * @fn: callback function
1557 * @data: context to be passed to callback function
1558 *
1559 */
1560static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1561{
1562 struct list_head *node;
1563 struct acpiphp_bridge *bridge;
1564 struct acpiphp_slot *slot;
1565 int retval = 0;
1566
1567 list_for_each (node, &bridge_list) {
1568 bridge = (struct acpiphp_bridge *)node;
1569 for (slot = bridge->slots; slot; slot = slot->next) {
1570 retval = fn(slot, data);
1571 if (!retval)
1572 goto err_exit;
1573 }
1574 }
1575
1576 err_exit:
1577 return retval;
1578}
1579#endif
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582/**
1583 * acpiphp_enable_slot - power on slot
1584 */
1585int acpiphp_enable_slot(struct acpiphp_slot *slot)
1586{
1587 int retval;
1588
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001589 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 /* wake up all functions */
1592 retval = power_on_slot(slot);
1593 if (retval)
1594 goto err_exit;
1595
1596 if (get_slot_status(slot) == ACPI_STA_ALL)
1597 /* configure all functions */
1598 retval = enable_device(slot);
1599
1600 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001601 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return retval;
1603}
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605/**
1606 * acpiphp_disable_slot - power off slot
1607 */
1608int acpiphp_disable_slot(struct acpiphp_slot *slot)
1609{
1610 int retval = 0;
1611
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001612 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /* unconfigure all functions */
1615 retval = disable_device(slot);
1616 if (retval)
1617 goto err_exit;
1618
1619 /* power off all functions */
1620 retval = power_off_slot(slot);
1621 if (retval)
1622 goto err_exit;
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001625 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return retval;
1627}
1628
1629
1630/*
1631 * slot enabled: 1
1632 * slot disabled: 0
1633 */
1634u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1635{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001636 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639
1640/*
1641 * latch closed: 1
1642 * latch open: 0
1643 */
1644u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1645{
1646 unsigned int sta;
1647
1648 sta = get_slot_status(slot);
1649
1650 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1651}
1652
1653
1654/*
1655 * adapter presence : 1
1656 * absence : 0
1657 */
1658u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1659{
1660 unsigned int sta;
1661
1662 sta = get_slot_status(slot);
1663
1664 return (sta == 0) ? 0 : 1;
1665}
1666
1667
1668/*
1669 * pci address (seg/bus/dev)
1670 */
1671u32 acpiphp_get_address(struct acpiphp_slot *slot)
1672{
1673 u32 address;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001674 struct pci_bus *pci_bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Rajesh Shah42f49a62005-04-28 00:25:53 -07001676 address = (pci_domain_nr(pci_bus) << 16) |
1677 (pci_bus->number << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 slot->device;
1679
1680 return address;
1681}