From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
"Eduardo Habkost" <ehabkost@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PATCH 1/6] pci: implement power state
Date: Mon, 11 Oct 2021 14:04:59 +0200 [thread overview]
Message-ID: <20211011120504.254053-2-kraxel@redhat.com> (raw)
In-Reply-To: <20211011120504.254053-1-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>
---
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 7fc90132cf1d..7cb6d92e17f7 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;
@@ -904,5 +905,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 186758ee11fe..94f53944c7cc 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);
@@ -2190,6 +2193,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,
@@ -2861,6 +2866,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 cf02f0d6a501..7beafd40a8e9 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;
}
--
2.31.1
next prev parent reply other threads:[~2021-10-11 12:11 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-11 12:04 [PATCH 0/6] RfC: try improve native hotplug for pcie root ports Gerd Hoffmann
2021-10-11 12:04 ` Gerd Hoffmann [this message]
2021-10-11 12:05 ` [PATCH 2/6] pcie: implement slow power control " Gerd Hoffmann
2021-10-11 12:05 ` [PATCH 3/6] pcie: add power indicator blink check Gerd Hoffmann
2021-11-15 11:29 ` Michael S. Tsirkin
2021-11-15 14:52 ` Gerd Hoffmann
2021-10-11 12:05 ` [PATCH 4/6] pcie: factor out pcie_cap_slot_unplug() Gerd Hoffmann
2021-10-11 12:05 ` [PATCH 5/6] pcie: fast unplug when slot power is off Gerd Hoffmann
2021-10-12 5:56 ` Michael S. Tsirkin
2021-10-12 6:46 ` Gerd Hoffmann
2021-10-11 12:05 ` [PATCH 6/6] pcie: expire pending delete Gerd Hoffmann
2021-10-11 12:49 ` Michael S. Tsirkin
2021-10-12 5:30 ` Gerd Hoffmann
2021-10-12 5:46 ` Michael S. Tsirkin
2021-10-12 6:44 ` Gerd Hoffmann
2021-10-12 7:01 ` Michael S. Tsirkin
2021-10-18 15:36 ` [PATCH 0/6] RfC: try improve native hotplug for pcie root ports Michael S. Tsirkin
2021-10-19 5:21 ` Gerd Hoffmann
2021-10-19 5:46 ` Michael S. Tsirkin
2021-10-19 6:29 ` Gerd Hoffmann
2021-11-01 21:47 ` Michael S. Tsirkin
2021-11-02 12:09 ` Gerd Hoffmann
2021-11-10 12:02 ` Michael S. Tsirkin
2021-11-11 7:53 ` Gerd Hoffmann
2021-11-11 8:20 ` Michael S. Tsirkin
2021-11-11 9:34 ` Gerd Hoffmann
2021-11-11 12:09 ` Gerd Hoffmann
2021-11-11 15:39 ` Michael S. Tsirkin
2021-11-12 11:15 ` Gerd Hoffmann
2021-11-12 12:17 ` Igor Mammedov
2021-11-15 11:13 ` Michael S. Tsirkin
2021-11-11 9:35 ` Daniel P. Berrangé
2021-11-11 17:11 ` Michael S. Tsirkin
2021-11-11 18:08 ` Daniel P. Berrangé
2021-11-11 18:43 ` Michael S. Tsirkin
2021-11-12 10:16 ` Gerd Hoffmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211011120504.254053-2-kraxel@redhat.com \
--to=kraxel@redhat.com \
--cc=berrange@redhat.com \
--cc=ehabkost@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).