blob: cbd5893d198e398ad8fe721938ba8ac90d51da86 [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
146 newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
147 if (!newfunc)
148 return AE_NO_MEMORY;
149 memset(newfunc, 0, sizeof(struct acpiphp_func));
150
151 INIT_LIST_HEAD(&newfunc->sibling);
152 newfunc->handle = handle;
153 newfunc->function = function;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800154 if (ACPI_SUCCESS(status))
155 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
158 newfunc->flags |= FUNC_HAS_STA;
159
160 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
161 newfunc->flags |= FUNC_HAS_PS0;
162
163 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
164 newfunc->flags |= FUNC_HAS_PS3;
165
Kristen Accardi20416ea2006-02-23 17:56:03 -0800166 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) {
167 newfunc->flags |= FUNC_HAS_DCK;
168 /* add to devices dependent on dock station,
169 * because this may actually be the dock bridge
170 */
171 dd = alloc_dependent_device(handle);
172 if (!dd)
173 err("Can't allocate memory for "
174 "new dependent device!\n");
175 else
176 add_dependent_device(dd);
177 }
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
180 if (ACPI_FAILURE(status))
181 sun = -1;
182
183 /* search for objects that share the same slot */
184 for (slot = bridge->slots; slot; slot = slot->next)
185 if (slot->device == device) {
186 if (slot->sun != sun)
187 warn("sibling found, but _SUN doesn't match!\n");
188 break;
189 }
190
191 if (!slot) {
192 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
193 if (!slot) {
194 kfree(newfunc);
195 return AE_NO_MEMORY;
196 }
197
198 memset(slot, 0, sizeof(struct acpiphp_slot));
199 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 slot->device = device;
201 slot->sun = sun;
202 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100203 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 slot->next = bridge->slots;
206 bridge->slots = slot;
207
208 bridge->nr_slots++;
209
Rajesh Shah42f49a62005-04-28 00:25:53 -0700210 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
211 slot->sun, pci_domain_nr(bridge->pci_bus),
212 bridge->pci_bus->number, slot->device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800213 retval = acpiphp_register_hotplug_slot(slot);
214 if (retval) {
215 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
216 goto err_exit;
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
220 newfunc->slot = slot;
221 list_add_tail(&newfunc->sibling, &slot->funcs);
222
223 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700224 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 PCI_DEVFN(device, function));
226 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
228 }
229
Kristen Accardi20416ea2006-02-23 17:56:03 -0800230 /* if this is a device dependent on a dock station,
231 * associate the acpiphp_func to the dependent_device
232 * struct.
233 */
234 if ((dd = get_dependent_device(handle))) {
235 newfunc->flags |= FUNC_IS_DD;
236 /*
237 * we don't want any devices which is dependent
238 * on the dock to have it's _EJ0 method executed.
239 * because we need to run _DCK first.
240 */
241 newfunc->flags &= ~FUNC_HAS_EJ0;
242 dd->func = newfunc;
243 add_pci_dependent_device(dd);
244 }
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800247 if (!(newfunc->flags & FUNC_HAS_DCK)) {
248 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 ACPI_SYSTEM_NOTIFY,
250 handle_hotplug_event_func,
251 newfunc);
252
Kristen Accardi20416ea2006-02-23 17:56:03 -0800253 if (ACPI_FAILURE(status))
254 err("failed to register interrupt notify handler\n");
255 } else
256 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Kristen Accardi20416ea2006-02-23 17:56:03 -0800258 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800259
260 err_exit:
261 bridge->nr_slots--;
262 bridge->slots = slot->next;
263 kfree(slot);
264 kfree(newfunc);
265
266 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* see if it's worth looking at this bridge */
271static int detect_ejectable_slots(acpi_handle *bridge_handle)
272{
273 acpi_status status;
274 int count;
275
276 count = 0;
277
278 /* only check slots defined directly below bridge object */
279 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
280 is_ejectable_slot, (void *)&count, NULL);
281
282 return count;
283}
284
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/* decode ACPI 2.0 _HPP hot plug parameters */
287static void decode_hpp(struct acpiphp_bridge *bridge)
288{
289 acpi_status status;
290 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
291 .pointer = NULL};
292 union acpi_object *package;
293 int i;
294
295 /* default numbers */
296 bridge->hpp.cache_line_size = 0x10;
297 bridge->hpp.latency_timer = 0x40;
298 bridge->hpp.enable_SERR = 0;
299 bridge->hpp.enable_PERR = 0;
300
301 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
302
303 if (ACPI_FAILURE(status)) {
304 dbg("_HPP evaluation failed\n");
305 return;
306 }
307
308 package = (union acpi_object *) buffer.pointer;
309
310 if (!package || package->type != ACPI_TYPE_PACKAGE ||
311 package->package.count != 4 || !package->package.elements) {
312 err("invalid _HPP object; ignoring\n");
313 goto err_exit;
314 }
315
316 for (i = 0; i < 4; i++) {
317 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
318 err("invalid _HPP parameter type; ignoring\n");
319 goto err_exit;
320 }
321 }
322
323 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
324 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
325 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
326 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
327
328 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
329 bridge->hpp.cache_line_size,
330 bridge->hpp.latency_timer,
331 bridge->hpp.enable_SERR,
332 bridge->hpp.enable_PERR);
333
334 bridge->flags |= BRIDGE_HAS_HPP;
335
336 err_exit:
337 kfree(buffer.pointer);
338}
339
340
341/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
342static void init_bridge_misc(struct acpiphp_bridge *bridge)
343{
344 acpi_status status;
345
346 /* decode ACPI 2.0 _HPP (hot plug parameters) */
347 decode_hpp(bridge);
348
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800349 /* must be added to the list prior to calling register_slot */
350 list_add(&bridge->list, &bridge_list);
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* register all slot objects under this bridge */
353 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
354 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800355 if (ACPI_FAILURE(status)) {
356 list_del(&bridge->list);
357 return;
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700361 if (bridge->type != BRIDGE_TYPE_HOST) {
362 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ACPI_SYSTEM_NOTIFY,
364 handle_hotplug_event_bridge,
365 bridge);
366
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700367 if (ACPI_FAILURE(status)) {
368 err("failed to register interrupt notify handler\n");
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373
374/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700375static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct acpiphp_bridge *bridge;
378
379 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
380 if (bridge == NULL)
381 return;
382
383 memset(bridge, 0, sizeof(struct acpiphp_bridge));
384
385 bridge->type = BRIDGE_TYPE_HOST;
386 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Rajesh Shah42f49a62005-04-28 00:25:53 -0700388 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 spin_lock_init(&bridge->res_lock);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 init_bridge_misc(bridge);
393}
394
395
396/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700397static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
402 if (bridge == NULL) {
403 err("out of memory\n");
404 return;
405 }
406
407 memset(bridge, 0, sizeof(struct acpiphp_bridge));
408
409 bridge->type = BRIDGE_TYPE_P2P;
410 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Rajesh Shah42f49a62005-04-28 00:25:53 -0700412 bridge->pci_dev = pci_dev_get(pci_dev);
413 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!bridge->pci_bus) {
415 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700416 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 spin_lock_init(&bridge->res_lock);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700422 return;
423 err:
424 pci_dev_put(pci_dev);
425 kfree(bridge);
426 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429
430/* callback routine to find P2P bridges */
431static acpi_status
432find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
433{
434 acpi_status status;
435 acpi_handle dummy_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700437 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700439 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
442 if (ACPI_FAILURE(status))
443 return AE_OK; /* continue */
444
445 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
446 if (ACPI_FAILURE(status)) {
447 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
448 return AE_OK;
449 }
450
451 device = (tmp >> 16) & 0xffff;
452 function = tmp & 0xffff;
453
Rajesh Shah42f49a62005-04-28 00:25:53 -0700454 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Rajesh Shah42f49a62005-04-28 00:25:53 -0700456 if (!dev || !dev->subordinate)
457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* check if this bridge has ejectable slots */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800460 if ((detect_ejectable_slots(handle) > 0) ||
461 (detect_dependent_devices(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700463 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900466 /* search P2P bridges under this p2p bridge */
467 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
468 find_p2p_bridge, dev->subordinate, NULL);
469 if (ACPI_FAILURE(status))
470 warn("find_p2p_bridge faied (error code = 0x%x)\n", status);
471
Rajesh Shah42f49a62005-04-28 00:25:53 -0700472 out:
473 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return AE_OK;
475}
476
477
478/* find hot-pluggable slots, and then find P2P bridge */
479static int add_bridge(acpi_handle handle)
480{
481 acpi_status status;
482 unsigned long tmp;
483 int seg, bus;
484 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700485 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* if the bridge doesn't have _STA, we assume it is always there */
488 status = acpi_get_handle(handle, "_STA", &dummy_handle);
489 if (ACPI_SUCCESS(status)) {
490 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
491 if (ACPI_FAILURE(status)) {
492 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
493 return 0;
494 }
495 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
496 /* don't register this object */
497 return 0;
498 }
499
500 /* get PCI segment number */
501 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
502
503 seg = ACPI_SUCCESS(status) ? tmp : 0;
504
505 /* get PCI bus number */
506 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
507
508 if (ACPI_SUCCESS(status)) {
509 bus = tmp;
510 } else {
511 warn("can't get bus number, assuming 0\n");
512 bus = 0;
513 }
514
Rajesh Shah42f49a62005-04-28 00:25:53 -0700515 pci_bus = pci_find_bus(seg, bus);
516 if (!pci_bus) {
517 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 0;
519 }
520
Rajesh Shah42f49a62005-04-28 00:25:53 -0700521 /* check if this bridge has ejectable slots */
522 if (detect_ejectable_slots(handle) > 0) {
523 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
524 add_host_bridge(handle, pci_bus);
525 return 0;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* search P2P bridges under this host bridge */
529 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700530 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (ACPI_FAILURE(status))
533 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
534
535 return 0;
536}
537
Rajesh Shah42f49a62005-04-28 00:25:53 -0700538static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
539{
540 struct list_head *head;
541 list_for_each(head, &bridge_list) {
542 struct acpiphp_bridge *bridge = list_entry(head,
543 struct acpiphp_bridge, list);
544 if (bridge->handle == handle)
545 return bridge;
546 }
547
548 return NULL;
549}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Rajesh Shah364d5092005-04-28 00:25:54 -0700551static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700553 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700554 struct acpiphp_slot *slot;
555 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700556 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700557
558 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
559 handle_hotplug_event_bridge);
560 if (ACPI_FAILURE(status))
561 err("failed to remove notify handler\n");
562
563 slot = bridge->slots;
564 while (slot) {
565 struct acpiphp_slot *next = slot->next;
566 list_for_each_safe (list, tmp, &slot->funcs) {
567 struct acpiphp_func *func;
568 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800569 if (!(func->flags & FUNC_HAS_DCK)) {
570 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700571 ACPI_SYSTEM_NOTIFY,
572 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800573 if (ACPI_FAILURE(status))
574 err("failed to remove notify handler\n");
575 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700576 pci_dev_put(func->pci_dev);
577 list_del(list);
578 kfree(func);
579 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800580 acpiphp_unregister_hotplug_slot(slot);
581 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700582 kfree(slot);
583 slot = next;
584 }
585
586 pci_dev_put(bridge->pci_dev);
587 list_del(&bridge->list);
588 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Rajesh Shah364d5092005-04-28 00:25:54 -0700591static acpi_status
592cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
593{
594 struct acpiphp_bridge *bridge;
595
596 if (!(bridge = acpiphp_handle_to_bridge(handle)))
597 return AE_OK;
598 cleanup_bridge(bridge);
599 return AE_OK;
600}
601
602static void remove_bridge(acpi_handle handle)
603{
604 struct acpiphp_bridge *bridge;
605
606 bridge = acpiphp_handle_to_bridge(handle);
607 if (bridge) {
608 cleanup_bridge(bridge);
609 } else {
610 /* clean-up p2p bridges under this host bridge */
611 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900612 ACPI_UINT32_MAX, cleanup_p2p_bridge,
613 NULL, NULL);
Rajesh Shah364d5092005-04-28 00:25:54 -0700614 }
615}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700617static struct pci_dev * get_apic_pci_info(acpi_handle handle)
618{
619 struct acpi_pci_id id;
620 struct pci_bus *bus;
621 struct pci_dev *dev;
622
623 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
624 return NULL;
625
626 bus = pci_find_bus(id.segment, id.bus);
627 if (!bus)
628 return NULL;
629
630 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
631 if (!dev)
632 return NULL;
633
634 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
635 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
636 {
637 pci_dev_put(dev);
638 return NULL;
639 }
640
641 return dev;
642}
643
644static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
645{
646 acpi_status status;
647 int result = -1;
648 unsigned long gsb;
649 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
650 union acpi_object *obj;
651 void *table;
652
653 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
654 if (ACPI_SUCCESS(status)) {
655 *gsi_base = (u32)gsb;
656 return 0;
657 }
658
659 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
660 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
661 return -1;
662
663 obj = buffer.pointer;
664 if (obj->type != ACPI_TYPE_BUFFER)
665 goto out;
666
667 table = obj->buffer.pointer;
668 switch (((acpi_table_entry_header *)table)->type) {
669 case ACPI_MADT_IOSAPIC:
670 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
671 result = 0;
672 break;
673 case ACPI_MADT_IOAPIC:
674 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
675 result = 0;
676 break;
677 default:
678 break;
679 }
680 out:
681 acpi_os_free(buffer.pointer);
682 return result;
683}
684
685static acpi_status
686ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
687{
688 acpi_status status;
689 unsigned long sta;
690 acpi_handle tmp;
691 struct pci_dev *pdev;
692 u32 gsi_base;
693 u64 phys_addr;
694
695 /* Evaluate _STA if present */
696 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
697 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
698 return AE_CTRL_DEPTH;
699
700 /* Scan only PCI bus scope */
701 status = acpi_get_handle(handle, "_HID", &tmp);
702 if (ACPI_SUCCESS(status))
703 return AE_CTRL_DEPTH;
704
705 if (get_gsi_base(handle, &gsi_base))
706 return AE_OK;
707
708 pdev = get_apic_pci_info(handle);
709 if (!pdev)
710 return AE_OK;
711
712 if (pci_enable_device(pdev)) {
713 pci_dev_put(pdev);
714 return AE_OK;
715 }
716
717 pci_set_master(pdev);
718
719 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
720 pci_disable_device(pdev);
721 pci_dev_put(pdev);
722 return AE_OK;
723 }
724
725 phys_addr = pci_resource_start(pdev, 0);
726 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
727 pci_release_region(pdev, 0);
728 pci_disable_device(pdev);
729 pci_dev_put(pdev);
730 return AE_OK;
731 }
732
733 return AE_OK;
734}
735
736static int acpiphp_configure_ioapics(acpi_handle handle)
737{
738 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
739 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
740 return 0;
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743static int power_on_slot(struct acpiphp_slot *slot)
744{
745 acpi_status status;
746 struct acpiphp_func *func;
747 struct list_head *l;
748 int retval = 0;
749
750 /* if already enabled, just skip */
751 if (slot->flags & SLOT_POWEREDON)
752 goto err_exit;
753
754 list_for_each (l, &slot->funcs) {
755 func = list_entry(l, struct acpiphp_func, sibling);
756
757 if (func->flags & FUNC_HAS_PS0) {
758 dbg("%s: executing _PS0\n", __FUNCTION__);
759 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
760 if (ACPI_FAILURE(status)) {
761 warn("%s: _PS0 failed\n", __FUNCTION__);
762 retval = -1;
763 goto err_exit;
764 } else
765 break;
766 }
767 }
768
769 /* TBD: evaluate _STA to check if the slot is enabled */
770
771 slot->flags |= SLOT_POWEREDON;
772
773 err_exit:
774 return retval;
775}
776
777
778static int power_off_slot(struct acpiphp_slot *slot)
779{
780 acpi_status status;
781 struct acpiphp_func *func;
782 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 int retval = 0;
785
786 /* if already disabled, just skip */
787 if ((slot->flags & SLOT_POWEREDON) == 0)
788 goto err_exit;
789
790 list_for_each (l, &slot->funcs) {
791 func = list_entry(l, struct acpiphp_func, sibling);
792
Rajesh Shah2f523b12005-04-28 00:25:55 -0700793 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
795 if (ACPI_FAILURE(status)) {
796 warn("%s: _PS3 failed\n", __FUNCTION__);
797 retval = -1;
798 goto err_exit;
799 } else
800 break;
801 }
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* TBD: evaluate _STA to check if the slot is disabled */
805
806 slot->flags &= (~SLOT_POWEREDON);
807
808 err_exit:
809 return retval;
810}
811
812
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800813
814/**
815 * acpiphp_max_busnr - return the highest reserved bus number under
816 * the given bus.
817 * @bus: bus to start search with
818 *
819 */
820static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
821{
822 struct list_head *tmp;
823 unsigned char max, n;
824
825 /*
826 * pci_bus_max_busnr will return the highest
827 * reserved busnr for all these children.
828 * that is equivalent to the bus->subordinate
829 * value. We don't want to use the parent's
830 * bus->subordinate value because it could have
831 * padding in it.
832 */
833 max = bus->secondary;
834
835 list_for_each(tmp, &bus->children) {
836 n = pci_bus_max_busnr(pci_bus_b(tmp));
837 if (n > max)
838 max = n;
839 }
840 return max;
841}
842
843
844
845/**
846 * get_func - get a pointer to acpiphp_func given a slot, device
847 * @slot: slot to search
848 * @dev: pci_dev struct to match.
849 *
850 * This function will increase the reference count of pci_dev,
851 * so callers should call pci_dev_put when complete.
852 *
853 */
854static struct acpiphp_func *
855get_func(struct acpiphp_slot *slot, struct pci_dev *dev)
856{
857 struct acpiphp_func *func = NULL;
858 struct pci_bus *bus = slot->bridge->pci_bus;
859 struct pci_dev *pdev;
860
861 list_for_each_entry(func, &slot->funcs, sibling) {
862 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device,
863 func->function));
864 if (pdev) {
865 if (pdev == dev)
866 break;
867 pci_dev_put(pdev);
868 }
869 }
870 return func;
871}
872
873
874/**
875 * acpiphp_bus_add - add a new bus to acpi subsystem
876 * @func: acpiphp_func of the bridge
877 *
878 */
879static int acpiphp_bus_add(struct acpiphp_func *func)
880{
881 acpi_handle phandle;
882 struct acpi_device *device, *pdevice;
883 int ret_val;
884
885 acpi_get_parent(func->handle, &phandle);
886 if (acpi_bus_get_device(phandle, &pdevice)) {
887 dbg("no parent device, assuming NULL\n");
888 pdevice = NULL;
889 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800890 if (!acpi_bus_get_device(func->handle, &device)) {
891 dbg("bus exists... trim\n");
892 /* this shouldn't be in here, so remove
893 * the bus then re-add it...
894 */
895 ret_val = acpi_bus_trim(device, 1);
896 dbg("acpi_bus_trim return %x\n", ret_val);
897 }
898
899 ret_val = acpi_bus_add(&device, pdevice, func->handle,
900 ACPI_BUS_TYPE_DEVICE);
901 if (ret_val) {
902 dbg("error adding bus, %x\n",
903 -ret_val);
904 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800905 }
906 /*
907 * try to start anyway. We could have failed to add
908 * simply because this bus had previously been added
909 * on another add. Don't bother with the return value
910 * we just keep going.
911 */
912 ret_val = acpi_bus_start(device);
913
914acpiphp_bus_add_out:
915 return ret_val;
916}
917
918
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/**
921 * enable_device - enable, configure a slot
922 * @slot: slot to be enabled
923 *
924 * This function should be called per *physical slot*,
925 * not per each slot object in ACPI namespace.
926 *
927 */
928static int enable_device(struct acpiphp_slot *slot)
929{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700931 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct list_head *l;
933 struct acpiphp_func *func;
934 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700935 int num, max, pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (slot->flags & SLOT_ENABLED)
938 goto err_exit;
939
940 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700941 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (dev) {
943 /* This case shouldn't happen */
944 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700945 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 retval = -1;
947 goto err_exit;
948 }
949
Rajesh Shah42f49a62005-04-28 00:25:53 -0700950 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
951 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 err("No new device found\n");
953 retval = -1;
954 goto err_exit;
955 }
956
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800957 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700958 for (pass = 0; pass < 2; pass++) {
959 list_for_each_entry(dev, &bus->devices, bus_list) {
960 if (PCI_SLOT(dev->devfn) != slot->device)
961 continue;
962 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800963 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700964 max = pci_scan_bridge(bus, dev, max, pass);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800965 if (pass && dev->subordinate) {
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800966 pci_bus_size_bridges(dev->subordinate);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800967 func = get_func(slot, dev);
968 if (func) {
969 acpiphp_bus_add(func);
970 /* side effect of get_func */
971 pci_dev_put(dev);
972 }
973 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800974 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
Rajesh Shah42f49a62005-04-28 00:25:53 -0700978 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700979 acpiphp_sanitize_bus(bus);
980 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700981 pci_bus_add_devices(bus);
MUNEDA Takahiro0cccd0c2006-02-24 17:46:04 +0900982 acpiphp_set_hpp_values(slot->bridge->handle, bus);
983 acpiphp_configure_ioapics(slot->bridge->handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* associate pci_dev to our representation */
986 list_for_each (l, &slot->funcs) {
987 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700988 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 func->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
992 slot->flags |= SLOT_ENABLED;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 err_exit:
995 return retval;
996}
997
998
999/**
1000 * disable_device - disable a slot
1001 */
1002static int disable_device(struct acpiphp_slot *slot)
1003{
1004 int retval = 0;
1005 struct acpiphp_func *func;
1006 struct list_head *l;
1007
1008 /* is this slot already disabled? */
1009 if (!(slot->flags & SLOT_ENABLED))
1010 goto err_exit;
1011
1012 list_for_each (l, &slot->funcs) {
1013 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001014 if (!func->pci_dev)
1015 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Rajesh Shah42f49a62005-04-28 00:25:53 -07001017 pci_remove_bus_device(func->pci_dev);
1018 pci_dev_put(func->pci_dev);
1019 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
1022 slot->flags &= (~SLOT_ENABLED);
1023
1024 err_exit:
1025 return retval;
1026}
1027
1028
1029/**
1030 * get_slot_status - get ACPI slot status
1031 *
1032 * if a slot has _STA for each function and if any one of them
1033 * returned non-zero status, return it
1034 *
1035 * if a slot doesn't have _STA and if any one of its functions'
1036 * configuration space is configured, return 0x0f as a _STA
1037 *
1038 * otherwise return 0
1039 */
1040static unsigned int get_slot_status(struct acpiphp_slot *slot)
1041{
1042 acpi_status status;
1043 unsigned long sta = 0;
1044 u32 dvid;
1045 struct list_head *l;
1046 struct acpiphp_func *func;
1047
1048 list_for_each (l, &slot->funcs) {
1049 func = list_entry(l, struct acpiphp_func, sibling);
1050
1051 if (func->flags & FUNC_HAS_STA) {
1052 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1053 if (ACPI_SUCCESS(status) && sta)
1054 break;
1055 } else {
1056 pci_bus_read_config_dword(slot->bridge->pci_bus,
1057 PCI_DEVFN(slot->device,
1058 func->function),
1059 PCI_VENDOR_ID, &dvid);
1060 if (dvid != 0xffffffff) {
1061 sta = ACPI_STA_ALL;
1062 break;
1063 }
1064 }
1065 }
1066
1067 return (unsigned int)sta;
1068}
1069
1070/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001071 * acpiphp_eject_slot - physically eject the slot
1072 */
1073static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1074{
1075 acpi_status status;
1076 struct acpiphp_func *func;
1077 struct list_head *l;
1078 struct acpi_object_list arg_list;
1079 union acpi_object arg;
1080
1081 list_for_each (l, &slot->funcs) {
1082 func = list_entry(l, struct acpiphp_func, sibling);
1083
1084 /* We don't want to call _EJ0 on non-existing functions. */
1085 if ((func->flags & FUNC_HAS_EJ0)) {
1086 /* _EJ0 method take one argument */
1087 arg_list.count = 1;
1088 arg_list.pointer = &arg;
1089 arg.type = ACPI_TYPE_INTEGER;
1090 arg.integer.value = 1;
1091
1092 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1093 if (ACPI_FAILURE(status)) {
1094 warn("%s: _EJ0 failed\n", __FUNCTION__);
1095 return -1;
1096 } else
1097 break;
1098 }
1099 }
1100 return 0;
1101}
1102
1103/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * acpiphp_check_bridge - re-enumerate devices
1105 *
1106 * Iterate over all slots under this bridge and make sure that if a
1107 * card is present they are enabled, and if not they are disabled.
1108 */
1109static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1110{
1111 struct acpiphp_slot *slot;
1112 int retval = 0;
1113 int enabled, disabled;
1114
1115 enabled = disabled = 0;
1116
1117 for (slot = bridge->slots; slot; slot = slot->next) {
1118 unsigned int status = get_slot_status(slot);
1119 if (slot->flags & SLOT_ENABLED) {
1120 if (status == ACPI_STA_ALL)
1121 continue;
1122 retval = acpiphp_disable_slot(slot);
1123 if (retval) {
1124 err("Error occurred in disabling\n");
1125 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001126 } else {
1127 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129 disabled++;
1130 } else {
1131 if (status != ACPI_STA_ALL)
1132 continue;
1133 retval = acpiphp_enable_slot(slot);
1134 if (retval) {
1135 err("Error occurred in enabling\n");
1136 goto err_exit;
1137 }
1138 enabled++;
1139 }
1140 }
1141
1142 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1143
1144 err_exit:
1145 return retval;
1146}
1147
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001148static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1149{
1150 u16 pci_cmd, pci_bctl;
1151 struct pci_dev *cdev;
1152
1153 /* Program hpp values for this device */
1154 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1155 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1156 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1157 return;
1158 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1159 bridge->hpp.cache_line_size);
1160 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1161 bridge->hpp.latency_timer);
1162 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1163 if (bridge->hpp.enable_SERR)
1164 pci_cmd |= PCI_COMMAND_SERR;
1165 else
1166 pci_cmd &= ~PCI_COMMAND_SERR;
1167 if (bridge->hpp.enable_PERR)
1168 pci_cmd |= PCI_COMMAND_PARITY;
1169 else
1170 pci_cmd &= ~PCI_COMMAND_PARITY;
1171 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1172
1173 /* Program bridge control value and child devices */
1174 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1175 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1176 bridge->hpp.latency_timer);
1177 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1178 if (bridge->hpp.enable_SERR)
1179 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1180 else
1181 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1182 if (bridge->hpp.enable_PERR)
1183 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1184 else
1185 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1186 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1187 if (dev->subordinate) {
1188 list_for_each_entry(cdev, &dev->subordinate->devices,
1189 bus_list)
1190 program_hpp(cdev, bridge);
1191 }
1192 }
1193}
1194
1195static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1196{
1197 struct acpiphp_bridge bridge;
1198 struct pci_dev *dev;
1199
1200 memset(&bridge, 0, sizeof(bridge));
1201 bridge.handle = handle;
1202 decode_hpp(&bridge);
1203 list_for_each_entry(dev, &bus->devices, bus_list)
1204 program_hpp(dev, &bridge);
1205
1206}
1207
1208/*
1209 * Remove devices for which we could not assign resources, call
1210 * arch specific code to fix-up the bus
1211 */
1212static void acpiphp_sanitize_bus(struct pci_bus *bus)
1213{
1214 struct pci_dev *dev;
1215 int i;
1216 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1217
1218 list_for_each_entry(dev, &bus->devices, bus_list) {
1219 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1220 struct resource *res = &dev->resource[i];
1221 if ((res->flags & type_mask) && !res->start &&
1222 res->end) {
1223 /* Could not assign a required resources
1224 * for this device, remove it */
1225 pci_remove_bus_device(dev);
1226 break;
1227 }
1228 }
1229 }
1230}
1231
1232/* Program resources in newly inserted bridge */
1233static int acpiphp_configure_bridge (acpi_handle handle)
1234{
1235 struct acpi_pci_id pci_id;
1236 struct pci_bus *bus;
1237
1238 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1239 err("cannot get PCI domain and bus number for bridge\n");
1240 return -EINVAL;
1241 }
1242 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1243 if (!bus) {
1244 err("cannot find bus %d:%d\n",
1245 pci_id.segment, pci_id.bus);
1246 return -EINVAL;
1247 }
1248
1249 pci_bus_size_bridges(bus);
1250 pci_bus_assign_resources(bus);
1251 acpiphp_sanitize_bus(bus);
1252 acpiphp_set_hpp_values(handle, bus);
1253 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001254 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001255 return 0;
1256}
1257
1258static void handle_bridge_insertion(acpi_handle handle, u32 type)
1259{
1260 struct acpi_device *device, *pdevice;
1261 acpi_handle phandle;
1262
1263 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1264 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1265 err("unexpected notification type %d\n", type);
1266 return;
1267 }
1268
1269 acpi_get_parent(handle, &phandle);
1270 if (acpi_bus_get_device(phandle, &pdevice)) {
1271 dbg("no parent device, assuming NULL\n");
1272 pdevice = NULL;
1273 }
1274 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1275 err("cannot add bridge to acpi list\n");
1276 return;
1277 }
1278 if (!acpiphp_configure_bridge(handle) &&
1279 !acpi_bus_start(device))
1280 add_bridge(handle);
1281 else
1282 err("cannot configure and start bridge\n");
1283
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/*
1287 * ACPI event handlers
1288 */
1289
1290/**
1291 * handle_hotplug_event_bridge - handle ACPI event on bridges
1292 *
1293 * @handle: Notify()'ed acpi_handle
1294 * @type: Notify code
1295 * @context: pointer to acpiphp_bridge structure
1296 *
1297 * handles ACPI event notification on {host,p2p} bridges
1298 *
1299 */
1300static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1301{
1302 struct acpiphp_bridge *bridge;
1303 char objname[64];
1304 struct acpi_buffer buffer = { .length = sizeof(objname),
1305 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001306 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001308 if (acpi_bus_get_device(handle, &device)) {
1309 /* This bridge must have just been physically inserted */
1310 handle_bridge_insertion(handle, type);
1311 return;
1312 }
1313
1314 bridge = acpiphp_handle_to_bridge(handle);
1315 if (!bridge) {
1316 err("cannot get bridge info\n");
1317 return;
1318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1321
1322 switch (type) {
1323 case ACPI_NOTIFY_BUS_CHECK:
1324 /* bus re-enumerate */
1325 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1326 acpiphp_check_bridge(bridge);
1327 break;
1328
1329 case ACPI_NOTIFY_DEVICE_CHECK:
1330 /* device check */
1331 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1332 acpiphp_check_bridge(bridge);
1333 break;
1334
1335 case ACPI_NOTIFY_DEVICE_WAKE:
1336 /* wake event */
1337 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1338 break;
1339
1340 case ACPI_NOTIFY_EJECT_REQUEST:
1341 /* request device eject */
1342 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1343 break;
1344
1345 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1346 printk(KERN_ERR "Device %s cannot be configured due"
1347 " to a frequency mismatch\n", objname);
1348 break;
1349
1350 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1351 printk(KERN_ERR "Device %s cannot be configured due"
1352 " to a bus mode mismatch\n", objname);
1353 break;
1354
1355 case ACPI_NOTIFY_POWER_FAULT:
1356 printk(KERN_ERR "Device %s has suffered a power fault\n",
1357 objname);
1358 break;
1359
1360 default:
1361 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1362 break;
1363 }
1364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/**
1367 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1368 *
1369 * @handle: Notify()'ed acpi_handle
1370 * @type: Notify code
1371 * @context: pointer to acpiphp_func structure
1372 *
1373 * handles ACPI event notification on slots
1374 *
1375 */
Kristen Accardi20416ea2006-02-23 17:56:03 -08001376void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct acpiphp_func *func;
1379 char objname[64];
1380 struct acpi_buffer buffer = { .length = sizeof(objname),
1381 .pointer = objname };
1382
1383 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1384
1385 func = (struct acpiphp_func *)context;
1386
1387 switch (type) {
1388 case ACPI_NOTIFY_BUS_CHECK:
1389 /* bus re-enumerate */
1390 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1391 acpiphp_enable_slot(func->slot);
1392 break;
1393
1394 case ACPI_NOTIFY_DEVICE_CHECK:
1395 /* device check : re-enumerate from parent bus */
1396 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1397 acpiphp_check_bridge(func->slot->bridge);
1398 break;
1399
1400 case ACPI_NOTIFY_DEVICE_WAKE:
1401 /* wake event */
1402 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1403 break;
1404
1405 case ACPI_NOTIFY_EJECT_REQUEST:
1406 /* request device eject */
1407 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001408 if (!(acpiphp_disable_slot(func->slot)))
1409 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 break;
1411
1412 default:
1413 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1414 break;
1415 }
1416}
1417
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001418static int is_root_bridge(acpi_handle handle)
1419{
1420 acpi_status status;
1421 struct acpi_device_info *info;
1422 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1423 int i;
1424
1425 status = acpi_get_object_info(handle, &buffer);
1426 if (ACPI_SUCCESS(status)) {
1427 info = buffer.pointer;
1428 if ((info->valid & ACPI_VALID_HID) &&
1429 !strcmp(PCI_ROOT_HID_STRING,
1430 info->hardware_id.value)) {
1431 acpi_os_free(buffer.pointer);
1432 return 1;
1433 }
1434 if (info->valid & ACPI_VALID_CID) {
1435 for (i=0; i < info->compatibility_id.count; i++) {
1436 if (!strcmp(PCI_ROOT_HID_STRING,
1437 info->compatibility_id.id[i].value)) {
1438 acpi_os_free(buffer.pointer);
1439 return 1;
1440 }
1441 }
1442 }
1443 }
1444 return 0;
1445}
1446
1447static acpi_status
1448find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1449{
1450 int *count = (int *)context;
1451
1452 if (is_root_bridge(handle)) {
1453 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1454 handle_hotplug_event_bridge, NULL);
1455 (*count)++;
1456 }
1457 return AE_OK ;
1458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460static struct acpi_pci_driver acpi_pci_hp_driver = {
1461 .add = add_bridge,
1462 .remove = remove_bridge,
1463};
1464
1465/**
1466 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1467 *
1468 */
1469int __init acpiphp_glue_init(void)
1470{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001471 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001473 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1474 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 if (num <= 0)
1477 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001478 else
1479 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 return 0;
1482}
1483
1484
1485/**
1486 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1487 *
1488 * This function frees all data allocated in acpiphp_glue_init()
1489 */
1490void __exit acpiphp_glue_exit(void)
1491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1493}
1494
1495
1496/**
1497 * acpiphp_get_num_slots - count number of slots in a system
1498 */
1499int __init acpiphp_get_num_slots(void)
1500{
1501 struct list_head *node;
1502 struct acpiphp_bridge *bridge;
1503 int num_slots;
1504
1505 num_slots = 0;
1506
1507 list_for_each (node, &bridge_list) {
1508 bridge = (struct acpiphp_bridge *)node;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001509 dbg("Bus %04x:%02x has %d slot%s\n",
1510 pci_domain_nr(bridge->pci_bus),
1511 bridge->pci_bus->number, bridge->nr_slots,
1512 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 num_slots += bridge->nr_slots;
1514 }
1515
Rajesh Shah42f49a62005-04-28 00:25:53 -07001516 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return num_slots;
1518}
1519
1520
1521#if 0
1522/**
1523 * acpiphp_for_each_slot - call function for each slot
1524 * @fn: callback function
1525 * @data: context to be passed to callback function
1526 *
1527 */
1528static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1529{
1530 struct list_head *node;
1531 struct acpiphp_bridge *bridge;
1532 struct acpiphp_slot *slot;
1533 int retval = 0;
1534
1535 list_for_each (node, &bridge_list) {
1536 bridge = (struct acpiphp_bridge *)node;
1537 for (slot = bridge->slots; slot; slot = slot->next) {
1538 retval = fn(slot, data);
1539 if (!retval)
1540 goto err_exit;
1541 }
1542 }
1543
1544 err_exit:
1545 return retval;
1546}
1547#endif
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550/**
1551 * acpiphp_enable_slot - power on slot
1552 */
1553int acpiphp_enable_slot(struct acpiphp_slot *slot)
1554{
1555 int retval;
1556
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001557 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* wake up all functions */
1560 retval = power_on_slot(slot);
1561 if (retval)
1562 goto err_exit;
1563
1564 if (get_slot_status(slot) == ACPI_STA_ALL)
1565 /* configure all functions */
1566 retval = enable_device(slot);
1567
1568 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001569 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return retval;
1571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573/**
1574 * acpiphp_disable_slot - power off slot
1575 */
1576int acpiphp_disable_slot(struct acpiphp_slot *slot)
1577{
1578 int retval = 0;
1579
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001580 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /* unconfigure all functions */
1583 retval = disable_device(slot);
1584 if (retval)
1585 goto err_exit;
1586
1587 /* power off all functions */
1588 retval = power_off_slot(slot);
1589 if (retval)
1590 goto err_exit;
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001593 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return retval;
1595}
1596
1597
1598/*
1599 * slot enabled: 1
1600 * slot disabled: 0
1601 */
1602u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1603{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001604 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
1607
1608/*
1609 * latch closed: 1
1610 * latch open: 0
1611 */
1612u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1613{
1614 unsigned int sta;
1615
1616 sta = get_slot_status(slot);
1617
1618 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1619}
1620
1621
1622/*
1623 * adapter presence : 1
1624 * absence : 0
1625 */
1626u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1627{
1628 unsigned int sta;
1629
1630 sta = get_slot_status(slot);
1631
1632 return (sta == 0) ? 0 : 1;
1633}
1634
1635
1636/*
1637 * pci address (seg/bus/dev)
1638 */
1639u32 acpiphp_get_address(struct acpiphp_slot *slot)
1640{
1641 u32 address;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001642 struct pci_bus *pci_bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Rajesh Shah42f49a62005-04-28 00:25:53 -07001644 address = (pci_domain_nr(pci_bus) << 16) |
1645 (pci_bus->number << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 slot->device;
1647
1648 return address;
1649}