Skip to contentSkip to conversation

AMD/IOMMU: Cleanup

PATCH In review Andrew Cooper 3 participants 21 messages Feb 3, 2020 – Feb 11, 2020

lore
Share & tools
lore
#0 lore raw

cover AMD/IOMMU: Cleanup

Various bits of cleanup without any major functional change. The eventual plan is to purge the {get,set}_field_{from,in}_u32() helpers, which are responsible for very hard to follow code, and poor code generation.

Andrew Cooper (4):
AMD/IOMMU: Move headers to be local
AMD/IOMMU: Delete iommu_{get,set,clear}_bit() helpers AMD/IOMMU: Treat guest head/tail pointers as byte offsets AMD/IOMMU: Treat head/tail pointers as byte offsets

 xen/arch/x86/mm/p2m.c                              |   1 -
 .../passthrough/amd/iommu-defs.h}                  |  41 ++--
 .../passthrough/amd/iommu.h}                       | 206 +++++++++++++++++----
 xen/drivers/passthrough/amd/iommu_acpi.c           |   7 +-
 xen/drivers/passthrough/amd/iommu_cmd.c            |  32 ++--
 xen/drivers/passthrough/amd/iommu_detect.c         |   7 +-
 xen/drivers/passthrough/amd/iommu_guest.c          | 132 ++++++-------
 xen/drivers/passthrough/amd/iommu_init.c           |  64 +++----
 xen/drivers/passthrough/amd/iommu_intr.c           |   9 +-
 xen/drivers/passthrough/amd/iommu_map.c            |   8 +-
 xen/drivers/passthrough/amd/pci_amd_iommu.c        |   9 +-
 xen/include/asm-x86/amd-iommu.h                    | 190 -------------------
 12 files changed, 298 insertions(+), 408 deletions(-)
 rename xen/{include/asm-x86/hvm/svm/amd-iommu-defs.h => drivers/passthrough/amd/iommu-defs.h} (92%)
 rename xen/{include/asm-x86/hvm/svm/amd-iommu-proto.h => drivers/passthrough/amd/iommu.h} (68%)
 delete mode 100644 xen/include/asm-x86/amd-iommu.h
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/4 AMD/IOMMU: Move headers to be local

We currently have amd-iommu-defs.h, amd-iommu-proto.h and amd-iommu.h, and no references outside of the AMD IOMMU driver.

Keep iommu-defs.h as is, but merge amd-iommu.h and amd-iommu-proto.h to just iommu.h, and move them both into drivers/passthrough/amd/. (While merging, drop the bogus #pragma pack around the *_entry structures.)

Take the opportunity to trim the include lists, including x86/mm/p2m.c which (AFAICT) hasn't needed this include since c/s aef3f2275 "x86/mm/p2m:
break into common, pt-implementation and pod parts" in 2011.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: George Dunlap <[email protected]>

---
 xen/arch/x86/mm/p2m.c                              |   1 -
 .../passthrough/amd/iommu-defs.h}                  |   6 +-
 .../passthrough/amd/iommu.h}                       | 179 ++++++++++++++++++-
 xen/drivers/passthrough/amd/iommu_acpi.c           |   7 +-
 xen/drivers/passthrough/amd/iommu_cmd.c            |   4 +-
 xen/drivers/passthrough/amd/iommu_detect.c         |   7 +-
 xen/drivers/passthrough/amd/iommu_guest.c          |   4 +-
 xen/drivers/passthrough/amd/iommu_init.c           |  13 +-
 xen/drivers/passthrough/amd/iommu_intr.c           |   9 +-
 xen/drivers/passthrough/amd/iommu_map.c            |   8 +-
 xen/drivers/passthrough/amd/pci_amd_iommu.c        |   9 +-
 xen/include/asm-x86/amd-iommu.h                    | 190 ---------------------
 12 files changed, 194 insertions(+), 243 deletions(-)
 rename xen/{include/asm-x86/hvm/svm/amd-iommu-defs.h => drivers/passthrough/amd/iommu-defs.h} (99%)
 rename xen/{include/asm-x86/hvm/svm/amd-iommu-proto.h => drivers/passthrough/amd/iommu.h} (70%)
 delete mode 100644 xen/include/asm-x86/amd-iommu.h
xen/arch/x86/mm/p2m.c +0 -1
index def13f657b..fd9f09536d 100644
@@ -38,7 +38,6 @@
 #include <asm/mem_sharing.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/altp2m.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
 #include <asm/vm_event.h>
 #include <xsm/xsm.h>
 
xen/drivers/passthrough/amd/iommu-defs.h renamed from xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +3 -3
similarity index 99%
rename from xen/include/asm-x86/hvm/svm/amd-iommu-defs.h
rename to xen/drivers/passthrough/amd/iommu-defs.h
index 78368f16d9..f8b62cb033 100644
@@ -17,8 +17,8 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef _ASM_X86_64_AMD_IOMMU_DEFS_H
-#define _ASM_X86_64_AMD_IOMMU_DEFS_H
+#ifndef AMD_IOMMU_DEFS_H
+#define AMD_IOMMU_DEFS_H
 
 /* IOMMU Command Buffer entries: in power of 2 increments, minimum of 256 */
 #define IOMMU_CMD_BUFFER_DEFAULT_ENTRIES	512
