All of lore.kernel.org
 help / color / mirror / Atom feed
From: Roger Pau Monne <roger.pau@citrix.com>
To: xen-devel@lists.xenproject.org
Cc: Roger Pau Monne <roger.pau@citrix.com>,
	Jan Beulich <jbeulich@suse.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>
Subject: [PATCH v4 2/6] amd-vi: set IOMMU page table levels based on guest reported paddr width
Date: Wed, 20 Dec 2023 14:43:42 +0100	[thread overview]
Message-ID: <20231220134346.22430-3-roger.pau@citrix.com> (raw)
In-Reply-To: <20231220134346.22430-1-roger.pau@citrix.com>

However take into account the minimum number of levels required by unity maps
when setting the page table levels.

The previous setting of the page table levels for PV guests based on the
highest RAM address was bogus, as there can be other non-RAM regions past the
highest RAM address that need to be mapped, for example device MMIO.

For HVM we also take amd_iommu_min_paging_mode into account, however if unity
maps require more than 4 levels attempting to add those will currently fail at
the p2m level, as 4 levels is the maximum supported.

Fixes: 0700c962ac2d ('Add AMD IOMMU support into hypervisor')
Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
---
Changes since v3:
 - Avoid possible UB if domain_max_paddr_bits() ever returns 64.

Changes since v2:
 - Use the renamed domain_max_paddr_bits().

Changes since v1:
 - Use paging_max_paddr_bits() instead of hardcoding
   DEFAULT_DOMAIN_ADDRESS_WIDTH.
---
 xen/drivers/passthrough/amd/pci_amd_iommu.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/xen/drivers/passthrough/amd/pci_amd_iommu.c b/xen/drivers/passthrough/amd/pci_amd_iommu.c
index 6bc73dc21052..146cf60084f4 100644
--- a/xen/drivers/passthrough/amd/pci_amd_iommu.c
+++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c
@@ -359,21 +359,17 @@ int __read_mostly amd_iommu_min_paging_mode = 1;
 static int cf_check amd_iommu_domain_init(struct domain *d)
 {
     struct domain_iommu *hd = dom_iommu(d);
+    int pglvl = amd_iommu_get_paging_mode(
+                1UL << (domain_max_paddr_bits(d) - PAGE_SHIFT));
+
+    if ( pglvl < 0 )
+        return pglvl;
 
     /*
-     * Choose the number of levels for the IOMMU page tables.
-     * - PV needs 3 or 4, depending on whether there is RAM (including hotplug
-     *   RAM) above the 512G boundary.
-     * - HVM could in principle use 3 or 4 depending on how much guest
-     *   physical address space we give it, but this isn't known yet so use 4
-     *   unilaterally.
-     * - Unity maps may require an even higher number.
+     * Choose the number of levels for the IOMMU page tables, taking into
+     * account unity maps.
      */
-    hd->arch.amd.paging_mode = max(amd_iommu_get_paging_mode(
-            is_hvm_domain(d)
-            ? 1UL << (DEFAULT_DOMAIN_ADDRESS_WIDTH - PAGE_SHIFT)
-            : get_upper_mfn_bound() + 1),
-        amd_iommu_min_paging_mode);
+    hd->arch.amd.paging_mode = max(pglvl, amd_iommu_min_paging_mode);
 
     return 0;
 }
-- 
2.43.0



  parent reply	other threads:[~2023-12-20 13:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-20 13:43 [PATCH v4 0/6] x86/iommu: improve setup time of hwdom IOMMU Roger Pau Monne
2023-12-20 13:43 ` [PATCH v4 1/6] x86/p2m: move and rename paging_max_paddr_bits() Roger Pau Monne
2023-12-20 13:43 ` Roger Pau Monne [this message]
2023-12-21  8:13   ` [PATCH v4 2/6] amd-vi: set IOMMU page table levels based on guest reported paddr width Jan Beulich
2023-12-20 13:43 ` [PATCH v4 3/6] x86/iommu: introduce a rangeset to perform hwdom IOMMU setup Roger Pau Monne
2024-01-09 11:29   ` Jan Beulich
2023-12-20 13:43 ` [PATCH v4 4/6] x86/iommu: remove regions not to be mapped Roger Pau Monne
2023-12-20 13:43 ` [PATCH v4 5/6] x86/iommu: switch hwdom IOMMU to use a rangeset Roger Pau Monne
2023-12-20 13:43 ` [PATCH v4 6/6] x86/iommu: cleanup unused functions Roger Pau Monne
2023-12-21 10:12 ` [PATCH] x86/iommu: add CHANGELOG entry for hwdom setup time improvements Roger Pau Monne
2023-12-21 10:23   ` Jan Beulich
2023-12-21 11:24     ` Roger Pau Monné
2024-01-09 11:38 ` [PATCH v4 0/6] x86/iommu: improve setup time of hwdom IOMMU Jan Beulich

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=20231220134346.22430-3-roger.pau@citrix.com \
    --to=roger.pau@citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jbeulich@suse.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.