blob: 3a6064bce5614a84ba16e1411a50bcfc7afddce3 [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:
Rajesh Shah42f49a62005-04-28 00:25:53 -070035 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
36 * when the bridge is scanned and it loses a refcount when the bridge
37 * is removed.
Alex Chiang5d4a4b22009-03-30 10:50:14 -060038 * - When a P2P bridge is present, we elevate the refcount on the subordinate
39 * bus. It loses the refcount when the the driver unloads.
Rajesh Shah42f49a62005-04-28 00:25:53 -070040 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/init.h>
43#include <linux/module.h>
44
45#include <linux/kernel.h>
46#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070047#include <linux/pci_hotplug.h>
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +090048#include <linux/pci-acpi.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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "acpiphp.h"
53
54static LIST_HEAD(bridge_list);
Satoru Takeuchi600812e2006-09-12 10:22:53 -070055static LIST_HEAD(ioapic_list);
56static DEFINE_SPINLOCK(ioapic_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define MY_NAME "acpiphp_glue"
59
60static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070061static void acpiphp_sanitize_bus(struct pci_bus *bus);
62static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
Len Brown2b85e132006-06-27 01:50:14 -040063static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070064
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +090065/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -040066static acpi_status
67is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
68{
69 int *count = (int *)context;
70
71 if (is_dock_device(handle)) {
72 (*count)++;
73 return AE_CTRL_TERMINATE;
74 } else {
75 return AE_OK;
76 }
77}
78
Kristen Accardi4e8662b2006-06-28 03:08:06 -040079/*
80 * the _DCK method can do funny things... and sometimes not
81 * hah-hah funny.
82 *
83 * TBD - figure out a way to only call fixups for
84 * systems that require them.
85 */
86static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
87 void *v)
88{
89 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90 struct pci_bus *bus = func->slot->bridge->pci_bus;
91 u32 buses;
92
93 if (!bus->self)
94 return NOTIFY_OK;
95
96 /* fixup bad _DCK function that rewrites
97 * secondary bridge on slot
98 */
99 pci_read_config_dword(bus->self,
100 PCI_PRIMARY_BUS,
101 &buses);
102
103 if (((buses >> 8) & 0xff) != bus->secondary) {
104 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700105 | ((unsigned int)(bus->primary) << 0)
106 | ((unsigned int)(bus->secondary) << 8)
107 | ((unsigned int)(bus->subordinate) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400108 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
109 }
110 return NOTIFY_OK;
111}
112
113
Shaohua Li1253f7a2008-08-28 10:06:16 +0800114static struct acpi_dock_ops acpiphp_dock_ops = {
115 .handler = handle_hotplug_event_func,
116};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* callback routine to register each ACPI PCI slot object */
119static acpi_status
120register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
121{
122 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
123 struct acpiphp_slot *slot;
124 struct acpiphp_func *newfunc;
125 acpi_handle tmp;
126 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400127 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800128 int device, function, retval;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900129 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600130 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900132 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return AE_OK;
134
Matthew Garrett56ee3252008-11-25 21:48:14 +0000135 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 device = (adr >> 16) & 0xffff;
137 function = adr & 0xffff;
138
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100139 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!newfunc)
141 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 INIT_LIST_HEAD(&newfunc->sibling);
144 newfunc->handle = handle;
145 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000146
147 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800148 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
151 newfunc->flags |= FUNC_HAS_STA;
152
153 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
154 newfunc->flags |= FUNC_HAS_PS0;
155
156 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
157 newfunc->flags |= FUNC_HAS_PS3;
158
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800160 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400163 if (ACPI_FAILURE(status)) {
164 /*
165 * use the count of the number of slots we've found
166 * for the number of the slot
167 */
168 sun = bridge->nr_slots+1;
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* search for objects that share the same slot */
172 for (slot = bridge->slots; slot; slot = slot->next)
173 if (slot->device == device) {
174 if (slot->sun != sun)
175 warn("sibling found, but _SUN doesn't match!\n");
176 break;
177 }
178
179 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100180 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (!slot) {
182 kfree(newfunc);
183 return AE_NO_MEMORY;
184 }
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 slot->device = device;
188 slot->sun = sun;
189 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100190 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 slot->next = bridge->slots;
193 bridge->slots = slot;
194
195 bridge->nr_slots++;
196
Justin Chenb6adc192008-12-11 11:16:44 -0700197 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900198 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800199 retval = acpiphp_register_hotplug_slot(slot);
200 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600201 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700202 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600203 "hotplug driver\n", slot->sun);
204 else
205 warn("acpiphp_register_hotplug_slot failed "
206 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800207 goto err_exit;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 newfunc->slot = slot;
212 list_add_tail(&newfunc->sibling, &slot->funcs);
213
Alex Chiang9d911d72009-05-21 16:21:15 -0600214 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
215 if (pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Alex Chiang9d911d72009-05-21 16:21:15 -0600217 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400220 if (is_dock_device(handle)) {
221 /* we don't want to call this device's _EJ0
222 * because we want the dock notify handler
223 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800224 */
225 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400226 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800227 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400228 dbg("failed to register dock device\n");
229
230 /* we need to be notified when dock events happen
231 * outside of the hotplug operation, since we may
232 * need to do fixups before we can hotplug.
233 */
234 newfunc->nb.notifier_call = post_dock_fixups;
235 if (register_dock_notifier(&newfunc->nb))
236 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800237 }
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800240 if (!(newfunc->flags & FUNC_HAS_DCK)) {
241 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ACPI_SYSTEM_NOTIFY,
243 handle_hotplug_event_func,
244 newfunc);
245
Kristen Accardi20416ea2006-02-23 17:56:03 -0800246 if (ACPI_FAILURE(status))
247 err("failed to register interrupt notify handler\n");
248 } else
249 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Kristen Accardi20416ea2006-02-23 17:56:03 -0800251 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800252
253 err_exit:
254 bridge->nr_slots--;
255 bridge->slots = slot->next;
256 kfree(slot);
257 kfree(newfunc);
258
259 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262
263/* see if it's worth looking at this bridge */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900264static int detect_ejectable_slots(struct pci_bus *pbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900266 int found = acpi_pci_detect_ejectable(pbus);
267 if (!found) {
268 acpi_handle bridge_handle = acpi_pci_get_bridge_handle(pbus);
James Bottomleyd45e0852009-01-15 15:12:27 -0500269 if (!bridge_handle)
270 return 0;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900271 acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
272 is_pci_dock_device, (void *)&found, NULL);
273 }
274 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/* decode ACPI 2.0 _HPP hot plug parameters */
279static void decode_hpp(struct acpiphp_bridge *bridge)
280{
281 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900283 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900284 if (ACPI_FAILURE(status) ||
285 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800286 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900287 printk(KERN_WARNING
288 "%s: Could not get hotplug parameters. Use defaults\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800289 __func__);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900290 bridge->hpp.t0 = &bridge->hpp.type0_data;
291 bridge->hpp.t0->revision = 0;
292 bridge->hpp.t0->cache_line_size = 0x10;
293 bridge->hpp.t0->latency_timer = 0x40;
294 bridge->hpp.t0->enable_serr = 0;
295 bridge->hpp.t0->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) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900322 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
323 status = acpi_remove_notify_handler(bridge->func->handle,
324 ACPI_SYSTEM_NOTIFY,
325 handle_hotplug_event_func);
326 if (ACPI_FAILURE(status))
327 err("failed to remove notify handler\n");
328 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700329 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ACPI_SYSTEM_NOTIFY,
331 handle_hotplug_event_bridge,
332 bridge);
333
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700334 if (ACPI_FAILURE(status)) {
335 err("failed to register interrupt notify handler\n");
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900341/* find acpiphp_func from acpiphp_bridge */
342static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
343{
344 struct list_head *node, *l;
345 struct acpiphp_bridge *bridge;
346 struct acpiphp_slot *slot;
347 struct acpiphp_func *func;
348
349 list_for_each(node, &bridge_list) {
350 bridge = list_entry(node, struct acpiphp_bridge, list);
351 for (slot = bridge->slots; slot; slot = slot->next) {
352 list_for_each(l, &slot->funcs) {
353 func = list_entry(l, struct acpiphp_func,
354 sibling);
355 if (func->handle == handle)
356 return func;
357 }
358 }
359 }
360
361 return NULL;
362}
363
364
365static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
366{
367 acpi_handle dummy_handle;
368
369 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
370 "_STA", &dummy_handle)))
371 bridge->flags |= BRIDGE_HAS_STA;
372
373 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
374 "_EJ0", &dummy_handle)))
375 bridge->flags |= BRIDGE_HAS_EJ0;
376
377 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
378 "_PS0", &dummy_handle)))
379 bridge->flags |= BRIDGE_HAS_PS0;
380
381 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
382 "_PS3", &dummy_handle)))
383 bridge->flags |= BRIDGE_HAS_PS3;
384
385 /* is this ejectable p2p bridge? */
386 if (bridge->flags & BRIDGE_HAS_EJ0) {
387 struct acpiphp_func *func;
388
389 dbg("found ejectable p2p bridge\n");
390
391 /* make link between PCI bridge and PCI function */
392 func = acpiphp_bridge_handle_to_function(bridge->handle);
393 if (!func)
394 return;
395 bridge->func = func;
396 func->bridge = bridge;
397 }
398}
399
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700402static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct acpiphp_bridge *bridge;
405
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100406 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (bridge == NULL)
408 return;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 bridge->type = BRIDGE_TYPE_HOST;
411 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Rajesh Shah42f49a62005-04-28 00:25:53 -0700413 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 spin_lock_init(&bridge->res_lock);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 init_bridge_misc(bridge);
418}
419
420
421/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700422static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100426 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (bridge == NULL) {
428 err("out of memory\n");
429 return;
430 }
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 bridge->type = BRIDGE_TYPE_P2P;
433 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900434 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Rajesh Shah42f49a62005-04-28 00:25:53 -0700436 bridge->pci_dev = pci_dev_get(pci_dev);
437 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!bridge->pci_bus) {
439 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700440 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600443 /*
444 * Grab a ref to the subordinate PCI bus in case the bus is
445 * removed via PCI core logical hotplug. The ref pins the bus
446 * (which we access during module unload).
447 */
448 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_lock_init(&bridge->res_lock);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700452 return;
453 err:
454 pci_dev_put(pci_dev);
455 kfree(bridge);
456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459
460/* callback routine to find P2P bridges */
461static acpi_status
462find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
463{
464 acpi_status status;
465 acpi_handle dummy_handle;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400466 unsigned long long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700467 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700469 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
472 if (ACPI_FAILURE(status))
473 return AE_OK; /* continue */
474
475 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
476 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800477 dbg("%s: _ADR evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return AE_OK;
479 }
480
481 device = (tmp >> 16) & 0xffff;
482 function = tmp & 0xffff;
483
Rajesh Shah42f49a62005-04-28 00:25:53 -0700484 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Rajesh Shah42f49a62005-04-28 00:25:53 -0700486 if (!dev || !dev->subordinate)
487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* check if this bridge has ejectable slots */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900490 if ((detect_ejectable_slots(dev->subordinate) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700492 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900495 /* search P2P bridges under this p2p bridge */
496 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
497 find_p2p_bridge, dev->subordinate, NULL);
498 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900499 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900500
Rajesh Shah42f49a62005-04-28 00:25:53 -0700501 out:
502 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return AE_OK;
504}
505
506
507/* find hot-pluggable slots, and then find P2P bridge */
508static int add_bridge(acpi_handle handle)
509{
510 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400511 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int seg, bus;
513 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700514 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* if the bridge doesn't have _STA, we assume it is always there */
517 status = acpi_get_handle(handle, "_STA", &dummy_handle);
518 if (ACPI_SUCCESS(status)) {
519 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
520 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800521 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 0;
523 }
524 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
525 /* don't register this object */
526 return 0;
527 }
528
529 /* get PCI segment number */
530 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
531
532 seg = ACPI_SUCCESS(status) ? tmp : 0;
533
534 /* get PCI bus number */
535 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
536
537 if (ACPI_SUCCESS(status)) {
538 bus = tmp;
539 } else {
540 warn("can't get bus number, assuming 0\n");
541 bus = 0;
542 }
543
Rajesh Shah42f49a62005-04-28 00:25:53 -0700544 pci_bus = pci_find_bus(seg, bus);
545 if (!pci_bus) {
546 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0;
548 }
549
Rajesh Shah42f49a62005-04-28 00:25:53 -0700550 /* check if this bridge has ejectable slots */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900551 if (detect_ejectable_slots(pci_bus) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700552 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
553 add_host_bridge(handle, pci_bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /* search P2P bridges under this host bridge */
557 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700558 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900561 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 return 0;
564}
565
Rajesh Shah42f49a62005-04-28 00:25:53 -0700566static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
567{
568 struct list_head *head;
569 list_for_each(head, &bridge_list) {
570 struct acpiphp_bridge *bridge = list_entry(head,
571 struct acpiphp_bridge, list);
572 if (bridge->handle == handle)
573 return bridge;
574 }
575
576 return NULL;
577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Rajesh Shah364d5092005-04-28 00:25:54 -0700579static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700581 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700582 struct acpiphp_slot *slot;
583 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700584 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700585
586 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
587 handle_hotplug_event_bridge);
588 if (ACPI_FAILURE(status))
589 err("failed to remove notify handler\n");
590
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900591 if ((bridge->type != BRIDGE_TYPE_HOST) &&
592 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
593 status = acpi_install_notify_handler(bridge->func->handle,
594 ACPI_SYSTEM_NOTIFY,
595 handle_hotplug_event_func,
596 bridge->func);
597 if (ACPI_FAILURE(status))
598 err("failed to install interrupt notify handler\n");
599 }
600
Rajesh Shah42f49a62005-04-28 00:25:53 -0700601 slot = bridge->slots;
602 while (slot) {
603 struct acpiphp_slot *next = slot->next;
604 list_for_each_safe (list, tmp, &slot->funcs) {
605 struct acpiphp_func *func;
606 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400607 if (is_dock_device(func->handle)) {
608 unregister_hotplug_dock_device(func->handle);
609 unregister_dock_notifier(&func->nb);
610 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800611 if (!(func->flags & FUNC_HAS_DCK)) {
612 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700613 ACPI_SYSTEM_NOTIFY,
614 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800615 if (ACPI_FAILURE(status))
616 err("failed to remove notify handler\n");
617 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700618 list_del(list);
619 kfree(func);
620 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800621 acpiphp_unregister_hotplug_slot(slot);
622 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700623 kfree(slot);
624 slot = next;
625 }
626
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600627 /*
628 * Only P2P bridges have a pci_dev
629 */
630 if (bridge->pci_dev)
631 put_device(&bridge->pci_bus->dev);
632
Rajesh Shah42f49a62005-04-28 00:25:53 -0700633 pci_dev_put(bridge->pci_dev);
634 list_del(&bridge->list);
635 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Rajesh Shah364d5092005-04-28 00:25:54 -0700638static acpi_status
639cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
640{
641 struct acpiphp_bridge *bridge;
642
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900643 /* cleanup p2p bridges under this P2P bridge
644 in a depth-first manner */
645 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
646 cleanup_p2p_bridge, NULL, NULL);
647
Alex Chianga13307c2008-07-01 20:02:23 -0600648 bridge = acpiphp_handle_to_bridge(handle);
649 if (bridge)
650 cleanup_bridge(bridge);
651
Rajesh Shah364d5092005-04-28 00:25:54 -0700652 return AE_OK;
653}
654
655static void remove_bridge(acpi_handle handle)
656{
657 struct acpiphp_bridge *bridge;
658
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900659 /* cleanup p2p bridges under this host bridge
660 in a depth-first manner */
661 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
662 (u32)1, cleanup_p2p_bridge, NULL, NULL);
663
Alex Chianga13307c2008-07-01 20:02:23 -0600664 /*
665 * On root bridges with hotplug slots directly underneath (ie,
666 * no p2p bridge inbetween), we call cleanup_bridge().
667 *
668 * The else clause cleans up root bridges that either had no
669 * hotplug slots at all, or had a p2p bridge underneath.
670 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700671 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900672 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700673 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600674 else
675 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
676 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700677}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700679static struct pci_dev * get_apic_pci_info(acpi_handle handle)
680{
681 struct acpi_pci_id id;
682 struct pci_bus *bus;
683 struct pci_dev *dev;
684
685 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
686 return NULL;
687
688 bus = pci_find_bus(id.segment, id.bus);
689 if (!bus)
690 return NULL;
691
692 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
693 if (!dev)
694 return NULL;
695
696 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
697 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
698 {
699 pci_dev_put(dev);
700 return NULL;
701 }
702
703 return dev;
704}
705
706static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
707{
708 acpi_status status;
709 int result = -1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400710 unsigned long long gsb;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700711 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
712 union acpi_object *obj;
713 void *table;
714
715 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
716 if (ACPI_SUCCESS(status)) {
717 *gsi_base = (u32)gsb;
718 return 0;
719 }
720
721 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
722 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
723 return -1;
724
725 obj = buffer.pointer;
726 if (obj->type != ACPI_TYPE_BUFFER)
727 goto out;
728
729 table = obj->buffer.pointer;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300730 switch (((struct acpi_subtable_header *)table)->type) {
731 case ACPI_MADT_TYPE_IO_SAPIC:
732 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700733 result = 0;
734 break;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300735 case ACPI_MADT_TYPE_IO_APIC:
736 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700737 result = 0;
738 break;
739 default:
740 break;
741 }
742 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700743 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700744 return result;
745}
746
747static acpi_status
748ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
749{
750 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400751 unsigned long long sta;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700752 acpi_handle tmp;
753 struct pci_dev *pdev;
754 u32 gsi_base;
755 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700756 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700757
758 /* Evaluate _STA if present */
759 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
760 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
761 return AE_CTRL_DEPTH;
762
763 /* Scan only PCI bus scope */
764 status = acpi_get_handle(handle, "_HID", &tmp);
765 if (ACPI_SUCCESS(status))
766 return AE_CTRL_DEPTH;
767
768 if (get_gsi_base(handle, &gsi_base))
769 return AE_OK;
770
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700771 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
772 if (!ioapic)
773 return AE_NO_MEMORY;
774
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700775 pdev = get_apic_pci_info(handle);
776 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700777 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700778
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700779 if (pci_enable_device(pdev))
780 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700781
782 pci_set_master(pdev);
783
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700784 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
785 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700786
787 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700788 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
789 goto exit_pci_release_region;
790
791 ioapic->gsi_base = gsi_base;
792 ioapic->dev = pdev;
793 spin_lock(&ioapic_list_lock);
794 list_add_tail(&ioapic->list, &ioapic_list);
795 spin_unlock(&ioapic_list_lock);
796
797 return AE_OK;
798
799 exit_pci_release_region:
800 pci_release_region(pdev, 0);
801 exit_pci_disable_device:
802 pci_disable_device(pdev);
803 exit_pci_dev_put:
804 pci_dev_put(pdev);
805 exit_kfree:
806 kfree(ioapic);
807
808 return AE_OK;
809}
810
811static acpi_status
812ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
813{
814 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400815 unsigned long long sta;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700816 acpi_handle tmp;
817 u32 gsi_base;
818 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
819
820 /* Evaluate _STA if present */
821 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
822 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
823 return AE_CTRL_DEPTH;
824
825 /* Scan only PCI bus scope */
826 status = acpi_get_handle(handle, "_HID", &tmp);
827 if (ACPI_SUCCESS(status))
828 return AE_CTRL_DEPTH;
829
830 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700831 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700832
833 acpi_unregister_ioapic(handle, gsi_base);
834
835 spin_lock(&ioapic_list_lock);
836 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
837 if (pos->gsi_base != gsi_base)
838 continue;
839 ioapic = pos;
840 list_del(&ioapic->list);
841 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700842 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700843 spin_unlock(&ioapic_list_lock);
844
845 if (!ioapic)
846 return AE_OK;
847
848 pci_release_region(ioapic->dev, 0);
849 pci_disable_device(ioapic->dev);
850 pci_dev_put(ioapic->dev);
851 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700852
853 return AE_OK;
854}
855
856static int acpiphp_configure_ioapics(acpi_handle handle)
857{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700858 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700859 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
860 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
861 return 0;
862}
863
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700864static int acpiphp_unconfigure_ioapics(acpi_handle handle)
865{
866 ioapic_remove(handle, 0, NULL, NULL);
867 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
868 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
869 return 0;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872static int power_on_slot(struct acpiphp_slot *slot)
873{
874 acpi_status status;
875 struct acpiphp_func *func;
876 struct list_head *l;
877 int retval = 0;
878
879 /* if already enabled, just skip */
880 if (slot->flags & SLOT_POWEREDON)
881 goto err_exit;
882
883 list_for_each (l, &slot->funcs) {
884 func = list_entry(l, struct acpiphp_func, sibling);
885
886 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800887 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
889 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800890 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 retval = -1;
892 goto err_exit;
893 } else
894 break;
895 }
896 }
897
898 /* TBD: evaluate _STA to check if the slot is enabled */
899
900 slot->flags |= SLOT_POWEREDON;
901
902 err_exit:
903 return retval;
904}
905
906
907static int power_off_slot(struct acpiphp_slot *slot)
908{
909 acpi_status status;
910 struct acpiphp_func *func;
911 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 int retval = 0;
914
915 /* if already disabled, just skip */
916 if ((slot->flags & SLOT_POWEREDON) == 0)
917 goto err_exit;
918
919 list_for_each (l, &slot->funcs) {
920 func = list_entry(l, struct acpiphp_func, sibling);
921
Rajesh Shah2f523b12005-04-28 00:25:55 -0700922 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
924 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800925 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 retval = -1;
927 goto err_exit;
928 } else
929 break;
930 }
931 }
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* TBD: evaluate _STA to check if the slot is disabled */
934
935 slot->flags &= (~SLOT_POWEREDON);
936
937 err_exit:
938 return retval;
939}
940
941
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800942
943/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800944 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800945 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800946 */
947static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
948{
949 struct list_head *tmp;
950 unsigned char max, n;
951
952 /*
953 * pci_bus_max_busnr will return the highest
954 * reserved busnr for all these children.
955 * that is equivalent to the bus->subordinate
956 * value. We don't want to use the parent's
957 * bus->subordinate value because it could have
958 * padding in it.
959 */
960 max = bus->secondary;
961
962 list_for_each(tmp, &bus->children) {
963 n = pci_bus_max_busnr(pci_bus_b(tmp));
964 if (n > max)
965 max = n;
966 }
967 return max;
968}
969
970
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800971/**
972 * acpiphp_bus_add - add a new bus to acpi subsystem
973 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800974 */
975static int acpiphp_bus_add(struct acpiphp_func *func)
976{
977 acpi_handle phandle;
978 struct acpi_device *device, *pdevice;
979 int ret_val;
980
981 acpi_get_parent(func->handle, &phandle);
982 if (acpi_bus_get_device(phandle, &pdevice)) {
983 dbg("no parent device, assuming NULL\n");
984 pdevice = NULL;
985 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800986 if (!acpi_bus_get_device(func->handle, &device)) {
987 dbg("bus exists... trim\n");
988 /* this shouldn't be in here, so remove
989 * the bus then re-add it...
990 */
991 ret_val = acpi_bus_trim(device, 1);
992 dbg("acpi_bus_trim return %x\n", ret_val);
993 }
994
995 ret_val = acpi_bus_add(&device, pdevice, func->handle,
996 ACPI_BUS_TYPE_DEVICE);
997 if (ret_val) {
998 dbg("error adding bus, %x\n",
999 -ret_val);
1000 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001001 }
1002 /*
1003 * try to start anyway. We could have failed to add
1004 * simply because this bus had previously been added
1005 * on another add. Don't bother with the return value
1006 * we just keep going.
1007 */
1008 ret_val = acpi_bus_start(device);
1009
1010acpiphp_bus_add_out:
1011 return ret_val;
1012}
1013
1014
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001015/**
1016 * acpiphp_bus_trim - trim a bus from acpi subsystem
1017 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001018 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -07001019static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001020{
1021 struct acpi_device *device;
1022 int retval;
1023
1024 retval = acpi_bus_get_device(handle, &device);
1025 if (retval) {
1026 dbg("acpi_device not found\n");
1027 return retval;
1028 }
1029
1030 retval = acpi_bus_trim(device, 1);
1031 if (retval)
1032 err("cannot remove from acpi list\n");
1033
1034 return retval;
1035}
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/**
1038 * enable_device - enable, configure a slot
1039 * @slot: slot to be enabled
1040 *
1041 * This function should be called per *physical slot*,
1042 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +01001044static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001047 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct list_head *l;
1049 struct acpiphp_func *func;
1050 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001051 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001052 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (slot->flags & SLOT_ENABLED)
1055 goto err_exit;
1056
1057 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001058 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (dev) {
1060 /* This case shouldn't happen */
1061 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001062 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 retval = -1;
1064 goto err_exit;
1065 }
1066
Rajesh Shah42f49a62005-04-28 00:25:53 -07001067 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1068 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 err("No new device found\n");
1070 retval = -1;
1071 goto err_exit;
1072 }
1073
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001074 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001075 for (pass = 0; pass < 2; pass++) {
1076 list_for_each_entry(dev, &bus->devices, bus_list) {
1077 if (PCI_SLOT(dev->devfn) != slot->device)
1078 continue;
1079 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001080 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001081 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001082 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001083 pci_bus_size_bridges(dev->subordinate);
1084 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001088 list_for_each (l, &slot->funcs) {
1089 func = list_entry(l, struct acpiphp_func, sibling);
1090 acpiphp_bus_add(func);
1091 }
1092
Rajesh Shah42f49a62005-04-28 00:25:53 -07001093 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001094 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001095 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001096 list_for_each_entry(func, &slot->funcs, sibling)
1097 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001098 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001099 pci_bus_add_devices(bus);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 list_for_each (l, &slot->funcs) {
1102 func = list_entry(l, struct acpiphp_func, sibling);
Alex Chiang9d911d72009-05-21 16:21:15 -06001103 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1104 func->function));
1105 if (!dev)
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001106 continue;
1107
Alex Chiang9d911d72009-05-21 16:21:15 -06001108 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1109 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1110 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001111 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -06001112 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001113
1114 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1115 if (ACPI_FAILURE(status))
1116 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1117 status);
Alex Chiang9d911d72009-05-21 16:21:15 -06001118 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
1121 slot->flags |= SLOT_ENABLED;
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 err_exit:
1124 return retval;
1125}
1126
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001127static void disable_bridges(struct pci_bus *bus)
1128{
1129 struct pci_dev *dev;
1130 list_for_each_entry(dev, &bus->devices, bus_list) {
1131 if (dev->subordinate) {
1132 disable_bridges(dev->subordinate);
1133 pci_disable_device(dev);
1134 }
1135 }
1136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138/**
1139 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001140 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
1142static int disable_device(struct acpiphp_slot *slot)
1143{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -06001145 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* is this slot already disabled? */
1148 if (!(slot->flags & SLOT_ENABLED))
1149 goto err_exit;
1150
Alex Chiang9d911d72009-05-21 16:21:15 -06001151 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001152 if (func->bridge) {
1153 /* cleanup p2p bridges under this P2P bridge */
1154 cleanup_p2p_bridge(func->bridge->handle,
1155 (u32)1, NULL, NULL);
1156 func->bridge = NULL;
1157 }
1158
Alex Chiang9d911d72009-05-21 16:21:15 -06001159 pdev = pci_get_slot(slot->bridge->pci_bus,
1160 PCI_DEVFN(slot->device, func->function));
1161 if (pdev) {
1162 pci_stop_bus_device(pdev);
1163 if (pdev->subordinate) {
1164 disable_bridges(pdev->subordinate);
1165 pci_disable_device(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001166 }
Alex Chiang9d911d72009-05-21 16:21:15 -06001167 pci_remove_bus_device(pdev);
1168 pci_dev_put(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001169 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001170 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001171
Alex Chiang9d911d72009-05-21 16:21:15 -06001172 list_for_each_entry(func, &slot->funcs, sibling) {
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001173 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001174 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
1177 slot->flags &= (~SLOT_ENABLED);
1178
Alex Chiang9d911d72009-05-21 16:21:15 -06001179err_exit:
1180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183
1184/**
1185 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -08001186 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001188 * If a slot has _STA for each function and if any one of them
1189 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001191 * If a slot doesn't have _STA and if any one of its functions'
1192 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001194 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
1196static unsigned int get_slot_status(struct acpiphp_slot *slot)
1197{
1198 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001199 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 u32 dvid;
1201 struct list_head *l;
1202 struct acpiphp_func *func;
1203
1204 list_for_each (l, &slot->funcs) {
1205 func = list_entry(l, struct acpiphp_func, sibling);
1206
1207 if (func->flags & FUNC_HAS_STA) {
1208 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1209 if (ACPI_SUCCESS(status) && sta)
1210 break;
1211 } else {
1212 pci_bus_read_config_dword(slot->bridge->pci_bus,
1213 PCI_DEVFN(slot->device,
1214 func->function),
1215 PCI_VENDOR_ID, &dvid);
1216 if (dvid != 0xffffffff) {
1217 sta = ACPI_STA_ALL;
1218 break;
1219 }
1220 }
1221 }
1222
1223 return (unsigned int)sta;
1224}
1225
1226/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001227 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001228 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001229 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001230int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001231{
1232 acpi_status status;
1233 struct acpiphp_func *func;
1234 struct list_head *l;
1235 struct acpi_object_list arg_list;
1236 union acpi_object arg;
1237
1238 list_for_each (l, &slot->funcs) {
1239 func = list_entry(l, struct acpiphp_func, sibling);
1240
1241 /* We don't want to call _EJ0 on non-existing functions. */
1242 if ((func->flags & FUNC_HAS_EJ0)) {
1243 /* _EJ0 method take one argument */
1244 arg_list.count = 1;
1245 arg_list.pointer = &arg;
1246 arg.type = ACPI_TYPE_INTEGER;
1247 arg.integer.value = 1;
1248
1249 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1250 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001251 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001252 return -1;
1253 } else
1254 break;
1255 }
1256 }
1257 return 0;
1258}
1259
1260/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001262 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 *
1264 * Iterate over all slots under this bridge and make sure that if a
1265 * card is present they are enabled, and if not they are disabled.
1266 */
1267static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1268{
1269 struct acpiphp_slot *slot;
1270 int retval = 0;
1271 int enabled, disabled;
1272
1273 enabled = disabled = 0;
1274
1275 for (slot = bridge->slots; slot; slot = slot->next) {
1276 unsigned int status = get_slot_status(slot);
1277 if (slot->flags & SLOT_ENABLED) {
1278 if (status == ACPI_STA_ALL)
1279 continue;
1280 retval = acpiphp_disable_slot(slot);
1281 if (retval) {
1282 err("Error occurred in disabling\n");
1283 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001284 } else {
1285 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 disabled++;
1288 } else {
1289 if (status != ACPI_STA_ALL)
1290 continue;
1291 retval = acpiphp_enable_slot(slot);
1292 if (retval) {
1293 err("Error occurred in enabling\n");
1294 goto err_exit;
1295 }
1296 enabled++;
1297 }
1298 }
1299
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001300 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 err_exit:
1303 return retval;
1304}
1305
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001306static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1307{
1308 u16 pci_cmd, pci_bctl;
1309 struct pci_dev *cdev;
1310
1311 /* Program hpp values for this device */
1312 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1313 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1314 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1315 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001316
Gary Hade9ef22412007-07-05 11:10:47 -07001317 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1318 return;
1319
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001320 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001321 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001322 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001323 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001324 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001325 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001326 pci_cmd |= PCI_COMMAND_SERR;
1327 else
1328 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001329 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001330 pci_cmd |= PCI_COMMAND_PARITY;
1331 else
1332 pci_cmd &= ~PCI_COMMAND_PARITY;
1333 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1334
1335 /* Program bridge control value and child devices */
1336 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1337 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001338 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001339 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001340 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001341 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1342 else
1343 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001344 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001345 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1346 else
1347 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1348 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1349 if (dev->subordinate) {
1350 list_for_each_entry(cdev, &dev->subordinate->devices,
1351 bus_list)
1352 program_hpp(cdev, bridge);
1353 }
1354 }
1355}
1356
1357static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1358{
1359 struct acpiphp_bridge bridge;
1360 struct pci_dev *dev;
1361
1362 memset(&bridge, 0, sizeof(bridge));
1363 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001364 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001365 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001366 decode_hpp(&bridge);
1367 list_for_each_entry(dev, &bus->devices, bus_list)
1368 program_hpp(dev, &bridge);
1369
1370}
1371
1372/*
1373 * Remove devices for which we could not assign resources, call
1374 * arch specific code to fix-up the bus
1375 */
1376static void acpiphp_sanitize_bus(struct pci_bus *bus)
1377{
1378 struct pci_dev *dev;
1379 int i;
1380 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1381
1382 list_for_each_entry(dev, &bus->devices, bus_list) {
1383 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1384 struct resource *res = &dev->resource[i];
1385 if ((res->flags & type_mask) && !res->start &&
1386 res->end) {
1387 /* Could not assign a required resources
1388 * for this device, remove it */
1389 pci_remove_bus_device(dev);
1390 break;
1391 }
1392 }
1393 }
1394}
1395
1396/* Program resources in newly inserted bridge */
1397static int acpiphp_configure_bridge (acpi_handle handle)
1398{
1399 struct acpi_pci_id pci_id;
1400 struct pci_bus *bus;
1401
1402 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1403 err("cannot get PCI domain and bus number for bridge\n");
1404 return -EINVAL;
1405 }
1406 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1407 if (!bus) {
1408 err("cannot find bus %d:%d\n",
1409 pci_id.segment, pci_id.bus);
1410 return -EINVAL;
1411 }
1412
1413 pci_bus_size_bridges(bus);
1414 pci_bus_assign_resources(bus);
1415 acpiphp_sanitize_bus(bus);
1416 acpiphp_set_hpp_values(handle, bus);
1417 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001418 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001419 return 0;
1420}
1421
1422static void handle_bridge_insertion(acpi_handle handle, u32 type)
1423{
1424 struct acpi_device *device, *pdevice;
1425 acpi_handle phandle;
1426
1427 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1428 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1429 err("unexpected notification type %d\n", type);
1430 return;
1431 }
1432
1433 acpi_get_parent(handle, &phandle);
1434 if (acpi_bus_get_device(phandle, &pdevice)) {
1435 dbg("no parent device, assuming NULL\n");
1436 pdevice = NULL;
1437 }
1438 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1439 err("cannot add bridge to acpi list\n");
1440 return;
1441 }
1442 if (!acpiphp_configure_bridge(handle) &&
1443 !acpi_bus_start(device))
1444 add_bridge(handle);
1445 else
1446 err("cannot configure and start bridge\n");
1447
1448}
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450/*
1451 * ACPI event handlers
1452 */
1453
Gary Hade0bbd6422007-07-05 11:10:48 -07001454static acpi_status
1455count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1456{
1457 int *count = (int *)context;
1458 struct acpiphp_bridge *bridge;
1459
1460 bridge = acpiphp_handle_to_bridge(handle);
1461 if (bridge)
1462 (*count)++;
1463 return AE_OK ;
1464}
1465
1466static acpi_status
1467check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1468{
1469 struct acpiphp_bridge *bridge;
1470 char objname[64];
1471 struct acpi_buffer buffer = { .length = sizeof(objname),
1472 .pointer = objname };
1473
1474 bridge = acpiphp_handle_to_bridge(handle);
1475 if (bridge) {
1476 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1477 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001478 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001479 acpiphp_check_bridge(bridge);
1480 }
1481 return AE_OK ;
1482}
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/**
1485 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 * @handle: Notify()'ed acpi_handle
1487 * @type: Notify code
1488 * @context: pointer to acpiphp_bridge structure
1489 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001490 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
1492static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1493{
1494 struct acpiphp_bridge *bridge;
1495 char objname[64];
1496 struct acpi_buffer buffer = { .length = sizeof(objname),
1497 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001498 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001499 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001501 if (acpi_bus_get_device(handle, &device)) {
1502 /* This bridge must have just been physically inserted */
1503 handle_bridge_insertion(handle, type);
1504 return;
1505 }
1506
1507 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001508 if (type == ACPI_NOTIFY_BUS_CHECK) {
1509 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1510 count_sub_bridges, &num_sub_bridges, NULL);
1511 }
1512
1513 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001514 err("cannot get bridge info\n");
1515 return;
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1519
1520 switch (type) {
1521 case ACPI_NOTIFY_BUS_CHECK:
1522 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001523 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001524 if (bridge) {
1525 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001526 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001527 acpiphp_check_bridge(bridge);
1528 }
1529 if (num_sub_bridges)
1530 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1531 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 break;
1533
1534 case ACPI_NOTIFY_DEVICE_CHECK:
1535 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001536 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 acpiphp_check_bridge(bridge);
1538 break;
1539
1540 case ACPI_NOTIFY_DEVICE_WAKE:
1541 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001542 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 break;
1544
1545 case ACPI_NOTIFY_EJECT_REQUEST:
1546 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001547 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001548 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1549 (bridge->flags & BRIDGE_HAS_EJ0)) {
1550 struct acpiphp_slot *slot;
1551 slot = bridge->func->slot;
1552 if (!acpiphp_disable_slot(slot))
1553 acpiphp_eject_slot(slot);
1554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 break;
1556
1557 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1558 printk(KERN_ERR "Device %s cannot be configured due"
1559 " to a frequency mismatch\n", objname);
1560 break;
1561
1562 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1563 printk(KERN_ERR "Device %s cannot be configured due"
1564 " to a bus mode mismatch\n", objname);
1565 break;
1566
1567 case ACPI_NOTIFY_POWER_FAULT:
1568 printk(KERN_ERR "Device %s has suffered a power fault\n",
1569 objname);
1570 break;
1571
1572 default:
1573 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1574 break;
1575 }
1576}
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578/**
1579 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 * @handle: Notify()'ed acpi_handle
1581 * @type: Notify code
1582 * @context: pointer to acpiphp_func structure
1583 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001584 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 */
Len Brown2b85e132006-06-27 01:50:14 -04001586static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct acpiphp_func *func;
1589 char objname[64];
1590 struct acpi_buffer buffer = { .length = sizeof(objname),
1591 .pointer = objname };
1592
1593 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1594
1595 func = (struct acpiphp_func *)context;
1596
1597 switch (type) {
1598 case ACPI_NOTIFY_BUS_CHECK:
1599 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001600 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 acpiphp_enable_slot(func->slot);
1602 break;
1603
1604 case ACPI_NOTIFY_DEVICE_CHECK:
1605 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001606 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 acpiphp_check_bridge(func->slot->bridge);
1608 break;
1609
1610 case ACPI_NOTIFY_DEVICE_WAKE:
1611 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001612 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
1614
1615 case ACPI_NOTIFY_EJECT_REQUEST:
1616 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001617 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001618 if (!(acpiphp_disable_slot(func->slot)))
1619 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 break;
1621
1622 default:
1623 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1624 break;
1625 }
1626}
1627
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001628
1629static acpi_status
1630find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1631{
1632 int *count = (int *)context;
1633
Kristen Accardi783c49f2006-03-03 10:16:05 -08001634 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001635 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1636 handle_hotplug_event_bridge, NULL);
1637 (*count)++;
1638 }
1639 return AE_OK ;
1640}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642static struct acpi_pci_driver acpi_pci_hp_driver = {
1643 .add = add_bridge,
1644 .remove = remove_bridge,
1645};
1646
1647/**
1648 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 */
1650int __init acpiphp_glue_init(void)
1651{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001652 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001654 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1655 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if (num <= 0)
1658 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001659 else
1660 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 return 0;
1663}
1664
1665
1666/**
1667 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1668 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001669 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001671void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1674}
1675
1676
1677/**
1678 * acpiphp_get_num_slots - count number of slots in a system
1679 */
1680int __init acpiphp_get_num_slots(void)
1681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001683 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Akinobu Mita467c4422006-10-30 13:07:58 -08001685 list_for_each_entry (bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001686 dbg("Bus %04x:%02x has %d slot%s\n",
1687 pci_domain_nr(bridge->pci_bus),
1688 bridge->pci_bus->number, bridge->nr_slots,
1689 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 num_slots += bridge->nr_slots;
1691 }
1692
Rajesh Shah42f49a62005-04-28 00:25:53 -07001693 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return num_slots;
1695}
1696
1697
1698#if 0
1699/**
1700 * acpiphp_for_each_slot - call function for each slot
1701 * @fn: callback function
1702 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 */
1704static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1705{
1706 struct list_head *node;
1707 struct acpiphp_bridge *bridge;
1708 struct acpiphp_slot *slot;
1709 int retval = 0;
1710
1711 list_for_each (node, &bridge_list) {
1712 bridge = (struct acpiphp_bridge *)node;
1713 for (slot = bridge->slots; slot; slot = slot->next) {
1714 retval = fn(slot, data);
1715 if (!retval)
1716 goto err_exit;
1717 }
1718 }
1719
1720 err_exit:
1721 return retval;
1722}
1723#endif
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726/**
1727 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001728 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 */
1730int acpiphp_enable_slot(struct acpiphp_slot *slot)
1731{
1732 int retval;
1733
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001734 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 /* wake up all functions */
1737 retval = power_on_slot(slot);
1738 if (retval)
1739 goto err_exit;
1740
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001741 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /* configure all functions */
1743 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001744 if (retval)
1745 power_off_slot(slot);
1746 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001747 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001748 power_off_slot(slot);
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001752 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return retval;
1754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756/**
1757 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001758 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 */
1760int acpiphp_disable_slot(struct acpiphp_slot *slot)
1761{
1762 int retval = 0;
1763
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001764 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /* unconfigure all functions */
1767 retval = disable_device(slot);
1768 if (retval)
1769 goto err_exit;
1770
1771 /* power off all functions */
1772 retval = power_off_slot(slot);
1773 if (retval)
1774 goto err_exit;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001777 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return retval;
1779}
1780
1781
1782/*
1783 * slot enabled: 1
1784 * slot disabled: 0
1785 */
1786u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1787{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001788 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
1791
1792/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001793 * latch open: 1
1794 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 */
1796u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1797{
1798 unsigned int sta;
1799
1800 sta = get_slot_status(slot);
1801
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001802 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805
1806/*
1807 * adapter presence : 1
1808 * absence : 0
1809 */
1810u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1811{
1812 unsigned int sta;
1813
1814 sta = get_slot_status(slot);
1815
1816 return (sta == 0) ? 0 : 1;
1817}