[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 15/20] pci: implement power state
From: |
Michael S. Tsirkin |
Subject: |
[PULL 15/20] pci: implement power state |
Date: |
Mon, 15 Nov 2021 11:38:46 -0500 |
From: Gerd Hoffmann <kraxel@redhat.com>
This allows to power off pci devices. In "off" state the devices will
not be visible. No pci config space access, no pci bar access, no dma.
Default state is "on", so this patch (alone) should not change behavior.
Use case: Allows hotplug controllers implement slot power. Hotplug
controllers doing so should set the inital power state for devices in
the ->plug callback.
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Message-Id: <20211111130859.1171890-2-kraxel@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
include/hw/pci/pci.h | 2 ++
hw/pci/pci.c | 25 +++++++++++++++++++++++--
hw/pci/pci_host.c | 6 ++++--
3 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
index 5c4016b995..e7cdf2d5ec 100644
--- a/include/hw/pci/pci.h
+++ b/include/hw/pci/pci.h
@@ -268,6 +268,7 @@ typedef struct PCIReqIDCache PCIReqIDCache;
struct PCIDevice {
DeviceState qdev;
bool partially_hotplugged;
+ bool has_power;
/* PCI config space */
uint8_t *config;
@@ -908,5 +909,6 @@ extern const VMStateDescription vmstate_pci_device;
}
MSIMessage pci_get_msi_message(PCIDevice *dev, int vector);
+void pci_set_power(PCIDevice *pci_dev, bool state);
#endif
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 4a84e478ce..e5993c1ef5 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -1380,6 +1380,9 @@ static void pci_update_mappings(PCIDevice *d)
continue;
new_addr = pci_bar_address(d, i, r->type, r->size);
+ if (!d->has_power) {
+ new_addr = PCI_BAR_UNMAPPED;
+ }
/* This bar isn't changed */
if (new_addr == r->addr)
@@ -1464,8 +1467,8 @@ void pci_default_write_config(PCIDevice *d, uint32_t
addr, uint32_t val_in, int
if (range_covers_byte(addr, l, PCI_COMMAND)) {
pci_update_irq_disabled(d, was_irq_disabled);
memory_region_set_enabled(&d->bus_master_enable_region,
- pci_get_word(d->config + PCI_COMMAND)
- & PCI_COMMAND_MASTER);
+ (pci_get_word(d->config + PCI_COMMAND)
+ & PCI_COMMAND_MASTER) && d->has_power);
}
msi_write_config(d, addr, val_in, l);
@@ -2182,6 +2185,8 @@ static void pci_qdev_realize(DeviceState *qdev, Error
**errp)
pci_qdev_unrealize(DEVICE(pci_dev));
return;
}
+
+ pci_set_power(pci_dev, true);
}
PCIDevice *pci_new_multifunction(int devfn, bool multifunction,
@@ -2853,6 +2858,22 @@ MSIMessage pci_get_msi_message(PCIDevice *dev, int
vector)
return msg;
}
+void pci_set_power(PCIDevice *d, bool state)
+{
+ if (d->has_power == state) {
+ return;
+ }
+
+ d->has_power = state;
+ pci_update_mappings(d);
+ memory_region_set_enabled(&d->bus_master_enable_region,
+ (pci_get_word(d->config + PCI_COMMAND)
+ & PCI_COMMAND_MASTER) && d->has_power);
+ if (!d->has_power) {
+ pci_device_reset(d);
+ }
+}
+
static const TypeInfo pci_device_type_info = {
.name = TYPE_PCI_DEVICE,
.parent = TYPE_DEVICE,
diff --git a/hw/pci/pci_host.c b/hw/pci/pci_host.c
index cf02f0d6a5..7beafd40a8 100644
--- a/hw/pci/pci_host.c
+++ b/hw/pci/pci_host.c
@@ -74,7 +74,8 @@ void pci_host_config_write_common(PCIDevice *pci_dev,
uint32_t addr,
/* non-zero functions are only exposed when function 0 is present,
* allowing direct removal of unexposed functions.
*/
- if (pci_dev->qdev.hotplugged && !pci_get_function_0(pci_dev)) {
+ if ((pci_dev->qdev.hotplugged && !pci_get_function_0(pci_dev)) ||
+ !pci_dev->has_power) {
return;
}
@@ -97,7 +98,8 @@ uint32_t pci_host_config_read_common(PCIDevice *pci_dev,
uint32_t addr,
/* non-zero functions are only exposed when function 0 is present,
* allowing direct removal of unexposed functions.
*/
- if (pci_dev->qdev.hotplugged && !pci_get_function_0(pci_dev)) {
+ if ((pci_dev->qdev.hotplugged && !pci_get_function_0(pci_dev)) ||
+ !pci_dev->has_power) {
return ~0x0;
}
--
MST
- [PULL 06/20] pcie: rename 'native-hotplug' to 'x-native-hotplug', (continued)
- [PULL 06/20] pcie: rename 'native-hotplug' to 'x-native-hotplug', Michael S. Tsirkin, 2021/11/15
- [PULL 05/20] hw/mem/pc-dimm: Restrict NUMA-specific code to NUMA machines, Michael S. Tsirkin, 2021/11/15
- [PULL 07/20] hw/acpi/ich9: Add compat prop to keep HPC bit set for 6.1 machine type, Michael S. Tsirkin, 2021/11/15
- [PULL 08/20] bios-tables-test: Allow changes in DSDT ACPI tables, Michael S. Tsirkin, 2021/11/15
- [PULL 09/20] hw/i386/acpi-build: Deny control on PCIe Native Hot-plug in _OSC, Michael S. Tsirkin, 2021/11/15
- [PULL 10/20] tests: bios-tables-test update expected blobs, Michael S. Tsirkin, 2021/11/15
- [PULL 11/20] virtio: use virtio accessor to access packed descriptor flags, Michael S. Tsirkin, 2021/11/15
- [PULL 12/20] virtio: use virtio accessor to access packed event, Michael S. Tsirkin, 2021/11/15
- [PULL 13/20] vdpa: Replace qemu_open_old by qemu_open at, Michael S. Tsirkin, 2021/11/15
- [PULL 14/20] vdpa: Check for existence of opts.vhostdev, Michael S. Tsirkin, 2021/11/15
- [PULL 15/20] pci: implement power state,
Michael S. Tsirkin <=
- [PULL 16/20] pcie: implement slot power control for pcie root ports, Michael S. Tsirkin, 2021/11/15
- [PULL 17/20] pcie: add power indicator blink check, Michael S. Tsirkin, 2021/11/15
- [PULL 18/20] pcie: factor out pcie_cap_slot_unplug(), Michael S. Tsirkin, 2021/11/15
- [PULL 19/20] pcie: fast unplug when slot power is off, Michael S. Tsirkin, 2021/11/15
- [PULL 20/20] pcie: expire pending delete, Michael S. Tsirkin, 2021/11/15
- [PULL 03/20] vhost: Rename last_index to vq_index_end, Michael S. Tsirkin, 2021/11/15
- Re: [PULL 00/20] pci,pc,virtio: bugfixes, Philippe Mathieu-Daudé, 2021/11/15