@@ -506,7 +506,7 @@ struct amd_iommu_pte {
 #define IOMMU_REG_BASE_ADDR_HIGH_MASK               0x000FFFFF
 #define IOMMU_REG_BASE_ADDR_HIGH_SHIFT              0
 
-#endif /* _ASM_X86_64_AMD_IOMMU_DEFS_H */
+#endif /* AMD_IOMMU_DEFS_H */
 
 /*
  * Local variables:
xen/drivers/passthrough/amd/iommu.h renamed from xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +172 -7

191 lines not on this page · raw message

xen/drivers/passthrough/amd/iommu_acpi.c +3 -4
index 6c5f8e46ec..f4abbfd9dc 100644
@@ -17,13 +17,12 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <xen/errno.h>
 #include <xen/acpi.h>
 #include <xen/param.h>
-#include <asm/apicdef.h>
+
 #include <asm/io_apic.h>
-#include <asm/amd-iommu.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
+
+#include "iommu.h"
 
 /* Some helper structures, particularly to deal with ranges. */
 
xen/drivers/passthrough/amd/iommu_cmd.c +1 -3
index af3a1fb865..92eaab407b 100644
@@ -17,9 +17,7 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <xen/sched.h>
-#include <asm/amd-iommu.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
+#include "iommu.h"
 #include "../ats.h"
 
 static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
xen/drivers/passthrough/amd/iommu_detect.c +2 -5
index d782e66eee..8312bb4b6f 100644
@@ -17,13 +17,10 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <xen/errno.h>
 #include <xen/acpi.h>
-#include <xen/iommu.h>
 #include <xen/pci.h>
-#include <xen/pci_regs.h>
-#include <asm/amd-iommu.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
+
+#include "iommu.h"
 
 static int __init get_iommu_msi_capabilities(
     u16 seg, u8 bus, u8 dev, u8 func, struct amd_iommu *iommu)
xen/drivers/passthrough/amd/iommu_guest.c +1 -3
index 4ed6519e6e..aaf12fe1cb 100644
@@ -16,11 +16,9 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <xen/sched.h>
 #include <asm/p2m.h>
-#include <asm/amd-iommu.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
 
+#include "iommu.h"
 
 #define IOMMU_MMIO_SIZE                         0x8000
 #define IOMMU_MMIO_PAGE_NR                      0x8
xen/drivers/passthrough/amd/iommu_init.c +3 -10
index 2f26fed4a3..0ffc83a843 100644
@@ -17,18 +17,11 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <xen/errno.h>
 #include <xen/acpi.h>
-#include <xen/keyhandler.h>
-#include <xen/pci.h>
-#include <xen/pci_regs.h>
-#include <xen/irq.h>
-#include <asm/amd-iommu.h>
-#include <asm/msi.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
-#include <asm-x86/fixmap.h>
-#include <mach_apic.h>
 #include <xen/delay.h>
+#include <xen/keyhandler.h>
+
+#include "iommu.h"
 
 static int __initdata nr_amd_iommus;
 static bool __initdata pci_init;
xen/drivers/passthrough/amd/iommu_intr.c +4 -5
index 5e92c023f8..e1cc13b873 100644
@@ -16,13 +16,12 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <xen/err.h>
-#include <xen/sched.h>
-#include <asm/amd-iommu.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
-#include <asm/io_apic.h>
 #include <xen/softirq.h>
 
+#include <asm/io_apic.h>
+
+#include "iommu.h"
+
 union irte32 {
     uint32_t raw;
     struct {
xen/drivers/passthrough/amd/iommu_map.c +2 -6
index 4e041b960f..2f3b47b366 100644
@@ -18,12 +18,8 @@
  */
 
 #include <xen/acpi.h>
-#include <xen/sched.h>
-#include <asm/p2m.h>
-#include <asm/amd-iommu.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
-#include "../ats.h"
-#include <xen/pci.h>
+
+#include "iommu.h"
 
 /* Given pfn and page table level, return pde index */
 static unsigned int pfn_to_pde_idx(unsigned long pfn, unsigned int level)
xen/drivers/passthrough/amd/pci_amd_iommu.c +3 -6
index dd3401f0dc..3112653960 100644
@@ -17,15 +17,12 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <xen/sched.h>
 #include <xen/iocap.h>
-#include <xen/pci.h>
-#include <xen/pci_regs.h>
-#include <xen/paging.h>
 #include <xen/softirq.h>
+
 #include <asm/acpi.h>
-#include <asm/amd-iommu.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
+
+#include "iommu.h"
 #include "../ats.h"
 
 static bool_t __read_mostly init_done;
xen/include/asm-x86/amd-iommu.h deleted +0 -190

190 lines not on this page · raw message

Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 1/4
#2 lore raw

Re: [Xen-devel] [PATCH 1/4] AMD/IOMMU: Move headers to be local

Quoted 42 lines · Andrew Cooper

We currently have amd-iommu-defs.h, amd-iommu-proto.h and amd-iommu.h, and no references outside of the AMD IOMMU driver.

Keep iommu-defs.h as is, but merge amd-iommu.h and amd-iommu-proto.h to just iommu.h, and move them both into drivers/passthrough/amd/. (While merging, drop the bogus #pragma pack around the *_entry structures.)

Take the opportunity to trim the include lists, including x86/mm/p2m.c which (AFAICT) hasn't needed this include since c/s aef3f2275 "x86/mm/p2m:
break into common, pt-implementation and pod parts" in 2011.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: George Dunlap <[email protected]>

---
 xen/arch/x86/mm/p2m.c                              |   1 -
 .../passthrough/amd/iommu-defs.h}                  |   6 +-
 .../passthrough/amd/iommu.h}                       | 179 ++++++++++++++++++-
 xen/drivers/passthrough/amd/iommu_acpi.c           |   7 +-
 xen/drivers/passthrough/amd/iommu_cmd.c            |   4 +-
 xen/drivers/passthrough/amd/iommu_detect.c         |   7 +-
 xen/drivers/passthrough/amd/iommu_guest.c          |   4 +-
 xen/drivers/passthrough/amd/iommu_init.c           |  13 +-
 xen/drivers/passthrough/amd/iommu_intr.c           |   9 +-
 xen/drivers/passthrough/amd/iommu_map.c            |   8 +-
 xen/drivers/passthrough/amd/pci_amd_iommu.c        |   9 +-
 xen/include/asm-x86/amd-iommu.h                    | 190 ---------------------
 12 files changed, 194 insertions(+), 243 deletions(-)
 rename xen/{include/asm-x86/hvm/svm/amd-iommu-defs.h => drivers/passthrough/amd/iommu-defs.h} (99%)
 rename xen/{include/asm-x86/hvm/svm/amd-iommu-proto.h => drivers/passthrough/amd/iommu.h} (70%)
 delete mode 100644 xen/include/asm-x86/amd-iommu.h
xen/arch/x86/mm/p2m.c +0 -0
index def13f657b..fd9f09536d 100644

xen/arch/x86/mm/p2m.c line 43

Quoted 7 lines
xen/arch/x86/mm/p2m.c +0 -1
@@ -38,7 +38,6 @@
 #include <asm/mem_sharing.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/altp2m.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
 #include <asm/vm_event.h>
 #include <xsm/xsm.h>
 

p2m bits:

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v1 1/4
#3 lore raw

Re: [Xen-devel] [PATCH 1/4] AMD/IOMMU: Move headers to be local

In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper

We currently have amd-iommu-defs.h, amd-iommu-proto.h and amd-iommu.h, and no references outside of the AMD IOMMU driver.

Keep iommu-defs.h as is, but merge amd-iommu.h and amd-iommu-proto.h to just iommu.h, and move them both into drivers/passthrough/amd/. (While merging, drop the bogus #pragma pack around the *_entry structures.)

Take the opportunity to trim the include lists, including x86/mm/p2m.c

I guess you mean p2m.h here.

Quoted 6 lines

which (AFAICT) hasn't needed this include since c/s aef3f2275 "x86/mm/p2m:
break into common, pt-implementation and pod parts" in 2011.

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Re: [Xen-devel] [PATCH 1/4] AMD/IOMMU: Move headers to be local

Quoted 10 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper

We currently have amd-iommu-defs.h, amd-iommu-proto.h and amd-iommu.h, and no references outside of the AMD IOMMU driver.

Keep iommu-defs.h as is, but merge amd-iommu.h and amd-iommu-proto.h to just iommu.h, and move them both into drivers/passthrough/amd/. (While merging, drop the bogus #pragma pack around the *_entry structures.)

Take the opportunity to trim the include lists, including x86/mm/p2m.c

I guess you mean p2m.h here.

Why?

xen/arch/x86/mm/p2m.c line 43

Quoted 12 lines
xen/arch/x86/mm/p2m.c +0 -1
index def13f657b..fd9f09536d 100644
@@ -38,7 +38,6 @@
 #include <asm/mem_sharing.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/altp2m.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
 #include <asm/vm_event.h>
 #include <xsm/xsm.h>
 

I really do mean p2m.c

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Re: [Xen-devel] [PATCH 1/4] AMD/IOMMU: Move headers to be local

Quoted 14 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper

We currently have amd-iommu-defs.h, amd-iommu-proto.h and amd-iommu.h, and no references outside of the AMD IOMMU driver.

Keep iommu-defs.h as is, but merge amd-iommu.h and amd-iommu-proto.h to just iommu.h, and move them both into drivers/passthrough/amd/. (While merging, drop the bogus #pragma pack around the *_entry structures.)

Take the opportunity to trim the include lists, including x86/mm/p2m.c

I guess you mean p2m.h here.

Why?

Quoted 14 lines

xen/arch/x86/mm/p2m.c line 43

Quoted 12 lines
xen/arch/x86/mm/p2m.c +0 -1
index def13f657b..fd9f09536d 100644
@@ -38,7 +38,6 @@
 #include <asm/mem_sharing.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/altp2m.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
 #include <asm/vm_event.h>
 #include <xsm/xsm.h>
 

I really do mean p2m.c

Okay, I misunderstood then. I've been judging from

Quoted 4 lines
xen/drivers/passthrough/amd/iommu_map.c +0 -0
@@ -18,12 +18,8 @@
 */

xen/drivers/passthrough/amd/iommu_map.c line 22

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_map.c +2 -6
 
 #include <xen/acpi.h>
-#include <xen/sched.h>
-#include <asm/p2m.h>
-#include <asm/amd-iommu.h>
-#include <asm/hvm/svm/amd-iommu-proto.h>
-#include "../ats.h"
-#include <xen/pci.h>
+
+#include "iommu.h"

where you _also_ (re)move p2m.h.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

PATCH 2/4 AMD/IOMMU: Delete iommu_{get, set, clear}_bit() helpers

In reply to Andrew Cooper

These are obfuscations around simple bit operations, and the compiler really can do a better job when it can see them normally:

add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-181 (-181)

  Function                                     old     new   delta
  guest_iommu_add_ppr_log                      266     251     -15
  guest_iommu_add_event_log                    266     251     -15
  iommu_reset_log                              274     250     -24
  guest_iommu_process_command                 1602    1544     -58
  guest_iommu_mmio_write                      1123    1054     -69

Total: Before=3014099, After=3013918, chg -0.01%

Drop all status register MASK/SHIFT constants, and enumerate the bits normally. Rename EVENT_OVERFLOW to EVENT_LOG_OVERFLOW for consistency. (The field name in the spec is inconsistent, despite the description referring to an overflow of the event log.)

The only semantic change is in iommu_reset_log() where 'run_bit' changes from being a bit position to being a single-bit mask.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/drivers/passthrough/amd/iommu-defs.h  | 34 +++++++++---------------
 xen/drivers/passthrough/amd/iommu.h       | 15 -----------
 xen/drivers/passthrough/amd/iommu_cmd.c   |  8 +++---
 xen/drivers/passthrough/amd/iommu_guest.c | 43 +++++++++++++------------------
 xen/drivers/passthrough/amd/iommu_init.c  | 21 +++++++--------
 5 files changed, 43 insertions(+), 78 deletions(-)
xen/drivers/passthrough/amd/iommu-defs.h +12 -22
index f8b62cb033..963009de6a 100644
@@ -437,28 +437,18 @@ union amd_iommu_x2apic_control {
 
 /* Status Register*/
 #define IOMMU_STATUS_MMIO_OFFSET		0x2020
-#define IOMMU_STATUS_EVENT_OVERFLOW_MASK	0x00000001
-#define IOMMU_STATUS_EVENT_OVERFLOW_SHIFT	0
-#define IOMMU_STATUS_EVENT_LOG_INT_MASK		0x00000002
-#define IOMMU_STATUS_EVENT_LOG_INT_SHIFT	1
-#define IOMMU_STATUS_COMP_WAIT_INT_MASK		0x00000004
-#define IOMMU_STATUS_COMP_WAIT_INT_SHIFT	2
-#define IOMMU_STATUS_EVENT_LOG_RUN_MASK		0x00000008
-#define IOMMU_STATUS_EVENT_LOG_RUN_SHIFT	3
-#define IOMMU_STATUS_CMD_BUFFER_RUN_MASK	0x00000010
-#define IOMMU_STATUS_CMD_BUFFER_RUN_SHIFT	4
-#define IOMMU_STATUS_PPR_LOG_OVERFLOW_MASK      0x00000020
-#define IOMMU_STATUS_PPR_LOG_OVERFLOW_SHIFT     5
-#define IOMMU_STATUS_PPR_LOG_INT_MASK           0x00000040
-#define IOMMU_STATUS_PPR_LOG_INT_SHIFT          6
-#define IOMMU_STATUS_PPR_LOG_RUN_MASK           0x00000080
-#define IOMMU_STATUS_PPR_LOG_RUN_SHIFT          7
-#define IOMMU_STATUS_GAPIC_LOG_OVERFLOW_MASK    0x00000100
-#define IOMMU_STATUS_GAPIC_LOG_OVERFLOW_SHIFT   8
-#define IOMMU_STATUS_GAPIC_LOG_INT_MASK         0x00000200
-#define IOMMU_STATUS_GAPIC_LOG_INT_SHIFT        9
-#define IOMMU_STATUS_GAPIC_LOG_RUN_MASK         0x00000400
-#define IOMMU_STATUS_GAPIC_LOG_RUN_SHIFT        10
+
+#define IOMMU_STATUS_EVENT_LOG_OVERFLOW   0x00000001
+#define IOMMU_STATUS_EVENT_LOG_INT        0x00000002
+#define IOMMU_STATUS_COMP_WAIT_INT        0x00000004
+#define IOMMU_STATUS_EVENT_LOG_RUN        0x00000008
+#define IOMMU_STATUS_CMD_BUFFER_RUN       0x00000010
+#define IOMMU_STATUS_PPR_LOG_OVERFLOW     0x00000020
+#define IOMMU_STATUS_PPR_LOG_INT          0x00000040
+#define IOMMU_STATUS_PPR_LOG_RUN          0x00000080
+#define IOMMU_STATUS_GAPIC_LOG_OVERFLOW   0x00000100
+#define IOMMU_STATUS_GAPIC_LOG_INT        0x00000200
+#define IOMMU_STATUS_GAPIC_LOG_RUN        0x00000400
 
 /* I/O Page Table */
 #define IOMMU_PAGE_TABLE_ENTRY_SIZE	8
xen/drivers/passthrough/amd/iommu.h +0 -15
index f590de8cbf..81b6812d3a 100644
@@ -374,21 +374,6 @@ static inline void __free_amd_iommu_tables(void *table, int order)
     free_xenheap_pages(table, order);
 }
 
-static inline void iommu_set_bit(uint32_t *reg, uint32_t bit)
-{
-    set_field_in_reg_u32(IOMMU_CONTROL_ENABLED, *reg, 1U << bit, bit, reg);
-}
-
-static inline void iommu_clear_bit(uint32_t *reg, uint32_t bit)
-{
-    set_field_in_reg_u32(IOMMU_CONTROL_DISABLED, *reg, 1U << bit, bit, reg);
-}
-
-static inline uint32_t iommu_get_bit(uint32_t reg, uint32_t bit)
-{
-    return get_field_from_reg_u32(reg, 1U << bit, bit);
-}
-
 static inline int iommu_has_cap(struct amd_iommu *iommu, uint32_t bit)
 {
     return !!(iommu->cap.header & (1u << bit));
xen/drivers/passthrough/amd/iommu_cmd.c +3 -5
index 92eaab407b..166f0e7263 100644
@@ -68,7 +68,7 @@ static void flush_command_buffer(struct amd_iommu *iommu)
     int loop_count, comp_wait;
 
     /* RW1C 'ComWaitInt' in status register */
-    writel(IOMMU_STATUS_COMP_WAIT_INT_MASK,
+    writel(IOMMU_STATUS_COMP_WAIT_INT,
            iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
 
     /* send an empty COMPLETION_WAIT command to flush command buffer */
@@ -85,16 +85,14 @@ static void flush_command_buffer(struct amd_iommu *iommu)
     loop_count = 1000;
     do {
         status = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-        comp_wait = get_field_from_reg_u32(status,
-                                           IOMMU_STATUS_COMP_WAIT_INT_MASK,
-                                           IOMMU_STATUS_COMP_WAIT_INT_SHIFT);
+        comp_wait = status & IOMMU_STATUS_COMP_WAIT_INT;
         --loop_count;
     } while ( !comp_wait && loop_count );
 
     if ( comp_wait )
     {
         /* RW1C 'ComWaitInt' in status register */
-        writel(IOMMU_STATUS_COMP_WAIT_INT_MASK,
+        writel(IOMMU_STATUS_COMP_WAIT_INT,
                iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
         return;
     }
xen/drivers/passthrough/amd/iommu_guest.c +18 -25

99 lines not on this page · raw message

xen/drivers/passthrough/amd/iommu_init.c +10 -11
index 0ffc83a843..7bf6fef3ee 100644
@@ -351,13 +351,12 @@ static void iommu_reset_log(struct amd_iommu *iommu,
     BUG_ON(!iommu || ((log != &iommu->event_log) && (log != &iommu->ppr_log)));
 
     run_bit = ( log == &iommu->event_log ) ?
-        IOMMU_STATUS_EVENT_LOG_RUN_SHIFT :
-        IOMMU_STATUS_PPR_LOG_RUN_SHIFT;
+        IOMMU_STATUS_EVENT_LOG_RUN : IOMMU_STATUS_PPR_LOG_RUN;
 
     /* wait until EventLogRun bit = 0 */
     do {
         entry = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-        log_run = iommu_get_bit(entry, run_bit);
+        log_run = entry & run_bit;
         loop_count--;
     } while ( log_run && loop_count );
 
@@ -371,8 +370,8 @@ static void iommu_reset_log(struct amd_iommu *iommu,
     ctrl_func(iommu, IOMMU_CONTROL_DISABLED);
 
     /* RW1C overflow bit */
-    writel(log == &iommu->event_log ? IOMMU_STATUS_EVENT_OVERFLOW_MASK
-                                    : IOMMU_STATUS_PPR_LOG_OVERFLOW_MASK,
+    writel(log == &iommu->event_log ? IOMMU_STATUS_EVENT_LOG_OVERFLOW
+                                    : IOMMU_STATUS_PPR_LOG_OVERFLOW,
            iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
 
     /*reset event log base address */
@@ -589,7 +588,7 @@ static void iommu_check_event_log(struct amd_iommu *iommu)
     unsigned long flags;
 
     /* RW1C interrupt status bit */
-    writel(IOMMU_STATUS_EVENT_LOG_INT_MASK,
+    writel(IOMMU_STATUS_EVENT_LOG_INT,
            iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
 
     iommu_read_log(iommu, &iommu->event_log,
@@ -599,7 +598,7 @@ static void iommu_check_event_log(struct amd_iommu *iommu)
     
     /* Check event overflow. */
     entry = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-    if ( iommu_get_bit(entry, IOMMU_STATUS_EVENT_OVERFLOW_SHIFT) )
+    if ( entry & IOMMU_STATUS_EVENT_LOG_OVERFLOW )
         iommu_reset_log(iommu, &iommu->event_log, set_iommu_event_log_control);
     else
     {
@@ -621,7 +620,7 @@ static void iommu_check_event_log(struct amd_iommu *iommu)
      * Re-check to make sure the bit has been cleared.
      */
     entry = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-    if ( entry & IOMMU_STATUS_EVENT_LOG_INT_MASK )
+    if ( entry & IOMMU_STATUS_EVENT_LOG_INT )
         tasklet_schedule(&amd_iommu_irq_tasklet);
 
     spin_unlock_irqrestore(&iommu->lock, flags);
@@ -678,7 +677,7 @@ static void iommu_check_ppr_log(struct amd_iommu *iommu)
     unsigned long flags;
 
     /* RW1C interrupt status bit */
-    writel(IOMMU_STATUS_PPR_LOG_INT_MASK,
+    writel(IOMMU_STATUS_PPR_LOG_INT,
            iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
 
     iommu_read_log(iommu, &iommu->ppr_log,
@@ -688,7 +687,7 @@ static void iommu_check_ppr_log(struct amd_iommu *iommu)
 
     /* Check event overflow. */
     entry = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-    if ( iommu_get_bit(entry, IOMMU_STATUS_PPR_LOG_OVERFLOW_SHIFT) )
+    if ( entry & IOMMU_STATUS_PPR_LOG_OVERFLOW )
         iommu_reset_log(iommu, &iommu->ppr_log, set_iommu_ppr_log_control);
     else
     {
@@ -710,7 +709,7 @@ static void iommu_check_ppr_log(struct amd_iommu *iommu)
      * Re-check to make sure the bit has been cleared.
      */
     entry = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-    if ( entry & IOMMU_STATUS_PPR_LOG_INT_MASK )
+    if ( entry & IOMMU_STATUS_PPR_LOG_INT )
         tasklet_schedule(&amd_iommu_irq_tasklet);
 
     spin_unlock_irqrestore(&iommu->lock, flags);
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 2/4
#7 lore raw

Re: [Xen-devel] [PATCH 2/4] AMD/IOMMU: Delete iommu_{get, set, clear}_bit() helpers

xen/drivers/passthrough/amd/iommu_cmd.c static void flush_command_buffer(struct amd_iommu *iommu) · line 88

Quoted 8 lines · Andrew Cooper
xen/drivers/passthrough/amd/iommu_cmd.c +1 -3
@@ -85,16 +85,14 @@ static void flush_command_buffer(struct amd_iommu *iommu)
     loop_count = 1000;
     do {
         status = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-        comp_wait = get_field_from_reg_u32(status,
-                                           IOMMU_STATUS_COMP_WAIT_INT_MASK,
-                                           IOMMU_STATUS_COMP_WAIT_INT_SHIFT);
+        comp_wait = status & IOMMU_STATUS_COMP_WAIT_INT;

Unless you also change comp_wait to bool, this just happens to be correct this way because of the bit checked being at a low enough position.

Quoted 5 lines
xen/drivers/passthrough/amd/iommu_init.c +0 -0
@@ -351,13 +351,12 @@ static void iommu_reset_log(struct amd_iommu *iommu,
     BUG_ON(!iommu || ((log != &iommu->event_log) && (log != &iommu->ppr_log)));
 

xen/drivers/passthrough/amd/iommu_init.c static void iommu_reset_log(struct amd_iommu *iommu, · line 359

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_init.c +2 -3
     run_bit = ( log == &iommu->event_log ) ?
-        IOMMU_STATUS_EVENT_LOG_RUN_SHIFT :
-        IOMMU_STATUS_PPR_LOG_RUN_SHIFT;
+        IOMMU_STATUS_EVENT_LOG_RUN : IOMMU_STATUS_PPR_LOG_RUN;
 
     /* wait until EventLogRun bit = 0 */
     do {
         entry = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-        log_run = iommu_get_bit(entry, run_bit);
+        log_run = entry & run_bit;

Same here for log_run then. I also think run_bit would better become unsigned int then.

With these taken care of

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Re: [Xen-devel] [PATCH 2/4] AMD/IOMMU: Delete iommu_{get, set, clear}_bit() helpers

Quoted 31 lines · Jan Beulich

xen/drivers/passthrough/amd/iommu_cmd.c static void flush_command_buffer(struct amd_iommu *iommu) · line 88

Quoted 8 lines · Andrew Cooper
xen/drivers/passthrough/amd/iommu_cmd.c +1 -3
@@ -85,16 +85,14 @@ static void flush_command_buffer(struct amd_iommu *iommu)
     loop_count = 1000;
     do {
         status = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-        comp_wait = get_field_from_reg_u32(status,
-                                           IOMMU_STATUS_COMP_WAIT_INT_MASK,
-                                           IOMMU_STATUS_COMP_WAIT_INT_SHIFT);
+        comp_wait = status & IOMMU_STATUS_COMP_WAIT_INT;

Unless you also change comp_wait to bool, this just happens to be correct this way because of the bit checked being at a low enough position.

Quoted 5 lines
xen/drivers/passthrough/amd/iommu_init.c +0 -0
@@ -351,13 +351,12 @@ static void iommu_reset_log(struct amd_iommu *iommu,
     BUG_ON(!iommu || ((log != &iommu->event_log) && (log != &iommu->ppr_log)));
 

xen/drivers/passthrough/amd/iommu_init.c static void iommu_reset_log(struct amd_iommu *iommu, · line 359

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_init.c +2 -3
     run_bit = ( log == &iommu->event_log ) ?
-        IOMMU_STATUS_EVENT_LOG_RUN_SHIFT :
-        IOMMU_STATUS_PPR_LOG_RUN_SHIFT;
+        IOMMU_STATUS_EVENT_LOG_RUN : IOMMU_STATUS_PPR_LOG_RUN;
 
     /* wait until EventLogRun bit = 0 */
     do {
         entry = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-        log_run = iommu_get_bit(entry, run_bit);
+        log_run = entry & run_bit;

Same here for log_run then. I also think run_bit would better become unsigned int then.

Quoted 2 lines

With these taken care of

I have separate cleanup doing that (and more).  I don't want to conflate unrelated changes - this patch is complicated enough to follow.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

Re: [Xen-devel] [PATCH 2/4] AMD/IOMMU: Delete iommu_{get, set, clear}_bit() helpers

Quoted 32 lines · Andrew Cooper
Quoted 31 lines · Jan Beulich

xen/drivers/passthrough/amd/iommu_cmd.c static void flush_command_buffer(struct amd_iommu *iommu) · line 88

Quoted 8 lines · Andrew Cooper
xen/drivers/passthrough/amd/iommu_cmd.c +1 -3
@@ -85,16 +85,14 @@ static void flush_command_buffer(struct amd_iommu *iommu)
     loop_count = 1000;
     do {
         status = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-        comp_wait = get_field_from_reg_u32(status,
-                                           IOMMU_STATUS_COMP_WAIT_INT_MASK,
-                                           IOMMU_STATUS_COMP_WAIT_INT_SHIFT);
+        comp_wait = status & IOMMU_STATUS_COMP_WAIT_INT;

Unless you also change comp_wait to bool, this just happens to be correct this way because of the bit checked being at a low enough position.

Quoted 5 lines
xen/drivers/passthrough/amd/iommu_init.c +0 -0
@@ -351,13 +351,12 @@ static void iommu_reset_log(struct amd_iommu *iommu,
     BUG_ON(!iommu || ((log != &iommu->event_log) && (log != &iommu->ppr_log)));
 

xen/drivers/passthrough/amd/iommu_init.c static void iommu_reset_log(struct amd_iommu *iommu, · line 359

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_init.c +2 -3
     run_bit = ( log == &iommu->event_log ) ?
-        IOMMU_STATUS_EVENT_LOG_RUN_SHIFT :
-        IOMMU_STATUS_PPR_LOG_RUN_SHIFT;
+        IOMMU_STATUS_EVENT_LOG_RUN : IOMMU_STATUS_PPR_LOG_RUN;
 
     /* wait until EventLogRun bit = 0 */
     do {
         entry = readl(iommu->mmio_base + IOMMU_STATUS_MMIO_OFFSET);
-        log_run = iommu_get_bit(entry, run_bit);
+        log_run = entry & run_bit;

Same here for log_run then. I also think run_bit would better become unsigned int then.

Quoted 5 lines
Quoted 2 lines

With these taken care of

I have separate cleanup doing that (and more).  I don't want to conflate unrelated changes - this patch is complicated enough to follow.

But strictly speaking the assignments end up wrong this way. If you really think such (benign) wrongness is okay, then may I please ask that you point out this aspect in half a sentence in the description?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

PATCH 3/4 AMD/IOMMU: Treat guest head/tail pointers as byte offsets

In reply to Andrew Cooper

The MMIO registers as already byte offsets. By masking out the reserved bits suitably in guest_iommu_mmio_write64(), we can use the values directly, instead of masking/shifting on every use.

Store the buffer size, rather than the number of entries, to keep the same units for comparison purposes.

This simplifies guest_iommu_get_table_mfn() by dropping the entry_size parameter, and simplifies the map_domain_page() handling by being able to drop the log_base variables.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

Untested - this is all dead code, and there are plenty of security issues which need adjusting before it can start being used again.

---
 xen/drivers/passthrough/amd/iommu.h       |  2 +-
 xen/drivers/passthrough/amd/iommu_guest.c | 85 +++++++++++++++----------------
 2 files changed, 42 insertions(+), 45 deletions(-)
xen/drivers/passthrough/amd/iommu.h +1 -1
index 81b6812d3a..0b598d06f8 100644
@@ -152,7 +152,7 @@ struct guest_buffer {
     struct mmio_reg         reg_base;
     struct mmio_reg         reg_tail;
     struct mmio_reg         reg_head;
-    uint32_t                entries;
+    uint32_t                size;
 };
 
 struct guest_iommu_msi {
xen/drivers/passthrough/amd/iommu_guest.c +41 -44

232 lines not on this page · raw message

Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 3/4
#11 lore raw

Re: [Xen-devel] [PATCH 3/4] AMD/IOMMU: Treat guest head/tail pointers as byte offsets

Quoted 3 lines · Andrew Cooper

The MMIO registers as already byte offsets. By masking out the reserved bits suitably in guest_iommu_mmio_write64(), we can use the values directly, instead of masking/shifting on every use.

I guess it's unclear whether such masking matches real hardware behavior, but it's certainly within spec with all other bits there reserved.

Quoted 8 lines

Store the buffer size, rather than the number of entries, to keep the same units for comparison purposes.

This simplifies guest_iommu_get_table_mfn() by dropping the entry_size parameter, and simplifies the map_domain_page() handling by being able to drop the log_base variables.

No functional change.

Well, not exactly - reads of those head/tail registers previously returned the last written value afaict.

Quoted 1 line

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw

Re: [Xen-devel] [PATCH 3/4] AMD/IOMMU: Treat guest head/tail pointers as byte offsets

Quoted 11 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper

The MMIO registers as already byte offsets. By masking out the reserved bits suitably in guest_iommu_mmio_write64(), we can use the values directly, instead of masking/shifting on every use.

I guess it's unclear whether such masking matches real hardware behavior, but it's certainly within spec with all other bits there reserved.

Quoted 3 lines

Store the buffer size, rather than the number of entries, to keep the same units for comparison purposes.

Quoted 7 lines
Quoted 5 lines

This simplifies guest_iommu_get_table_mfn() by dropping the entry_size parameter, and simplifies the map_domain_page() handling by being able to drop the log_base variables.

No functional change.

Well, not exactly - reads of those head/tail registers previously returned the last written value afaict.

It is actually a bugfix, because the spec prohibits preservation of reserved bits.  I'll adjust the commit message to indicate this.

Quoted 3 lines
Quoted 2 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw

PATCH 4/4 AMD/IOMMU: Treat head/tail pointers as byte offsets

In reply to Andrew Cooper

The MMIO registers as already byte offsets. Using them in this form removes the need to shift their values for use.

It is also inefficient to store both entries and alloc_size (which differ by a fixed entry_size). Rename alloc_size to size, and drop entries entirely, which simplifies the allocation/deallocation helpers slightly.

Mark send_iommu_command() and invalidate_iommu_all() as static, as they have no external declaration or callers.

Overall, this simplification allows GCC to optimise sufficiently to construct commands directly in the command ring, rather than on the stack and copying them into place.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

Tested on a Rome system.

---
 xen/drivers/passthrough/amd/iommu-defs.h |  1 -
 xen/drivers/passthrough/amd/iommu.h      | 18 ++----------------
 xen/drivers/passthrough/amd/iommu_cmd.c  | 20 ++++++++------------
 xen/drivers/passthrough/amd/iommu_init.c | 30 +++++++++++++-----------------
 4 files changed, 23 insertions(+), 46 deletions(-)
xen/drivers/passthrough/amd/iommu-defs.h +0 -1
index 963009de6a..50613ca150 100644
@@ -481,7 +481,6 @@ struct amd_iommu_pte {
 #define INV_IOMMU_ALL_PAGES_ADDRESS      ((1ULL << 63) - 1)
 
 #define IOMMU_RING_BUFFER_PTR_MASK                  0x0007FFF0
-#define IOMMU_RING_BUFFER_PTR_SHIFT                 4
 
 #define IOMMU_CMD_DEVICE_ID_MASK                    0x0000FFFF
 #define IOMMU_CMD_DEVICE_ID_SHIFT                   0
xen/drivers/passthrough/amd/iommu.h +2 -16
index 0b598d06f8..1abfdc685a 100644
@@ -58,12 +58,11 @@ struct table_struct {
 };
 
 struct ring_buffer {
+    spinlock_t lock;    /* protect buffer pointers */
     void *buffer;
-    unsigned long entries;
-    unsigned long alloc_size;
     uint32_t tail;
     uint32_t head;
-    spinlock_t lock;    /* protect buffer pointers */
+    uint32_t size;
 };
 
 typedef struct iommu_cap {
@@ -379,19 +378,6 @@ static inline int iommu_has_cap(struct amd_iommu *iommu, uint32_t bit)
     return !!(iommu->cap.header & (1u << bit));
 }
 
-/* access tail or head pointer of ring buffer */
-static inline uint32_t iommu_get_rb_pointer(uint32_t reg)
-{
-    return get_field_from_reg_u32(reg, IOMMU_RING_BUFFER_PTR_MASK,
-                                  IOMMU_RING_BUFFER_PTR_SHIFT);
-}
-
-static inline void iommu_set_rb_pointer(uint32_t *reg, uint32_t val)
-{
-    set_field_in_reg_u32(val, *reg, IOMMU_RING_BUFFER_PTR_MASK,
-                         IOMMU_RING_BUFFER_PTR_SHIFT, reg);
-}
-
 /* access device id field from iommu cmd */
 static inline uint16_t iommu_get_devid_from_cmd(uint32_t cmd)
 {
xen/drivers/passthrough/amd/iommu_cmd.c +8 -12
index 166f0e7263..24ef59d436 100644
@@ -24,16 +24,14 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     uint32_t tail, head;
 
-    tail = iommu->cmd_buffer.tail;
-    if ( ++tail == iommu->cmd_buffer.entries )
+    tail = iommu->cmd_buffer.tail + IOMMU_CMD_BUFFER_ENTRY_SIZE;
+    if ( tail == iommu->cmd_buffer.size )
         tail = 0;
 
-    head = iommu_get_rb_pointer(readl(iommu->mmio_base +
-                                      IOMMU_CMD_BUFFER_HEAD_OFFSET));
+    head = readl(iommu->mmio_base + IOMMU_CMD_BUFFER_HEAD_OFFSET);
     if ( head != tail )
     {
-        memcpy(iommu->cmd_buffer.buffer +
-               (iommu->cmd_buffer.tail * IOMMU_CMD_BUFFER_ENTRY_SIZE),
+        memcpy(iommu->cmd_buffer.buffer + iommu->cmd_buffer.tail,
                cmd, IOMMU_CMD_BUFFER_ENTRY_SIZE);
 
         iommu->cmd_buffer.tail = tail;
@@ -45,13 +43,11 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 
 static void commit_iommu_command_buffer(struct amd_iommu *iommu)
 {
-    u32 tail = 0;
-
-    iommu_set_rb_pointer(&tail, iommu->cmd_buffer.tail);
-    writel(tail, iommu->mmio_base+IOMMU_CMD_BUFFER_TAIL_OFFSET);
+    writel(iommu->cmd_buffer.tail,
+           iommu->mmio_base + IOMMU_CMD_BUFFER_TAIL_OFFSET);
 }
 
-int send_iommu_command(struct amd_iommu *iommu, u32 cmd[])
+static int send_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     if ( queue_iommu_command(iommu, cmd) )
     {
@@ -261,7 +257,7 @@ static void invalidate_interrupt_table(struct amd_iommu *iommu, u16 device_id)
     send_iommu_command(iommu, cmd);
 }
 
-void invalidate_iommu_all(struct amd_iommu *iommu)
+static void invalidate_iommu_all(struct amd_iommu *iommu)
 {
     u32 cmd[4], entry;
 
xen/drivers/passthrough/amd/iommu_init.c +13 -17
index 7bf6fef3ee..cfdeeefc2d 100644
@@ -117,7 +117,7 @@ static void register_iommu_cmd_buffer_in_mmio_space(struct amd_iommu *iommu)
     iommu_set_addr_lo_to_reg(&entry, addr_lo >> PAGE_SHIFT);
     writel(entry, iommu->mmio_base + IOMMU_CMD_BUFFER_BASE_LOW_OFFSET);
 
-    power_of2_entries = get_order_from_bytes(iommu->cmd_buffer.alloc_size) +
+    power_of2_entries = get_order_from_bytes(iommu->cmd_buffer.size) +
         IOMMU_CMD_BUFFER_POWER_OF2_ENTRIES_PER_PAGE;
 
     entry = 0;
@@ -145,7 +145,7 @@ static void register_iommu_event_log_in_mmio_space(struct amd_iommu *iommu)
     iommu_set_addr_lo_to_reg(&entry, addr_lo >> PAGE_SHIFT);
     writel(entry, iommu->mmio_base + IOMMU_EVENT_LOG_BASE_LOW_OFFSET);
 
-    power_of2_entries = get_order_from_bytes(iommu->event_log.alloc_size) +
+    power_of2_entries = get_order_from_bytes(iommu->event_log.size) +
                         IOMMU_EVENT_LOG_POWER_OF2_ENTRIES_PER_PAGE;
 
     entry = 0;
@@ -173,7 +173,7 @@ static void register_iommu_ppr_log_in_mmio_space(struct amd_iommu *iommu)
     iommu_set_addr_lo_to_reg(&entry, addr_lo >> PAGE_SHIFT);
     writel(entry, iommu->mmio_base + IOMMU_PPR_LOG_BASE_LOW_OFFSET);
 
-    power_of2_entries = get_order_from_bytes(iommu->ppr_log.alloc_size) +
+    power_of2_entries = get_order_from_bytes(iommu->ppr_log.size) +
                         IOMMU_PPR_LOG_POWER_OF2_ENTRIES_PER_PAGE;
 
     entry = 0;
@@ -300,7 +300,7 @@ static int iommu_read_log(struct amd_iommu *iommu,
                           unsigned int entry_size,
                           void (*parse_func)(struct amd_iommu *, u32 *))
 {
-    u32 tail, head, *entry, tail_offest, head_offset;
+    u32 tail, *entry, tail_offest, head_offset;
 
     BUG_ON(!iommu || ((log != &iommu->event_log) && (log != &iommu->ppr_log)));
     
@@ -316,22 +316,20 @@ static int iommu_read_log(struct amd_iommu *iommu,
         IOMMU_PPR_LOG_HEAD_OFFSET;
 
     tail = readl(iommu->mmio_base + tail_offest);
-    tail = iommu_get_rb_pointer(tail);
 
     while ( tail != log->head )
     {
         /* read event log entry */
-        entry = (u32 *)(log->buffer + log->head * entry_size);
+        entry = (u32 *)(log->buffer + log->head);
 
         parse_func(iommu, entry);
-        if ( ++log->head == log->entries )
+
+        log->head += entry_size;
+        if ( log->head == log->size )
             log->head = 0;
 
         /* update head pointer */
-        head = 0;
-        iommu_set_rb_pointer(&head, log->head);
-
-        writel(head, iommu->mmio_base + head_offset);
+        writel(log->head, iommu->mmio_base + head_offset);
     }
 
     spin_unlock(&log->lock);
@@ -1001,7 +999,7 @@ static void __init deallocate_buffer(void *buf, unsigned long sz)
 
 static void __init deallocate_ring_buffer(struct ring_buffer *ring_buf)
 {
-    deallocate_buffer(ring_buf->buffer, ring_buf->alloc_size);
+    deallocate_buffer(ring_buf->buffer, ring_buf->size);
     ring_buf->buffer = NULL;
     ring_buf->head = 0;
     ring_buf->tail = 0;
@@ -1036,11 +1034,9 @@ static void *__init allocate_ring_buffer(struct ring_buffer *ring_buf,
     ring_buf->tail = 0;
 
     spin_lock_init(&ring_buf->lock);
-    
-    ring_buf->alloc_size = PAGE_SIZE << get_order_from_bytes(entries *
-                                                             entry_size);
-    ring_buf->entries = ring_buf->alloc_size / entry_size;
-    ring_buf->buffer = allocate_buffer(ring_buf->alloc_size, name, clear);
+
+    ring_buf->size = PAGE_SIZE << get_order_from_bytes(entries * entry_size);
+    ring_buf->buffer = allocate_buffer(ring_buf->size, name, clear);
 
     return ring_buf->buffer;
 }
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#14 lore raw

Re: [Xen-devel] [PATCH 4/4] AMD/IOMMU: Treat head/tail pointers as byte offsets

Quoted 6 lines · Andrew Cooper
xen/drivers/passthrough/amd/iommu_cmd.c +0 -0
@@ -24,16 +24,14 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     uint32_t tail, head;
 

xen/drivers/passthrough/amd/iommu_cmd.c static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[]) · line 32

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_cmd.c +3 -4
-    tail = iommu->cmd_buffer.tail;
-    if ( ++tail == iommu->cmd_buffer.entries )
+    tail = iommu->cmd_buffer.tail + IOMMU_CMD_BUFFER_ENTRY_SIZE;
+    if ( tail == iommu->cmd_buffer.size )
         tail = 0;
 
-    head = iommu_get_rb_pointer(readl(iommu->mmio_base +
-                                      IOMMU_CMD_BUFFER_HEAD_OFFSET));
+    head = readl(iommu->mmio_base + IOMMU_CMD_BUFFER_HEAD_OFFSET);
     if ( head != tail )

Surely you want to mask off reserved (or more generally unrelated) bits, before consuming the value for the purpose here (and elsewhere below)?

xen/drivers/passthrough/amd/iommu_cmd.c static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[]) · line 47

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_cmd.c +2 -4
@@ -45,13 +43,11 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 
 static void commit_iommu_command_buffer(struct amd_iommu *iommu)
 {
-    u32 tail = 0;
-
-    iommu_set_rb_pointer(&tail, iommu->cmd_buffer.tail);
-    writel(tail, iommu->mmio_base+IOMMU_CMD_BUFFER_TAIL_OFFSET);
+    writel(iommu->cmd_buffer.tail,
+           iommu->mmio_base + IOMMU_CMD_BUFFER_TAIL_OFFSET);

I guess not preserving the reserved bits isn't a problem right now, but is doing so a good idea in general? (I notice the head point updating when processing the logs did so before, so perhaps it's indeed acceptable.)

xen/drivers/passthrough/amd/iommu_cmd.c static int iommu_read_log(struct amd_iommu *iommu, · line 323

Quoted 11 lines
xen/drivers/passthrough/amd/iommu_cmd.c +1 -2
@@ -316,22 +316,20 @@ static int iommu_read_log(struct amd_iommu *iommu,
         IOMMU_PPR_LOG_HEAD_OFFSET;
 
     tail = readl(iommu->mmio_base + tail_offest);
-    tail = iommu_get_rb_pointer(tail);
 
     while ( tail != log->head )
     {
         /* read event log entry */
-        entry = (u32 *)(log->buffer + log->head * entry_size);
+        entry = (u32 *)(log->buffer + log->head);

Would you mind dropping the pointless cast here at the same time?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw

Re: [Xen-devel] [PATCH 4/4] AMD/IOMMU: Treat head/tail pointers as byte offsets

Quoted 13 lines · Jan Beulich
Quoted 12 lines · Andrew Cooper
xen/drivers/passthrough/amd/iommu_cmd.c +2 -2
@@ -24,16 +24,14 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     uint32_t tail, head;
 
-    tail = iommu->cmd_buffer.tail;
-    if ( ++tail == iommu->cmd_buffer.entries )
+    tail = iommu->cmd_buffer.tail + IOMMU_CMD_BUFFER_ENTRY_SIZE;
+    if ( tail == iommu->cmd_buffer.size )
         tail = 0;
 
Quoted 7 lines

xen/drivers/passthrough/amd/iommu_cmd.c static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[]) · line 32

Quoted 4 lines
xen/drivers/passthrough/amd/iommu_cmd.c +1 -2
-    head = iommu_get_rb_pointer(readl(iommu->mmio_base +
-                                      IOMMU_CMD_BUFFER_HEAD_OFFSET));
+    head = readl(iommu->mmio_base + IOMMU_CMD_BUFFER_HEAD_OFFSET);
     if ( head != tail )

Surely you want to mask off reserved (or more generally unrelated) bits, before consuming the value for the purpose here (and elsewhere below)?

Reserved bits are defined in the IOMMU spec to be read-only zero.

It is also undefined behaviour for this value to ever be outside of the size configured for command buffer, so using the value like this is spec compliant.

As for actually masking the values, that breaks the optimisers ability to construct commands in the command ring.  This aspect can be worked around with other code changes, but I also think it is implausible that the remaining reserved bits here are going to sprout incompatible future uses.

Quoted 13 lines

xen/drivers/passthrough/amd/iommu_cmd.c static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[]) · line 47

Quoted 11 lines
xen/drivers/passthrough/amd/iommu_cmd.c +2 -4
@@ -45,13 +43,11 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 
 static void commit_iommu_command_buffer(struct amd_iommu *iommu)
 {
-    u32 tail = 0;
-
-    iommu_set_rb_pointer(&tail, iommu->cmd_buffer.tail);
-    writel(tail, iommu->mmio_base+IOMMU_CMD_BUFFER_TAIL_OFFSET);
+    writel(iommu->cmd_buffer.tail,
+           iommu->mmio_base + IOMMU_CMD_BUFFER_TAIL_OFFSET);

I guess not preserving the reserved bits isn't a problem right now, but is doing so a good idea in general?

As above - there are by definition no bits to preserve.

Quoted 6 lines
Quoted 6 lines
xen/drivers/passthrough/amd/iommu_cmd.c +0 -1
@@ -316,22 +316,20 @@ static int iommu_read_log(struct amd_iommu *iommu,
         IOMMU_PPR_LOG_HEAD_OFFSET;
 
     tail = readl(iommu->mmio_base + tail_offest);
-    tail = iommu_get_rb_pointer(tail);
 
Quoted 6 lines

xen/drivers/passthrough/amd/iommu_cmd.c static int iommu_read_log(struct amd_iommu *iommu, · line 323

Quoted 5 lines
xen/drivers/passthrough/amd/iommu_cmd.c +1 -1
     while ( tail != log->head )
     {
         /* read event log entry */
-        entry = (u32 *)(log->buffer + log->head * entry_size);
+        entry = (u32 *)(log->buffer + log->head);

Would you mind dropping the pointless cast here at the same time?

Can do.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw

Re: [Xen-devel] [PATCH 4/4] AMD/IOMMU: Treat head/tail pointers as byte offsets

Quoted 28 lines · Andrew Cooper
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Andrew Cooper
xen/drivers/passthrough/amd/iommu_cmd.c +2 -2
@@ -24,16 +24,14 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     uint32_t tail, head;
 
-    tail = iommu->cmd_buffer.tail;
-    if ( ++tail == iommu->cmd_buffer.entries )
+    tail = iommu->cmd_buffer.tail + IOMMU_CMD_BUFFER_ENTRY_SIZE;
+    if ( tail == iommu->cmd_buffer.size )
         tail = 0;
 
Quoted 7 lines

xen/drivers/passthrough/amd/iommu_cmd.c static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[]) · line 32

Quoted 4 lines
xen/drivers/passthrough/amd/iommu_cmd.c +1 -2
-    head = iommu_get_rb_pointer(readl(iommu->mmio_base +
-                                      IOMMU_CMD_BUFFER_HEAD_OFFSET));
+    head = readl(iommu->mmio_base + IOMMU_CMD_BUFFER_HEAD_OFFSET);
     if ( head != tail )

Surely you want to mask off reserved (or more generally unrelated) bits, before consuming the value for the purpose here (and elsewhere below)?

Reserved bits are defined in the IOMMU spec to be read-only zero.

It is also undefined behaviour for this value to ever be outside of the size configured for command buffer, so using the value like this is spec compliant.

Quoted 5 lines

As for actually masking the values, that breaks the optimisers ability to construct commands in the command ring.  This aspect can be worked around with other code changes, but I also think it is implausible that the remaining reserved bits here are going to sprout incompatible future uses.

Implausible - perhaps. But impossible - no. There could be a simple flag bit appearing somewhere in these registers. I simply don't it is a good idea to set a precedent of not honoring reserved bit as being reserved. The spec saying "read-only zero" can only be viewed as correct for the current version of the spec, or else why would the bits be called "reserved" rather than e.g. "read-as-zero"?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw

Re: [Xen-devel] [PATCH 4/4] AMD/IOMMU: Treat head/tail pointers as byte offsets

Quoted 33 lines · Jan Beulich
Quoted 27 lines · Andrew Cooper
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Andrew Cooper
xen/drivers/passthrough/amd/iommu_cmd.c +2 -2
@@ -24,16 +24,14 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     uint32_t tail, head;
 
-    tail = iommu->cmd_buffer.tail;
-    if ( ++tail == iommu->cmd_buffer.entries )
+    tail = iommu->cmd_buffer.tail + IOMMU_CMD_BUFFER_ENTRY_SIZE;
+    if ( tail == iommu->cmd_buffer.size )
         tail = 0;
 
Quoted 7 lines

xen/drivers/passthrough/amd/iommu_cmd.c static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[]) · line 32

Quoted 4 lines
xen/drivers/passthrough/amd/iommu_cmd.c +1 -2
-    head = iommu_get_rb_pointer(readl(iommu->mmio_base +
-                                      IOMMU_CMD_BUFFER_HEAD_OFFSET));
+    head = readl(iommu->mmio_base + IOMMU_CMD_BUFFER_HEAD_OFFSET);
     if ( head != tail )

Surely you want to mask off reserved (or more generally unrelated) bits, before consuming the value for the purpose here (and elsewhere below)?

Reserved bits are defined in the IOMMU spec to be read-only zero.

It is also undefined behaviour for this value to ever be outside of the size configured for command buffer, so using the value like this is spec compliant.

Quoted 5 lines

As for actually masking the values, that breaks the optimisers ability to construct commands in the command ring.  This aspect can be worked around with other code changes, but I also think it is implausible that the remaining reserved bits here are going to sprout incompatible future uses.

Quoted 5 lines

Implausible - perhaps. But impossible - no. There could be a simple flag bit appearing somewhere in these registers. I simply don't it is a good idea to set a precedent of not honoring reserved bit as being reserved. The spec saying "read-only zero" can only be viewed as correct for the current version of the spec,

Its perfectly easy to do forward compatible changes with a spec written in this way.

It means that new behaviours have to be opted in to, and this is how the AMD IOMMU spec has evolved.  Notice how every new feature declares "this bit is reserved unless $OTHER_THING is enabled."

It is also a very sane way of doing forward compatibility, from software's point of view.

Quoted 2 lines

or else why would
the bits be called "reserved" rather than e.g. "read-as-zero"?

Read Table 1, but it also ought to be obvious.  "Reserved", "Resv" and "Res" are all shorter to write than "read-as-zero", especially in the diagrams of a few individual bits in a register.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw

Re: [Xen-devel] [PATCH 4/4] AMD/IOMMU: Treat head/tail pointers as byte offsets

Quoted 43 lines · Andrew Cooper
Quoted 33 lines · Jan Beulich
Quoted 27 lines · Andrew Cooper
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Andrew Cooper
xen/drivers/passthrough/amd/iommu_cmd.c +2 -2
@@ -24,16 +24,14 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     uint32_t tail, head;
 
-    tail = iommu->cmd_buffer.tail;
-    if ( ++tail == iommu->cmd_buffer.entries )
+    tail = iommu->cmd_buffer.tail + IOMMU_CMD_BUFFER_ENTRY_SIZE;
+    if ( tail == iommu->cmd_buffer.size )
         tail = 0;
 
Quoted 7 lines

xen/drivers/passthrough/amd/iommu_cmd.c static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[]) · line 32

Quoted 4 lines
xen/drivers/passthrough/amd/iommu_cmd.c +1 -2
-    head = iommu_get_rb_pointer(readl(iommu->mmio_base +
-                                      IOMMU_CMD_BUFFER_HEAD_OFFSET));
+    head = readl(iommu->mmio_base + IOMMU_CMD_BUFFER_HEAD_OFFSET);
     if ( head != tail )

Surely you want to mask off reserved (or more generally unrelated) bits, before consuming the value for the purpose here (and elsewhere below)?

Reserved bits are defined in the IOMMU spec to be read-only zero.

It is also undefined behaviour for this value to ever be outside of the size configured for command buffer, so using the value like this is spec compliant.

Quoted 5 lines

As for actually masking the values, that breaks the optimisers ability to construct commands in the command ring.  This aspect can be worked around with other code changes, but I also think it is implausible that the remaining reserved bits here are going to sprout incompatible future uses.

Quoted 5 lines

Implausible - perhaps. But impossible - no. There could be a simple flag bit appearing somewhere in these registers. I simply don't it is a good idea to set a precedent of not honoring reserved bit as being reserved. The spec saying "read-only zero" can only be viewed as correct for the current version of the spec,

Its perfectly easy to do forward compatible changes with a spec written in this way.

Quoted 6 lines

It means that new behaviours have to be opted in to, and this is how the AMD IOMMU spec has evolved.  Notice how every new feature declares "this bit is reserved unless $OTHER_THING is enabled."

It is also a very sane way of doing forward compatibility, from software's point of view.

Yes. But does the IOMMU spec spell out that it'll follow this in the future?

Quoted 6 lines
Quoted 2 lines

or else why would
the bits be called "reserved" rather than e.g. "read-as-zero"?

Read Table 1, but it also ought to be obvious.  "Reserved", "Resv" and "Res" are all shorter to write than "read-as-zero", especially in the diagrams of a few individual bits in a register.

There's also the common acronym "raz", which is as short. That table in particular says nothing about future uses of currently reserved bits. Just take the Extended Feature Register as a reference: How would new features be advertised (in currently reserved bits) if use of those bits was to be qualified by yet some other feature bit(s). Past growth of the set of used bits also hasn't followed a pattern you seem to suggest.

Don't get me wrong - I agree it's unlikely for these bits to gain a meaning that would conflict with a more relaxed use like you do suggest. But I don't think better code generation should be an argument against having code written as compatibly as possible.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#19 lore raw

PATCH v2 4/4 AMD/IOMMU: Treat head/tail pointers as byte offsets

In reply to Andrew Cooper

The MMIO registers as already byte offsets. Using them in this form removes the need to shift their values for use.

It is also inefficient to store both entries and alloc_size (which only differ by entry_size). Rename alloc_size to size, and drop entries entirely, which simplifies the allocation/deallocation helpers slightly.

Mark send_iommu_command() and invalidate_iommu_all() as static, as they have no external declaration or callers.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* Mask head/tail pointers
* Drop unnecessary cast.

Tested on a Rome system.

---
 xen/drivers/passthrough/amd/iommu-defs.h |  1 -
 xen/drivers/passthrough/amd/iommu.h      | 18 ++----------------
 xen/drivers/passthrough/amd/iommu_cmd.c  | 21 +++++++++------------
 xen/drivers/passthrough/amd/iommu_init.c | 32 ++++++++++++++------------------
 4 files changed, 25 insertions(+), 47 deletions(-)
xen/drivers/passthrough/amd/iommu-defs.h +0 -1
index 963009de6a..50613ca150 100644
@@ -481,7 +481,6 @@ struct amd_iommu_pte {
 #define INV_IOMMU_ALL_PAGES_ADDRESS      ((1ULL << 63) - 1)
 
 #define IOMMU_RING_BUFFER_PTR_MASK                  0x0007FFF0
-#define IOMMU_RING_BUFFER_PTR_SHIFT                 4
 
 #define IOMMU_CMD_DEVICE_ID_MASK                    0x0000FFFF
 #define IOMMU_CMD_DEVICE_ID_SHIFT                   0
xen/drivers/passthrough/amd/iommu.h +2 -16
index 0b598d06f8..1abfdc685a 100644
@@ -58,12 +58,11 @@ struct table_struct {
 };
 
 struct ring_buffer {
+    spinlock_t lock;    /* protect buffer pointers */
     void *buffer;
-    unsigned long entries;
-    unsigned long alloc_size;
     uint32_t tail;
     uint32_t head;
-    spinlock_t lock;    /* protect buffer pointers */
+    uint32_t size;
 };
 
 typedef struct iommu_cap {
@@ -379,19 +378,6 @@ static inline int iommu_has_cap(struct amd_iommu *iommu, uint32_t bit)
     return !!(iommu->cap.header & (1u << bit));
 }
 
-/* access tail or head pointer of ring buffer */
-static inline uint32_t iommu_get_rb_pointer(uint32_t reg)
-{
-    return get_field_from_reg_u32(reg, IOMMU_RING_BUFFER_PTR_MASK,
-                                  IOMMU_RING_BUFFER_PTR_SHIFT);
-}
-
-static inline void iommu_set_rb_pointer(uint32_t *reg, uint32_t val)
-{
-    set_field_in_reg_u32(val, *reg, IOMMU_RING_BUFFER_PTR_MASK,
-                         IOMMU_RING_BUFFER_PTR_SHIFT, reg);
-}
-
 /* access device id field from iommu cmd */
 static inline uint16_t iommu_get_devid_from_cmd(uint32_t cmd)
 {
xen/drivers/passthrough/amd/iommu_cmd.c +9 -12
index 1eae339692..249ed345a0 100644
@@ -24,16 +24,15 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     uint32_t tail, head;
 
-    tail = iommu->cmd_buffer.tail;
-    if ( ++tail == iommu->cmd_buffer.entries )
+    tail = iommu->cmd_buffer.tail + IOMMU_CMD_BUFFER_ENTRY_SIZE;
+    if ( tail == iommu->cmd_buffer.size )
         tail = 0;
 
-    head = iommu_get_rb_pointer(readl(iommu->mmio_base +
-                                      IOMMU_CMD_BUFFER_HEAD_OFFSET));
+    head = readl(iommu->mmio_base +
+                 IOMMU_CMD_BUFFER_HEAD_OFFSET) & IOMMU_RING_BUFFER_PTR_MASK;
     if ( head != tail )
     {
-        memcpy(iommu->cmd_buffer.buffer +
-               (iommu->cmd_buffer.tail * IOMMU_CMD_BUFFER_ENTRY_SIZE),
+        memcpy(iommu->cmd_buffer.buffer + iommu->cmd_buffer.tail,
                cmd, IOMMU_CMD_BUFFER_ENTRY_SIZE);
 
         iommu->cmd_buffer.tail = tail;
@@ -45,13 +44,11 @@ static int queue_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 
 static void commit_iommu_command_buffer(struct amd_iommu *iommu)
 {
-    u32 tail = 0;
-
-    iommu_set_rb_pointer(&tail, iommu->cmd_buffer.tail);
-    writel(tail, iommu->mmio_base+IOMMU_CMD_BUFFER_TAIL_OFFSET);
+    writel(iommu->cmd_buffer.tail,
+           iommu->mmio_base + IOMMU_CMD_BUFFER_TAIL_OFFSET);
 }
 
-int send_iommu_command(struct amd_iommu *iommu, u32 cmd[])
+static int send_iommu_command(struct amd_iommu *iommu, u32 cmd[])
 {
     if ( queue_iommu_command(iommu, cmd) )
     {
@@ -261,7 +258,7 @@ static void invalidate_interrupt_table(struct amd_iommu *iommu, u16 device_id)
     send_iommu_command(iommu, cmd);
 }
 
-void invalidate_iommu_all(struct amd_iommu *iommu)
+static void invalidate_iommu_all(struct amd_iommu *iommu)
 {
     u32 cmd[4], entry;
 
xen/drivers/passthrough/amd/iommu_init.c +14 -18
index 5544dd9505..c42b608f07 100644
@@ -117,7 +117,7 @@ static void register_iommu_cmd_buffer_in_mmio_space(struct amd_iommu *iommu)
     iommu_set_addr_lo_to_reg(&entry, addr_lo >> PAGE_SHIFT);
     writel(entry, iommu->mmio_base + IOMMU_CMD_BUFFER_BASE_LOW_OFFSET);
 
-    power_of2_entries = get_order_from_bytes(iommu->cmd_buffer.alloc_size) +
+    power_of2_entries = get_order_from_bytes(iommu->cmd_buffer.size) +
         IOMMU_CMD_BUFFER_POWER_OF2_ENTRIES_PER_PAGE;
 
     entry = 0;
@@ -145,7 +145,7 @@ static void register_iommu_event_log_in_mmio_space(struct amd_iommu *iommu)
     iommu_set_addr_lo_to_reg(&entry, addr_lo >> PAGE_SHIFT);
     writel(entry, iommu->mmio_base + IOMMU_EVENT_LOG_BASE_LOW_OFFSET);
 
-    power_of2_entries = get_order_from_bytes(iommu->event_log.alloc_size) +
+    power_of2_entries = get_order_from_bytes(iommu->event_log.size) +
                         IOMMU_EVENT_LOG_POWER_OF2_ENTRIES_PER_PAGE;
 
     entry = 0;
@@ -173,7 +173,7 @@ static void register_iommu_ppr_log_in_mmio_space(struct amd_iommu *iommu)
     iommu_set_addr_lo_to_reg(&entry, addr_lo >> PAGE_SHIFT);
     writel(entry, iommu->mmio_base + IOMMU_PPR_LOG_BASE_LOW_OFFSET);
 
-    power_of2_entries = get_order_from_bytes(iommu->ppr_log.alloc_size) +
+    power_of2_entries = get_order_from_bytes(iommu->ppr_log.size) +
                         IOMMU_PPR_LOG_POWER_OF2_ENTRIES_PER_PAGE;
 
     entry = 0;
@@ -300,7 +300,7 @@ static int iommu_read_log(struct amd_iommu *iommu,
                           unsigned int entry_size,
                           void (*parse_func)(struct amd_iommu *, u32 *))
 {
-    u32 tail, head, *entry, tail_offest, head_offset;
+    u32 tail, *entry, tail_offest, head_offset;
 
     BUG_ON(!iommu || ((log != &iommu->event_log) && (log != &iommu->ppr_log)));
     
@@ -315,23 +315,21 @@ static int iommu_read_log(struct amd_iommu *iommu,
         IOMMU_EVENT_LOG_HEAD_OFFSET :
         IOMMU_PPR_LOG_HEAD_OFFSET;
 
-    tail = readl(iommu->mmio_base + tail_offest);
-    tail = iommu_get_rb_pointer(tail);
+    tail = readl(iommu->mmio_base + tail_offest) & IOMMU_RING_BUFFER_PTR_MASK;
 
     while ( tail != log->head )
     {
         /* read event log entry */
-        entry = (u32 *)(log->buffer + log->head * entry_size);
+        entry = log->buffer + log->head;
 
         parse_func(iommu, entry);
-        if ( ++log->head == log->entries )
+
+        log->head += entry_size;
+        if ( log->head == log->size )
             log->head = 0;
 
         /* update head pointer */
-        head = 0;
-        iommu_set_rb_pointer(&head, log->head);
-
-        writel(head, iommu->mmio_base + head_offset);
+        writel(log->head, iommu->mmio_base + head_offset);
     }
 
     spin_unlock(&log->lock);
@@ -1000,7 +998,7 @@ static void __init deallocate_buffer(void *buf, unsigned long sz)
 
 static void __init deallocate_ring_buffer(struct ring_buffer *ring_buf)
 {
-    deallocate_buffer(ring_buf->buffer, ring_buf->alloc_size);
+    deallocate_buffer(ring_buf->buffer, ring_buf->size);
     ring_buf->buffer = NULL;
     ring_buf->head = 0;
     ring_buf->tail = 0;
@@ -1035,11 +1033,9 @@ static void *__init allocate_ring_buffer(struct ring_buffer *ring_buf,
     ring_buf->tail = 0;
 
     spin_lock_init(&ring_buf->lock);
-    
-    ring_buf->alloc_size = PAGE_SIZE << get_order_from_bytes(entries *
-                                                             entry_size);
-    ring_buf->entries = ring_buf->alloc_size / entry_size;
-    ring_buf->buffer = allocate_buffer(ring_buf->alloc_size, name, clear);
+
+    ring_buf->size = PAGE_SIZE << get_order_from_bytes(entries * entry_size);
+    ring_buf->buffer = allocate_buffer(ring_buf->size, name, clear);
 
     return ring_buf->buffer;
 }
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 4/4
#20 lore raw

Re: [Xen-devel] [PATCH v2 4/4] AMD/IOMMU: Treat head/tail pointers as byte offsets

Quoted 11 lines · Andrew Cooper

The MMIO registers as already byte offsets. Using them in this form removes the need to shift their values for use.

It is also inefficient to store both entries and alloc_size (which only differ by entry_size). Rename alloc_size to size, and drop entries entirely, which simplifies the allocation/deallocation helpers slightly.

Mark send_iommu_command() and invalidate_iommu_all() as static, as they have no external declaration or callers.

Quoted 8 lines

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* Mask head/tail pointers
* Drop unnecessary cast.

Thanks for adjusting these.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel