blob: 22d0f1cf136299867dd526b0def29bdba3e6a5c9 [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;
131 int device, function;
132 static int num_slots = 0; /* XXX if we support I/O node hotplug... */
133
134 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
135
136 if (ACPI_FAILURE(status))
137 return AE_OK;
138
139 status = acpi_get_handle(handle, "_EJ0", &tmp);
140
Kristen Accardi20416ea2006-02-23 17:56:03 -0800141 if (ACPI_FAILURE(status) && !(is_dependent_device(handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return AE_OK;
143
144 device = (adr >> 16) & 0xffff;
145 function = adr & 0xffff;
146
147 newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
148 if (!newfunc)
149 return AE_NO_MEMORY;
150 memset(newfunc, 0, sizeof(struct acpiphp_func));
151
152 INIT_LIST_HEAD(&newfunc->sibling);
153 newfunc->handle = handle;
154 newfunc->function = function;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800155 if (ACPI_SUCCESS(status))
156 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
159 newfunc->flags |= FUNC_HAS_STA;
160
161 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
162 newfunc->flags |= FUNC_HAS_PS0;
163
164 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
165 newfunc->flags |= FUNC_HAS_PS3;
166
Kristen Accardi20416ea2006-02-23 17:56:03 -0800167 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) {
168 newfunc->flags |= FUNC_HAS_DCK;
169 /* add to devices dependent on dock station,
170 * because this may actually be the dock bridge
171 */
172 dd = alloc_dependent_device(handle);
173 if (!dd)
174 err("Can't allocate memory for "
175 "new dependent device!\n");
176 else
177 add_dependent_device(dd);
178 }
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
181 if (ACPI_FAILURE(status))
182 sun = -1;
183
184 /* search for objects that share the same slot */
185 for (slot = bridge->slots; slot; slot = slot->next)
186 if (slot->device == device) {
187 if (slot->sun != sun)
188 warn("sibling found, but _SUN doesn't match!\n");
189 break;
190 }
191
192 if (!slot) {
193 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
194 if (!slot) {
195 kfree(newfunc);
196 return AE_NO_MEMORY;
197 }
198
199 memset(slot, 0, sizeof(struct acpiphp_slot));
200 slot->bridge = bridge;
201 slot->id = num_slots++;
202 slot->device = device;
203 slot->sun = sun;
204 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100205 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 slot->next = bridge->slots;
208 bridge->slots = slot;
209
210 bridge->nr_slots++;
211
Rajesh Shah42f49a62005-04-28 00:25:53 -0700212 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
213 slot->sun, pci_domain_nr(bridge->pci_bus),
214 bridge->pci_bus->number, slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 newfunc->slot = slot;
218 list_add_tail(&newfunc->sibling, &slot->funcs);
219
220 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700221 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 PCI_DEVFN(device, function));
223 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
225 }
226
Kristen Accardi20416ea2006-02-23 17:56:03 -0800227 /* if this is a device dependent on a dock station,
228 * associate the acpiphp_func to the dependent_device
229 * struct.
230 */
231 if ((dd = get_dependent_device(handle))) {
232 newfunc->flags |= FUNC_IS_DD;
233 /*
234 * we don't want any devices which is dependent
235 * on the dock to have it's _EJ0 method executed.
236 * because we need to run _DCK first.
237 */
238 newfunc->flags &= ~FUNC_HAS_EJ0;
239 dd->func = newfunc;
240 add_pci_dependent_device(dd);
241 }
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800244 if (!(newfunc->flags & FUNC_HAS_DCK)) {
245 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 ACPI_SYSTEM_NOTIFY,
247 handle_hotplug_event_func,
248 newfunc);
249
Kristen Accardi20416ea2006-02-23 17:56:03 -0800250 if (ACPI_FAILURE(status))
251 err("failed to register interrupt notify handler\n");
252 } else
253 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Kristen Accardi20416ea2006-02-23 17:56:03 -0800255 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258
259/* see if it's worth looking at this bridge */
260static int detect_ejectable_slots(acpi_handle *bridge_handle)
261{
262 acpi_status status;
263 int count;
264
265 count = 0;
266
267 /* only check slots defined directly below bridge object */
268 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
269 is_ejectable_slot, (void *)&count, NULL);
270
271 return count;
272}
273
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/* decode ACPI 2.0 _HPP hot plug parameters */
276static void decode_hpp(struct acpiphp_bridge *bridge)
277{
278 acpi_status status;
279 struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
280 .pointer = NULL};
281 union acpi_object *package;
282 int i;
283
284 /* default numbers */
285 bridge->hpp.cache_line_size = 0x10;
286 bridge->hpp.latency_timer = 0x40;
287 bridge->hpp.enable_SERR = 0;
288 bridge->hpp.enable_PERR = 0;
289
290 status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
291
292 if (ACPI_FAILURE(status)) {
293 dbg("_HPP evaluation failed\n");
294 return;
295 }
296
297 package = (union acpi_object *) buffer.pointer;
298
299 if (!package || package->type != ACPI_TYPE_PACKAGE ||
300 package->package.count != 4 || !package->package.elements) {
301 err("invalid _HPP object; ignoring\n");
302 goto err_exit;
303 }
304
305 for (i = 0; i < 4; i++) {
306 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
307 err("invalid _HPP parameter type; ignoring\n");
308 goto err_exit;
309 }
310 }
311
312 bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
313 bridge->hpp.latency_timer = package->package.elements[1].integer.value;
314 bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
315 bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
316
317 dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
318 bridge->hpp.cache_line_size,
319 bridge->hpp.latency_timer,
320 bridge->hpp.enable_SERR,
321 bridge->hpp.enable_PERR);
322
323 bridge->flags |= BRIDGE_HAS_HPP;
324
325 err_exit:
326 kfree(buffer.pointer);
327}
328
329
330/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
331static void init_bridge_misc(struct acpiphp_bridge *bridge)
332{
333 acpi_status status;
334
335 /* decode ACPI 2.0 _HPP (hot plug parameters) */
336 decode_hpp(bridge);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* register all slot objects under this bridge */
339 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
340 register_slot, bridge, NULL);
341
342 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700343 if (bridge->type != BRIDGE_TYPE_HOST) {
344 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ACPI_SYSTEM_NOTIFY,
346 handle_hotplug_event_bridge,
347 bridge);
348
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700349 if (ACPI_FAILURE(status)) {
350 err("failed to register interrupt notify handler\n");
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 list_add(&bridge->list, &bridge_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357
358/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700359static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct acpiphp_bridge *bridge;
362
363 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
364 if (bridge == NULL)
365 return;
366
367 memset(bridge, 0, sizeof(struct acpiphp_bridge));
368
369 bridge->type = BRIDGE_TYPE_HOST;
370 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Rajesh Shah42f49a62005-04-28 00:25:53 -0700372 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 spin_lock_init(&bridge->res_lock);
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 init_bridge_misc(bridge);
377}
378
379
380/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700381static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
386 if (bridge == NULL) {
387 err("out of memory\n");
388 return;
389 }
390
391 memset(bridge, 0, sizeof(struct acpiphp_bridge));
392
393 bridge->type = BRIDGE_TYPE_P2P;
394 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Rajesh Shah42f49a62005-04-28 00:25:53 -0700396 bridge->pci_dev = pci_dev_get(pci_dev);
397 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!bridge->pci_bus) {
399 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700400 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403 spin_lock_init(&bridge->res_lock);
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700406 return;
407 err:
408 pci_dev_put(pci_dev);
409 kfree(bridge);
410 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413
414/* callback routine to find P2P bridges */
415static acpi_status
416find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
417{
418 acpi_status status;
419 acpi_handle dummy_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 unsigned long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700421 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700423 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
426 if (ACPI_FAILURE(status))
427 return AE_OK; /* continue */
428
429 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
430 if (ACPI_FAILURE(status)) {
431 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
432 return AE_OK;
433 }
434
435 device = (tmp >> 16) & 0xffff;
436 function = tmp & 0xffff;
437
Rajesh Shah42f49a62005-04-28 00:25:53 -0700438 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Rajesh Shah42f49a62005-04-28 00:25:53 -0700440 if (!dev || !dev->subordinate)
441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* check if this bridge has ejectable slots */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800444 if ((detect_ejectable_slots(handle) > 0) ||
445 (detect_dependent_devices(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700447 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Rajesh Shah42f49a62005-04-28 00:25:53 -0700450 out:
451 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return AE_OK;
453}
454
455
456/* find hot-pluggable slots, and then find P2P bridge */
457static int add_bridge(acpi_handle handle)
458{
459 acpi_status status;
460 unsigned long tmp;
461 int seg, bus;
462 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700463 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 /* if the bridge doesn't have _STA, we assume it is always there */
466 status = acpi_get_handle(handle, "_STA", &dummy_handle);
467 if (ACPI_SUCCESS(status)) {
468 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
469 if (ACPI_FAILURE(status)) {
470 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
471 return 0;
472 }
473 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
474 /* don't register this object */
475 return 0;
476 }
477
478 /* get PCI segment number */
479 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
480
481 seg = ACPI_SUCCESS(status) ? tmp : 0;
482
483 /* get PCI bus number */
484 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
485
486 if (ACPI_SUCCESS(status)) {
487 bus = tmp;
488 } else {
489 warn("can't get bus number, assuming 0\n");
490 bus = 0;
491 }
492
Rajesh Shah42f49a62005-04-28 00:25:53 -0700493 pci_bus = pci_find_bus(seg, bus);
494 if (!pci_bus) {
495 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
497 }
498
Rajesh Shah42f49a62005-04-28 00:25:53 -0700499 /* check if this bridge has ejectable slots */
500 if (detect_ejectable_slots(handle) > 0) {
501 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
502 add_host_bridge(handle, pci_bus);
503 return 0;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* search P2P bridges under this host bridge */
507 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700508 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (ACPI_FAILURE(status))
511 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
512
513 return 0;
514}
515
Rajesh Shah42f49a62005-04-28 00:25:53 -0700516static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
517{
518 struct list_head *head;
519 list_for_each(head, &bridge_list) {
520 struct acpiphp_bridge *bridge = list_entry(head,
521 struct acpiphp_bridge, list);
522 if (bridge->handle == handle)
523 return bridge;
524 }
525
526 return NULL;
527}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Rajesh Shah364d5092005-04-28 00:25:54 -0700529static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700531 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700532 struct acpiphp_slot *slot;
533 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700534 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700535
536 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
537 handle_hotplug_event_bridge);
538 if (ACPI_FAILURE(status))
539 err("failed to remove notify handler\n");
540
541 slot = bridge->slots;
542 while (slot) {
543 struct acpiphp_slot *next = slot->next;
544 list_for_each_safe (list, tmp, &slot->funcs) {
545 struct acpiphp_func *func;
546 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800547 if (!(func->flags & FUNC_HAS_DCK)) {
548 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700549 ACPI_SYSTEM_NOTIFY,
550 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800551 if (ACPI_FAILURE(status))
552 err("failed to remove notify handler\n");
553 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700554 pci_dev_put(func->pci_dev);
555 list_del(list);
556 kfree(func);
557 }
558 kfree(slot);
559 slot = next;
560 }
561
562 pci_dev_put(bridge->pci_dev);
563 list_del(&bridge->list);
564 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Rajesh Shah364d5092005-04-28 00:25:54 -0700567static acpi_status
568cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
569{
570 struct acpiphp_bridge *bridge;
571
572 if (!(bridge = acpiphp_handle_to_bridge(handle)))
573 return AE_OK;
574 cleanup_bridge(bridge);
575 return AE_OK;
576}
577
578static void remove_bridge(acpi_handle handle)
579{
580 struct acpiphp_bridge *bridge;
581
582 bridge = acpiphp_handle_to_bridge(handle);
583 if (bridge) {
584 cleanup_bridge(bridge);
585 } else {
586 /* clean-up p2p bridges under this host bridge */
587 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
588 (u32)1, cleanup_p2p_bridge, NULL, NULL);
589 }
590}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700592static struct pci_dev * get_apic_pci_info(acpi_handle handle)
593{
594 struct acpi_pci_id id;
595 struct pci_bus *bus;
596 struct pci_dev *dev;
597
598 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
599 return NULL;
600
601 bus = pci_find_bus(id.segment, id.bus);
602 if (!bus)
603 return NULL;
604
605 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
606 if (!dev)
607 return NULL;
608
609 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
610 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
611 {
612 pci_dev_put(dev);
613 return NULL;
614 }
615
616 return dev;
617}
618
619static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
620{
621 acpi_status status;
622 int result = -1;
623 unsigned long gsb;
624 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
625 union acpi_object *obj;
626 void *table;
627
628 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
629 if (ACPI_SUCCESS(status)) {
630 *gsi_base = (u32)gsb;
631 return 0;
632 }
633
634 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
635 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
636 return -1;
637
638 obj = buffer.pointer;
639 if (obj->type != ACPI_TYPE_BUFFER)
640 goto out;
641
642 table = obj->buffer.pointer;
643 switch (((acpi_table_entry_header *)table)->type) {
644 case ACPI_MADT_IOSAPIC:
645 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
646 result = 0;
647 break;
648 case ACPI_MADT_IOAPIC:
649 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
650 result = 0;
651 break;
652 default:
653 break;
654 }
655 out:
656 acpi_os_free(buffer.pointer);
657 return result;
658}
659
660static acpi_status
661ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
662{
663 acpi_status status;
664 unsigned long sta;
665 acpi_handle tmp;
666 struct pci_dev *pdev;
667 u32 gsi_base;
668 u64 phys_addr;
669
670 /* Evaluate _STA if present */
671 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
672 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
673 return AE_CTRL_DEPTH;
674
675 /* Scan only PCI bus scope */
676 status = acpi_get_handle(handle, "_HID", &tmp);
677 if (ACPI_SUCCESS(status))
678 return AE_CTRL_DEPTH;
679
680 if (get_gsi_base(handle, &gsi_base))
681 return AE_OK;
682
683 pdev = get_apic_pci_info(handle);
684 if (!pdev)
685 return AE_OK;
686
687 if (pci_enable_device(pdev)) {
688 pci_dev_put(pdev);
689 return AE_OK;
690 }
691
692 pci_set_master(pdev);
693
694 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
695 pci_disable_device(pdev);
696 pci_dev_put(pdev);
697 return AE_OK;
698 }
699
700 phys_addr = pci_resource_start(pdev, 0);
701 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
702 pci_release_region(pdev, 0);
703 pci_disable_device(pdev);
704 pci_dev_put(pdev);
705 return AE_OK;
706 }
707
708 return AE_OK;
709}
710
711static int acpiphp_configure_ioapics(acpi_handle handle)
712{
713 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
714 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
715 return 0;
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static int power_on_slot(struct acpiphp_slot *slot)
719{
720 acpi_status status;
721 struct acpiphp_func *func;
722 struct list_head *l;
723 int retval = 0;
724
725 /* if already enabled, just skip */
726 if (slot->flags & SLOT_POWEREDON)
727 goto err_exit;
728
729 list_for_each (l, &slot->funcs) {
730 func = list_entry(l, struct acpiphp_func, sibling);
731
732 if (func->flags & FUNC_HAS_PS0) {
733 dbg("%s: executing _PS0\n", __FUNCTION__);
734 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
735 if (ACPI_FAILURE(status)) {
736 warn("%s: _PS0 failed\n", __FUNCTION__);
737 retval = -1;
738 goto err_exit;
739 } else
740 break;
741 }
742 }
743
744 /* TBD: evaluate _STA to check if the slot is enabled */
745
746 slot->flags |= SLOT_POWEREDON;
747
748 err_exit:
749 return retval;
750}
751
752
753static int power_off_slot(struct acpiphp_slot *slot)
754{
755 acpi_status status;
756 struct acpiphp_func *func;
757 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 int retval = 0;
760
761 /* if already disabled, just skip */
762 if ((slot->flags & SLOT_POWEREDON) == 0)
763 goto err_exit;
764
765 list_for_each (l, &slot->funcs) {
766 func = list_entry(l, struct acpiphp_func, sibling);
767
Rajesh Shah2f523b12005-04-28 00:25:55 -0700768 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
770 if (ACPI_FAILURE(status)) {
771 warn("%s: _PS3 failed\n", __FUNCTION__);
772 retval = -1;
773 goto err_exit;
774 } else
775 break;
776 }
777 }
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* TBD: evaluate _STA to check if the slot is disabled */
780
781 slot->flags &= (~SLOT_POWEREDON);
782
783 err_exit:
784 return retval;
785}
786
787
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800788
789/**
790 * acpiphp_max_busnr - return the highest reserved bus number under
791 * the given bus.
792 * @bus: bus to start search with
793 *
794 */
795static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
796{
797 struct list_head *tmp;
798 unsigned char max, n;
799
800 /*
801 * pci_bus_max_busnr will return the highest
802 * reserved busnr for all these children.
803 * that is equivalent to the bus->subordinate
804 * value. We don't want to use the parent's
805 * bus->subordinate value because it could have
806 * padding in it.
807 */
808 max = bus->secondary;
809
810 list_for_each(tmp, &bus->children) {
811 n = pci_bus_max_busnr(pci_bus_b(tmp));
812 if (n > max)
813 max = n;
814 }
815 return max;
816}
817
818
819
820/**
821 * get_func - get a pointer to acpiphp_func given a slot, device
822 * @slot: slot to search
823 * @dev: pci_dev struct to match.
824 *
825 * This function will increase the reference count of pci_dev,
826 * so callers should call pci_dev_put when complete.
827 *
828 */
829static struct acpiphp_func *
830get_func(struct acpiphp_slot *slot, struct pci_dev *dev)
831{
832 struct acpiphp_func *func = NULL;
833 struct pci_bus *bus = slot->bridge->pci_bus;
834 struct pci_dev *pdev;
835
836 list_for_each_entry(func, &slot->funcs, sibling) {
837 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device,
838 func->function));
839 if (pdev) {
840 if (pdev == dev)
841 break;
842 pci_dev_put(pdev);
843 }
844 }
845 return func;
846}
847
848
849/**
850 * acpiphp_bus_add - add a new bus to acpi subsystem
851 * @func: acpiphp_func of the bridge
852 *
853 */
854static int acpiphp_bus_add(struct acpiphp_func *func)
855{
856 acpi_handle phandle;
857 struct acpi_device *device, *pdevice;
858 int ret_val;
859
860 acpi_get_parent(func->handle, &phandle);
861 if (acpi_bus_get_device(phandle, &pdevice)) {
862 dbg("no parent device, assuming NULL\n");
863 pdevice = NULL;
864 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800865 if (!acpi_bus_get_device(func->handle, &device)) {
866 dbg("bus exists... trim\n");
867 /* this shouldn't be in here, so remove
868 * the bus then re-add it...
869 */
870 ret_val = acpi_bus_trim(device, 1);
871 dbg("acpi_bus_trim return %x\n", ret_val);
872 }
873
874 ret_val = acpi_bus_add(&device, pdevice, func->handle,
875 ACPI_BUS_TYPE_DEVICE);
876 if (ret_val) {
877 dbg("error adding bus, %x\n",
878 -ret_val);
879 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800880 }
881 /*
882 * try to start anyway. We could have failed to add
883 * simply because this bus had previously been added
884 * on another add. Don't bother with the return value
885 * we just keep going.
886 */
887 ret_val = acpi_bus_start(device);
888
889acpiphp_bus_add_out:
890 return ret_val;
891}
892
893
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/**
896 * enable_device - enable, configure a slot
897 * @slot: slot to be enabled
898 *
899 * This function should be called per *physical slot*,
900 * not per each slot object in ACPI namespace.
901 *
902 */
903static int enable_device(struct acpiphp_slot *slot)
904{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700906 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct list_head *l;
908 struct acpiphp_func *func;
909 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700910 int num, max, pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (slot->flags & SLOT_ENABLED)
913 goto err_exit;
914
915 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700916 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (dev) {
918 /* This case shouldn't happen */
919 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700920 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 retval = -1;
922 goto err_exit;
923 }
924
Rajesh Shah42f49a62005-04-28 00:25:53 -0700925 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
926 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 err("No new device found\n");
928 retval = -1;
929 goto err_exit;
930 }
931
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800932 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700933 for (pass = 0; pass < 2; pass++) {
934 list_for_each_entry(dev, &bus->devices, bus_list) {
935 if (PCI_SLOT(dev->devfn) != slot->device)
936 continue;
937 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800938 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700939 max = pci_scan_bridge(bus, dev, max, pass);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800940 if (pass && dev->subordinate) {
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800941 pci_bus_size_bridges(dev->subordinate);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800942 func = get_func(slot, dev);
943 if (func) {
944 acpiphp_bus_add(func);
945 /* side effect of get_func */
946 pci_dev_put(dev);
947 }
948 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800949 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
Rajesh Shah42f49a62005-04-28 00:25:53 -0700953 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700954 acpiphp_sanitize_bus(bus);
955 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700956 pci_bus_add_devices(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700957 acpiphp_set_hpp_values(DEVICE_ACPI_HANDLE(&bus->self->dev), bus);
958 acpiphp_configure_ioapics(DEVICE_ACPI_HANDLE(&bus->self->dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /* associate pci_dev to our representation */
961 list_for_each (l, &slot->funcs) {
962 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700963 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 func->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
967 slot->flags |= SLOT_ENABLED;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 err_exit:
970 return retval;
971}
972
973
974/**
975 * disable_device - disable a slot
976 */
977static int disable_device(struct acpiphp_slot *slot)
978{
979 int retval = 0;
980 struct acpiphp_func *func;
981 struct list_head *l;
982
983 /* is this slot already disabled? */
984 if (!(slot->flags & SLOT_ENABLED))
985 goto err_exit;
986
987 list_for_each (l, &slot->funcs) {
988 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700989 if (!func->pci_dev)
990 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Rajesh Shah42f49a62005-04-28 00:25:53 -0700992 pci_remove_bus_device(func->pci_dev);
993 pci_dev_put(func->pci_dev);
994 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
997 slot->flags &= (~SLOT_ENABLED);
998
999 err_exit:
1000 return retval;
1001}
1002
1003
1004/**
1005 * get_slot_status - get ACPI slot status
1006 *
1007 * if a slot has _STA for each function and if any one of them
1008 * returned non-zero status, return it
1009 *
1010 * if a slot doesn't have _STA and if any one of its functions'
1011 * configuration space is configured, return 0x0f as a _STA
1012 *
1013 * otherwise return 0
1014 */
1015static unsigned int get_slot_status(struct acpiphp_slot *slot)
1016{
1017 acpi_status status;
1018 unsigned long sta = 0;
1019 u32 dvid;
1020 struct list_head *l;
1021 struct acpiphp_func *func;
1022
1023 list_for_each (l, &slot->funcs) {
1024 func = list_entry(l, struct acpiphp_func, sibling);
1025
1026 if (func->flags & FUNC_HAS_STA) {
1027 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1028 if (ACPI_SUCCESS(status) && sta)
1029 break;
1030 } else {
1031 pci_bus_read_config_dword(slot->bridge->pci_bus,
1032 PCI_DEVFN(slot->device,
1033 func->function),
1034 PCI_VENDOR_ID, &dvid);
1035 if (dvid != 0xffffffff) {
1036 sta = ACPI_STA_ALL;
1037 break;
1038 }
1039 }
1040 }
1041
1042 return (unsigned int)sta;
1043}
1044
1045/**
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001046 * acpiphp_eject_slot - physically eject the slot
1047 */
1048static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1049{
1050 acpi_status status;
1051 struct acpiphp_func *func;
1052 struct list_head *l;
1053 struct acpi_object_list arg_list;
1054 union acpi_object arg;
1055
1056 list_for_each (l, &slot->funcs) {
1057 func = list_entry(l, struct acpiphp_func, sibling);
1058
1059 /* We don't want to call _EJ0 on non-existing functions. */
1060 if ((func->flags & FUNC_HAS_EJ0)) {
1061 /* _EJ0 method take one argument */
1062 arg_list.count = 1;
1063 arg_list.pointer = &arg;
1064 arg.type = ACPI_TYPE_INTEGER;
1065 arg.integer.value = 1;
1066
1067 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1068 if (ACPI_FAILURE(status)) {
1069 warn("%s: _EJ0 failed\n", __FUNCTION__);
1070 return -1;
1071 } else
1072 break;
1073 }
1074 }
1075 return 0;
1076}
1077
1078/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * acpiphp_check_bridge - re-enumerate devices
1080 *
1081 * Iterate over all slots under this bridge and make sure that if a
1082 * card is present they are enabled, and if not they are disabled.
1083 */
1084static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1085{
1086 struct acpiphp_slot *slot;
1087 int retval = 0;
1088 int enabled, disabled;
1089
1090 enabled = disabled = 0;
1091
1092 for (slot = bridge->slots; slot; slot = slot->next) {
1093 unsigned int status = get_slot_status(slot);
1094 if (slot->flags & SLOT_ENABLED) {
1095 if (status == ACPI_STA_ALL)
1096 continue;
1097 retval = acpiphp_disable_slot(slot);
1098 if (retval) {
1099 err("Error occurred in disabling\n");
1100 goto err_exit;
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001101 } else {
1102 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 disabled++;
1105 } else {
1106 if (status != ACPI_STA_ALL)
1107 continue;
1108 retval = acpiphp_enable_slot(slot);
1109 if (retval) {
1110 err("Error occurred in enabling\n");
1111 goto err_exit;
1112 }
1113 enabled++;
1114 }
1115 }
1116
1117 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1118
1119 err_exit:
1120 return retval;
1121}
1122
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001123static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1124{
1125 u16 pci_cmd, pci_bctl;
1126 struct pci_dev *cdev;
1127
1128 /* Program hpp values for this device */
1129 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1130 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1131 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1132 return;
1133 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1134 bridge->hpp.cache_line_size);
1135 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1136 bridge->hpp.latency_timer);
1137 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1138 if (bridge->hpp.enable_SERR)
1139 pci_cmd |= PCI_COMMAND_SERR;
1140 else
1141 pci_cmd &= ~PCI_COMMAND_SERR;
1142 if (bridge->hpp.enable_PERR)
1143 pci_cmd |= PCI_COMMAND_PARITY;
1144 else
1145 pci_cmd &= ~PCI_COMMAND_PARITY;
1146 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1147
1148 /* Program bridge control value and child devices */
1149 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1150 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1151 bridge->hpp.latency_timer);
1152 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1153 if (bridge->hpp.enable_SERR)
1154 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1155 else
1156 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1157 if (bridge->hpp.enable_PERR)
1158 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1159 else
1160 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1161 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1162 if (dev->subordinate) {
1163 list_for_each_entry(cdev, &dev->subordinate->devices,
1164 bus_list)
1165 program_hpp(cdev, bridge);
1166 }
1167 }
1168}
1169
1170static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1171{
1172 struct acpiphp_bridge bridge;
1173 struct pci_dev *dev;
1174
1175 memset(&bridge, 0, sizeof(bridge));
1176 bridge.handle = handle;
1177 decode_hpp(&bridge);
1178 list_for_each_entry(dev, &bus->devices, bus_list)
1179 program_hpp(dev, &bridge);
1180
1181}
1182
1183/*
1184 * Remove devices for which we could not assign resources, call
1185 * arch specific code to fix-up the bus
1186 */
1187static void acpiphp_sanitize_bus(struct pci_bus *bus)
1188{
1189 struct pci_dev *dev;
1190 int i;
1191 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1192
1193 list_for_each_entry(dev, &bus->devices, bus_list) {
1194 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1195 struct resource *res = &dev->resource[i];
1196 if ((res->flags & type_mask) && !res->start &&
1197 res->end) {
1198 /* Could not assign a required resources
1199 * for this device, remove it */
1200 pci_remove_bus_device(dev);
1201 break;
1202 }
1203 }
1204 }
1205}
1206
1207/* Program resources in newly inserted bridge */
1208static int acpiphp_configure_bridge (acpi_handle handle)
1209{
1210 struct acpi_pci_id pci_id;
1211 struct pci_bus *bus;
1212
1213 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1214 err("cannot get PCI domain and bus number for bridge\n");
1215 return -EINVAL;
1216 }
1217 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1218 if (!bus) {
1219 err("cannot find bus %d:%d\n",
1220 pci_id.segment, pci_id.bus);
1221 return -EINVAL;
1222 }
1223
1224 pci_bus_size_bridges(bus);
1225 pci_bus_assign_resources(bus);
1226 acpiphp_sanitize_bus(bus);
1227 acpiphp_set_hpp_values(handle, bus);
1228 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001229 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001230 return 0;
1231}
1232
1233static void handle_bridge_insertion(acpi_handle handle, u32 type)
1234{
1235 struct acpi_device *device, *pdevice;
1236 acpi_handle phandle;
1237
1238 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1239 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1240 err("unexpected notification type %d\n", type);
1241 return;
1242 }
1243
1244 acpi_get_parent(handle, &phandle);
1245 if (acpi_bus_get_device(phandle, &pdevice)) {
1246 dbg("no parent device, assuming NULL\n");
1247 pdevice = NULL;
1248 }
1249 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1250 err("cannot add bridge to acpi list\n");
1251 return;
1252 }
1253 if (!acpiphp_configure_bridge(handle) &&
1254 !acpi_bus_start(device))
1255 add_bridge(handle);
1256 else
1257 err("cannot configure and start bridge\n");
1258
1259}
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261/*
1262 * ACPI event handlers
1263 */
1264
1265/**
1266 * handle_hotplug_event_bridge - handle ACPI event on bridges
1267 *
1268 * @handle: Notify()'ed acpi_handle
1269 * @type: Notify code
1270 * @context: pointer to acpiphp_bridge structure
1271 *
1272 * handles ACPI event notification on {host,p2p} bridges
1273 *
1274 */
1275static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1276{
1277 struct acpiphp_bridge *bridge;
1278 char objname[64];
1279 struct acpi_buffer buffer = { .length = sizeof(objname),
1280 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001281 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001283 if (acpi_bus_get_device(handle, &device)) {
1284 /* This bridge must have just been physically inserted */
1285 handle_bridge_insertion(handle, type);
1286 return;
1287 }
1288
1289 bridge = acpiphp_handle_to_bridge(handle);
1290 if (!bridge) {
1291 err("cannot get bridge info\n");
1292 return;
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1296
1297 switch (type) {
1298 case ACPI_NOTIFY_BUS_CHECK:
1299 /* bus re-enumerate */
1300 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1301 acpiphp_check_bridge(bridge);
1302 break;
1303
1304 case ACPI_NOTIFY_DEVICE_CHECK:
1305 /* device check */
1306 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1307 acpiphp_check_bridge(bridge);
1308 break;
1309
1310 case ACPI_NOTIFY_DEVICE_WAKE:
1311 /* wake event */
1312 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1313 break;
1314
1315 case ACPI_NOTIFY_EJECT_REQUEST:
1316 /* request device eject */
1317 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1318 break;
1319
1320 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1321 printk(KERN_ERR "Device %s cannot be configured due"
1322 " to a frequency mismatch\n", objname);
1323 break;
1324
1325 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1326 printk(KERN_ERR "Device %s cannot be configured due"
1327 " to a bus mode mismatch\n", objname);
1328 break;
1329
1330 case ACPI_NOTIFY_POWER_FAULT:
1331 printk(KERN_ERR "Device %s has suffered a power fault\n",
1332 objname);
1333 break;
1334
1335 default:
1336 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1337 break;
1338 }
1339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/**
1342 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1343 *
1344 * @handle: Notify()'ed acpi_handle
1345 * @type: Notify code
1346 * @context: pointer to acpiphp_func structure
1347 *
1348 * handles ACPI event notification on slots
1349 *
1350 */
Kristen Accardi20416ea2006-02-23 17:56:03 -08001351void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct acpiphp_func *func;
1354 char objname[64];
1355 struct acpi_buffer buffer = { .length = sizeof(objname),
1356 .pointer = objname };
1357
1358 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1359
1360 func = (struct acpiphp_func *)context;
1361
1362 switch (type) {
1363 case ACPI_NOTIFY_BUS_CHECK:
1364 /* bus re-enumerate */
1365 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1366 acpiphp_enable_slot(func->slot);
1367 break;
1368
1369 case ACPI_NOTIFY_DEVICE_CHECK:
1370 /* device check : re-enumerate from parent bus */
1371 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1372 acpiphp_check_bridge(func->slot->bridge);
1373 break;
1374
1375 case ACPI_NOTIFY_DEVICE_WAKE:
1376 /* wake event */
1377 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1378 break;
1379
1380 case ACPI_NOTIFY_EJECT_REQUEST:
1381 /* request device eject */
1382 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001383 if (!(acpiphp_disable_slot(func->slot)))
1384 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
1386
1387 default:
1388 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1389 break;
1390 }
1391}
1392
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001393static int is_root_bridge(acpi_handle handle)
1394{
1395 acpi_status status;
1396 struct acpi_device_info *info;
1397 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1398 int i;
1399
1400 status = acpi_get_object_info(handle, &buffer);
1401 if (ACPI_SUCCESS(status)) {
1402 info = buffer.pointer;
1403 if ((info->valid & ACPI_VALID_HID) &&
1404 !strcmp(PCI_ROOT_HID_STRING,
1405 info->hardware_id.value)) {
1406 acpi_os_free(buffer.pointer);
1407 return 1;
1408 }
1409 if (info->valid & ACPI_VALID_CID) {
1410 for (i=0; i < info->compatibility_id.count; i++) {
1411 if (!strcmp(PCI_ROOT_HID_STRING,
1412 info->compatibility_id.id[i].value)) {
1413 acpi_os_free(buffer.pointer);
1414 return 1;
1415 }
1416 }
1417 }
1418 }
1419 return 0;
1420}
1421
1422static acpi_status
1423find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1424{
1425 int *count = (int *)context;
1426
1427 if (is_root_bridge(handle)) {
1428 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1429 handle_hotplug_event_bridge, NULL);
1430 (*count)++;
1431 }
1432 return AE_OK ;
1433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435static struct acpi_pci_driver acpi_pci_hp_driver = {
1436 .add = add_bridge,
1437 .remove = remove_bridge,
1438};
1439
1440/**
1441 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1442 *
1443 */
1444int __init acpiphp_glue_init(void)
1445{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001446 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001448 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1449 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (num <= 0)
1452 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001453 else
1454 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 return 0;
1457}
1458
1459
1460/**
1461 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1462 *
1463 * This function frees all data allocated in acpiphp_glue_init()
1464 */
1465void __exit acpiphp_glue_exit(void)
1466{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1468}
1469
1470
1471/**
1472 * acpiphp_get_num_slots - count number of slots in a system
1473 */
1474int __init acpiphp_get_num_slots(void)
1475{
1476 struct list_head *node;
1477 struct acpiphp_bridge *bridge;
1478 int num_slots;
1479
1480 num_slots = 0;
1481
1482 list_for_each (node, &bridge_list) {
1483 bridge = (struct acpiphp_bridge *)node;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001484 dbg("Bus %04x:%02x has %d slot%s\n",
1485 pci_domain_nr(bridge->pci_bus),
1486 bridge->pci_bus->number, bridge->nr_slots,
1487 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 num_slots += bridge->nr_slots;
1489 }
1490
Rajesh Shah42f49a62005-04-28 00:25:53 -07001491 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return num_slots;
1493}
1494
1495
1496#if 0
1497/**
1498 * acpiphp_for_each_slot - call function for each slot
1499 * @fn: callback function
1500 * @data: context to be passed to callback function
1501 *
1502 */
1503static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1504{
1505 struct list_head *node;
1506 struct acpiphp_bridge *bridge;
1507 struct acpiphp_slot *slot;
1508 int retval = 0;
1509
1510 list_for_each (node, &bridge_list) {
1511 bridge = (struct acpiphp_bridge *)node;
1512 for (slot = bridge->slots; slot; slot = slot->next) {
1513 retval = fn(slot, data);
1514 if (!retval)
1515 goto err_exit;
1516 }
1517 }
1518
1519 err_exit:
1520 return retval;
1521}
1522#endif
1523
1524/* search matching slot from id */
1525struct acpiphp_slot *get_slot_from_id(int id)
1526{
1527 struct list_head *node;
1528 struct acpiphp_bridge *bridge;
1529 struct acpiphp_slot *slot;
1530
1531 list_for_each (node, &bridge_list) {
1532 bridge = (struct acpiphp_bridge *)node;
1533 for (slot = bridge->slots; slot; slot = slot->next)
1534 if (slot->id == id)
1535 return slot;
1536 }
1537
1538 /* should never happen! */
1539 err("%s: no object for id %d\n", __FUNCTION__, id);
1540 WARN_ON(1);
1541 return NULL;
1542}
1543
1544
1545/**
1546 * acpiphp_enable_slot - power on slot
1547 */
1548int acpiphp_enable_slot(struct acpiphp_slot *slot)
1549{
1550 int retval;
1551
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001552 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /* wake up all functions */
1555 retval = power_on_slot(slot);
1556 if (retval)
1557 goto err_exit;
1558
1559 if (get_slot_status(slot) == ACPI_STA_ALL)
1560 /* configure all functions */
1561 retval = enable_device(slot);
1562
1563 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001564 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return retval;
1566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568/**
1569 * acpiphp_disable_slot - power off slot
1570 */
1571int acpiphp_disable_slot(struct acpiphp_slot *slot)
1572{
1573 int retval = 0;
1574
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001575 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /* unconfigure all functions */
1578 retval = disable_device(slot);
1579 if (retval)
1580 goto err_exit;
1581
1582 /* power off all functions */
1583 retval = power_off_slot(slot);
1584 if (retval)
1585 goto err_exit;
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001588 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return retval;
1590}
1591
1592
1593/*
1594 * slot enabled: 1
1595 * slot disabled: 0
1596 */
1597u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1598{
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001599 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602
1603/*
1604 * latch closed: 1
1605 * latch open: 0
1606 */
1607u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1608{
1609 unsigned int sta;
1610
1611 sta = get_slot_status(slot);
1612
1613 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1614}
1615
1616
1617/*
1618 * adapter presence : 1
1619 * absence : 0
1620 */
1621u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1622{
1623 unsigned int sta;
1624
1625 sta = get_slot_status(slot);
1626
1627 return (sta == 0) ? 0 : 1;
1628}
1629
1630
1631/*
1632 * pci address (seg/bus/dev)
1633 */
1634u32 acpiphp_get_address(struct acpiphp_slot *slot)
1635{
1636 u32 address;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001637 struct pci_bus *pci_bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Rajesh Shah42f49a62005-04-28 00:25:53 -07001639 address = (pci_domain_nr(pci_bus) << 16) |
1640 (pci_bus->number << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 slot->device;
1642
1643 return address;
1644}