blob: 2e5f25969e11b99019b9f189c24bfcaea2fd8d4a [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
Alex Chiang5228a822009-07-23 17:03:05 -060065static struct pci_bus *pci_bus_from_handle(acpi_handle handle)
66{
67 struct pci_bus *pbus;
68 struct acpi_pci_root *root;
69
70 root = acpi_pci_find_root(handle);
71 if (root)
72 pbus = root->bus;
73 else {
74 struct pci_dev *pdev = acpi_get_pci_dev(handle);
75 pbus = pdev->subordinate;
76 pci_dev_put(pdev);
77 }
78 return pbus;
79}
80
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +090081/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -040082static acpi_status
83is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
84{
85 int *count = (int *)context;
86
87 if (is_dock_device(handle)) {
88 (*count)++;
89 return AE_CTRL_TERMINATE;
90 } else {
91 return AE_OK;
92 }
93}
94
Kristen Accardi4e8662b2006-06-28 03:08:06 -040095/*
96 * the _DCK method can do funny things... and sometimes not
97 * hah-hah funny.
98 *
99 * TBD - figure out a way to only call fixups for
100 * systems that require them.
101 */
102static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
103 void *v)
104{
105 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
106 struct pci_bus *bus = func->slot->bridge->pci_bus;
107 u32 buses;
108
109 if (!bus->self)
110 return NOTIFY_OK;
111
112 /* fixup bad _DCK function that rewrites
113 * secondary bridge on slot
114 */
115 pci_read_config_dword(bus->self,
116 PCI_PRIMARY_BUS,
117 &buses);
118
119 if (((buses >> 8) & 0xff) != bus->secondary) {
120 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700121 | ((unsigned int)(bus->primary) << 0)
122 | ((unsigned int)(bus->secondary) << 8)
123 | ((unsigned int)(bus->subordinate) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400124 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
125 }
126 return NOTIFY_OK;
127}
128
129
Shaohua Li1253f7a2008-08-28 10:06:16 +0800130static struct acpi_dock_ops acpiphp_dock_ops = {
131 .handler = handle_hotplug_event_func,
132};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* callback routine to register each ACPI PCI slot object */
135static acpi_status
136register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
137{
138 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
139 struct acpiphp_slot *slot;
140 struct acpiphp_func *newfunc;
141 acpi_handle tmp;
142 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400143 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800144 int device, function, retval;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900145 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600146 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900148 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return AE_OK;
150
Matthew Garrett56ee3252008-11-25 21:48:14 +0000151 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 device = (adr >> 16) & 0xffff;
153 function = adr & 0xffff;
154
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100155 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (!newfunc)
157 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 INIT_LIST_HEAD(&newfunc->sibling);
160 newfunc->handle = handle;
161 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000162
163 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800164 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
167 newfunc->flags |= FUNC_HAS_STA;
168
169 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
170 newfunc->flags |= FUNC_HAS_PS0;
171
172 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
173 newfunc->flags |= FUNC_HAS_PS3;
174
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400175 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800176 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400179 if (ACPI_FAILURE(status)) {
180 /*
181 * use the count of the number of slots we've found
182 * for the number of the slot
183 */
184 sun = bridge->nr_slots+1;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* search for objects that share the same slot */
188 for (slot = bridge->slots; slot; slot = slot->next)
189 if (slot->device == device) {
190 if (slot->sun != sun)
191 warn("sibling found, but _SUN doesn't match!\n");
192 break;
193 }
194
195 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100196 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!slot) {
198 kfree(newfunc);
199 return AE_NO_MEMORY;
200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 slot->device = device;
204 slot->sun = sun;
205 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100206 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 slot->next = bridge->slots;
209 bridge->slots = slot;
210
211 bridge->nr_slots++;
212
Justin Chenb6adc192008-12-11 11:16:44 -0700213 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900214 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800215 retval = acpiphp_register_hotplug_slot(slot);
216 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600217 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700218 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600219 "hotplug driver\n", slot->sun);
220 else
221 warn("acpiphp_register_hotplug_slot failed "
222 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800223 goto err_exit;
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
227 newfunc->slot = slot;
228 list_add_tail(&newfunc->sibling, &slot->funcs);
229
Alex Chiang9d911d72009-05-21 16:21:15 -0600230 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
231 if (pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Alex Chiang9d911d72009-05-21 16:21:15 -0600233 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400236 if (is_dock_device(handle)) {
237 /* we don't want to call this device's _EJ0
238 * because we want the dock notify handler
239 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800240 */
241 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400242 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800243 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400244 dbg("failed to register dock device\n");
245
246 /* we need to be notified when dock events happen
247 * outside of the hotplug operation, since we may
248 * need to do fixups before we can hotplug.
249 */
250 newfunc->nb.notifier_call = post_dock_fixups;
251 if (register_dock_notifier(&newfunc->nb))
252 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800253 }
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800256 if (!(newfunc->flags & FUNC_HAS_DCK)) {
257 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ACPI_SYSTEM_NOTIFY,
259 handle_hotplug_event_func,
260 newfunc);
261
Kristen Accardi20416ea2006-02-23 17:56:03 -0800262 if (ACPI_FAILURE(status))
263 err("failed to register interrupt notify handler\n");
264 } else
265 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Kristen Accardi20416ea2006-02-23 17:56:03 -0800267 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800268
269 err_exit:
270 bridge->nr_slots--;
271 bridge->slots = slot->next;
272 kfree(slot);
273 kfree(newfunc);
274
275 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278
279/* see if it's worth looking at this bridge */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900280static int detect_ejectable_slots(struct pci_bus *pbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900282 int found = acpi_pci_detect_ejectable(pbus);
283 if (!found) {
284 acpi_handle bridge_handle = acpi_pci_get_bridge_handle(pbus);
James Bottomleyd45e0852009-01-15 15:12:27 -0500285 if (!bridge_handle)
286 return 0;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900287 acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
288 is_pci_dock_device, (void *)&found, NULL);
289 }
290 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/* decode ACPI 2.0 _HPP hot plug parameters */
295static void decode_hpp(struct acpiphp_bridge *bridge)
296{
297 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900299 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900300 if (ACPI_FAILURE(status) ||
301 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800302 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900303 printk(KERN_WARNING
304 "%s: Could not get hotplug parameters. Use defaults\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800305 __func__);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900306 bridge->hpp.t0 = &bridge->hpp.type0_data;
307 bridge->hpp.t0->revision = 0;
308 bridge->hpp.t0->cache_line_size = 0x10;
309 bridge->hpp.t0->latency_timer = 0x40;
310 bridge->hpp.t0->enable_serr = 0;
311 bridge->hpp.t0->enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315
Kristen Accardi783c49f2006-03-03 10:16:05 -0800316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
318static void init_bridge_misc(struct acpiphp_bridge *bridge)
319{
320 acpi_status status;
321
322 /* decode ACPI 2.0 _HPP (hot plug parameters) */
323 decode_hpp(bridge);
324
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800325 /* must be added to the list prior to calling register_slot */
326 list_add(&bridge->list, &bridge_list);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* register all slot objects under this bridge */
329 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
330 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800331 if (ACPI_FAILURE(status)) {
332 list_del(&bridge->list);
333 return;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700337 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900338 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
339 status = acpi_remove_notify_handler(bridge->func->handle,
340 ACPI_SYSTEM_NOTIFY,
341 handle_hotplug_event_func);
342 if (ACPI_FAILURE(status))
343 err("failed to remove notify handler\n");
344 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700345 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ACPI_SYSTEM_NOTIFY,
347 handle_hotplug_event_bridge,
348 bridge);
349
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700350 if (ACPI_FAILURE(status)) {
351 err("failed to register interrupt notify handler\n");
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900357/* find acpiphp_func from acpiphp_bridge */
358static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
359{
360 struct list_head *node, *l;
361 struct acpiphp_bridge *bridge;
362 struct acpiphp_slot *slot;
363 struct acpiphp_func *func;
364
365 list_for_each(node, &bridge_list) {
366 bridge = list_entry(node, struct acpiphp_bridge, list);
367 for (slot = bridge->slots; slot; slot = slot->next) {
368 list_for_each(l, &slot->funcs) {
369 func = list_entry(l, struct acpiphp_func,
370 sibling);
371 if (func->handle == handle)
372 return func;
373 }
374 }
375 }
376
377 return NULL;
378}
379
380
381static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
382{
383 acpi_handle dummy_handle;
384
385 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
386 "_STA", &dummy_handle)))
387 bridge->flags |= BRIDGE_HAS_STA;
388
389 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
390 "_EJ0", &dummy_handle)))
391 bridge->flags |= BRIDGE_HAS_EJ0;
392
393 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
394 "_PS0", &dummy_handle)))
395 bridge->flags |= BRIDGE_HAS_PS0;
396
397 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
398 "_PS3", &dummy_handle)))
399 bridge->flags |= BRIDGE_HAS_PS3;
400
401 /* is this ejectable p2p bridge? */
402 if (bridge->flags & BRIDGE_HAS_EJ0) {
403 struct acpiphp_func *func;
404
405 dbg("found ejectable p2p bridge\n");
406
407 /* make link between PCI bridge and PCI function */
408 func = acpiphp_bridge_handle_to_function(bridge->handle);
409 if (!func)
410 return;
411 bridge->func = func;
412 func->bridge = bridge;
413 }
414}
415
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700418static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct acpiphp_bridge *bridge;
421
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100422 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (bridge == NULL)
424 return;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 bridge->type = BRIDGE_TYPE_HOST;
427 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Rajesh Shah42f49a62005-04-28 00:25:53 -0700429 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 spin_lock_init(&bridge->res_lock);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 init_bridge_misc(bridge);
434}
435
436
437/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700438static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100442 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (bridge == NULL) {
444 err("out of memory\n");
445 return;
446 }
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 bridge->type = BRIDGE_TYPE_P2P;
449 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900450 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Rajesh Shah42f49a62005-04-28 00:25:53 -0700452 bridge->pci_dev = pci_dev_get(pci_dev);
453 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!bridge->pci_bus) {
455 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700456 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600459 /*
460 * Grab a ref to the subordinate PCI bus in case the bus is
461 * removed via PCI core logical hotplug. The ref pins the bus
462 * (which we access during module unload).
463 */
464 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_lock_init(&bridge->res_lock);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700468 return;
469 err:
470 pci_dev_put(pci_dev);
471 kfree(bridge);
472 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475
476/* callback routine to find P2P bridges */
477static acpi_status
478find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
479{
480 acpi_status status;
481 acpi_handle dummy_handle;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400482 unsigned long long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700483 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700485 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
488 if (ACPI_FAILURE(status))
489 return AE_OK; /* continue */
490
491 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
492 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800493 dbg("%s: _ADR evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return AE_OK;
495 }
496
497 device = (tmp >> 16) & 0xffff;
498 function = tmp & 0xffff;
499
Rajesh Shah42f49a62005-04-28 00:25:53 -0700500 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Rajesh Shah42f49a62005-04-28 00:25:53 -0700502 if (!dev || !dev->subordinate)
503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* check if this bridge has ejectable slots */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900506 if ((detect_ejectable_slots(dev->subordinate) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700508 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900511 /* search P2P bridges under this p2p bridge */
512 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
513 find_p2p_bridge, dev->subordinate, NULL);
514 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900515 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900516
Rajesh Shah42f49a62005-04-28 00:25:53 -0700517 out:
518 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return AE_OK;
520}
521
522
523/* find hot-pluggable slots, and then find P2P bridge */
524static int add_bridge(acpi_handle handle)
525{
526 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400527 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int seg, bus;
529 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700530 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* if the bridge doesn't have _STA, we assume it is always there */
533 status = acpi_get_handle(handle, "_STA", &dummy_handle);
534 if (ACPI_SUCCESS(status)) {
535 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
536 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800537 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539 }
540 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
541 /* don't register this object */
542 return 0;
543 }
544
545 /* get PCI segment number */
546 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
547
548 seg = ACPI_SUCCESS(status) ? tmp : 0;
549
550 /* get PCI bus number */
551 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
552
553 if (ACPI_SUCCESS(status)) {
554 bus = tmp;
555 } else {
556 warn("can't get bus number, assuming 0\n");
557 bus = 0;
558 }
559
Rajesh Shah42f49a62005-04-28 00:25:53 -0700560 pci_bus = pci_find_bus(seg, bus);
561 if (!pci_bus) {
562 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564 }
565
Rajesh Shah42f49a62005-04-28 00:25:53 -0700566 /* check if this bridge has ejectable slots */
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900567 if (detect_ejectable_slots(pci_bus) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700568 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
569 add_host_bridge(handle, pci_bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* search P2P bridges under this host bridge */
573 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700574 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900577 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 return 0;
580}
581
Rajesh Shah42f49a62005-04-28 00:25:53 -0700582static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
583{
584 struct list_head *head;
585 list_for_each(head, &bridge_list) {
586 struct acpiphp_bridge *bridge = list_entry(head,
587 struct acpiphp_bridge, list);
588 if (bridge->handle == handle)
589 return bridge;
590 }
591
592 return NULL;
593}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Rajesh Shah364d5092005-04-28 00:25:54 -0700595static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700597 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700598 struct acpiphp_slot *slot;
599 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700600 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700601
602 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
603 handle_hotplug_event_bridge);
604 if (ACPI_FAILURE(status))
605 err("failed to remove notify handler\n");
606
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900607 if ((bridge->type != BRIDGE_TYPE_HOST) &&
608 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
609 status = acpi_install_notify_handler(bridge->func->handle,
610 ACPI_SYSTEM_NOTIFY,
611 handle_hotplug_event_func,
612 bridge->func);
613 if (ACPI_FAILURE(status))
614 err("failed to install interrupt notify handler\n");
615 }
616
Rajesh Shah42f49a62005-04-28 00:25:53 -0700617 slot = bridge->slots;
618 while (slot) {
619 struct acpiphp_slot *next = slot->next;
620 list_for_each_safe (list, tmp, &slot->funcs) {
621 struct acpiphp_func *func;
622 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400623 if (is_dock_device(func->handle)) {
624 unregister_hotplug_dock_device(func->handle);
625 unregister_dock_notifier(&func->nb);
626 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800627 if (!(func->flags & FUNC_HAS_DCK)) {
628 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700629 ACPI_SYSTEM_NOTIFY,
630 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800631 if (ACPI_FAILURE(status))
632 err("failed to remove notify handler\n");
633 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700634 list_del(list);
635 kfree(func);
636 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800637 acpiphp_unregister_hotplug_slot(slot);
638 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700639 kfree(slot);
640 slot = next;
641 }
642
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600643 /*
644 * Only P2P bridges have a pci_dev
645 */
646 if (bridge->pci_dev)
647 put_device(&bridge->pci_bus->dev);
648
Rajesh Shah42f49a62005-04-28 00:25:53 -0700649 pci_dev_put(bridge->pci_dev);
650 list_del(&bridge->list);
651 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Rajesh Shah364d5092005-04-28 00:25:54 -0700654static acpi_status
655cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
656{
657 struct acpiphp_bridge *bridge;
658
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900659 /* cleanup p2p bridges under this P2P bridge
660 in a depth-first manner */
661 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
662 cleanup_p2p_bridge, NULL, NULL);
663
Alex Chianga13307c2008-07-01 20:02:23 -0600664 bridge = acpiphp_handle_to_bridge(handle);
665 if (bridge)
666 cleanup_bridge(bridge);
667
Rajesh Shah364d5092005-04-28 00:25:54 -0700668 return AE_OK;
669}
670
671static void remove_bridge(acpi_handle handle)
672{
673 struct acpiphp_bridge *bridge;
674
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900675 /* cleanup p2p bridges under this host bridge
676 in a depth-first manner */
677 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
678 (u32)1, cleanup_p2p_bridge, NULL, NULL);
679
Alex Chianga13307c2008-07-01 20:02:23 -0600680 /*
681 * On root bridges with hotplug slots directly underneath (ie,
682 * no p2p bridge inbetween), we call cleanup_bridge().
683 *
684 * The else clause cleans up root bridges that either had no
685 * hotplug slots at all, or had a p2p bridge underneath.
686 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700687 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900688 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700689 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600690 else
691 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
692 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700693}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700695static struct pci_dev * get_apic_pci_info(acpi_handle handle)
696{
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700697 struct pci_dev *dev;
698
Alexander Chiangd6aa4842009-06-10 19:55:50 +0000699 dev = acpi_get_pci_dev(handle);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700700 if (!dev)
701 return NULL;
702
703 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
704 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
705 {
706 pci_dev_put(dev);
707 return NULL;
708 }
709
710 return dev;
711}
712
713static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
714{
715 acpi_status status;
716 int result = -1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400717 unsigned long long gsb;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700718 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
719 union acpi_object *obj;
720 void *table;
721
722 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
723 if (ACPI_SUCCESS(status)) {
724 *gsi_base = (u32)gsb;
725 return 0;
726 }
727
728 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
729 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
730 return -1;
731
732 obj = buffer.pointer;
733 if (obj->type != ACPI_TYPE_BUFFER)
734 goto out;
735
736 table = obj->buffer.pointer;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300737 switch (((struct acpi_subtable_header *)table)->type) {
738 case ACPI_MADT_TYPE_IO_SAPIC:
739 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700740 result = 0;
741 break;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300742 case ACPI_MADT_TYPE_IO_APIC:
743 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700744 result = 0;
745 break;
746 default:
747 break;
748 }
749 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700750 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700751 return result;
752}
753
754static acpi_status
755ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
756{
757 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400758 unsigned long long sta;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700759 acpi_handle tmp;
760 struct pci_dev *pdev;
761 u32 gsi_base;
762 u64 phys_addr;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700763 struct acpiphp_ioapic *ioapic;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700764
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))
776 return AE_OK;
777
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700778 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
779 if (!ioapic)
780 return AE_NO_MEMORY;
781
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700782 pdev = get_apic_pci_info(handle);
783 if (!pdev)
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700784 goto exit_kfree;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700785
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700786 if (pci_enable_device(pdev))
787 goto exit_pci_dev_put;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700788
789 pci_set_master(pdev);
790
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700791 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
792 goto exit_pci_disable_device;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700793
794 phys_addr = pci_resource_start(pdev, 0);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700795 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
796 goto exit_pci_release_region;
797
798 ioapic->gsi_base = gsi_base;
799 ioapic->dev = pdev;
800 spin_lock(&ioapic_list_lock);
801 list_add_tail(&ioapic->list, &ioapic_list);
802 spin_unlock(&ioapic_list_lock);
803
804 return AE_OK;
805
806 exit_pci_release_region:
807 pci_release_region(pdev, 0);
808 exit_pci_disable_device:
809 pci_disable_device(pdev);
810 exit_pci_dev_put:
811 pci_dev_put(pdev);
812 exit_kfree:
813 kfree(ioapic);
814
815 return AE_OK;
816}
817
818static acpi_status
819ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
820{
821 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400822 unsigned long long sta;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700823 acpi_handle tmp;
824 u32 gsi_base;
825 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
826
827 /* Evaluate _STA if present */
828 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
829 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
830 return AE_CTRL_DEPTH;
831
832 /* Scan only PCI bus scope */
833 status = acpi_get_handle(handle, "_HID", &tmp);
834 if (ACPI_SUCCESS(status))
835 return AE_CTRL_DEPTH;
836
837 if (get_gsi_base(handle, &gsi_base))
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700838 return AE_OK;
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700839
840 acpi_unregister_ioapic(handle, gsi_base);
841
842 spin_lock(&ioapic_list_lock);
843 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
844 if (pos->gsi_base != gsi_base)
845 continue;
846 ioapic = pos;
847 list_del(&ioapic->list);
848 break;
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700849 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700850 spin_unlock(&ioapic_list_lock);
851
852 if (!ioapic)
853 return AE_OK;
854
855 pci_release_region(ioapic->dev, 0);
856 pci_disable_device(ioapic->dev);
857 pci_dev_put(ioapic->dev);
858 kfree(ioapic);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700859
860 return AE_OK;
861}
862
863static int acpiphp_configure_ioapics(acpi_handle handle)
864{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700865 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700866 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
867 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
868 return 0;
869}
870
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700871static int acpiphp_unconfigure_ioapics(acpi_handle handle)
872{
873 ioapic_remove(handle, 0, NULL, NULL);
874 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
875 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
876 return 0;
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879static int power_on_slot(struct acpiphp_slot *slot)
880{
881 acpi_status status;
882 struct acpiphp_func *func;
883 struct list_head *l;
884 int retval = 0;
885
886 /* if already enabled, just skip */
887 if (slot->flags & SLOT_POWEREDON)
888 goto err_exit;
889
890 list_for_each (l, &slot->funcs) {
891 func = list_entry(l, struct acpiphp_func, sibling);
892
893 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800894 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
896 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800897 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 retval = -1;
899 goto err_exit;
900 } else
901 break;
902 }
903 }
904
905 /* TBD: evaluate _STA to check if the slot is enabled */
906
907 slot->flags |= SLOT_POWEREDON;
908
909 err_exit:
910 return retval;
911}
912
913
914static int power_off_slot(struct acpiphp_slot *slot)
915{
916 acpi_status status;
917 struct acpiphp_func *func;
918 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 int retval = 0;
921
922 /* if already disabled, just skip */
923 if ((slot->flags & SLOT_POWEREDON) == 0)
924 goto err_exit;
925
926 list_for_each (l, &slot->funcs) {
927 func = list_entry(l, struct acpiphp_func, sibling);
928
Rajesh Shah2f523b12005-04-28 00:25:55 -0700929 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
931 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800932 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 retval = -1;
934 goto err_exit;
935 } else
936 break;
937 }
938 }
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* TBD: evaluate _STA to check if the slot is disabled */
941
942 slot->flags &= (~SLOT_POWEREDON);
943
944 err_exit:
945 return retval;
946}
947
948
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800949
950/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800951 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800952 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800953 */
954static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
955{
956 struct list_head *tmp;
957 unsigned char max, n;
958
959 /*
960 * pci_bus_max_busnr will return the highest
961 * reserved busnr for all these children.
962 * that is equivalent to the bus->subordinate
963 * value. We don't want to use the parent's
964 * bus->subordinate value because it could have
965 * padding in it.
966 */
967 max = bus->secondary;
968
969 list_for_each(tmp, &bus->children) {
970 n = pci_bus_max_busnr(pci_bus_b(tmp));
971 if (n > max)
972 max = n;
973 }
974 return max;
975}
976
977
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800978/**
979 * acpiphp_bus_add - add a new bus to acpi subsystem
980 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800981 */
982static int acpiphp_bus_add(struct acpiphp_func *func)
983{
984 acpi_handle phandle;
985 struct acpi_device *device, *pdevice;
986 int ret_val;
987
988 acpi_get_parent(func->handle, &phandle);
989 if (acpi_bus_get_device(phandle, &pdevice)) {
990 dbg("no parent device, assuming NULL\n");
991 pdevice = NULL;
992 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800993 if (!acpi_bus_get_device(func->handle, &device)) {
994 dbg("bus exists... trim\n");
995 /* this shouldn't be in here, so remove
996 * the bus then re-add it...
997 */
998 ret_val = acpi_bus_trim(device, 1);
999 dbg("acpi_bus_trim return %x\n", ret_val);
1000 }
1001
1002 ret_val = acpi_bus_add(&device, pdevice, func->handle,
1003 ACPI_BUS_TYPE_DEVICE);
1004 if (ret_val) {
1005 dbg("error adding bus, %x\n",
1006 -ret_val);
1007 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001008 }
1009 /*
1010 * try to start anyway. We could have failed to add
1011 * simply because this bus had previously been added
1012 * on another add. Don't bother with the return value
1013 * we just keep going.
1014 */
1015 ret_val = acpi_bus_start(device);
1016
1017acpiphp_bus_add_out:
1018 return ret_val;
1019}
1020
1021
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001022/**
1023 * acpiphp_bus_trim - trim a bus from acpi subsystem
1024 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001025 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -07001026static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001027{
1028 struct acpi_device *device;
1029 int retval;
1030
1031 retval = acpi_bus_get_device(handle, &device);
1032 if (retval) {
1033 dbg("acpi_device not found\n");
1034 return retval;
1035 }
1036
1037 retval = acpi_bus_trim(device, 1);
1038 if (retval)
1039 err("cannot remove from acpi list\n");
1040
1041 return retval;
1042}
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044/**
1045 * enable_device - enable, configure a slot
1046 * @slot: slot to be enabled
1047 *
1048 * This function should be called per *physical slot*,
1049 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +01001051static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001054 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct list_head *l;
1056 struct acpiphp_func *func;
1057 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001058 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001059 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (slot->flags & SLOT_ENABLED)
1062 goto err_exit;
1063
1064 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001065 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (dev) {
1067 /* This case shouldn't happen */
1068 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001069 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 retval = -1;
1071 goto err_exit;
1072 }
1073
Rajesh Shah42f49a62005-04-28 00:25:53 -07001074 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1075 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 err("No new device found\n");
1077 retval = -1;
1078 goto err_exit;
1079 }
1080
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001081 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001082 for (pass = 0; pass < 2; pass++) {
1083 list_for_each_entry(dev, &bus->devices, bus_list) {
1084 if (PCI_SLOT(dev->devfn) != slot->device)
1085 continue;
1086 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001087 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001088 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001089 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001090 pci_bus_size_bridges(dev->subordinate);
1091 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001095 list_for_each (l, &slot->funcs) {
1096 func = list_entry(l, struct acpiphp_func, sibling);
1097 acpiphp_bus_add(func);
1098 }
1099
Rajesh Shah42f49a62005-04-28 00:25:53 -07001100 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001101 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001102 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001103 list_for_each_entry(func, &slot->funcs, sibling)
1104 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001105 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001106 pci_bus_add_devices(bus);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 list_for_each (l, &slot->funcs) {
1109 func = list_entry(l, struct acpiphp_func, sibling);
Alex Chiang9d911d72009-05-21 16:21:15 -06001110 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1111 func->function));
1112 if (!dev)
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001113 continue;
1114
Alex Chiang9d911d72009-05-21 16:21:15 -06001115 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1116 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1117 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001118 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -06001119 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001120
1121 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1122 if (ACPI_FAILURE(status))
1123 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1124 status);
Alex Chiang9d911d72009-05-21 16:21:15 -06001125 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 slot->flags |= SLOT_ENABLED;
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 err_exit:
1131 return retval;
1132}
1133
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001134static void disable_bridges(struct pci_bus *bus)
1135{
1136 struct pci_dev *dev;
1137 list_for_each_entry(dev, &bus->devices, bus_list) {
1138 if (dev->subordinate) {
1139 disable_bridges(dev->subordinate);
1140 pci_disable_device(dev);
1141 }
1142 }
1143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145/**
1146 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001147 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 */
1149static int disable_device(struct acpiphp_slot *slot)
1150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -06001152 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /* is this slot already disabled? */
1155 if (!(slot->flags & SLOT_ENABLED))
1156 goto err_exit;
1157
Alex Chiang9d911d72009-05-21 16:21:15 -06001158 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001159 if (func->bridge) {
1160 /* cleanup p2p bridges under this P2P bridge */
1161 cleanup_p2p_bridge(func->bridge->handle,
1162 (u32)1, NULL, NULL);
1163 func->bridge = NULL;
1164 }
1165
Alex Chiang9d911d72009-05-21 16:21:15 -06001166 pdev = pci_get_slot(slot->bridge->pci_bus,
1167 PCI_DEVFN(slot->device, func->function));
1168 if (pdev) {
1169 pci_stop_bus_device(pdev);
1170 if (pdev->subordinate) {
1171 disable_bridges(pdev->subordinate);
1172 pci_disable_device(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001173 }
Alex Chiang9d911d72009-05-21 16:21:15 -06001174 pci_remove_bus_device(pdev);
1175 pci_dev_put(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001176 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001177 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001178
Alex Chiang9d911d72009-05-21 16:21:15 -06001179 list_for_each_entry(func, &slot->funcs, sibling) {
Satoru Takeuchi600812e2006-09-12 10:22:53 -07001180 acpiphp_unconfigure_ioapics(func->handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001181 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 slot->flags &= (~SLOT_ENABLED);
1185
Alex Chiang9d911d72009-05-21 16:21:15 -06001186err_exit:
1187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190
1191/**
1192 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -08001193 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001195 * If a slot has _STA for each function and if any one of them
1196 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001198 * If a slot doesn't have _STA and if any one of its functions'
1199 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001201 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
1203static unsigned int get_slot_status(struct acpiphp_slot *slot)
1204{
1205 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001206 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 u32 dvid;
1208 struct list_head *l;
1209 struct acpiphp_func *func;
1210
1211 list_for_each (l, &slot->funcs) {
1212 func = list_entry(l, struct acpiphp_func, sibling);
1213
1214 if (func->flags & FUNC_HAS_STA) {
1215 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1216 if (ACPI_SUCCESS(status) && sta)
1217 break;
1218 } else {
1219 pci_bus_read_config_dword(slot->bridge->pci_bus,
1220 PCI_DEVFN(slot->device,
1221 func->function),
1222 PCI_VENDOR_ID, &dvid);
1223 if (dvid != 0xffffffff) {
1224 sta = ACPI_STA_ALL;
1225 break;
1226 }
1227 }
1228 }
1229
1230 return (unsigned int)sta;
1231}
1232
1233/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001234 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001235 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001236 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001237int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001238{
1239 acpi_status status;
1240 struct acpiphp_func *func;
1241 struct list_head *l;
1242 struct acpi_object_list arg_list;
1243 union acpi_object arg;
1244
1245 list_for_each (l, &slot->funcs) {
1246 func = list_entry(l, struct acpiphp_func, sibling);
1247
1248 /* We don't want to call _EJ0 on non-existing functions. */
1249 if ((func->flags & FUNC_HAS_EJ0)) {
1250 /* _EJ0 method take one argument */
1251 arg_list.count = 1;
1252 arg_list.pointer = &arg;
1253 arg.type = ACPI_TYPE_INTEGER;
1254 arg.integer.value = 1;
1255
1256 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1257 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001258 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001259 return -1;
1260 } else
1261 break;
1262 }
1263 }
1264 return 0;
1265}
1266
1267/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001269 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 *
1271 * Iterate over all slots under this bridge and make sure that if a
1272 * card is present they are enabled, and if not they are disabled.
1273 */
1274static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1275{
1276 struct acpiphp_slot *slot;
1277 int retval = 0;
1278 int enabled, disabled;
1279
1280 enabled = disabled = 0;
1281
1282 for (slot = bridge->slots; slot; slot = slot->next) {
1283 unsigned int status = get_slot_status(slot);
1284 if (slot->flags & SLOT_ENABLED) {
1285 if (status == ACPI_STA_ALL)
1286 continue;
1287 retval = acpiphp_disable_slot(slot);
1288 if (retval) {
1289 err("Error occurred in disabling\n");
1290 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001291 } else {
1292 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 disabled++;
1295 } else {
1296 if (status != ACPI_STA_ALL)
1297 continue;
1298 retval = acpiphp_enable_slot(slot);
1299 if (retval) {
1300 err("Error occurred in enabling\n");
1301 goto err_exit;
1302 }
1303 enabled++;
1304 }
1305 }
1306
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001307 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 err_exit:
1310 return retval;
1311}
1312
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001313static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1314{
1315 u16 pci_cmd, pci_bctl;
1316 struct pci_dev *cdev;
1317
1318 /* Program hpp values for this device */
1319 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1320 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1321 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1322 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001323
Gary Hade9ef22412007-07-05 11:10:47 -07001324 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1325 return;
1326
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001327 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001328 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001329 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001330 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001331 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001332 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001333 pci_cmd |= PCI_COMMAND_SERR;
1334 else
1335 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001336 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001337 pci_cmd |= PCI_COMMAND_PARITY;
1338 else
1339 pci_cmd &= ~PCI_COMMAND_PARITY;
1340 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1341
1342 /* Program bridge control value and child devices */
1343 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1344 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001345 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001346 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001347 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001348 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1349 else
1350 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001351 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001352 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1353 else
1354 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1355 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1356 if (dev->subordinate) {
1357 list_for_each_entry(cdev, &dev->subordinate->devices,
1358 bus_list)
1359 program_hpp(cdev, bridge);
1360 }
1361 }
1362}
1363
1364static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1365{
1366 struct acpiphp_bridge bridge;
1367 struct pci_dev *dev;
1368
1369 memset(&bridge, 0, sizeof(bridge));
1370 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001371 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001372 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001373 decode_hpp(&bridge);
1374 list_for_each_entry(dev, &bus->devices, bus_list)
1375 program_hpp(dev, &bridge);
1376
1377}
1378
1379/*
1380 * Remove devices for which we could not assign resources, call
1381 * arch specific code to fix-up the bus
1382 */
1383static void acpiphp_sanitize_bus(struct pci_bus *bus)
1384{
1385 struct pci_dev *dev;
1386 int i;
1387 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1388
1389 list_for_each_entry(dev, &bus->devices, bus_list) {
1390 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1391 struct resource *res = &dev->resource[i];
1392 if ((res->flags & type_mask) && !res->start &&
1393 res->end) {
1394 /* Could not assign a required resources
1395 * for this device, remove it */
1396 pci_remove_bus_device(dev);
1397 break;
1398 }
1399 }
1400 }
1401}
1402
1403/* Program resources in newly inserted bridge */
1404static int acpiphp_configure_bridge (acpi_handle handle)
1405{
Alex Chiang5228a822009-07-23 17:03:05 -06001406 struct pci_bus *bus = pci_bus_from_handle(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001407
1408 pci_bus_size_bridges(bus);
1409 pci_bus_assign_resources(bus);
1410 acpiphp_sanitize_bus(bus);
1411 acpiphp_set_hpp_values(handle, bus);
1412 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001413 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001414 return 0;
1415}
1416
1417static void handle_bridge_insertion(acpi_handle handle, u32 type)
1418{
1419 struct acpi_device *device, *pdevice;
1420 acpi_handle phandle;
1421
1422 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1423 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1424 err("unexpected notification type %d\n", type);
1425 return;
1426 }
1427
1428 acpi_get_parent(handle, &phandle);
1429 if (acpi_bus_get_device(phandle, &pdevice)) {
1430 dbg("no parent device, assuming NULL\n");
1431 pdevice = NULL;
1432 }
1433 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1434 err("cannot add bridge to acpi list\n");
1435 return;
1436 }
1437 if (!acpiphp_configure_bridge(handle) &&
1438 !acpi_bus_start(device))
1439 add_bridge(handle);
1440 else
1441 err("cannot configure and start bridge\n");
1442
1443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445/*
1446 * ACPI event handlers
1447 */
1448
Gary Hade0bbd6422007-07-05 11:10:48 -07001449static acpi_status
1450count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1451{
1452 int *count = (int *)context;
1453 struct acpiphp_bridge *bridge;
1454
1455 bridge = acpiphp_handle_to_bridge(handle);
1456 if (bridge)
1457 (*count)++;
1458 return AE_OK ;
1459}
1460
1461static acpi_status
1462check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1463{
1464 struct acpiphp_bridge *bridge;
1465 char objname[64];
1466 struct acpi_buffer buffer = { .length = sizeof(objname),
1467 .pointer = objname };
1468
1469 bridge = acpiphp_handle_to_bridge(handle);
1470 if (bridge) {
1471 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1472 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001473 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001474 acpiphp_check_bridge(bridge);
1475 }
1476 return AE_OK ;
1477}
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479/**
1480 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * @handle: Notify()'ed acpi_handle
1482 * @type: Notify code
1483 * @context: pointer to acpiphp_bridge structure
1484 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001485 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
1487static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1488{
1489 struct acpiphp_bridge *bridge;
1490 char objname[64];
1491 struct acpi_buffer buffer = { .length = sizeof(objname),
1492 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001493 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001494 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001496 if (acpi_bus_get_device(handle, &device)) {
1497 /* This bridge must have just been physically inserted */
1498 handle_bridge_insertion(handle, type);
1499 return;
1500 }
1501
1502 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001503 if (type == ACPI_NOTIFY_BUS_CHECK) {
1504 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1505 count_sub_bridges, &num_sub_bridges, NULL);
1506 }
1507
1508 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001509 err("cannot get bridge info\n");
1510 return;
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1514
1515 switch (type) {
1516 case ACPI_NOTIFY_BUS_CHECK:
1517 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001518 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001519 if (bridge) {
1520 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001521 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001522 acpiphp_check_bridge(bridge);
1523 }
1524 if (num_sub_bridges)
1525 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1526 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 break;
1528
1529 case ACPI_NOTIFY_DEVICE_CHECK:
1530 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001531 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 acpiphp_check_bridge(bridge);
1533 break;
1534
1535 case ACPI_NOTIFY_DEVICE_WAKE:
1536 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001537 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
1539
1540 case ACPI_NOTIFY_EJECT_REQUEST:
1541 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001542 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001543 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1544 (bridge->flags & BRIDGE_HAS_EJ0)) {
1545 struct acpiphp_slot *slot;
1546 slot = bridge->func->slot;
1547 if (!acpiphp_disable_slot(slot))
1548 acpiphp_eject_slot(slot);
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 break;
1551
1552 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1553 printk(KERN_ERR "Device %s cannot be configured due"
1554 " to a frequency mismatch\n", objname);
1555 break;
1556
1557 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1558 printk(KERN_ERR "Device %s cannot be configured due"
1559 " to a bus mode mismatch\n", objname);
1560 break;
1561
1562 case ACPI_NOTIFY_POWER_FAULT:
1563 printk(KERN_ERR "Device %s has suffered a power fault\n",
1564 objname);
1565 break;
1566
1567 default:
1568 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1569 break;
1570 }
1571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573/**
1574 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 * @handle: Notify()'ed acpi_handle
1576 * @type: Notify code
1577 * @context: pointer to acpiphp_func structure
1578 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001579 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 */
Len Brown2b85e132006-06-27 01:50:14 -04001581static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 struct acpiphp_func *func;
1584 char objname[64];
1585 struct acpi_buffer buffer = { .length = sizeof(objname),
1586 .pointer = objname };
1587
1588 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1589
1590 func = (struct acpiphp_func *)context;
1591
1592 switch (type) {
1593 case ACPI_NOTIFY_BUS_CHECK:
1594 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001595 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 acpiphp_enable_slot(func->slot);
1597 break;
1598
1599 case ACPI_NOTIFY_DEVICE_CHECK:
1600 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001601 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 acpiphp_check_bridge(func->slot->bridge);
1603 break;
1604
1605 case ACPI_NOTIFY_DEVICE_WAKE:
1606 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001607 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 break;
1609
1610 case ACPI_NOTIFY_EJECT_REQUEST:
1611 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001612 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001613 if (!(acpiphp_disable_slot(func->slot)))
1614 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 break;
1616
1617 default:
1618 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1619 break;
1620 }
1621}
1622
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001623
1624static acpi_status
1625find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1626{
1627 int *count = (int *)context;
1628
Alexander Chiang27558202009-06-10 19:55:14 +00001629 if (acpi_is_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001630 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1631 handle_hotplug_event_bridge, NULL);
1632 (*count)++;
1633 }
1634 return AE_OK ;
1635}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637static struct acpi_pci_driver acpi_pci_hp_driver = {
1638 .add = add_bridge,
1639 .remove = remove_bridge,
1640};
1641
1642/**
1643 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
1645int __init acpiphp_glue_init(void)
1646{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001647 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001649 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1650 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (num <= 0)
1653 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001654 else
1655 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 return 0;
1658}
1659
1660
1661/**
1662 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1663 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001664 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001666void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1669}
1670
1671
1672/**
1673 * acpiphp_get_num_slots - count number of slots in a system
1674 */
1675int __init acpiphp_get_num_slots(void)
1676{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001678 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Akinobu Mita467c4422006-10-30 13:07:58 -08001680 list_for_each_entry (bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001681 dbg("Bus %04x:%02x has %d slot%s\n",
1682 pci_domain_nr(bridge->pci_bus),
1683 bridge->pci_bus->number, bridge->nr_slots,
1684 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 num_slots += bridge->nr_slots;
1686 }
1687
Rajesh Shah42f49a62005-04-28 00:25:53 -07001688 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return num_slots;
1690}
1691
1692
1693#if 0
1694/**
1695 * acpiphp_for_each_slot - call function for each slot
1696 * @fn: callback function
1697 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 */
1699static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1700{
1701 struct list_head *node;
1702 struct acpiphp_bridge *bridge;
1703 struct acpiphp_slot *slot;
1704 int retval = 0;
1705
1706 list_for_each (node, &bridge_list) {
1707 bridge = (struct acpiphp_bridge *)node;
1708 for (slot = bridge->slots; slot; slot = slot->next) {
1709 retval = fn(slot, data);
1710 if (!retval)
1711 goto err_exit;
1712 }
1713 }
1714
1715 err_exit:
1716 return retval;
1717}
1718#endif
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721/**
1722 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001723 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
1725int acpiphp_enable_slot(struct acpiphp_slot *slot)
1726{
1727 int retval;
1728
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001729 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /* wake up all functions */
1732 retval = power_on_slot(slot);
1733 if (retval)
1734 goto err_exit;
1735
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001736 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /* configure all functions */
1738 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001739 if (retval)
1740 power_off_slot(slot);
1741 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001742 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001743 power_off_slot(slot);
1744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001747 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return retval;
1749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751/**
1752 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001753 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
1755int acpiphp_disable_slot(struct acpiphp_slot *slot)
1756{
1757 int retval = 0;
1758
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001759 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /* unconfigure all functions */
1762 retval = disable_device(slot);
1763 if (retval)
1764 goto err_exit;
1765
1766 /* power off all functions */
1767 retval = power_off_slot(slot);
1768 if (retval)
1769 goto err_exit;
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001772 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return retval;
1774}
1775
1776
1777/*
1778 * slot enabled: 1
1779 * slot disabled: 0
1780 */
1781u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1782{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001783 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
1786
1787/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001788 * latch open: 1
1789 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 */
1791u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1792{
1793 unsigned int sta;
1794
1795 sta = get_slot_status(slot);
1796
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001797 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800
1801/*
1802 * adapter presence : 1
1803 * absence : 0
1804 */
1805u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1806{
1807 unsigned int sta;
1808
1809 sta = get_slot_status(slot);
1810
1811 return (sta == 0) ? 0 : 1;
1812}