qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: qemu-devel@nongnu.org, lersek@redhat.com
Cc: Marcel Apfelbaum <marcel@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>
Subject: [Qemu-devel] [PATCH 3/4] hw/pci: allow the caller of pci_bar_address() to ignore command register
Date: Sat,  6 Jun 2015 01:46:28 +0200	[thread overview]
Message-ID: <1433547989-7238-4-git-send-email-lersek@redhat.com> (raw)
In-Reply-To: <1433547989-7238-1-git-send-email-lersek@redhat.com>

The pci_bar_address() function calculates the start address of a given PCI
BAR. Its main purpose is supporting MemoryRegion mapping / unmapping in
pci_update_mappings(), which is guest-observable.

For that reason it contains heavy logic to enforce the validity of the
BAR, and this logic includes checks for the PCI command register:
- If the PCI_COMMAND_IO bit is clear in the command register, then all IO
  space BARs will be reported unmapped (all bits one),
- If the PCI_COMMAND_MEMORY bit is clear in the command register, then all
  MMIO space BARs will be reported unmapped (all bits one).

For an upcoming use case, we'd like pci_bar_address() to calculate the BAR
address from the config space *regardless* of the command register. Add a
new parameter that allows the caller to cut out the command register
verification.

All current callers preserve their behaviors.

Cc: Marcel Apfelbaum <marcel@redhat.com>
Cc: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 include/hw/pci/pci.h |  2 ++
 hw/pci/pci.c         | 18 +++++++++++-------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
index d44bc84..16b467b 100644
--- a/include/hw/pci/pci.h
+++ b/include/hw/pci/pci.h
@@ -314,6 +314,8 @@ void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
                       MemoryRegion *io_lo, MemoryRegion *io_hi);
 void pci_unregister_vga(PCIDevice *pci_dev);
 pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num);
+pcibus_t pci_bar_address(PCIDevice *d, int reg, uint8_t type, pcibus_t size,
+                         bool respect_command);
 
 int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
                        uint8_t offset, uint8_t size);
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 750f3da..61a70de 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -1054,15 +1054,19 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
     return pci_dev->io_regions[region_num].addr;
 }
 
-static pcibus_t pci_bar_address(PCIDevice *d,
-				int reg, uint8_t type, pcibus_t size)
+pcibus_t pci_bar_address(PCIDevice *d, int reg, uint8_t type, pcibus_t size,
+                         bool respect_command)
 {
     pcibus_t new_addr, last_addr;
     int bar = pci_bar(d, reg);
-    uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
+    uint16_t cmd;
+
+    if (respect_command) {
+        cmd = pci_get_word(d->config + PCI_COMMAND);
+    }
 
     if (type & PCI_BASE_ADDRESS_SPACE_IO) {
-        if (!(cmd & PCI_COMMAND_IO)) {
+        if (respect_command && !(cmd & PCI_COMMAND_IO)) {
             return PCI_BAR_UNMAPPED;
         }
         new_addr = pci_get_long(d->config + bar) & ~(size - 1);
@@ -1076,7 +1080,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,
         return new_addr;
     }
 
-    if (!(cmd & PCI_COMMAND_MEMORY)) {
+    if (respect_command && !(cmd & PCI_COMMAND_MEMORY)) {
         return PCI_BAR_UNMAPPED;
     }
     if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
@@ -1134,7 +1138,7 @@ static void pci_update_mappings(PCIDevice *d)
         if (!r->size)
             continue;
 
-        new_addr = pci_bar_address(d, i, r->type, r->size);
+        new_addr = pci_bar_address(d, i, r->type, r->size, true);
 
         /* This bar isn't changed */
         if (new_addr == r->addr)
@@ -2427,7 +2431,7 @@ static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque)
             !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
             continue;
         }
-        region_range.begin = pci_bar_address(dev, i, r->type, r->size);
+        region_range.begin = pci_bar_address(dev, i, r->type, r->size, true);
         region_range.end = region_range.begin + r->size;
 
         if (region_range.begin == PCI_BAR_UNMAPPED) {
-- 
1.8.3.1

  parent reply	other threads:[~2015-06-05 23:46 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-05 23:45 [Qemu-devel] PXB fixes for QEMU, and extra root buses for OVMF Laszlo Ersek
2015-06-05 23:46 ` [Qemu-devel] [PATCH 0/4] PXB tweaks and fixes Laszlo Ersek
2015-06-05 23:46   ` [Qemu-devel] [PATCH 1/4] i386/acpi-build: more traditional _UID and _HID for PXB root buses Laszlo Ersek
2015-06-10  9:16     ` Marcel Apfelbaum
2015-06-05 23:46   ` [Qemu-devel] [PATCH 2/4] i386/acpi-build: fix PXB workarounds for unsupported BIOSes Laszlo Ersek
2015-06-10  9:17     ` Marcel Apfelbaum
2015-06-05 23:46   ` Laszlo Ersek [this message]
2015-06-05 23:46   ` [Qemu-devel] [PATCH 4/4] i386/acpi-build: build_crs(): fetch BAR from PCI config space directly Laszlo Ersek
2015-06-07  9:23     ` Michael S. Tsirkin
2015-06-08  7:56       ` Laszlo Ersek
2015-06-08  9:40         ` Michael S. Tsirkin
2015-06-09 20:34       ` Laszlo Ersek
2015-06-10 10:06         ` Marcel Apfelbaum
2015-06-10 11:07           ` Laszlo Ersek
2015-06-10 16:21             ` Michael S. Tsirkin
2015-06-10 16:19         ` Michael S. Tsirkin
2015-06-10  9:09 ` [Qemu-devel] PXB fixes for QEMU, and extra root buses for OVMF Marcel Apfelbaum
2015-06-10 11:04   ` Laszlo Ersek
2015-06-10 11:55     ` Marcel Apfelbaum
2015-06-10 12:05       ` Laszlo Ersek

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=1433547989-7238-4-git-send-email-lersek@redhat.com \
    --to=lersek@redhat.com \
    --cc=marcel@redhat.com \
    --cc=mst@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).