All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stewart Hildebrand <stewart.hildebrand@amd.com>
To: <xen-devel@lists.xenproject.org>
Cc: "Oleksandr Andrushchenko" <oleksandr_andrushchenko@epam.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>,
	"Volodymyr Babchuk" <volodymyr_babchuk@epam.com>,
	"Stewart Hildebrand" <stewart.hildebrand@amd.com>
Subject: [PATCH v12 05/15] vpci/header: implement guest BAR register handlers
Date: Tue, 9 Jan 2024 16:51:20 -0500	[thread overview]
Message-ID: <20240109215145.430207-6-stewart.hildebrand@amd.com> (raw)
In-Reply-To: <20240109215145.430207-1-stewart.hildebrand@amd.com>

From: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>

Add relevant vpci register handlers when assigning PCI device to a domain
and remove those when de-assigning. This allows having different
handlers for different domains, e.g. hwdom and other guests.

Emulate guest BAR register values: this allows creating a guest view
of the registers and emulates size and properties probe as it is done
during PCI device enumeration by the guest.

All empty, IO and ROM BARs for guests are emulated by returning 0 on
reads and ignoring writes: this BARs are special with this respect as
their lower bits have special meaning, so returning default ~0 on read
may confuse guest OS.

Introduce is_hwdom convenience variable and convert an existing
is_hardware_domain() check.

Signed-off-by: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
Signed-off-by: Volodymyr Babchuk <volodymyr_babchuk@epam.com>
Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>
Signed-off-by: Stewart Hildebrand <stewart.hildebrand@amd.com>
---
In v12:
- Add Roger's R-b
- Get rid of empty_bar_read, use vpci_read_val instead
- Convert an existing is_hardware_domain() check to is_hwdom
- Re-indent usage of ternary operator
- Use ternary operator to avoid re-indenting expansion ROM block
In v11:
- Access guest_addr after adjusting for MEM64_HI bar in
guest_bar_write()
- guest bar handlers renamed and now  _mem_ part to denote
that they are handling only memory BARs
- refuse to update guest BAR address if BAR is enabled
In v10:
- ull -> ULL to be MISRA-compatbile
- Use PAGE_OFFSET() instead of combining with ~PAGE_MASK
- Set type of empty bars to VPCI_BAR_EMPTY
In v9:
- factored-out "fail" label introduction in init_bars()
- replaced #ifdef CONFIG_X86 with IS_ENABLED()
- do not pass bars[i] to empty_bar_read() handler
- store guest's BAR address instead of guests BAR register view
Since v6:
- unify the writing of the PCI_COMMAND register on the
  error path into a label
- do not introduce bar_ignore_access helper and open code
- s/guest_bar_ignore_read/empty_bar_read
- update error message in guest_bar_write
- only setup empty_bar_read for IO if !x86
Since v5:
- make sure that the guest set address has the same page offset
  as the physical address on the host
- remove guest_rom_{read|write} as those just implement the default
  behaviour of the registers not being handled
- adjusted comment for struct vpci.addr field
- add guest handlers for BARs which are not handled and will otherwise
  return ~0 on read and ignore writes. The BARs are special with this
  respect as their lower bits have special meaning, so returning ~0
  doesn't seem to be right
Since v4:
- updated commit message
- s/guest_addr/guest_reg
Since v3:
- squashed two patches: dynamic add/remove handlers and guest BAR
  handler implementation
- fix guest BAR read of the high part of a 64bit BAR (Roger)
- add error handling to vpci_assign_device
- s/dom%pd/%pd
- blank line before return
Since v2:
- remove unneeded ifdefs for CONFIG_HAS_VPCI_GUEST_SUPPORT as more code
  has been eliminated from being built on x86
Since v1:
 - constify struct pci_dev where possible
 - do not open code is_system_domain()
 - simplify some code3. simplify
 - use gdprintk + error code instead of gprintk
 - gate vpci_bar_{add|remove}_handlers with CONFIG_HAS_VPCI_GUEST_SUPPORT,
   so these do not get compiled for x86
 - removed unneeded is_system_domain check
 - re-work guest read/write to be much simpler and do more work on write
   than read which is expected to be called more frequently
 - removed one too obvious comment
