blob: a33794d9e0dce60d3ae57948122e1e53efa57aa5 [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 *
Kristen Carlson Accardi998be202006-07-26 10:52:33 -070029 * Send feedback to <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
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.
Alex Chiang5d4a4b22009-03-30 10:50:14 -060041 * - When a P2P bridge is present, we elevate the refcount on the subordinate
42 * bus. It loses the refcount when the the driver unloads.
Rajesh Shah42f49a62005-04-28 00:25:53 -070043 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/init.h>
46#include <linux/module.h>
47
48#include <linux/kernel.h>
49#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070050#include <linux/pci_hotplug.h>
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +090051#include <linux/pci-acpi.h>
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +010052#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "../pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include "acpiphp.h"
56
57static LIST_HEAD(bridge_list);
Satoru Takeuchi600812e2006-09-12 10:22:53 -070058static LIST_HEAD(ioapic_list);
59static DEFINE_SPINLOCK(ioapic_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define MY_NAME "acpiphp_glue"
62
63static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070064static void acpiphp_sanitize_bus(struct pci_bus *bus);
65static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
Len Brown2b85e132006-06-27 01:50:14 -040066static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070067
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +090068/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -040069static acpi_status
70is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
71{
72 int *count = (int *)context;
73
74 if (is_dock_device(handle)) {
75 (*count)++;
76 return AE_CTRL_TERMINATE;
77 } else {
78 return AE_OK;
79 }
80}
81
Kristen Accardi4e8662b2006-06-28 03:08:06 -040082/*
83 * the _DCK method can do funny things... and sometimes not
84 * hah-hah funny.
85 *
86 * TBD - figure out a way to only call fixups for
87 * systems that require them.
88 */
89static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
90 void *v)
91{
92 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
93 struct pci_bus *bus = func->slot->bridge->pci_bus;
94 u32 buses;
95
96 if (!bus->self)
97 return NOTIFY_OK;
98
99 /* fixup bad _DCK function that rewrites
100 * secondary bridge on slot
101 */
102 pci_read_config_dword(bus->self,
103 PCI_PRIMARY_BUS,
104 &buses);
105
106 if (((buses >> 8) & 0xff) != bus->secondary) {
107 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700108 | ((unsigned int)(bus->primary) << 0)
109 | ((unsigned int)(bus->secondary) << 8)
110 | ((unsigned int)(bus->subordinate) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400111 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
112 }
113 return NOTIFY_OK;
114}
115
116
Shaohua Li1253f7a2008-08-28 10:06:16 +0800117static struct acpi_dock_ops acpiphp_dock_ops = {
118 .handler = handle_hotplug_event_func,
119};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/* callback routine to register each ACPI PCI slot object */
122static acpi_status
123register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
124{
125 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
126 struct acpiphp_slot *slot;
127 struct acpiphp_func *newfunc;
128 acpi_handle tmp;
129 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400130 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800131 int device, function, retval;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900132 struct pci_bus *pbus = bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900134 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return AE_OK;
136
Matthew Garrett56ee3252008-11-25 21:48:14 +0000137 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 device = (adr >> 16) & 0xffff;
139 function = adr & 0xffff;
140
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100141 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!newfunc)
143 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 INIT_LIST_HEAD(&newfunc->sibling);
146 newfunc->handle = handle;
147 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000148
149 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800150 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
153 newfunc->flags |= FUNC_HAS_STA;
154
155 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
156 newfunc->flags |= FUNC_HAS_PS0;
157
158 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
159 newfunc->flags |= FUNC_HAS_PS3;
160
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400161 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800162 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400165 if (ACPI_FAILURE(status)) {
166 /*
167 * use the count of the number of slots we've found
168 * for the number of the slot
169 */
170 sun = bridge->nr_slots+1;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* search for objects that share the same slot */
174 for (slot = bridge->slots; slot; slot = slot->next)
175 if (slot->device == device) {
176 if (slot->sun != sun)
177 warn("sibling found, but _SUN doesn't match!\n");
178 break;
179 }
180
181 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100182 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!slot) {
184 kfree(newfunc);
185 return AE_NO_MEMORY;
186 }
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 slot->device = device;
190 slot->sun = sun;
191 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100192 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 slot->next = bridge->slots;
195 bridge->slots = slot;
196
197 bridge->nr_slots++;
198
Justin Chenb6adc192008-12-11 11:16:44 -0700199 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900200 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800201 retval = acpiphp_register_hotplug_slot(slot);
202 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600203 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700204 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600205 "hotplug driver\n", slot->sun);
206 else
207 warn("acpiphp_register_hotplug_slot failed "
208 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800209 goto err_exit;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
213 newfunc->slot = slot;
214 list_add_tail(&newfunc->sibling, &slot->funcs);
215
216 /* associate corresponding pci_dev */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900217 newfunc->pci_dev = pci_get_slot(pbus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
220 }
221
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400222 if (is_dock_device(handle)) {
223 /* we don't want to call this device's _EJ0
224 * because we want the dock notify handler
225 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800226 */
227 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400228 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800229 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400230 dbg("failed to register dock device\n");
231
232 /* we need to be notified when dock events happen
233 * outside of the hotplug operation, since we may
234 * need to do fixups before we can hotplug.
235 */
236 newfunc->nb.notifier_call = post_dock_fixups;
237 if (register_dock_notifier(&newfunc->nb))
238 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800239 }
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800242 if (!(newfunc->flags & FUNC_HAS_DCK)) {
243 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 ACPI_SYSTEM_NOTIFY,
245 handle_hotplug_event_func,
246 newfunc);
247
Kristen Accardi20416ea2006-02-23 17:56:03 -0800248 if (ACPI_FAILURE(status))
249 err("failed to register interrupt notify handler\n");
250 } else
251 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Kristen Accardi20416ea2006-02-23 17:56:03 -0800253 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800254
255 err_exit:
256 bridge->nr_slots--;
257 bridge->slots = slot->next;
258 kfree(slot);
259 kfree(newfunc);
260
261 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264
265/* see if it's worth looking at this bridge */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900266static int detect_ejectable_slots(struct pci_bus *pbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900268 int found = acpi_pci_detect_ejectable(pbus);
269 if (!found) {
270 acpi_handle bridge_handle = acpi_pci_get_bridge_handle(pbus);
James Bottomleyd45e0852009-01-15 15:12:27 -0500271 if (!bridge_handle)
272 return 0;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900273 acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
274 is_pci_dock_device, (void *)&found, NULL);
275 }
276 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/* decode ACPI 2.0 _HPP hot plug parameters */
281static void decode_hpp(struct acpiphp_bridge *bridge)
282{
283 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900285 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900286 if (ACPI_FAILURE(status) ||
287 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800288 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900289 printk(KERN_WARNING
290 "%s: Could not get hotplug parameters. Use defaults\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800291 __func__);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900292 bridge->hpp.t0 = &bridge->hpp.type0_data;
293 bridge->hpp.t0->revision = 0;
294 bridge->hpp.t0->cache_line_size = 0x10;
295 bridge->hpp.t0->latency_timer = 0x40;
296 bridge->hpp.t0->enable_serr = 0;
297 bridge->hpp.t0->enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301
Kristen Accardi783c49f2006-03-03 10:16:05 -0800302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
304static void init_bridge_misc(struct acpiphp_bridge *bridge)
305{
306 acpi_status status;
307
308 /* decode ACPI 2.0 _HPP (hot plug parameters) */
309 decode_hpp(bridge);
310
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800311 /* must be added to the list prior to calling register_slot */
312 list_add(&bridge->list, &bridge_list);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* register all slot objects under this bridge */
315 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
316 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800317 if (ACPI_FAILURE(status)) {
318 list_del(&bridge->list);
319 return;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700323 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900324 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
325 status = acpi_remove_notify_handler(bridge->func->handle,
326 ACPI_SYSTEM_NOTIFY,
327 handle_hotplug_event_func);
328 if (ACPI_FAILURE(status))
329 err("failed to remove notify handler\n");
330 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700331 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ACPI_SYSTEM_NOTIFY,
333 handle_hotplug_event_bridge,
334 bridge);
335
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700336 if (ACPI_FAILURE(status)) {
337 err("failed to register interrupt notify handler\n");
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900343/* find acpiphp_func from acpiphp_bridge */
344static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
345{
346 struct list_head *node, *l;
347 struct acpiphp_bridge *bridge;
348 struct acpiphp_slot *slot;
349 struct acpiphp_func *func;
350
351 list_for_each(node, &bridge_list) {
352 bridge = list_entry(node, struct acpiphp_bridge, list);
353 for (slot = bridge->slots; slot; slot = slot->next) {
354 list_for_each(l, &slot->funcs) {
355 func = list_entry(l, struct acpiphp_func,
356 sibling);
357 if (func->handle == handle)
358 return func;
359 }
360 }
361 }
362
363 return NULL;
364}
365
366
367static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
368{
369 acpi_handle dummy_handle;
370
371 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
372 "_STA", &dummy_handle)))
373 bridge->flags |= BRIDGE_HAS_STA;
374
375 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
376 "_EJ0", &dummy_handle)))
377 bridge->flags |= BRIDGE_HAS_EJ0;
378
379 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
380 "_PS0", &dummy_handle)))
381 bridge->flags |= BRIDGE_HAS_PS0;
382
383 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
384 "_PS3", &dummy_handle)))
385 bridge->flags |= BRIDGE_HAS_PS3;
386
387 /* is this ejectable p2p bridge? */
388 if (bridge->flags & BRIDGE_HAS_EJ0) {
389 struct acpiphp_func *func;
390
391 dbg("found ejectable p2p bridge\n");
392
393 /* make link between PCI bridge and PCI function */
394 func = acpiphp_bridge_handle_to_function(bridge->handle);
395 if (!func)
396 return;
397 bridge->func = func;
398 func->bridge = bridge;
399 }
400}
401
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700404static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct acpiphp_bridge *bridge;
407
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100408 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (bridge == NULL)
410 return;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 bridge->type = BRIDGE_TYPE_HOST;
413 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Rajesh Shah42f49a62005-04-28 00:25:53 -0700415 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 spin_lock_init(&bridge->res_lock);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 init_bridge_misc(bridge);
420}
421
422
423/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700424static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100428 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (bridge == NULL) {
430 err("out of memory\n");
431 return;
432 }
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 bridge->type = BRIDGE_TYPE_P2P;
435 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900436 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Rajesh Shah42f49a62005-04-28 00:25:53 -0700438 bridge->pci_dev = pci_dev_get(pci_dev);
439 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!bridge->pci_bus) {
441 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700442 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600445 /*
446 * Grab a ref to the subordinate PCI bus in case the bus is
447 * removed via PCI core logical hotplug. The ref pins the bus
448 * (which we access during module unload).
449 */
450 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 spin_lock_init(&bridge->res_lock);
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700454 return;
455 err:
456 pci_dev_put(pci_dev);
457 kfree(bridge);
458 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461
462/* callback routine to find P2P bridges */
463static acpi_status
464find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
465{
466 acpi_status status;
467 acpi_handle dummy_handle;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400468 unsigned long long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700469 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700471 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
474 if (ACPI_FAILURE(status))
475 return AE_OK; /* continue */
476
477 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
478 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800479 dbg("%s: _ADR evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return AE_OK;
481 }
482
483 device = (tmp >> 16) & 0xffff;
484 function = tmp & 0xffff;
485
Rajesh Shah42f49a62005-04-28 00:25:53 -0700486 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Rajesh Shah42f49a62005-04-28 00:25:53 -0700488 if (!dev || !dev->subordinate)
489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* check if this bridge has ejectable slots */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900492 if ((detect_ejectable_slots(dev->subordinate) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700494 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900497 /* search P2P bridges under this p2p bridge */
498 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
499 find_p2p_bridge, dev->subordinate, NULL);
500 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900501 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900502
Rajesh Shah42f49a62005-04-28 00:25:53 -0700503 out:
504 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return AE_OK;
506}
507
508
509/* find hot-pluggable slots, and then find P2P bridge */
510static int add_bridge(acpi_handle handle)
511{
512 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400513 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int seg, bus;
515 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700516 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* if the bridge doesn't have _STA, we assume it is always there */
519 status = acpi_get_handle(handle, "_STA", &dummy_handle);
520 if (ACPI_SUCCESS(status)) {
521 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
522 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800523 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return 0;
525 }
526 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
527 /* don't register this object */
528 return 0;
529 }
530
531 /* get PCI segment number */
532 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
533
534 seg = ACPI_SUCCESS(status) ? tmp : 0;
535
536 /* get PCI bus number */
537 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
538
539 if (ACPI_SUCCESS(status)) {
540 bus = tmp;
541 } else {
542 warn("can't get bus number, assuming 0\n");
543 bus = 0;
544 }
545
Rajesh Shah42f49a62005-04-28 00:25:53 -0700546 pci_bus = pci_find_bus(seg, bus);
547 if (!pci_bus) {
548 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0;
550 }
551
Rajesh Shah42f49a62005-04-28 00:25:53 -0700552 /* check if this bridge has ejectable slots */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900553 if (detect_ejectable_slots(pci_bus) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700554 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
555 add_host_bridge(handle, pci_bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* search P2P bridges under this host bridge */
559 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700560 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900563 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 return 0;
566}
567
Rajesh Shah42f49a62005-04-28 00:25:53 -0700568static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
569{
570 struct list_head *head;
571 list_for_each(head, &bridge_list) {
572 struct acpiphp_bridge *bridge = list_entry(head,
573 struct acpiphp_bridge, list);
574 if (bridge->handle == handle)
575 return bridge;
576 }
577
578 return NULL;
579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Rajesh Shah364d5092005-04-28 00:25:54 -0700581static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700583 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700584 struct acpiphp_slot *slot;
585 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700586 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700587
588 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
589 handle_hotplug_event_bridge);
590 if (ACPI_FAILURE(status))
591 err("failed to remove notify handler\n");
592
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900593 if ((bridge->type != BRIDGE_TYPE_HOST) &&
594 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
595 status = acpi_install_notify_handler(bridge->func->handle,
596 ACPI_SYSTEM_NOTIFY,
597 handle_hotplug_event_func,
598 bridge->func);
599 if (ACPI_FAILURE(status))
600 err("failed to install interrupt notify handler\n");
601 }
602
Rajesh Shah42f49a62005-04-28 00:25:53 -0700603 slot = bridge->slots;
604 while (slot) {
605 struct acpiphp_slot *next = slot->next;
606 list_for_each_safe (list, tmp, &slot->funcs) {
607 struct acpiphp_func *func;
608 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400609 if (is_dock_device(func->handle)) {
610 unregister_hotplug_dock_device(func->handle);
611 unregister_dock_notifier(&func->nb);
612 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800613 if (!(func->flags & FUNC_HAS_DCK)) {
614 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700615 ACPI_SYSTEM_NOTIFY,
616 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800617 if (ACPI_FAILURE(status))
618 err("failed to remove notify handler\n");
619 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700620 pci_dev_put(func->pci_dev);
621 list_del(list);
622 kfree(func);
623 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800624 acpiphp_unregister_hotplug_slot(slot);
625 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700626 kfree(slot);
627 slot = next;
628 }
629
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600630 /*
631 * Only P2P bridges have a pci_dev
632 */
633 if (bridge->pci_dev)
634 put_device(&bridge->pci_bus->dev);
635
Rajesh Shah42f49a62005-04-28 00:25:53 -0700636 pci_dev_put(bridge->pci_dev);
637 list_del(&bridge->list);
638 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Rajesh Shah364d5092005-04-28 00:25:54 -0700641static acpi_status
642cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
643{
644 struct acpiphp_bridge *bridge;
645
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900646 /* cleanup p2p bridges under this P2P bridge
647 in a depth-first manner */
648 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
649 cleanup_p2p_bridge, NULL, NULL);
650
Alex Chianga13307c2008-07-01 20:02:23 -0600651 bridge = acpiphp_handle_to_bridge(handle);
652 if (bridge)
653 cleanup_bridge(bridge);
654
Rajesh Shah364d5092005-04-28 00:25:54 -0700655 return AE_OK;
656}
657
658static void remove_bridge(acpi_handle handle)
659{
660 struct acpiphp_bridge *bridge;
661
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900662 /* cleanup p2p bridges under this host bridge
663 in a depth-first manner */
664 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
665 (u32)1, cleanup_p2p_bridge, NULL, NULL);
666
Alex Chianga13307c2008-07-01 20:02:23 -0600667 /*
668 * On root bridges with hotplug slots directly underneath (ie,
669 * no p2p bridge inbetween), we call cleanup_bridge().
670 *
671 * The else clause cleans up root bridges that either had no
672 * hotplug slots at all, or had a p2p bridge underneath.
673 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700674 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900675 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700676 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600677 else
678 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
679 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700680}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700682static struct pci_dev * get_apic_pci_info(acpi_handle handle)
683{
684 struct acpi_pci_id id;
685 struct pci_bus *bus;
686 struct pci_dev *dev;
687
688 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
689 return NULL;
690
691 bus = pci_find_bus(id.segment, id.bus);
692 if (!bus)
693 return NULL;
694
695 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
696 if (!dev)
697 return NULL;
698
699 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
700 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
701 {
702 pci_dev_put(dev);
703 return NULL;
704 }
705
706 return dev;
707}
708
709static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
710{
711 acpi_status status;
712 int result = -1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400713 unsigned long long gsb;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700714 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
715 union acpi_object *obj;
716 void *table;
717
718 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
719 if (ACPI_SUCCESS(status)) {
720 *gsi_base = (u32)gsb;
721 return 0;
722 }
723
724 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
725 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
726 return -1;
727
728 obj = buffer.pointer;
729 if (obj->type != ACPI_TYPE_BUFFER)
730 goto out;
731
732 table = obj->buffer.pointer;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300733 switch (((struct acpi_subtable_header *)table)->type) {
734 case ACPI_MADT_TYPE_IO_SAPIC:
735 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700736 result = 0;
737 break;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300738 case ACPI_MADT_TYPE_IO_APIC:
739 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700740 result = 0;
741 break;
742 default:
743 break;
744 }
745 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700746 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700747 return result;
748}
749
750static acpi_status
751ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
752{
753 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400754 unsigned long long sta;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700755 acpi_handle tmp;
756 struct pci_dev *pdev;
757 u32 gsi_base;
758 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700759 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700760
761 /* Evaluate _STA if present */
762 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
763 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
764 return AE_CTRL_DEPTH;
765
766 /* Scan only PCI bus scope */
767 status = acpi_get_handle(handle, "_HID", &tmp);
768 if (ACPI_SUCCESS(status))
769 return AE_CTRL_DEPTH;
770
771 if (get_gsi_base(handle, &gsi_base))
772 return AE_OK;
773
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700774 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
775 if (!ioapic)
776 return AE_NO_MEMORY;
777
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700778 pdev = get_apic_pci_info(handle);
779 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700780 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700781
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700782 if (pci_enable_device(pdev))
783 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700784
785 pci_set_master(pdev);
786
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700787 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
788 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700789
790 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700791 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
792 goto exit_pci_release_region;
793
794 ioapic->gsi_base = gsi_base;
795 ioapic->dev = pdev;
796 spin_lock(&ioapic_list_lock);
797 list_add_tail(&ioapic->list, &ioapic_list);
798 spin_unlock(&ioapic_list_lock);
799
800 return AE_OK;
801
802 exit_pci_release_region:
803 pci_release_region(pdev, 0);
804 exit_pci_disable_device:
805 pci_disable_device(pdev);
806 exit_pci_dev_put:
807 pci_dev_put(pdev);
808 exit_kfree:
809 kfree(ioapic);
810
811 return AE_OK;
812}
813
814static acpi_status
815ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
816{
817 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400818 unsigned long long sta;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700819 acpi_handle tmp;
820 u32 gsi_base;
821 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
822
823 /* Evaluate _STA if present */
824 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
825 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
826 return AE_CTRL_DEPTH;
827
828 /* Scan only PCI bus scope */
829 status = acpi_get_handle(handle, "_HID", &tmp);
830 if (ACPI_SUCCESS(status))
831 return AE_CTRL_DEPTH;
832
833 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700834 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700835
836 acpi_unregister_ioapic(handle, gsi_base);
837
838 spin_lock(&ioapic_list_lock);
839 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
840 if (pos->gsi_base != gsi_base)
841 continue;
842 ioapic = pos;
843 list_del(&ioapic->list);
844 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700845 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700846 spin_unlock(&ioapic_list_lock);
847
848 if (!ioapic)
849 return AE_OK;
850
851 pci_release_region(ioapic->dev, 0);
852 pci_disable_device(ioapic->dev);
853 pci_dev_put(ioapic->dev);
854 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700855
856 return AE_OK;
857}
858
859static int acpiphp_configure_ioapics(acpi_handle handle)
860{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700861 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700862 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
863 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
864 return 0;
865}
866
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700867static int acpiphp_unconfigure_ioapics(acpi_handle handle)
868{
869 ioapic_remove(handle, 0, NULL, NULL);
870 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
871 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
872 return 0;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875static int power_on_slot(struct acpiphp_slot *slot)
876{
877 acpi_status status;
878 struct acpiphp_func *func;
879 struct list_head *l;
880 int retval = 0;
881
882 /* if already enabled, just skip */
883 if (slot->flags & SLOT_POWEREDON)
884 goto err_exit;
885
886 list_for_each (l, &slot->funcs) {
887 func = list_entry(l, struct acpiphp_func, sibling);
888
889 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800890 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
892 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800893 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 retval = -1;
895 goto err_exit;
896 } else
897 break;
898 }
899 }
900
901 /* TBD: evaluate _STA to check if the slot is enabled */
902
903 slot->flags |= SLOT_POWEREDON;
904
905 err_exit:
906 return retval;
907}
908
909
910static int power_off_slot(struct acpiphp_slot *slot)
911{
912 acpi_status status;
913 struct acpiphp_func *func;
914 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 int retval = 0;
917
918 /* if already disabled, just skip */
919 if ((slot->flags & SLOT_POWEREDON) == 0)
920 goto err_exit;
921
922 list_for_each (l, &slot->funcs) {
923 func = list_entry(l, struct acpiphp_func, sibling);
924
Rajesh Shah2f523b12005-04-28 00:25:55 -0700925 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
927 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800928 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 retval = -1;
930 goto err_exit;
931 } else
932 break;
933 }
934 }
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /* TBD: evaluate _STA to check if the slot is disabled */
937
938 slot->flags &= (~SLOT_POWEREDON);
939
940 err_exit:
941 return retval;
942}
943
944
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800945
946/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800947 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800948 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800949 */
950static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
951{
952 struct list_head *tmp;
953 unsigned char max, n;
954
955 /*
956 * pci_bus_max_busnr will return the highest
957 * reserved busnr for all these children.
958 * that is equivalent to the bus->subordinate
959 * value. We don't want to use the parent's
960 * bus->subordinate value because it could have
961 * padding in it.
962 */
963 max = bus->secondary;
964
965 list_for_each(tmp, &bus->children) {
966 n = pci_bus_max_busnr(pci_bus_b(tmp));
967 if (n > max)
968 max = n;
969 }
970 return max;
971}
972
973
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800974/**
975 * acpiphp_bus_add - add a new bus to acpi subsystem
976 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800977 */
978static int acpiphp_bus_add(struct acpiphp_func *func)
979{
980 acpi_handle phandle;
981 struct acpi_device *device, *pdevice;
982 int ret_val;
983
984 acpi_get_parent(func->handle, &phandle);
985 if (acpi_bus_get_device(phandle, &pdevice)) {
986 dbg("no parent device, assuming NULL\n");
987 pdevice = NULL;
988 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800989 if (!acpi_bus_get_device(func->handle, &device)) {
990 dbg("bus exists... trim\n");
991 /* this shouldn't be in here, so remove
992 * the bus then re-add it...
993 */
994 ret_val = acpi_bus_trim(device, 1);
995 dbg("acpi_bus_trim return %x\n", ret_val);
996 }
997
998 ret_val = acpi_bus_add(&device, pdevice, func->handle,
999 ACPI_BUS_TYPE_DEVICE);
1000 if (ret_val) {
1001 dbg("error adding bus, %x\n",
1002 -ret_val);
1003 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001004 }
1005 /*
1006 * try to start anyway. We could have failed to add
1007 * simply because this bus had previously been added
1008 * on another add. Don't bother with the return value
1009 * we just keep going.
1010 */
1011 ret_val = acpi_bus_start(device);
1012
1013acpiphp_bus_add_out:
1014 return ret_val;
1015}
1016
1017
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001018/**
1019 * acpiphp_bus_trim - trim a bus from acpi subsystem
1020 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001021 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -07001022static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001023{
1024 struct acpi_device *device;
1025 int retval;
1026
1027 retval = acpi_bus_get_device(handle, &device);
1028 if (retval) {
1029 dbg("acpi_device not found\n");
1030 return retval;
1031 }
1032
1033 retval = acpi_bus_trim(device, 1);
1034 if (retval)
1035 err("cannot remove from acpi list\n");
1036
1037 return retval;
1038}
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/**
1041 * enable_device - enable, configure a slot
1042 * @slot: slot to be enabled
1043 *
1044 * This function should be called per *physical slot*,
1045 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +01001047static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001050 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct list_head *l;
1052 struct acpiphp_func *func;
1053 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001054 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001055 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (slot->flags & SLOT_ENABLED)
1058 goto err_exit;
1059
1060 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001061 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (dev) {
1063 /* This case shouldn't happen */
1064 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001065 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 retval = -1;
1067 goto err_exit;
1068 }
1069
Rajesh Shah42f49a62005-04-28 00:25:53 -07001070 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1071 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 err("No new device found\n");
1073 retval = -1;
1074 goto err_exit;
1075 }
1076
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001077 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001078 for (pass = 0; pass < 2; pass++) {
1079 list_for_each_entry(dev, &bus->devices, bus_list) {
1080 if (PCI_SLOT(dev->devfn) != slot->device)
1081 continue;
1082 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001083 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001084 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001085 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001086 pci_bus_size_bridges(dev->subordinate);
1087 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001091 list_for_each (l, &slot->funcs) {
1092 func = list_entry(l, struct acpiphp_func, sibling);
1093 acpiphp_bus_add(func);
1094 }
1095
Rajesh Shah42f49a62005-04-28 00:25:53 -07001096 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001097 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001098 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001099 list_for_each_entry(func, &slot->funcs, sibling)
1100 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001101 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001102 pci_bus_add_devices(bus);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* associate pci_dev to our representation */
1105 list_for_each (l, &slot->funcs) {
1106 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001107 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 func->function));
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001109 if (!func->pci_dev)
1110 continue;
1111
1112 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1113 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1114 continue;
1115
1116 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1117 if (ACPI_FAILURE(status))
1118 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1119 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
1122 slot->flags |= SLOT_ENABLED;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 err_exit:
1125 return retval;
1126}
1127
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001128static void disable_bridges(struct pci_bus *bus)
1129{
1130 struct pci_dev *dev;
1131 list_for_each_entry(dev, &bus->devices, bus_list) {
1132 if (dev->subordinate) {
1133 disable_bridges(dev->subordinate);
1134 pci_disable_device(dev);
1135 }
1136 }
1137}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139/**
1140 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001141 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
1143static int disable_device(struct acpiphp_slot *slot)
1144{
1145 int retval = 0;
1146 struct acpiphp_func *func;
1147 struct list_head *l;
1148
1149 /* is this slot already disabled? */
1150 if (!(slot->flags & SLOT_ENABLED))
1151 goto err_exit;
1152
1153 list_for_each (l, &slot->funcs) {
1154 func = list_entry(l, struct acpiphp_func, sibling);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001155
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001156 if (func->bridge) {
1157 /* cleanup p2p bridges under this P2P bridge */
1158 cleanup_p2p_bridge(func->bridge->handle,
1159 (u32)1, NULL, NULL);
1160 func->bridge = NULL;
1161 }
1162
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001163 if (func->pci_dev) {
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001164 pci_stop_bus_device(func->pci_dev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001165 if (func->pci_dev->subordinate) {
1166 disable_bridges(func->pci_dev->subordinate);
1167 pci_disable_device(func->pci_dev);
1168 }
1169 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001170 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001171
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001172 list_for_each (l, &slot->funcs) {
1173 func = list_entry(l, struct acpiphp_func, sibling);
1174
1175 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001176 acpiphp_bus_trim(func->handle);
1177 /* try to remove anyway.
1178 * acpiphp_bus_add might have been failed */
1179
Rajesh Shah42f49a62005-04-28 00:25:53 -07001180 if (!func->pci_dev)
1181 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Rajesh Shah42f49a62005-04-28 00:25:53 -07001183 pci_remove_bus_device(func->pci_dev);
1184 pci_dev_put(func->pci_dev);
1185 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
1188 slot->flags &= (~SLOT_ENABLED);
1189
1190 err_exit:
1191 return retval;
1192}
1193
1194
1195/**
1196 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -08001197 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001199 * If a slot has _STA for each function and if any one of them
1200 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001202 * If a slot doesn't have _STA and if any one of its functions'
1203 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001205 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
1207static unsigned int get_slot_status(struct acpiphp_slot *slot)
1208{
1209 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001210 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 u32 dvid;
1212 struct list_head *l;
1213 struct acpiphp_func *func;
1214
1215 list_for_each (l, &slot->funcs) {
1216 func = list_entry(l, struct acpiphp_func, sibling);
1217
1218 if (func->flags & FUNC_HAS_STA) {
1219 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1220 if (ACPI_SUCCESS(status) && sta)
1221 break;
1222 } else {
1223 pci_bus_read_config_dword(slot->bridge->pci_bus,
1224 PCI_DEVFN(slot->device,
1225 func->function),
1226 PCI_VENDOR_ID, &dvid);
1227 if (dvid != 0xffffffff) {
1228 sta = ACPI_STA_ALL;
1229 break;
1230 }
1231 }
1232 }
1233
1234 return (unsigned int)sta;
1235}
1236
1237/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001238 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001239 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001240 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001241int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001242{
1243 acpi_status status;
1244 struct acpiphp_func *func;
1245 struct list_head *l;
1246 struct acpi_object_list arg_list;
1247 union acpi_object arg;
1248
1249 list_for_each (l, &slot->funcs) {
1250 func = list_entry(l, struct acpiphp_func, sibling);
1251
1252 /* We don't want to call _EJ0 on non-existing functions. */
1253 if ((func->flags & FUNC_HAS_EJ0)) {
1254 /* _EJ0 method take one argument */
1255 arg_list.count = 1;
1256 arg_list.pointer = &arg;
1257 arg.type = ACPI_TYPE_INTEGER;
1258 arg.integer.value = 1;
1259
1260 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1261 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001262 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001263 return -1;
1264 } else
1265 break;
1266 }
1267 }
1268 return 0;
1269}
1270
1271/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001273 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 *
1275 * Iterate over all slots under this bridge and make sure that if a
1276 * card is present they are enabled, and if not they are disabled.
1277 */
1278static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1279{
1280 struct acpiphp_slot *slot;
1281 int retval = 0;
1282 int enabled, disabled;
1283
1284 enabled = disabled = 0;
1285
1286 for (slot = bridge->slots; slot; slot = slot->next) {
1287 unsigned int status = get_slot_status(slot);
1288 if (slot->flags & SLOT_ENABLED) {
1289 if (status == ACPI_STA_ALL)
1290 continue;
1291 retval = acpiphp_disable_slot(slot);
1292 if (retval) {
1293 err("Error occurred in disabling\n");
1294 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001295 } else {
1296 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 disabled++;
1299 } else {
1300 if (status != ACPI_STA_ALL)
1301 continue;
1302 retval = acpiphp_enable_slot(slot);
1303 if (retval) {
1304 err("Error occurred in enabling\n");
1305 goto err_exit;
1306 }
1307 enabled++;
1308 }
1309 }
1310
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001311 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 err_exit:
1314 return retval;
1315}
1316
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001317static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1318{
1319 u16 pci_cmd, pci_bctl;
1320 struct pci_dev *cdev;
1321
1322 /* Program hpp values for this device */
1323 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1324 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1325 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1326 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001327
Gary Hade9ef22412007-07-05 11:10:47 -07001328 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1329 return;
1330
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001331 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001332 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001333 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001334 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001335 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001336 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001337 pci_cmd |= PCI_COMMAND_SERR;
1338 else
1339 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001340 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001341 pci_cmd |= PCI_COMMAND_PARITY;
1342 else
1343 pci_cmd &= ~PCI_COMMAND_PARITY;
1344 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1345
1346 /* Program bridge control value and child devices */
1347 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1348 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001349 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001350 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001351 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001352 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1353 else
1354 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001355 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001356 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1357 else
1358 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1359 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1360 if (dev->subordinate) {
1361 list_for_each_entry(cdev, &dev->subordinate->devices,
1362 bus_list)
1363 program_hpp(cdev, bridge);
1364 }
1365 }
1366}
1367
1368static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1369{
1370 struct acpiphp_bridge bridge;
1371 struct pci_dev *dev;
1372
1373 memset(&bridge, 0, sizeof(bridge));
1374 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001375 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001376 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001377 decode_hpp(&bridge);
1378 list_for_each_entry(dev, &bus->devices, bus_list)
1379 program_hpp(dev, &bridge);
1380
1381}
1382
1383/*
1384 * Remove devices for which we could not assign resources, call
1385 * arch specific code to fix-up the bus
1386 */
1387static void acpiphp_sanitize_bus(struct pci_bus *bus)
1388{
1389 struct pci_dev *dev;
1390 int i;
1391 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1392
1393 list_for_each_entry(dev, &bus->devices, bus_list) {
1394 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1395 struct resource *res = &dev->resource[i];
1396 if ((res->flags & type_mask) && !res->start &&
1397 res->end) {
1398 /* Could not assign a required resources
1399 * for this device, remove it */
1400 pci_remove_bus_device(dev);
1401 break;
1402 }
1403 }
1404 }
1405}
1406
1407/* Program resources in newly inserted bridge */
1408static int acpiphp_configure_bridge (acpi_handle handle)
1409{
1410 struct acpi_pci_id pci_id;
1411 struct pci_bus *bus;
1412
1413 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1414 err("cannot get PCI domain and bus number for bridge\n");
1415 return -EINVAL;
1416 }
1417 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1418 if (!bus) {
1419 err("cannot find bus %d:%d\n",
1420 pci_id.segment, pci_id.bus);
1421 return -EINVAL;
1422 }
1423
1424 pci_bus_size_bridges(bus);
1425 pci_bus_assign_resources(bus);
1426 acpiphp_sanitize_bus(bus);
1427 acpiphp_set_hpp_values(handle, bus);
1428 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001429 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001430 return 0;
1431}
1432
1433static void handle_bridge_insertion(acpi_handle handle, u32 type)
1434{
1435 struct acpi_device *device, *pdevice;
1436 acpi_handle phandle;
1437
1438 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1439 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1440 err("unexpected notification type %d\n", type);
1441 return;
1442 }
1443
1444 acpi_get_parent(handle, &phandle);
1445 if (acpi_bus_get_device(phandle, &pdevice)) {
1446 dbg("no parent device, assuming NULL\n");
1447 pdevice = NULL;
1448 }
1449 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1450 err("cannot add bridge to acpi list\n");
1451 return;
1452 }
1453 if (!acpiphp_configure_bridge(handle) &&
1454 !acpi_bus_start(device))
1455 add_bridge(handle);
1456 else
1457 err("cannot configure and start bridge\n");
1458
1459}
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461/*
1462 * ACPI event handlers
1463 */
1464
Gary Hade0bbd6422007-07-05 11:10:48 -07001465static acpi_status
1466count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1467{
1468 int *count = (int *)context;
1469 struct acpiphp_bridge *bridge;
1470
1471 bridge = acpiphp_handle_to_bridge(handle);
1472 if (bridge)
1473 (*count)++;
1474 return AE_OK ;
1475}
1476
1477static acpi_status
1478check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1479{
1480 struct acpiphp_bridge *bridge;
1481 char objname[64];
1482 struct acpi_buffer buffer = { .length = sizeof(objname),
1483 .pointer = objname };
1484
1485 bridge = acpiphp_handle_to_bridge(handle);
1486 if (bridge) {
1487 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1488 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001489 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001490 acpiphp_check_bridge(bridge);
1491 }
1492 return AE_OK ;
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/**
1496 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 * @handle: Notify()'ed acpi_handle
1498 * @type: Notify code
1499 * @context: pointer to acpiphp_bridge structure
1500 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001501 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 */
1503static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1504{
1505 struct acpiphp_bridge *bridge;
1506 char objname[64];
1507 struct acpi_buffer buffer = { .length = sizeof(objname),
1508 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001509 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001510 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001512 if (acpi_bus_get_device(handle, &device)) {
1513 /* This bridge must have just been physically inserted */
1514 handle_bridge_insertion(handle, type);
1515 return;
1516 }
1517
1518 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001519 if (type == ACPI_NOTIFY_BUS_CHECK) {
1520 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1521 count_sub_bridges, &num_sub_bridges, NULL);
1522 }
1523
1524 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001525 err("cannot get bridge info\n");
1526 return;
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1530
1531 switch (type) {
1532 case ACPI_NOTIFY_BUS_CHECK:
1533 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001534 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001535 if (bridge) {
1536 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001537 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001538 acpiphp_check_bridge(bridge);
1539 }
1540 if (num_sub_bridges)
1541 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1542 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 break;
1544
1545 case ACPI_NOTIFY_DEVICE_CHECK:
1546 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001547 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 acpiphp_check_bridge(bridge);
1549 break;
1550
1551 case ACPI_NOTIFY_DEVICE_WAKE:
1552 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001553 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
1555
1556 case ACPI_NOTIFY_EJECT_REQUEST:
1557 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001558 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001559 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1560 (bridge->flags & BRIDGE_HAS_EJ0)) {
1561 struct acpiphp_slot *slot;
1562 slot = bridge->func->slot;
1563 if (!acpiphp_disable_slot(slot))
1564 acpiphp_eject_slot(slot);
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567
1568 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1569 printk(KERN_ERR "Device %s cannot be configured due"
1570 " to a frequency mismatch\n", objname);
1571 break;
1572
1573 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1574 printk(KERN_ERR "Device %s cannot be configured due"
1575 " to a bus mode mismatch\n", objname);
1576 break;
1577
1578 case ACPI_NOTIFY_POWER_FAULT:
1579 printk(KERN_ERR "Device %s has suffered a power fault\n",
1580 objname);
1581 break;
1582
1583 default:
1584 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1585 break;
1586 }
1587}
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589/**
1590 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * @handle: Notify()'ed acpi_handle
1592 * @type: Notify code
1593 * @context: pointer to acpiphp_func structure
1594 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001595 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
Len Brown2b85e132006-06-27 01:50:14 -04001597static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 struct acpiphp_func *func;
1600 char objname[64];
1601 struct acpi_buffer buffer = { .length = sizeof(objname),
1602 .pointer = objname };
1603
1604 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1605
1606 func = (struct acpiphp_func *)context;
1607
1608 switch (type) {
1609 case ACPI_NOTIFY_BUS_CHECK:
1610 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001611 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 acpiphp_enable_slot(func->slot);
1613 break;
1614
1615 case ACPI_NOTIFY_DEVICE_CHECK:
1616 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001617 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 acpiphp_check_bridge(func->slot->bridge);
1619 break;
1620
1621 case ACPI_NOTIFY_DEVICE_WAKE:
1622 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001623 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 break;
1625
1626 case ACPI_NOTIFY_EJECT_REQUEST:
1627 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001628 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001629 if (!(acpiphp_disable_slot(func->slot)))
1630 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632
1633 default:
1634 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1635 break;
1636 }
1637}
1638
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001639
1640static acpi_status
1641find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1642{
1643 int *count = (int *)context;
1644
Kristen Accardi783c49f2006-03-03 10:16:05 -08001645 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001646 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1647 handle_hotplug_event_bridge, NULL);
1648 (*count)++;
1649 }
1650 return AE_OK ;
1651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653static struct acpi_pci_driver acpi_pci_hp_driver = {
1654 .add = add_bridge,
1655 .remove = remove_bridge,
1656};
1657
1658/**
1659 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 */
1661int __init acpiphp_glue_init(void)
1662{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001663 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001665 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1666 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (num <= 0)
1669 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001670 else
1671 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 return 0;
1674}
1675
1676
1677/**
1678 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1679 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001680 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001682void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1685}
1686
1687
1688/**
1689 * acpiphp_get_num_slots - count number of slots in a system
1690 */
1691int __init acpiphp_get_num_slots(void)
1692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001694 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Akinobu Mita467c4422006-10-30 13:07:58 -08001696 list_for_each_entry (bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001697 dbg("Bus %04x:%02x has %d slot%s\n",
1698 pci_domain_nr(bridge->pci_bus),
1699 bridge->pci_bus->number, bridge->nr_slots,
1700 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 num_slots += bridge->nr_slots;
1702 }
1703
Rajesh Shah42f49a62005-04-28 00:25:53 -07001704 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return num_slots;
1706}
1707
1708
1709#if 0
1710/**
1711 * acpiphp_for_each_slot - call function for each slot
1712 * @fn: callback function
1713 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 */
1715static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1716{
1717 struct list_head *node;
1718 struct acpiphp_bridge *bridge;
1719 struct acpiphp_slot *slot;
1720 int retval = 0;
1721
1722 list_for_each (node, &bridge_list) {
1723 bridge = (struct acpiphp_bridge *)node;
1724 for (slot = bridge->slots; slot; slot = slot->next) {
1725 retval = fn(slot, data);
1726 if (!retval)
1727 goto err_exit;
1728 }
1729 }
1730
1731 err_exit:
1732 return retval;
1733}
1734#endif
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737/**
1738 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001739 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
1741int acpiphp_enable_slot(struct acpiphp_slot *slot)
1742{
1743 int retval;
1744
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001745 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* wake up all functions */
1748 retval = power_on_slot(slot);
1749 if (retval)
1750 goto err_exit;
1751
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001752 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /* configure all functions */
1754 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001755 if (retval)
1756 power_off_slot(slot);
1757 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001758 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001759 power_off_slot(slot);
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001763 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return retval;
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/**
1768 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001769 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 */
1771int acpiphp_disable_slot(struct acpiphp_slot *slot)
1772{
1773 int retval = 0;
1774
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001775 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 /* unconfigure all functions */
1778 retval = disable_device(slot);
1779 if (retval)
1780 goto err_exit;
1781
1782 /* power off all functions */
1783 retval = power_off_slot(slot);
1784 if (retval)
1785 goto err_exit;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001788 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return retval;
1790}
1791
1792
1793/*
1794 * slot enabled: 1
1795 * slot disabled: 0
1796 */
1797u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1798{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001799 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
1802
1803/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001804 * latch open: 1
1805 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 */
1807u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1808{
1809 unsigned int sta;
1810
1811 sta = get_slot_status(slot);
1812
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001813 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
1816
1817/*
1818 * adapter presence : 1
1819 * absence : 0
1820 */
1821u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1822{
1823 unsigned int sta;
1824
1825 sta = get_slot_status(slot);
1826
1827 return (sta == 0) ? 0 : 1;
1828}