Merge remote-tracking branches 'asoc/topic/fsl-easi', 'asoc/topic/fsl-sai', 'asoc...
[linux-2.6-block.git] / drivers / pci / hotplug / acpiphp_glue.c
CommitLineData
1da177e4
LT
1/*
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
42f49a6a
RS
7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
8e7561cf
RS
9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
1da177e4
LT
11 *
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 *
998be20f 29 * Send feedback to <kristen.c.accardi@intel.com>
1da177e4
LT
30 *
31 */
32
42f49a6a
RS
33/*
34 * Lifetime rules for pci_dev:
42f49a6a
RS
35 * - 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.
5d4a4b25
AC
38 * - When a P2P bridge is present, we elevate the refcount on the subordinate
39 * bus. It loses the refcount when the the driver unloads.
42f49a6a
RS
40 */
41
bd950799
LT
42#define pr_fmt(fmt) "acpiphp_glue: " fmt
43
1da177e4
LT
44#include <linux/module.h>
45
46#include <linux/kernel.h>
47#include <linux/pci.h>
7a54f25c 48#include <linux/pci_hotplug.h>
e8c331e9 49#include <linux/pci-acpi.h>
4ebe3450 50#include <linux/pm_runtime.h>
6aa4cdd0 51#include <linux/mutex.h>
5a0e3ad6 52#include <linux/slab.h>
6af8bef1 53#include <linux/acpi.h>
1da177e4
LT
54
55#include "../pci.h"
1da177e4
LT
56#include "acpiphp.h"
57
58static LIST_HEAD(bridge_list);
3d54a316 59static DEFINE_MUTEX(bridge_mutex);
1da177e4 60
be27b3dc 61static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type);
edf5bf34 62static void acpiphp_post_dock_fixup(struct acpi_device *adev);
8e5dce35 63static void acpiphp_sanitize_bus(struct pci_bus *bus);
fca6825a 64static void acpiphp_set_hpp_values(struct pci_bus *bus);
d3a1ebb0 65static void hotplug_event(u32 type, struct acpiphp_context *context);
3d54a316 66static void free_bridge(struct kref *kref);
8e5dce35 67
cb7b8ced
RW
68/**
69 * acpiphp_init_context - Create hotplug context and grab a reference to it.
bbcbfc0e 70 * @adev: ACPI device object to create the context for.
cb7b8ced 71 *
e525506f 72 * Call under acpi_hp_context_lock.
cb7b8ced 73 */
bbcbfc0e 74static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev)
cb7b8ced
RW
75{
76 struct acpiphp_context *context;
cb7b8ced
RW
77
78 context = kzalloc(sizeof(*context), GFP_KERNEL);
79 if (!context)
80 return NULL;
81
cb7b8ced 82 context->refcount = 1;
ba574dc8
RW
83 context->hp.notify = acpiphp_hotplug_notify;
84 context->hp.fixup = acpiphp_post_dock_fixup;
85 acpi_set_hp_context(adev, &context->hp);
cb7b8ced
RW
86 return context;
87}
88
89/**
90 * acpiphp_get_context - Get hotplug context and grab a reference to it.
3c2cc7ff 91 * @adev: ACPI device object to get the context for.
cb7b8ced 92 *
e525506f 93 * Call under acpi_hp_context_lock.
cb7b8ced 94 */
3c2cc7ff 95static struct acpiphp_context *acpiphp_get_context(struct acpi_device *adev)
cb7b8ced 96{
3c2cc7ff 97 struct acpiphp_context *context;
cb7b8ced 98
3c2cc7ff
RW
99 if (!adev->hp)
100 return NULL;
101
102 context = to_acpiphp_context(adev->hp);
103 context->refcount++;
cb7b8ced
RW
104 return context;
105}
106
107/**
108 * acpiphp_put_context - Drop a reference to ACPI hotplug context.
bbcbfc0e 109 * @context: ACPI hotplug context to drop a reference to.
cb7b8ced
RW
110 *
111 * The context object is removed if there are no more references to it.
112 *
e525506f 113 * Call under acpi_hp_context_lock.
cb7b8ced
RW
114 */
115static void acpiphp_put_context(struct acpiphp_context *context)
116{
117 if (--context->refcount)
118 return;
119
bd4674df 120 WARN_ON(context->bridge);
3c2cc7ff 121 context->hp.self->hp = NULL;
cb7b8ced
RW
122 kfree(context);
123}
124
3d54a316
JL
125static inline void get_bridge(struct acpiphp_bridge *bridge)
126{
127 kref_get(&bridge->ref);
128}
129
130static inline void put_bridge(struct acpiphp_bridge *bridge)
131{
132 kref_put(&bridge->ref, free_bridge);
133}
134
edf5bf34
RW
135static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
136{
137 struct acpiphp_context *context;
138
139 acpi_lock_hp_context();
140 context = acpiphp_get_context(adev);
141 if (!context || context->func.parent->is_going_away) {
142 acpi_unlock_hp_context();
143 return NULL;
144 }
145 get_bridge(context->func.parent);
146 acpiphp_put_context(context);
147 acpi_unlock_hp_context();
148 return context;
149}
150
151static void acpiphp_let_context_go(struct acpiphp_context *context)
152{
153 put_bridge(context->func.parent);
154}
155
3d54a316
JL
156static void free_bridge(struct kref *kref)
157{
cb7b8ced 158 struct acpiphp_context *context;
3d54a316
JL
159 struct acpiphp_bridge *bridge;
160 struct acpiphp_slot *slot, *next;
161 struct acpiphp_func *func, *tmp;
162
e525506f 163 acpi_lock_hp_context();
cb7b8ced 164
3d54a316
JL
165 bridge = container_of(kref, struct acpiphp_bridge, ref);
166
167 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
bd4674df
RW
168 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
169 acpiphp_put_context(func_to_context(func));
170
3d54a316
JL
171 kfree(slot);
172 }
173
87831273 174 context = bridge->context;
bbd34fcd
RW
175 /* Root bridges will not have hotplug context. */
176 if (context) {
177 /* Release the reference taken by acpiphp_enumerate_slots(). */
bda46dbb 178 put_bridge(context->func.parent);
bbd34fcd
RW
179 context->bridge = NULL;
180 acpiphp_put_context(context);
181 }
cb7b8ced 182
3d54a316
JL
183 put_device(&bridge->pci_bus->dev);
184 pci_dev_put(bridge->pci_dev);
185 kfree(bridge);
cb7b8ced 186
e525506f 187 acpi_unlock_hp_context();
3d54a316
JL
188}
189
edf5bf34
RW
190/**
191 * acpiphp_post_dock_fixup - Post-dock fixups for PCI devices.
192 * @adev: ACPI device object corresponding to a PCI device.
4e8662bb 193 *
edf5bf34 194 * TBD - figure out a way to only call fixups for systems that require them.
4e8662bb 195 */
edf5bf34 196static void acpiphp_post_dock_fixup(struct acpi_device *adev)
4e8662bb 197{
edf5bf34
RW
198 struct acpiphp_context *context = acpiphp_grab_context(adev);
199 struct pci_bus *bus;
4e8662bb
KA
200 u32 buses;
201
edf5bf34 202 if (!context)
f09ce741 203 return;
4e8662bb 204
edf5bf34
RW
205 bus = context->func.slot->bus;
206 if (!bus->self)
207 goto out;
208
4e8662bb
KA
209 /* fixup bad _DCK function that rewrites
210 * secondary bridge on slot
211 */
edf5bf34 212 pci_read_config_dword(bus->self, PCI_PRIMARY_BUS, &buses);
4e8662bb 213
b918c62e 214 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
4e8662bb 215 buses = (buses & 0xff000000)
2a9d3521 216 | ((unsigned int)(bus->primary) << 0)
b918c62e
YL
217 | ((unsigned int)(bus->busn_res.start) << 8)
218 | ((unsigned int)(bus->busn_res.end) << 16);
4e8662bb
KA
219 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
220 }
4e8662bb 221
edf5bf34
RW
222 out:
223 acpiphp_let_context_go(context);
af9d8adc 224}
4e8662bb 225
5ba113f7
JL
226/* Check whether the PCI device is managed by native PCIe hotplug driver */
227static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
228{
229 u32 reg32;
230 acpi_handle tmp;
231 struct acpi_pci_root *root;
232
233 /* Check whether the PCIe port supports native PCIe hotplug */
234 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
235 return false;
236 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
237 return false;
238
239 /*
240 * Check whether native PCIe hotplug has been enabled for
241 * this PCIe hierarchy.
242 */
243 tmp = acpi_find_root_bridge_handle(pdev);
244 if (!tmp)
245 return false;
246 root = acpi_pci_find_root(tmp);
247 if (!root)
248 return false;
249 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
250 return false;
251
252 return true;
253}
254
3799c5a0
RW
255/**
256 * acpiphp_add_context - Add ACPIPHP context to an ACPI device object.
257 * @handle: ACPI handle of the object to add a context to.
258 * @lvl: Not used.
259 * @data: The object's parent ACPIPHP bridge.
260 * @rv: Not used.
261 */
262static acpi_status acpiphp_add_context(acpi_handle handle, u32 lvl, void *data,
263 void **rv)
1da177e4 264{
cb7b8ced
RW
265 struct acpiphp_bridge *bridge = data;
266 struct acpiphp_context *context;
bbcbfc0e 267 struct acpi_device *adev;
1da177e4
LT
268 struct acpiphp_slot *slot;
269 struct acpiphp_func *newfunc;
1da177e4 270 acpi_status status = AE_OK;
bbd34fcd
RW
271 unsigned long long adr;
272 int device, function;
e8c331e9 273 struct pci_bus *pbus = bridge->pci_bus;
bbd34fcd 274 struct pci_dev *pdev = bridge->pci_dev;
3b63aaa7 275 u32 val;
1da177e4 276
dfb117b3
BH
277 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
278 if (ACPI_FAILURE(status)) {
f26ca1d6
TK
279 if (status != AE_NOT_FOUND)
280 acpi_handle_warn(handle,
281 "can't evaluate _ADR (%#x)\n", status);
dfb117b3
BH
282 return AE_OK;
283 }
bbcbfc0e
RW
284 if (acpi_bus_get_device(handle, &adev))
285 return AE_OK;
dfb117b3
BH
286
287 device = (adr >> 16) & 0xffff;
288 function = adr & 0xffff;
289
e525506f 290 acpi_lock_hp_context();
bbcbfc0e 291 context = acpiphp_init_context(adev);
cb7b8ced 292 if (!context) {
e525506f 293 acpi_unlock_hp_context();
cb7b8ced 294 acpi_handle_err(handle, "No hotplug context\n");
cb7b8ced
RW
295 return AE_NOT_EXIST;
296 }
bd4674df 297 newfunc = &context->func;
bd4674df 298 newfunc->function = function;
bda46dbb 299 newfunc->parent = bridge;
edf5bf34 300 acpi_unlock_hp_context();
cb7b8ced 301
edf5bf34
RW
302 /*
303 * If this is a dock device, its _EJ0 should be executed by the dock
304 * notify handler after calling _DCK.
305 */
306 if (!is_dock_device(adev) && acpi_has_method(handle, "_EJ0"))
20416ea5 307 newfunc->flags = FUNC_HAS_EJ0;
1da177e4 308
ecd046da 309 if (acpi_has_method(handle, "_STA"))
1da177e4
LT
310 newfunc->flags |= FUNC_HAS_STA;
311
1da177e4 312 /* search for objects that share the same slot */
ad41dd9d 313 list_for_each_entry(slot, &bridge->slots, node)
bbd34fcd 314 if (slot->device == device)
ac372338 315 goto slot_found;
1da177e4 316
ac372338
RW
317 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
318 if (!slot) {
e525506f 319 acpi_lock_hp_context();
146fc68a 320 acpiphp_put_context(context);
e525506f 321 acpi_unlock_hp_context();
146fc68a 322 return AE_NO_MEMORY;
ac372338
RW
323 }
324
bda46dbb 325 slot->bus = bridge->pci_bus;
ac372338 326 slot->device = device;
ac372338 327 INIT_LIST_HEAD(&slot->funcs);
ac372338 328
ac372338 329 list_add_tail(&slot->node, &bridge->slots);
1da177e4 330
cc6254e0
RW
331 /*
332 * Expose slots to user space for functions that have _EJ0 or _RMV or
333 * are located in dock stations. Do not expose them for devices handled
334 * by the native PCIe hotplug (PCIeHP), becuase that code is supposed to
335 * expose slots to user space in those cases.
336 */
3b52b21f 337 if ((acpi_pci_check_ejectable(pbus, handle) || is_dock_device(adev))
cc6254e0 338 && !(pdev && device_is_managed_by_native_pciehp(pdev))) {
bbd34fcd
RW
339 unsigned long long sun;
340 int retval;
ac372338 341
bbd34fcd
RW
342 bridge->nr_slots++;
343 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
344 if (ACPI_FAILURE(status))
345 sun = bridge->nr_slots;
346
bd950799 347 pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
7342798d 348 sun, pci_domain_nr(pbus), pbus->number, device);
bbd34fcd 349
7342798d 350 retval = acpiphp_register_hotplug_slot(slot, sun);
bbd34fcd 351 if (retval) {
1aaac071 352 slot->slot = NULL;
bbd34fcd
RW
353 bridge->nr_slots--;
354 if (retval == -EBUSY)
227f0647 355 pr_warn("Slot %llu already registered by another hotplug driver\n", sun);
bbd34fcd 356 else
227f0647 357 pr_warn("acpiphp_register_hotplug_slot failed (err code = 0x%x)\n", retval);
bbd34fcd
RW
358 }
359 /* Even if the slot registration fails, we can still use it. */
1da177e4
LT
360 }
361
ac372338 362 slot_found:
1da177e4
LT
363 newfunc->slot = slot;
364 list_add_tail(&newfunc->sibling, &slot->funcs);
365
3b63aaa7
JL
366 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
367 &val, 60*1000))
bc805a55 368 slot->flags |= SLOT_ENABLED;
1da177e4 369
2e862c51 370 return AE_OK;
1da177e4
LT
371}
372
364d5094 373static void cleanup_bridge(struct acpiphp_bridge *bridge)
1da177e4 374{
3d54a316
JL
375 struct acpiphp_slot *slot;
376 struct acpiphp_func *func;
42f49a6a 377
3d54a316
JL
378 list_for_each_entry(slot, &bridge->slots, node) {
379 list_for_each_entry(func, &slot->funcs, sibling) {
1a699476 380 struct acpi_device *adev = func_to_acpi_device(func);
5a3bc573 381
1a699476 382 acpi_lock_hp_context();
be27b3dc 383 adev->hp->notify = NULL;
edf5bf34 384 adev->hp->fixup = NULL;
1a699476 385 acpi_unlock_hp_context();
42f49a6a 386 }
9217a984 387 slot->flags |= SLOT_IS_GOING_AWAY;
1aaac071
RW
388 if (slot->slot)
389 acpiphp_unregister_hotplug_slot(slot);
42f49a6a
RS
390 }
391
3d54a316 392 mutex_lock(&bridge_mutex);
42f49a6a 393 list_del(&bridge->list);
3d54a316 394 mutex_unlock(&bridge_mutex);
9217a984 395
e525506f 396 acpi_lock_hp_context();
9217a984 397 bridge->is_going_away = true;
e525506f 398 acpi_unlock_hp_context();
1da177e4
LT
399}
400
15a1ae74 401/**
26e6c66e 402 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
15a1ae74 403 * @bus: bus to start search with
15a1ae74
KA
404 */
405static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
406{
c6f0d5ad 407 struct pci_bus *tmp;
15a1ae74
KA
408 unsigned char max, n;
409
410 /*
411 * pci_bus_max_busnr will return the highest
412 * reserved busnr for all these children.
413 * that is equivalent to the bus->subordinate
414 * value. We don't want to use the parent's
415 * bus->subordinate value because it could have
416 * padding in it.
417 */
b918c62e 418 max = bus->busn_res.start;
15a1ae74 419
c6f0d5ad
YW
420 list_for_each_entry(tmp, &bus->children, node) {
421 n = pci_bus_max_busnr(tmp);
15a1ae74
KA
422 if (n > max)
423 max = n;
424 }
425 return max;
426}
427
d0607050
SL
428static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
429{
430 struct acpiphp_func *func;
431 union acpi_object params[2];
432 struct acpi_object_list arg_list;
433
434 list_for_each_entry(func, &slot->funcs, sibling) {
435 arg_list.count = 2;
436 arg_list.pointer = params;
437 params[0].type = ACPI_TYPE_INTEGER;
438 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
439 params[1].type = ACPI_TYPE_INTEGER;
440 params[1].integer.value = 1;
441 /* _REG is optional, we don't care about if there is failure */
5a3bc573
RW
442 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
443 NULL);
d0607050
SL
444 }
445}
446
1f96a965
YL
447static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
448{
449 struct acpiphp_func *func;
450
1f96a965
YL
451 /* quirk, or pcie could set it already */
452 if (dev->is_hotplug_bridge)
453 return;
454
1f96a965
YL
455 list_for_each_entry(func, &slot->funcs, sibling) {
456 if (PCI_FUNC(dev->devfn) == func->function) {
bbd34fcd 457 dev->is_hotplug_bridge = 1;
1f96a965
YL
458 break;
459 }
460 }
461}
3b63aaa7 462
a47d8c8e
RW
463static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
464{
465 struct acpiphp_func *func;
466
b6708fbf
RW
467 list_for_each_entry(func, &slot->funcs, sibling) {
468 struct acpi_device *adev = func_to_acpi_device(func);
a47d8c8e 469
b6708fbf
RW
470 acpi_bus_scan(adev->handle);
471 if (acpi_device_enumerated(adev))
472 acpi_device_set_power(adev, ACPI_STATE_D0);
473 }
a47d8c8e
RW
474 return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
475}
476
1da177e4 477/**
a1d0abce 478 * enable_slot - enable, configure a slot
1da177e4
LT
479 * @slot: slot to be enabled
480 *
481 * This function should be called per *physical slot*,
482 * not per each slot object in ACPI namespace.
1da177e4 483 */
10874f5a 484static void enable_slot(struct acpiphp_slot *slot)
1da177e4 485{
1da177e4 486 struct pci_dev *dev;
bda46dbb 487 struct pci_bus *bus = slot->bus;
1da177e4 488 struct acpiphp_func *func;
b91182a6 489 int max, pass;
d66ecb72 490 LIST_HEAD(add_list);
1da177e4 491
ab122590 492 acpiphp_rescan_slot(slot);
15a1ae74 493 max = acpiphp_max_busnr(bus);
42f49a6a
RS
494 for (pass = 0; pass < 2; pass++) {
495 list_for_each_entry(dev, &bus->devices, bus_list) {
496 if (PCI_SLOT(dev->devfn) != slot->device)
497 continue;
a1d0abce 498
c7a071f6 499 if (pci_is_bridge(dev)) {
42f49a6a 500 max = pci_scan_bridge(bus, dev, max, pass);
1f96a965
YL
501 if (pass && dev->subordinate) {
502 check_hotplug_bridge(slot, dev);
d66ecb72
JL
503 pcibios_resource_survey_bus(dev->subordinate);
504 __pci_bus_size_bridges(dev->subordinate,
505 &add_list);
1f96a965 506 }
c64b5eea 507 }
42f49a6a 508 }
1da177e4 509 }
d66ecb72 510 __pci_bus_assign_resources(bus, &add_list, NULL);
2dc41281 511
8e5dce35 512 acpiphp_sanitize_bus(bus);
fca6825a 513 acpiphp_set_hpp_values(bus);
d0607050 514 acpiphp_set_acpi_region(slot);
69643e48
IC
515
516 list_for_each_entry(dev, &bus->devices, bus_list) {
517 /* Assume that newly added devices are powered on already. */
518 if (!dev->is_added)
519 dev->current_state = PCI_D0;
520 }
521
42f49a6a
RS
522 pci_bus_add_devices(bus);
523
f382a086 524 slot->flags |= SLOT_ENABLED;
58c08628 525 list_for_each_entry(func, &slot->funcs, sibling) {
9d911d79
AC
526 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
527 func->function));
f382a086
AK
528 if (!dev) {
529 /* Do not set SLOT_ENABLED flag if some funcs
530 are not added. */
531 slot->flags &= (~SLOT_ENABLED);
551bcb75 532 continue;
f382a086 533 }
1da177e4 534 }
1da177e4
LT
535}
536
1da177e4 537/**
a1d0abce 538 * disable_slot - disable a slot
26e6c66e 539 * @slot: ACPI PHP slot
1da177e4 540 */
a1d0abce 541static void disable_slot(struct acpiphp_slot *slot)
1da177e4 542{
1c0c5443
RW
543 struct pci_bus *bus = slot->bus;
544 struct pci_dev *dev, *prev;
1da177e4 545 struct acpiphp_func *func;
551bcb75 546
ce29ca3e 547 /*
a1d0abce 548 * enable_slot() enumerates all functions in this device via
ce29ca3e
AK
549 * pci_scan_slot(), whether they have associated ACPI hotplug
550 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
551 * here.
552 */
1c0c5443
RW
553 list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
554 if (PCI_SLOT(dev->devfn) == slot->device)
555 pci_stop_and_remove_bus_device(dev);
600812ec 556
5a3bc573 557 list_for_each_entry(func, &slot->funcs, sibling)
bbcbfc0e 558 acpi_bus_trim(func_to_acpi_device(func));
1da177e4
LT
559
560 slot->flags &= (~SLOT_ENABLED);
1da177e4
LT
561}
562
f244d8b6
RW
563static bool slot_no_hotplug(struct acpiphp_slot *slot)
564{
b440bde7
BH
565 struct pci_bus *bus = slot->bus;
566 struct pci_dev *dev;
f244d8b6 567
b440bde7
BH
568 list_for_each_entry(dev, &bus->devices, bus_list) {
569 if (PCI_SLOT(dev->devfn) == slot->device && dev->ignore_hotplug)
f244d8b6 570 return true;
b440bde7 571 }
f244d8b6
RW
572 return false;
573}
1da177e4
LT
574
575/**
576 * get_slot_status - get ACPI slot status
26e6c66e 577 * @slot: ACPI PHP slot
1da177e4 578 *
26e6c66e
RD
579 * If a slot has _STA for each function and if any one of them
580 * returned non-zero status, return it.
1da177e4 581 *
26e6c66e
RD
582 * If a slot doesn't have _STA and if any one of its functions'
583 * configuration space is configured, return 0x0f as a _STA.
1da177e4 584 *
26e6c66e 585 * Otherwise return 0.
1da177e4
LT
586 */
587static unsigned int get_slot_status(struct acpiphp_slot *slot)
588{
27663c58 589 unsigned long long sta = 0;
1da177e4
LT
590 struct acpiphp_func *func;
591
58c08628 592 list_for_each_entry(func, &slot->funcs, sibling) {
1da177e4 593 if (func->flags & FUNC_HAS_STA) {
5a3bc573
RW
594 acpi_status status;
595
596 status = acpi_evaluate_integer(func_to_handle(func),
597 "_STA", NULL, &sta);
1da177e4
LT
598 if (ACPI_SUCCESS(status) && sta)
599 break;
600 } else {
5a3bc573
RW
601 u32 dvid;
602
bda46dbb 603 pci_bus_read_config_dword(slot->bus,
1da177e4
LT
604 PCI_DEVFN(slot->device,
605 func->function),
606 PCI_VENDOR_ID, &dvid);
607 if (dvid != 0xffffffff) {
608 sta = ACPI_STA_ALL;
609 break;
610 }
611 }
612 }
613
614 return (unsigned int)sta;
615}
616
72820594
MW
617static inline bool device_status_valid(unsigned int sta)
618{
619 /*
620 * ACPI spec says that _STA may return bit 0 clear with bit 3 set
621 * if the device is valid but does not require a device driver to be
622 * loaded (Section 6.3.7 of ACPI 5.0A).
623 */
624 unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
625 return (sta & mask) == mask;
626}
627
4ebe3450
RW
628/**
629 * trim_stale_devices - remove PCI devices that are not responding.
630 * @dev: PCI device to start walking the hierarchy from.
631 */
632static void trim_stale_devices(struct pci_dev *dev)
633{
4dc3082d 634 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
4ebe3450
RW
635 struct pci_bus *bus = dev->subordinate;
636 bool alive = false;
637
4dc3082d 638 if (adev) {
4ebe3450
RW
639 acpi_status status;
640 unsigned long long sta;
641
b2118d6a 642 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
72820594 643 alive = (ACPI_SUCCESS(status) && device_status_valid(sta))
b440bde7 644 || dev->ignore_hotplug;
4ebe3450 645 }
b8a62d54
RW
646 if (!alive)
647 alive = pci_device_is_present(dev);
4ebe3450 648
4ebe3450
RW
649 if (!alive) {
650 pci_stop_and_remove_bus_device(dev);
4dc3082d
RW
651 if (adev)
652 acpi_bus_trim(adev);
4ebe3450
RW
653 } else if (bus) {
654 struct pci_dev *child, *tmp;
655
656 /* The device is a bridge. so check the bus below it. */
657 pm_runtime_get_sync(&dev->dev);
2d7c1b77 658 list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
4ebe3450
RW
659 trim_stale_devices(child);
660
661 pm_runtime_put(&dev->dev);
662 }
663}
664
1da177e4
LT
665/**
666 * acpiphp_check_bridge - re-enumerate devices
26e6c66e 667 * @bridge: where to begin re-enumeration
1da177e4
LT
668 *
669 * Iterate over all slots under this bridge and make sure that if a
670 * card is present they are enabled, and if not they are disabled.
671 */
4ebe3450 672static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1da177e4
LT
673{
674 struct acpiphp_slot *slot;
1da177e4 675
9217a984
RW
676 /* Bail out if the bridge is going away. */
677 if (bridge->is_going_away)
678 return;
679
ad41dd9d 680 list_for_each_entry(slot, &bridge->slots, node) {
4ebe3450
RW
681 struct pci_bus *bus = slot->bus;
682 struct pci_dev *dev, *tmp;
683
f244d8b6
RW
684 if (slot_no_hotplug(slot)) {
685 ; /* do nothing */
72820594 686 } else if (device_status_valid(get_slot_status(slot))) {
4ebe3450 687 /* remove stale devices if any */
2d7c1b77
RW
688 list_for_each_entry_safe_reverse(dev, tmp,
689 &bus->devices, bus_list)
4ebe3450
RW
690 if (PCI_SLOT(dev->devfn) == slot->device)
691 trim_stale_devices(dev);
692
693 /* configure all functions */
a1d0abce 694 enable_slot(slot);
1da177e4 695 } else {
a1d0abce 696 disable_slot(slot);
1da177e4
LT
697 }
698 }
1da177e4
LT
699}
700
fca6825a 701static void acpiphp_set_hpp_values(struct pci_bus *bus)
8e7561cf 702{
8e7561cf
RS
703 struct pci_dev *dev;
704
e81995bb
BH
705 list_for_each_entry(dev, &bus->devices, bus_list)
706 pci_configure_slot(dev);
8e7561cf
RS
707}
708
709/*
710 * Remove devices for which we could not assign resources, call
711 * arch specific code to fix-up the bus
712 */
713static void acpiphp_sanitize_bus(struct pci_bus *bus)
714{
d65eba6a 715 struct pci_dev *dev, *tmp;
8e7561cf
RS
716 int i;
717 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
718
2d7c1b77 719 list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
8e7561cf
RS
720 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
721 struct resource *res = &dev->resource[i];
722 if ((res->flags & type_mask) && !res->start &&
723 res->end) {
724 /* Could not assign a required resources
725 * for this device, remove it */
210647af 726 pci_stop_and_remove_bus_device(dev);
8e7561cf
RS
727 break;
728 }
729 }
730 }
731}
732
1da177e4
LT
733/*
734 * ACPI event handlers
735 */
736
1f7c164b 737void acpiphp_check_host_bridge(struct acpi_device *adev)
3f327e39 738{
86f5f3ca 739 struct acpiphp_bridge *bridge = NULL;
3f327e39 740
86f5f3ca
RW
741 acpi_lock_hp_context();
742 if (adev->hp) {
743 bridge = to_acpiphp_root_context(adev->hp)->root_bridge;
744 if (bridge)
745 get_bridge(bridge);
746 }
747 acpi_unlock_hp_context();
3f327e39 748 if (bridge) {
d42f5da2
RW
749 pci_lock_rescan_remove();
750
3f327e39 751 acpiphp_check_bridge(bridge);
d42f5da2
RW
752
753 pci_unlock_rescan_remove();
3f327e39
YL
754 put_bridge(bridge);
755 }
3f327e39
YL
756}
757
9217a984
RW
758static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
759
d3a1ebb0 760static void hotplug_event(u32 type, struct acpiphp_context *context)
1da177e4 761{
3c2cc7ff 762 acpi_handle handle = context->hp.self->handle;
bd4674df 763 struct acpiphp_func *func = &context->func;
b75cece1 764 struct acpiphp_slot *slot = func->slot;
1da177e4 765 struct acpiphp_bridge *bridge;
6af8bef1 766
e525506f 767 acpi_lock_hp_context();
c8ebcf1f 768 bridge = context->bridge;
43e5c091
RW
769 if (bridge)
770 get_bridge(bridge);
1da177e4 771
e525506f 772 acpi_unlock_hp_context();
3757b948 773
f41b3261 774 pci_lock_rescan_remove();
1da177e4
LT
775
776 switch (type) {
777 case ACPI_NOTIFY_BUS_CHECK:
778 /* bus re-enumerate */
1d4a5b61 779 acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
b75cece1 780 if (bridge)
43e5c091 781 acpiphp_check_bridge(bridge);
b75cece1 782 else if (!(slot->flags & SLOT_IS_GOING_AWAY))
a1d0abce 783 enable_slot(slot);
4ebe3450 784
1da177e4
LT
785 break;
786
787 case ACPI_NOTIFY_DEVICE_CHECK:
788 /* device check */
1d4a5b61 789 acpi_handle_debug(handle, "Device check in %s()\n", __func__);
a47d8c8e 790 if (bridge) {
43e5c091 791 acpiphp_check_bridge(bridge);
b75cece1 792 } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
a47d8c8e
RW
793 /*
794 * Check if anything has changed in the slot and rescan
795 * from the parent if that's the case.
796 */
661b4064 797 if (acpiphp_rescan_slot(slot))
a47d8c8e
RW
798 acpiphp_check_bridge(func->parent);
799 }
1da177e4
LT
800 break;
801
1da177e4
LT
802 case ACPI_NOTIFY_EJECT_REQUEST:
803 /* request device eject */
1d4a5b61 804 acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
b75cece1 805 acpiphp_disable_and_eject_slot(slot);
1da177e4 806 break;
1da177e4 807 }
6af8bef1 808
f41b3261 809 pci_unlock_rescan_remove();
43e5c091
RW
810 if (bridge)
811 put_bridge(bridge);
21a31013
RW
812}
813
be27b3dc 814static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
6af8bef1 815{
87831273 816 struct acpiphp_context *context;
87831273 817
edf5bf34
RW
818 context = acpiphp_grab_context(adev);
819 if (!context)
3c2cc7ff 820 return -ENODATA;
1b360f44 821
3c2cc7ff 822 hotplug_event(type, context);
edf5bf34 823 acpiphp_let_context_go(context);
3c2cc7ff 824 return 0;
8e7561cf 825}
1da177e4 826
454481ad
RW
827/**
828 * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
829 * @bus: PCI bus to enumerate the slots for.
830 *
831 * A "slot" is an object associated with a PCI device number. All functions
832 * (PCI devices) with the same bus and device number belong to the same slot.
1da177e4 833 */
be1c9de9 834void acpiphp_enumerate_slots(struct pci_bus *bus)
1da177e4 835{
3b63aaa7 836 struct acpiphp_bridge *bridge;
bbcbfc0e 837 struct acpi_device *adev;
2552002a
RW
838 acpi_handle handle;
839 acpi_status status;
1da177e4 840
3b63aaa7
JL
841 if (acpiphp_disabled)
842 return;
1da177e4 843
bbcbfc0e
RW
844 adev = ACPI_COMPANION(bus->bridge);
845 if (!adev)
3b63aaa7 846 return;
1da177e4 847
bbcbfc0e 848 handle = adev->handle;
3b63aaa7 849 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
cb7b8ced
RW
850 if (!bridge) {
851 acpi_handle_err(handle, "No memory for bridge object\n");
3b63aaa7
JL
852 return;
853 }
854
ad41dd9d 855 INIT_LIST_HEAD(&bridge->slots);
3d54a316 856 kref_init(&bridge->ref);
3b63aaa7
JL
857 bridge->pci_dev = pci_dev_get(bus->self);
858 bridge->pci_bus = bus;
859
860 /*
861 * Grab a ref to the subordinate PCI bus in case the bus is
862 * removed via PCI core logical hotplug. The ref pins the bus
863 * (which we access during module unload).
864 */
865 get_device(&bus->dev);
866
882d18a7
RW
867 acpi_lock_hp_context();
868 if (pci_is_root_bus(bridge->pci_bus)) {
869 struct acpiphp_root_context *root_context;
870
871 root_context = kzalloc(sizeof(*root_context), GFP_KERNEL);
872 if (!root_context)
873 goto err;
874
875 root_context->root_bridge = bridge;
ba574dc8 876 acpi_set_hp_context(adev, &root_context->hp);
882d18a7 877 } else {
bbd34fcd
RW
878 struct acpiphp_context *context;
879
880 /*
881 * This bridge should have been registered as a hotplug function
fd3cfebe
RW
882 * under its parent, so the context should be there, unless the
883 * parent is going to be handled by pciehp, in which case this
884 * bridge is not interesting to us either.
bbd34fcd 885 */
3c2cc7ff 886 context = acpiphp_get_context(adev);
882d18a7
RW
887 if (!context)
888 goto err;
889
bbd34fcd
RW
890 bridge->context = context;
891 context->bridge = bridge;
892 /* Get a reference to the parent bridge. */
bda46dbb 893 get_bridge(context->func.parent);
bbd34fcd 894 }
882d18a7 895 acpi_unlock_hp_context();
bbd34fcd 896
3799c5a0 897 /* Must be added to the list prior to calling acpiphp_add_context(). */
2552002a
RW
898 mutex_lock(&bridge_mutex);
899 list_add(&bridge->list, &bridge_list);
900 mutex_unlock(&bridge_mutex);
901
902 /* register all slot objects under this bridge */
89373a55 903 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
3799c5a0 904 acpiphp_add_context, NULL, bridge, NULL);
2552002a 905 if (ACPI_FAILURE(status)) {
89373a55 906 acpi_handle_err(handle, "failed to register slots\n");
bbd34fcd
RW
907 cleanup_bridge(bridge);
908 put_bridge(bridge);
2552002a 909 }
882d18a7
RW
910 return;
911
912 err:
913 acpi_unlock_hp_context();
914 put_device(&bus->dev);
915 pci_dev_put(bridge->pci_dev);
916 kfree(bridge);
917}
918
902ee490 919static void acpiphp_drop_bridge(struct acpiphp_bridge *bridge)
882d18a7
RW
920{
921 if (pci_is_root_bus(bridge->pci_bus)) {
922 struct acpiphp_root_context *root_context;
923 struct acpi_device *adev;
924
925 acpi_lock_hp_context();
926 adev = ACPI_COMPANION(bridge->pci_bus->bridge);
927 root_context = to_acpiphp_root_context(adev->hp);
928 adev->hp = NULL;
929 acpi_unlock_hp_context();
930 kfree(root_context);
931 }
932 cleanup_bridge(bridge);
933 put_bridge(bridge);
3b63aaa7
JL
934}
935
454481ad
RW
936/**
937 * acpiphp_remove_slots - Remove slot objects associated with a given bus.
938 * @bus: PCI bus to remove the slot objects for.
939 */
3b63aaa7 940void acpiphp_remove_slots(struct pci_bus *bus)
1da177e4 941{
ff181e5a 942 struct acpiphp_bridge *bridge;
3b63aaa7
JL
943
944 if (acpiphp_disabled)
945 return;
946
ff181e5a
RW
947 mutex_lock(&bridge_mutex);
948 list_for_each_entry(bridge, &bridge_list, list)
3b63aaa7 949 if (bridge->pci_bus == bus) {
ff181e5a 950 mutex_unlock(&bridge_mutex);
882d18a7 951 acpiphp_drop_bridge(bridge);
ff181e5a 952 return;
3b63aaa7 953 }
ff181e5a
RW
954
955 mutex_unlock(&bridge_mutex);
1da177e4
LT
956}
957
1da177e4
LT
958/**
959 * acpiphp_enable_slot - power on slot
26e6c66e 960 * @slot: ACPI PHP slot
1da177e4
LT
961 */
962int acpiphp_enable_slot(struct acpiphp_slot *slot)
963{
9217a984
RW
964 pci_lock_rescan_remove();
965
966 if (slot->flags & SLOT_IS_GOING_AWAY)
967 return -ENODEV;
968
bc805a55 969 /* configure all functions */
55502ddb 970 if (!(slot->flags & SLOT_ENABLED))
a1d0abce 971 enable_slot(slot);
55502ddb 972
9217a984 973 pci_unlock_rescan_remove();
a1d0abce 974 return 0;
1da177e4
LT
975}
976
1da177e4 977/**
ad21d2d0 978 * acpiphp_disable_and_eject_slot - power off and eject slot
26e6c66e 979 * @slot: ACPI PHP slot
1da177e4 980 */
9217a984 981static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
1da177e4 982{
ad21d2d0 983 struct acpiphp_func *func;
9217a984
RW
984
985 if (slot->flags & SLOT_IS_GOING_AWAY)
986 return -ENODEV;
1da177e4 987
1da177e4 988 /* unconfigure all functions */
a1d0abce 989 disable_slot(slot);
1da177e4 990
ad21d2d0
MW
991 list_for_each_entry(func, &slot->funcs, sibling)
992 if (func->flags & FUNC_HAS_EJ0) {
993 acpi_handle handle = func_to_handle(func);
994
995 if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
996 acpi_handle_err(handle, "_EJ0 failed\n");
997
998 break;
999 }
1000
9217a984 1001 return 0;
1da177e4
LT
1002}
1003
9217a984
RW
1004int acpiphp_disable_slot(struct acpiphp_slot *slot)
1005{
1006 int ret;
1007
21369c77
RW
1008 /*
1009 * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
1010 * acpiphp_disable_and_eject_slot() will be synchronized properly.
1011 */
1012 acpi_scan_lock_acquire();
9217a984
RW
1013 pci_lock_rescan_remove();
1014 ret = acpiphp_disable_and_eject_slot(slot);
1015 pci_unlock_rescan_remove();
21369c77 1016 acpi_scan_lock_release();
9217a984
RW
1017 return ret;
1018}
1da177e4
LT
1019
1020/*
1021 * slot enabled: 1
1022 * slot disabled: 0
1023 */
1024u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1025{
bc805a55 1026 return (slot->flags & SLOT_ENABLED);
1da177e4
LT
1027}
1028
1da177e4 1029/*
35ae61a0
MT
1030 * latch open: 1
1031 * latch closed: 0
1da177e4
LT
1032 */
1033u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1034{
1ad3790a 1035 return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
1da177e4
LT
1036}
1037
1da177e4
LT
1038/*
1039 * adapter presence : 1
1040 * absence : 0
1041 */
1042u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1043{
1ad3790a 1044 return !!get_slot_status(slot);
1da177e4 1045}