blob: fadd264b64c32728c66cf4156cb3255f0ac41fbf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
3 *
4 * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5 * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6 * Copyright (C) 2002,2003 NEC Corporation
Rajesh Shah42f49a62005-04-28 00:25:53 -07007 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
Rajesh Shah8e7561c2005-04-28 00:25:56 -07009 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * All rights reserved.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22 * NON INFRINGEMENT. See the GNU General Public License for more
23 * details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 * Send feedback to <t-kochi@bq.jp.nec.com>
30 *
31 */
32
Rajesh Shah42f49a62005-04-28 00:25:53 -070033/*
34 * Lifetime rules for pci_dev:
35 * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
36 * when the driver is loaded or when an insertion event occurs. It loses
37 * a refcount when its ejected or the driver unloads.
38 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
39 * when the bridge is scanned and it loses a refcount when the bridge
40 * is removed.
41 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/init.h>
44#include <linux/module.h>
45
46#include <linux/kernel.h>
47#include <linux/pci.h>
48#include <linux/smp_lock.h>
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +010049#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "../pci.h"
52#include "pci_hotplug.h"
53#include "acpiphp.h"
54
55static LIST_HEAD(bridge_list);
56
57#define MY_NAME "acpiphp_glue"
58
59static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070060static void acpiphp_sanitize_bus(struct pci_bus *bus);
61static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * initialization & terminatation routines
66 */
67
68/**
69 * is_ejectable - determine if a slot is ejectable
70 * @handle: handle to acpi namespace
71 *
72 * Ejectable slot should satisfy at least these conditions:
73 *
74 * 1. has _ADR method
75 * 2. has _EJ0 method
76 *
77 * optionally
78 *
79 * 1. has _STA method
80 * 2. has _PS0 method
81 * 3. has _PS3 method
82 * 4. ..
83 *
84 */
85static int is_ejectable(acpi_handle handle)
86{
87 acpi_status status;
88 acpi_handle tmp;
89
90 status = acpi_get_handle(handle, "_ADR", &tmp);
91 if (ACPI_FAILURE(status)) {
92 return 0;
93 }
94
95 status = acpi_get_handle(handle, "_EJ0", &tmp);
96 if (ACPI_FAILURE(status)) {
97 return 0;
98 }
99
100 return 1;
101}
102
103
104/* callback routine to check the existence of ejectable slots */
105static acpi_status
106is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
107{
108 int *count = (int *)context;
109
110 if (is_ejectable(handle)) {
111 (*count)++;
112 /* only one ejectable slot is enough */
113 return AE_CTRL_TERMINATE;
114 } else {
115 return AE_OK;
116 }
117}
118
119
120/* callback routine to register each ACPI PCI slot object */
121static acpi_status
122register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
123{
124 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
125 struct acpiphp_slot *slot;
126 struct acpiphp_func *newfunc;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800127 struct dependent_device *dd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 acpi_handle tmp;
129 acpi_status status = AE_OK;
130 unsigned long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800131 int device, function, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
134
135 if (ACPI_FAILURE(status))
136 return AE_OK;
137
138 status = acpi_get_handle(handle, "_EJ0", &tmp);
139
Kristen Accardi20416ea2006-02-23 17:56:03 -0800140 if (ACPI_FAILURE(status) && !(is_dependent_device(handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return AE_OK;
142
143 device = (adr >> 16) & 0xffff;
144 function = adr & 0xffff;
145
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100146 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!newfunc)
148 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 INIT_LIST_HEAD(&newfunc->sibling);
151 newfunc->handle = handle;
152 newfunc->function = function;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800153 if (ACPI_SUCCESS(status))
154 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
157 newfunc->flags |= FUNC_HAS_STA;
158
159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
160 newfunc->flags |= FUNC_HAS_PS0;
161
162 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
163 newfunc->flags |= FUNC_HAS_PS3;
164
Kristen Accardi20416ea2006-02-23 17:56:03 -0800165 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) {
166 newfunc->flags |= FUNC_HAS_DCK;
167 /* add to devices dependent on dock station,
168 * because this may actually be the dock bridge
169 */
170 dd = alloc_dependent_device(handle);
171 if (!dd)
172 err("Can't allocate memory for "
173 "new dependent device!\n");
174 else
175 add_dependent_device(dd);
176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
179 if (ACPI_FAILURE(status))
180 sun = -1;
181
182 /* search for objects that share the same slot */
183 for (slot = bridge->slots; slot; slot = slot->next)
184 if (slot->device == device) {
185 if (slot->sun != sun)
186 warn("sibling found, but _SUN doesn't match!\n");
187 break;
188 }
189
190 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100191 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!slot) {
193 kfree(newfunc);
194 return AE_NO_MEMORY;
195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 slot->device = device;
199 slot->sun = sun;
200 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100201 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 slot->next = bridge->slots;
204 bridge->slots = slot;
205
206 bridge->nr_slots++;
207
Rajesh Shah42f49a62005-04-28 00:25:53 -0700208 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
209 slot->sun, pci_domain_nr(bridge->pci_bus),
210 bridge->pci_bus->number, slot->device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800211 retval = acpiphp_register_hotplug_slot(slot);
212 if (retval) {
213 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
214 goto err_exit;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 newfunc->slot = slot;
219 list_add_tail(&newfunc->sibling, &slot->funcs);
220
221 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700222 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 PCI_DEVFN(device, function));
224 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
226 }
227
Kristen Accardi20416ea2006-02-23 17:56:03 -0800228 /* if this is a device dependent on a dock station,
229 * associate the acpiphp_func to the dependent_device
230 * struct.
231 */
232 if ((dd = get_dependent_device(handle))) {
233 newfunc->flags |= FUNC_IS_DD;
234 /*
235 * we don't want any devices which is dependent
236 * on the dock to have it's _EJ0 method executed.
237 * because we need to run _DCK first.
238 */
239 newfunc->flags &= ~FUNC_HAS_EJ0;
240 dd->func = newfunc;
241 add_pci_dependent_device(dd);
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800245 if (!(newfunc->flags & FUNC_HAS_DCK)) {
246 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ACPI_SYSTEM_NOTIFY,
248 handle_hotplug_event_func,
249 newfunc);
250
Kristen Accardi20416ea2006-02-23 17:56:03 -0800251 if (ACPI_FAILURE(status))
252 err("failed to register interrupt notify handler\n");
253 } else
254 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Kristen Accardi20416ea2006-02-23 17:56:03 -0800256 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800257
258 err_exit:
259 bridge->nr_slots--;
260 bridge->slots = slot->next;
261 kfree(slot);
262 kfree(newfunc);
263
264 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267
268/* see if it's worth looking at this bridge */
269static int detect_ejectable_slots(acpi_handle *bridge_handle)
270{
271 acpi_status status;
272 int count;
273
274 count = 0;
275
276 /* only check slots defined directly below bridge object */
277 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
278 is_ejectable_slot, (void *)&count, NULL);
279
280 return count;
281}
282
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* decode ACPI 2.0 _HPP hot plug parameters */
285static void decode_hpp(struct acpiphp_bridge *bridge)
286{
287 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Kristen Accardi783c49f2006-03-03 10:16:05 -0800289 status = acpi_get_hp_params_from_firmware(bridge->pci_dev, &bridge->hpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (ACPI_FAILURE(status)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800291 /* use default numbers */
292 bridge->hpp.cache_line_size = 0x10;
293 bridge->hpp.latency_timer = 0x40;
294 bridge->hpp.enable_serr = 0;
295 bridge->hpp.enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299
Kristen Accardi783c49f2006-03-03 10:16:05 -0800300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
302static void init_bridge_misc(struct acpiphp_bridge *bridge)
303{
304 acpi_status status;
305
306 /* decode ACPI 2.0 _HPP (hot plug parameters) */
307 decode_hpp(bridge);
308
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800309 /* must be added to the list prior to calling register_slot */
310 list_add(&bridge->list, &bridge_list);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* register all slot objects under this bridge */
313 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
314 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800315 if (ACPI_FAILURE(status)) {
316 list_del(&bridge->list);
317 return;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700321 if (bridge->type != BRIDGE_TYPE_HOST) {
322 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ACPI_SYSTEM_NOTIFY,
324 handle_hotplug_event_bridge,
325 bridge);
326
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700327 if (ACPI_FAILURE(status)) {
328 err("failed to register interrupt notify handler\n");
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333
334/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700335static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct acpiphp_bridge *bridge;
338
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100339 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (bridge == NULL)
341 return;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 bridge->type = BRIDGE_TYPE_HOST;
344 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Rajesh Shah42f49a62005-04-28 00:25:53 -0700346 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 spin_lock_init(&bridge->res_lock);
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 init_bridge_misc(bridge);
351}
352
353
354/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700355static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100359 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (bridge == NULL) {
361 err("out of memory\n");
362 return;
363 }
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 bridge->type = BRIDGE_TYPE_P2P;
366 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Rajesh Shah42f49a62005-04-28 00:25:53 -0700368 bridge->pci_dev = pci_dev_get(pci_dev);
369 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!bridge->pci_bus) {
371 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700372 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 spin_lock_init(&bridge->res_lock);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700378 return;
379 err:
380 pci_dev_put(pci_dev);
381 kfree(bridge);
382 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385
386/* callback routine to find P2P bridges */
387static acpi_status
388find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
389{
390 acpi_status status;
391 acpi_handle dummy_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 unsigned long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700393 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700395 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
398 if (ACPI_FAILURE(status))
399 return AE_OK; /* continue */
400
401 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
402 if (ACPI_FAILURE(status)) {
403 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
404 return AE_OK;
405 }
406
407 device = (tmp >> 16) & 0xffff;
408 function = tmp & 0xffff;
409
Rajesh Shah42f49a62005-04-28 00:25:53 -0700410 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Rajesh Shah42f49a62005-04-28 00:25:53 -0700412 if (!dev || !dev->subordinate)
413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* check if this bridge has ejectable slots */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800416 if ((detect_ejectable_slots(handle) > 0) ||
417 (detect_dependent_devices(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700419 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900422 /* search P2P bridges under this p2p bridge */
423 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
424 find_p2p_bridge, dev->subordinate, NULL);
425 if (ACPI_FAILURE(status))
426 warn("find_p2p_bridge faied (error code = 0x%x)\n", status);
427
Rajesh Shah42f49a62005-04-28 00:25:53 -0700428 out:
429 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return AE_OK;
431}
432
433
434/* find hot-pluggable slots, and then find P2P bridge */
435static int add_bridge(acpi_handle handle)
436{
437 acpi_status status;
438 unsigned long tmp;
439 int seg, bus;
440 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700441 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* if the bridge doesn't have _STA, we assume it is always there */
444 status = acpi_get_handle(handle, "_STA", &dummy_handle);
445 if (ACPI_SUCCESS(status)) {
446 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
447 if (ACPI_FAILURE(status)) {
448 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
449 return 0;
450 }
451 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
452 /* don't register this object */
453 return 0;
454 }
455
456 /* get PCI segment number */
457 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
458
459 seg = ACPI_SUCCESS(status) ? tmp : 0;
460
461 /* get PCI bus number */
462 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
463
464 if (ACPI_SUCCESS(status)) {
465 bus = tmp;
466 } else {
467 warn("can't get bus number, assuming 0\n");
468 bus = 0;
469 }
470
Rajesh Shah42f49a62005-04-28 00:25:53 -0700471 pci_bus = pci_find_bus(seg, bus);
472 if (!pci_bus) {
473 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475 }
476
Rajesh Shah42f49a62005-04-28 00:25:53 -0700477 /* check if this bridge has ejectable slots */
478 if (detect_ejectable_slots(handle) > 0) {
479 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
480 add_host_bridge(handle, pci_bus);
481 return 0;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 /* search P2P bridges under this host bridge */
485 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700486 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (ACPI_FAILURE(status))
489 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
490
491 return 0;
492}
493
Rajesh Shah42f49a62005-04-28 00:25:53 -0700494static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
495{
496 struct list_head *head;
497 list_for_each(head, &bridge_list) {
498 struct acpiphp_bridge *bridge = list_entry(head,
499 struct acpiphp_bridge, list);
500 if (bridge->handle == handle)
501 return bridge;
502 }
503
504 return NULL;
505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Rajesh Shah364d5092005-04-28 00:25:54 -0700507static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700509 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700510 struct acpiphp_slot *slot;
511 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700512 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700513
514 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
515 handle_hotplug_event_bridge);
516 if (ACPI_FAILURE(status))
517 err("failed to remove notify handler\n");
518
519 slot = bridge->slots;
520 while (slot) {
521 struct acpiphp_slot *next = slot->next;
522 list_for_each_safe (list, tmp, &slot->funcs) {
523 struct acpiphp_func *func;
524 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800525 if (!(func->flags & FUNC_HAS_DCK)) {
526 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700527 ACPI_SYSTEM_NOTIFY,
528 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800529 if (ACPI_FAILURE(status))
530 err("failed to remove notify handler\n");
531 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700532 pci_dev_put(func->pci_dev);
533 list_del(list);
534 kfree(func);
535 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800536 acpiphp_unregister_hotplug_slot(slot);
537 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700538 kfree(slot);
539 slot = next;
540 }
541
542 pci_dev_put(bridge->pci_dev);
543 list_del(&bridge->list);
544 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Rajesh Shah364d5092005-04-28 00:25:54 -0700547static acpi_status
548cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
549{
550 struct acpiphp_bridge *bridge;
551
552 if (!(bridge = acpiphp_handle_to_bridge(handle)))
553 return AE_OK;
554 cleanup_bridge(bridge);
555 return AE_OK;
556}
557
558static void remove_bridge(acpi_handle handle)
559{
560 struct acpiphp_bridge *bridge;
561
562 bridge = acpiphp_handle_to_bridge(handle);
563 if (bridge) {
564 cleanup_bridge(bridge);
565 } else {
566 /* clean-up p2p bridges under this host bridge */
567 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900568 ACPI_UINT32_MAX, cleanup_p2p_bridge,
569 NULL, NULL);
Rajesh Shah364d5092005-04-28 00:25:54 -0700570 }
571}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700573static struct pci_dev * get_apic_pci_info(acpi_handle handle)
574{
575 struct acpi_pci_id id;
576 struct pci_bus *bus;
577 struct pci_dev *dev;
578
579 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
580 return NULL;
581
582 bus = pci_find_bus(id.segment, id.bus);
583 if (!bus)
584 return NULL;
585
586 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
587 if (!dev)
588 return NULL;
589
590 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
591 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
592 {
593 pci_dev_put(dev);
594 return NULL;
595 }
596
597 return dev;
598}
599
600static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
601{
602 acpi_status status;
603 int result = -1;
604 unsigned long gsb;
605 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
606 union acpi_object *obj;
607 void *table;
608
609 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
610 if (ACPI_SUCCESS(status)) {
611 *gsi_base = (u32)gsb;
612 return 0;
613 }
614
615 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
616 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
617 return -1;
618
619 obj = buffer.pointer;
620 if (obj->type != ACPI_TYPE_BUFFER)
621 goto out;
622
623 table = obj->buffer.pointer;
624 switch (((acpi_table_entry_header *)table)->type) {
625 case ACPI_MADT_IOSAPIC:
626 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
627 result = 0;
628 break;
629 case ACPI_MADT_IOAPIC:
630 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
631 result = 0;
632 break;
633 default:
634 break;
635 }
636 out:
637 acpi_os_free(buffer.pointer);
638 return result;
639}
640
641static acpi_status
642ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
643{
644 acpi_status status;
645 unsigned long sta;
646 acpi_handle tmp;
647 struct pci_dev *pdev;
648 u32 gsi_base;
649 u64 phys_addr;
650
651 /* Evaluate _STA if present */
652 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
653 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
654 return AE_CTRL_DEPTH;
655
656 /* Scan only PCI bus scope */
657 status = acpi_get_handle(handle, "_HID", &tmp);
658 if (ACPI_SUCCESS(status))
659 return AE_CTRL_DEPTH;
660
661 if (get_gsi_base(handle, &gsi_base))
662 return AE_OK;
663
664 pdev = get_apic_pci_info(handle);
665 if (!pdev)
666 return AE_OK;
667
668 if (pci_enable_device(pdev)) {
669 pci_dev_put(pdev);
670 return AE_OK;
671 }
672
673 pci_set_master(pdev);
674
675 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
676 pci_disable_device(pdev);
677 pci_dev_put(pdev);
678 return AE_OK;
679 }
680
681 phys_addr = pci_resource_start(pdev, 0);
682 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
683 pci_release_region(pdev, 0);
684 pci_disable_device(pdev);
685 pci_dev_put(pdev);
686 return AE_OK;
687 }
688
689 return AE_OK;
690}
691
692static int acpiphp_configure_ioapics(acpi_handle handle)
693{
694 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
695 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
696 return 0;
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static int power_on_slot(struct acpiphp_slot *slot)
700{
701 acpi_status status;
702 struct acpiphp_func *func;
703 struct list_head *l;
704 int retval = 0;
705
706 /* if already enabled, just skip */
707 if (slot->flags & SLOT_POWEREDON)
708 goto err_exit;
709
710 list_for_each (l, &slot->funcs) {
711 func = list_entry(l, struct acpiphp_func, sibling);
712
713 if (func->flags & FUNC_HAS_PS0) {
714 dbg("%s: executing _PS0\n", __FUNCTION__);
715 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
716 if (ACPI_FAILURE(status)) {
717 warn("%s: _PS0 failed\n", __FUNCTION__);
718 retval = -1;
719 goto err_exit;
720 } else
721 break;
722 }
723 }
724
725 /* TBD: evaluate _STA to check if the slot is enabled */
726
727 slot->flags |= SLOT_POWEREDON;
728
729 err_exit:
730 return retval;
731}
732
733
734static int power_off_slot(struct acpiphp_slot *slot)
735{
736 acpi_status status;
737 struct acpiphp_func *func;
738 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 int retval = 0;
741
742 /* if already disabled, just skip */
743 if ((slot->flags & SLOT_POWEREDON) == 0)
744 goto err_exit;
745
746 list_for_each (l, &slot->funcs) {
747 func = list_entry(l, struct acpiphp_func, sibling);
748
Rajesh Shah2f523b12005-04-28 00:25:55 -0700749 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
751 if (ACPI_FAILURE(status)) {
752 warn("%s: _PS3 failed\n", __FUNCTION__);
753 retval = -1;
754 goto err_exit;
755 } else
756 break;
757 }
758 }
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* TBD: evaluate _STA to check if the slot is disabled */
761
762 slot->flags &= (~SLOT_POWEREDON);
763
764 err_exit:
765 return retval;
766}
767
768
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800769
770/**
771 * acpiphp_max_busnr - return the highest reserved bus number under
772 * the given bus.
773 * @bus: bus to start search with
774 *
775 */
776static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
777{
778 struct list_head *tmp;
779 unsigned char max, n;
780
781 /*
782 * pci_bus_max_busnr will return the highest
783 * reserved busnr for all these children.
784 * that is equivalent to the bus->subordinate
785 * value. We don't want to use the parent's
786 * bus->subordinate value because it could have
787 * padding in it.
788 */
789 max = bus->secondary;
790
791 list_for_each(tmp, &bus->children) {
792 n = pci_bus_max_busnr(pci_bus_b(tmp));
793 if (n > max)
794 max = n;
795 }
796 return max;
797}
798
799
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800800/**
801 * acpiphp_bus_add - add a new bus to acpi subsystem
802 * @func: acpiphp_func of the bridge
803 *
804 */
805static int acpiphp_bus_add(struct acpiphp_func *func)
806{
807 acpi_handle phandle;
808 struct acpi_device *device, *pdevice;
809 int ret_val;
810
811 acpi_get_parent(func->handle, &phandle);
812 if (acpi_bus_get_device(phandle, &pdevice)) {
813 dbg("no parent device, assuming NULL\n");
814 pdevice = NULL;
815 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800816 if (!acpi_bus_get_device(func->handle, &device)) {
817 dbg("bus exists... trim\n");
818 /* this shouldn't be in here, so remove
819 * the bus then re-add it...
820 */
821 ret_val = acpi_bus_trim(device, 1);
822 dbg("acpi_bus_trim return %x\n", ret_val);
823 }
824
825 ret_val = acpi_bus_add(&device, pdevice, func->handle,
826 ACPI_BUS_TYPE_DEVICE);
827 if (ret_val) {
828 dbg("error adding bus, %x\n",
829 -ret_val);
830 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800831 }
832 /*
833 * try to start anyway. We could have failed to add
834 * simply because this bus had previously been added
835 * on another add. Don't bother with the return value
836 * we just keep going.
837 */
838 ret_val = acpi_bus_start(device);
839
840acpiphp_bus_add_out:
841 return ret_val;
842}
843
844
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900845/**
846 * acpiphp_bus_trim - trim a bus from acpi subsystem
847 * @handle: handle to acpi namespace
848 *
849 */
850int acpiphp_bus_trim(acpi_handle handle)
851{
852 struct acpi_device *device;
853 int retval;
854
855 retval = acpi_bus_get_device(handle, &device);
856 if (retval) {
857 dbg("acpi_device not found\n");
858 return retval;
859 }
860
861 retval = acpi_bus_trim(device, 1);
862 if (retval)
863 err("cannot remove from acpi list\n");
864
865 return retval;
866}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/**
869 * enable_device - enable, configure a slot
870 * @slot: slot to be enabled
871 *
872 * This function should be called per *physical slot*,
873 * not per each slot object in ACPI namespace.
874 *
875 */
876static int enable_device(struct acpiphp_slot *slot)
877{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700879 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct list_head *l;
881 struct acpiphp_func *func;
882 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700883 int num, max, pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (slot->flags & SLOT_ENABLED)
886 goto err_exit;
887
888 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700889 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (dev) {
891 /* This case shouldn't happen */
892 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700893 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 retval = -1;
895 goto err_exit;
896 }
897
Rajesh Shah42f49a62005-04-28 00:25:53 -0700898 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
899 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 err("No new device found\n");
901 retval = -1;
902 goto err_exit;
903 }
904
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800905 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700906 for (pass = 0; pass < 2; pass++) {
907 list_for_each_entry(dev, &bus->devices, bus_list) {
908 if (PCI_SLOT(dev->devfn) != slot->device)
909 continue;
910 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800911 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700912 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900913 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800914 pci_bus_size_bridges(dev->subordinate);
915 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900919 list_for_each (l, &slot->funcs) {
920 func = list_entry(l, struct acpiphp_func, sibling);
921 acpiphp_bus_add(func);
922 }
923
Rajesh Shah42f49a62005-04-28 00:25:53 -0700924 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700925 acpiphp_sanitize_bus(bus);
926 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700927 pci_bus_add_devices(bus);
MUNEDA Takahiro0cccd0c2006-02-24 17:46:04 +0900928 acpiphp_set_hpp_values(slot->bridge->handle, bus);
929 acpiphp_configure_ioapics(slot->bridge->handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* associate pci_dev to our representation */
932 list_for_each (l, &slot->funcs) {
933 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700934 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 func->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
938 slot->flags |= SLOT_ENABLED;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 err_exit:
941 return retval;
942}
943
944
945/**
946 * disable_device - disable a slot
947 */
948static int disable_device(struct acpiphp_slot *slot)
949{
950 int retval = 0;
951 struct acpiphp_func *func;
952 struct list_head *l;
953
954 /* is this slot already disabled? */
955 if (!(slot->flags & SLOT_ENABLED))
956 goto err_exit;
957
958 list_for_each (l, &slot->funcs) {
959 func = list_entry(l, struct acpiphp_func, sibling);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900960
961 acpiphp_bus_trim(func->handle);
962 /* try to remove anyway.
963 * acpiphp_bus_add might have been failed */
964
Rajesh Shah42f49a62005-04-28 00:25:53 -0700965 if (!func->pci_dev)
966 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Rajesh Shah42f49a62005-04-28 00:25:53 -0700968 pci_remove_bus_device(func->pci_dev);
969 pci_dev_put(func->pci_dev);
970 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
973 slot->flags &= (~SLOT_ENABLED);
974
975 err_exit:
976 return retval;
977}
978
979
980/**
981 * get_slot_status - get ACPI slot status
982 *
983 * if a slot has _STA for each function and if any one of them
984 * returned non-zero status, return it
985 *
986 * if a slot doesn't have _STA and if any one of its functions'
987 * configuration space is configured, return 0x0f as a _STA
988 *
989 * otherwise return 0
990 */
991static unsigned int get_slot_status(struct acpiphp_slot *slot)
992{
993 acpi_status status;
994 unsigned long sta = 0;
995 u32 dvid;
996 struct list_head *l;
997 struct acpiphp_func *func;
998
999 list_for_each (l, &slot->funcs) {
1000 func = list_entry(l, struct acpiphp_func, sibling);
1001
1002 if (func->flags & FUNC_HAS_STA) {
1003 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1004 if (ACPI_SUCCESS(status) && sta)
1005 break;
1006 } else {
1007 pci_bus_read_config_dword(slot->bridge->pci_bus,
1008 PCI_DEVFN(slot->device,
1009 func->function),
1010 PCI_VENDOR_ID, &dvid);
1011 if (dvid != 0xffffffff) {
1012 sta = ACPI_STA_ALL;
1013 break;
1014 }
1015 }
1016 }
1017
1018 return (unsigned int)sta;
1019}
1020
1021/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001022 * acpiphp_eject_slot - physically eject the slot
1023 */
1024static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1025{
1026 acpi_status status;
1027 struct acpiphp_func *func;
1028 struct list_head *l;
1029 struct acpi_object_list arg_list;
1030 union acpi_object arg;
1031
1032 list_for_each (l, &slot->funcs) {
1033 func = list_entry(l, struct acpiphp_func, sibling);
1034
1035 /* We don't want to call _EJ0 on non-existing functions. */
1036 if ((func->flags & FUNC_HAS_EJ0)) {
1037 /* _EJ0 method take one argument */
1038 arg_list.count = 1;
1039 arg_list.pointer = &arg;
1040 arg.type = ACPI_TYPE_INTEGER;
1041 arg.integer.value = 1;
1042
1043 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1044 if (ACPI_FAILURE(status)) {
1045 warn("%s: _EJ0 failed\n", __FUNCTION__);
1046 return -1;
1047 } else
1048 break;
1049 }
1050 }
1051 return 0;
1052}
1053
1054/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * acpiphp_check_bridge - re-enumerate devices
1056 *
1057 * Iterate over all slots under this bridge and make sure that if a
1058 * card is present they are enabled, and if not they are disabled.
1059 */
1060static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1061{
1062 struct acpiphp_slot *slot;
1063 int retval = 0;
1064 int enabled, disabled;
1065
1066 enabled = disabled = 0;
1067
1068 for (slot = bridge->slots; slot; slot = slot->next) {
1069 unsigned int status = get_slot_status(slot);
1070 if (slot->flags & SLOT_ENABLED) {
1071 if (status == ACPI_STA_ALL)
1072 continue;
1073 retval = acpiphp_disable_slot(slot);
1074 if (retval) {
1075 err("Error occurred in disabling\n");
1076 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001077 } else {
1078 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 disabled++;
1081 } else {
1082 if (status != ACPI_STA_ALL)
1083 continue;
1084 retval = acpiphp_enable_slot(slot);
1085 if (retval) {
1086 err("Error occurred in enabling\n");
1087 goto err_exit;
1088 }
1089 enabled++;
1090 }
1091 }
1092
1093 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1094
1095 err_exit:
1096 return retval;
1097}
1098
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001099static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1100{
1101 u16 pci_cmd, pci_bctl;
1102 struct pci_dev *cdev;
1103
1104 /* Program hpp values for this device */
1105 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1106 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1107 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1108 return;
1109 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1110 bridge->hpp.cache_line_size);
1111 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1112 bridge->hpp.latency_timer);
1113 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001114 if (bridge->hpp.enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001115 pci_cmd |= PCI_COMMAND_SERR;
1116 else
1117 pci_cmd &= ~PCI_COMMAND_SERR;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001118 if (bridge->hpp.enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001119 pci_cmd |= PCI_COMMAND_PARITY;
1120 else
1121 pci_cmd &= ~PCI_COMMAND_PARITY;
1122 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1123
1124 /* Program bridge control value and child devices */
1125 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1126 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1127 bridge->hpp.latency_timer);
1128 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001129 if (bridge->hpp.enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001130 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1131 else
1132 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001133 if (bridge->hpp.enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001134 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1135 else
1136 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1137 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1138 if (dev->subordinate) {
1139 list_for_each_entry(cdev, &dev->subordinate->devices,
1140 bus_list)
1141 program_hpp(cdev, bridge);
1142 }
1143 }
1144}
1145
1146static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1147{
1148 struct acpiphp_bridge bridge;
1149 struct pci_dev *dev;
1150
1151 memset(&bridge, 0, sizeof(bridge));
1152 bridge.handle = handle;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001153 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001154 decode_hpp(&bridge);
1155 list_for_each_entry(dev, &bus->devices, bus_list)
1156 program_hpp(dev, &bridge);
1157
1158}
1159
1160/*
1161 * Remove devices for which we could not assign resources, call
1162 * arch specific code to fix-up the bus
1163 */
1164static void acpiphp_sanitize_bus(struct pci_bus *bus)
1165{
1166 struct pci_dev *dev;
1167 int i;
1168 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1169
1170 list_for_each_entry(dev, &bus->devices, bus_list) {
1171 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1172 struct resource *res = &dev->resource[i];
1173 if ((res->flags & type_mask) && !res->start &&
1174 res->end) {
1175 /* Could not assign a required resources
1176 * for this device, remove it */
1177 pci_remove_bus_device(dev);
1178 break;
1179 }
1180 }
1181 }
1182}
1183
1184/* Program resources in newly inserted bridge */
1185static int acpiphp_configure_bridge (acpi_handle handle)
1186{
1187 struct acpi_pci_id pci_id;
1188 struct pci_bus *bus;
1189
1190 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1191 err("cannot get PCI domain and bus number for bridge\n");
1192 return -EINVAL;
1193 }
1194 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1195 if (!bus) {
1196 err("cannot find bus %d:%d\n",
1197 pci_id.segment, pci_id.bus);
1198 return -EINVAL;
1199 }
1200
1201 pci_bus_size_bridges(bus);
1202 pci_bus_assign_resources(bus);
1203 acpiphp_sanitize_bus(bus);
1204 acpiphp_set_hpp_values(handle, bus);
1205 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001206 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001207 return 0;
1208}
1209
1210static void handle_bridge_insertion(acpi_handle handle, u32 type)
1211{
1212 struct acpi_device *device, *pdevice;
1213 acpi_handle phandle;
1214
1215 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1216 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1217 err("unexpected notification type %d\n", type);
1218 return;
1219 }
1220
1221 acpi_get_parent(handle, &phandle);
1222 if (acpi_bus_get_device(phandle, &pdevice)) {
1223 dbg("no parent device, assuming NULL\n");
1224 pdevice = NULL;
1225 }
1226 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1227 err("cannot add bridge to acpi list\n");
1228 return;
1229 }
1230 if (!acpiphp_configure_bridge(handle) &&
1231 !acpi_bus_start(device))
1232 add_bridge(handle);
1233 else
1234 err("cannot configure and start bridge\n");
1235
1236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/*
1239 * ACPI event handlers
1240 */
1241
1242/**
1243 * handle_hotplug_event_bridge - handle ACPI event on bridges
1244 *
1245 * @handle: Notify()'ed acpi_handle
1246 * @type: Notify code
1247 * @context: pointer to acpiphp_bridge structure
1248 *
1249 * handles ACPI event notification on {host,p2p} bridges
1250 *
1251 */
1252static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1253{
1254 struct acpiphp_bridge *bridge;
1255 char objname[64];
1256 struct acpi_buffer buffer = { .length = sizeof(objname),
1257 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001258 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001260 if (acpi_bus_get_device(handle, &device)) {
1261 /* This bridge must have just been physically inserted */
1262 handle_bridge_insertion(handle, type);
1263 return;
1264 }
1265
1266 bridge = acpiphp_handle_to_bridge(handle);
1267 if (!bridge) {
1268 err("cannot get bridge info\n");
1269 return;
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1273
1274 switch (type) {
1275 case ACPI_NOTIFY_BUS_CHECK:
1276 /* bus re-enumerate */
1277 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1278 acpiphp_check_bridge(bridge);
1279 break;
1280
1281 case ACPI_NOTIFY_DEVICE_CHECK:
1282 /* device check */
1283 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1284 acpiphp_check_bridge(bridge);
1285 break;
1286
1287 case ACPI_NOTIFY_DEVICE_WAKE:
1288 /* wake event */
1289 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1290 break;
1291
1292 case ACPI_NOTIFY_EJECT_REQUEST:
1293 /* request device eject */
1294 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1295 break;
1296
1297 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1298 printk(KERN_ERR "Device %s cannot be configured due"
1299 " to a frequency mismatch\n", objname);
1300 break;
1301
1302 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1303 printk(KERN_ERR "Device %s cannot be configured due"
1304 " to a bus mode mismatch\n", objname);
1305 break;
1306
1307 case ACPI_NOTIFY_POWER_FAULT:
1308 printk(KERN_ERR "Device %s has suffered a power fault\n",
1309 objname);
1310 break;
1311
1312 default:
1313 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1314 break;
1315 }
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318/**
1319 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1320 *
1321 * @handle: Notify()'ed acpi_handle
1322 * @type: Notify code
1323 * @context: pointer to acpiphp_func structure
1324 *
1325 * handles ACPI event notification on slots
1326 *
1327 */
Kristen Accardi20416ea2006-02-23 17:56:03 -08001328void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 struct acpiphp_func *func;
1331 char objname[64];
1332 struct acpi_buffer buffer = { .length = sizeof(objname),
1333 .pointer = objname };
1334
1335 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1336
1337 func = (struct acpiphp_func *)context;
1338
1339 switch (type) {
1340 case ACPI_NOTIFY_BUS_CHECK:
1341 /* bus re-enumerate */
1342 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1343 acpiphp_enable_slot(func->slot);
1344 break;
1345
1346 case ACPI_NOTIFY_DEVICE_CHECK:
1347 /* device check : re-enumerate from parent bus */
1348 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1349 acpiphp_check_bridge(func->slot->bridge);
1350 break;
1351
1352 case ACPI_NOTIFY_DEVICE_WAKE:
1353 /* wake event */
1354 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1355 break;
1356
1357 case ACPI_NOTIFY_EJECT_REQUEST:
1358 /* request device eject */
1359 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001360 if (!(acpiphp_disable_slot(func->slot)))
1361 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 break;
1363
1364 default:
1365 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1366 break;
1367 }
1368}
1369
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001370
1371static acpi_status
1372find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1373{
1374 int *count = (int *)context;
1375
Kristen Accardi783c49f2006-03-03 10:16:05 -08001376 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001377 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1378 handle_hotplug_event_bridge, NULL);
1379 (*count)++;
1380 }
1381 return AE_OK ;
1382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384static struct acpi_pci_driver acpi_pci_hp_driver = {
1385 .add = add_bridge,
1386 .remove = remove_bridge,
1387};
1388
1389/**
1390 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1391 *
1392 */
1393int __init acpiphp_glue_init(void)
1394{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001395 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001397 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1398 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 if (num <= 0)
1401 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001402 else
1403 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return 0;
1406}
1407
1408
1409/**
1410 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1411 *
1412 * This function frees all data allocated in acpiphp_glue_init()
1413 */
1414void __exit acpiphp_glue_exit(void)
1415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1417}
1418
1419
1420/**
1421 * acpiphp_get_num_slots - count number of slots in a system
1422 */
1423int __init acpiphp_get_num_slots(void)
1424{
1425 struct list_head *node;
1426 struct acpiphp_bridge *bridge;
1427 int num_slots;
1428
1429 num_slots = 0;
1430
1431 list_for_each (node, &bridge_list) {
1432 bridge = (struct acpiphp_bridge *)node;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001433 dbg("Bus %04x:%02x has %d slot%s\n",
1434 pci_domain_nr(bridge->pci_bus),
1435 bridge->pci_bus->number, bridge->nr_slots,
1436 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 num_slots += bridge->nr_slots;
1438 }
1439
Rajesh Shah42f49a62005-04-28 00:25:53 -07001440 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return num_slots;
1442}
1443
1444
1445#if 0
1446/**
1447 * acpiphp_for_each_slot - call function for each slot
1448 * @fn: callback function
1449 * @data: context to be passed to callback function
1450 *
1451 */
1452static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1453{
1454 struct list_head *node;
1455 struct acpiphp_bridge *bridge;
1456 struct acpiphp_slot *slot;
1457 int retval = 0;
1458
1459 list_for_each (node, &bridge_list) {
1460 bridge = (struct acpiphp_bridge *)node;
1461 for (slot = bridge->slots; slot; slot = slot->next) {
1462 retval = fn(slot, data);
1463 if (!retval)
1464 goto err_exit;
1465 }
1466 }
1467
1468 err_exit:
1469 return retval;
1470}
1471#endif
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474/**
1475 * acpiphp_enable_slot - power on slot
1476 */
1477int acpiphp_enable_slot(struct acpiphp_slot *slot)
1478{
1479 int retval;
1480
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001481 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 /* wake up all functions */
1484 retval = power_on_slot(slot);
1485 if (retval)
1486 goto err_exit;
1487
1488 if (get_slot_status(slot) == ACPI_STA_ALL)
1489 /* configure all functions */
1490 retval = enable_device(slot);
1491
1492 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001493 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return retval;
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497/**
1498 * acpiphp_disable_slot - power off slot
1499 */
1500int acpiphp_disable_slot(struct acpiphp_slot *slot)
1501{
1502 int retval = 0;
1503
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001504 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 /* unconfigure all functions */
1507 retval = disable_device(slot);
1508 if (retval)
1509 goto err_exit;
1510
1511 /* power off all functions */
1512 retval = power_off_slot(slot);
1513 if (retval)
1514 goto err_exit;
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001517 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return retval;
1519}
1520
1521
1522/*
1523 * slot enabled: 1
1524 * slot disabled: 0
1525 */
1526u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1527{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001528 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531
1532/*
1533 * latch closed: 1
1534 * latch open: 0
1535 */
1536u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1537{
1538 unsigned int sta;
1539
1540 sta = get_slot_status(slot);
1541
1542 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1543}
1544
1545
1546/*
1547 * adapter presence : 1
1548 * absence : 0
1549 */
1550u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1551{
1552 unsigned int sta;
1553
1554 sta = get_slot_status(slot);
1555
1556 return (sta == 0) ? 0 : 1;
1557}
1558
1559
1560/*
1561 * pci address (seg/bus/dev)
1562 */
1563u32 acpiphp_get_address(struct acpiphp_slot *slot)
1564{
1565 u32 address;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001566 struct pci_bus *pci_bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Rajesh Shah42f49a62005-04-28 00:25:53 -07001568 address = (pci_domain_nr(pci_bus) << 16) |
1569 (pci_bus->number << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 slot->device;
1571
1572 return address;
1573}