---
 xen/drivers/vpci/header.c | 109 +++++++++++++++++++++++++++++++++++---
 xen/include/xen/vpci.h    |   3 ++
 2 files changed, 106 insertions(+), 6 deletions(-)

diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c
index 803fe4bb99a6..39e11e141b38 100644
--- a/xen/drivers/vpci/header.c
+++ b/xen/drivers/vpci/header.c
@@ -478,6 +478,69 @@ static void cf_check bar_write(
     pci_conf_write32(pdev->sbdf, reg, val);
 }
 
+static void cf_check guest_mem_bar_write(const struct pci_dev *pdev,
+                                         unsigned int reg, uint32_t val,
+                                         void *data)
+{
+    struct vpci_bar *bar = data;
+    bool hi = false;
+    uint64_t guest_addr;
+
+    if ( bar->type == VPCI_BAR_MEM64_HI )
+    {
+        ASSERT(reg > PCI_BASE_ADDRESS_0);
+        bar--;
+        hi = true;
+    }
+    else
+    {
+        val &= PCI_BASE_ADDRESS_MEM_MASK;
+    }
+
+    guest_addr = bar->guest_addr;
+    guest_addr &= ~(0xffffffffULL << (hi ? 32 : 0));
+    guest_addr |= (uint64_t)val << (hi ? 32 : 0);
+
+    /* Allow guest to size BAR correctly */
+    guest_addr &= ~(bar->size - 1);
+
+    /*
+     * Xen only cares whether the BAR is mapped into the p2m, so allow BAR
+     * writes as long as the BAR is not mapped into the p2m.
+     */
+    if ( bar->enabled )
+    {
+        /* If the value written is the current one avoid printing a warning. */
+        if ( guest_addr != bar->guest_addr )
+            gprintk(XENLOG_WARNING,
+                    "%pp: ignored guest BAR %zu write while mapped\n",
+                    &pdev->sbdf, bar - pdev->vpci->header.bars + hi);
+        return;
+    }
+    bar->guest_addr = guest_addr;
+}
+
+static uint32_t cf_check guest_mem_bar_read(const struct pci_dev *pdev,
+                                            unsigned int reg, void *data)
+{
+    const struct vpci_bar *bar = data;
+    uint32_t reg_val;
+
+    if ( bar->type == VPCI_BAR_MEM64_HI )
+    {
+        ASSERT(reg > PCI_BASE_ADDRESS_0);
+        bar--;
+        return bar->guest_addr >> 32;
+    }
+
+    reg_val = bar->guest_addr;
+    reg_val |= bar->type == VPCI_BAR_MEM32 ? PCI_BASE_ADDRESS_MEM_TYPE_32 :
+                                             PCI_BASE_ADDRESS_MEM_TYPE_64;
+    reg_val |= bar->prefetchable ? PCI_BASE_ADDRESS_MEM_PREFETCH : 0;
+
+    return reg_val;
+}
+
 static void cf_check rom_write(
     const struct pci_dev *pdev, unsigned int reg, uint32_t val, void *data)
 {
@@ -539,6 +602,7 @@ static int cf_check init_header(struct pci_dev *pdev)
     struct vpci_bar *bars = header->bars;
     int rc;
     bool mask_cap_list = false;
+    bool is_hwdom = is_hardware_domain(pdev->domain);
 
     ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));
 
@@ -564,7 +628,7 @@ static int cf_check init_header(struct pci_dev *pdev)
     if ( rc )
         return rc;
 
