blob: db81c08de8d570a4d1b168eb6a962f8cff8c9e55 [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);
Bjorn Helgaasfca68252009-09-14 16:35:10 -060062static void acpiphp_set_hpp_values(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 */
Alex Chiang6edd7672009-09-10 12:34:04 -0600264static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Alex Chiang7f538662009-09-10 12:34:09 -0600266 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900267 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600268 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900269 is_pci_dock_device, (void *)&found, NULL);
270 }
271 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/* decode ACPI 2.0 _HPP hot plug parameters */
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600276static void decode_hpp(struct pci_dev *dev, struct hotplug_params *hpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600280 status = acpi_get_hp_params_from_firmware(dev->bus, hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900281 if (ACPI_FAILURE(status) ||
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600282 !hpp->t0 || (hpp->t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800283 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900284 printk(KERN_WARNING
285 "%s: Could not get hotplug parameters. Use defaults\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800286 __func__);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600287 hpp->t0 = &hpp->type0_data;
288 hpp->t0->revision = 0;
289 hpp->t0->cache_line_size = 0x10;
290 hpp->t0->latency_timer = 0x40;
291 hpp->t0->enable_serr = 0;
292 hpp->t0->enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296
Kristen Accardi783c49f2006-03-03 10:16:05 -0800297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
299static void init_bridge_misc(struct acpiphp_bridge *bridge)
300{
301 acpi_status status;
302
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800303 /* must be added to the list prior to calling register_slot */
304 list_add(&bridge->list, &bridge_list);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* register all slot objects under this bridge */
307 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
308 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800309 if (ACPI_FAILURE(status)) {
310 list_del(&bridge->list);
311 return;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700315 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900316 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
317 status = acpi_remove_notify_handler(bridge->func->handle,
318 ACPI_SYSTEM_NOTIFY,
319 handle_hotplug_event_func);
320 if (ACPI_FAILURE(status))
321 err("failed to remove notify handler\n");
322 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700323 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ACPI_SYSTEM_NOTIFY,
325 handle_hotplug_event_bridge,
326 bridge);
327
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700328 if (ACPI_FAILURE(status)) {
329 err("failed to register interrupt notify handler\n");
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900335/* find acpiphp_func from acpiphp_bridge */
336static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
337{
338 struct list_head *node, *l;
339 struct acpiphp_bridge *bridge;
340 struct acpiphp_slot *slot;
341 struct acpiphp_func *func;
342
343 list_for_each(node, &bridge_list) {
344 bridge = list_entry(node, struct acpiphp_bridge, list);
345 for (slot = bridge->slots; slot; slot = slot->next) {
346 list_for_each(l, &slot->funcs) {
347 func = list_entry(l, struct acpiphp_func,
348 sibling);
349 if (func->handle == handle)
350 return func;
351 }
352 }
353 }
354
355 return NULL;
356}
357
358
359static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
360{
361 acpi_handle dummy_handle;
362
363 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
364 "_STA", &dummy_handle)))
365 bridge->flags |= BRIDGE_HAS_STA;
366
367 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
368 "_EJ0", &dummy_handle)))
369 bridge->flags |= BRIDGE_HAS_EJ0;
370
371 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
372 "_PS0", &dummy_handle)))
373 bridge->flags |= BRIDGE_HAS_PS0;
374
375 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
376 "_PS3", &dummy_handle)))
377 bridge->flags |= BRIDGE_HAS_PS3;
378
379 /* is this ejectable p2p bridge? */
380 if (bridge->flags & BRIDGE_HAS_EJ0) {
381 struct acpiphp_func *func;
382
383 dbg("found ejectable p2p bridge\n");
384
385 /* make link between PCI bridge and PCI function */
386 func = acpiphp_bridge_handle_to_function(bridge->handle);
387 if (!func)
388 return;
389 bridge->func = func;
390 func->bridge = bridge;
391 }
392}
393
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/* allocate and initialize host bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600396static void add_host_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct acpiphp_bridge *bridge;
Alex Chiang6edd7672009-09-10 12:34:04 -0600399 struct acpi_pci_root *root = acpi_pci_find_root(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100401 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (bridge == NULL)
403 return;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 bridge->type = BRIDGE_TYPE_HOST;
406 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Alex Chiang6edd7672009-09-10 12:34:04 -0600408 bridge->pci_bus = root->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 spin_lock_init(&bridge->res_lock);
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 init_bridge_misc(bridge);
413}
414
415
416/* allocate and initialize PCI-to-PCI bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600417static void add_p2p_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100421 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (bridge == NULL) {
423 err("out of memory\n");
424 return;
425 }
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 bridge->type = BRIDGE_TYPE_P2P;
428 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900429 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Alex Chiang6edd7672009-09-10 12:34:04 -0600431 bridge->pci_dev = acpi_get_pci_dev(handle);
432 bridge->pci_bus = bridge->pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (!bridge->pci_bus) {
434 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700435 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600438 /*
439 * Grab a ref to the subordinate PCI bus in case the bus is
440 * removed via PCI core logical hotplug. The ref pins the bus
441 * (which we access during module unload).
442 */
443 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 spin_lock_init(&bridge->res_lock);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700447 return;
448 err:
Alex Chiang6edd7672009-09-10 12:34:04 -0600449 pci_dev_put(bridge->pci_dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700450 kfree(bridge);
451 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454
455/* callback routine to find P2P bridges */
456static acpi_status
457find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
458{
459 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Alex Chiang6edd7672009-09-10 12:34:04 -0600462 dev = acpi_get_pci_dev(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700463 if (!dev || !dev->subordinate)
464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600467 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Alex Chiang6edd7672009-09-10 12:34:04 -0600469 add_p2p_bridge(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900472 /* search P2P bridges under this p2p bridge */
473 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Alex Chiang6edd7672009-09-10 12:34:04 -0600474 find_p2p_bridge, NULL, NULL);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900475 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900476 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900477
Rajesh Shah42f49a62005-04-28 00:25:53 -0700478 out:
479 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return AE_OK;
481}
482
483
484/* find hot-pluggable slots, and then find P2P bridge */
485static int add_bridge(acpi_handle handle)
486{
487 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400488 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 acpi_handle dummy_handle;
490
491 /* if the bridge doesn't have _STA, we assume it is always there */
492 status = acpi_get_handle(handle, "_STA", &dummy_handle);
493 if (ACPI_SUCCESS(status)) {
494 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
495 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800496 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return 0;
498 }
499 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
500 /* don't register this object */
501 return 0;
502 }
503
Rajesh Shah42f49a62005-04-28 00:25:53 -0700504 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600505 if (detect_ejectable_slots(handle) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700506 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
Alex Chiang6edd7672009-09-10 12:34:04 -0600507 add_host_bridge(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* search P2P bridges under this host bridge */
511 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Alex Chiang6edd7672009-09-10 12:34:04 -0600512 find_p2p_bridge, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900515 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 return 0;
518}
519
Rajesh Shah42f49a62005-04-28 00:25:53 -0700520static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
521{
522 struct list_head *head;
523 list_for_each(head, &bridge_list) {
524 struct acpiphp_bridge *bridge = list_entry(head,
525 struct acpiphp_bridge, list);
526 if (bridge->handle == handle)
527 return bridge;
528 }
529
530 return NULL;
531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Rajesh Shah364d5092005-04-28 00:25:54 -0700533static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700535 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700536 struct acpiphp_slot *slot;
537 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700538 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700539
540 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
541 handle_hotplug_event_bridge);
542 if (ACPI_FAILURE(status))
543 err("failed to remove notify handler\n");
544
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900545 if ((bridge->type != BRIDGE_TYPE_HOST) &&
546 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
547 status = acpi_install_notify_handler(bridge->func->handle,
548 ACPI_SYSTEM_NOTIFY,
549 handle_hotplug_event_func,
550 bridge->func);
551 if (ACPI_FAILURE(status))
552 err("failed to install interrupt notify handler\n");
553 }
554
Rajesh Shah42f49a62005-04-28 00:25:53 -0700555 slot = bridge->slots;
556 while (slot) {
557 struct acpiphp_slot *next = slot->next;
558 list_for_each_safe (list, tmp, &slot->funcs) {
559 struct acpiphp_func *func;
560 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400561 if (is_dock_device(func->handle)) {
562 unregister_hotplug_dock_device(func->handle);
563 unregister_dock_notifier(&func->nb);
564 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800565 if (!(func->flags & FUNC_HAS_DCK)) {
566 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700567 ACPI_SYSTEM_NOTIFY,
568 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800569 if (ACPI_FAILURE(status))
570 err("failed to remove notify handler\n");
571 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700572 list_del(list);
573 kfree(func);
574 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800575 acpiphp_unregister_hotplug_slot(slot);
576 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700577 kfree(slot);
578 slot = next;
579 }
580
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600581 /*
582 * Only P2P bridges have a pci_dev
583 */
584 if (bridge->pci_dev)
585 put_device(&bridge->pci_bus->dev);
586
Rajesh Shah42f49a62005-04-28 00:25:53 -0700587 pci_dev_put(bridge->pci_dev);
588 list_del(&bridge->list);
589 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Rajesh Shah364d5092005-04-28 00:25:54 -0700592static acpi_status
593cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
594{
595 struct acpiphp_bridge *bridge;
596
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900597 /* cleanup p2p bridges under this P2P bridge
598 in a depth-first manner */
599 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
600 cleanup_p2p_bridge, NULL, NULL);
601
Alex Chianga13307c2008-07-01 20:02:23 -0600602 bridge = acpiphp_handle_to_bridge(handle);
603 if (bridge)
604 cleanup_bridge(bridge);
605
Rajesh Shah364d5092005-04-28 00:25:54 -0700606 return AE_OK;
607}
608
609static void remove_bridge(acpi_handle handle)
610{
611 struct acpiphp_bridge *bridge;
612
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900613 /* cleanup p2p bridges under this host bridge
614 in a depth-first manner */
615 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
616 (u32)1, cleanup_p2p_bridge, NULL, NULL);
617
Alex Chianga13307c2008-07-01 20:02:23 -0600618 /*
619 * On root bridges with hotplug slots directly underneath (ie,
620 * no p2p bridge inbetween), we call cleanup_bridge().
621 *
622 * The else clause cleans up root bridges that either had no
623 * hotplug slots at all, or had a p2p bridge underneath.
624 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700625 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900626 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700627 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600628 else
629 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
630 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700633static struct pci_dev * get_apic_pci_info(acpi_handle handle)
634{
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700635 struct pci_dev *dev;
636
Alexander Chiangd6aa4842009-06-10 19:55:50 +0000637 dev = acpi_get_pci_dev(handle);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700638 if (!dev)
639 return NULL;
640
641 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
642 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
643 {
644 pci_dev_put(dev);
645 return NULL;
646 }
647
648 return dev;
649}
650
651static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
652{
653 acpi_status status;
654 int result = -1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400655 unsigned long long gsb;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700656 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
657 union acpi_object *obj;
658 void *table;
659
660 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
661 if (ACPI_SUCCESS(status)) {
662 *gsi_base = (u32)gsb;
663 return 0;
664 }
665
666 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
667 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
668 return -1;
669
670 obj = buffer.pointer;
671 if (obj->type != ACPI_TYPE_BUFFER)
672 goto out;
673
674 table = obj->buffer.pointer;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300675 switch (((struct acpi_subtable_header *)table)->type) {
676 case ACPI_MADT_TYPE_IO_SAPIC:
677 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700678 result = 0;
679 break;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300680 case ACPI_MADT_TYPE_IO_APIC:
681 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700682 result = 0;
683 break;
684 default:
685 break;
686 }
687 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700688 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700689 return result;
690}
691
692static acpi_status
693ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
694{
695 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400696 unsigned long long sta;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700697 acpi_handle tmp;
698 struct pci_dev *pdev;
699 u32 gsi_base;
700 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700701 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700702
703 /* Evaluate _STA if present */
704 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
705 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
706 return AE_CTRL_DEPTH;
707
708 /* Scan only PCI bus scope */
709 status = acpi_get_handle(handle, "_HID", &tmp);
710 if (ACPI_SUCCESS(status))
711 return AE_CTRL_DEPTH;
712
713 if (get_gsi_base(handle, &gsi_base))
714 return AE_OK;
715
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700716 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
717 if (!ioapic)
718 return AE_NO_MEMORY;
719
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700720 pdev = get_apic_pci_info(handle);
721 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700722 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700723
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700724 if (pci_enable_device(pdev))
725 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700726
727 pci_set_master(pdev);
728
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700729 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
730 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700731
732 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700733 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
734 goto exit_pci_release_region;
735
736 ioapic->gsi_base = gsi_base;
737 ioapic->dev = pdev;
738 spin_lock(&ioapic_list_lock);
739 list_add_tail(&ioapic->list, &ioapic_list);
740 spin_unlock(&ioapic_list_lock);
741
742 return AE_OK;
743
744 exit_pci_release_region:
745 pci_release_region(pdev, 0);
746 exit_pci_disable_device:
747 pci_disable_device(pdev);
748 exit_pci_dev_put:
749 pci_dev_put(pdev);
750 exit_kfree:
751 kfree(ioapic);
752
753 return AE_OK;
754}
755
756static acpi_status
757ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
758{
759 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400760 unsigned long long sta;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700761 acpi_handle tmp;
762 u32 gsi_base;
763 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
764
765 /* Evaluate _STA if present */
766 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
767 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
768 return AE_CTRL_DEPTH;
769
770 /* Scan only PCI bus scope */
771 status = acpi_get_handle(handle, "_HID", &tmp);
772 if (ACPI_SUCCESS(status))
773 return AE_CTRL_DEPTH;
774
775 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700776 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700777
778 acpi_unregister_ioapic(handle, gsi_base);
779
780 spin_lock(&ioapic_list_lock);
781 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
782 if (pos->gsi_base != gsi_base)
783 continue;
784 ioapic = pos;
785 list_del(&ioapic->list);
786 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700787 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700788 spin_unlock(&ioapic_list_lock);
789
790 if (!ioapic)
791 return AE_OK;
792
793 pci_release_region(ioapic->dev, 0);
794 pci_disable_device(ioapic->dev);
795 pci_dev_put(ioapic->dev);
796 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700797
798 return AE_OK;
799}
800
801static int acpiphp_configure_ioapics(acpi_handle handle)
802{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700803 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700804 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
805 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
806 return 0;
807}
808
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700809static int acpiphp_unconfigure_ioapics(acpi_handle handle)
810{
811 ioapic_remove(handle, 0, NULL, NULL);
812 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
813 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
814 return 0;
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817static int power_on_slot(struct acpiphp_slot *slot)
818{
819 acpi_status status;
820 struct acpiphp_func *func;
821 struct list_head *l;
822 int retval = 0;
823
824 /* if already enabled, just skip */
825 if (slot->flags & SLOT_POWEREDON)
826 goto err_exit;
827
828 list_for_each (l, &slot->funcs) {
829 func = list_entry(l, struct acpiphp_func, sibling);
830
831 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800832 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
834 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800835 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 retval = -1;
837 goto err_exit;
838 } else
839 break;
840 }
841 }
842
843 /* TBD: evaluate _STA to check if the slot is enabled */
844
845 slot->flags |= SLOT_POWEREDON;
846
847 err_exit:
848 return retval;
849}
850
851
852static int power_off_slot(struct acpiphp_slot *slot)
853{
854 acpi_status status;
855 struct acpiphp_func *func;
856 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 int retval = 0;
859
860 /* if already disabled, just skip */
861 if ((slot->flags & SLOT_POWEREDON) == 0)
862 goto err_exit;
863
864 list_for_each (l, &slot->funcs) {
865 func = list_entry(l, struct acpiphp_func, sibling);
866
Rajesh Shah2f523b12005-04-28 00:25:55 -0700867 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
869 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800870 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 retval = -1;
872 goto err_exit;
873 } else
874 break;
875 }
876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* TBD: evaluate _STA to check if the slot is disabled */
879
880 slot->flags &= (~SLOT_POWEREDON);
881
882 err_exit:
883 return retval;
884}
885
886
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800887
888/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800889 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800890 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800891 */
892static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
893{
894 struct list_head *tmp;
895 unsigned char max, n;
896
897 /*
898 * pci_bus_max_busnr will return the highest
899 * reserved busnr for all these children.
900 * that is equivalent to the bus->subordinate
901 * value. We don't want to use the parent's
902 * bus->subordinate value because it could have
903 * padding in it.
904 */
905 max = bus->secondary;
906
907 list_for_each(tmp, &bus->children) {
908 n = pci_bus_max_busnr(pci_bus_b(tmp));
909 if (n > max)
910 max = n;
911 }
912 return max;
913}
914
915
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800916/**
917 * acpiphp_bus_add - add a new bus to acpi subsystem
918 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800919 */
920static int acpiphp_bus_add(struct acpiphp_func *func)
921{
922 acpi_handle phandle;
923 struct acpi_device *device, *pdevice;
924 int ret_val;
925
926 acpi_get_parent(func->handle, &phandle);
927 if (acpi_bus_get_device(phandle, &pdevice)) {
928 dbg("no parent device, assuming NULL\n");
929 pdevice = NULL;
930 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800931 if (!acpi_bus_get_device(func->handle, &device)) {
932 dbg("bus exists... trim\n");
933 /* this shouldn't be in here, so remove
934 * the bus then re-add it...
935 */
936 ret_val = acpi_bus_trim(device, 1);
937 dbg("acpi_bus_trim return %x\n", ret_val);
938 }
939
940 ret_val = acpi_bus_add(&device, pdevice, func->handle,
941 ACPI_BUS_TYPE_DEVICE);
942 if (ret_val) {
943 dbg("error adding bus, %x\n",
944 -ret_val);
945 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800946 }
947 /*
948 * try to start anyway. We could have failed to add
949 * simply because this bus had previously been added
950 * on another add. Don't bother with the return value
951 * we just keep going.
952 */
953 ret_val = acpi_bus_start(device);
954
955acpiphp_bus_add_out:
956 return ret_val;
957}
958
959
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900960/**
961 * acpiphp_bus_trim - trim a bus from acpi subsystem
962 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900963 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700964static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900965{
966 struct acpi_device *device;
967 int retval;
968
969 retval = acpi_bus_get_device(handle, &device);
970 if (retval) {
971 dbg("acpi_device not found\n");
972 return retval;
973 }
974
975 retval = acpi_bus_trim(device, 1);
976 if (retval)
977 err("cannot remove from acpi list\n");
978
979 return retval;
980}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/**
983 * enable_device - enable, configure a slot
984 * @slot: slot to be enabled
985 *
986 * This function should be called per *physical slot*,
987 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100989static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700992 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct list_head *l;
994 struct acpiphp_func *func;
995 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700996 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900997 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (slot->flags & SLOT_ENABLED)
1000 goto err_exit;
1001
1002 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001003 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (dev) {
1005 /* This case shouldn't happen */
1006 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001007 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 retval = -1;
1009 goto err_exit;
1010 }
1011
Rajesh Shah42f49a62005-04-28 00:25:53 -07001012 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1013 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 err("No new device found\n");
1015 retval = -1;
1016 goto err_exit;
1017 }
1018
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001019 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001020 for (pass = 0; pass < 2; pass++) {
1021 list_for_each_entry(dev, &bus->devices, bus_list) {
1022 if (PCI_SLOT(dev->devfn) != slot->device)
1023 continue;
1024 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001025 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001026 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001027 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001028 pci_bus_size_bridges(dev->subordinate);
1029 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001033 list_for_each (l, &slot->funcs) {
1034 func = list_entry(l, struct acpiphp_func, sibling);
1035 acpiphp_bus_add(func);
1036 }
1037
Rajesh Shah42f49a62005-04-28 00:25:53 -07001038 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001039 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001040 acpiphp_set_hpp_values(bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001041 list_for_each_entry(func, &slot->funcs, sibling)
1042 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001043 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001044 pci_bus_add_devices(bus);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 list_for_each (l, &slot->funcs) {
1047 func = list_entry(l, struct acpiphp_func, sibling);
Alex Chiang9d911d72009-05-21 16:21:15 -06001048 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1049 func->function));
1050 if (!dev)
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001051 continue;
1052
Alex Chiang9d911d72009-05-21 16:21:15 -06001053 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1054 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1055 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001056 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -06001057 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001058
1059 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1060 if (ACPI_FAILURE(status))
1061 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1062 status);
Alex Chiang9d911d72009-05-21 16:21:15 -06001063 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 slot->flags |= SLOT_ENABLED;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 err_exit:
1069 return retval;
1070}
1071
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001072static void disable_bridges(struct pci_bus *bus)
1073{
1074 struct pci_dev *dev;
1075 list_for_each_entry(dev, &bus->devices, bus_list) {
1076 if (dev->subordinate) {
1077 disable_bridges(dev->subordinate);
1078 pci_disable_device(dev);
1079 }
1080 }
1081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083/**
1084 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001085 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
1087static int disable_device(struct acpiphp_slot *slot)
1088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -06001090 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /* is this slot already disabled? */
1093 if (!(slot->flags & SLOT_ENABLED))
1094 goto err_exit;
1095
Alex Chiang9d911d72009-05-21 16:21:15 -06001096 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001097 if (func->bridge) {
1098 /* cleanup p2p bridges under this P2P bridge */
1099 cleanup_p2p_bridge(func->bridge->handle,
1100 (u32)1, NULL, NULL);
1101 func->bridge = NULL;
1102 }
1103
Alex Chiang9d911d72009-05-21 16:21:15 -06001104 pdev = pci_get_slot(slot->bridge->pci_bus,
1105 PCI_DEVFN(slot->device, func->function));
1106 if (pdev) {
1107 pci_stop_bus_device(pdev);
1108 if (pdev->subordinate) {
1109 disable_bridges(pdev->subordinate);
1110 pci_disable_device(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001111 }
Alex Chiang9d911d72009-05-21 16:21:15 -06001112 pci_remove_bus_device(pdev);
1113 pci_dev_put(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001114 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001115 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001116
Alex Chiang9d911d72009-05-21 16:21:15 -06001117 list_for_each_entry(func, &slot->funcs, sibling) {
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001118 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001119 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
1122 slot->flags &= (~SLOT_ENABLED);
1123
Alex Chiang9d911d72009-05-21 16:21:15 -06001124err_exit:
1125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128
1129/**
1130 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -08001131 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001133 * If a slot has _STA for each function and if any one of them
1134 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001136 * If a slot doesn't have _STA and if any one of its functions'
1137 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001139 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
1141static unsigned int get_slot_status(struct acpiphp_slot *slot)
1142{
1143 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001144 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 u32 dvid;
1146 struct list_head *l;
1147 struct acpiphp_func *func;
1148
1149 list_for_each (l, &slot->funcs) {
1150 func = list_entry(l, struct acpiphp_func, sibling);
1151
1152 if (func->flags & FUNC_HAS_STA) {
1153 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1154 if (ACPI_SUCCESS(status) && sta)
1155 break;
1156 } else {
1157 pci_bus_read_config_dword(slot->bridge->pci_bus,
1158 PCI_DEVFN(slot->device,
1159 func->function),
1160 PCI_VENDOR_ID, &dvid);
1161 if (dvid != 0xffffffff) {
1162 sta = ACPI_STA_ALL;
1163 break;
1164 }
1165 }
1166 }
1167
1168 return (unsigned int)sta;
1169}
1170
1171/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001172 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001173 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001174 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001175int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001176{
1177 acpi_status status;
1178 struct acpiphp_func *func;
1179 struct list_head *l;
1180 struct acpi_object_list arg_list;
1181 union acpi_object arg;
1182
1183 list_for_each (l, &slot->funcs) {
1184 func = list_entry(l, struct acpiphp_func, sibling);
1185
1186 /* We don't want to call _EJ0 on non-existing functions. */
1187 if ((func->flags & FUNC_HAS_EJ0)) {
1188 /* _EJ0 method take one argument */
1189 arg_list.count = 1;
1190 arg_list.pointer = &arg;
1191 arg.type = ACPI_TYPE_INTEGER;
1192 arg.integer.value = 1;
1193
1194 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1195 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001196 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001197 return -1;
1198 } else
1199 break;
1200 }
1201 }
1202 return 0;
1203}
1204
1205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001207 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *
1209 * Iterate over all slots under this bridge and make sure that if a
1210 * card is present they are enabled, and if not they are disabled.
1211 */
1212static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1213{
1214 struct acpiphp_slot *slot;
1215 int retval = 0;
1216 int enabled, disabled;
1217
1218 enabled = disabled = 0;
1219
1220 for (slot = bridge->slots; slot; slot = slot->next) {
1221 unsigned int status = get_slot_status(slot);
1222 if (slot->flags & SLOT_ENABLED) {
1223 if (status == ACPI_STA_ALL)
1224 continue;
1225 retval = acpiphp_disable_slot(slot);
1226 if (retval) {
1227 err("Error occurred in disabling\n");
1228 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001229 } else {
1230 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 disabled++;
1233 } else {
1234 if (status != ACPI_STA_ALL)
1235 continue;
1236 retval = acpiphp_enable_slot(slot);
1237 if (retval) {
1238 err("Error occurred in enabling\n");
1239 goto err_exit;
1240 }
1241 enabled++;
1242 }
1243 }
1244
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001245 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 err_exit:
1248 return retval;
1249}
1250
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001251static void program_hpp(struct pci_dev *dev, struct hotplug_params *hpp)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001252{
1253 u16 pci_cmd, pci_bctl;
1254 struct pci_dev *cdev;
1255
1256 /* Program hpp values for this device */
1257 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1258 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1259 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1260 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001261
Gary Hade9ef22412007-07-05 11:10:47 -07001262 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1263 return;
1264
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001265 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001266 hpp->t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001267 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001268 hpp->t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001269 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001270 if (hpp->t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001271 pci_cmd |= PCI_COMMAND_SERR;
1272 else
1273 pci_cmd &= ~PCI_COMMAND_SERR;
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001274 if (hpp->t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001275 pci_cmd |= PCI_COMMAND_PARITY;
1276 else
1277 pci_cmd &= ~PCI_COMMAND_PARITY;
1278 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1279
1280 /* Program bridge control value and child devices */
1281 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1282 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001283 hpp->t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001284 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001285 if (hpp->t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001286 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1287 else
1288 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001289 if (hpp->t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001290 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1291 else
1292 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1293 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1294 if (dev->subordinate) {
1295 list_for_each_entry(cdev, &dev->subordinate->devices,
1296 bus_list)
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001297 program_hpp(cdev, hpp);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001298 }
1299 }
1300}
1301
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001302static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001303{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001304 struct pci_dev *dev;
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001305 struct hotplug_params hpp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001306
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001307 list_for_each_entry(dev, &bus->devices, bus_list) {
1308 decode_hpp(dev, &hpp);
1309 program_hpp(dev, &hpp);
1310 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001311}
1312
1313/*
1314 * Remove devices for which we could not assign resources, call
1315 * arch specific code to fix-up the bus
1316 */
1317static void acpiphp_sanitize_bus(struct pci_bus *bus)
1318{
1319 struct pci_dev *dev;
1320 int i;
1321 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1322
1323 list_for_each_entry(dev, &bus->devices, bus_list) {
1324 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1325 struct resource *res = &dev->resource[i];
1326 if ((res->flags & type_mask) && !res->start &&
1327 res->end) {
1328 /* Could not assign a required resources
1329 * for this device, remove it */
1330 pci_remove_bus_device(dev);
1331 break;
1332 }
1333 }
1334 }
1335}
1336
1337/* Program resources in newly inserted bridge */
1338static int acpiphp_configure_bridge (acpi_handle handle)
1339{
Alex Chiang7f538662009-09-10 12:34:09 -06001340 struct pci_bus *bus;
1341
1342 if (acpi_is_root_bridge(handle)) {
1343 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1344 bus = root->bus;
1345 } else {
1346 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1347 bus = pdev->subordinate;
1348 pci_dev_put(pdev);
1349 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001350
1351 pci_bus_size_bridges(bus);
1352 pci_bus_assign_resources(bus);
1353 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001354 acpiphp_set_hpp_values(bus);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001355 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001356 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001357 return 0;
1358}
1359
1360static void handle_bridge_insertion(acpi_handle handle, u32 type)
1361{
1362 struct acpi_device *device, *pdevice;
1363 acpi_handle phandle;
1364
1365 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1366 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1367 err("unexpected notification type %d\n", type);
1368 return;
1369 }
1370
1371 acpi_get_parent(handle, &phandle);
1372 if (acpi_bus_get_device(phandle, &pdevice)) {
1373 dbg("no parent device, assuming NULL\n");
1374 pdevice = NULL;
1375 }
1376 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1377 err("cannot add bridge to acpi list\n");
1378 return;
1379 }
1380 if (!acpiphp_configure_bridge(handle) &&
1381 !acpi_bus_start(device))
1382 add_bridge(handle);
1383 else
1384 err("cannot configure and start bridge\n");
1385
1386}
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/*
1389 * ACPI event handlers
1390 */
1391
Gary Hade0bbd6422007-07-05 11:10:48 -07001392static acpi_status
1393count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1394{
1395 int *count = (int *)context;
1396 struct acpiphp_bridge *bridge;
1397
1398 bridge = acpiphp_handle_to_bridge(handle);
1399 if (bridge)
1400 (*count)++;
1401 return AE_OK ;
1402}
1403
1404static acpi_status
1405check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1406{
1407 struct acpiphp_bridge *bridge;
1408 char objname[64];
1409 struct acpi_buffer buffer = { .length = sizeof(objname),
1410 .pointer = objname };
1411
1412 bridge = acpiphp_handle_to_bridge(handle);
1413 if (bridge) {
1414 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1415 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001416 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001417 acpiphp_check_bridge(bridge);
1418 }
1419 return AE_OK ;
1420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/**
1423 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * @handle: Notify()'ed acpi_handle
1425 * @type: Notify code
1426 * @context: pointer to acpiphp_bridge structure
1427 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001428 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
1430static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1431{
1432 struct acpiphp_bridge *bridge;
1433 char objname[64];
1434 struct acpi_buffer buffer = { .length = sizeof(objname),
1435 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001436 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001437 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001439 if (acpi_bus_get_device(handle, &device)) {
1440 /* This bridge must have just been physically inserted */
1441 handle_bridge_insertion(handle, type);
1442 return;
1443 }
1444
1445 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001446 if (type == ACPI_NOTIFY_BUS_CHECK) {
1447 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1448 count_sub_bridges, &num_sub_bridges, NULL);
1449 }
1450
1451 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001452 err("cannot get bridge info\n");
1453 return;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1457
1458 switch (type) {
1459 case ACPI_NOTIFY_BUS_CHECK:
1460 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001461 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001462 if (bridge) {
1463 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001464 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001465 acpiphp_check_bridge(bridge);
1466 }
1467 if (num_sub_bridges)
1468 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1469 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471
1472 case ACPI_NOTIFY_DEVICE_CHECK:
1473 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001474 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 acpiphp_check_bridge(bridge);
1476 break;
1477
1478 case ACPI_NOTIFY_DEVICE_WAKE:
1479 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001480 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 break;
1482
1483 case ACPI_NOTIFY_EJECT_REQUEST:
1484 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001485 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001486 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1487 (bridge->flags & BRIDGE_HAS_EJ0)) {
1488 struct acpiphp_slot *slot;
1489 slot = bridge->func->slot;
1490 if (!acpiphp_disable_slot(slot))
1491 acpiphp_eject_slot(slot);
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494
1495 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1496 printk(KERN_ERR "Device %s cannot be configured due"
1497 " to a frequency mismatch\n", objname);
1498 break;
1499
1500 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1501 printk(KERN_ERR "Device %s cannot be configured due"
1502 " to a bus mode mismatch\n", objname);
1503 break;
1504
1505 case ACPI_NOTIFY_POWER_FAULT:
1506 printk(KERN_ERR "Device %s has suffered a power fault\n",
1507 objname);
1508 break;
1509
1510 default:
1511 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1512 break;
1513 }
1514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/**
1517 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * @handle: Notify()'ed acpi_handle
1519 * @type: Notify code
1520 * @context: pointer to acpiphp_func structure
1521 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001522 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 */
Len Brown2b85e132006-06-27 01:50:14 -04001524static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 struct acpiphp_func *func;
1527 char objname[64];
1528 struct acpi_buffer buffer = { .length = sizeof(objname),
1529 .pointer = objname };
1530
1531 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1532
1533 func = (struct acpiphp_func *)context;
1534
1535 switch (type) {
1536 case ACPI_NOTIFY_BUS_CHECK:
1537 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001538 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 acpiphp_enable_slot(func->slot);
1540 break;
1541
1542 case ACPI_NOTIFY_DEVICE_CHECK:
1543 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001544 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 acpiphp_check_bridge(func->slot->bridge);
1546 break;
1547
1548 case ACPI_NOTIFY_DEVICE_WAKE:
1549 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001550 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 break;
1552
1553 case ACPI_NOTIFY_EJECT_REQUEST:
1554 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001555 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001556 if (!(acpiphp_disable_slot(func->slot)))
1557 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 break;
1559
1560 default:
1561 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1562 break;
1563 }
1564}
1565
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001566
1567static acpi_status
1568find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1569{
1570 int *count = (int *)context;
1571
Alexander Chiang27558202009-06-10 19:55:14 +00001572 if (acpi_is_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001573 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1574 handle_hotplug_event_bridge, NULL);
1575 (*count)++;
1576 }
1577 return AE_OK ;
1578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580static struct acpi_pci_driver acpi_pci_hp_driver = {
1581 .add = add_bridge,
1582 .remove = remove_bridge,
1583};
1584
1585/**
1586 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 */
1588int __init acpiphp_glue_init(void)
1589{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001590 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001592 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1593 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 if (num <= 0)
1596 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001597 else
1598 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 return 0;
1601}
1602
1603
1604/**
1605 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1606 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001607 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001609void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1612}
1613
1614
1615/**
1616 * acpiphp_get_num_slots - count number of slots in a system
1617 */
1618int __init acpiphp_get_num_slots(void)
1619{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001621 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Akinobu Mita467c4422006-10-30 13:07:58 -08001623 list_for_each_entry (bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001624 dbg("Bus %04x:%02x has %d slot%s\n",
1625 pci_domain_nr(bridge->pci_bus),
1626 bridge->pci_bus->number, bridge->nr_slots,
1627 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 num_slots += bridge->nr_slots;
1629 }
1630
Rajesh Shah42f49a62005-04-28 00:25:53 -07001631 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return num_slots;
1633}
1634
1635
1636#if 0
1637/**
1638 * acpiphp_for_each_slot - call function for each slot
1639 * @fn: callback function
1640 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
1642static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1643{
1644 struct list_head *node;
1645 struct acpiphp_bridge *bridge;
1646 struct acpiphp_slot *slot;
1647 int retval = 0;
1648
1649 list_for_each (node, &bridge_list) {
1650 bridge = (struct acpiphp_bridge *)node;
1651 for (slot = bridge->slots; slot; slot = slot->next) {
1652 retval = fn(slot, data);
1653 if (!retval)
1654 goto err_exit;
1655 }
1656 }
1657
1658 err_exit:
1659 return retval;
1660}
1661#endif
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664/**
1665 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001666 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
1668int acpiphp_enable_slot(struct acpiphp_slot *slot)
1669{
1670 int retval;
1671
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001672 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /* wake up all functions */
1675 retval = power_on_slot(slot);
1676 if (retval)
1677 goto err_exit;
1678
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001679 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* configure all functions */
1681 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001682 if (retval)
1683 power_off_slot(slot);
1684 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001685 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001686 power_off_slot(slot);
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001690 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return retval;
1692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694/**
1695 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001696 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 */
1698int acpiphp_disable_slot(struct acpiphp_slot *slot)
1699{
1700 int retval = 0;
1701
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001702 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /* unconfigure all functions */
1705 retval = disable_device(slot);
1706 if (retval)
1707 goto err_exit;
1708
1709 /* power off all functions */
1710 retval = power_off_slot(slot);
1711 if (retval)
1712 goto err_exit;
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001715 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return retval;
1717}
1718
1719
1720/*
1721 * slot enabled: 1
1722 * slot disabled: 0
1723 */
1724u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1725{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001726 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729
1730/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001731 * latch open: 1
1732 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
1734u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1735{
1736 unsigned int sta;
1737
1738 sta = get_slot_status(slot);
1739
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001740 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743
1744/*
1745 * adapter presence : 1
1746 * absence : 0
1747 */
1748u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1749{
1750 unsigned int sta;
1751
1752 sta = get_slot_status(slot);
1753
1754 return (sta == 0) ? 0 : 1;
1755}