// SPDX-License-Identifier: GPL-2.0+ /* * ACPI PCI HotPlug glue functions to ACPI CA subsystem * * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com) * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com) * Copyright (C) 2002,2003 NEC Corporation * Copyright (C) 2003-2005 Matthew Wilcox (willy@infradead.org) * Copyright (C) 2003-2005 Hewlett Packard * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com) * Copyright (C) 2005 Intel Corporation * * All rights reserved. * * Send feedback to <kristen.c.accardi@intel.com> *
*/
/* * Lifetime rules for pci_dev: * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot() * when the bridge is scanned and it loses a refcount when the bridge * is removed. * - When a P2P bridge is present, we elevate the refcount on the subordinate * bus. It loses the refcount when the driver unloads.
*/
/** * acpiphp_get_context - Get hotplug context and grab a reference to it. * @adev: ACPI device object to get the context for. * * Call under acpi_hp_context_lock.
*/ staticstruct acpiphp_context *acpiphp_get_context(struct acpi_device *adev)
{ struct acpiphp_context *context;
/** * acpiphp_put_context - Drop a reference to ACPI hotplug context. * @context: ACPI hotplug context to drop a reference to. * * The context object is removed if there are no more references to it. * * Call under acpi_hp_context_lock.
*/ staticvoid acpiphp_put_context(struct acpiphp_context *context)
{ if (--context->refcount) return;
context = bridge->context; /* Root bridges will not have hotplug context. */ if (context) { /* Release the reference taken by acpiphp_enumerate_slots(). */
put_bridge(context->func.parent);
context->bridge = NULL;
acpiphp_put_context(context);
}
/** * acpiphp_post_dock_fixup - Post-dock fixups for PCI devices. * @adev: ACPI device object corresponding to a PCI device. * * TBD - figure out a way to only call fixups for systems that require them.
*/ staticvoid acpiphp_post_dock_fixup(struct acpi_device *adev)
{ struct acpiphp_context *context = acpiphp_grab_context(adev); struct pci_bus *bus;
u32 buses;
if (!context) return;
bus = context->func.slot->bus; if (!bus->self) goto out;
/* fixup bad _DCK function that rewrites * secondary bridge on slot
*/
pci_read_config_dword(bus->self, PCI_PRIMARY_BUS, &buses);
/* * If this is a dock device, its _EJ0 should be executed by the dock * notify handler after calling _DCK.
*/ if (!is_dock_device(adev) && acpi_has_method(handle, "_EJ0"))
newfunc->flags = FUNC_HAS_EJ0;
if (acpi_has_method(handle, "_STA"))
newfunc->flags |= FUNC_HAS_STA;
/* search for objects that share the same slot */
list_for_each_entry(slot, &bridge->slots, node) if (slot->device == device) goto slot_found;
/* * Expose slots to user space for functions that have _EJ0 or _RMV or * are located in dock stations. Do not expose them for devices handled * by the native PCIe hotplug (PCIeHP) or standard PCI hotplug * (SHPCHP), because that code is supposed to expose slots to user * space in those cases.
*/ if ((acpi_pci_check_ejectable(pbus, handle) || is_dock_device(adev))
&& !(pdev && hotplug_is_native(pdev))) { unsignedlonglong sun; int retval;
bridge->nr_slots++;
status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun); if (ACPI_FAILURE(status))
sun = bridge->nr_slots;
retval = acpiphp_register_hotplug_slot(slot, sun); if (retval) {
slot->slot = NULL;
bridge->nr_slots--; if (retval == -EBUSY)
pr_warn("Slot %llu already registered by another hotplug driver\n", sun); else
pr_warn("acpiphp_register_hotplug_slot failed (err code = 0x%x)\n", retval);
} /* Even if the slot registration fails, we can still use it. */
}
/** * acpiphp_max_busnr - return the highest reserved bus number under the given bus. * @bus: bus to start search with
*/ staticunsignedchar acpiphp_max_busnr(struct pci_bus *bus)
{ struct pci_bus *tmp; unsignedchar max, n;
/* * pci_bus_max_busnr will return the highest * reserved busnr for all these children. * that is equivalent to the bus->subordinate * value. We don't want to use the parent's * bus->subordinate value because it could have * padding in it.
*/
max = bus->busn_res.start;
list_for_each_entry(tmp, &bus->children, node) {
n = pci_bus_max_busnr(tmp); if (n > max)
max = n;
} return max;
}
list_for_each_entry(func, &slot->funcs, sibling) { /* _REG is optional, we don't care about if there is failure */
acpi_evaluate_reg(func_to_handle(func),
ACPI_ADR_SPACE_PCI_CONFIG,
ACPI_REG_CONNECT);
}
}
/* quirk, or pcie could set it already */ if (dev->is_hotplug_bridge) return;
/* * In the PCIe case, only Root Ports and Downstream Ports are capable of * accommodating hotplug devices, so avoid marking Upstream Ports as * "hotplug bridges".
*/ if (pci_is_pcie(dev) && pci_pcie_type(dev) == PCI_EXP_TYPE_UPSTREAM) return;
max = bus->busn_res.start; /* Scan already configured non-hotplug bridges */
for_each_pci_bridge(dev, bus) { if (!hotplug_is_native(dev))
max = pci_scan_bridge(bus, dev, max, 0);
}
/* Scan non-hotplug bridges that need to be reconfigured */
for_each_pci_bridge(dev, bus) { if (hotplug_is_native(dev)) continue;
max = pci_scan_bridge(bus, dev, max, 1); if (dev->subordinate) {
pcibios_resource_survey_bus(dev->subordinate);
pci_bus_size_bridges(dev->subordinate);
pci_bus_assign_resources(dev->subordinate);
}
}
}
/** * enable_slot - enable, configure a slot * @slot: slot to be enabled * @bridge: true if enable is for the whole bridge (not a single slot) * * This function should be called per *physical slot*, * not per each slot object in ACPI namespace.
*/ staticvoid enable_slot(struct acpiphp_slot *slot, bool bridge)
{ struct pci_dev *dev; struct pci_bus *bus = slot->bus; struct acpiphp_func *func;
if (bridge && bus->self && hotplug_is_native(bus->self)) { /* * If native hotplug is used, it will take care of hotplug * slot management and resource allocation for hotplug * bridges. However, ACPI hotplug may still be used for * non-hotplug bridges to bring in additional devices such * as a Thunderbolt host controller.
*/
for_each_pci_bridge(dev, bus) { if (PCI_SLOT(dev->devfn) == slot->device)
acpiphp_native_scan_bridge(dev);
}
} else {
LIST_HEAD(add_list); int max, pass;
acpiphp_rescan_slot(slot);
max = acpiphp_max_busnr(bus); for (pass = 0; pass < 2; pass++) {
for_each_pci_bridge(dev, bus) { if (PCI_SLOT(dev->devfn) != slot->device) continue;
list_for_each_entry(dev, &bus->devices, bus_list) { /* Assume that newly added devices are powered on already. */ if (!pci_dev_is_added(dev))
dev->current_state = PCI_D0;
}
pci_bus_add_devices(bus);
slot->flags |= SLOT_ENABLED;
list_for_each_entry(func, &slot->funcs, sibling) {
dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
func->function)); if (!dev) { /* Do not set SLOT_ENABLED flag if some funcs
are not added. */
slot->flags &= ~SLOT_ENABLED; continue;
}
pci_dev_put(dev);
}
}
/* * enable_slot() enumerates all functions in this device via * pci_scan_slot(), whether they have associated ACPI hotplug * methods (_EJ0, etc.) or not. Therefore, we remove all functions * here.
*/
list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list) if (PCI_SLOT(dev->devfn) == slot->device)
pci_stop_and_remove_bus_device(dev);
/** * get_slot_status - get ACPI slot status * @slot: ACPI PHP slot * * If a slot has _STA for each function and if any one of them * returned non-zero status, return it. * * If a slot doesn't have _STA and if any one of its functions' * configuration space is configured, return 0x0f as a _STA. * * Otherwise return 0.
*/ staticunsignedint get_slot_status(struct acpiphp_slot *slot)
{ unsignedlonglong sta = 0; struct acpiphp_func *func;
u32 dvid;
status = acpi_evaluate_integer(func_to_handle(func), "_STA", NULL, &sta); if (ACPI_SUCCESS(status) && sta) break;
} else { if (pci_bus_read_dev_vendor_id(slot->bus,
PCI_DEVFN(slot->device, func->function),
&dvid, 0)) {
sta = ACPI_STA_ALL; break;
}
}
}
if (!sta) { /* * Check for the slot itself since it may be that the * ACPI slot is a device below PCIe upstream port so in * that case it may not even be reachable yet.
*/ if (pci_bus_read_dev_vendor_id(slot->bus,
PCI_DEVFN(slot->device, 0), &dvid, 0)) {
sta = ACPI_STA_ALL;
}
}
return (unsignedint)sta;
}
staticinlinebool device_status_valid(unsignedint sta)
{ /* * ACPI spec says that _STA may return bit 0 clear with bit 3 set * if the device is valid but does not require a device driver to be * loaded (Section 6.3.7 of ACPI 5.0A).
*/ unsignedint mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING; return (sta & mask) == mask;
}
/** * trim_stale_devices - remove PCI devices that are not responding. * @dev: PCI device to start walking the hierarchy from.
*/ staticvoid trim_stale_devices(struct pci_dev *dev)
{ struct acpi_device *adev = ACPI_COMPANION(&dev->dev); struct pci_bus *bus = dev->subordinate; bool alive = dev->ignore_hotplug;
if (adev) {
acpi_status status; unsignedlonglong sta;
status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
alive = alive || (ACPI_SUCCESS(status) && device_status_valid(sta));
} if (!alive)
alive = pci_device_is_present(dev);
if (!alive) {
pci_dev_set_disconnected(dev, NULL); if (pci_has_subordinate(dev))
pci_walk_bus(dev->subordinate, pci_dev_set_disconnected,
NULL);
/* The device is a bridge. so check the bus below it. */
pm_runtime_get_sync(&dev->dev);
list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
trim_stale_devices(child);
pm_runtime_put(&dev->dev);
}
}
/** * acpiphp_check_bridge - re-enumerate devices * @bridge: where to begin re-enumeration * * Iterate over all slots under this bridge and make sure that if a * card is present they are enabled, and if not they are disabled.
*/ staticvoid acpiphp_check_bridge(struct acpiphp_bridge *bridge)
{ struct acpiphp_slot *slot;
/* Bail out if the bridge is going away. */ if (bridge->is_going_away) return;
if (bridge->pci_dev)
pm_runtime_get_sync(&bridge->pci_dev->dev);
if (slot_no_hotplug(slot)) {
; /* do nothing */
} elseif (device_status_valid(get_slot_status(slot))) { /* remove stale devices if any */
list_for_each_entry_safe_reverse(dev, tmp,
&bus->devices, bus_list) if (PCI_SLOT(dev->devfn) == slot->device)
trim_stale_devices(dev);
if (bridge->pci_dev)
pm_runtime_put(&bridge->pci_dev->dev);
}
/* * Remove devices for which we could not assign resources, call * arch specific code to fix-up the bus
*/ staticvoid acpiphp_sanitize_bus(struct pci_bus *bus)
{ struct pci_dev *dev, *tmp; int i; unsignedlong type_mask = IORESOURCE_IO | IORESOURCE_MEM;
list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) { for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) { struct resource *res = &dev->resource[i]; if ((res->flags & type_mask) && !res->start &&
res->end) { /* Could not assign a required resources
* for this device, remove it */
pci_stop_and_remove_bus_device(dev); break;
}
}
}
}
acpi_lock_hp_context();
bridge = context->bridge; if (bridge)
get_bridge(bridge);
acpi_unlock_hp_context();
pci_lock_rescan_remove();
switch (type) { case ACPI_NOTIFY_BUS_CHECK: /* bus re-enumerate */
acpi_handle_debug(handle, "Bus check in %s()\n", __func__); if (bridge)
acpiphp_check_bridge(bridge); elseif (!(slot->flags & SLOT_IS_GOING_AWAY))
enable_slot(slot, false);
break;
case ACPI_NOTIFY_DEVICE_CHECK: /* device check */
acpi_handle_debug(handle, "Device check in %s()\n", __func__); if (bridge) {
acpiphp_check_bridge(bridge);
} elseif (!(slot->flags & SLOT_IS_GOING_AWAY)) { /* * Check if anything has changed in the slot and rescan * from the parent if that's the case.
*/ if (acpiphp_rescan_slot(slot))
acpiphp_check_bridge(func->parent);
} break;
case ACPI_NOTIFY_EJECT_REQUEST: /* request device eject */
acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
acpiphp_disable_and_eject_slot(slot); break;
}
pci_unlock_rescan_remove(); if (bridge)
put_bridge(bridge);
}
/** * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus. * @bus: PCI bus to enumerate the slots for. * * A "slot" is an object associated with a PCI device number. All functions * (PCI devices) with the same bus and device number belong to the same slot.
*/ void acpiphp_enumerate_slots(struct pci_bus *bus)
{ struct acpiphp_bridge *bridge; struct acpi_device *adev;
acpi_handle handle;
acpi_status status;
if (acpiphp_disabled) return;
adev = ACPI_COMPANION(bus->bridge); if (!adev) return;
/* * Grab a ref to the subordinate PCI bus in case the bus is * removed via PCI core logical hotplug. The ref pins the bus * (which we access during module unload).
*/
get_device(&bus->dev);
acpi_lock_hp_context(); if (pci_is_root_bus(bridge->pci_bus)) { struct acpiphp_root_context *root_context;
root_context = kzalloc(sizeof(*root_context), GFP_KERNEL); if (!root_context) goto err;
/* * This bridge should have been registered as a hotplug function * under its parent, so the context should be there, unless the * parent is going to be handled by pciehp, in which case this * bridge is not interesting to us either.
*/
context = acpiphp_get_context(adev); if (!context) goto err;
bridge->context = context;
context->bridge = bridge; /* Get a reference to the parent bridge. */
get_bridge(context->func.parent);
}
acpi_unlock_hp_context();
/* Must be added to the list prior to calling acpiphp_add_context(). */
mutex_lock(&bridge_mutex);
list_add(&bridge->list, &bridge_list);
mutex_unlock(&bridge_mutex);
/* register all slot objects under this bridge */
status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
acpiphp_add_context, NULL, bridge, NULL); if (ACPI_FAILURE(status)) {
acpi_handle_err(handle, "failed to register slots\n");
cleanup_bridge(bridge);
put_bridge(bridge);
} return;
if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
acpi_handle_err(handle, "_EJ0 failed\n");
break;
}
return 0;
}
int acpiphp_disable_slot(struct acpiphp_slot *slot)
{ int ret;
/* * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in * acpiphp_disable_and_eject_slot() will be synchronized properly.
*/
acpi_scan_lock_acquire();
pci_lock_rescan_remove();
ret = acpiphp_disable_and_eject_slot(slot);
pci_unlock_rescan_remove();
acpi_scan_lock_release(); return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.