blob: 680c33635b6f3a21e3824f81fc2b7e7be678aab3 [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 */
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 */
276static void decode_hpp(struct acpiphp_bridge *bridge)
277{
278 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900280 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900281 if (ACPI_FAILURE(status) ||
282 !bridge->hpp.t0 || (bridge->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__);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900287 bridge->hpp.t0 = &bridge->hpp.type0_data;
288 bridge->hpp.t0->revision = 0;
289 bridge->hpp.t0->cache_line_size = 0x10;
290 bridge->hpp.t0->latency_timer = 0x40;
291 bridge->hpp.t0->enable_serr = 0;
292 bridge->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
303 /* decode ACPI 2.0 _HPP (hot plug parameters) */
304 decode_hpp(bridge);
305
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800306 /* must be added to the list prior to calling register_slot */
307 list_add(&bridge->list, &bridge_list);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* register all slot objects under this bridge */
310 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
311 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800312 if (ACPI_FAILURE(status)) {
313 list_del(&bridge->list);
314 return;
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700318 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900319 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
320 status = acpi_remove_notify_handler(bridge->func->handle,
321 ACPI_SYSTEM_NOTIFY,
322 handle_hotplug_event_func);
323 if (ACPI_FAILURE(status))
324 err("failed to remove notify handler\n");
325 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700326 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 ACPI_SYSTEM_NOTIFY,
328 handle_hotplug_event_bridge,
329 bridge);
330
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700331 if (ACPI_FAILURE(status)) {
332 err("failed to register interrupt notify handler\n");
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900338/* find acpiphp_func from acpiphp_bridge */
339static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
340{
341 struct list_head *node, *l;
342 struct acpiphp_bridge *bridge;
343 struct acpiphp_slot *slot;
344 struct acpiphp_func *func;
345
346 list_for_each(node, &bridge_list) {
347 bridge = list_entry(node, struct acpiphp_bridge, list);
348 for (slot = bridge->slots; slot; slot = slot->next) {
349 list_for_each(l, &slot->funcs) {
350 func = list_entry(l, struct acpiphp_func,
351 sibling);
352 if (func->handle == handle)
353 return func;
354 }
355 }
356 }
357
358 return NULL;
359}
360
361
362static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
363{
364 acpi_handle dummy_handle;
365
366 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
367 "_STA", &dummy_handle)))
368 bridge->flags |= BRIDGE_HAS_STA;
369
370 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
371 "_EJ0", &dummy_handle)))
372 bridge->flags |= BRIDGE_HAS_EJ0;
373
374 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
375 "_PS0", &dummy_handle)))
376 bridge->flags |= BRIDGE_HAS_PS0;
377
378 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
379 "_PS3", &dummy_handle)))
380 bridge->flags |= BRIDGE_HAS_PS3;
381
382 /* is this ejectable p2p bridge? */
383 if (bridge->flags & BRIDGE_HAS_EJ0) {
384 struct acpiphp_func *func;
385
386 dbg("found ejectable p2p bridge\n");
387
388 /* make link between PCI bridge and PCI function */
389 func = acpiphp_bridge_handle_to_function(bridge->handle);
390 if (!func)
391 return;
392 bridge->func = func;
393 func->bridge = bridge;
394 }
395}
396
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/* allocate and initialize host bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600399static void add_host_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct acpiphp_bridge *bridge;
Alex Chiang6edd7672009-09-10 12:34:04 -0600402 struct acpi_pci_root *root = acpi_pci_find_root(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100404 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (bridge == NULL)
406 return;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 bridge->type = BRIDGE_TYPE_HOST;
409 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alex Chiang6edd7672009-09-10 12:34:04 -0600411 bridge->pci_bus = root->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 spin_lock_init(&bridge->res_lock);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 init_bridge_misc(bridge);
416}
417
418
419/* allocate and initialize PCI-to-PCI bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600420static void add_p2p_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100424 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (bridge == NULL) {
426 err("out of memory\n");
427 return;
428 }
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 bridge->type = BRIDGE_TYPE_P2P;
431 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900432 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Alex Chiang6edd7672009-09-10 12:34:04 -0600434 bridge->pci_dev = acpi_get_pci_dev(handle);
435 bridge->pci_bus = bridge->pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!bridge->pci_bus) {
437 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700438 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600441 /*
442 * Grab a ref to the subordinate PCI bus in case the bus is
443 * removed via PCI core logical hotplug. The ref pins the bus
444 * (which we access during module unload).
445 */
446 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 spin_lock_init(&bridge->res_lock);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700450 return;
451 err:
Alex Chiang6edd7672009-09-10 12:34:04 -0600452 pci_dev_put(bridge->pci_dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700453 kfree(bridge);
454 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457
458/* callback routine to find P2P bridges */
459static acpi_status
460find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
461{
462 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Alex Chiang6edd7672009-09-10 12:34:04 -0600465 dev = acpi_get_pci_dev(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700466 if (!dev || !dev->subordinate)
467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600470 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Alex Chiang6edd7672009-09-10 12:34:04 -0600472 add_p2p_bridge(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900475 /* search P2P bridges under this p2p bridge */
476 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Alex Chiang6edd7672009-09-10 12:34:04 -0600477 find_p2p_bridge, NULL, NULL);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900478 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900479 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900480
Rajesh Shah42f49a62005-04-28 00:25:53 -0700481 out:
482 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return AE_OK;
484}
485
486
487/* find hot-pluggable slots, and then find P2P bridge */
488static int add_bridge(acpi_handle handle)
489{
490 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400491 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 acpi_handle dummy_handle;
493
494 /* if the bridge doesn't have _STA, we assume it is always there */
495 status = acpi_get_handle(handle, "_STA", &dummy_handle);
496 if (ACPI_SUCCESS(status)) {
497 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
498 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800499 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501 }
502 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
503 /* don't register this object */
504 return 0;
505 }
506
Rajesh Shah42f49a62005-04-28 00:25:53 -0700507 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600508 if (detect_ejectable_slots(handle) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700509 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
Alex Chiang6edd7672009-09-10 12:34:04 -0600510 add_host_bridge(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* search P2P bridges under this host bridge */
514 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Alex Chiang6edd7672009-09-10 12:34:04 -0600515 find_p2p_bridge, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900518 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 return 0;
521}
522
Rajesh Shah42f49a62005-04-28 00:25:53 -0700523static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
524{
525 struct list_head *head;
526 list_for_each(head, &bridge_list) {
527 struct acpiphp_bridge *bridge = list_entry(head,
528 struct acpiphp_bridge, list);
529 if (bridge->handle == handle)
530 return bridge;
531 }
532
533 return NULL;
534}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Rajesh Shah364d5092005-04-28 00:25:54 -0700536static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700538 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700539 struct acpiphp_slot *slot;
540 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700541 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700542
543 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
544 handle_hotplug_event_bridge);
545 if (ACPI_FAILURE(status))
546 err("failed to remove notify handler\n");
547
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900548 if ((bridge->type != BRIDGE_TYPE_HOST) &&
549 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
550 status = acpi_install_notify_handler(bridge->func->handle,
551 ACPI_SYSTEM_NOTIFY,
552 handle_hotplug_event_func,
553 bridge->func);
554 if (ACPI_FAILURE(status))
555 err("failed to install interrupt notify handler\n");
556 }
557
Rajesh Shah42f49a62005-04-28 00:25:53 -0700558 slot = bridge->slots;
559 while (slot) {
560 struct acpiphp_slot *next = slot->next;
561 list_for_each_safe (list, tmp, &slot->funcs) {
562 struct acpiphp_func *func;
563 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400564 if (is_dock_device(func->handle)) {
565 unregister_hotplug_dock_device(func->handle);
566 unregister_dock_notifier(&func->nb);
567 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800568 if (!(func->flags & FUNC_HAS_DCK)) {
569 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700570 ACPI_SYSTEM_NOTIFY,
571 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800572 if (ACPI_FAILURE(status))
573 err("failed to remove notify handler\n");
574 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700575 list_del(list);
576 kfree(func);
577 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800578 acpiphp_unregister_hotplug_slot(slot);
579 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700580 kfree(slot);
581 slot = next;
582 }
583
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600584 /*
585 * Only P2P bridges have a pci_dev
586 */
587 if (bridge->pci_dev)
588 put_device(&bridge->pci_bus->dev);
589
Rajesh Shah42f49a62005-04-28 00:25:53 -0700590 pci_dev_put(bridge->pci_dev);
591 list_del(&bridge->list);
592 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Rajesh Shah364d5092005-04-28 00:25:54 -0700595static acpi_status
596cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
597{
598 struct acpiphp_bridge *bridge;
599
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900600 /* cleanup p2p bridges under this P2P bridge
601 in a depth-first manner */
602 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
603 cleanup_p2p_bridge, NULL, NULL);
604
Alex Chianga13307c2008-07-01 20:02:23 -0600605 bridge = acpiphp_handle_to_bridge(handle);
606 if (bridge)
607 cleanup_bridge(bridge);
608
Rajesh Shah364d5092005-04-28 00:25:54 -0700609 return AE_OK;
610}
611
612static void remove_bridge(acpi_handle handle)
613{
614 struct acpiphp_bridge *bridge;
615
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900616 /* cleanup p2p bridges under this host bridge
617 in a depth-first manner */
618 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
619 (u32)1, cleanup_p2p_bridge, NULL, NULL);
620
Alex Chianga13307c2008-07-01 20:02:23 -0600621 /*
622 * On root bridges with hotplug slots directly underneath (ie,
623 * no p2p bridge inbetween), we call cleanup_bridge().
624 *
625 * The else clause cleans up root bridges that either had no
626 * hotplug slots at all, or had a p2p bridge underneath.
627 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700628 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900629 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700630 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600631 else
632 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
633 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700634}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700636static struct pci_dev * get_apic_pci_info(acpi_handle handle)
637{
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700638 struct pci_dev *dev;
639
Alexander Chiangd6aa4842009-06-10 19:55:50 +0000640 dev = acpi_get_pci_dev(handle);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700641 if (!dev)
642 return NULL;
643
644 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
645 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
646 {
647 pci_dev_put(dev);
648 return NULL;
649 }
650
651 return dev;
652}
653
654static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
655{
656 acpi_status status;
657 int result = -1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400658 unsigned long long gsb;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700659 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
660 union acpi_object *obj;
661 void *table;
662
663 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
664 if (ACPI_SUCCESS(status)) {
665 *gsi_base = (u32)gsb;
666 return 0;
667 }
668
669 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
670 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
671 return -1;
672
673 obj = buffer.pointer;
674 if (obj->type != ACPI_TYPE_BUFFER)
675 goto out;
676
677 table = obj->buffer.pointer;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300678 switch (((struct acpi_subtable_header *)table)->type) {
679 case ACPI_MADT_TYPE_IO_SAPIC:
680 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700681 result = 0;
682 break;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300683 case ACPI_MADT_TYPE_IO_APIC:
684 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700685 result = 0;
686 break;
687 default:
688 break;
689 }
690 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700691 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700692 return result;
693}
694
695static acpi_status
696ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
697{
698 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400699 unsigned long long sta;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700700 acpi_handle tmp;
701 struct pci_dev *pdev;
702 u32 gsi_base;
703 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700704 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700705
706 /* Evaluate _STA if present */
707 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
708 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
709 return AE_CTRL_DEPTH;
710
711 /* Scan only PCI bus scope */
712 status = acpi_get_handle(handle, "_HID", &tmp);
713 if (ACPI_SUCCESS(status))
714 return AE_CTRL_DEPTH;
715
716 if (get_gsi_base(handle, &gsi_base))
717 return AE_OK;
718
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700719 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
720 if (!ioapic)
721 return AE_NO_MEMORY;
722
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700723 pdev = get_apic_pci_info(handle);
724 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700725 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700726
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700727 if (pci_enable_device(pdev))
728 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700729
730 pci_set_master(pdev);
731
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700732 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
733 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700734
735 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700736 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
737 goto exit_pci_release_region;
738
739 ioapic->gsi_base = gsi_base;
740 ioapic->dev = pdev;
741 spin_lock(&ioapic_list_lock);
742 list_add_tail(&ioapic->list, &ioapic_list);
743 spin_unlock(&ioapic_list_lock);
744
745 return AE_OK;
746
747 exit_pci_release_region:
748 pci_release_region(pdev, 0);
749 exit_pci_disable_device:
750 pci_disable_device(pdev);
751 exit_pci_dev_put:
752 pci_dev_put(pdev);
753 exit_kfree:
754 kfree(ioapic);
755
756 return AE_OK;
757}
758
759static acpi_status
760ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
761{
762 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400763 unsigned long long sta;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700764 acpi_handle tmp;
765 u32 gsi_base;
766 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
767
768 /* Evaluate _STA if present */
769 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
770 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
771 return AE_CTRL_DEPTH;
772
773 /* Scan only PCI bus scope */
774 status = acpi_get_handle(handle, "_HID", &tmp);
775 if (ACPI_SUCCESS(status))
776 return AE_CTRL_DEPTH;
777
778 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700779 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700780
781 acpi_unregister_ioapic(handle, gsi_base);
782
783 spin_lock(&ioapic_list_lock);
784 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
785 if (pos->gsi_base != gsi_base)
786 continue;
787 ioapic = pos;
788 list_del(&ioapic->list);
789 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700790 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700791 spin_unlock(&ioapic_list_lock);
792
793 if (!ioapic)
794 return AE_OK;
795
796 pci_release_region(ioapic->dev, 0);
797 pci_disable_device(ioapic->dev);
798 pci_dev_put(ioapic->dev);
799 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700800
801 return AE_OK;
802}
803
804static int acpiphp_configure_ioapics(acpi_handle handle)
805{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700806 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700807 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
808 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
809 return 0;
810}
811
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700812static int acpiphp_unconfigure_ioapics(acpi_handle handle)
813{
814 ioapic_remove(handle, 0, NULL, NULL);
815 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
816 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
817 return 0;
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static int power_on_slot(struct acpiphp_slot *slot)
821{
822 acpi_status status;
823 struct acpiphp_func *func;
824 struct list_head *l;
825 int retval = 0;
826
827 /* if already enabled, just skip */
828 if (slot->flags & SLOT_POWEREDON)
829 goto err_exit;
830
831 list_for_each (l, &slot->funcs) {
832 func = list_entry(l, struct acpiphp_func, sibling);
833
834 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800835 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
837 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800838 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 retval = -1;
840 goto err_exit;
841 } else
842 break;
843 }
844 }
845
846 /* TBD: evaluate _STA to check if the slot is enabled */
847
848 slot->flags |= SLOT_POWEREDON;
849
850 err_exit:
851 return retval;
852}
853
854
855static int power_off_slot(struct acpiphp_slot *slot)
856{
857 acpi_status status;
858 struct acpiphp_func *func;
859 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 int retval = 0;
862
863 /* if already disabled, just skip */
864 if ((slot->flags & SLOT_POWEREDON) == 0)
865 goto err_exit;
866
867 list_for_each (l, &slot->funcs) {
868 func = list_entry(l, struct acpiphp_func, sibling);
869
Rajesh Shah2f523b12005-04-28 00:25:55 -0700870 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
872 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800873 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 retval = -1;
875 goto err_exit;
876 } else
877 break;
878 }
879 }
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* TBD: evaluate _STA to check if the slot is disabled */
882
883 slot->flags &= (~SLOT_POWEREDON);
884
885 err_exit:
886 return retval;
887}
888
889
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800890
891/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800892 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800893 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800894 */
895static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
896{
897 struct list_head *tmp;
898 unsigned char max, n;
899
900 /*
901 * pci_bus_max_busnr will return the highest
902 * reserved busnr for all these children.
903 * that is equivalent to the bus->subordinate
904 * value. We don't want to use the parent's
905 * bus->subordinate value because it could have
906 * padding in it.
907 */
908 max = bus->secondary;
909
910 list_for_each(tmp, &bus->children) {
911 n = pci_bus_max_busnr(pci_bus_b(tmp));
912 if (n > max)
913 max = n;
914 }
915 return max;
916}
917
918
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800919/**
920 * acpiphp_bus_add - add a new bus to acpi subsystem
921 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800922 */
923static int acpiphp_bus_add(struct acpiphp_func *func)
924{
925 acpi_handle phandle;
926 struct acpi_device *device, *pdevice;
927 int ret_val;
928
929 acpi_get_parent(func->handle, &phandle);
930 if (acpi_bus_get_device(phandle, &pdevice)) {
931 dbg("no parent device, assuming NULL\n");
932 pdevice = NULL;
933 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800934 if (!acpi_bus_get_device(func->handle, &device)) {
935 dbg("bus exists... trim\n");
936 /* this shouldn't be in here, so remove
937 * the bus then re-add it...
938 */
939 ret_val = acpi_bus_trim(device, 1);
940 dbg("acpi_bus_trim return %x\n", ret_val);
941 }
942
943 ret_val = acpi_bus_add(&device, pdevice, func->handle,
944 ACPI_BUS_TYPE_DEVICE);
945 if (ret_val) {
946 dbg("error adding bus, %x\n",
947 -ret_val);
948 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800949 }
950 /*
951 * try to start anyway. We could have failed to add
952 * simply because this bus had previously been added
953 * on another add. Don't bother with the return value
954 * we just keep going.
955 */
956 ret_val = acpi_bus_start(device);
957
958acpiphp_bus_add_out:
959 return ret_val;
960}
961
962
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900963/**
964 * acpiphp_bus_trim - trim a bus from acpi subsystem
965 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900966 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700967static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900968{
969 struct acpi_device *device;
970 int retval;
971
972 retval = acpi_bus_get_device(handle, &device);
973 if (retval) {
974 dbg("acpi_device not found\n");
975 return retval;
976 }
977
978 retval = acpi_bus_trim(device, 1);
979 if (retval)
980 err("cannot remove from acpi list\n");
981
982 return retval;
983}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/**
986 * enable_device - enable, configure a slot
987 * @slot: slot to be enabled
988 *
989 * This function should be called per *physical slot*,
990 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100992static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700995 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct list_head *l;
997 struct acpiphp_func *func;
998 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700999 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001000 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (slot->flags & SLOT_ENABLED)
1003 goto err_exit;
1004
1005 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001006 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (dev) {
1008 /* This case shouldn't happen */
1009 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001010 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 retval = -1;
1012 goto err_exit;
1013 }
1014
Rajesh Shah42f49a62005-04-28 00:25:53 -07001015 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1016 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 err("No new device found\n");
1018 retval = -1;
1019 goto err_exit;
1020 }
1021
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001022 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001023 for (pass = 0; pass < 2; pass++) {
1024 list_for_each_entry(dev, &bus->devices, bus_list) {
1025 if (PCI_SLOT(dev->devfn) != slot->device)
1026 continue;
1027 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001028 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001029 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001030 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001031 pci_bus_size_bridges(dev->subordinate);
1032 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001036 list_for_each (l, &slot->funcs) {
1037 func = list_entry(l, struct acpiphp_func, sibling);
1038 acpiphp_bus_add(func);
1039 }
1040
Rajesh Shah42f49a62005-04-28 00:25:53 -07001041 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001042 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001043 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001044 list_for_each_entry(func, &slot->funcs, sibling)
1045 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001046 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001047 pci_bus_add_devices(bus);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 list_for_each (l, &slot->funcs) {
1050 func = list_entry(l, struct acpiphp_func, sibling);
Alex Chiang9d911d72009-05-21 16:21:15 -06001051 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1052 func->function));
1053 if (!dev)
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001054 continue;
1055
Alex Chiang9d911d72009-05-21 16:21:15 -06001056 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1057 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1058 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001059 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -06001060 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001061
1062 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1063 if (ACPI_FAILURE(status))
1064 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1065 status);
Alex Chiang9d911d72009-05-21 16:21:15 -06001066 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
1069 slot->flags |= SLOT_ENABLED;
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 err_exit:
1072 return retval;
1073}
1074
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001075static void disable_bridges(struct pci_bus *bus)
1076{
1077 struct pci_dev *dev;
1078 list_for_each_entry(dev, &bus->devices, bus_list) {
1079 if (dev->subordinate) {
1080 disable_bridges(dev->subordinate);
1081 pci_disable_device(dev);
1082 }
1083 }
1084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086/**
1087 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001088 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
1090static int disable_device(struct acpiphp_slot *slot)
1091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -06001093 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /* is this slot already disabled? */
1096 if (!(slot->flags & SLOT_ENABLED))
1097 goto err_exit;
1098
Alex Chiang9d911d72009-05-21 16:21:15 -06001099 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001100 if (func->bridge) {
1101 /* cleanup p2p bridges under this P2P bridge */
1102 cleanup_p2p_bridge(func->bridge->handle,
1103 (u32)1, NULL, NULL);
1104 func->bridge = NULL;
1105 }
1106
Alex Chiang9d911d72009-05-21 16:21:15 -06001107 pdev = pci_get_slot(slot->bridge->pci_bus,
1108 PCI_DEVFN(slot->device, func->function));
1109 if (pdev) {
1110 pci_stop_bus_device(pdev);
1111 if (pdev->subordinate) {
1112 disable_bridges(pdev->subordinate);
1113 pci_disable_device(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001114 }
Alex Chiang9d911d72009-05-21 16:21:15 -06001115 pci_remove_bus_device(pdev);
1116 pci_dev_put(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001117 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001118 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001119
Alex Chiang9d911d72009-05-21 16:21:15 -06001120 list_for_each_entry(func, &slot->funcs, sibling) {
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001121 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001122 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 slot->flags &= (~SLOT_ENABLED);
1126
Alex Chiang9d911d72009-05-21 16:21:15 -06001127err_exit:
1128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131
1132/**
1133 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -08001134 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001136 * If a slot has _STA for each function and if any one of them
1137 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001139 * If a slot doesn't have _STA and if any one of its functions'
1140 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001142 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
1144static unsigned int get_slot_status(struct acpiphp_slot *slot)
1145{
1146 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001147 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 u32 dvid;
1149 struct list_head *l;
1150 struct acpiphp_func *func;
1151
1152 list_for_each (l, &slot->funcs) {
1153 func = list_entry(l, struct acpiphp_func, sibling);
1154
1155 if (func->flags & FUNC_HAS_STA) {
1156 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1157 if (ACPI_SUCCESS(status) && sta)
1158 break;
1159 } else {
1160 pci_bus_read_config_dword(slot->bridge->pci_bus,
1161 PCI_DEVFN(slot->device,
1162 func->function),
1163 PCI_VENDOR_ID, &dvid);
1164 if (dvid != 0xffffffff) {
1165 sta = ACPI_STA_ALL;
1166 break;
1167 }
1168 }
1169 }
1170
1171 return (unsigned int)sta;
1172}
1173
1174/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001175 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001176 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001177 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001178int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001179{
1180 acpi_status status;
1181 struct acpiphp_func *func;
1182 struct list_head *l;
1183 struct acpi_object_list arg_list;
1184 union acpi_object arg;
1185
1186 list_for_each (l, &slot->funcs) {
1187 func = list_entry(l, struct acpiphp_func, sibling);
1188
1189 /* We don't want to call _EJ0 on non-existing functions. */
1190 if ((func->flags & FUNC_HAS_EJ0)) {
1191 /* _EJ0 method take one argument */
1192 arg_list.count = 1;
1193 arg_list.pointer = &arg;
1194 arg.type = ACPI_TYPE_INTEGER;
1195 arg.integer.value = 1;
1196
1197 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1198 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001199 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001200 return -1;
1201 } else
1202 break;
1203 }
1204 }
1205 return 0;
1206}
1207
1208/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001210 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *
1212 * Iterate over all slots under this bridge and make sure that if a
1213 * card is present they are enabled, and if not they are disabled.
1214 */
1215static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1216{
1217 struct acpiphp_slot *slot;
1218 int retval = 0;
1219 int enabled, disabled;
1220
1221 enabled = disabled = 0;
1222
1223 for (slot = bridge->slots; slot; slot = slot->next) {
1224 unsigned int status = get_slot_status(slot);
1225 if (slot->flags & SLOT_ENABLED) {
1226 if (status == ACPI_STA_ALL)
1227 continue;
1228 retval = acpiphp_disable_slot(slot);
1229 if (retval) {
1230 err("Error occurred in disabling\n");
1231 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001232 } else {
1233 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 disabled++;
1236 } else {
1237 if (status != ACPI_STA_ALL)
1238 continue;
1239 retval = acpiphp_enable_slot(slot);
1240 if (retval) {
1241 err("Error occurred in enabling\n");
1242 goto err_exit;
1243 }
1244 enabled++;
1245 }
1246 }
1247
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001248 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 err_exit:
1251 return retval;
1252}
1253
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001254static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1255{
1256 u16 pci_cmd, pci_bctl;
1257 struct pci_dev *cdev;
1258
1259 /* Program hpp values for this device */
1260 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1261 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1262 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1263 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001264
Gary Hade9ef22412007-07-05 11:10:47 -07001265 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1266 return;
1267
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001268 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001269 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001270 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001271 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001272 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001273 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001274 pci_cmd |= PCI_COMMAND_SERR;
1275 else
1276 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001277 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001278 pci_cmd |= PCI_COMMAND_PARITY;
1279 else
1280 pci_cmd &= ~PCI_COMMAND_PARITY;
1281 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1282
1283 /* Program bridge control value and child devices */
1284 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1285 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001286 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001287 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001288 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001289 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1290 else
1291 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001292 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001293 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1294 else
1295 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1296 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1297 if (dev->subordinate) {
1298 list_for_each_entry(cdev, &dev->subordinate->devices,
1299 bus_list)
1300 program_hpp(cdev, bridge);
1301 }
1302 }
1303}
1304
1305static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1306{
1307 struct acpiphp_bridge bridge;
1308 struct pci_dev *dev;
1309
1310 memset(&bridge, 0, sizeof(bridge));
1311 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001312 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001313 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001314 decode_hpp(&bridge);
1315 list_for_each_entry(dev, &bus->devices, bus_list)
1316 program_hpp(dev, &bridge);
1317
1318}
1319
1320/*
1321 * Remove devices for which we could not assign resources, call
1322 * arch specific code to fix-up the bus
1323 */
1324static void acpiphp_sanitize_bus(struct pci_bus *bus)
1325{
1326 struct pci_dev *dev;
1327 int i;
1328 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1329
1330 list_for_each_entry(dev, &bus->devices, bus_list) {
1331 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1332 struct resource *res = &dev->resource[i];
1333 if ((res->flags & type_mask) && !res->start &&
1334 res->end) {
1335 /* Could not assign a required resources
1336 * for this device, remove it */
1337 pci_remove_bus_device(dev);
1338 break;
1339 }
1340 }
1341 }
1342}
1343
1344/* Program resources in newly inserted bridge */
1345static int acpiphp_configure_bridge (acpi_handle handle)
1346{
Alex Chiang7f538662009-09-10 12:34:09 -06001347 struct pci_bus *bus;
1348
1349 if (acpi_is_root_bridge(handle)) {
1350 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1351 bus = root->bus;
1352 } else {
1353 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1354 bus = pdev->subordinate;
1355 pci_dev_put(pdev);
1356 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001357
1358 pci_bus_size_bridges(bus);
1359 pci_bus_assign_resources(bus);
1360 acpiphp_sanitize_bus(bus);
1361 acpiphp_set_hpp_values(handle, bus);
1362 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001363 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001364 return 0;
1365}
1366
1367static void handle_bridge_insertion(acpi_handle handle, u32 type)
1368{
1369 struct acpi_device *device, *pdevice;
1370 acpi_handle phandle;
1371
1372 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1373 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1374 err("unexpected notification type %d\n", type);
1375 return;
1376 }
1377
1378 acpi_get_parent(handle, &phandle);
1379 if (acpi_bus_get_device(phandle, &pdevice)) {
1380 dbg("no parent device, assuming NULL\n");
1381 pdevice = NULL;
1382 }
1383 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1384 err("cannot add bridge to acpi list\n");
1385 return;
1386 }
1387 if (!acpiphp_configure_bridge(handle) &&
1388 !acpi_bus_start(device))
1389 add_bridge(handle);
1390 else
1391 err("cannot configure and start bridge\n");
1392
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/*
1396 * ACPI event handlers
1397 */
1398
Gary Hade0bbd6422007-07-05 11:10:48 -07001399static acpi_status
1400count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1401{
1402 int *count = (int *)context;
1403 struct acpiphp_bridge *bridge;
1404
1405 bridge = acpiphp_handle_to_bridge(handle);
1406 if (bridge)
1407 (*count)++;
1408 return AE_OK ;
1409}
1410
1411static acpi_status
1412check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1413{
1414 struct acpiphp_bridge *bridge;
1415 char objname[64];
1416 struct acpi_buffer buffer = { .length = sizeof(objname),
1417 .pointer = objname };
1418
1419 bridge = acpiphp_handle_to_bridge(handle);
1420 if (bridge) {
1421 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1422 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001423 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001424 acpiphp_check_bridge(bridge);
1425 }
1426 return AE_OK ;
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/**
1430 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 * @handle: Notify()'ed acpi_handle
1432 * @type: Notify code
1433 * @context: pointer to acpiphp_bridge structure
1434 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001435 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 */
1437static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1438{
1439 struct acpiphp_bridge *bridge;
1440 char objname[64];
1441 struct acpi_buffer buffer = { .length = sizeof(objname),
1442 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001443 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001444 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001446 if (acpi_bus_get_device(handle, &device)) {
1447 /* This bridge must have just been physically inserted */
1448 handle_bridge_insertion(handle, type);
1449 return;
1450 }
1451
1452 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001453 if (type == ACPI_NOTIFY_BUS_CHECK) {
1454 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1455 count_sub_bridges, &num_sub_bridges, NULL);
1456 }
1457
1458 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001459 err("cannot get bridge info\n");
1460 return;
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1464
1465 switch (type) {
1466 case ACPI_NOTIFY_BUS_CHECK:
1467 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001468 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001469 if (bridge) {
1470 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001471 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001472 acpiphp_check_bridge(bridge);
1473 }
1474 if (num_sub_bridges)
1475 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1476 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 break;
1478
1479 case ACPI_NOTIFY_DEVICE_CHECK:
1480 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001481 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 acpiphp_check_bridge(bridge);
1483 break;
1484
1485 case ACPI_NOTIFY_DEVICE_WAKE:
1486 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001487 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
1489
1490 case ACPI_NOTIFY_EJECT_REQUEST:
1491 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001492 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001493 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1494 (bridge->flags & BRIDGE_HAS_EJ0)) {
1495 struct acpiphp_slot *slot;
1496 slot = bridge->func->slot;
1497 if (!acpiphp_disable_slot(slot))
1498 acpiphp_eject_slot(slot);
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 break;
1501
1502 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1503 printk(KERN_ERR "Device %s cannot be configured due"
1504 " to a frequency mismatch\n", objname);
1505 break;
1506
1507 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1508 printk(KERN_ERR "Device %s cannot be configured due"
1509 " to a bus mode mismatch\n", objname);
1510 break;
1511
1512 case ACPI_NOTIFY_POWER_FAULT:
1513 printk(KERN_ERR "Device %s has suffered a power fault\n",
1514 objname);
1515 break;
1516
1517 default:
1518 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1519 break;
1520 }
1521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523/**
1524 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 * @handle: Notify()'ed acpi_handle
1526 * @type: Notify code
1527 * @context: pointer to acpiphp_func structure
1528 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001529 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 */
Len Brown2b85e132006-06-27 01:50:14 -04001531static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 struct acpiphp_func *func;
1534 char objname[64];
1535 struct acpi_buffer buffer = { .length = sizeof(objname),
1536 .pointer = objname };
1537
1538 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1539
1540 func = (struct acpiphp_func *)context;
1541
1542 switch (type) {
1543 case ACPI_NOTIFY_BUS_CHECK:
1544 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001545 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 acpiphp_enable_slot(func->slot);
1547 break;
1548
1549 case ACPI_NOTIFY_DEVICE_CHECK:
1550 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001551 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 acpiphp_check_bridge(func->slot->bridge);
1553 break;
1554
1555 case ACPI_NOTIFY_DEVICE_WAKE:
1556 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001557 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 break;
1559
1560 case ACPI_NOTIFY_EJECT_REQUEST:
1561 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001562 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001563 if (!(acpiphp_disable_slot(func->slot)))
1564 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566
1567 default:
1568 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1569 break;
1570 }
1571}
1572
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001573
1574static acpi_status
1575find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1576{
1577 int *count = (int *)context;
1578
Alexander Chiang27558202009-06-10 19:55:14 +00001579 if (acpi_is_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001580 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1581 handle_hotplug_event_bridge, NULL);
1582 (*count)++;
1583 }
1584 return AE_OK ;
1585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587static struct acpi_pci_driver acpi_pci_hp_driver = {
1588 .add = add_bridge,
1589 .remove = remove_bridge,
1590};
1591
1592/**
1593 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
1595int __init acpiphp_glue_init(void)
1596{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001597 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001599 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1600 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 if (num <= 0)
1603 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001604 else
1605 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 return 0;
1608}
1609
1610
1611/**
1612 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1613 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001614 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001616void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1619}
1620
1621
1622/**
1623 * acpiphp_get_num_slots - count number of slots in a system
1624 */
1625int __init acpiphp_get_num_slots(void)
1626{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001628 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Akinobu Mita467c4422006-10-30 13:07:58 -08001630 list_for_each_entry (bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001631 dbg("Bus %04x:%02x has %d slot%s\n",
1632 pci_domain_nr(bridge->pci_bus),
1633 bridge->pci_bus->number, bridge->nr_slots,
1634 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 num_slots += bridge->nr_slots;
1636 }
1637
Rajesh Shah42f49a62005-04-28 00:25:53 -07001638 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return num_slots;
1640}
1641
1642
1643#if 0
1644/**
1645 * acpiphp_for_each_slot - call function for each slot
1646 * @fn: callback function
1647 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 */
1649static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1650{
1651 struct list_head *node;
1652 struct acpiphp_bridge *bridge;
1653 struct acpiphp_slot *slot;
1654 int retval = 0;
1655
1656 list_for_each (node, &bridge_list) {
1657 bridge = (struct acpiphp_bridge *)node;
1658 for (slot = bridge->slots; slot; slot = slot->next) {
1659 retval = fn(slot, data);
1660 if (!retval)
1661 goto err_exit;
1662 }
1663 }
1664
1665 err_exit:
1666 return retval;
1667}
1668#endif
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671/**
1672 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001673 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 */
1675int acpiphp_enable_slot(struct acpiphp_slot *slot)
1676{
1677 int retval;
1678
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001679 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* wake up all functions */
1682 retval = power_on_slot(slot);
1683 if (retval)
1684 goto err_exit;
1685
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001686 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* configure all functions */
1688 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001689 if (retval)
1690 power_off_slot(slot);
1691 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001692 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001693 power_off_slot(slot);
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001697 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return retval;
1699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701/**
1702 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001703 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
1705int acpiphp_disable_slot(struct acpiphp_slot *slot)
1706{
1707 int retval = 0;
1708
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001709 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 /* unconfigure all functions */
1712 retval = disable_device(slot);
1713 if (retval)
1714 goto err_exit;
1715
1716 /* power off all functions */
1717 retval = power_off_slot(slot);
1718 if (retval)
1719 goto err_exit;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001722 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return retval;
1724}
1725
1726
1727/*
1728 * slot enabled: 1
1729 * slot disabled: 0
1730 */
1731u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1732{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001733 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
1736
1737/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001738 * latch open: 1
1739 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
1741u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1742{
1743 unsigned int sta;
1744
1745 sta = get_slot_status(slot);
1746
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001747 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
1750
1751/*
1752 * adapter presence : 1
1753 * absence : 0
1754 */
1755u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1756{
1757 unsigned int sta;
1758
1759 sta = get_slot_status(slot);
1760
1761 return (sta == 0) ? 0 : 1;
1762}