All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joao Martins <joao.m.martins@oracle.com>
To: qemu-devel@nongnu.org
Cc: "Joao Martins" <joao.m.martins@oracle.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
	"Cornelia Huck" <cohuck@redhat.com>,
	"Juan Quintela" <quintela@redhat.com>,
	"Eric Blake" <eblake@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Jason Gunthorpe" <jgg@nvidia.com>,
	"Nicolin Chen" <nicolinc@nvidia.com>,
	"Yishai Hadas" <yishaih@nvidia.com>,
	"Kevin Tian" <kevin.tian@intel.com>,
	"Yi Liu" <yi.l.liu@intel.com>,
	"Eric Auger" <eric.auger@redhat.com>,
	"Thanos Makatos" <thanos.makatos@nutanix.com>,
	"John G . Johnson" <john.g.johnson@oracle.com>,
	kvm@vger.kernel.org
Subject: [PATCH RFC 01/10] amd-iommu: Cache PTE/DTE info in IOTLB
Date: Thu, 28 Apr 2022 22:13:42 +0100	[thread overview]
Message-ID: <20220428211351.3897-2-joao.m.martins@oracle.com> (raw)
In-Reply-To: <20220428211351.3897-1-joao.m.martins@oracle.com>

On a successful translation, cache the PTE and DTE
flags set at the time of the translation i.e. the first 12bits
as well as the PTE storage. These bits contain read, write,
dirty and access for example. In theory the DTE lookup takes
precendence in the translation path, but in the interest of
performance extend the AMDVIIOTLBEntry to include that information.

This is a preparatory for AMD HDSup/HASup which requires updating
A/D bits off the PTE (even after its insertion in the IOTLB) based
on the fact that HAD bits (0x3 or 0x1) were set on the Device
Table Entry.

Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
---
 hw/i386/amd_iommu.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
index ea8eaeb330b6..25b5c3be70ea 100644
--- a/hw/i386/amd_iommu.c
+++ b/hw/i386/amd_iommu.c
@@ -72,6 +72,9 @@ typedef struct AMDVIIOTLBEntry {
     uint64_t perms;             /* access permissions  */
     uint64_t translated_addr;   /* translated address  */
     uint64_t page_mask;         /* physical page size  */
+    uint16_t dte_flags;         /* device table entry flags */
+    uint64_t pte;               /* pte entry */
+    uint64_t pte_addr;          /* pte entry iova */
 } AMDVIIOTLBEntry;
 
 /* configure MMIO registers at startup/reset */
@@ -340,7 +343,8 @@ static void amdvi_iotlb_remove_page(AMDVIState *s, hwaddr addr,
 
 static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
                                uint64_t gpa, IOMMUTLBEntry to_cache,
-                               uint16_t domid)
+                               uint16_t domid, uint16_t dte_flags,
+                               uint64_t pte, uint64_t pte_addr)
 {
     AMDVIIOTLBEntry *entry = g_new(AMDVIIOTLBEntry, 1);
     uint64_t *key = g_new(uint64_t, 1);
@@ -359,6 +363,9 @@ static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
         entry->perms = to_cache.perm;
         entry->translated_addr = to_cache.translated_addr;
         entry->page_mask = to_cache.addr_mask;
+        entry->dte_flags = dte_flags;
+        entry->pte = pte;
+        entry->pte_addr = pte_addr;
         *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
         g_hash_table_replace(s->iotlb, key, entry);
     }
@@ -896,7 +903,8 @@ static inline uint64_t amdvi_get_pte_entry(AMDVIState *s, uint64_t pte_addr,
 
 static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
                             IOMMUTLBEntry *ret, unsigned perms,
-                            hwaddr addr)
+                            hwaddr addr, uint64_t *iotlb_pte,
+                            uint64_t *iotlb_pte_addr)
 {
     unsigned level, present, pte_perms, oldlevel;
     uint64_t pte = dte[0], pte_addr, page_mask;
@@ -945,6 +953,8 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
         ret->translated_addr = (pte & AMDVI_DEV_PT_ROOT_MASK) & page_mask;
         ret->addr_mask = ~page_mask;
         ret->perm = amdvi_get_perms(pte);
+        *iotlb_pte = pte;
+        *iotlb_pte_addr = addr;
         return;
     }
 no_remap:
@@ -952,6 +962,8 @@ no_remap:
     ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
     ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
     ret->perm = amdvi_get_perms(pte);
+    *iotlb_pte = pte;
+    *iotlb_pte_addr = addr;
 }
 
 static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
@@ -960,7 +972,7 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
     AMDVIState *s = as->iommu_state;
     uint16_t devid = PCI_BUILD_BDF(as->bus_num, as->devfn);
     AMDVIIOTLBEntry *iotlb_entry = amdvi_iotlb_lookup(s, addr, devid);
