Skip to contentSkip to conversation

hw: Let DMA/PCI API propagate MemTxResult

PATCH RESEND In review Philippe Mathieu-Daudé 1 participant 12 messages Oct 23, 2020

lore
Share & tools
lore
#0 lore raw

v3 cover hw: Let DMA/PCI API propagate MemTxResult

Currently the MemTxResult return value is not propagated, so lost. The motivation behind this series is to make the API more robust to enforce correct use by the consumers.

All series reviewed.

Since v2:
- Rebased
- Removed patches adding MemTxAttrs argument to DMA/PCI API (we will discuss them later).

v2:
https://www.mail-archive.com/[email protected]/msg746761.html

The DMA API propagates MemTxResult:
- MEMTX_OK,
- MEMTX_device_ERROR,
- MEMTX_DECODE_ERROR.

Let the PCI DMA API propagate them, as they are clearer than an undocumented 'int'.

Supersedes: <[email protected]>

Klaus Jensen (1):
pci: pass along the return value of dma_memory_rw

Philippe Mathieu-Daudé (9):
docs/devel/loads-stores: Add regexp for DMA functions
dma: Document address_space_map/address_space_unmap() prototypes
dma: Let dma_memory_set() propagate MemTxResult
dma: Let dma_memory_rw() propagate MemTxResult
dma: Let dma_memory_read() propagate MemTxResult
dma: Let dma_memory_write() propagate MemTxResult
pci: Let pci_dma_rw() propagate MemTxResult
pci: Let pci_dma_read() propagate MemTxResult
pci: Let pci_dma_write() propagate MemTxResult

 docs/devel/loads-stores.rst |   2 +
 include/hw/pci/pci.h        |  53 +++++++++++++---
 include/sysemu/dma.h        | 117 ++++++++++++++++++++++++++++++------
 softmmu/dma-helpers.c       |   7 ++-
 4 files changed, 150 insertions(+), 29 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH RESEND v3 1/10 pci: pass along the return value of dma_memory_rw

From: Klaus Jensen <[email protected]>

Some might actually care about the return value of dma_memory_rw. So let us pass it along instead of ignoring it.

There are no existing users of the return value, so this patch should be safe.

Message-Id: <[email protected]>

---
 include/hw/pci/pci.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/hw/pci/pci.h +1 -2
index 0a59a06b149..f19ffe6b4fe 100644
@@ -783,8 +783,7 @@ static inline AddressSpace *pci_get_address_space(PCIDevice *dev)
 static inline int pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
                              void *buf, dma_addr_t len, DMADirection dir)
 {
-    dma_memory_rw(pci_get_address_space(dev), addr, buf, len, dir);
-    return 0;
+    return dma_memory_rw(pci_get_address_space(dev), addr, buf, len, dir);
 }
 
 static inline int pci_dma_read(PCIDevice *dev, dma_addr_t addr,
Signature · 1 line
2.26.2
#2 lore raw

PATCH RESEND v3 2/10 docs/devel/loads-stores: Add regexp for DMA functions

In reply to Philippe Mathieu-Daudé
---
 docs/devel/loads-stores.rst | 2 ++
 1 file changed, 2 insertions(+)
docs/devel/loads-stores.rst +2 -0
index 59c1225391c..ee43f5dfee2 100644
@@ -483,6 +483,8 @@ make sure our existing code is doing things correctly.
 
 Regexes for git grep
  - ``\<dma_memory_\(read\|write\|rw\)\>``
+ - ``\<ldu\?[bwlq]\(_[bl]e\)\?_dma\>``
+ - ``\<st[bwlq]\(_[bl]e\)\?_dma\>``
 
 ``pci_dma_*`` and ``{ld,st}*_pci_dma``
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signature · 1 line
2.26.2
#3 lore raw

PATCH RESEND v3 3/10 dma: Document address_space_map/address_space_unmap() prototypes

In reply to Philippe Mathieu-Daudé

Add documentation based on address_space_map / address_space_unmap.

---
 include/sysemu/dma.h | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
include/sysemu/dma.h +28 -1
index 80c5bc3e02d..c6e12b4c243 100644
@@ -1,7 +1,7 @@
 /*
  * DMA helper functions
  *
- * Copyright (c) 2009 Red Hat
+ * Copyright (c) 2009, 2020 Red Hat
  *
  * This work is licensed under the terms of the GNU General Public License
  * (GNU GPL), version 2 or later.
@@ -125,6 +125,19 @@ static inline int dma_memory_write(AddressSpace *as, dma_addr_t addr,
 
 int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len);
 
+/**
+ * address_space_map: Map a physical memory region into a host virtual address.
+ *
+ * May map a subset of the requested range, given by and returned in @plen.
+ * May return %NULL and set *@plen to zero(0), if resources needed to perform
+ * the mapping are exhausted.
+ * Use only for reads OR writes - not for read-modify-write operations.
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @len: pointer to length of buffer; updated on return
+ * @dir: indicates the transfer direction
+ */
 static inline void *dma_memory_map(AddressSpace *as,
                                    dma_addr_t addr, dma_addr_t *len,
                                    DMADirection dir)
@@ -138,6 +151,20 @@ static inline void *dma_memory_map(AddressSpace *as,
     return p;
 }
 
+/**
+ * address_space_unmap: Unmaps a memory region previously mapped
+ *                      by dma_memory_map()
+ *
+ * Will also mark the memory as dirty if @dir == %DMA_DIRECTION_FROM_DEVICE.
+ * @access_len gives the amount of memory that was actually read or written
+ * by the caller.
+ *
+ * @as: #AddressSpace used
+ * @buffer: host pointer as returned by address_space_map()
+ * @len: buffer length as returned by address_space_map()
+ * @dir: indicates the transfer direction
+ * @access_len: amount of data actually transferred
+ */
 static inline void dma_memory_unmap(AddressSpace *as,
                                     void *buffer, dma_addr_t len,
                                     DMADirection dir, dma_addr_t access_len)
Signature · 1 line
2.26.2
#4 lore raw

PATCH RESEND v3 4/10 dma: Let dma_memory_set() propagate MemTxResult

In reply to Philippe Mathieu-Daudé

address_space_write() returns a MemTxResult type. Do not discard it, return it to the caller.

---
 include/sysemu/dma.h  | 15 ++++++++++++++-
 softmmu/dma-helpers.c |  7 ++++---
 2 files changed, 18 insertions(+), 4 deletions(-)
include/sysemu/dma.h +14 -1
index c6e12b4c243..37cd9f11217 100644
@@ -123,7 +123,20 @@ static inline int dma_memory_write(AddressSpace *as, dma_addr_t addr,
                          DMA_DIRECTION_FROM_DEVICE);
 }
 
-int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len);
+/**
+ * dma_memory_set: Fill memory with a constant byte from DMA controller.
+ *
+ * Return a MemTxResult indicating whether the operation succeeded
+ * or failed (eg unassigned memory, device rejected the transaction,
+ * IOMMU fault).
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @c: constant byte to fill the memory
+ * @len: the number of bytes to fill with the constant byte
+ */
+MemTxResult dma_memory_set(AddressSpace *as, dma_addr_t addr,
+                           uint8_t c, dma_addr_t len);
 
 /**
  * address_space_map: Map a physical memory region into a host virtual address.
softmmu/dma-helpers.c +4 -3
index 03c92e0cc60..29001b54595 100644
@@ -1,7 +1,7 @@
 /*
  * DMA helper functions
  *
- * Copyright (c) 2009 Red Hat
+ * Copyright (c) 2009,2020 Red Hat
  *
  * This work is licensed under the terms of the GNU General Public License
  * (GNU GPL), version 2 or later.
@@ -18,14 +18,15 @@
 
 /* #define DEBUG_IOMMU */
 
-int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len)
+MemTxResult dma_memory_set(AddressSpace *as, dma_addr_t addr,
+                           uint8_t c, dma_addr_t len)
 {
     dma_barrier(as, DMA_DIRECTION_FROM_DEVICE);
 
 #define FILLBUF_SIZE 512
     uint8_t fillbuf[FILLBUF_SIZE];
     int l;
-    bool error = false;
+    MemTxResult error = MEMTX_OK;
 
     memset(fillbuf, c, FILLBUF_SIZE);
     while (len > 0) {
Signature · 1 line
2.26.2
#5 lore raw

PATCH RESEND v3 5/10 dma: Let dma_memory_rw() propagate MemTxResult

In reply to Philippe Mathieu-Daudé

address_space_rw() returns a MemTxResult type.
Do not discard it, return it to the caller.

---
 include/sysemu/dma.h | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
include/sysemu/dma.h +22 -8
index 37cd9f11217..04687d8825e 100644
@@ -80,12 +80,13 @@ static inline bool dma_memory_valid(AddressSpace *as,
                                       MEMTXATTRS_UNSPECIFIED);
 }
 
-static inline int dma_memory_rw_relaxed(AddressSpace *as, dma_addr_t addr,
-                                        void *buf, dma_addr_t len,
-                                        DMADirection dir)
+static inline MemTxResult dma_memory_rw_relaxed(AddressSpace *as,
+                                                dma_addr_t addr,
+                                                void *buf, dma_addr_t len,
+                                                DMADirection dir)
 {
-    return (bool)address_space_rw(as, addr, MEMTXATTRS_UNSPECIFIED,
-                                  buf, len, dir == DMA_DIRECTION_FROM_DEVICE);
+    return address_space_rw(as, addr, MEMTXATTRS_UNSPECIFIED,
+                            buf, len, dir == DMA_DIRECTION_FROM_DEVICE);
 }
 
 static inline int dma_memory_read_relaxed(AddressSpace *as, dma_addr_t addr,
@@ -101,9 +102,22 @@ static inline int dma_memory_write_relaxed(AddressSpace *as, dma_addr_t addr,
                                  DMA_DIRECTION_FROM_DEVICE);
 }
 
-static inline int dma_memory_rw(AddressSpace *as, dma_addr_t addr,
-                                void *buf, dma_addr_t len,
-                                DMADirection dir)
+/**
+ * dma_memory_rw: Read from or write to an address space from DMA controller.
+ *
+ * Return a MemTxResult indicating whether the operation succeeded
+ * or failed (eg unassigned memory, device rejected the transaction,
+ * IOMMU fault).
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @buf: buffer with the data transferred
+ * @len: the number of bytes to read or write
+ * @dir: indicates the transfer direction
+ */
+static inline MemTxResult dma_memory_rw(AddressSpace *as, dma_addr_t addr,
+                                        void *buf, dma_addr_t len,
+                                        DMADirection dir)
 {
     dma_barrier(as, dir);
 
Signature · 1 line
2.26.2
#6 lore raw

PATCH RESEND v3 6/10 dma: Let dma_memory_read() propagate MemTxResult

In reply to Philippe Mathieu-Daudé

dma_memory_rw_relaxed() returns a MemTxResult type. Do not discard it, return it to the caller.

---
 include/sysemu/dma.h | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
include/sysemu/dma.h +17 -4
index 04687d8825e..df170047eb3 100644
@@ -89,8 +89,9 @@ static inline MemTxResult dma_memory_rw_relaxed(AddressSpace *as,
                             buf, len, dir == DMA_DIRECTION_FROM_DEVICE);
 }
 
-static inline int dma_memory_read_relaxed(AddressSpace *as, dma_addr_t addr,
-                                          void *buf, dma_addr_t len)
+static inline MemTxResult dma_memory_read_relaxed(AddressSpace *as,
+                                                  dma_addr_t addr,
+                                                  void *buf, dma_addr_t len)
 {
     return dma_memory_rw_relaxed(as, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
 }
@@ -124,8 +125,20 @@ static inline MemTxResult dma_memory_rw(AddressSpace *as, dma_addr_t addr,
     return dma_memory_rw_relaxed(as, addr, buf, len, dir);
 }
 
-static inline int dma_memory_read(AddressSpace *as, dma_addr_t addr,
-                                  void *buf, dma_addr_t len)
+/**
+ * dma_memory_read: Read from an address space from DMA controller.
+ *
+ * Return a MemTxResult indicating whether the operation succeeded
+ * or failed (eg unassigned memory, device rejected the transaction,
+ * IOMMU fault).  Called within RCU critical section.
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @buf: buffer with the data transferred
+ * @len: length of the data transferred
+ */
+static inline MemTxResult dma_memory_read(AddressSpace *as, dma_addr_t addr,
+                                          void *buf, dma_addr_t len)
 {
     return dma_memory_rw(as, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
 }
Signature · 1 line
2.26.2
#7 lore raw

PATCH RESEND v3 7/10 dma: Let dma_memory_write() propagate MemTxResult

In reply to Philippe Mathieu-Daudé

dma_memory_rw_relaxed() returns a MemTxResult type. Do not discard it, return it to the caller.

---
 include/sysemu/dma.h | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
include/sysemu/dma.h +18 -4
index df170047eb3..a052f7bca3f 100644
@@ -96,8 +96,10 @@ static inline MemTxResult dma_memory_read_relaxed(AddressSpace *as,
     return dma_memory_rw_relaxed(as, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
 }
 
-static inline int dma_memory_write_relaxed(AddressSpace *as, dma_addr_t addr,
-                                           const void *buf, dma_addr_t len)
+static inline MemTxResult dma_memory_write_relaxed(AddressSpace *as,
+                                                   dma_addr_t addr,
+                                                   const void *buf,
+                                                   dma_addr_t len)
 {
     return dma_memory_rw_relaxed(as, addr, (void *)buf, len,
                                  DMA_DIRECTION_FROM_DEVICE);
@@ -143,8 +145,20 @@ static inline MemTxResult dma_memory_read(AddressSpace *as, dma_addr_t addr,
     return dma_memory_rw(as, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
 }
 
-static inline int dma_memory_write(AddressSpace *as, dma_addr_t addr,
-                                   const void *buf, dma_addr_t len)
+/**
+ * address_space_write: Write to address space from DMA controller.
+ *
+ * Return a MemTxResult indicating whether the operation succeeded
+ * or failed (eg unassigned memory, device rejected the transaction,
+ * IOMMU fault).
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @buf: buffer with the data transferred
+ * @len: the number of bytes to write
+ */
+static inline MemTxResult dma_memory_write(AddressSpace *as, dma_addr_t addr,
+                                           const void *buf, dma_addr_t len)
 {
     return dma_memory_rw(as, addr, (void *)buf, len,
                          DMA_DIRECTION_FROM_DEVICE);
Signature · 1 line
2.26.2
#8 lore raw

PATCH RESEND v3 8/10 pci: Let pci_dma_rw() propagate MemTxResult

In reply to Philippe Mathieu-Daudé

dma_memory_rw() returns a MemTxResult type.
Do not discard it, return it to the caller.

---
 include/hw/pci/pci.h | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +16 -2
index f19ffe6b4fe..b8780ea18c1 100644
@@ -780,8 +780,22 @@ static inline AddressSpace *pci_get_address_space(PCIDevice *dev)
     return &dev->bus_master_as;
 }
 
-static inline int pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
-                             void *buf, dma_addr_t len, DMADirection dir)
+/**
+ * pci_dma_rw: Read from or write to an address space from PCI device.
+ *
+ * Return a MemTxResult indicating whether the operation succeeded
+ * or failed (eg unassigned memory, device rejected the transaction,
+ * IOMMU fault).
+ *
+ * @dev: #PCIDevice doing the memory access
+ * @addr: address within the #PCIDevice address space
+ * @buf: buffer with the data transferred
+ * @len: the number of bytes to read or write
+ * @dir: indicates the transfer direction
+ */
+static inline MemTxResult pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
+                                     void *buf, dma_addr_t len,
+                                     DMADirection dir)
 {
     return dma_memory_rw(pci_get_address_space(dev), addr, buf, len, dir);
 }
Signature · 1 line
2.26.2
#9 lore raw

PATCH RESEND v3 9/10 pci: Let pci_dma_read() propagate MemTxResult

In reply to Philippe Mathieu-Daudé

pci_dma_rw() returns a MemTxResult type.
Do not discard it, return it to the caller.

---
 include/hw/pci/pci.h | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +14 -2
index b8780ea18c1..85b5786a5da 100644
@@ -800,8 +800,20 @@ static inline MemTxResult pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
     return dma_memory_rw(pci_get_address_space(dev), addr, buf, len, dir);
 }
 
-static inline int pci_dma_read(PCIDevice *dev, dma_addr_t addr,
-                               void *buf, dma_addr_t len)
+/**
+ * pci_dma_read: Read from an address space from PCI device.
+ *
+ * Return a MemTxResult indicating whether the operation succeeded
+ * or failed (eg unassigned memory, device rejected the transaction,
+ * IOMMU fault).  Called within RCU critical section.
+ *
+ * @dev: #PCIDevice doing the memory access
+ * @addr: address within the #PCIDevice address space
+ * @buf: buffer with the data transferred
+ * @len: length of the data transferred
+ */
+static inline MemTxResult pci_dma_read(PCIDevice *dev, dma_addr_t addr,
+                                       void *buf, dma_addr_t len)
 {
     return pci_dma_rw(dev, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
 }
Signature · 1 line
2.26.2
#10 lore raw

PATCH RESEND v3 10/10 pci: Let pci_dma_write() propagate MemTxResult

In reply to Philippe Mathieu-Daudé

pci_dma_rw() returns a MemTxResult type.
Do not discard it, return it to the caller.

---
 include/hw/pci/pci.h | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +14 -2
index 85b5786a5da..dd486cd941a 100644
@@ -818,8 +818,20 @@ static inline MemTxResult pci_dma_read(PCIDevice *dev, dma_addr_t addr,
     return pci_dma_rw(dev, addr, buf, len, DMA_DIRECTION_TO_DEVICE);
 }
 
-static inline int pci_dma_write(PCIDevice *dev, dma_addr_t addr,
-                                const void *buf, dma_addr_t len)
+/**
+ * pci_dma_write: Write to address space from PCI device.
+ *
+ * Return a MemTxResult indicating whether the operation succeeded
+ * or failed (eg unassigned memory, device rejected the transaction,
+ * IOMMU fault).
+ *
+ * @dev: #PCIDevice doing the memory access
+ * @addr: address within the #PCIDevice address space
+ * @buf: buffer with the data transferred
+ * @len: the number of bytes to write
+ */
+static inline MemTxResult pci_dma_write(PCIDevice *dev, dma_addr_t addr,
+                                        const void *buf, dma_addr_t len)
 {
     return pci_dma_rw(dev, addr, (void *) buf, len, DMA_DIRECTION_FROM_DEVICE);
 }
Signature · 1 line
2.26.2
#11 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé

Currently the MemTxResult return value is not propagated, so lost. The motivation behind this series is to make the API more robust to enforce correct use by the consumers.

All series reviewed.

Since v2:
- Rebased
- Removed patches adding MemTxAttrs argument to DMA/PCI API (we will discuss them later).

Sorry this is not a RESEND, this is first post of v3, I forgot to use '--clear-subject-prefix' and git-publish reused the same prefix from v2.