2018-01-26 17:45:16 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/module.h>
|
2024-06-12 08:20:16 +00:00
|
|
|
#include <linux/of_platform.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include "pci.h"
|
|
|
|
|
2014-02-01 14:38:29 +00:00
|
|
|
static void pci_free_resources(struct pci_dev *dev)
|
|
|
|
{
|
2023-03-30 16:24:30 +00:00
|
|
|
struct resource *res;
|
2014-02-01 14:38:29 +00:00
|
|
|
|
2023-03-30 16:24:30 +00:00
|
|
|
pci_dev_for_each_resource(dev, res) {
|
2014-02-01 14:38:29 +00:00
|
|
|
if (res->parent)
|
|
|
|
release_resource(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-12 17:16:36 +00:00
|
|
|
static void pci_stop_dev(struct pci_dev *dev)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-02-11 19:49:49 +00:00
|
|
|
pci_pme_active(dev, false);
|
|
|
|
|
2018-07-03 09:05:41 +00:00
|
|
|
if (pci_dev_is_added(dev)) {
|
2024-06-12 08:20:16 +00:00
|
|
|
of_platform_depopulate(&dev->dev);
|
2017-10-11 21:35:56 +00:00
|
|
|
device_release_driver(&dev->dev);
|
2005-04-28 07:25:49 +00:00
|
|
|
pci_proc_detach_device(dev);
|
|
|
|
pci_remove_sysfs_dev_files(dev);
|
PCI: Create device tree node for bridge
The PCI endpoint device such as Xilinx Alveo PCI card maps the register
spaces from multiple hardware peripherals to its PCI BAR. Normally,
the PCI core discovers devices and BARs using the PCI enumeration process.
There is no infrastructure to discover the hardware peripherals that are
present in a PCI device, and which can be accessed through the PCI BARs.
Apparently, the device tree framework requires a device tree node for the
PCI device. Thus, it can generate the device tree nodes for hardware
peripherals underneath. Because PCI is self discoverable bus, there might
not be a device tree node created for PCI devices. Furthermore, if the PCI
device is hot pluggable, when it is plugged in, the device tree nodes for
its parent bridges are required. Add support to generate device tree node
for PCI bridges.
Add an of_pci_make_dev_node() interface that can be used to create device
tree node for PCI devices.
Add a PCI_DYNAMIC_OF_NODES config option. When the option is turned on,
the kernel will generate device tree nodes for PCI bridges unconditionally.
Initially, add the basic properties for the dynamically generated device
tree nodes which include #address-cells, #size-cells, device_type,
compatible, ranges, reg.
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
Link: https://lore.kernel.org/r/1692120000-46900-3-git-send-email-lizhi.hou@amd.com
Signed-off-by: Rob Herring <robh@kernel.org>
2023-08-15 17:19:57 +00:00
|
|
|
of_pci_remove_node(dev);
|
2018-07-03 09:05:41 +00:00
|
|
|
|
|
|
|
pci_dev_assign_added(dev, false);
|
2005-04-28 07:25:49 +00:00
|
|
|
}
|
2006-09-12 17:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_destroy_dev(struct pci_dev *dev)
|
|
|
|
{
|
2014-01-14 19:04:51 +00:00
|
|
|
if (!dev->dev.kobj.parent)
|
|
|
|
return;
|
|
|
|
|
PCI: Move device_del() from pci_stop_dev() to pci_destroy_dev()
After commit bcdde7e221a8 (sysfs: make __sysfs_remove_dir() recursive)
I'm seeing traces analogous to the one below in Thunderbolt testing:
WARNING: CPU: 3 PID: 76 at /scratch/rafael/work/linux-pm/fs/sysfs/group.c:214 sysfs_remove_group+0x59/0xe0()
sysfs group ffffffff81c6c500 not found for kobject '0000:08'
Modules linked in: ...
CPU: 3 PID: 76 Comm: kworker/u16:7 Not tainted 3.13.0-rc1+ #76
Hardware name: Acer Aspire S5-391/Venus , BIOS V1.02 05/29/2012
Workqueue: kacpi_hotplug acpi_hotplug_work_fn
0000000000000009 ffff8801644b9ac8 ffffffff816b23bf 0000000000000007
ffff8801644b9b18 ffff8801644b9b08 ffffffff81046607 ffff88016925b800
0000000000000000 ffffffff81c6c500 ffff88016924f928 ffff88016924f800
Call Trace:
[<ffffffff816b23bf>] dump_stack+0x4e/0x71
[<ffffffff81046607>] warn_slowpath_common+0x87/0xb0
[<ffffffff810466d1>] warn_slowpath_fmt+0x41/0x50
[<ffffffff811e42ef>] ? sysfs_get_dirent_ns+0x6f/0x80
[<ffffffff811e5389>] sysfs_remove_group+0x59/0xe0
[<ffffffff8149f00b>] dpm_sysfs_remove+0x3b/0x50
[<ffffffff81495818>] device_del+0x58/0x1c0
[<ffffffff814959c8>] device_unregister+0x48/0x60
[<ffffffff813254fe>] pci_remove_bus+0x6e/0x80
[<ffffffff81325548>] pci_remove_bus_device+0x38/0x110
[<ffffffff8132555d>] pci_remove_bus_device+0x4d/0x110
[<ffffffff81325639>] pci_stop_and_remove_bus_device+0x19/0x20
[<ffffffff813418d0>] disable_slot+0x20/0xe0
[<ffffffff81341a38>] acpiphp_check_bridge+0xa8/0xd0
[<ffffffff813427ad>] hotplug_event+0x17d/0x220
[<ffffffff81342880>] hotplug_event_work+0x30/0x70
[<ffffffff8136d665>] acpi_hotplug_work_fn+0x18/0x24
[<ffffffff81061331>] process_one_work+0x261/0x450
[<ffffffff81061a7e>] worker_thread+0x21e/0x370
[<ffffffff81061860>] ? rescuer_thread+0x300/0x300
[<ffffffff81068342>] kthread+0xd2/0xe0
[<ffffffff81068270>] ? flush_kthread_worker+0x70/0x70
[<ffffffff816c19bc>] ret_from_fork+0x7c/0xb0
[<ffffffff81068270>] ? flush_kthread_worker+0x70/0x70
(Mika Westerberg sees them too in his tests).
Some investigation documented in kernel bug #65281 led me to the
conclusion that the source of the problem is the device_del() in
pci_stop_dev() as it now causes the sysfs directory of the device to be
removed recursively along with all of its subdirectories. That includes
the sysfs directory of the device's subordinate bus (dev->subordinate) and
its "power" group.
Consequently, when pci_remove_bus() is called for dev->subordinate in
pci_remove_bus_device(), it calls device_unregister(&bus->dev), but at this
point the sysfs directory of bus->dev doesn't exist any more and its
"power" group doesn't exist either. Thus, when dpm_sysfs_remove() called
from device_del() tries to remove that group, it triggers the above
warning.
That indicates a logical mistake in the design of
pci_stop_and_remove_bus_device(), which causes bus device objects to be
left behind their parents (bridge device objects) and can be fixed by
moving the device_del() from pci_stop_dev() into pci_destroy_dev(), so
pci_remove_bus() can be called for the device's subordinate bus before the
device itself is unregistered from the hierarchy. Still, the driver, if
any, should be detached from the device in pci_stop_dev(), so use
device_release_driver() directly from there.
References: https://bugzilla.kernel.org/show_bug.cgi?id=65281#c6
Reported-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
2013-12-18 20:53:32 +00:00
|
|
|
device_del(&dev->dev);
|
|
|
|
|
2014-02-01 14:38:29 +00:00
|
|
|
down_write(&pci_bus_sem);
|
|
|
|
list_del(&dev->bus_list);
|
|
|
|
up_write(&pci_bus_sem);
|
|
|
|
|
2023-03-11 14:40:12 +00:00
|
|
|
pci_doe_destroy(dev);
|
2018-09-04 17:34:18 +00:00
|
|
|
pcie_aspm_exit_link_state(dev);
|
2016-10-28 08:52:06 +00:00
|
|
|
pci_bridge_d3_update(dev);
|
2014-02-01 14:38:29 +00:00
|
|
|
pci_free_resources(dev);
|
2013-01-21 21:20:46 +00:00
|
|
|
put_device(&dev->dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 02:43:11 +00:00
|
|
|
void pci_remove_bus(struct pci_bus *bus)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2012-08-17 02:43:11 +00:00
|
|
|
pci_proc_detach_bus(bus);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-06-02 04:35:43 +00:00
|
|
|
down_write(&pci_bus_sem);
|
2012-08-17 02:43:11 +00:00
|
|
|
list_del(&bus->node);
|
|
|
|
pci_bus_release_busn_res(bus);
|
2006-06-02 04:35:43 +00:00
|
|
|
up_write(&pci_bus_sem);
|
2012-08-17 02:43:11 +00:00
|
|
|
pci_remove_legacy_files(bus);
|
2016-02-09 14:30:47 +00:00
|
|
|
|
|
|
|
if (bus->ops->remove_bus)
|
|
|
|
bus->ops->remove_bus(bus);
|
|
|
|
|
2013-04-12 05:44:20 +00:00
|
|
|
pcibios_remove_bus(bus);
|
2012-08-17 02:43:11 +00:00
|
|
|
device_unregister(&bus->dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(pci_remove_bus);
|
|
|
|
|
2012-09-19 18:54:20 +00:00
|
|
|
static void pci_stop_bus_device(struct pci_dev *dev)
|
2006-09-12 17:16:36 +00:00
|
|
|
{
|
2012-08-17 17:25:01 +00:00
|
|
|
struct pci_bus *bus = dev->subordinate;
|
|
|
|
struct pci_dev *child, *tmp;
|
|
|
|
|
|
|
|
/*
|
2012-09-19 18:54:20 +00:00
|
|
|
* Stopping an SR-IOV PF device removes all the associated VFs,
|
2012-08-17 17:25:01 +00:00
|
|
|
* which will update the bus->devices list and confuse the
|
|
|
|
* iterator. Therefore, iterate in reverse so we remove the VFs
|
|
|
|
* first, then the PF.
|
|
|
|
*/
|
2012-08-17 17:57:48 +00:00
|
|
|
if (bus) {
|
2012-08-17 17:25:01 +00:00
|
|
|
list_for_each_entry_safe_reverse(child, tmp,
|
|
|
|
&bus->devices, bus_list)
|
2012-09-19 18:54:20 +00:00
|
|
|
pci_stop_bus_device(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
pci_stop_dev(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_remove_bus_device(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
struct pci_bus *bus = dev->subordinate;
|
|
|
|
struct pci_dev *child, *tmp;
|
|
|
|
|
|
|
|
if (bus) {
|
|
|
|
list_for_each_entry_safe(child, tmp,
|
|
|
|
&bus->devices, bus_list)
|
|
|
|
pci_remove_bus_device(child);
|
2012-08-17 17:57:48 +00:00
|
|
|
|
|
|
|
pci_remove_bus(bus);
|
|
|
|
dev->subordinate = NULL;
|
|
|
|
}
|
2006-09-12 17:16:36 +00:00
|
|
|
|
2012-08-17 17:57:48 +00:00
|
|
|
pci_destroy_dev(dev);
|
2006-09-12 17:16:36 +00:00
|
|
|
}
|
2012-09-19 18:54:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* pci_stop_and_remove_bus_device - remove a PCI device and any children
|
|
|
|
* @dev: the device to remove
|
|
|
|
*
|
|
|
|
* Remove a PCI device from the device lists, informing the drivers
|
|
|
|
* that the device has been removed. We also remove any subordinate
|
|
|
|
* buses and children in a depth-first manner.
|
|
|
|
*
|
|
|
|
* For each device we remove, delete the device structure from the
|
|
|
|
* device lists, remove the /proc entry, and notify userspace
|
|
|
|
* (/sbin/hotplug).
|
|
|
|
*/
|
|
|
|
void pci_stop_and_remove_bus_device(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
pci_stop_bus_device(dev);
|
|
|
|
pci_remove_bus_device(dev);
|
|
|
|
}
|
2012-02-25 21:54:20 +00:00
|
|
|
EXPORT_SYMBOL(pci_stop_and_remove_bus_device);
|
2012-10-30 20:31:38 +00:00
|
|
|
|
2014-01-10 14:22:18 +00:00
|
|
|
void pci_stop_and_remove_bus_device_locked(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
pci_lock_rescan_remove();
|
|
|
|
pci_stop_and_remove_bus_device(dev);
|
|
|
|
pci_unlock_rescan_remove();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(pci_stop_and_remove_bus_device_locked);
|
|
|
|
|
2012-10-30 20:31:38 +00:00
|
|
|
void pci_stop_root_bus(struct pci_bus *bus)
|
|
|
|
{
|
|
|
|
struct pci_dev *child, *tmp;
|
|
|
|
struct pci_host_bridge *host_bridge;
|
|
|
|
|
|
|
|
if (!pci_is_root_bus(bus))
|
|
|
|
return;
|
|
|
|
|
|
|
|
host_bridge = to_pci_host_bridge(bus->bridge);
|
|
|
|
list_for_each_entry_safe_reverse(child, tmp,
|
|
|
|
&bus->devices, bus_list)
|
|
|
|
pci_stop_bus_device(child);
|
|
|
|
|
|
|
|
/* stop the host bridge */
|
2013-11-30 22:40:27 +00:00
|
|
|
device_release_driver(&host_bridge->dev);
|
2012-10-30 20:31:38 +00:00
|
|
|
}
|
2015-04-08 18:21:33 +00:00
|
|
|
EXPORT_SYMBOL_GPL(pci_stop_root_bus);
|
2012-10-30 20:31:38 +00:00
|
|
|
|
|
|
|
void pci_remove_root_bus(struct pci_bus *bus)
|
|
|
|
{
|
|
|
|
struct pci_dev *child, *tmp;
|
|
|
|
struct pci_host_bridge *host_bridge;
|
|
|
|
|
|
|
|
if (!pci_is_root_bus(bus))
|
|
|
|
return;
|
|
|
|
|
|
|
|
host_bridge = to_pci_host_bridge(bus->bridge);
|
|
|
|
list_for_each_entry_safe(child, tmp,
|
|
|
|
&bus->devices, bus_list)
|
|
|
|
pci_remove_bus_device(child);
|
|
|
|
|
2022-07-14 18:41:30 +00:00
|
|
|
#ifdef CONFIG_PCI_DOMAINS_GENERIC
|
|
|
|
/* Release domain_nr if it was dynamically allocated */
|
|
|
|
if (host_bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET)
|
|
|
|
pci_bus_release_domain_nr(bus, host_bridge->dev.parent);
|
|
|
|
#endif
|
|
|
|
|
2023-03-29 12:38:35 +00:00
|
|
|
pci_remove_bus(bus);
|
|
|
|
host_bridge->bus = NULL;
|
|
|
|
|
2012-10-30 20:31:38 +00:00
|
|
|
/* remove the host bridge */
|
2020-05-13 22:38:59 +00:00
|
|
|
device_del(&host_bridge->dev);
|
2012-10-30 20:31:38 +00:00
|
|
|
}
|
2015-04-08 18:21:33 +00:00
|
|
|
EXPORT_SYMBOL_GPL(pci_remove_root_bus);
|