-    uint64_t entry[4];
+    uint64_t entry[4], pte, pte_addr;
 
     if (iotlb_entry) {
         trace_amdvi_iotlb_hit(PCI_BUS_NUM(devid), PCI_SLOT(devid),
@@ -982,10 +994,12 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
     }
 
     amdvi_page_walk(as, entry, ret,
-                    is_write ? AMDVI_PERM_WRITE : AMDVI_PERM_READ, addr);
+                    is_write ? AMDVI_PERM_WRITE : AMDVI_PERM_READ, addr,
+                    &pte, &pte_addr);
 
     amdvi_update_iotlb(s, devid, addr, *ret,
-                       entry[1] & AMDVI_DEV_DOMID_ID_MASK);
+                       entry[1] & AMDVI_DEV_DOMID_ID_MASK,
+                       entry[0] & ~AMDVI_DEV_PT_ROOT_MASK, pte, pte_addr);
     return;
 
 out:
-- 
2.17.2


WARNING: multiple messages have this Message-ID (diff)
From: Joao Martins <joao.m.martins@oracle.com>
To: qemu-devel@nongnu.org
Cc: "John G . Johnson" <john.g.johnson@oracle.com>,
	kvm@vger.kernel.org, "Michael S. Tsirkin" <mst@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Joao Martins" <joao.m.martins@oracle.com>,
	"Eric Blake" <eblake@redhat.com>, "Yi Liu" <yi.l.liu@intel.com>,
	"Juan Quintela" <quintela@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Nicolin Chen" <nicolinc@nvidia.com>,
	"Jason Gunthorpe" <jgg@nvidia.com>,
	"Kevin Tian" <kevin.tian@intel.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	"Eric Auger" <eric.auger@redhat.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"Thanos Makatos" <thanos.makatos@nutanix.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Yishai Hadas" <yishaih@nvidia.com>,
	"Cornelia Huck" <cohuck@redhat.com>,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PATCH RFC 01/10] amd-iommu: Cache PTE/DTE info in IOTLB
Date: Thu, 28 Apr 2022 22:13:42 +0100	[thread overview]
Message-ID: <20220428211351.3897-2-joao.m.martins@oracle.com> (raw)
In-Reply-To: <20220428211351.3897-1-joao.m.martins@oracle.com>

On a successful translation, cache the PTE and DTE
flags set at the time of the translation i.e. the first 12bits
as well as the PTE storage. These bits contain read, write,
dirty and access for example. In theory the DTE lookup takes
precendence in the translation path, but in the interest of
performance extend the AMDVIIOTLBEntry to include that information.

This is a preparatory for AMD HDSup/HASup which requires updating
A/D bits off the PTE (even after its insertion in the IOTLB) based
on the fact that HAD bits (0x3 or 0x1) were set on the Device
Table Entry.

Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
---
 hw/i386/amd_iommu.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
index ea8eaeb330b6..25b5c3be70ea 100644
--- a/hw/i386/amd_iommu.c
+++ b/hw/i386/amd_iommu.c
@@ -72,6 +72,9 @@ typedef struct AMDVIIOTLBEntry {
     uint64_t perms;             /* access permissions  */
     uint64_t translated_addr;   /* translated address  */
     uint64_t page_mask;         /* physical page size  */
+    uint16_t dte_flags;         /* device table entry flags */
+    uint64_t pte;               /* pte entry */
+    uint64_t pte_addr;          /* pte entry iova */
 } AMDVIIOTLBEntry;
 
 /* configure MMIO registers at startup/reset */
@@ -340,7 +343,8 @@ static void amdvi_iotlb_remove_page(AMDVIState *s, hwaddr addr,
 
 static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
                                uint64_t gpa, IOMMUTLBEntry to_cache,
-                               uint16_t domid)
+                               uint16_t domid, uint16_t dte_flags,
+                               uint64_t pte, uint64_t pte_addr)
 {
     AMDVIIOTLBEntry *entry = g_new(AMDVIIOTLBEntry, 1);
     uint64_t *key = g_new(uint64_t, 1);
@@ -359,6 +363,9 @@ static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
         entry->perms = to_cache.perm;
         entry->translated_addr = to_cache.translated_addr;
         entry->page_mask = to_cache.addr_mask;
+        entry->dte_flags = dte_flags;
+        entry->pte = pte;
+        entry->pte_addr = pte_addr;
         *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
         g_hash_table_replace(s->iotlb, key, entry);
     }
@@ -896,7 +903,8 @@ static inline uint64_t amdvi_get_pte_entry(AMDVIState *s, uint64_t pte_addr,
 
 static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
                             IOMMUTLBEntry *ret, unsigned perms,
-                            hwaddr addr)
+                            hwaddr addr, uint64_t *iotlb_pte,
+                            uint64_t *iotlb_pte_addr)
 {
     unsigned level, present, pte_perms, oldlevel;
     uint64_t pte = dte[0], pte_addr, page_mask;
@@ -945,6 +953,8 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
         ret->translated_addr = (pte & AMDVI_DEV_PT_ROOT_MASK) & page_mask;
         ret->addr_mask = ~page_mask;
         ret->perm = amdvi_get_perms(pte);
+        *iotlb_pte = pte;
+        *iotlb_pte_addr = addr;
         return;
     }
 no_remap:
@@ -952,6 +962,8 @@ no_remap:
     ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
     ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
     ret->perm = amdvi_get_perms(pte);
+    *iotlb_pte = pte;
+    *iotlb_pte_addr = addr;
 }
 
 static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
@@ -960,7 +972,7 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
     AMDVIState *s = as->iommu_state;
     uint16_t devid = PCI_BUILD_BDF(as->bus_num, as->devfn);
     AMDVIIOTLBEntry *iotlb_entry = amdvi_iotlb_lookup(s, addr, devid);
-    uint64_t entry[4];
+    uint64_t entry[4], pte, pte_addr;
 
     if (iotlb_entry) {
         trace_amdvi_iotlb_hit(PCI_BUS_NUM(devid), PCI_SLOT(devid),
@@ -982,10 +994,12 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
     }
 
     amdvi_page_walk(as, entry, ret,
-                    is_write ? AMDVI_PERM_WRITE : AMDVI_PERM_READ, addr);
+                    is_write ? AMDVI_PERM_WRITE : AMDVI_PERM_READ, addr,
+                    &pte, &pte_addr);
 
     amdvi_update_iotlb(s, devid, addr, *ret,
-                       entry[1] & AMDVI_DEV_DOMID_ID_MASK);
+                       entry[1] & AMDVI_DEV_DOMID_ID_MASK,
+                       entry[0] & ~AMDVI_DEV_PT_ROOT_MASK, pte, pte_addr);
     return;
 
 out:
-- 
2.17.2



  reply	other threads:[~2022-04-28 21:14 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-28 21:13 [PATCH RFC 00/10] hw/vfio, x86/iommu: IOMMUFD Dirty Tracking Joao Martins
2022-04-28 21:13 ` Joao Martins
2022-04-28 21:13 ` Joao Martins [this message]
2022-04-28 21:13   ` [PATCH RFC 01/10] amd-iommu: Cache PTE/DTE info in IOTLB Joao Martins
2022-04-28 21:13 ` [PATCH RFC 02/10] amd-iommu: Access/Dirty bit support Joao Martins
2022-04-28 21:13   ` Joao Martins
2022-04-28 21:13 ` [PATCH RFC 03/10] intel-iommu: Cache PASID entry flags Joao Martins
2022-04-28 21:13   ` Joao Martins
2022-04-28 21:13 ` [PATCH RFC 04/10] intel_iommu: Second Stage Access Dirty bit support Joao Martins
2022-04-28 21:13   ` Joao Martins
2022-04-29  2:26   ` Jason Wang
2022-04-29  2:26     ` Jason Wang
2022-04-29  9:12     ` Joao Martins
2022-04-29  9:12       ` Joao Martins
2022-04-29 18:21       ` Peter Xu
2022-04-29 18:21         ` Peter Xu
2022-05-03 11:54         ` Joao Martins
2022-05-05  7:41           ` Jason Wang
2022-05-05  9:57             ` Joao Martins
2022-05-04 20:11   ` Peter Xu
2022-05-05  9:54     ` Joao Martins
2022-04-28 21:13 ` [PATCH RFC 05/10] linux-headers: import iommufd.h hwpt extensions Joao Martins
2022-04-28 21:13   ` Joao Martins
2022-04-28 21:13 ` [PATCH RFC 06/10] vfio/iommufd: Add HWPT_SET_DIRTY support Joao Martins
2022-04-28 21:13   ` Joao Martins
2022-04-28 21:13 ` [PATCH RFC 07/10] vfio/iommufd: Add HWPT_GET_DIRTY_IOVA support Joao Martins
2022-04-28 21:13   ` Joao Martins
2022-04-28 21:13 ` [PATCH RFC 08/10] vfio/iommufd: Add IOAS_UNMAP_DIRTY support Joao Martins
2022-04-28 21:13   ` Joao Martins
2022-04-28 21:13 ` [PATCH RFC 09/10] migration/dirtyrate: Expand dirty_bitmap to be tracked separately for devices Joao Martins
2022-04-28 21:13   ` Joao Martins
2022-05-02 12:54   ` Markus Armbruster
2022-05-02 12:54     ` Markus Armbruster
2022-05-02 14:35     ` Joao Martins
2022-05-02 14:35       ` Joao Martins
2022-04-28 21:13 ` [PATCH RFC 10/10] hw/vfio: Add nr of dirty pages to tracepoints Joao Martins
2022-04-28 21:13   ` Joao Martins

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=20220428211351.3897-2-joao.m.martins@oracle.com \
    --to=joao.m.martins@oracle.com \
    --cc=alex.williamson@redhat.com \
    --cc=armbru@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=david@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=eric.auger@redhat.com \
    --cc=f4bug@amsat.org \
    --cc=jasowang@redhat.com \
    --cc=jgg@nvidia.com \
    --cc=john.g.johnson@oracle.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=nicolinc@nvidia.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=richard.henderson@linaro.org \
    --cc=thanos.makatos@nutanix.com \
    --cc=yi.l.liu@intel.com \
    --cc=yishaih@nvidia.com \
    /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.