-    if ( !is_hardware_domain(pdev->domain) )
+    if ( !is_hwdom )
     {
         if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
         {
@@ -653,8 +717,11 @@ static int cf_check init_header(struct pci_dev *pdev)
         if ( i && bars[i - 1].type == VPCI_BAR_MEM64_LO )
         {
             bars[i].type = VPCI_BAR_MEM64_HI;
-            rc = vpci_add_register(pdev->vpci, vpci_hw_read32, bar_write, reg,
-                                   4, &bars[i]);
+            rc = vpci_add_register(pdev->vpci,
+                                   is_hwdom ? vpci_hw_read32
+                                            : guest_mem_bar_read,
+                                   is_hwdom ? bar_write : guest_mem_bar_write,
+                                   reg, 4, &bars[i]);
             if ( rc )
                 goto fail;
 
@@ -665,6 +732,14 @@ static int cf_check init_header(struct pci_dev *pdev)
         if ( (val & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO )
         {
             bars[i].type = VPCI_BAR_IO;
+            if ( !IS_ENABLED(CONFIG_X86) && !is_hwdom )
+            {
+                rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                                       reg, 4, (void *)0);
+                if ( rc )
+                    goto fail;
+            }
+
             continue;
         }
         if ( (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
@@ -681,6 +756,15 @@ static int cf_check init_header(struct pci_dev *pdev)
         if ( size == 0 )
         {
             bars[i].type = VPCI_BAR_EMPTY;
+
+            if ( !is_hwdom )
+            {
+                rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                                       reg, 4, (void *)0);
+                if ( rc )
+                    goto fail;
+            }
+
             continue;
         }
 
@@ -688,14 +772,18 @@ static int cf_check init_header(struct pci_dev *pdev)
         bars[i].size = size;
         bars[i].prefetchable = val & PCI_BASE_ADDRESS_MEM_PREFETCH;
 
-        rc = vpci_add_register(pdev->vpci, vpci_hw_read32, bar_write, reg, 4,
-                               &bars[i]);
+        rc = vpci_add_register(pdev->vpci,
+                               is_hwdom ? vpci_hw_read32 : guest_mem_bar_read,
+                               is_hwdom ? bar_write : guest_mem_bar_write,
+                               reg, 4, &bars[i]);
         if ( rc )
             goto fail;
     }
 
     /* Check expansion ROM. */
-    rc = pci_size_mem_bar(pdev->sbdf, rom_reg, &addr, &size, PCI_BAR_ROM);
+    rc = is_hwdom ? pci_size_mem_bar(pdev->sbdf, rom_reg, &addr, &size,
+                                     PCI_BAR_ROM)
+                  : 0;
     if ( rc > 0 && size )
     {
         struct vpci_bar *rom = &header->bars[num_bars];
@@ -711,6 +799,15 @@ static int cf_check init_header(struct pci_dev *pdev)
         if ( rc )
             rom->type = VPCI_BAR_EMPTY;
     }
+    else if ( !is_hwdom )
+    {
+        /* TODO: Check expansion ROM, we do not handle ROM for guests for now */
+        header->bars[num_bars].type = VPCI_BAR_EMPTY;
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               rom_reg, 4, (void *)0);
+        if ( rc )
+            goto fail;
+    }
 
     return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0;
 
diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h
index 99fe76f08ace..b0e38a5a1acb 100644
--- a/xen/include/xen/vpci.h
+++ b/xen/include/xen/vpci.h
@@ -87,7 +87,10 @@ struct vpci {
     struct vpci_header {
         /* Information about the PCI BARs of this device. */
         struct vpci_bar {
+            /* Physical (host) address. */
             uint64_t addr;
+            /* Guest address. */
+            uint64_t guest_addr;
             uint64_t size;
             enum {
                 VPCI_BAR_EMPTY,
-- 
2.43.0



  parent reply	other threads:[~2024-01-09 21:53 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-09 21:51 [PATCH v12 00/15] PCI devices passthrough on Arm, part 3 Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 01/15] vpci: use per-domain PCI lock to protect vpci structure Stewart Hildebrand
2024-01-12 13:48   ` Roger Pau Monné
2024-01-12 17:54     ` Stewart Hildebrand
2024-01-12 18:14       ` [PATCH v12.1 " Stewart Hildebrand
2024-01-15  8:58         ` Jan Beulich
2024-01-15 15:42           ` Stewart Hildebrand
2024-01-15  8:53       ` [PATCH v12 " Roger Pau Monné
2024-01-15 15:08         ` Stewart Hildebrand
2024-01-15 19:43   ` [PATCH v12.2 " Stewart Hildebrand
2024-01-19 13:42     ` Roger Pau Monné
2024-01-23 14:26     ` Jan Beulich
2024-01-23 15:23       ` Roger Pau Monné
2024-01-24  8:56         ` Jan Beulich
2024-01-24  9:39           ` Roger Pau Monné
2024-01-23 14:29     ` Jan Beulich
2024-01-24  5:07       ` Stewart Hildebrand
2024-01-24  8:21         ` Roger Pau Monné
2024-01-24 20:21           ` Stewart Hildebrand
2024-01-24  8:50         ` Jan Beulich
2024-01-23 14:32     ` Jan Beulich
2024-01-23 15:07       ` Roger Pau Monné
2024-01-24  5:00         ` Stewart Hildebrand
2024-01-30 14:59           ` Stewart Hildebrand
2024-01-24  8:48         ` Jan Beulich
2024-01-24  9:24           ` Roger Pau Monné
2024-01-24 11:34             ` Jan Beulich
2024-01-24 17:51               ` Roger Pau Monné
2024-01-25  7:43                 ` Jan Beulich
2024-01-25  9:05                   ` Roger Pau Monné
2024-01-25 11:23                     ` Jan Beulich
2024-01-25 12:33                       ` Roger Pau Monné
2024-01-30 15:04                         ` Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 02/15] vpci: restrict unhandled read/write operations for guests Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 03/15] vpci: add hooks for PCI device assign/de-assign Stewart Hildebrand
2024-01-23 14:36   ` Jan Beulich
2024-01-30 19:22   ` Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 04/15] vpci/header: rework exit path in init_header() Stewart Hildebrand
2024-01-09 21:51 ` Stewart Hildebrand [this message]
2024-01-09 21:51 ` [PATCH v12 06/15] rangeset: add RANGESETF_no_print flag Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 07/15] rangeset: add rangeset_purge() function Stewart Hildebrand
2024-01-10 10:00   ` Jan Beulich
2024-01-09 21:51 ` [PATCH v12 08/15] vpci/header: handle p2m range sets per BAR Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 09/15] vpci/header: program p2m with guest BAR view Stewart Hildebrand
2024-01-12 15:06   ` Roger Pau Monné
2024-01-12 20:31     ` Stewart Hildebrand
2024-01-12 20:49       ` [PATCH v12.1 " Stewart Hildebrand
2024-01-15  9:07     ` [PATCH v12 " Jan Beulich
2024-01-15 19:03       ` Stewart Hildebrand
2024-01-15 19:44   ` [PATCH v12.2 " Stewart Hildebrand
2024-01-17  3:01     ` Stewart Hildebrand
2024-01-19 13:43       ` Roger Pau Monné
2024-01-19 14:28   ` [PATCH v12.3 " Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 10/15] vpci/header: emulate PCI_COMMAND register for guests Stewart Hildebrand
2024-01-25 15:43   ` Jan Beulich
2024-02-01  4:50     ` Stewart Hildebrand
2024-02-01  8:14       ` Jan Beulich
2024-01-09 21:51 ` [PATCH v12 11/15] vpci: add initial support for virtual PCI bus topology Stewart Hildebrand
2024-01-12 11:46   ` George Dunlap
2024-01-12 13:50     ` Stewart Hildebrand
2024-01-15 11:48       ` George Dunlap
2024-01-25 16:00   ` Jan Beulich
2024-02-02  3:30     ` Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 12/15] xen/arm: translate virtual PCI bus topology for guests Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 13/15] xen/arm: account IO handlers for emulated PCI MSI-X Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 14/15] xen/arm: vpci: permit access to guest vpci space Stewart Hildebrand
2024-01-17  3:03   ` Stewart Hildebrand
2024-01-09 21:51 ` [PATCH v12 15/15] arm/vpci: honor access size when returning an error Stewart Hildebrand

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=20240109215145.430207-6-stewart.hildebrand@amd.com \
    --to=stewart.hildebrand@amd.com \
    --cc=oleksandr_andrushchenko@epam.com \
    --cc=roger.pau@citrix.com \
    --cc=volodymyr_babchuk@epam.com \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.