PCI Hotplug: add acpiphp to MAINTAINERS
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / pci / hotplug / acpiphp_glue.c
CommitLineData
1da177e4
LT
1/*
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
42f49a6a
RS
7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
8e7561cf
RS
9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
1da177e4
LT
11 *
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 *
998be20f 29 * Send feedback to <kristen.c.accardi@intel.com>
1da177e4
LT
30 *
31 */
32
42f49a6a
RS
33/*
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
1da177e4
LT
43#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>
6aa4cdd0 49#include <linux/mutex.h>
1da177e4
LT
50
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 *);
8e5dce35
KA
60static void acpiphp_sanitize_bus(struct pci_bus *bus);
61static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
2b85e130 62static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
8e5dce35 63
1da177e4
LT
64
65/*
66 * initialization & terminatation routines
67 */
68
69/**
70 * is_ejectable - determine if a slot is ejectable
71 * @handle: handle to acpi namespace
72 *
73 * Ejectable slot should satisfy at least these conditions:
74 *
75 * 1. has _ADR method
76 * 2. has _EJ0 method
77 *
78 * optionally
79 *
80 * 1. has _STA method
81 * 2. has _PS0 method
82 * 3. has _PS3 method
83 * 4. ..
84 *
85 */
86static int is_ejectable(acpi_handle handle)
87{
88 acpi_status status;
89 acpi_handle tmp;
90
91 status = acpi_get_handle(handle, "_ADR", &tmp);
92 if (ACPI_FAILURE(status)) {
93 return 0;
94 }
95
96 status = acpi_get_handle(handle, "_EJ0", &tmp);
97 if (ACPI_FAILURE(status)) {
98 return 0;
99 }
100
101 return 1;
102}
103
104
105/* callback routine to check the existence of ejectable slots */
106static acpi_status
107is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
108{
109 int *count = (int *)context;
110
111 if (is_ejectable(handle)) {
112 (*count)++;
113 /* only one ejectable slot is enough */
114 return AE_CTRL_TERMINATE;
115 } else {
116 return AE_OK;
117 }
118}
119
4e8662bb
KA
120/* callback routine to check for the existance of a pci dock device */
121static acpi_status
122is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
123{
124 int *count = (int *)context;
125
126 if (is_dock_device(handle)) {
127 (*count)++;
128 return AE_CTRL_TERMINATE;
129 } else {
130 return AE_OK;
131 }
132}
133
134
135
136
137/*
138 * the _DCK method can do funny things... and sometimes not
139 * hah-hah funny.
140 *
141 * TBD - figure out a way to only call fixups for
142 * systems that require them.
143 */
144static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
145 void *v)
146{
147 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
148 struct pci_bus *bus = func->slot->bridge->pci_bus;
149 u32 buses;
150
151 if (!bus->self)
152 return NOTIFY_OK;
153
154 /* fixup bad _DCK function that rewrites
155 * secondary bridge on slot
156 */
157 pci_read_config_dword(bus->self,
158 PCI_PRIMARY_BUS,
159 &buses);
160
161 if (((buses >> 8) & 0xff) != bus->secondary) {
162 buses = (buses & 0xff000000)
163 | ((unsigned int)(bus->primary) << 0)
164 | ((unsigned int)(bus->secondary) << 8)
165 | ((unsigned int)(bus->subordinate) << 16);
166 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
167 }
168 return NOTIFY_OK;
169}
170
171
172
1da177e4
LT
173
174/* callback routine to register each ACPI PCI slot object */
175static acpi_status
176register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
177{
178 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
179 struct acpiphp_slot *slot;
180 struct acpiphp_func *newfunc;
181 acpi_handle tmp;
182 acpi_status status = AE_OK;
183 unsigned long adr, sun;
e27da381 184 int device, function, retval;
1da177e4
LT
185
186 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
187
188 if (ACPI_FAILURE(status))
189 return AE_OK;
190
191 status = acpi_get_handle(handle, "_EJ0", &tmp);
192
4e8662bb 193 if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
1da177e4
LT
194 return AE_OK;
195
196 device = (adr >> 16) & 0xffff;
197 function = adr & 0xffff;
198
f5afe806 199 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
1da177e4
LT
200 if (!newfunc)
201 return AE_NO_MEMORY;
1da177e4
LT
202
203 INIT_LIST_HEAD(&newfunc->sibling);
204 newfunc->handle = handle;
205 newfunc->function = function;
20416ea5
KA
206 if (ACPI_SUCCESS(status))
207 newfunc->flags = FUNC_HAS_EJ0;
1da177e4
LT
208
209 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
210 newfunc->flags |= FUNC_HAS_STA;
211
212 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
213 newfunc->flags |= FUNC_HAS_PS0;
214
215 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
216 newfunc->flags |= FUNC_HAS_PS3;
217
4e8662bb 218 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
20416ea5 219 newfunc->flags |= FUNC_HAS_DCK;
20416ea5 220
1da177e4 221 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
95b38b3f
KA
222 if (ACPI_FAILURE(status)) {
223 /*
224 * use the count of the number of slots we've found
225 * for the number of the slot
226 */
227 sun = bridge->nr_slots+1;
228 }
1da177e4
LT
229
230 /* search for objects that share the same slot */
231 for (slot = bridge->slots; slot; slot = slot->next)
232 if (slot->device == device) {
233 if (slot->sun != sun)
234 warn("sibling found, but _SUN doesn't match!\n");
235 break;
236 }
237
238 if (!slot) {
f5afe806 239 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
1da177e4
LT
240 if (!slot) {
241 kfree(newfunc);
242 return AE_NO_MEMORY;
243 }
244
1da177e4 245 slot->bridge = bridge;
1da177e4
LT
246 slot->device = device;
247 slot->sun = sun;
248 INIT_LIST_HEAD(&slot->funcs);
6aa4cdd0 249 mutex_init(&slot->crit_sect);
1da177e4
LT
250
251 slot->next = bridge->slots;
252 bridge->slots = slot;
253
254 bridge->nr_slots++;
255
42f49a6a
RS
256 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
257 slot->sun, pci_domain_nr(bridge->pci_bus),
258 bridge->pci_bus->number, slot->device);
e27da381
MT
259 retval = acpiphp_register_hotplug_slot(slot);
260 if (retval) {
261 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
262 goto err_exit;
263 }
1da177e4
LT
264 }
265
266 newfunc->slot = slot;
267 list_add_tail(&newfunc->sibling, &slot->funcs);
268
269 /* associate corresponding pci_dev */
42f49a6a 270 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
1da177e4
LT
271 PCI_DEVFN(device, function));
272 if (newfunc->pci_dev) {
1da177e4
LT
273 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
274 }
275
4e8662bb
KA
276 if (is_dock_device(handle)) {
277 /* we don't want to call this device's _EJ0
278 * because we want the dock notify handler
279 * to call it after it calls _DCK
20416ea5
KA
280 */
281 newfunc->flags &= ~FUNC_HAS_EJ0;
4e8662bb
KA
282 if (register_hotplug_dock_device(handle,
283 handle_hotplug_event_func, newfunc))
284 dbg("failed to register dock device\n");
285
286 /* we need to be notified when dock events happen
287 * outside of the hotplug operation, since we may
288 * need to do fixups before we can hotplug.
289 */
290 newfunc->nb.notifier_call = post_dock_fixups;
291 if (register_dock_notifier(&newfunc->nb))
292 dbg("failed to register a dock notifier");
20416ea5
KA
293 }
294
1da177e4 295 /* install notify handler */
20416ea5
KA
296 if (!(newfunc->flags & FUNC_HAS_DCK)) {
297 status = acpi_install_notify_handler(handle,
1da177e4
LT
298 ACPI_SYSTEM_NOTIFY,
299 handle_hotplug_event_func,
300 newfunc);
301
20416ea5
KA
302 if (ACPI_FAILURE(status))
303 err("failed to register interrupt notify handler\n");
304 } else
305 status = AE_OK;
1da177e4 306
20416ea5 307 return status;
e27da381
MT
308
309 err_exit:
310 bridge->nr_slots--;
311 bridge->slots = slot->next;
312 kfree(slot);
313 kfree(newfunc);
314
315 return AE_OK;
1da177e4
LT
316}
317
318
319/* see if it's worth looking at this bridge */
320static int detect_ejectable_slots(acpi_handle *bridge_handle)
321{
322 acpi_status status;
323 int count;
324
325 count = 0;
326
327 /* only check slots defined directly below bridge object */
328 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
329 is_ejectable_slot, (void *)&count, NULL);
330
4e8662bb
KA
331 /*
332 * we also need to add this bridge if there is a dock bridge or
333 * other pci device on a dock station (removable)
334 */
335 if (!count)
336 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
337 (u32)1, is_pci_dock_device, (void *)&count,
338 NULL);
339
1da177e4
LT
340 return count;
341}
342
343
1da177e4
LT
344/* decode ACPI 2.0 _HPP hot plug parameters */
345static void decode_hpp(struct acpiphp_bridge *bridge)
346{
347 acpi_status status;
1da177e4 348
7430e34c 349 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
e22b7350
KK
350 if (ACPI_FAILURE(status) ||
351 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
783c49fc 352 /* use default numbers */
e22b7350
KK
353 printk(KERN_WARNING
354 "%s: Could not get hotplug parameters. Use defaults\n",
355 __FUNCTION__);
356 bridge->hpp.t0 = &bridge->hpp.type0_data;
357 bridge->hpp.t0->revision = 0;
358 bridge->hpp.t0->cache_line_size = 0x10;
359 bridge->hpp.t0->latency_timer = 0x40;
360 bridge->hpp.t0->enable_serr = 0;
361 bridge->hpp.t0->enable_perr = 0;
1da177e4 362 }
1da177e4
LT
363}
364
365
783c49fc 366
1da177e4
LT
367/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
368static void init_bridge_misc(struct acpiphp_bridge *bridge)
369{
370 acpi_status status;
371
372 /* decode ACPI 2.0 _HPP (hot plug parameters) */
373 decode_hpp(bridge);
374
e27da381
MT
375 /* must be added to the list prior to calling register_slot */
376 list_add(&bridge->list, &bridge_list);
377
1da177e4
LT
378 /* register all slot objects under this bridge */
379 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
380 register_slot, bridge, NULL);
e27da381
MT
381 if (ACPI_FAILURE(status)) {
382 list_del(&bridge->list);
383 return;
384 }
1da177e4
LT
385
386 /* install notify handler */
8e7561cf 387 if (bridge->type != BRIDGE_TYPE_HOST) {
551bcb75
MT
388 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
389 status = acpi_remove_notify_handler(bridge->func->handle,
390 ACPI_SYSTEM_NOTIFY,
391 handle_hotplug_event_func);
392 if (ACPI_FAILURE(status))
393 err("failed to remove notify handler\n");
394 }
8e7561cf 395 status = acpi_install_notify_handler(bridge->handle,
1da177e4
LT
396 ACPI_SYSTEM_NOTIFY,
397 handle_hotplug_event_bridge,
398 bridge);
399
8e7561cf
RS
400 if (ACPI_FAILURE(status)) {
401 err("failed to register interrupt notify handler\n");
402 }
1da177e4 403 }
1da177e4
LT
404}
405
406
551bcb75
MT
407/* find acpiphp_func from acpiphp_bridge */
408static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
409{
410 struct list_head *node, *l;
411 struct acpiphp_bridge *bridge;
412 struct acpiphp_slot *slot;
413 struct acpiphp_func *func;
414
415 list_for_each(node, &bridge_list) {
416 bridge = list_entry(node, struct acpiphp_bridge, list);
417 for (slot = bridge->slots; slot; slot = slot->next) {
418 list_for_each(l, &slot->funcs) {
419 func = list_entry(l, struct acpiphp_func,
420 sibling);
421 if (func->handle == handle)
422 return func;
423 }
424 }
425 }
426
427 return NULL;
428}
429
430
431static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
432{
433 acpi_handle dummy_handle;
434
435 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
436 "_STA", &dummy_handle)))
437 bridge->flags |= BRIDGE_HAS_STA;
438
439 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
440 "_EJ0", &dummy_handle)))
441 bridge->flags |= BRIDGE_HAS_EJ0;
442
443 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
444 "_PS0", &dummy_handle)))
445 bridge->flags |= BRIDGE_HAS_PS0;
446
447 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
448 "_PS3", &dummy_handle)))
449 bridge->flags |= BRIDGE_HAS_PS3;
450
451 /* is this ejectable p2p bridge? */
452 if (bridge->flags & BRIDGE_HAS_EJ0) {
453 struct acpiphp_func *func;
454
455 dbg("found ejectable p2p bridge\n");
456
457 /* make link between PCI bridge and PCI function */
458 func = acpiphp_bridge_handle_to_function(bridge->handle);
459 if (!func)
460 return;
461 bridge->func = func;
462 func->bridge = bridge;
463 }
464}
465
466
1da177e4 467/* allocate and initialize host bridge data structure */
42f49a6a 468static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
1da177e4 469{
1da177e4
LT
470 struct acpiphp_bridge *bridge;
471
f5afe806 472 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1da177e4
LT
473 if (bridge == NULL)
474 return;
475
1da177e4
LT
476 bridge->type = BRIDGE_TYPE_HOST;
477 bridge->handle = handle;
1da177e4 478
42f49a6a 479 bridge->pci_bus = pci_bus;
1da177e4
LT
480
481 spin_lock_init(&bridge->res_lock);
482
1da177e4
LT
483 init_bridge_misc(bridge);
484}
485
486
487/* allocate and initialize PCI-to-PCI bridge data structure */
42f49a6a 488static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
1da177e4
LT
489{
490 struct acpiphp_bridge *bridge;
1da177e4 491
f5afe806 492 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1da177e4
LT
493 if (bridge == NULL) {
494 err("out of memory\n");
495 return;
496 }
497
1da177e4
LT
498 bridge->type = BRIDGE_TYPE_P2P;
499 bridge->handle = handle;
551bcb75 500 config_p2p_bridge_flags(bridge);
1da177e4 501
42f49a6a
RS
502 bridge->pci_dev = pci_dev_get(pci_dev);
503 bridge->pci_bus = pci_dev->subordinate;
1da177e4
LT
504 if (!bridge->pci_bus) {
505 err("This is not a PCI-to-PCI bridge!\n");
42f49a6a 506 goto err;
1da177e4
LT
507 }
508
509 spin_lock_init(&bridge->res_lock);
510
1da177e4 511 init_bridge_misc(bridge);
42f49a6a
RS
512 return;
513 err:
514 pci_dev_put(pci_dev);
515 kfree(bridge);
516 return;
1da177e4
LT
517}
518
519
520/* callback routine to find P2P bridges */
521static acpi_status
522find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
523{
524 acpi_status status;
525 acpi_handle dummy_handle;
1da177e4 526 unsigned long tmp;
42f49a6a 527 int device, function;
1da177e4 528 struct pci_dev *dev;
42f49a6a 529 struct pci_bus *pci_bus = context;
1da177e4
LT
530
531 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
532 if (ACPI_FAILURE(status))
533 return AE_OK; /* continue */
534
535 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
536 if (ACPI_FAILURE(status)) {
537 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
538 return AE_OK;
539 }
540
541 device = (tmp >> 16) & 0xffff;
542 function = tmp & 0xffff;
543
42f49a6a 544 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
1da177e4 545
42f49a6a
RS
546 if (!dev || !dev->subordinate)
547 goto out;
1da177e4
LT
548
549 /* check if this bridge has ejectable slots */
4e8662bb 550 if ((detect_ejectable_slots(handle) > 0)) {
1da177e4 551 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
42f49a6a 552 add_p2p_bridge(handle, dev);
1da177e4
LT
553 }
554
7c8f25da
KK
555 /* search P2P bridges under this p2p bridge */
556 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
557 find_p2p_bridge, dev->subordinate, NULL);
558 if (ACPI_FAILURE(status))
551bcb75 559 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
7c8f25da 560
42f49a6a
RS
561 out:
562 pci_dev_put(dev);
1da177e4
LT
563 return AE_OK;
564}
565
566
567/* find hot-pluggable slots, and then find P2P bridge */
568static int add_bridge(acpi_handle handle)
569{
570 acpi_status status;
571 unsigned long tmp;
572 int seg, bus;
573 acpi_handle dummy_handle;
42f49a6a 574 struct pci_bus *pci_bus;
1da177e4
LT
575
576 /* if the bridge doesn't have _STA, we assume it is always there */
577 status = acpi_get_handle(handle, "_STA", &dummy_handle);
578 if (ACPI_SUCCESS(status)) {
579 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
580 if (ACPI_FAILURE(status)) {
581 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
582 return 0;
583 }
584 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
585 /* don't register this object */
586 return 0;
587 }
588
589 /* get PCI segment number */
590 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
591
592 seg = ACPI_SUCCESS(status) ? tmp : 0;
593
594 /* get PCI bus number */
595 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
596
597 if (ACPI_SUCCESS(status)) {
598 bus = tmp;
599 } else {
600 warn("can't get bus number, assuming 0\n");
601 bus = 0;
602 }
603
42f49a6a
RS
604 pci_bus = pci_find_bus(seg, bus);
605 if (!pci_bus) {
606 err("Can't find bus %04x:%02x\n", seg, bus);
607 return 0;
608 }
609
1da177e4
LT
610 /* check if this bridge has ejectable slots */
611 if (detect_ejectable_slots(handle) > 0) {
612 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
42f49a6a 613 add_host_bridge(handle, pci_bus);
1da177e4
LT
614 }
615
1da177e4
LT
616 /* search P2P bridges under this host bridge */
617 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
42f49a6a 618 find_p2p_bridge, pci_bus, NULL);
1da177e4
LT
619
620 if (ACPI_FAILURE(status))
551bcb75 621 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
1da177e4
LT
622
623 return 0;
624}
625
42f49a6a
RS
626static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
627{
628 struct list_head *head;
629 list_for_each(head, &bridge_list) {
630 struct acpiphp_bridge *bridge = list_entry(head,
631 struct acpiphp_bridge, list);
632 if (bridge->handle == handle)
633 return bridge;
634 }
635
636 return NULL;
637}
1da177e4 638
364d5094 639static void cleanup_bridge(struct acpiphp_bridge *bridge)
1da177e4 640{
42f49a6a 641 struct list_head *list, *tmp;
42f49a6a
RS
642 struct acpiphp_slot *slot;
643 acpi_status status;
364d5094 644 acpi_handle handle = bridge->handle;
42f49a6a
RS
645
646 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
647 handle_hotplug_event_bridge);
648 if (ACPI_FAILURE(status))
649 err("failed to remove notify handler\n");
650
551bcb75
MT
651 if ((bridge->type != BRIDGE_TYPE_HOST) &&
652 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
653 status = acpi_install_notify_handler(bridge->func->handle,
654 ACPI_SYSTEM_NOTIFY,
655 handle_hotplug_event_func,
656 bridge->func);
657 if (ACPI_FAILURE(status))
658 err("failed to install interrupt notify handler\n");
659 }
660
42f49a6a
RS
661 slot = bridge->slots;
662 while (slot) {
663 struct acpiphp_slot *next = slot->next;
664 list_for_each_safe (list, tmp, &slot->funcs) {
665 struct acpiphp_func *func;
666 func = list_entry(list, struct acpiphp_func, sibling);
4e8662bb
KA
667 if (is_dock_device(func->handle)) {
668 unregister_hotplug_dock_device(func->handle);
669 unregister_dock_notifier(&func->nb);
670 }
20416ea5
KA
671 if (!(func->flags & FUNC_HAS_DCK)) {
672 status = acpi_remove_notify_handler(func->handle,
42f49a6a
RS
673 ACPI_SYSTEM_NOTIFY,
674 handle_hotplug_event_func);
20416ea5
KA
675 if (ACPI_FAILURE(status))
676 err("failed to remove notify handler\n");
677 }
42f49a6a
RS
678 pci_dev_put(func->pci_dev);
679 list_del(list);
680 kfree(func);
681 }
e27da381
MT
682 acpiphp_unregister_hotplug_slot(slot);
683 list_del(&slot->funcs);
42f49a6a
RS
684 kfree(slot);
685 slot = next;
686 }
687
688 pci_dev_put(bridge->pci_dev);
689 list_del(&bridge->list);
690 kfree(bridge);
1da177e4
LT
691}
692
364d5094
RS
693static acpi_status
694cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
695{
696 struct acpiphp_bridge *bridge;
697
551bcb75
MT
698 /* cleanup p2p bridges under this P2P bridge
699 in a depth-first manner */
700 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
701 cleanup_p2p_bridge, NULL, NULL);
702
364d5094
RS
703 if (!(bridge = acpiphp_handle_to_bridge(handle)))
704 return AE_OK;
705 cleanup_bridge(bridge);
706 return AE_OK;
707}
708
709static void remove_bridge(acpi_handle handle)
710{
711 struct acpiphp_bridge *bridge;
712
551bcb75
MT
713 /* cleanup p2p bridges under this host bridge
714 in a depth-first manner */
715 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
716 (u32)1, cleanup_p2p_bridge, NULL, NULL);
717
364d5094 718 bridge = acpiphp_handle_to_bridge(handle);
551bcb75 719 if (bridge)
364d5094 720 cleanup_bridge(bridge);
364d5094 721}
1da177e4 722
a0d399a8
KK
723static struct pci_dev * get_apic_pci_info(acpi_handle handle)
724{
725 struct acpi_pci_id id;
726 struct pci_bus *bus;
727 struct pci_dev *dev;
728
729 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
730 return NULL;
731
732 bus = pci_find_bus(id.segment, id.bus);
733 if (!bus)
734 return NULL;
735
736 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
737 if (!dev)
738 return NULL;
739
740 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
741 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
742 {
743 pci_dev_put(dev);
744 return NULL;
745 }
746
747 return dev;
748}
749
750static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
751{
752 acpi_status status;
753 int result = -1;
754 unsigned long gsb;
755 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
756 union acpi_object *obj;
757 void *table;
758
759 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
760 if (ACPI_SUCCESS(status)) {
761 *gsi_base = (u32)gsb;
762 return 0;
763 }
764
765 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
766 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
767 return -1;
768
769 obj = buffer.pointer;
770 if (obj->type != ACPI_TYPE_BUFFER)
771 goto out;
772
773 table = obj->buffer.pointer;
774 switch (((acpi_table_entry_header *)table)->type) {
775 case ACPI_MADT_IOSAPIC:
776 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
777 result = 0;
778 break;
779 case ACPI_MADT_IOAPIC:
780 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
781 result = 0;
782 break;
783 default:
784 break;
785 }
786 out:
81b26bca 787 kfree(buffer.pointer);
a0d399a8
KK
788 return result;
789}
790
791static acpi_status
792ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
793{
794 acpi_status status;
795 unsigned long sta;
796 acpi_handle tmp;
797 struct pci_dev *pdev;
798 u32 gsi_base;
799 u64 phys_addr;
800
801 /* Evaluate _STA if present */
802 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
803 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
804 return AE_CTRL_DEPTH;
805
806 /* Scan only PCI bus scope */
807 status = acpi_get_handle(handle, "_HID", &tmp);
808 if (ACPI_SUCCESS(status))
809 return AE_CTRL_DEPTH;
810
811 if (get_gsi_base(handle, &gsi_base))
812 return AE_OK;
813
814 pdev = get_apic_pci_info(handle);
815 if (!pdev)
816 return AE_OK;
817
818 if (pci_enable_device(pdev)) {
819 pci_dev_put(pdev);
820 return AE_OK;
821 }
822
823 pci_set_master(pdev);
824
825 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
826 pci_disable_device(pdev);
827 pci_dev_put(pdev);
828 return AE_OK;
829 }
830
831 phys_addr = pci_resource_start(pdev, 0);
832 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
833 pci_release_region(pdev, 0);
834 pci_disable_device(pdev);
835 pci_dev_put(pdev);
836 return AE_OK;
837 }
838
839 return AE_OK;
840}
841
842static int acpiphp_configure_ioapics(acpi_handle handle)
843{
844 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
845 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
846 return 0;
847}
848
1da177e4
LT
849static int power_on_slot(struct acpiphp_slot *slot)
850{
851 acpi_status status;
852 struct acpiphp_func *func;
853 struct list_head *l;
854 int retval = 0;
855
856 /* if already enabled, just skip */
857 if (slot->flags & SLOT_POWEREDON)
858 goto err_exit;
859
860 list_for_each (l, &slot->funcs) {
861 func = list_entry(l, struct acpiphp_func, sibling);
862
863 if (func->flags & FUNC_HAS_PS0) {
864 dbg("%s: executing _PS0\n", __FUNCTION__);
865 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
866 if (ACPI_FAILURE(status)) {
867 warn("%s: _PS0 failed\n", __FUNCTION__);
868 retval = -1;
869 goto err_exit;
870 } else
871 break;
872 }
873 }
874
875 /* TBD: evaluate _STA to check if the slot is enabled */
876
877 slot->flags |= SLOT_POWEREDON;
878
879 err_exit:
880 return retval;
881}
882
883
884static int power_off_slot(struct acpiphp_slot *slot)
885{
886 acpi_status status;
887 struct acpiphp_func *func;
888 struct list_head *l;
1da177e4
LT
889
890 int retval = 0;
891
892 /* if already disabled, just skip */
893 if ((slot->flags & SLOT_POWEREDON) == 0)
894 goto err_exit;
895
896 list_for_each (l, &slot->funcs) {
897 func = list_entry(l, struct acpiphp_func, sibling);
898
2f523b15 899 if (func->flags & FUNC_HAS_PS3) {
1da177e4
LT
900 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
901 if (ACPI_FAILURE(status)) {
902 warn("%s: _PS3 failed\n", __FUNCTION__);
903 retval = -1;
904 goto err_exit;
905 } else
906 break;
907 }
908 }
909
1da177e4
LT
910 /* TBD: evaluate _STA to check if the slot is disabled */
911
912 slot->flags &= (~SLOT_POWEREDON);
913
914 err_exit:
915 return retval;
916}
917
918
15a1ae74
KA
919
920/**
921 * acpiphp_max_busnr - return the highest reserved bus number under
922 * the given bus.
923 * @bus: bus to start search with
924 *
925 */
926static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
927{
928 struct list_head *tmp;
929 unsigned char max, n;
930
931 /*
932 * pci_bus_max_busnr will return the highest
933 * reserved busnr for all these children.
934 * that is equivalent to the bus->subordinate
935 * value. We don't want to use the parent's
936 * bus->subordinate value because it could have
937 * padding in it.
938 */
939 max = bus->secondary;
940
941 list_for_each(tmp, &bus->children) {
942 n = pci_bus_max_busnr(pci_bus_b(tmp));
943 if (n > max)
944 max = n;
945 }
946 return max;
947}
948
949
15a1ae74
KA
950/**
951 * acpiphp_bus_add - add a new bus to acpi subsystem
952 * @func: acpiphp_func of the bridge
953 *
954 */
955static int acpiphp_bus_add(struct acpiphp_func *func)
956{
957 acpi_handle phandle;
958 struct acpi_device *device, *pdevice;
959 int ret_val;
960
961 acpi_get_parent(func->handle, &phandle);
962 if (acpi_bus_get_device(phandle, &pdevice)) {
963 dbg("no parent device, assuming NULL\n");
964 pdevice = NULL;
965 }
20416ea5
KA
966 if (!acpi_bus_get_device(func->handle, &device)) {
967 dbg("bus exists... trim\n");
968 /* this shouldn't be in here, so remove
969 * the bus then re-add it...
970 */
971 ret_val = acpi_bus_trim(device, 1);
972 dbg("acpi_bus_trim return %x\n", ret_val);
973 }
974
975 ret_val = acpi_bus_add(&device, pdevice, func->handle,
976 ACPI_BUS_TYPE_DEVICE);
977 if (ret_val) {
978 dbg("error adding bus, %x\n",
979 -ret_val);
980 goto acpiphp_bus_add_out;
15a1ae74
KA
981 }
982 /*
983 * try to start anyway. We could have failed to add
984 * simply because this bus had previously been added
985 * on another add. Don't bother with the return value
986 * we just keep going.
987 */
988 ret_val = acpi_bus_start(device);
989
990acpiphp_bus_add_out:
991 return ret_val;
992}
993
994
92c9be95
MT
995/**
996 * acpiphp_bus_trim - trim a bus from acpi subsystem
997 * @handle: handle to acpi namespace
998 *
999 */
1000int acpiphp_bus_trim(acpi_handle handle)
1001{
1002 struct acpi_device *device;
1003 int retval;
1004
1005 retval = acpi_bus_get_device(handle, &device);
1006 if (retval) {
1007 dbg("acpi_device not found\n");
1008 return retval;
1009 }
1010
1011 retval = acpi_bus_trim(device, 1);
1012 if (retval)
1013 err("cannot remove from acpi list\n");
1014
1015 return retval;
1016}
15a1ae74 1017
1da177e4
LT
1018/**
1019 * enable_device - enable, configure a slot
1020 * @slot: slot to be enabled
1021 *
1022 * This function should be called per *physical slot*,
1023 * not per each slot object in ACPI namespace.
1024 *
1025 */
1026static int enable_device(struct acpiphp_slot *slot)
1027{
1da177e4 1028 struct pci_dev *dev;
42f49a6a 1029 struct pci_bus *bus = slot->bridge->pci_bus;
1da177e4
LT
1030 struct list_head *l;
1031 struct acpiphp_func *func;
1032 int retval = 0;
42f49a6a 1033 int num, max, pass;
551bcb75 1034 acpi_status status;
1da177e4
LT
1035
1036 if (slot->flags & SLOT_ENABLED)
1037 goto err_exit;
1038
1039 /* sanity check: dev should be NULL when hot-plugged in */
42f49a6a 1040 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1da177e4
LT
1041 if (dev) {
1042 /* This case shouldn't happen */
1043 err("pci_dev structure already exists.\n");
42f49a6a 1044 pci_dev_put(dev);
1da177e4
LT
1045 retval = -1;
1046 goto err_exit;
1047 }
1048
42f49a6a
RS
1049 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1050 if (num == 0) {
1da177e4
LT
1051 err("No new device found\n");
1052 retval = -1;
1053 goto err_exit;
1054 }
1055
15a1ae74 1056 max = acpiphp_max_busnr(bus);
42f49a6a
RS
1057 for (pass = 0; pass < 2; pass++) {
1058 list_for_each_entry(dev, &bus->devices, bus_list) {
1059 if (PCI_SLOT(dev->devfn) != slot->device)
1060 continue;
1061 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
c64b5eea 1062 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
42f49a6a 1063 max = pci_scan_bridge(bus, dev, max, pass);
92c9be95 1064 if (pass && dev->subordinate)
c64b5eea
KA
1065 pci_bus_size_bridges(dev->subordinate);
1066 }
42f49a6a 1067 }
1da177e4
LT
1068 }
1069
92c9be95
MT
1070 list_for_each (l, &slot->funcs) {
1071 func = list_entry(l, struct acpiphp_func, sibling);
1072 acpiphp_bus_add(func);
1073 }
1074
42f49a6a 1075 pci_bus_assign_resources(bus);
8e5dce35
KA
1076 acpiphp_sanitize_bus(bus);
1077 pci_enable_bridges(bus);
42f49a6a 1078 pci_bus_add_devices(bus);
0cccd0c2
MT
1079 acpiphp_set_hpp_values(slot->bridge->handle, bus);
1080 acpiphp_configure_ioapics(slot->bridge->handle);
42f49a6a 1081
1da177e4
LT
1082 /* associate pci_dev to our representation */
1083 list_for_each (l, &slot->funcs) {
1084 func = list_entry(l, struct acpiphp_func, sibling);
42f49a6a 1085 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1da177e4 1086 func->function));
551bcb75
MT
1087 if (!func->pci_dev)
1088 continue;
1089
1090 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1091 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1092 continue;
1093
1094 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1095 if (ACPI_FAILURE(status))
1096 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1097 status);
1da177e4
LT
1098 }
1099
1100 slot->flags |= SLOT_ENABLED;
1101
1da177e4
LT
1102 err_exit:
1103 return retval;
1104}
1105
1106
1107/**
1108 * disable_device - disable a slot
1109 */
1110static int disable_device(struct acpiphp_slot *slot)
1111{
1112 int retval = 0;
1113 struct acpiphp_func *func;
1114 struct list_head *l;
1115
1116 /* is this slot already disabled? */
1117 if (!(slot->flags & SLOT_ENABLED))
1118 goto err_exit;
1119
1120 list_for_each (l, &slot->funcs) {
1121 func = list_entry(l, struct acpiphp_func, sibling);
92c9be95 1122
551bcb75
MT
1123 if (func->bridge) {
1124 /* cleanup p2p bridges under this P2P bridge */
1125 cleanup_p2p_bridge(func->bridge->handle,
1126 (u32)1, NULL, NULL);
1127 func->bridge = NULL;
1128 }
1129
92c9be95
MT
1130 acpiphp_bus_trim(func->handle);
1131 /* try to remove anyway.
1132 * acpiphp_bus_add might have been failed */
1133
42f49a6a
RS
1134 if (!func->pci_dev)
1135 continue;
1da177e4 1136
42f49a6a
RS
1137 pci_remove_bus_device(func->pci_dev);
1138 pci_dev_put(func->pci_dev);
1139 func->pci_dev = NULL;
1da177e4
LT
1140 }
1141
1142 slot->flags &= (~SLOT_ENABLED);
1143
1144 err_exit:
1145 return retval;
1146}
1147
1148
1149/**
1150 * get_slot_status - get ACPI slot status
1151 *
1152 * if a slot has _STA for each function and if any one of them
1153 * returned non-zero status, return it
1154 *
1155 * if a slot doesn't have _STA and if any one of its functions'
1156 * configuration space is configured, return 0x0f as a _STA
1157 *
1158 * otherwise return 0
1159 */
1160static unsigned int get_slot_status(struct acpiphp_slot *slot)
1161{
1162 acpi_status status;
1163 unsigned long sta = 0;
1164 u32 dvid;
1165 struct list_head *l;
1166 struct acpiphp_func *func;
1167
1168 list_for_each (l, &slot->funcs) {
1169 func = list_entry(l, struct acpiphp_func, sibling);
1170
1171 if (func->flags & FUNC_HAS_STA) {
1172 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1173 if (ACPI_SUCCESS(status) && sta)
1174 break;
1175 } else {
1176 pci_bus_read_config_dword(slot->bridge->pci_bus,
1177 PCI_DEVFN(slot->device,
1178 func->function),
1179 PCI_VENDOR_ID, &dvid);
1180 if (dvid != 0xffffffff) {
1181 sta = ACPI_STA_ALL;
1182 break;
1183 }
1184 }
1185 }
1186
1187 return (unsigned int)sta;
1188}
1189
8d50e332
RS
1190/**
1191 * acpiphp_eject_slot - physically eject the slot
1192 */
1193static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1194{
1195 acpi_status status;
1196 struct acpiphp_func *func;
1197 struct list_head *l;
1198 struct acpi_object_list arg_list;
1199 union acpi_object arg;
1200
1201 list_for_each (l, &slot->funcs) {
1202 func = list_entry(l, struct acpiphp_func, sibling);
1203
1204 /* We don't want to call _EJ0 on non-existing functions. */
1205 if ((func->flags & FUNC_HAS_EJ0)) {
1206 /* _EJ0 method take one argument */
1207 arg_list.count = 1;
1208 arg_list.pointer = &arg;
1209 arg.type = ACPI_TYPE_INTEGER;
1210 arg.integer.value = 1;
1211
1212 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1213 if (ACPI_FAILURE(status)) {
1214 warn("%s: _EJ0 failed\n", __FUNCTION__);
1215 return -1;
1216 } else
1217 break;
1218 }
1219 }
1220 return 0;
1221}
1222
1da177e4
LT
1223/**
1224 * acpiphp_check_bridge - re-enumerate devices
1225 *
1226 * Iterate over all slots under this bridge and make sure that if a
1227 * card is present they are enabled, and if not they are disabled.
1228 */
1229static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1230{
1231 struct acpiphp_slot *slot;
1232 int retval = 0;
1233 int enabled, disabled;
1234
1235 enabled = disabled = 0;
1236
1237 for (slot = bridge->slots; slot; slot = slot->next) {
1238 unsigned int status = get_slot_status(slot);
1239 if (slot->flags & SLOT_ENABLED) {
1240 if (status == ACPI_STA_ALL)
1241 continue;
1242 retval = acpiphp_disable_slot(slot);
1243 if (retval) {
1244 err("Error occurred in disabling\n");
1245 goto err_exit;
8d50e332
RS
1246 } else {
1247 acpiphp_eject_slot(slot);
1da177e4
LT
1248 }
1249 disabled++;
1250 } else {
1251 if (status != ACPI_STA_ALL)
1252 continue;
1253 retval = acpiphp_enable_slot(slot);
1254 if (retval) {
1255 err("Error occurred in enabling\n");
1256 goto err_exit;
1257 }
1258 enabled++;
1259 }
1260 }
1261
1262 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1263
1264 err_exit:
1265 return retval;
1266}
1267
8e7561cf
RS
1268static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1269{
1270 u16 pci_cmd, pci_bctl;
1271 struct pci_dev *cdev;
1272
1273 /* Program hpp values for this device */
1274 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1275 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1276 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1277 return;
e22b7350 1278
8e7561cf 1279 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
e22b7350 1280 bridge->hpp.t0->cache_line_size);
8e7561cf 1281 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
e22b7350 1282 bridge->hpp.t0->latency_timer);
8e7561cf 1283 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
e22b7350 1284 if (bridge->hpp.t0->enable_serr)
8e7561cf
RS
1285 pci_cmd |= PCI_COMMAND_SERR;
1286 else
1287 pci_cmd &= ~PCI_COMMAND_SERR;
e22b7350 1288 if (bridge->hpp.t0->enable_perr)
8e7561cf
RS
1289 pci_cmd |= PCI_COMMAND_PARITY;
1290 else
1291 pci_cmd &= ~PCI_COMMAND_PARITY;
1292 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1293
1294 /* Program bridge control value and child devices */
1295 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1296 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
e22b7350 1297 bridge->hpp.t0->latency_timer);
8e7561cf 1298 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
e22b7350 1299 if (bridge->hpp.t0->enable_serr)
8e7561cf
RS
1300 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1301 else
1302 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
e22b7350 1303 if (bridge->hpp.t0->enable_perr)
8e7561cf
RS
1304 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1305 else
1306 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1307 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1308 if (dev->subordinate) {
1309 list_for_each_entry(cdev, &dev->subordinate->devices,
1310 bus_list)
1311 program_hpp(cdev, bridge);
1312 }
1313 }
1314}
1315
1316static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1317{
1318 struct acpiphp_bridge bridge;
1319 struct pci_dev *dev;
1320
1321 memset(&bridge, 0, sizeof(bridge));
1322 bridge.handle = handle;
7430e34c 1323 bridge.pci_bus = bus;
783c49fc 1324 bridge.pci_dev = bus->self;
8e7561cf
RS
1325 decode_hpp(&bridge);
1326 list_for_each_entry(dev, &bus->devices, bus_list)
1327 program_hpp(dev, &bridge);
1328
1329}
1330
1331/*
1332 * Remove devices for which we could not assign resources, call
1333 * arch specific code to fix-up the bus
1334 */
1335static void acpiphp_sanitize_bus(struct pci_bus *bus)
1336{
1337 struct pci_dev *dev;
1338 int i;
1339 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1340
1341 list_for_each_entry(dev, &bus->devices, bus_list) {
1342 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1343 struct resource *res = &dev->resource[i];
1344 if ((res->flags & type_mask) && !res->start &&
1345 res->end) {
1346 /* Could not assign a required resources
1347 * for this device, remove it */
1348 pci_remove_bus_device(dev);
1349 break;
1350 }
1351 }
1352 }
1353}
1354
1355/* Program resources in newly inserted bridge */
1356static int acpiphp_configure_bridge (acpi_handle handle)
1357{
1358 struct acpi_pci_id pci_id;
1359 struct pci_bus *bus;
1360
1361 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1362 err("cannot get PCI domain and bus number for bridge\n");
1363 return -EINVAL;
1364 }
1365 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1366 if (!bus) {
1367 err("cannot find bus %d:%d\n",
1368 pci_id.segment, pci_id.bus);
1369 return -EINVAL;
1370 }
1371
1372 pci_bus_size_bridges(bus);
1373 pci_bus_assign_resources(bus);
1374 acpiphp_sanitize_bus(bus);
1375 acpiphp_set_hpp_values(handle, bus);
1376 pci_enable_bridges(bus);
a0d399a8 1377 acpiphp_configure_ioapics(handle);
8e7561cf
RS
1378 return 0;
1379}
1380
1381static void handle_bridge_insertion(acpi_handle handle, u32 type)
1382{
1383 struct acpi_device *device, *pdevice;
1384 acpi_handle phandle;
1385
1386 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1387 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1388 err("unexpected notification type %d\n", type);
1389 return;
1390 }
1391
1392 acpi_get_parent(handle, &phandle);
1393 if (acpi_bus_get_device(phandle, &pdevice)) {
1394 dbg("no parent device, assuming NULL\n");
1395 pdevice = NULL;
1396 }
1397 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1398 err("cannot add bridge to acpi list\n");
1399 return;
1400 }
1401 if (!acpiphp_configure_bridge(handle) &&
1402 !acpi_bus_start(device))
1403 add_bridge(handle);
1404 else
1405 err("cannot configure and start bridge\n");
1406
1407}
1408
1da177e4
LT
1409/*
1410 * ACPI event handlers
1411 */
1412
1413/**
1414 * handle_hotplug_event_bridge - handle ACPI event on bridges
1415 *
1416 * @handle: Notify()'ed acpi_handle
1417 * @type: Notify code
1418 * @context: pointer to acpiphp_bridge structure
1419 *
1420 * handles ACPI event notification on {host,p2p} bridges
1421 *
1422 */
1423static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1424{
1425 struct acpiphp_bridge *bridge;
1426 char objname[64];
1427 struct acpi_buffer buffer = { .length = sizeof(objname),
1428 .pointer = objname };
8e7561cf 1429 struct acpi_device *device;
1da177e4 1430
8e7561cf
RS
1431 if (acpi_bus_get_device(handle, &device)) {
1432 /* This bridge must have just been physically inserted */
1433 handle_bridge_insertion(handle, type);
1434 return;
1435 }
1436
1437 bridge = acpiphp_handle_to_bridge(handle);
1438 if (!bridge) {
1439 err("cannot get bridge info\n");
1440 return;
1441 }
1da177e4
LT
1442
1443 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1444
1445 switch (type) {
1446 case ACPI_NOTIFY_BUS_CHECK:
1447 /* bus re-enumerate */
1448 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1449 acpiphp_check_bridge(bridge);
1450 break;
1451
1452 case ACPI_NOTIFY_DEVICE_CHECK:
1453 /* device check */
1454 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1455 acpiphp_check_bridge(bridge);
1456 break;
1457
1458 case ACPI_NOTIFY_DEVICE_WAKE:
1459 /* wake event */
1460 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1461 break;
1462
1463 case ACPI_NOTIFY_EJECT_REQUEST:
1464 /* request device eject */
1465 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
551bcb75
MT
1466 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1467 (bridge->flags & BRIDGE_HAS_EJ0)) {
1468 struct acpiphp_slot *slot;
1469 slot = bridge->func->slot;
1470 if (!acpiphp_disable_slot(slot))
1471 acpiphp_eject_slot(slot);
1472 }
1da177e4
LT
1473 break;
1474
1475 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1476 printk(KERN_ERR "Device %s cannot be configured due"
1477 " to a frequency mismatch\n", objname);
1478 break;
1479
1480 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1481 printk(KERN_ERR "Device %s cannot be configured due"
1482 " to a bus mode mismatch\n", objname);
1483 break;
1484
1485 case ACPI_NOTIFY_POWER_FAULT:
1486 printk(KERN_ERR "Device %s has suffered a power fault\n",
1487 objname);
1488 break;
1489
1490 default:
1491 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1492 break;
1493 }
1494}
1495
1da177e4
LT
1496/**
1497 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1498 *
1499 * @handle: Notify()'ed acpi_handle
1500 * @type: Notify code
1501 * @context: pointer to acpiphp_func structure
1502 *
1503 * handles ACPI event notification on slots
1504 *
1505 */
2b85e130 1506static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1da177e4
LT
1507{
1508 struct acpiphp_func *func;
1509 char objname[64];
1510 struct acpi_buffer buffer = { .length = sizeof(objname),
1511 .pointer = objname };
1512
1513 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1514
1515 func = (struct acpiphp_func *)context;
1516
1517 switch (type) {
1518 case ACPI_NOTIFY_BUS_CHECK:
1519 /* bus re-enumerate */
1520 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1521 acpiphp_enable_slot(func->slot);
1522 break;
1523
1524 case ACPI_NOTIFY_DEVICE_CHECK:
1525 /* device check : re-enumerate from parent bus */
1526 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1527 acpiphp_check_bridge(func->slot->bridge);
1528 break;
1529
1530 case ACPI_NOTIFY_DEVICE_WAKE:
1531 /* wake event */
1532 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1533 break;
1534
1535 case ACPI_NOTIFY_EJECT_REQUEST:
1536 /* request device eject */
1537 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
8d50e332
RS
1538 if (!(acpiphp_disable_slot(func->slot)))
1539 acpiphp_eject_slot(func->slot);
1da177e4
LT
1540 break;
1541
1542 default:
1543 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1544 break;
1545 }
1546}
1547
8e7561cf
RS
1548
1549static acpi_status
1550find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1551{
1552 int *count = (int *)context;
1553
783c49fc 1554 if (acpi_root_bridge(handle)) {
8e7561cf
RS
1555 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1556 handle_hotplug_event_bridge, NULL);
1557 (*count)++;
1558 }
1559 return AE_OK ;
1560}
1da177e4
LT
1561
1562static struct acpi_pci_driver acpi_pci_hp_driver = {
1563 .add = add_bridge,
1564 .remove = remove_bridge,
1565};
1566
1567/**
1568 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1569 *
1570 */
1571int __init acpiphp_glue_init(void)
1572{
8e7561cf 1573 int num = 0;
1da177e4 1574
8e7561cf
RS
1575 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1576 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1da177e4
LT
1577
1578 if (num <= 0)
1579 return -1;
8e7561cf
RS
1580 else
1581 acpi_pci_register_driver(&acpi_pci_hp_driver);
1da177e4
LT
1582
1583 return 0;
1584}
1585
1586
1587/**
1588 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1589 *
1590 * This function frees all data allocated in acpiphp_glue_init()
1591 */
1592void __exit acpiphp_glue_exit(void)
1593{
1da177e4
LT
1594 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1595}
1596
1597
1598/**
1599 * acpiphp_get_num_slots - count number of slots in a system
1600 */
1601int __init acpiphp_get_num_slots(void)
1602{
1603 struct list_head *node;
1604 struct acpiphp_bridge *bridge;
1605 int num_slots;
1606
1607 num_slots = 0;
1608
1609 list_for_each (node, &bridge_list) {
1610 bridge = (struct acpiphp_bridge *)node;
42f49a6a
RS
1611 dbg("Bus %04x:%02x has %d slot%s\n",
1612 pci_domain_nr(bridge->pci_bus),
1613 bridge->pci_bus->number, bridge->nr_slots,
1614 bridge->nr_slots == 1 ? "" : "s");
1da177e4
LT
1615 num_slots += bridge->nr_slots;
1616 }
1617
42f49a6a 1618 dbg("Total %d slots\n", num_slots);
1da177e4
LT
1619 return num_slots;
1620}
1621
1622
1623#if 0
1624/**
1625 * acpiphp_for_each_slot - call function for each slot
1626 * @fn: callback function
1627 * @data: context to be passed to callback function
1628 *
1629 */
1630static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1631{
1632 struct list_head *node;
1633 struct acpiphp_bridge *bridge;
1634 struct acpiphp_slot *slot;
1635 int retval = 0;
1636
1637 list_for_each (node, &bridge_list) {
1638 bridge = (struct acpiphp_bridge *)node;
1639 for (slot = bridge->slots; slot; slot = slot->next) {
1640 retval = fn(slot, data);
1641 if (!retval)
1642 goto err_exit;
1643 }
1644 }
1645
1646 err_exit:
1647 return retval;
1648}
1649#endif
1650
1da177e4
LT
1651
1652/**
1653 * acpiphp_enable_slot - power on slot
1654 */
1655int acpiphp_enable_slot(struct acpiphp_slot *slot)
1656{
1657 int retval;
1658
6aa4cdd0 1659 mutex_lock(&slot->crit_sect);
1da177e4
LT
1660
1661 /* wake up all functions */
1662 retval = power_on_slot(slot);
1663 if (retval)
1664 goto err_exit;
1665
cde0e5d7 1666 if (get_slot_status(slot) == ACPI_STA_ALL) {
1da177e4
LT
1667 /* configure all functions */
1668 retval = enable_device(slot);
cde0e5d7
MT
1669 if (retval)
1670 power_off_slot(slot);
1671 } else {
1672 dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__);
1673 power_off_slot(slot);
1674 }
1da177e4
LT
1675
1676 err_exit:
6aa4cdd0 1677 mutex_unlock(&slot->crit_sect);
1da177e4
LT
1678 return retval;
1679}
1680
1da177e4
LT
1681/**
1682 * acpiphp_disable_slot - power off slot
1683 */
1684int acpiphp_disable_slot(struct acpiphp_slot *slot)
1685{
1686 int retval = 0;
1687
6aa4cdd0 1688 mutex_lock(&slot->crit_sect);
1da177e4
LT
1689
1690 /* unconfigure all functions */
1691 retval = disable_device(slot);
1692 if (retval)
1693 goto err_exit;
1694
1695 /* power off all functions */
1696 retval = power_off_slot(slot);
1697 if (retval)
1698 goto err_exit;
1699
1da177e4 1700 err_exit:
6aa4cdd0 1701 mutex_unlock(&slot->crit_sect);
1da177e4
LT
1702 return retval;
1703}
1704
1705
1706/*
1707 * slot enabled: 1
1708 * slot disabled: 0
1709 */
1710u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1711{
8d50e332 1712 return (slot->flags & SLOT_POWEREDON);
1da177e4
LT
1713}
1714
1715
1716/*
1717 * latch closed: 1
1718 * latch open: 0
1719 */
1720u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1721{
1722 unsigned int sta;
1723
1724 sta = get_slot_status(slot);
1725
1726 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1727}
1728
1729
1730/*
1731 * adapter presence : 1
1732 * absence : 0
1733 */
1734u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1735{
1736 unsigned int sta;
1737
1738 sta = get_slot_status(slot);
1739
1740 return (sta == 0) ? 0 : 1;
1741}
1742
1743
1744/*
1745 * pci address (seg/bus/dev)
1746 */
1747u32 acpiphp_get_address(struct acpiphp_slot *slot)
1748{
1749 u32 address;
42f49a6a 1750 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1da177e4 1751
42f49a6a
RS
1752 address = (pci_domain_nr(pci_bus) << 16) |
1753 (pci_bus->number << 8) |
1da177e4
LT
1754 slot->device;
1755
1756 return address;
1757}