Skip to contentSkip to conversation

Introduction of IOMMUs into the memory API

PATCH In review Paolo Bonzini 9 participants 106 messages May 21, 2013 – May 29, 2013

lore
Share & tools
lore
#0 lore raw

cover Introduction of IOMMUs into the memory API

This is part 1 of the memory API updates for 1.6. (Part 2 is the introduction of get/set_owner, part 3 is the introduction of the RCU-style flatview).

Compared to the first submissions, there are a few extra cleanup patches, and address_space_rw/read/write grew a return value so that the existing DMAContext API doesn't need to change.

Paolo

Alexey Kardashevskiy (1):
memory: give name to every AddressSpace

Avi Kivity (5):
memory: fix address space initialization/destruction
memory: limit sections in the radix tree to the actual address space size
memory: iommu support
vfio: abort if an emulated iommu is used
pci: use memory core for iommu support

David Gibson (1):
memory: Add iommu map/unmap notifiers

Jan Kiszka (3):
memory: Replace open-coded memory_region_is_romd
memory: Rename readable flag to romd_mode
memory: Introduce address_space_lookup_region

Paolo Bonzini (20):
exec: remove obsolete comment
exec: eliminate qemu_put_ram_ptr
exec: make qemu_get_ram_ptr private
exec: eliminate stq_phys_notdirty
memory: assert that PhysPageEntry's ptr does not overflow
memory: allow memory_region_find() to run on non-root memory regions
memory: do not duplicate memory_region_destructor_none
memory: make memory_global_sync_dirty_bitmap take an AddressSpace
s390x: reduce TARGET_PHYS_ADDR_SPACE_BITS to 62
memory: create FlatView for new address spaces
memory: add address_space_valid
memory: clean up phys_page_find
memory: add address_space_translate
memory: add return value to address_space_rw/read/write
spapr: convert TCE API to use an opaque type
spapr: make IOMMU translation go through IOMMUTLBEntry
spapr: use memory core for iommu support
dma: eliminate old-style IOMMU support
spapr_vio: take care of creating our own AddressSpace/DMAContext
dma: eliminate DMAContext

 arch_init.c                    |   2 +-
 cputlb.c                       |  20 +--
 dma-helpers.c                  | 185 ++---------------------
 exec.c                         | 332 ++++++++++++++++++++++-------------------
 hw/block/pflash_cfi01.c        |   6 +-
 hw/block/pflash_cfi02.c        |   2 +-
 hw/dma/pl330.c                 |   8 +-
 hw/ide/ahci.c                  |  18 +--
 hw/ide/ahci.h                  |   4 +-
 hw/ide/ich.c                   |   2 +-
 hw/ide/macio.c                 |   4 +-
 hw/misc/vfio.c                 |   2 +
 hw/pci/pci.c                   |  46 +++---
 hw/ppc/spapr_iommu.c           | 117 +++++++--------
 hw/ppc/spapr_pci.c             |  15 +-
 hw/ppc/spapr_vio.c             |  13 +-
 hw/scsi/megasas.c              |   5 +-
 hw/scsi/virtio-scsi.c          |   2 +-
 hw/scsi/vmw_pvscsi.c           |   2 +-
 hw/sd/sdhci.c                  |  22 +--
 hw/usb/hcd-ehci-pci.c          |   4 +-
 hw/usb/hcd-ehci-sysbus.c       |   2 +-
 hw/usb/hcd-ehci.c              |  12 +-
 hw/usb/hcd-ehci.h              |   2 +-
 hw/usb/hcd-ohci.c              |  30 ++--
 hw/usb/libhw.c                 |   4 +-
 include/exec/cpu-common.h      |   4 -
 include/exec/cputlb.h          |  12 +-
 include/exec/memory-internal.h |   1 +
 include/exec/memory.h          | 172 +++++++++++++++------
 include/exec/poison.h          |   1 -
 include/hw/pci-host/spapr.h    |   3 +-
 include/hw/pci/pci.h           |  21 ++-
 include/hw/pci/pci_bus.h       |   4 +-
 include/hw/ppc/spapr.h         |  12 +-
 include/hw/ppc/spapr_vio.h     |  21 +--
 include/sysemu/dma.h           | 143 +++++-------------
 memory.c                       | 111 ++++++++++----
 target-s390x/cpu.h             |   5 +-
 trace-events                   |   3 -
 translate-all.c                |   8 +-
 41 files changed, 654 insertions(+), 728 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/30 exec: remove obsolete comment

See how we call memory_region_section_addr two lines below to convert a physical address to a base address in the region.

---
 exec.c | 6 ------
 1 file changed, 6 deletions(-)
exec.c +0 -6
index aec65c5..197625c 100644
@@ -639,12 +639,6 @@ hwaddr memory_region_section_get_iotlb(CPUArchState *env,
             iotlb |= phys_section_rom;
         }
     } else {
-        /* IO handlers are currently passed a physical address.
-           It would be nice to pass an offset from the base address
-           of that region.  This would avoid having to special case RAM,
-           and avoid full address decoding in every device.
-           We can't use the high bits of pd for this because
-           IO_MEM_ROMD uses these as a ram address.  */
         iotlb = section - phys_sections;
         iotlb += memory_region_section_addr(section, paddr);
     }
Signature · 1 line
1.8.1.4
Reviewed-by on v1 1/30
#2 lore raw

Re: [Qemu-devel] [PATCH 01/30] exec: remove obsolete comment

Quoted 4 lines · Paolo Bonzini

See how we call memory_region_section_addr two lines below to convert a physical address to a base address in the region.

(I'm sure I've reviewed this patch already in a previous version)

-- PMM

#3 lore raw

PATCH 2/30 exec: eliminate qemu_put_ram_ptr

In reply to Paolo Bonzini
---
 exec.c                    | 8 --------
 hw/pci/pci.c              | 2 --
 hw/scsi/megasas.c         | 1 -
 include/exec/cpu-common.h | 1 -
 trace-events              | 3 ---
 5 files changed, 15 deletions(-)
exec.c +0 -8
index 197625c..fa5f9c3 100644
@@ -1334,11 +1334,6 @@ static void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
     }
 }
 
-void qemu_put_ram_ptr(void *addr)
-{
-    trace_qemu_put_ram_ptr(addr);
-}
-
 int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
 {
     RAMBlock *block;
@@ -1928,7 +1923,6 @@ void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
                 ptr = qemu_get_ram_ptr(addr1);
                 memcpy(ptr, buf, l);
                 invalidate_and_set_dirty(addr1, l);
-                qemu_put_ram_ptr(ptr);
             }
         } else {
             if (!(memory_region_is_ram(section->mr) ||
@@ -1958,7 +1952,6 @@ void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
                                        + memory_region_section_addr(section,
                                                                     addr));
                 memcpy(buf, ptr, l);
-                qemu_put_ram_ptr(ptr);
             }
         }
         len -= l;
@@ -2020,7 +2013,6 @@ void cpu_physical_memory_write_rom(hwaddr addr,
             ptr = qemu_get_ram_ptr(addr1);
             memcpy(ptr, buf, l);
             invalidate_and_set_dirty(addr1, l);
-            qemu_put_ram_ptr(ptr);
         }
         len -= l;
         buf += l;
hw/pci/pci.c +0 -2
index d5257ed..bb3879b 100644
@@ -1959,8 +1959,6 @@ static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom)
         pci_patch_ids(pdev, ptr, size);
     }
 
-    qemu_put_ram_ptr(ptr);
-
     pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
 
     return 0;
hw/scsi/megasas.c +0 -1
index 4934a81..fe6550c 100644
@@ -711,7 +711,6 @@ static int megasas_ctrl_get_info(MegasasState *s, MegasasCmd *cmd)
 
         ptr = memory_region_get_ram_ptr(&s->dev.rom);
         memcpy(biosver, ptr + 0x41, 31);
-        qemu_put_ram_ptr(ptr);
         memcpy(info.image_component[1].name, "BIOS", 4);
         memcpy(info.image_component[1].version, biosver,
                strlen((const char *)biosver));
include/exec/cpu-common.h +0 -1
index 2e5f11f..cafc3c2 100644
@@ -51,7 +51,6 @@ typedef uint32_t CPUReadMemoryFunc(void *opaque, hwaddr addr);
 void qemu_ram_remap(ram_addr_t addr, ram_addr_t length);
 /* This should only be used for ram local to a device.  */
 void *qemu_get_ram_ptr(ram_addr_t addr);
-void qemu_put_ram_ptr(void *addr);
 /* This should not be used by devices.  */
 int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr);
 ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr);
trace-events +0 -3
index 9c73931..b123b0f 100644
@@ -813,9 +813,6 @@ xen_map_cache_return(void* ptr) "%p"
 xen_map_block(uint64_t phys_addr, uint64_t size) "%#"PRIx64", size %#"PRIx64
 xen_unmap_block(void* addr, unsigned long size) "%p, size %#lx"
 
-# exec.c
-qemu_put_ram_ptr(void* addr) "%p"
-
 # hw/xen_platform.c
 xen_platform_log(char *s) "xen platform: %s"
 
Signature · 1 line
1.8.1.4
#5 lore raw

PATCH 3/30 exec: make qemu_get_ram_ptr private

In reply to Paolo Bonzini

It is a private interface between exec.c and memory.c.

---
 include/exec/cpu-common.h      | 2 --
 include/exec/memory-internal.h | 1 +
 2 files changed, 1 insertion(+), 2 deletions(-)
include/exec/cpu-common.h +0 -2
index cafc3c2..af851aa 100644
@@ -49,8 +49,6 @@ typedef void CPUWriteMemoryFunc(void *opaque, hwaddr addr, uint32_t value);
 typedef uint32_t CPUReadMemoryFunc(void *opaque, hwaddr addr);
 
 void qemu_ram_remap(ram_addr_t addr, ram_addr_t length);
-/* This should only be used for ram local to a device.  */
-void *qemu_get_ram_ptr(ram_addr_t addr);
 /* This should not be used by devices.  */
 int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr);
 ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr);
include/exec/memory-internal.h +1 -0
index 1b156fd..8d15f90 100644
@@ -46,6 +46,7 @@ void address_space_destroy_dispatch(AddressSpace *as);
 ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                    MemoryRegion *mr);
 ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr);
+void *qemu_get_ram_ptr(ram_addr_t addr);
 void qemu_ram_free(ram_addr_t addr);
 void qemu_ram_free_from_ptr(ram_addr_t addr);
 
Signature · 1 line
1.8.1.4
#7 lore raw

PATCH 4/30 exec: eliminate stq_phys_notdirty

In reply to Paolo Bonzini

It is not used anywhere.

---
 exec.c                    | 27 ---------------------------
 include/exec/cpu-common.h |  1 -
 include/exec/poison.h     |  1 -
 3 files changed, 29 deletions(-)
exec.c +0 -27
index fa5f9c3..1355661 100644
@@ -2390,33 +2390,6 @@ void stl_phys_notdirty(hwaddr addr, uint32_t val)
     }
 }
 
-void stq_phys_notdirty(hwaddr addr, uint64_t val)
-{
-    uint8_t *ptr;
-    MemoryRegionSection *section;
-
-    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
-
-    if (!memory_region_is_ram(section->mr) || section->readonly) {
-        addr = memory_region_section_addr(section, addr);
-        if (memory_region_is_ram(section->mr)) {
-            section = &phys_sections[phys_section_rom];
-        }
-#ifdef TARGET_WORDS_BIGENDIAN
-        io_mem_write(section->mr, addr, val >> 32, 4);
-        io_mem_write(section->mr, addr + 4, (uint32_t)val, 4);
-#else
-        io_mem_write(section->mr, addr, (uint32_t)val, 4);
-        io_mem_write(section->mr, addr + 4, val >> 32, 4);
-#endif
-    } else {
-        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
-                                & TARGET_PAGE_MASK)
-                               + memory_region_section_addr(section, addr));
-        stq_p(ptr, val);
-    }
-}
-
 /* warning: addr must be aligned */
 static inline void stl_phys_internal(hwaddr addr, uint32_t val,
                                      enum device_endian endian)
include/exec/cpu-common.h +0 -1
index af851aa..af5258d 100644
@@ -102,7 +102,6 @@ uint32_t lduw_phys(hwaddr addr);
 uint32_t ldl_phys(hwaddr addr);
 uint64_t ldq_phys(hwaddr addr);
 void stl_phys_notdirty(hwaddr addr, uint32_t val);
-void stq_phys_notdirty(hwaddr addr, uint64_t val);
 void stw_phys(hwaddr addr, uint32_t val);
 void stl_phys(hwaddr addr, uint32_t val);
 void stq_phys(hwaddr addr, uint64_t val);
include/exec/poison.h +0 -1
index 7d7b23b..2341a75 100644
@@ -42,7 +42,6 @@
 #pragma GCC poison ldl_phys
 #pragma GCC poison ldq_phys
 #pragma GCC poison stl_phys_notdirty
-#pragma GCC poison stq_phys_notdirty
 #pragma GCC poison stw_phys
 #pragma GCC poison stl_phys
 #pragma GCC poison stq_phys
Signature · 1 line
1.8.1.4
#8 lore raw

Re: [Qemu-devel] [PATCH 04/30] exec: eliminate stq_phys_notdirty

Quoted 1 line · Paolo Bonzini

It is not used anywhere.

So does this get deleted just because it's not used, or because it gets in the way of patches later in the series. As far as I can tell the st*_phys_notdirty() are for TCG to update page table entries with accessed/modified bits, and I don't see any reason we wouldn't have targets that need to update a 64 bit page table descriptor rather than just 32 bits of it.

thanks
-- PMM

Reviewed-by
#9 lore raw

Re: [Qemu-devel] [PATCH 04/30] exec: eliminate stq_phys_notdirty

Quoted 9 lines · Peter Maydell
Quoted 1 line · Paolo Bonzini

It is not used anywhere.

So does this get deleted just because it's not used, or because it gets in the way of patches later in the series. As far as I can tell the st*_phys_notdirty() are for TCG to update page table entries with accessed/modified bits, and I don't see any reason we wouldn't have targets that need to update a 64 bit page table descriptor rather than just 32 bits of it.

Unused code is untested code. Untested code is broken code.

Someone can add it back later if it's ever needed.

Regards,

Anthony Liguori

Quoted 3 lines

thanks
-- PMM

#10 lore raw

Re: [Qemu-devel] [PATCH 04/30] exec: eliminate stq_phys_notdirty

In reply to Peter Maydell

Il 23/05/2013 19:32, Peter Maydell ha scritto:

Quoted 4 lines · Paolo Bonzini
Quoted 1 line
Quoted 1 line

It is not used anywhere.

So does this get deleted just because it's not used, or because it gets in the way of patches later in the series.

No, it doesn't get in the way, I just preferred to kill it rather than have an untested conversion.

Paolo

#11 lore raw

Re: [Qemu-devel] [PATCH 04/30] exec: eliminate stq_phys_notdirty

Quoted 8 lines · Paolo Bonzini

Il 23/05/2013 19:32, Peter Maydell ha scritto:

Quoted 4 lines · Paolo Bonzini
Quoted 1 line
Quoted 1 line

It is not used anywhere.

So does this get deleted just because it's not used, or because it gets in the way of patches later in the series.

No, it doesn't get in the way, I just preferred to kill it rather than have an untested conversion.

Fair enough -- we can always put it back again if we need it.

-- PMM

#12 lore raw

PATCH 5/30 memory: assert that PhysPageEntry's ptr does not overflow

In reply to Paolo Bonzini

While sized to 15 bits in PhysPageEntry, the ptr field is ORed into the iotlb entries together with a page-aligned pointer. The ptr field must not overflow into this page-aligned value, assert that it is smaller than the page size.

---
 exec.c | 2 ++
 1 file changed, 2 insertions(+)
exec.c +2 -0
index 1355661..380245f 100644
@@ -713,6 +713,8 @@ static void destroy_all_mappings(AddressSpaceDispatch *d)
 
 static uint16_t phys_section_add(MemoryRegionSection *section)
 {
+    assert(phys_sections_nb < TARGET_PAGE_SIZE);
+
     if (phys_sections_nb == phys_sections_nb_alloc) {
         phys_sections_nb_alloc = MAX(phys_sections_nb_alloc * 2, 16);
         phys_sections = g_renew(MemoryRegionSection, phys_sections,
Signature · 1 line
1.8.1.4
Reviewed-by on v1 5/30
#13 lore raw

Re: [Qemu-devel] [PATCH 05/30] memory: assert that PhysPageEntry's ptr does not overflow

Quoted 15 lines · Paolo Bonzini

While sized to 15 bits in PhysPageEntry, the ptr field is ORed into the iotlb entries together with a page-aligned pointer. The ptr field must not overflow into this page-aligned value, assert that it is smaller than the page size.

---
 exec.c | 2 ++
 1 file changed, 2 insertions(+)
exec.c +0 -0
index 1355661..380245f 100644

exec.c static void destroy_all_mappings(AddressSpaceDispatch *d) · line 715

Quoted 3 lines
exec.c +0 -0
@@ -713,6 +713,8 @@ static void destroy_all_mappings(AddressSpaceDispatch *d)
 
 static uint16_t phys_section_add(MemoryRegionSection *section)
 {

You might consider having the rationale for the assert in a comment here as well as in the commit log.

exec.c static void destroy_all_mappings(AddressSpaceDispatch *d) · line 720

Quoted 5 lines
exec.c +2 -0
+    assert(phys_sections_nb < TARGET_PAGE_SIZE);
+
     if (phys_sections_nb == phys_sections_nb_alloc) {
         phys_sections_nb_alloc = MAX(phys_sections_nb_alloc * 2, 16);
         phys_sections = g_renew(MemoryRegionSection, phys_sections,

-- PMM

#14 lore raw

PATCH 6/30 memory: allow memory_region_find() to run on non-root memory regions

In reply to Paolo Bonzini

memory_region_find() is similar to registering a MemoryListener and checking for the MemoryRegionSections that come from a particular region. There is no reason for this to be limited to a root memory region.

---
 include/exec/memory.h | 13 +++++++------
 memory.c              | 20 +++++++++++++++-----
 2 files changed, 22 insertions(+), 11 deletions(-)
include/exec/memory.h +7 -6
index 9e88320..efe210b 100644
@@ -725,17 +725,18 @@ void memory_region_set_alias_offset(MemoryRegion *mr,
  *
  * Returns a #MemoryRegionSection that describes a contiguous overlap.
  * It will have the following characteristics:
- *    .@offset_within_address_space >= @addr
- *    .@offset_within_address_space + .@size <= @addr + @size
  *    .@size = 0 iff no overlap was found
  *    .@mr is non-%NULL iff an overlap was found
  *
- * @address_space: a top-level (i.e. parentless) region that contains
- *       the region to be found
- * @addr: start of the area within @address_space to be searched
+ * If @mr has no parent, @addr is an absolute address in an AddressSpace, hence:
+ *    .@offset_within_address_space >= @addr
+ *    .@offset_within_address_space + .@size <= @addr + @size
+ *
+ * @mr: a MemoryRegion within which @addr is a relative address
+ * @addr: start of the area within @as to be searched
  * @size: size of the area to be searched
  */
-MemoryRegionSection memory_region_find(MemoryRegion *address_space,
+MemoryRegionSection memory_region_find(MemoryRegion *mr,
                                        hwaddr addr, uint64_t size);
 
 /**
memory.c +15 -5
index 75ca281..34bfb13 100644
@@ -1451,15 +1451,24 @@ static FlatRange *address_space_lookup(AddressSpace *as, AddrRange addr)
                    sizeof(FlatRange), cmp_flatrange_addr);
 }
 
-MemoryRegionSection memory_region_find(MemoryRegion *address_space,
+MemoryRegionSection memory_region_find(MemoryRegion *mr,
                                        hwaddr addr, uint64_t size)
 {
-    AddressSpace *as = memory_region_to_address_space(address_space);
-    AddrRange range = addrrange_make(int128_make64(addr),
-                                     int128_make64(size));
-    FlatRange *fr = address_space_lookup(as, range);
     MemoryRegionSection ret = { .mr = NULL, .size = 0 };
+    MemoryRegion *root;
+    AddressSpace *as;
+    AddrRange range;
+    FlatRange *fr;
+
+    addr += mr->addr;
+    for (root = mr; root->parent; ) {
+        root = root->parent;
+        addr += root->addr;
+    }
 
+    as = memory_region_to_address_space(root);
+    range = addrrange_make(int128_make64(addr), int128_make64(size));
+    fr = address_space_lookup(as, range);
     if (!fr) {
         return ret;
     }
@@ -1470,6 +1479,7 @@ MemoryRegionSection memory_region_find(MemoryRegion *address_space,
     }
 
     ret.mr = fr->mr;
+    ret.address_space = as;
     range = addrrange_intersection(range, fr->addr);
     ret.offset_within_region = fr->offset_in_region;
     ret.offset_within_region += int128_get64(int128_sub(range.start,
Signature · 1 line
1.8.1.4
#15 lore raw

Re: [Qemu-devel] [PATCH 06/30] memory: allow memory_region_find() to run on non-root memory regions

Quoted 25 lines · Paolo Bonzini

memory_region_find() is similar to registering a MemoryListener and checking for the MemoryRegionSections that come from a particular region. There is no reason for this to be limited to a root memory region.

---
 include/exec/memory.h | 13 +++++++------
 memory.c              | 20 +++++++++++++++-----
 2 files changed, 22 insertions(+), 11 deletions(-)
include/exec/memory.h +0 -3
index 9e88320..efe210b 100644
@@ -725,17 +725,18 @@ void memory_region_set_alias_offset(MemoryRegion *mr,
  *
  * Returns a #MemoryRegionSection that describes a contiguous overlap.
  * It will have the following characteristics:
- *    .@offset_within_address_space >= @addr
- *    .@offset_within_address_space + .@size <= @addr + @size
  *    .@size = 0 iff no overlap was found
  *    .@mr is non-%NULL iff an overlap was found
  *
- * @address_space: a top-level (i.e. parentless) region that contains

include/exec/memory.h void memory_region_set_alias_offset(MemoryRegion *mr, · line 738

Quoted 10 lines
include/exec/memory.h +6 -2
- *       the region to be found
- * @addr: start of the area within @address_space to be searched
+ * If @mr has no parent, @addr is an absolute address in an AddressSpace, hence:
+ *    .@offset_within_address_space >= @addr
+ *    .@offset_within_address_space + .@size <= @addr + @size
+ *
+ * @mr: a MemoryRegion within which @addr is a relative address
+ * @addr: start of the area within @as to be searched
  * @size: size of the area to be searched
  */

You need to change the first para of the doc comment, which still says @address_space. In fact the entire doc comment is pretty confusing now. The code looks OK but I forget what the rationale for this change is; it would be nice if the comment was informative enough to remind me :-)

-- PMM

#16 lore raw

PATCH 7/30 memory: Replace open-coded memory_region_is_romd

In reply to Paolo Bonzini

From: Jan Kiszka <[email protected]>

Improves readability.

---
 translate-all.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
translate-all.c +1 -1
index da93608..0d84b0d 100644
@@ -1359,7 +1359,7 @@ void tb_invalidate_phys_addr(hwaddr addr)
     section = phys_page_find(address_space_memory.dispatch,
                              addr >> TARGET_PAGE_BITS);
     if (!(memory_region_is_ram(section->mr)
-          || (section->mr->rom_device && section->mr->readable))) {
+          || memory_region_is_romd(section->mr))) {
         return;
     }
     ram_addr = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
Signature · 1 line
1.8.1.4
#18 lore raw

PATCH 8/30 memory: Rename readable flag to romd_mode

In reply to Paolo Bonzini

From: Jan Kiszka <[email protected]>

"Readable" is a very unfortunate name for this flag because even a rom_device region will always be readable from the guest POV. What differs is the mapping, just like the comments had to explain already. Also, readable could currently be understood as being a generic region flag, but it only applies to rom_device regions.

So rename the flag and the function to modify it after the original term "ROMD" which could also be interpreted as "ROM direct", i.e. ROM mode with direct access. In any case, the scope of the flag is clearer now.

---
 hw/block/pflash_cfi01.c |  6 +++---
 hw/block/pflash_cfi02.c |  2 +-
 include/exec/memory.h   | 22 +++++++++++-----------
 memory.c                | 30 +++++++++++++++---------------
 4 files changed, 30 insertions(+), 30 deletions(-)
hw/block/pflash_cfi01.c +3 -3
index 3ff20e0..63d7c99 100644
@@ -105,7 +105,7 @@ static void pflash_timer (void *opaque)
     DPRINTF("%s: command %02x done\n", __func__, pfl->cmd);
     /* Reset flash */
     pfl->status ^= 0x80;
-    memory_region_rom_device_set_readable(&pfl->mem, true);
+    memory_region_rom_device_set_romd(&pfl->mem, true);
     pfl->wcycle = 0;
     pfl->cmd = 0;
 }
@@ -281,7 +281,7 @@ static void pflash_write(pflash_t *pfl, hwaddr offset,
 
     if (!pfl->wcycle) {
         /* Set the device in I/O access mode */
-        memory_region_rom_device_set_readable(&pfl->mem, false);
+        memory_region_rom_device_set_romd(&pfl->mem, false);
     }
 
     switch (pfl->wcycle) {
@@ -458,7 +458,7 @@ static void pflash_write(pflash_t *pfl, hwaddr offset,
                   "\n", __func__, offset, pfl->wcycle, pfl->cmd, value);
 
  reset_flash:
-    memory_region_rom_device_set_readable(&pfl->mem, true);
+    memory_region_rom_device_set_romd(&pfl->mem, true);
 
     pfl->wcycle = 0;
     pfl->cmd = 0;
hw/block/pflash_cfi02.c +1 -1
index 9a7fa70..5f25246 100644
@@ -111,7 +111,7 @@ static void pflash_setup_mappings(pflash_t *pfl)
 
 static void pflash_register_memory(pflash_t *pfl, int rom_mode)
 {
-    memory_region_rom_device_set_readable(&pfl->orig_mem, rom_mode);
+    memory_region_rom_device_set_romd(&pfl->orig_mem, rom_mode);
     pfl->rom_mode = rom_mode;
 }
 
include/exec/memory.h +11 -11
index efe210b..0409180 100644
@@ -126,7 +126,7 @@ struct MemoryRegion {
     ram_addr_t ram_addr;
     bool subpage;
     bool terminates;
-    bool readable;
+    bool romd_mode;
     bool ram;
     bool readonly; /* For RAM regions */
     bool enabled;
@@ -355,16 +355,16 @@ uint64_t memory_region_size(MemoryRegion *mr);
 bool memory_region_is_ram(MemoryRegion *mr);
 
 /**
- * memory_region_is_romd: check whether a memory region is ROMD
+ * memory_region_is_romd: check whether a memory region is in ROMD mode
  *
- * Returns %true is a memory region is ROMD and currently set to allow
+ * Returns %true if a memory region is a ROM device and currently set to allow
  * direct reads.
  *
  * @mr: the memory region being queried
  */
 static inline bool memory_region_is_romd(MemoryRegion *mr)
 {
-    return mr->rom_device && mr->readable;
+    return mr->rom_device && mr->romd_mode;
 }
 
 /**
@@ -502,18 +502,18 @@ void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
 
 /**
- * memory_region_rom_device_set_readable: enable/disable ROM readability
+ * memory_region_rom_device_set_romd: enable/disable ROMD mode
  *
  * Allows a ROM device (initialized with memory_region_init_rom_device() to
- * to be marked as readable (default) or not readable.  When it is readable,
- * the device is mapped to guest memory.  When not readable, reads are
- * forwarded to the #MemoryRegion.read function.
+ * set to ROMD mode (default) or MMIO mode.  When it is in ROMD mode, the
+ * device is mapped to guest memory and satisfies read access directly.
+ * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
+ * Writes are always handled by the #MemoryRegion.write function.
  *
  * @mr: the memory region to be updated
- * @readable: whether reads are satisified directly (%true) or via callbacks
- *            (%false)
+ * @romd_mode: %true to put the region into ROMD mode
  */
-void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable);
+void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
 
 /**
  * memory_region_set_coalescing: Enable memory coalescing for the region.
memory.c +15 -15
index 34bfb13..013464b 100644
@@ -213,7 +213,7 @@ struct FlatRange {
     hwaddr offset_in_region;
     AddrRange addr;
     uint8_t dirty_log_mask;
-    bool readable;
+    bool romd_mode;
     bool readonly;
 };
 
@@ -236,7 +236,7 @@ static bool flatrange_equal(FlatRange *a, FlatRange *b)
     return a->mr == b->mr
         && addrrange_equal(a->addr, b->addr)
         && a->offset_in_region == b->offset_in_region
-        && a->readable == b->readable
+        && a->romd_mode == b->romd_mode
         && a->readonly == b->readonly;
 }
 
@@ -276,7 +276,7 @@ static bool can_merge(FlatRange *r1, FlatRange *r2)
                                 r1->addr.size),
                      int128_make64(r2->offset_in_region))
         && r1->dirty_log_mask == r2->dirty_log_mask
-        && r1->readable == r2->readable
+        && r1->romd_mode == r2->romd_mode
         && r1->readonly == r2->readonly;
 }
 
@@ -532,7 +532,7 @@ static void render_memory_region(FlatView *view,
             fr.offset_in_region = offset_in_region;
             fr.addr = addrrange_make(base, now);
             fr.dirty_log_mask = mr->dirty_log_mask;
-            fr.readable = mr->readable;
+            fr.romd_mode = mr->romd_mode;
             fr.readonly = readonly;
             flatview_insert(view, i, &fr);
             ++i;
@@ -552,7 +552,7 @@ static void render_memory_region(FlatView *view,
         fr.offset_in_region = offset_in_region;
         fr.addr = addrrange_make(base, remain);
         fr.dirty_log_mask = mr->dirty_log_mask;
-        fr.readable = mr->readable;
+        fr.romd_mode = mr->romd_mode;
         fr.readonly = readonly;
         flatview_insert(view, i, &fr);
     }
@@ -801,7 +801,7 @@ void memory_region_init(MemoryRegion *mr,
     mr->enabled = true;
     mr->terminates = false;
     mr->ram = false;
-    mr->readable = true;
+    mr->romd_mode = true;
     mr->readonly = false;
     mr->rom_device = false;
     mr->destructor = memory_region_destructor_none;
@@ -1121,11 +1121,11 @@ void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
     }
 }
 
-void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable)
+void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
 {
-    if (mr->readable != readable) {
+    if (mr->romd_mode != romd_mode) {
         memory_region_transaction_begin();
-        mr->readable = readable;
+        mr->romd_mode = romd_mode;
         memory_region_update_pending |= mr->enabled;
         memory_region_transaction_commit();
     }
@@ -1659,9 +1659,9 @@ static void mtree_print_mr(fprintf_function mon_printf, void *f,
                    base + mr->addr
                    + (hwaddr)int128_get64(mr->size) - 1,
                    mr->priority,
-                   mr->readable ? 'R' : '-',
-                   !mr->readonly && !(mr->rom_device && mr->readable) ? 'W'
-                                                                      : '-',
+                   mr->romd_mode ? 'R' : '-',
+                   !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
+                                                                       : '-',
                    mr->name,
                    mr->alias->name,
                    mr->alias_offset,
@@ -1674,9 +1674,9 @@ static void mtree_print_mr(fprintf_function mon_printf, void *f,
                    base + mr->addr
                    + (hwaddr)int128_get64(mr->size) - 1,
                    mr->priority,
-                   mr->readable ? 'R' : '-',
-                   !mr->readonly && !(mr->rom_device && mr->readable) ? 'W'
-                                                                      : '-',
+                   mr->romd_mode ? 'R' : '-',
+                   !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
+                                                                       : '-',
                    mr->name);
     }
 
Signature · 1 line
1.8.1.4
#19 lore raw

PATCH 9/30 memory: do not duplicate memory_region_destructor_none

In reply to Paolo Bonzini
---
 memory.c | 5 -----
 1 file changed, 5 deletions(-)
memory.c +0 -5
index 013464b..5431463 100644
@@ -768,10 +768,6 @@ static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
     qemu_ram_free_from_ptr(mr->ram_addr);
 }
 
-static void memory_region_destructor_iomem(MemoryRegion *mr)
-{
-}
-
 static void memory_region_destructor_rom_device(MemoryRegion *mr)
 {
     qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
@@ -929,7 +925,6 @@ void memory_region_init_io(MemoryRegion *mr,
     mr->ops = ops;
     mr->opaque = opaque;
     mr->terminates = true;
-    mr->destructor = memory_region_destructor_iomem;
     mr->ram_addr = ~(ram_addr_t)0;
 }
 
Signature · 1 line
1.8.1.4
#21 lore raw

PATCH 10/30 memory: make memory_global_sync_dirty_bitmap take an AddressSpace

In reply to Paolo Bonzini
---
 arch_init.c           | 2 +-
 include/exec/memory.h | 7 +++----
 memory.c              | 3 +--
 3 files changed, 5 insertions(+), 7 deletions(-)
arch_init.c +1 -1
index 49c5dc2..5d32ecf 100644
@@ -386,7 +386,7 @@ static void migration_bitmap_sync(void)
     }
 
     trace_migration_bitmap_sync_start();
-    memory_global_sync_dirty_bitmap(get_system_memory());
+    address_space_sync_dirty_bitmap(&address_space_memory);
 
     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
         for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
include/exec/memory.h +3 -4
index 0409180..9fae89b 100644
@@ -757,13 +757,12 @@ memory_region_section_addr(MemoryRegionSection *section,
 }
 
 /**
- * memory_global_sync_dirty_bitmap: synchronize the dirty log for all memory
+ * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
  *
  * Synchronizes the dirty page log for an entire address space.
- * @address_space: a top-level (i.e. parentless) region that contains the
- *       memory being synchronized
+ * @as: the address space that contains the memory being synchronized
  */
-void memory_global_sync_dirty_bitmap(MemoryRegion *address_space);
+void address_space_sync_dirty_bitmap(AddressSpace *as);
 
 /**
  * memory_region_transaction_begin: Start a transaction.
memory.c +1 -2
index 5431463..11bbeb7 100644
@@ -1485,9 +1485,8 @@ MemoryRegionSection memory_region_find(MemoryRegion *mr,
     return ret;
 }
 
-void memory_global_sync_dirty_bitmap(MemoryRegion *address_space)
+void address_space_sync_dirty_bitmap(AddressSpace *as)
 {
-    AddressSpace *as = memory_region_to_address_space(address_space);
     FlatRange *fr;
 
     FOR_EACH_FLAT_RANGE(fr, as->current_map) {
Signature · 1 line
1.8.1.4
#23 lore raw

Re: [Qemu-devel] [PATCH 10/30] memory: make memory_global_sync_dirty_bitmap take an AddressSpace

In reply to Paolo Bonzini
Quoted 2 lines · Paolo Bonzini

Hrm. This could surely do with some sort of rationale in the comment.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#24 lore raw

PATCH 11/30 memory: fix address space initialization/destruction

In reply to Paolo Bonzini

From: Avi Kivity <[email protected]>

A couple of fields were left uninitialized. This was not observed earlier because all address spaces were statically allocated. Also free allocation for those fields.

---
 memory.c | 3 +++
 1 file changed, 3 insertions(+)
memory.c +3 -0
index 11bbeb7..9478f98 100644
@@ -1572,6 +1572,8 @@ void address_space_init(AddressSpace *as, MemoryRegion *root)
     as->root = root;
     as->current_map = g_new(FlatView, 1);
     flatview_init(as->current_map);
+    as->ioeventfd_nb = 0;
+    as->ioeventfds = NULL;
     QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
     as->name = NULL;
     memory_region_transaction_commit();
@@ -1588,6 +1590,7 @@ void address_space_destroy(AddressSpace *as)
     address_space_destroy_dispatch(as);
     flatview_destroy(as->current_map);
     g_free(as->current_map);
+    g_free(as->ioeventfds);
 }
 
 uint64_t io_mem_read(MemoryRegion *mr, hwaddr addr, unsigned size)
Signature · 1 line
1.8.1.4
Reviewed-by on v1 11/30
#25 lore raw

Re: [Qemu-devel] [PATCH 11/30] memory: fix address space initialization/destruction

Quoted 8 lines · Paolo Bonzini

From: Avi Kivity <[email protected]>

A couple of fields were left uninitialized. This was not observed earlier because all address spaces were statically allocated. Also free allocation for those fields.

-- PMM

#26 lore raw

PATCH 12/30 s390x: reduce TARGET_PHYS_ADDR_SPACE_BITS to 62

In reply to Paolo Bonzini

With the next patch, the memory API will complain if the TARGET_PHYS_ADDR_SPACE_BITS gets dangerously close to an overflow. s390x can handle up to 64 bit of physical address space from its page tables, but we never use that much. Just decrease the value.

---
 target-s390x/cpu.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target-s390x/cpu.h +4 -1
index 0ce82cf..6304c4d 100644
@@ -34,7 +34,10 @@
 #include "exec/cpu-defs.h"
 #define TARGET_PAGE_BITS 12
 
-#define TARGET_PHYS_ADDR_SPACE_BITS 64
+/* Actually 64-bits, limited by the memory API to 62 bits.  We
+ * never use that much.
+ */
+#define TARGET_PHYS_ADDR_SPACE_BITS 62
 #define TARGET_VIRT_ADDR_SPACE_BITS 64
 
 #include "exec/cpu-all.h"
Signature · 1 line
1.8.1.4
#27 lore raw

PATCH 13/30 memory: limit sections in the radix tree to the actual address space size

In reply to Paolo Bonzini

From: Avi Kivity <[email protected]>

The radix tree is statically sized to fit TARGET_PHYS_ADDR_SPACE_BITS. If a larger memory region is registered, it will overflow.

Fix by limiting any section in the radix tree to the supported size.

This problem was not observed earlier since artificial regions (containers and aliases) are eliminated by the memory core, leaving only device regions which have reasonable sizes. An IOMMU however cannot be eliminated by the memory core, and may have an artificial size.

[ Fail the build if TARGET_PHYS_ADDR_SPACE_BITS is too large - Paolo ]
---
 exec.c                | 13 ++++++++++++-
 include/exec/memory.h |  3 +++
 2 files changed, 15 insertions(+), 1 deletion(-)
exec.c +12 -1
index 380245f..8d91221 100644
@@ -771,10 +771,21 @@ static void register_multipage(AddressSpaceDispatch *d, MemoryRegionSection *sec
                   section_index);
 }
 
+QEMU_BUILD_BUG_ON(TARGET_PHYS_ADDR_SPACE_BITS > MAX_PHYS_ADDR_SPACE_BITS)
+
+static MemoryRegionSection limit(MemoryRegionSection section)
+{
+    section.size = MIN(section.offset_within_address_space + section.size,
+                       MAX_PHYS_ADDR + 1)
+                   - section.offset_within_address_space;
+
+    return section;
+}
+
 static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
 {
     AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
-    MemoryRegionSection now = *section, remain = *section;
+    MemoryRegionSection now = limit(*section), remain = limit(*section);
 
     if ((now.offset_within_address_space & ~TARGET_PAGE_MASK)
         || (now.size < TARGET_PAGE_SIZE)) {
include/exec/memory.h +3 -0
index 9fae89b..6ed593c 100644
@@ -26,6 +26,9 @@
 #include "exec/ioport.h"
 #include "qemu/int128.h"
 
+#define MAX_PHYS_ADDR_SPACE_BITS 62
+#define MAX_PHYS_ADDR            (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
+
 typedef struct MemoryRegionOps MemoryRegionOps;
 typedef struct MemoryRegionPortio MemoryRegionPortio;
 typedef struct MemoryRegionMmio MemoryRegionMmio;
Signature · 1 line
1.8.1.4
Reviewed-by on v1 13/30
#28 lore raw

Re: [Qemu-devel] [PATCH 13/30] memory: limit sections in the radix tree to the actual address space size

Quoted 7 lines · Paolo Bonzini

From: Avi Kivity <[email protected]>

The radix tree is statically sized to fit TARGET_PHYS_ADDR_SPACE_BITS. If a larger memory region is registered, it will overflow.

Fix by limiting any section in the radix tree to the supported size.

Quoted 8 lines

This problem was not observed earlier since artificial regions (containers and aliases) are eliminated by the memory core, leaving only device regions which have reasonable sizes. An IOMMU however cannot be eliminated by the memory core, and may have an artificial size.

[ Fail the build if TARGET_PHYS_ADDR_SPACE_BITS is too large - Paolo ]

-- PMM

#29 lore raw

PATCH 14/30 memory: create FlatView for new address spaces

In reply to Paolo Bonzini

Even a new address space might have a non-empty FlatView. In order to initialize it properly, address_space_init should (a) call memory_region_transaction_commit after the address space is inserted into the list; (b) force memory_region_transaction_commit to do something.

This bug was latent so far because all address spaces started empty, including the PCI address space where the bus master region is initially disabled. However, the target address space of an IOMMU is usually rooted at get_system_memory(), which might not be empty at the time the IOMMU is created.

---
 memory.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
memory.c +2 -1
index 9478f98..99f046d 100644
@@ -1576,8 +1576,9 @@ void address_space_init(AddressSpace *as, MemoryRegion *root)
     as->ioeventfds = NULL;
     QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
     as->name = NULL;
-    memory_region_transaction_commit();
     address_space_init_dispatch(as);
+    memory_region_update_pending |= root->enabled;
+    memory_region_transaction_commit();
 }
 
 void address_space_destroy(AddressSpace *as)
Signature · 1 line
1.8.1.4
Reviewed-by on v1 14/30
#30 lore raw

Re: [Qemu-devel] [PATCH 14/30] memory: create FlatView for new address spaces

Quoted 11 lines · Paolo Bonzini

Even a new address space might have a non-empty FlatView. In order to initialize it properly, address_space_init should (a) call memory_region_transaction_commit after the address space is inserted into the list; (b) force memory_region_transaction_commit to do something.

This bug was latent so far because all address spaces started empty, including the PCI address space where the bus master region is initially disabled. However, the target address space of an IOMMU is usually rooted at get_system_memory(), which might not be empty at the time the IOMMU is created.

The patch title should probably be "populate FlatView for new address spaces", not "create". Given that,

-- PMM

#31 lore raw

PATCH 15/30 memory: add address_space_valid

In reply to Paolo Bonzini

The old-style IOMMU lets you check whether an access is valid in a given DMAContext. There is no equivalent for AddressSpace in the memory API, implement it with a lookup of the dispatch tree.

---
 dma-helpers.c         |  5 +++++
 exec.c                | 25 +++++++++++++++++++++++++
 include/exec/memory.h | 14 ++++++++++++++
 include/sysemu/dma.h  |  3 ++-
 4 files changed, 46 insertions(+), 1 deletion(-)
dma-helpers.c +5 -0
index 272632f..2962b69 100644
@@ -298,6 +298,11 @@ bool iommu_dma_memory_valid(DMAContext *dma, dma_addr_t addr, dma_addr_t len,
             plen = len;
         }
 
+        if (!address_space_valid(dma->as, paddr, len,
+                                 dir == DMA_DIRECTION_FROM_DEVICE)) {
+            return false;
+        }
+
         len -= plen;
         addr += plen;
     }
exec.c +25 -0
index 8d91221..8f1b507 100644
@@ -2079,6 +2079,31 @@ static void cpu_notify_map_clients(void)
     }
 }
 
+bool address_space_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
+{
+    AddressSpaceDispatch *d = as->dispatch;
+    MemoryRegionSection *section;
+    int l;
+    hwaddr page;
+
+    while (len > 0) {
+        page = addr & TARGET_PAGE_MASK;
+        l = (page + TARGET_PAGE_SIZE) - addr;
+        if (l > len) {
+            l = len;
+        }
+        section = phys_page_find(d, addr >> TARGET_PAGE_BITS);
+        if (section->mr == &io_mem_unassigned ||
+            (is_write && section->mr->readonly)) {
+            return false;
+        }
+
+        len -= l;
+        addr += l;
+    }
+    return true;
+}
+
 /* 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 if resources needed to perform the mapping are exhausted.
include/exec/memory.h +14 -0
index 6ed593c..2e5fd11 100644
@@ -860,6 +860,20 @@ void address_space_write(AddressSpace *as, hwaddr addr,
  */
 void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
 
+/* address_space_valid: check for validity of an address space range
+ *
+ * Check whether memory is assigned to the given address space range.
+ *
+ * For now, addr and len should be aligned to a page size.  This limitation
+ * will be lifted in the future.
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @len: length of the area to be checked
+ * @is_write: indicates the transfer direction
+ */
+bool address_space_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
+
 /* 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.
include/sysemu/dma.h +2 -1
index a52c93a..2e239dc 100644
@@ -113,7 +113,8 @@ static inline bool dma_memory_valid(DMAContext *dma,
                                     DMADirection dir)
 {
     if (!dma_has_iommu(dma)) {
-        return true;
+        return address_space_valid(dma->as, addr, len,
+                                   dir == DMA_DIRECTION_FROM_DEVICE);
     } else {
         return iommu_dma_memory_valid(dma, addr, len, dir);
     }
Signature · 1 line
1.8.1.4
#32 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 3 lines · Paolo Bonzini

The old-style IOMMU lets you check whether an access is valid in a given DMAContext. There is no equivalent for AddressSpace in the memory API, implement it with a lookup of the dispatch tree.

I don't love the name - "address_space_valid" suggests to me it tests the validity of the whole address space, not a specific range. But an obviously better name doesn't quickly occur to me.

Obviously I like the functionality, since I wrote that into the DMAContext stuff specifically to support the spapr_llan driver :).

Quoted 100 lines
---
 dma-helpers.c         |  5 +++++
 exec.c                | 25 +++++++++++++++++++++++++
 include/exec/memory.h | 14 ++++++++++++++
 include/sysemu/dma.h  |  3 ++-
 4 files changed, 46 insertions(+), 1 deletion(-)
dma-helpers.c +5 -0
index 272632f..2962b69 100644
@@ -298,6 +298,11 @@ bool iommu_dma_memory_valid(DMAContext *dma, dma_addr_t addr, dma_addr_t len,
             plen = len;
         }
 
+        if (!address_space_valid(dma->as, paddr, len,
+                                 dir == DMA_DIRECTION_FROM_DEVICE)) {
+            return false;
+        }
+
         len -= plen;
         addr += plen;
     }
exec.c +25 -0
index 8d91221..8f1b507 100644
@@ -2079,6 +2079,31 @@ static void cpu_notify_map_clients(void)
     }
 }
 
+bool address_space_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
+{
+    AddressSpaceDispatch *d = as->dispatch;
+    MemoryRegionSection *section;
+    int l;
+    hwaddr page;
+
+    while (len > 0) {
+        page = addr & TARGET_PAGE_MASK;
+        l = (page + TARGET_PAGE_SIZE) - addr;
+        if (l > len) {
+            l = len;
+        }
+        section = phys_page_find(d, addr >> TARGET_PAGE_BITS);
+        if (section->mr == &io_mem_unassigned ||
+            (is_write && section->mr->readonly)) {
+            return false;
+        }
+
+        len -= l;
+        addr += l;
+    }
+    return true;
+}
+
 /* 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 if resources needed to perform the mapping are exhausted.
include/exec/memory.h +14 -0
index 6ed593c..2e5fd11 100644
@@ -860,6 +860,20 @@ void address_space_write(AddressSpace *as, hwaddr addr,
  */
 void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
 
+/* address_space_valid: check for validity of an address space range
+ *
+ * Check whether memory is assigned to the given address space range.
+ *
+ * For now, addr and len should be aligned to a page size.  This limitation
+ * will be lifted in the future.
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @len: length of the area to be checked
+ * @is_write: indicates the transfer direction
+ */
+bool address_space_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
+
 /* 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.
include/sysemu/dma.h +2 -1
index a52c93a..2e239dc 100644
@@ -113,7 +113,8 @@ static inline bool dma_memory_valid(DMAContext *dma,
                                     DMADirection dir)
 {
     if (!dma_has_iommu(dma)) {
-        return true;
+        return address_space_valid(dma->as, addr, len,
+                                   dir == DMA_DIRECTION_FROM_DEVICE);
     } else {
         return iommu_dma_memory_valid(dma, addr, len, dir);
     }
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#33 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 8 lines · David Gibson
Quoted 3 lines · Paolo Bonzini

The old-style IOMMU lets you check whether an access is valid in a given DMAContext. There is no equivalent for AddressSpace in the memory API, implement it with a lookup of the dispatch tree.

I don't love the name - "address_space_valid" suggests to me it tests the validity of the whole address space, not a specific range. But an obviously better name doesn't quickly occur to me.

Not sure ATM if someone else suggested this earlier already, but what about something like address_space_access_valid?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#34 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Il 23/05/2013 16:22, Jan Kiszka ha scritto:

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

The old-style IOMMU lets you check whether an access is valid in a given DMAContext. There is no equivalent for AddressSpace in the memory API, implement it with a lookup of the dispatch tree.

I don't love the name - "address_space_valid" suggests to me it tests the validity of the whole address space, not a specific range. But an obviously better name doesn't quickly occur to me.

Not sure ATM if someone else suggested this earlier already, but what about something like address_space_access_valid?

Sure.

Paolo

#35 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini
exec.c +6 -0
+bool address_space_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
+{
+    AddressSpaceDispatch *d = as->dispatch;
+    MemoryRegionSection *section;
+    int l;
+    hwaddr page;

exec.c static void cpu_notify_map_clients(void) · line 2097

Quoted 10 lines
exec.c +10 -0
+
+    while (len > 0) {
+        page = addr & TARGET_PAGE_MASK;
+        l = (page + TARGET_PAGE_SIZE) - addr;
+        if (l > len) {
+            l = len;
+        }
+        section = phys_page_find(d, addr >> TARGET_PAGE_BITS);
+        if (section->mr == &io_mem_unassigned ||
+            (is_write && section->mr->readonly)) {

It's kind of bogus that io_mem_unassigned is the only MemoryRegion that can be unreadable. Why is 'readonly' a MemoryRegion attribute and 'writeonly' not? Shouldn't we be calling the MemoryRegionOps accepts() callback here? What about access alignment constraints and access size restrictions? What if the validity of the range changes between the time you asked and when you actually do the access?

The whole API is kind of unconvincing really, especially since the only thing we seem to use it for is some parameter checking in spapr_llan.c (via a huge pile of intermediate wrappers).

thanks
-- PMM

#36 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 26 lines · Peter Maydell
Quoted 6 lines · Paolo Bonzini
exec.c +6 -0
+bool address_space_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
+{
+    AddressSpaceDispatch *d = as->dispatch;
+    MemoryRegionSection *section;
+    int l;
+    hwaddr page;

exec.c static void cpu_notify_map_clients(void) · line 2097

Quoted 10 lines
exec.c +10 -0
+
+    while (len > 0) {
+        page = addr & TARGET_PAGE_MASK;
+        l = (page + TARGET_PAGE_SIZE) - addr;
+        if (l > len) {
+            l = len;
+        }
+        section = phys_page_find(d, addr >> TARGET_PAGE_BITS);
+        if (section->mr == &io_mem_unassigned ||
+            (is_write && section->mr->readonly)) {

It's kind of bogus that io_mem_unassigned is the only MemoryRegion that can be unreadable. Why is 'readonly' a MemoryRegion attribute and 'writeonly' not? Shouldn't we be calling the MemoryRegionOps accepts() callback here? What about access alignment constraints and access size restrictions? What if the validity of the range changes between the time you asked and when you actually do the access?

Quoted 3 lines

The whole API is kind of unconvincing really, especially since the only thing we seem to use it for is some parameter checking in spapr_llan.c (via a huge pile of intermediate wrappers).

I'll also have a use for it: replace isa_is_ioport_assigned.

Jan

#37 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 27 lines · Jan Kiszka
Quoted 26 lines · Peter Maydell
Quoted 6 lines · Paolo Bonzini
exec.c +6 -0
+bool address_space_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
+{
+    AddressSpaceDispatch *d = as->dispatch;
+    MemoryRegionSection *section;
+    int l;
+    hwaddr page;

exec.c static void cpu_notify_map_clients(void) · line 2097

Quoted 10 lines
exec.c +10 -0
+
+    while (len > 0) {
+        page = addr & TARGET_PAGE_MASK;
+        l = (page + TARGET_PAGE_SIZE) - addr;
+        if (l > len) {
+            l = len;
+        }
+        section = phys_page_find(d, addr >> TARGET_PAGE_BITS);
+        if (section->mr == &io_mem_unassigned ||
+            (is_write && section->mr->readonly)) {

It's kind of bogus that io_mem_unassigned is the only MemoryRegion that can be unreadable. Why is 'readonly' a MemoryRegion attribute and 'writeonly' not? Shouldn't we be calling the MemoryRegionOps accepts() callback here? What about access alignment constraints and access size restrictions? What if the validity of the range changes between the time you asked and when you actually do the access?

Quoted 5 lines
Quoted 3 lines

The whole API is kind of unconvincing really, especially since the only thing we seem to use it for is some parameter checking in spapr_llan.c (via a huge pile of intermediate wrappers).

I'll also have a use for it: replace isa_is_ioport_assigned.

But for this use case, something like memory_region_access_valid(MemoryRegion *, ...) would be more helpful because pci_address_space_io returns a memory region, not an address space. Can we change it? dma_memory_valid could simply pass the address space's root region.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#38 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

In reply to Jan Kiszka
Quoted 1 line · Jan Kiszka

I'll also have a use for it: replace isa_is_ioport_assigned.

That seems like it's something different: it's asking "has some other bit of QEMU registered a handler for this ioport?", not "at this moment in time if I make an I/O access will it succeed?" (which in principle should actually go all the way through and call a registered ioport handler which might then say "no, for this specific access we will fail").

thanks
-- PMM

#39 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 10 lines · Peter Maydell
Quoted 1 line · Jan Kiszka

I'll also have a use for it: replace isa_is_ioport_assigned.

That seems like it's something different: it's asking "has some other bit of QEMU registered a handler for this ioport?", not "at this moment in time if I make an I/O access will it succeed?" (which in principle should actually go all the way through and call a registered ioport handler which might then say "no, for this specific access we will fail").

Hmm... Paolo suggested to use it. But given the current arguments I guess it's better to stick with memory_region_find for that purpose.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#40 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

In reply to Peter Maydell

Il 23/05/2013 20:04, Peter Maydell ha scritto:

Quoted 3 lines

Shouldn't we be calling the MemoryRegionOps
accepts() callback here? What about access alignment constraints and access size restrictions?

Yes, we should.

Quoted 3 lines

What if the validity of the range
changes between the time you asked and when you actually do the access?

If that's a concern, you shouldn't use this API, you should just do the access and rely on the return value of address_space_rw & friends.

(Or, at least for the foreseeable future, do the checks under the BQL).

Paolo

#41 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 5 lines · Paolo Bonzini

Il 23/05/2013 20:04, Peter Maydell ha scritto:

Quoted 3 lines

Shouldn't we be calling the MemoryRegionOps
accepts() callback here? What about access alignment constraints and access size restrictions?

Quoted 8 lines

Yes, we should.

Quoted 3 lines

What if the validity of the range
changes between the time you asked and when you actually do the access?

If that's a concern, you shouldn't use this API, you should just do the access and rely on the return value of address_space_rw & friends.

So when *is* it a good idea to use this API? In real hardware you don't usually get a "tell me whether this access would succeed if I did it" bus operation -- you just do the operation and the memory transaction either succeeds or it doesn't. Are we modelling something that really exists in hardware on spapr here?

thanks
-- PMM

#42 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Il 24/05/2013 12:52, Peter Maydell ha scritto:

Quoted 15 lines · Paolo Bonzini
Quoted 5 lines

Il 23/05/2013 20:04, Peter Maydell ha scritto:

Quoted 3 lines

Shouldn't we be calling the MemoryRegionOps
accepts() callback here? What about access alignment constraints and access size restrictions?

Quoted 8 lines

Yes, we should.

Quoted 3 lines

What if the validity of the range
changes between the time you asked and when you actually do the access?

If that's a concern, you shouldn't use this API, you should just do the access and rely on the return value of address_space_rw & friends.

Quoted 6 lines

So when *is* it a good idea to use this API? In real hardware you don't usually get a "tell me whether this access would succeed if I did it" bus operation -- you just do the operation and the memory transaction either succeeds or it doesn't. Are we modelling something that really exists in hardware on spapr here?

Well, sPAPR is not hardware. :) It is a paravirtualized interface.

Anyhow, I can eliminate all the references to unassigned and basically do this:

bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)

{
    MemoryRegionSection *section;
    hwaddr l, xlat;

    while (len > 0) {
        l = len;
        section = address_space_translate(as, addr, &xlat, &l, is_write);
        if (!memory_access_is_direct(section->mr, is_write)) {
            l = memory_access_size(l, addr);
            if (!memory_region_access_valid(section->mr, addr1, l) {
                return false;
            }
        }

        len -= l;
        addr += l;
    }
    return true;
}

It requires some more changes however. I'll drop this patch. If it's okay for you, I'll send a pull request up to "memory: clean up phys_page_find" and go on with the next series.

Paolo

#43 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 3 lines · Paolo Bonzini

If it's okay
for you, I'll send a pull request up to "memory: clean up phys_page_find" and go on with the next series.

That's fine with me, but please don't forget to fix up the doc comment for memory_region_find() (see comments on patch 6/30). You can do that in a patch in the next series, though, I guess.

thanks
-- PMM

#44 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Il 24/05/2013 15:27, Peter Maydell ha scritto:

Quoted 9 lines · Paolo Bonzini
Quoted 3 lines

If it's okay
for you, I'll send a pull request up to "memory: clean up phys_page_find" and go on with the next series.

That's fine with me, but please don't forget to fix up the doc comment for memory_region_find() (see comments on patch 6/30). You can do that in a patch in the next series, though, I guess.

What about this:

/**
* memory_region_find: translate an address/size relative to a
* MemoryRegion into a #MemoryRegionSection.
*
* Locates the first #MemoryRegion within @mr that overlaps the range
* given by @addr and @size.
*
* Returns a #MemoryRegionSection that describes a contiguous overlap.
* It will have the following characteristics:
* .@size = 0 iff no overlap was found
* .@mr is non-%NULL iff an overlap was found
*
* Remember that in the return value the @offset_within_region is
* relative to the returned region (in the .@mr field), not to the
* @mr argument.
*
* Similarly, the .@offset_within_address_space is relative to the
* address space that contains both regions, the passed and the
* returned one. However, in the special case where the @mr argument
* has no parent (and thus is the root of the address space), the
* following will hold:
* .@offset_within_address_space >= @addr
* .@offset_within_address_space + .@size <= @addr + @size
*
* @mr: a MemoryRegion within which @addr is a relative address
* @addr: start of the area within @as to be searched
* @size: size of the area to be searched
*/

?
Paolo

#45 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 33 lines · Paolo Bonzini

Il 24/05/2013 15:27, Peter Maydell ha scritto:

Quoted 9 lines · Paolo Bonzini
Quoted 3 lines

If it's okay
for you, I'll send a pull request up to "memory: clean up phys_page_find" and go on with the next series.

That's fine with me, but please don't forget to fix up the doc comment for memory_region_find() (see comments on patch 6/30). You can do that in a patch in the next series, though, I guess.

What about this:

/**
* memory_region_find: translate an address/size relative to a
* MemoryRegion into a #MemoryRegionSection.
*
* Locates the first #MemoryRegion within @mr that overlaps the range
* given by @addr and @size.
*
* Returns a #MemoryRegionSection that describes a contiguous overlap.
* It will have the following characteristics:
* .@size = 0 iff no overlap was found
* .@mr is non-%NULL iff an overlap was found
*
* Remember that in the return value the @offset_within_region is
* relative to the returned region (in the .@mr field), not to the
* @mr argument.
*
* Similarly, the .@offset_within_address_space is relative to the
* address space that contains both regions, the passed and the
* returned one. However, in the special case where the @mr argument
* has no parent (and thus is the root of the address space), the

Quoted 8 lines

* following will hold:
* .@offset_within_address_space >= @addr
* .@offset_within_address_space + .@size <= @addr + @size
*
* @mr: a MemoryRegion within which @addr is a relative address
* @addr: start of the area within @as to be searched
* @size: size of the area to be searched
*/

Yes, that looks OK to me.

thanks
-- PMM

#46 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

In reply to Peter Maydell
Quoted 15 lines · Peter Maydell
Quoted 5 lines · Paolo Bonzini

Il 23/05/2013 20:04, Peter Maydell ha scritto:

Quoted 3 lines

Shouldn't we be calling the MemoryRegionOps
accepts() callback here? What about access alignment constraints and access size restrictions?

Quoted 8 lines

Yes, we should.

Quoted 3 lines

What if the validity of the range
changes between the time you asked and when you actually do the access?

If that's a concern, you shouldn't use this API, you should just do the access and rely on the return value of address_space_rw & friends.

Quoted 6 lines

So when *is* it a good idea to use this API? In real hardware you don't usually get a "tell me whether this access would succeed if I did it" bus operation -- you just do the operation and the memory transaction either succeeds or it doesn't. Are we modelling something that really exists in hardware on spapr here?

So, as a general rule, you should just attempt the access and handle failures - this is a bad interface. The reason I added it, however, is that the PAPR specification mandates that the virtual LAN pre-check various buffers when they're registered, and return specific errors if they're not mapped to valid memory. Since we have nothing to read or write at that point, adding this interface was the only way I could see to implement that requirement.

Or... a bit more charitably: You should always handle failures at the point of read or write, but using this interface can give you an earlier, and therefore potentially easier to analyze, error in the more common failure cases, even if there are more complex cases where the pre-check succeeds but the read/write still fails later.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#47 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 8 lines · David Gibson
Quoted 6 lines · Peter Maydell

So when *is* it a good idea to use this API? In real hardware you don't usually get a "tell me whether this access would succeed if I did it" bus operation -- you just do the operation and the memory transaction either succeeds or it doesn't. Are we modelling something that really exists in hardware on spapr here?

Quoted 7 lines

So, as a general rule, you should just attempt the access and handle failures - this is a bad interface. The reason I added it, however, is that the PAPR specification mandates that the virtual LAN pre-check various buffers when they're registered, and return specific errors if they're not mapped to valid memory. Since we have nothing to read or write at that point, adding this interface was the only way I could see to implement that requirement.

Would it work to just read and throw away the result of the read?

Quoted 5 lines

Or... a bit more charitably: You should always handle failures at the point of read or write, but using this interface can give you an earlier, and therefore potentially easier to analyze, error in the more common failure cases, even if there are more complex cases where the pre-check succeeds but the read/write still fails later.

There's also the converse case where the pre-check fails but doing the operation at the proper time would succeed, in which case where we're modelling real hardware we would be doing it wrong. So the set of cases where it's OK to pre-check seems a bit limited.

thanks
-- PMM

#48 lore raw

Re: [Qemu-devel] [PATCH 15/30] memory: add address_space_valid

Quoted 17 lines · Peter Maydell
Quoted 8 lines · David Gibson
Quoted 6 lines · Peter Maydell

So when *is* it a good idea to use this API? In real hardware you don't usually get a "tell me whether this access would succeed if I did it" bus operation -- you just do the operation and the memory transaction either succeeds or it doesn't. Are we modelling something that really exists in hardware on spapr here?

Quoted 7 lines

So, as a general rule, you should just attempt the access and handle failures - this is a bad interface. The reason I added it, however, is that the PAPR specification mandates that the virtual LAN pre-check various buffers when they're registered, and return specific errors if they're not mapped to valid memory. Since we have nothing to read or write at that point, adding this interface was the only way I could see to implement that requirement.

Quoted 1 line

Would it work to just read and throw away the result of the read?

Not when checking for writability.

Quoted 11 lines
Quoted 5 lines

Or... a bit more charitably: You should always handle failures at the point of read or write, but using this interface can give you an earlier, and therefore potentially easier to analyze, error in the more common failure cases, even if there are more complex cases where the pre-check succeeds but the read/write still fails later.

There's also the converse case where the pre-check fails but doing the operation at the proper time would succeed, in which case where we're modelling real hardware we would be doing it wrong. So the set of cases where it's OK to pre-check seems a bit limited.

Whether originally real or otherwise, this is a question of faithfully implementing what the hardware is supposed to do. In the case of the PAPR llan, the (virtual) hardware specification says that the buffer accessibility is checked at buffer add time, and the driver won't work if it only makes the buffers accessible between that point and the actual buffer access. It would be entirely possible to design real hardware with similar behaviour (probe buffer accessibility when a descriptor is added to a pool, say), although it's not the sort of thing hardware people generally do.

So, certainly qemu should not go using this pre-check when the hardware it's emulating does not do such a check. Designing hardware that does do a pre-check is not, IMO, a good idea on balance, but the point is that there is a reason (albeit, not a great one) you might want to design the (possibly virtual) hardware that way.

As an aside, when you consider devices with embedded firmware - which is practically everything these days - the distinction between "real" hardware and virtual hardware can get kind of blurry.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#49 lore raw

PATCH 16/30 memory: clean up phys_page_find

In reply to Paolo Bonzini

Remove the goto.

---
 exec.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
exec.c +2 -6
index 8f1b507..82da067 100644
@@ -187,19 +187,15 @@ MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
     PhysPageEntry lp = d->phys_map;
     PhysPageEntry *p;
     int i;
-    uint16_t s_index = phys_section_unassigned;
 
     for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) {
         if (lp.ptr == PHYS_MAP_NODE_NIL) {
-            goto not_found;
+            return &phys_sections[phys_section_unassigned];
         }
         p = phys_map_nodes[lp.ptr];
         lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
     }
-
-    s_index = lp.ptr;
-not_found:
-    return &phys_sections[s_index];
+    return &phys_sections[lp.ptr];
 }
 
 bool memory_region_is_unassigned(MemoryRegion *mr)
Signature · 1 line
1.8.1.4
#51 lore raw

PATCH 17/30 memory: add address_space_translate

In reply to Paolo Bonzini

Using phys_page_find to translate an AddressSpace to a MemoryRegionSection is unwieldy. It requires to pass the page index rather than the address, and later memory_region_section_addr has to be called. Replace memory_region_section_addr with a function that does all of it: call phys_page_find, compute the offset within the region, and check how big the current mapping is. This way, a large flat region can be written with a single lookup rather than a page at a time.

address_space_translate will also provide a single point where IOMMU forwarding is implemented.

---
 cputlb.c              |  20 ++---
 exec.c                | 201 +++++++++++++++++++++++++++-----------------------
 include/exec/cputlb.h |  12 ++-
 include/exec/memory.h |  31 ++++----
 translate-all.c       |   6 +-
 5 files changed, 143 insertions(+), 127 deletions(-)
cputlb.c +11 -9
index aba7e44..1f85da0 100644
@@ -248,13 +248,18 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
     target_ulong code_address;
     uintptr_t addend;
     CPUTLBEntry *te;
-    hwaddr iotlb;
+    hwaddr iotlb, xlat, sz;
 
     assert(size >= TARGET_PAGE_SIZE);
     if (size != TARGET_PAGE_SIZE) {
         tlb_add_large_page(env, vaddr, size);
     }
-    section = phys_page_find(address_space_memory.dispatch, paddr >> TARGET_PAGE_BITS);
+
+    sz = size;
+    section = address_space_translate(&address_space_memory, paddr, &xlat, &sz,
+                                      false);
+    assert(sz >= TARGET_PAGE_SIZE);
+
 #if defined(DEBUG_TLB)
     printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
            " prot=%x idx=%d pd=0x%08lx\n",
@@ -269,15 +274,14 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
     }
     if (memory_region_is_ram(section->mr) ||
         memory_region_is_romd(section->mr)) {
-        addend = (uintptr_t)memory_region_get_ram_ptr(section->mr)
-        + memory_region_section_addr(section, paddr);
+        addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
     } else {
         addend = 0;
     }
 
     code_address = address;
-    iotlb = memory_region_section_get_iotlb(env, section, vaddr, paddr, prot,
-                                            &address);
+    iotlb = memory_region_section_get_iotlb(env, section, vaddr, paddr, xlat,
+                                            prot, &address);
 
     index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     env->iotlb[mmu_idx][index] = iotlb - vaddr;
@@ -300,9 +304,7 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
             /* Write access calls the I/O callback.  */
             te->addr_write = address | TLB_MMIO;
         } else if (memory_region_is_ram(section->mr)
-                   && !cpu_physical_memory_is_dirty(
-                           section->mr->ram_addr
-                           + memory_region_section_addr(section, paddr))) {
+                   && !cpu_physical_memory_is_dirty(section->mr->ram_addr + xlat)) {
             te->addr_write = address | TLB_NOTDIRTY;
         } else {
             te->addr_write = address;
exec.c +110 -91

417 lines not on this page · raw message

include/exec/cputlb.h +5 -7
index 733c885..e821660 100644
@@ -26,8 +26,6 @@ void tlb_unprotect_code_phys(CPUArchState *env, ram_addr_t ram_addr,
                              target_ulong vaddr);
 void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, uintptr_t start,
                            uintptr_t length);
-MemoryRegionSection *phys_page_find(struct AddressSpaceDispatch *d,
-                                    hwaddr index);
 void cpu_tlb_reset_dirty_all(ram_addr_t start1, ram_addr_t length);
 void tlb_set_dirty(CPUArchState *env, target_ulong vaddr);
 extern int tlb_flush_count;
@@ -35,11 +33,11 @@ extern int tlb_flush_count;
 /* exec.c */
 void tb_flush_jmp_cache(CPUArchState *env, target_ulong addr);
 hwaddr memory_region_section_get_iotlb(CPUArchState *env,
-                                                   MemoryRegionSection *section,
-                                                   target_ulong vaddr,
-                                                   hwaddr paddr,
-                                                   int prot,
-                                                   target_ulong *address);
+                                       MemoryRegionSection *section,
+                                       target_ulong vaddr,
+                                       hwaddr paddr, hwaddr xlat,
+                                       int prot,
+                                       target_ulong *address);
 bool memory_region_is_unassigned(MemoryRegion *mr);
 
 #endif
include/exec/memory.h +14 -17
index 2e5fd11..809a958 100644
@@ -743,23 +743,6 @@ MemoryRegionSection memory_region_find(MemoryRegion *mr,
                                        hwaddr addr, uint64_t size);
 
 /**
- * memory_region_section_addr: get offset within MemoryRegionSection
- *
- * Returns offset within MemoryRegionSection
- *
- * @section: the memory region section being queried
- * @addr: address in address space
- */
-static inline hwaddr
-memory_region_section_addr(MemoryRegionSection *section,
-                           hwaddr addr)
-{
-    addr -= section->offset_within_address_space;
-    addr += section->offset_within_region;
-    return addr;
-}
-
-/**
  * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
  *
  * Synchronizes the dirty page log for an entire address space.
@@ -860,6 +843,20 @@ void address_space_write(AddressSpace *as, hwaddr addr,
  */
 void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
 
+/* address_space_translate: translate an address range into an address space
+ * into a MemoryRegionSection and an address range into that section
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @xlat: pointer to address within the returned memory region section's
+ * #MemoryRegion.
+ * @len: pointer to length
+ * @is_write: indicates the transfer direction
+ */
+MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
+                                             hwaddr *xlat, hwaddr *len,
+                                             bool is_write);
+
 /* address_space_valid: check for validity of an address space range
  *
  * Check whether memory is assigned to the given address space range.
translate-all.c +3 -3
index 0d84b0d..7a7d537 100644
@@ -1355,15 +1355,15 @@ void tb_invalidate_phys_addr(hwaddr addr)
 {
     ram_addr_t ram_addr;
     MemoryRegionSection *section;
+    hwaddr l = 1;
 
-    section = phys_page_find(address_space_memory.dispatch,
-                             addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory, addr, &addr, &l, false);
     if (!(memory_region_is_ram(section->mr)
           || memory_region_is_romd(section->mr))) {
         return;
     }
     ram_addr = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
-        + memory_region_section_addr(section, addr);
+        + addr;
     tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
 }
 #endif /* TARGET_HAS_ICE && !defined(CONFIG_USER_ONLY) */
Signature · 1 line
1.8.1.4
#52 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 158 lines · Paolo Bonzini

Using phys_page_find to translate an AddressSpace to a MemoryRegionSection is unwieldy. It requires to pass the page index rather than the address, and later memory_region_section_addr has to be called. Replace memory_region_section_addr with a function that does all of it: call phys_page_find, compute the offset within the region, and check how big the current mapping is. This way, a large flat region can be written with a single lookup rather than a page at a time.

address_space_translate will also provide a single point where IOMMU forwarding is implemented.

---
 cputlb.c              |  20 ++---
 exec.c                | 201 +++++++++++++++++++++++++++-----------------------
 include/exec/cputlb.h |  12 ++-
 include/exec/memory.h |  31 ++++----
 translate-all.c       |   6 +-
 5 files changed, 143 insertions(+), 127 deletions(-)
cputlb.c +11 -9
index aba7e44..1f85da0 100644
@@ -248,13 +248,18 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
     target_ulong code_address;
     uintptr_t addend;
     CPUTLBEntry *te;
-    hwaddr iotlb;
+    hwaddr iotlb, xlat, sz;
 
     assert(size >= TARGET_PAGE_SIZE);
     if (size != TARGET_PAGE_SIZE) {
         tlb_add_large_page(env, vaddr, size);
     }
-    section = phys_page_find(address_space_memory.dispatch, paddr >> TARGET_PAGE_BITS);
+
+    sz = size;
+    section = address_space_translate(&address_space_memory, paddr, &xlat, &sz,
+                                      false);
+    assert(sz >= TARGET_PAGE_SIZE);
+
 #if defined(DEBUG_TLB)
     printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
            " prot=%x idx=%d pd=0x%08lx\n",
@@ -269,15 +274,14 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
     }
     if (memory_region_is_ram(section->mr) ||
         memory_region_is_romd(section->mr)) {
-        addend = (uintptr_t)memory_region_get_ram_ptr(section->mr)
-        + memory_region_section_addr(section, paddr);
+        addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
     } else {
         addend = 0;
     }
 
     code_address = address;
-    iotlb = memory_region_section_get_iotlb(env, section, vaddr, paddr, prot,
-                                            &address);
+    iotlb = memory_region_section_get_iotlb(env, section, vaddr, paddr, xlat,
+                                            prot, &address);
 
     index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     env->iotlb[mmu_idx][index] = iotlb - vaddr;
@@ -300,9 +304,7 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
             /* Write access calls the I/O callback.  */
             te->addr_write = address | TLB_MMIO;
         } else if (memory_region_is_ram(section->mr)
-                   && !cpu_physical_memory_is_dirty(
-                           section->mr->ram_addr
-                           + memory_region_section_addr(section, paddr))) {
+                   && !cpu_physical_memory_is_dirty(section->mr->ram_addr + xlat)) {
             te->addr_write = address | TLB_NOTDIRTY;
         } else {
             te->addr_write = address;
exec.c +26 -11
index 82da067..e5ee8ff 100644
@@ -182,7 +182,7 @@ static void phys_page_set(AddressSpaceDispatch *d,
     phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
 }
 
-MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
+static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
 {
     PhysPageEntry lp = d->phys_map;
     PhysPageEntry *p;
@@ -198,6 +198,22 @@ MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
     return &phys_sections[lp.ptr];
 }
 
+MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
+                                             hwaddr *xlat, hwaddr *plen,
+                                             bool is_write)
+{
+    MemoryRegionSection *section;
+
+    section = phys_page_find(as->dispatch, addr >> TARGET_PAGE_BITS);
+    /* Compute offset within MemoryRegionSection */
+    addr -= section->offset_within_address_space;
+    *plen = MIN(section->size - addr, *plen);
+
+    /* Compute offset within MemoryRegion */
+    *xlat = addr + section->offset_within_region;
+    return section;
+}
+
 bool memory_region_is_unassigned(MemoryRegion *mr)
 {
     return mr != &io_mem_ram && mr != &io_mem_rom
@@ -616,11 +632,11 @@ static int cpu_physical_memory_set_dirty_tracking(int enable)
 }
 
 hwaddr memory_region_section_get_iotlb(CPUArchState *env,
-                                                   MemoryRegionSection *section,
-                                                   target_ulong vaddr,
-                                                   hwaddr paddr,
-                                                   int prot,
-                                                   target_ulong *address)
+                                       MemoryRegionSection *section,
+                                       target_ulong vaddr,
+                                       hwaddr paddr, hwaddr xlat,
+                                       int prot,
+                                       target_ulong *address)
 {
     hwaddr iotlb;
     CPUWatchpoint *wp;
@@ -628,7 +644,7 @@ hwaddr memory_region_section_get_iotlb(CPUArchState *env,
     if (memory_region_is_ram(section->mr)) {
         /* Normal RAM.  */
         iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
-            + memory_region_section_addr(section, paddr);
+            + xlat;
         if (!section->readonly) {
             iotlb |= phys_section_notdirty;
         } else {
@@ -636,7 +652,7 @@ hwaddr memory_region_section_get_iotlb(CPUArchState *env,
         }
     } else {
         iotlb = section - phys_sections;
-        iotlb += memory_region_section_addr(section, paddr);
+        iotlb += xlat;
     }
 
     /* Make accesses to pages with watchpoints go via the
@@ -1888,24 +1904,18 @@ static void invalidate_and_set_dirty(hwaddr addr,
 void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
                       int len, bool is_write)
 {
-    AddressSpaceDispatch *d = as->dispatch;
-    int l;
+    hwaddr l;
     uint8_t *ptr;
     uint32_t val;
-    hwaddr page;
+    hwaddr addr1;
     MemoryRegionSection *section;

exec.c static void invalidate_and_set_dirty(hwaddr addr, · line 1916

Quoted 10 lines
exec.c +2 -5
 
     while (len > 0) {
-        page = addr & TARGET_PAGE_MASK;
-        l = (page + TARGET_PAGE_SIZE) - addr;
-        if (l > len)
-            l = len;
-        section = phys_page_find(d, page >> TARGET_PAGE_BITS);
+        l = len;
+        section = address_space_translate(as, addr, &addr1, &l, is_write);
 

Jan's "Rework sub-page handling" patch broke the tlb_set_page (anything else?) But I think we still need a function based on address_space_translate to wrap the subpage logic inside and ease the incoming RCU style.

Regards,
Pingfan

Quoted 459 lines
exec.c +82 -75
         if (is_write) {
             if (!memory_region_is_ram(section->mr)) {
-                hwaddr addr1;
-                addr1 = memory_region_section_addr(section, addr);
                 /* XXX: could force cpu_single_env to NULL to avoid
                    potential bugs */
                 if (l >= 4 && ((addr1 & 3) == 0)) {
@@ -1925,9 +1935,7 @@ void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
                     l = 1;
                 }
             } else if (!section->readonly) {
-                ram_addr_t addr1;
-                addr1 = memory_region_get_ram_addr(section->mr)
-                    + memory_region_section_addr(section, addr);
+                addr1 += memory_region_get_ram_addr(section->mr);
                 /* RAM case */
                 ptr = qemu_get_ram_ptr(addr1);
                 memcpy(ptr, buf, l);
@@ -1936,9 +1944,7 @@ void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
         } else {
             if (!(memory_region_is_ram(section->mr) ||
                   memory_region_is_romd(section->mr))) {
-                hwaddr addr1;
                 /* I/O case */
-                addr1 = memory_region_section_addr(section, addr);
                 if (l >= 4 && ((addr1 & 3) == 0)) {
                     /* 32 bit read access */
                     val = io_mem_read(section->mr, addr1, 4);
@@ -1957,9 +1963,7 @@ void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
                 }
             } else {
                 /* RAM case */
-                ptr = qemu_get_ram_ptr(section->mr->ram_addr
-                                       + memory_region_section_addr(section,
-                                                                    addr));
+                ptr = qemu_get_ram_ptr(section->mr->ram_addr + addr1);
                 memcpy(buf, ptr, l);
             }
         }
@@ -1998,26 +2002,21 @@ void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
 void cpu_physical_memory_write_rom(hwaddr addr,
                                    const uint8_t *buf, int len)
 {
-    AddressSpaceDispatch *d = address_space_memory.dispatch;
-    int l;
+    hwaddr l;
     uint8_t *ptr;
-    hwaddr page;
+    hwaddr addr1;
     MemoryRegionSection *section;
 
     while (len > 0) {
-        page = addr & TARGET_PAGE_MASK;
-        l = (page + TARGET_PAGE_SIZE) - addr;
-        if (l > len)
-            l = len;
-        section = phys_page_find(d, page >> TARGET_PAGE_BITS);
+        l = len;
+        section = address_space_translate(&address_space_memory,
+                                          addr, &addr1, &l, true);
 
         if (!(memory_region_is_ram(section->mr) ||
               memory_region_is_romd(section->mr))) {
             /* do nothing */
         } else {
-            unsigned long addr1;
-            addr1 = memory_region_get_ram_addr(section->mr)
-                + memory_region_section_addr(section, addr);
+            addr1 += memory_region_get_ram_addr(section->mr);
             /* ROM/RAM case */
             ptr = qemu_get_ram_ptr(addr1);
             memcpy(ptr, buf, l);
@@ -2077,18 +2076,12 @@ static void cpu_notify_map_clients(void)
 
 bool address_space_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
 {
-    AddressSpaceDispatch *d = as->dispatch;
     MemoryRegionSection *section;
-    int l;
-    hwaddr page;
+    hwaddr l, xlat;
 
     while (len > 0) {
-        page = addr & TARGET_PAGE_MASK;
-        l = (page + TARGET_PAGE_SIZE) - addr;
-        if (l > len) {
-            l = len;
-        }
-        section = phys_page_find(d, addr >> TARGET_PAGE_BITS);
+        l = len;
+        section = address_space_translate(as, addr, &xlat, &l, is_write);
         if (section->mr == &io_mem_unassigned ||
             (is_write && section->mr->readonly)) {
             return false;
@@ -2112,22 +2105,17 @@ void *address_space_map(AddressSpace *as,
                         hwaddr *plen,
                         bool is_write)
 {
-    AddressSpaceDispatch *d = as->dispatch;
     hwaddr len = *plen;
     hwaddr todo = 0;
-    int l;
-    hwaddr page;
+    hwaddr l, xlat;
     MemoryRegionSection *section;
     ram_addr_t raddr = RAM_ADDR_MAX;
     ram_addr_t rlen;
     void *ret;
 
     while (len > 0) {
-        page = addr & TARGET_PAGE_MASK;
-        l = (page + TARGET_PAGE_SIZE) - addr;
-        if (l > len)
-            l = len;
-        section = phys_page_find(d, page >> TARGET_PAGE_BITS);
+        l = len;
+        section = address_space_translate(as, addr, &xlat, &l, is_write);
 
         if (!(memory_region_is_ram(section->mr) && !section->readonly)) {
             if (todo || bounce.buffer) {
@@ -2144,8 +2132,11 @@ void *address_space_map(AddressSpace *as,
             return bounce.buffer;
         }
         if (!todo) {
-            raddr = memory_region_get_ram_addr(section->mr)
-                + memory_region_section_addr(section, addr);
+            raddr = memory_region_get_ram_addr(section->mr) + xlat;
+        } else {
+            if (memory_region_get_ram_addr(section->mr) + xlat != raddr + todo) {
+                break;
+            }
         }
 
         len -= l;
@@ -2211,14 +2202,19 @@ static inline uint32_t ldl_phys_internal(hwaddr addr,
     uint8_t *ptr;
     uint32_t val;
     MemoryRegionSection *section;
+    hwaddr l = 4;
+    hwaddr addr1;
 
-    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
+                                      false);
+    if (l < 4) {
+        return unassigned_mem_read(NULL, addr, 4);
+    }
 
     if (!(memory_region_is_ram(section->mr) ||
           memory_region_is_romd(section->mr))) {
         /* I/O case */
-        addr = memory_region_section_addr(section, addr);
-        val = io_mem_read(section->mr, addr, 4);
+        val = io_mem_read(section->mr, addr1, 4);
 #if defined(TARGET_WORDS_BIGENDIAN)
         if (endian == DEVICE_LITTLE_ENDIAN) {
             val = bswap32(val);
@@ -2232,7 +2228,7 @@ static inline uint32_t ldl_phys_internal(hwaddr addr,
         /* RAM case */
         ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
                                 & TARGET_PAGE_MASK)
-                               + memory_region_section_addr(section, addr));
+                               + addr1);
         switch (endian) {
         case DEVICE_LITTLE_ENDIAN:
             val = ldl_le_p(ptr);
@@ -2270,28 +2266,33 @@ static inline uint64_t ldq_phys_internal(hwaddr addr,
     uint8_t *ptr;
     uint64_t val;
     MemoryRegionSection *section;
+    hwaddr l = 8;
+    hwaddr addr1;
 
-    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
+                                      false);
+    if (l < 8) {
+        return unassigned_mem_read(NULL, addr, 8);
+    }
 
     if (!(memory_region_is_ram(section->mr) ||
           memory_region_is_romd(section->mr))) {
         /* I/O case */
-        addr = memory_region_section_addr(section, addr);
 
         /* XXX This is broken when device endian != cpu endian.
                Fix and add "endian" variable check */
 #ifdef TARGET_WORDS_BIGENDIAN
-        val = io_mem_read(section->mr, addr, 4) << 32;
-        val |= io_mem_read(section->mr, addr + 4, 4);
+        val = io_mem_read(section->mr, addr1, 4) << 32;
+        val |= io_mem_read(section->mr, addr1 + 4, 4);
 #else
-        val = io_mem_read(section->mr, addr, 4);
-        val |= io_mem_read(section->mr, addr + 4, 4) << 32;
+        val = io_mem_read(section->mr, addr1, 4);
+        val |= io_mem_read(section->mr, addr1 + 4, 4) << 32;
 #endif
     } else {
         /* RAM case */
         ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
                                 & TARGET_PAGE_MASK)
-                               + memory_region_section_addr(section, addr));
+                               + addr1);
         switch (endian) {
         case DEVICE_LITTLE_ENDIAN:
             val = ldq_le_p(ptr);
@@ -2337,14 +2338,19 @@ static inline uint32_t lduw_phys_internal(hwaddr addr,
     uint8_t *ptr;
     uint64_t val;
     MemoryRegionSection *section;
+    hwaddr l = 2;
+    hwaddr addr1;
 
-    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
+                                      false);
+    if (l < 2) {
+        return unassigned_mem_read(NULL, addr, 2);
+    }
 
     if (!(memory_region_is_ram(section->mr) ||
           memory_region_is_romd(section->mr))) {
         /* I/O case */
-        addr = memory_region_section_addr(section, addr);
-        val = io_mem_read(section->mr, addr, 2);
+        val = io_mem_read(section->mr, addr1, 2);
 #if defined(TARGET_WORDS_BIGENDIAN)
         if (endian == DEVICE_LITTLE_ENDIAN) {
             val = bswap16(val);
@@ -2358,7 +2364,7 @@ static inline uint32_t lduw_phys_internal(hwaddr addr,
         /* RAM case */
         ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
                                 & TARGET_PAGE_MASK)
-                               + memory_region_section_addr(section, addr));
+                               + addr1);
         switch (endian) {
         case DEVICE_LITTLE_ENDIAN:
             val = lduw_le_p(ptr);
@@ -2396,19 +2402,23 @@ void stl_phys_notdirty(hwaddr addr, uint32_t val)
 {
     uint8_t *ptr;
     MemoryRegionSection *section;
+    hwaddr l = 4;
+    hwaddr addr1;
 
-    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
+                                      true);
+    if (l < 4) {
+        unassigned_mem_write(NULL, addr, val, 4);
+        return;
+    }
 
     if (!memory_region_is_ram(section->mr) || section->readonly) {
-        addr = memory_region_section_addr(section, addr);
         if (memory_region_is_ram(section->mr)) {
             section = &phys_sections[phys_section_rom];
         }
-        io_mem_write(section->mr, addr, val, 4);
+        io_mem_write(section->mr, addr1, val, 4);
     } else {
-        unsigned long addr1 = (memory_region_get_ram_addr(section->mr)
-                               & TARGET_PAGE_MASK)
-            + memory_region_section_addr(section, addr);
+        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
         ptr = qemu_get_ram_ptr(addr1);
         stl_p(ptr, val);
 
@@ -2430,11 +2440,17 @@ static inline void stl_phys_internal(hwaddr addr, uint32_t val,
 {
     uint8_t *ptr;
     MemoryRegionSection *section;
+    hwaddr l = 4;
+    hwaddr addr1;
 
-    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
+                                      true);
+    if (l < 4) {
+        unassigned_mem_write(NULL, addr, val, 4);
+        return;
+    }
 
     if (!memory_region_is_ram(section->mr) || section->readonly) {
-        addr = memory_region_section_addr(section, addr);
         if (memory_region_is_ram(section->mr)) {
             section = &phys_sections[phys_section_rom];
         }
@@ -2447,12 +2463,10 @@ static inline void stl_phys_internal(hwaddr addr, uint32_t val,
             val = bswap32(val);
         }
 #endif
-        io_mem_write(section->mr, addr, val, 4);
+        io_mem_write(section->mr, addr1, val, 4);
     } else {
-        unsigned long addr1;
-        addr1 = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
-            + memory_region_section_addr(section, addr);
         /* RAM case */
+        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
         ptr = qemu_get_ram_ptr(addr1);
         switch (endian) {
         case DEVICE_LITTLE_ENDIAN:
@@ -2497,11 +2511,17 @@ static inline void stw_phys_internal(hwaddr addr, uint32_t val,
 {
     uint8_t *ptr;
     MemoryRegionSection *section;
+    hwaddr l = 2;
+    hwaddr addr1;
 
-    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
+                                      true);
+    if (l < 2) {
+        unassigned_mem_write(NULL, addr, val, 2);
+        return;
+    }
 
     if (!memory_region_is_ram(section->mr) || section->readonly) {
-        addr = memory_region_section_addr(section, addr);
         if (memory_region_is_ram(section->mr)) {
             section = &phys_sections[phys_section_rom];
         }
@@ -2514,12 +2534,10 @@ static inline void stw_phys_internal(hwaddr addr, uint32_t val,
             val = bswap16(val);
         }
 #endif
-        io_mem_write(section->mr, addr, val, 2);
+        io_mem_write(section->mr, addr1, val, 2);
     } else {
-        unsigned long addr1;
-        addr1 = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
-            + memory_region_section_addr(section, addr);
         /* RAM case */
+        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
         ptr = qemu_get_ram_ptr(addr1);
         switch (endian) {
         case DEVICE_LITTLE_ENDIAN:
@@ -2622,9 +2640,10 @@ bool virtio_is_big_endian(void)
 bool cpu_physical_memory_is_io(hwaddr phys_addr)
 {
     MemoryRegionSection *section;
+    hwaddr l = 1;
 
-    section = phys_page_find(address_space_memory.dispatch,
-                             phys_addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory,
+                                      phys_addr, &phys_addr, &l, false);
 
     return !(memory_region_is_ram(section->mr) ||
              memory_region_is_romd(section->mr));
include/exec/cputlb.h +5 -7
index 733c885..e821660 100644
@@ -26,8 +26,6 @@ void tlb_unprotect_code_phys(CPUArchState *env, ram_addr_t ram_addr,
                              target_ulong vaddr);
 void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, uintptr_t start,
                            uintptr_t length);
-MemoryRegionSection *phys_page_find(struct AddressSpaceDispatch *d,
-                                    hwaddr index);
 void cpu_tlb_reset_dirty_all(ram_addr_t start1, ram_addr_t length);
 void tlb_set_dirty(CPUArchState *env, target_ulong vaddr);
 extern int tlb_flush_count;
@@ -35,11 +33,11 @@ extern int tlb_flush_count;
 /* exec.c */
 void tb_flush_jmp_cache(CPUArchState *env, target_ulong addr);
 hwaddr memory_region_section_get_iotlb(CPUArchState *env,
-                                                   MemoryRegionSection *section,
-                                                   target_ulong vaddr,
-                                                   hwaddr paddr,
-                                                   int prot,
-                                                   target_ulong *address);
+                                       MemoryRegionSection *section,
+                                       target_ulong vaddr,
+                                       hwaddr paddr, hwaddr xlat,
+                                       int prot,
+                                       target_ulong *address);
 bool memory_region_is_unassigned(MemoryRegion *mr);
 
 #endif
include/exec/memory.h +14 -17
index 2e5fd11..809a958 100644
@@ -743,23 +743,6 @@ MemoryRegionSection memory_region_find(MemoryRegion *mr,
                                        hwaddr addr, uint64_t size);
 
 /**
- * memory_region_section_addr: get offset within MemoryRegionSection
- *
- * Returns offset within MemoryRegionSection
- *
- * @section: the memory region section being queried
- * @addr: address in address space
- */
-static inline hwaddr
-memory_region_section_addr(MemoryRegionSection *section,
-                           hwaddr addr)
-{
-    addr -= section->offset_within_address_space;
-    addr += section->offset_within_region;
-    return addr;
-}
-
-/**
  * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
  *
  * Synchronizes the dirty page log for an entire address space.
@@ -860,6 +843,20 @@ void address_space_write(AddressSpace *as, hwaddr addr,
  */
 void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
 
+/* address_space_translate: translate an address range into an address space
+ * into a MemoryRegionSection and an address range into that section
+ *
+ * @as: #AddressSpace to be accessed
+ * @addr: address within that address space
+ * @xlat: pointer to address within the returned memory region section's
+ * #MemoryRegion.
+ * @len: pointer to length
+ * @is_write: indicates the transfer direction
+ */
+MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
+                                             hwaddr *xlat, hwaddr *len,
+                                             bool is_write);
+
 /* address_space_valid: check for validity of an address space range
  *
  * Check whether memory is assigned to the given address space range.
translate-all.c +3 -4
index 0d84b0d..7a7d537 100644
@@ -1355,15 +1355,15 @@ void tb_invalidate_phys_addr(hwaddr addr)
 {
     ram_addr_t ram_addr;
     MemoryRegionSection *section;
+    hwaddr l = 1;
 
-    section = phys_page_find(address_space_memory.dispatch,
-                             addr >> TARGET_PAGE_BITS);
+    section = address_space_translate(&address_space_memory, addr, &addr, &l, false);
     if (!(memory_region_is_ram(section->mr)
           || memory_region_is_romd(section->mr))) {
         return;
     }
     ram_addr = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
-        + memory_region_section_addr(section, addr);
+        + addr;
     tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
 }
 #endif /* TARGET_HAS_ICE && !defined(CONFIG_USER_ONLY) */
--

1.8.1.4

#53 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Il 23/05/2013 09:09, liu ping fan ha scritto:

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines

void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
int len, bool is_write)
{

exec.c +2 -3
-    AddressSpaceDispatch *d = as->dispatch;
-    int l;
+    hwaddr l;
     uint8_t *ptr;
     uint32_t val;
-    hwaddr page;
+    hwaddr addr1;
     MemoryRegionSection *section;
Quoted 9 lines

exec.c static void invalidate_and_set_dirty(hwaddr addr, · line 1915

Quoted 9 lines
     while (len > 0) {
exec.c +2 -5
-        page = addr & TARGET_PAGE_MASK;
-        l = (page + TARGET_PAGE_SIZE) - addr;
-        if (l > len)
-            l = len;
-        section = phys_page_find(d, page >> TARGET_PAGE_BITS);
+        l = len;
+        section = address_space_translate(as, addr, &addr1, &l, is_write);
Quoted 4 lines

Jan's "Rework sub-page handling" patch broke the tlb_set_page (anything else?) But I think we still need a function based on address_space_translate to wrap the subpage logic inside and ease the incoming RCU style.

The idea is that address_space_translate gets a ref to the MemoryRegion, and the ref is then released by the caller of address_space_translate.

That means that the actual memory accesses can run outside the RCU critical section.

But I'm not sure how that is related to subpage logic. Subpages are simply MemoryRegions that only exist in the phys page map, rather than within an AddressSpace. Their destruction will be delayed anyway by doing call_rcu on the old phys page map.

Paolo

#54 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 22 lines · Paolo Bonzini

Il 23/05/2013 09:09, liu ping fan ha scritto:

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
int len, bool is_write)
{

exec.c +4 -8
-    AddressSpaceDispatch *d = as->dispatch;
-    int l;
+    hwaddr l;
     uint8_t *ptr;
     uint32_t val;
-    hwaddr page;
+    hwaddr addr1;
     MemoryRegionSection *section;
 
     while (len > 0) {
-        page = addr & TARGET_PAGE_MASK;
-        l = (page + TARGET_PAGE_SIZE) - addr;
-        if (l > len)
-            l = len;
-        section = phys_page_find(d, page >> TARGET_PAGE_BITS);
+        l = len;
+        section = address_space_translate(as, addr, &addr1, &l, is_write);
Quoted 8 lines
Quoted 4 lines

Jan's "Rework sub-page handling" patch broke the tlb_set_page (anything else?) But I think we still need a function based on address_space_translate to wrap the subpage logic inside and ease the incoming RCU style.

The idea is that address_space_translate gets a ref to the MemoryRegion, and the ref is then released by the caller of address_space_translate.

This will require subpage hold a reference to real mr. But I think it is needless. Maybe when later in your these series, we will see how to resolve it. BTW, do you target URCU patches to 1.6

Regards,
Pingfan

Quoted 9 lines

That means that the actual memory accesses can run outside the RCU critical section.

But I'm not sure how that is related to subpage logic. Subpages are simply MemoryRegions that only exist in the phys page map, rather than within an AddressSpace. Their destruction will be delayed anyway by doing call_rcu on the old phys page map.

Paolo

#55 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Il 23/05/2013 15:06, liu ping fan ha scritto:

Quoted 4 lines
Quoted 3 lines

The idea is that address_space_translate gets a ref to the MemoryRegion, and the ref is then released by the caller of address_space_translate.

This will require subpage hold a reference to real mr.

Hmm, that's right. Jan, that could be a concern when you update your PIO series.

Quoted 3 lines

But I think it
is needless. Maybe when later in your these series, we will see how to resolve it. BTW, do you target URCU patches to 1.6

Yes.

Paolo

Reviewed-by on v1 17/30
#56 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

Using phys_page_find to translate an AddressSpace to a MemoryRegionSection is unwieldy. It requires to pass the page index rather than the address, and later memory_region_section_addr has to be called. Replace memory_region_section_addr with a function that does all of it: call phys_page_find, compute the offset within the region, and check how big the current mapping is. This way, a large flat region can be written with a single lookup rather than a page at a time.

Quoted 4 lines

address_space_translate will also provide a single point where IOMMU forwarding is implemented.

4 overlength lines in this patch. Otherwise

-- PMM

#57 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

In reply to Paolo Bonzini
Quoted 92 lines · Paolo Bonzini

Using phys_page_find to translate an AddressSpace to a MemoryRegionSection is unwieldy. It requires to pass the page index rather than the address, and later memory_region_section_addr has to be called. Replace memory_region_section_addr with a function that does all of it: call phys_page_find, compute the offset within the region, and check how big the current mapping is. This way, a large flat region can be written with a single lookup rather than a page at a time.

address_space_translate will also provide a single point where IOMMU forwarding is implemented.

---
 cputlb.c              |  20 ++---
 exec.c                | 201 +++++++++++++++++++++++++++-----------------------
 include/exec/cputlb.h |  12 ++-
 include/exec/memory.h |  31 ++++----
 translate-all.c       |   6 +-
 5 files changed, 143 insertions(+), 127 deletions(-)
cputlb.c +11 -9
index aba7e44..1f85da0 100644
@@ -248,13 +248,18 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
     target_ulong code_address;
     uintptr_t addend;
     CPUTLBEntry *te;
-    hwaddr iotlb;
+    hwaddr iotlb, xlat, sz;
 
     assert(size >= TARGET_PAGE_SIZE);
     if (size != TARGET_PAGE_SIZE) {
         tlb_add_large_page(env, vaddr, size);
     }
-    section = phys_page_find(address_space_memory.dispatch, paddr >> TARGET_PAGE_BITS);
+
+    sz = size;
+    section = address_space_translate(&address_space_memory, paddr, &xlat, &sz,
+                                      false);
+    assert(sz >= TARGET_PAGE_SIZE);
+
 #if defined(DEBUG_TLB)
     printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
            " prot=%x idx=%d pd=0x%08lx\n",
@@ -269,15 +274,14 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
     }
     if (memory_region_is_ram(section->mr) ||
         memory_region_is_romd(section->mr)) {
-        addend = (uintptr_t)memory_region_get_ram_ptr(section->mr)
-        + memory_region_section_addr(section, paddr);
+        addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
     } else {
         addend = 0;
     }
 
     code_address = address;
-    iotlb = memory_region_section_get_iotlb(env, section, vaddr, paddr, prot,
-                                            &address);
+    iotlb = memory_region_section_get_iotlb(env, section, vaddr, paddr, xlat,
+                                            prot, &address);
 
     index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
     env->iotlb[mmu_idx][index] = iotlb - vaddr;
@@ -300,9 +304,7 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr,
             /* Write access calls the I/O callback.  */
             te->addr_write = address | TLB_MMIO;
         } else if (memory_region_is_ram(section->mr)
-                   && !cpu_physical_memory_is_dirty(
-                           section->mr->ram_addr
-                           + memory_region_section_addr(section, paddr))) {
+                   && !cpu_physical_memory_is_dirty(section->mr->ram_addr + xlat)) {
             te->addr_write = address | TLB_NOTDIRTY;
         } else {
             te->addr_write = address;
exec.c +1 -1
index 82da067..e5ee8ff 100644
@@ -182,7 +182,7 @@ static void phys_page_set(AddressSpaceDispatch *d,
     phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
 }
 
-MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
+static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
 {
     PhysPageEntry lp = d->phys_map;
     PhysPageEntry *p;
@@ -198,6 +198,22 @@ MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
     return &phys_sections[lp.ptr];
 }
 

exec.c MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index) · line 210

Quoted 10 lines
exec.c +10 -0
+MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
+                                             hwaddr *xlat, hwaddr *plen,
+                                             bool is_write)
+{
+    MemoryRegionSection *section;
+
+    section = phys_page_find(as->dispatch, addr >> TARGET_PAGE_BITS);
+    /* Compute offset within MemoryRegionSection */
+    addr -= section->offset_within_address_space;
+    *plen = MIN(section->size - addr, *plen);

This limitation causes problems. Consider two overlapping memory regions A and B. A handles 4-byte accesses and is at least 4 bytes long, B only deals with a single byte. They overlap like this:

B (prio 1): X
A (prio 0): XXXX...
^access here with 4 bytes length

Now if an access happens at the marked position, it is split into one 2-byte access to A, followed by a one-byte access to B and another one-byte access to A. But the right access emulation would be 4-bytes to A, no?

I think we need to check against the length of the target mr here instead, but I'm not totally sure yet.

A concrete scenario where this breaks popped up while reworking PIO dispatching. PCI accesses to 0xcf8 no longer worked:

0000000000000cf8-0000000000000cfb (prio 0, RW): pci-conf-idx 0000000000000cf9-0000000000000cf9 (prio 1, RW): piix3-reset-control

Jan

#58 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 25/05/2013 08:40, Jan Kiszka ha scritto:

Quoted 89 lines · Paolo Bonzini
Quoted 74 lines

Using phys_page_find to translate an AddressSpace to a MemoryRegionSection is unwieldy. It requires to pass the page index rather than the address, and later memory_region_section_addr has to be called. Replace memory_region_section_addr with a function that does all of it:
call phys_page_find, compute the offset within the region, and check how big the current mapping is. This way, a large flat region can be written with a single lookup rather than a page at a time.

address_space_translate will also provide a single point where IOMMU forwarding is implemented.

| 20 ++--- exec.c | 201
+++++++++++++++++++++++++++-----------------------
include/exec/cputlb.h | 12 ++- include/exec/memory.h | 31
++++---- translate-all.c | 6 +- 5 files changed, 143
insertions(+), 127 deletions(-)

cputlb.c index aba7e44..1f85da0 100644 renamed from cputlb.c +0 -1
--- a/cputlb.c +++ b/cputlb.c @@ -248,13 +248,18 @@ void

tlb_set_page(CPUArchState *env, target_ulong vaddr, target_ulong
code_address; uintptr_t addend; CPUTLBEntry *te; - hwaddr
iotlb; + hwaddr iotlb, xlat, sz;

assert(size >= TARGET_PAGE_SIZE); if (size != TARGET_PAGE_SIZE)
{ tlb_add_large_page(env, vaddr, size); } - section =
phys_page_find(address_space_memory.dispatch, paddr >>
TARGET_PAGE_BITS); + + sz = size; + section =
address_space_translate(&address_space_memory, paddr, &xlat,
&sz, + false); +
assert(sz >= TARGET_PAGE_SIZE); + #if defined(DEBUG_TLB) printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx " prot=%x idx=%d pd=0x%08lx\n", @@ -269,15 +274,14 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr, } if (memory_region_is_ram(section->mr) ||
memory_region_is_romd(section->mr)) { - addend =
(uintptr_t)memory_region_get_ram_ptr(section->mr) - +
memory_region_section_addr(section, paddr); + addend =
(uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat; } else { addend = 0; }

code_address = address; - iotlb =
memory_region_section_get_iotlb(env, section, vaddr, paddr,
prot, - &address); +
iotlb = memory_region_section_get_iotlb(env, section, vaddr,
paddr, xlat, + prot,
&address);

index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); env->iotlb[mmu_idx][index] = iotlb - vaddr; @@ -300,9 +304,7 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr, /* Write access calls the I/O callback. */ te->addr_write = address | TLB_MMIO; } else if (memory_region_is_ram(section->mr) - && !cpu_physical_memory_is_dirty( -
section->mr->ram_addr - +
memory_region_section_addr(section, paddr))) { +
&& !cpu_physical_memory_is_dirty(section->mr->ram_addr + xlat)) { te->addr_write = address | TLB_NOTDIRTY; } else { te->addr_write = address; diff --git a/exec.c b/exec.c index 82da067..e5ee8ff 100644 --- a/exec.c +++ b/exec.c @@ -182,7 +182,7 @@ static void phys_page_set(AddressSpaceDispatch *d, phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS
- 1); }

-MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index) +static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index) { PhysPageEntry lp = d->phys_map; PhysPageEntry *p; @@ -198,6 +198,22 @@ MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index) return &phys_sections[lp.ptr]; }

Quoted 9 lines

+MemoryRegionSection *address_space_translate(AddressSpace *as,
hwaddr addr, + hwaddr
*xlat, hwaddr *plen, +
bool is_write) +{ + MemoryRegionSection *section; + +
section = phys_page_find(as->dispatch, addr >>
TARGET_PAGE_BITS); + /* Compute offset within
MemoryRegionSection */ + addr -=
section->offset_within_address_space; + *plen =
MIN(section->size - addr, *plen);

This limitation causes problems. Consider two overlapping memory regions A and B. A handles 4-byte accesses and is at least 4 bytes long, B only deals with a single byte. They overlap like this:

Quoted 7 lines

B (prio 1): X A (prio 0): XXXX... ^access here with 4 bytes
length

Now if an access happens at the marked position, it is split into one 2-byte access to A, followed by a one-byte access to B and another one-byte access to A. But the right access emulation would be 4-bytes to A, no?

I guess it depends on the width of the bus. On a 16-bit computer the right thing to do would be to split the write in two, one two-byte access to A and one two-byte access to B. But as a first approximation the fix you suggest is the right one. Implementing bus width in TCG is tricky, so we should get by with the simple fix.

This patch is not in the pull request I sent, so there is time to make it work. Is it simply

- - *plen = MIN(section->size - addr, *plen);
+ addr += section->offset_within_memory_region;
+ *plen = MIN(section->mr->size - addr, *plen);

or something like that? If you post it as a diff I can squash it in.

Quoted 9 lines

I think we need to check against the length of the target mr here instead, but I'm not totally sure yet.

A concrete scenario where this breaks popped up while reworking PIO dispatching. PCI accesses to 0xcf8 no longer worked:

0000000000000cf8-0000000000000cfb (prio 0, RW): pci-conf-idx 0000000000000cf9-0000000000000cf9 (prio 1, RW):
piix3-reset-control

Paolo

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRoGybAAoJEBvWZb6bTYbyELUP/09bPNSKl39Tog0uyV+VJ3BR IGDsWfKHnIm6T2hC0S4BhVRaBkDyDAC6c8hJ7iy9084/pw61Cra1RrNMdSOzNHqo x4pC+Zw6hD9lK0ncRsB21w1n398yen7nk+RN4zf2LxxAnIiXrUuaG6SL5q3qSibz lXj1fKP+hG2yoxS7frMAJVjBEFIhBj/EY5u3pmd6+qREGxYaLBKkuXSnbjHdfG3s Q8OLJZ8bRH7o3c034yt+LvJ776PfhjzaggUmrYri7fZ2kU+SmUzqnkW9If2ekQsf MQ3ZQRVrVc5AaHLdQLyjSaSpzPcON5w4VpqU9SCisKi9rFdDOW6qHuZc6ZTbUpch wAbGeI0h1PA01o41milx0LU+Y7Qfl6wRzbB3CZEpgp8fFSR95LySwD1Zldt1Ft6E Zv8Mh0NVV4qvVEtp+HrTTh91kVz5W9OXnqFOz+wYrBlaVBRUxBLWSzQvuScnYv2B Rv51bcsq07bOZFR2BabC9zuRnHlCC9LqQAvP5i8AdvC1FsxIGPtK+FLc3QDeD+c7 Ndz5mZq6jgAeg0BQrBv7H++P6I2dw/lJkrb4zwGY68ckJ+Yo67NGbYvhM0/W1WF5 XVebDnb1F3+qWsVtSJWWoOHtW0HRFS+3il87/u4MKJc1BF+ceONrSJLBQyvSJDNf 9sPb/mhfVloOj7tWqWAk
=Y4yR
-----END PGP SIGNATURE-----

#59 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 111 lines · Paolo Bonzini

Il 25/05/2013 08:40, Jan Kiszka ha scritto:

Quoted 89 lines · Paolo Bonzini
Quoted 74 lines

Using phys_page_find to translate an AddressSpace to a MemoryRegionSection is unwieldy. It requires to pass the page index rather than the address, and later memory_region_section_addr has to be called. Replace memory_region_section_addr with a function that does all of it:
call phys_page_find, compute the offset within the region, and check how big the current mapping is. This way, a large flat region can be written with a single lookup rather than a page at a time.

address_space_translate will also provide a single point where IOMMU forwarding is implemented.

| 20 ++--- exec.c | 201
+++++++++++++++++++++++++++-----------------------
include/exec/cputlb.h | 12 ++- include/exec/memory.h | 31
++++---- translate-all.c | 6 +- 5 files changed, 143
insertions(+), 127 deletions(-)

cputlb.c index aba7e44..1f85da0 100644 renamed from cputlb.c +0 -1
--- a/cputlb.c +++ b/cputlb.c @@ -248,13 +248,18 @@ void

tlb_set_page(CPUArchState *env, target_ulong vaddr, target_ulong
code_address; uintptr_t addend; CPUTLBEntry *te; - hwaddr
iotlb; + hwaddr iotlb, xlat, sz;

assert(size >= TARGET_PAGE_SIZE); if (size != TARGET_PAGE_SIZE)
{ tlb_add_large_page(env, vaddr, size); } - section =
phys_page_find(address_space_memory.dispatch, paddr >>
TARGET_PAGE_BITS); + + sz = size; + section =
address_space_translate(&address_space_memory, paddr, &xlat,
&sz, + false); +
assert(sz >= TARGET_PAGE_SIZE); + #if defined(DEBUG_TLB) printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx " prot=%x idx=%d pd=0x%08lx\n", @@ -269,15 +274,14 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr, } if (memory_region_is_ram(section->mr) ||
memory_region_is_romd(section->mr)) { - addend =
(uintptr_t)memory_region_get_ram_ptr(section->mr) - +
memory_region_section_addr(section, paddr); + addend =
(uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat; } else { addend = 0; }

code_address = address; - iotlb =
memory_region_section_get_iotlb(env, section, vaddr, paddr,
prot, - &address); +
iotlb = memory_region_section_get_iotlb(env, section, vaddr,
paddr, xlat, + prot,
&address);

index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); env->iotlb[mmu_idx][index] = iotlb - vaddr; @@ -300,9 +304,7 @@ void tlb_set_page(CPUArchState *env, target_ulong vaddr, /* Write access calls the I/O callback. */ te->addr_write = address | TLB_MMIO; } else if (memory_region_is_ram(section->mr) - && !cpu_physical_memory_is_dirty( -
section->mr->ram_addr - +
memory_region_section_addr(section, paddr))) { +
&& !cpu_physical_memory_is_dirty(section->mr->ram_addr + xlat)) { te->addr_write = address | TLB_NOTDIRTY; } else { te->addr_write = address; diff --git a/exec.c b/exec.c index 82da067..e5ee8ff 100644 --- a/exec.c +++ b/exec.c @@ -182,7 +182,7 @@ static void phys_page_set(AddressSpaceDispatch *d, phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS
- 1); }

-MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index) +static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index) { PhysPageEntry lp = d->phys_map; PhysPageEntry *p; @@ -198,6 +198,22 @@ MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index) return &phys_sections[lp.ptr]; }

Quoted 9 lines

+MemoryRegionSection *address_space_translate(AddressSpace *as,
hwaddr addr, + hwaddr
*xlat, hwaddr *plen, +
bool is_write) +{ + MemoryRegionSection *section; + +
section = phys_page_find(as->dispatch, addr >>
TARGET_PAGE_BITS); + /* Compute offset within
MemoryRegionSection */ + addr -=
section->offset_within_address_space; + *plen =
MIN(section->size - addr, *plen);

This limitation causes problems. Consider two overlapping memory regions A and B. A handles 4-byte accesses and is at least 4 bytes long, B only deals with a single byte. They overlap like this:

Quoted 7 lines

B (prio 1): X A (prio 0): XXXX... ^access here with 4 bytes
length

Now if an access happens at the marked position, it is split into one 2-byte access to A, followed by a one-byte access to B and another one-byte access to A. But the right access emulation would be 4-bytes to A, no?

I guess it depends on the width of the bus. On a 16-bit computer the right thing to do would be to split the write in two, one two-byte access to A and one two-byte access to B. But as a first approximation the fix you suggest is the right one. Implementing bus width in TCG is tricky, so we should get by with the simple fix.

This patch is not in the pull request I sent, so there is time to make it work. Is it simply

(quoted diff) +2 -1
-    *plen = MIN(section->size - addr, *plen);
+    addr += section->offset_within_memory_region;
+    *plen = MIN(section->mr->size - addr, *plen);
Quoted 1 line

or something like that? If you post it as a diff I can squash it in.

This works for me now:

exec.c +3 -1
index 1610604..3d36bc7 100644
@@ -210,13 +210,15 @@ MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
     IOMMUTLBEntry iotlb;
     MemoryRegionSection *section;
     hwaddr len = *plen;
+    Int128 diff;
 
     for (;;) {
         section = address_space_lookup_region(as, addr);
 
         /* Compute offset within MemoryRegionSection */
         addr -= section->offset_within_address_space;
-        len = MIN(section->size - addr, len);
+        diff = int128_sub(section->mr->size, int128_make64(addr));
+        len = MIN(int128_get64(diff), len);
 
         /* Compute offset within MemoryRegion */
         addr += section->offset_within_region;

Jan

#60 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Il 25/05/2013 12:19, Jan Kiszka ha scritto:

Quoted 2 lines
         addr -= section->offset_within_address_space;

- len = MIN(section->size - addr, len);

                     ^^^^^^^^^^^^^   ^^^^

This is the size of a section minus an offset in the section.

Quoted 1 line

+ diff = int128_sub(section->mr->size, int128_make64(addr));

                             ^^^^^^^^^^^^^^^^^                ^^^^

This is the size of a region minus the same offset in the section.

Quoted 4 lines

+ len = MIN(int128_get64(diff), len);

         /* Compute offset within MemoryRegion */
         addr += section->offset_within_region;

So this has to be moved above. Do you have a branch pushed somewhere that I can test against?

Paolo

#61 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 10 lines · Paolo Bonzini

Il 25/05/2013 12:19, Jan Kiszka ha scritto:

Quoted 2 lines
         addr -= section->offset_within_address_space;

- len = MIN(section->size - addr, len);

                     ^^^^^^^^^^^^^   ^^^^

This is the size of a section minus an offset in the section.

Quoted 1 line

+ diff = int128_sub(section->mr->size, int128_make64(addr));

                             ^^^^^^^^^^^^^^^^^                ^^^^
Quoted 8 lines

This is the size of a region minus the same offset in the section.

Quoted 4 lines

+ len = MIN(int128_get64(diff), len);

         /* Compute offset within MemoryRegion */
         addr += section->offset_within_region;

So this has to be moved above.

Right, fixed.

Quoted 2 lines

Do you have a branch pushed somewhere
that I can test against?

git://git.kiszka.org/qemu.git queues/ioport

Jan

#62 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 25/05/2013 13:30, Jan Kiszka ha scritto:

Quoted 20 lines · Paolo Bonzini
Quoted 19 lines

Il 25/05/2013 12:19, Jan Kiszka ha scritto:

Quoted 2 lines

addr -= section->offset_within_address_space; - len =
MIN(section->size - addr, len);

^^^^^^^^^^^^^ ^^^^

This is the size of a section minus an offset in the section.

Quoted 2 lines

+ diff = int128_sub(section->mr->size,
int128_make64(addr));

^^^^^^^^^^^^^^^^^ ^^^^

This is the size of a region minus the same offset in the section.

Quoted 4 lines

+ len = MIN(int128_get64(diff), len);

/* Compute offset within MemoryRegion */ addr +=
section->offset_within_region;

Quoted 7 lines
Quoted 1 line

So this has to be moved above.

Right, fixed.

Quoted 1 line

Do you have a branch pushed somewhere that I can test against?

git://git.kiszka.org/qemu.git queues/ioport

Nice patches. Only one thing, how is .impl.unaligned different from the existing .valid.unaligned?

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRoc4vAAoJEBvWZb6bTYby7UEP/0l5Stow6nrKirryCNuds3ky L3FTjqEhLZ+aKha4rdbYNKhwbvY92rfR/Wo8hMckkFv6wwcieHD+nYQltuIDW3y2 xV7FmCPJd1M1pajIQ5Nk+Ssqf7Qhv7jRX5Kg2L6ksNf5srb5485J9oH4M9hIO6CH XFHO9GeKmynbeeZmSNFF9mNlQd0leHx6h6IbJxYov/eD0zxp5cxoN7xM6+V5hCP/ UjBa2Zn07AI94BS/64YSHSa155BLxIOS5jJi25PNOWti29wHE8Nhw2KF5pMD/JYM hPblsTIpFf6zg53FMHmrY/B990Ol+IjNogQhRh3tSfNf/48XWA4l+anLdaMweQf6 TuCEOoDqgNphz6c5lOPjzQB/CJZFUCQzxaUjok499ZQyPY5SpIgjKmzl7Vszos8I wnYmJJ3xVLyJujvx61rg7qiW3ckyVx0sPHZXt/DWEDUNDHTW+HdqAcrZ8TgJrIni gQF1VEKNplxdgauIv17RGxnVOXwb8PcoNwXE9Gn6XJ/KKQLh9KukmdufCSBmOARV iiAo8N4rUK8FaCFXDwrZ3S8OIiyWCSdnud/4wZPcsI+r5gM/rrtlrZoQExyx5meO 9RzLBtL5hwFaQ9erE8ce+GDKbo+nie593z3UiN5NKLuH5KjWig2OYQxV6ylg4Vx7 RscCNjGvppk+2I3PAv9R
=pCeD
-----END PGP SIGNATURE-----

#63 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 23 lines · Paolo Bonzini

Il 25/05/2013 13:30, Jan Kiszka ha scritto:

Quoted 21 lines · Paolo Bonzini
Quoted 20 lines

Il 25/05/2013 12:19, Jan Kiszka ha scritto:

Quoted 2 lines

addr -= section->offset_within_address_space; - len =
MIN(section->size - addr, len);

^^^^^^^^^^^^^ ^^^^

This is the size of a section minus an offset in the section.

Quoted 2 lines

+ diff = int128_sub(section->mr->size,
int128_make64(addr));

^^^^^^^^^^^^^^^^^ ^^^^

This is the size of a region minus the same offset in the section.

Quoted 4 lines

+ len = MIN(int128_get64(diff), len);

/* Compute offset within MemoryRegion */ addr +=
section->offset_within_region;

So this has to be moved above.

Quoted 8 lines
Quoted 5 lines

Right, fixed.

Quoted 1 line

Do you have a branch pushed somewhere that I can test against?

git://git.kiszka.org/qemu.git queues/ioport

Nice patches. Only one thing, how is .impl.unaligned different from the existing .valid.unaligned?

See memory.h: valid controls is an unaligned access traps or gets processed, impl manages if it is passed as-is to the device or broken up and aligned first.

Jan

#64 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 26/05/2013 11:02, Jan Kiszka ha scritto:

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Nice patches. Only one thing, how is .impl.unaligned different from the existing .valid.unaligned?

See memory.h: valid controls is an unaligned access traps or gets processed, impl manages if it is passed as-is to the device or broken up and aligned first.

I took the following patches:

exec: Allow unaligned address_space_rw
exec: Resolve subpages in one step except for IOTLB fills
exec: Implement subpage_read/write via address_space_rw

to limit the conflicts and because I realized that TCG should never encounter an IOMMU. Thus I removed the is_write argument from address_space_translate_internal and moved the IOMMU handling to address_space_translate. I'll push to my iommu branch after some more testing. If you reorganize the old_portio handling along the lines I suggested in my previous email you should have no conflicts when rebasing.

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRowkzAAoJEBvWZb6bTYbybiQP/i4S4tUisfJVPlnKosEwxUxk LY0I6oyeXIQznv/2VC+CkKuNFRpQyPgOoD+Nc4mVdVSEgexdJdNHvT3qQeewkRBB Kk074LS/tMdtyRFyhf+ZuhYFBFgCV4LINyJ3iZV9E913LqV/cM6CXnvYcOWwNspu 2ZPGLCueG5y0iHcHHFo2ZXRf/OqUKiKIzMMKPzS57+7o7rUqav8wSnQV1Qox/L0G xeeUZ4BO9a59ULIb63bwhrjcUHS77vddvAcPDe5LifAo1OZqZWXsWvzdj/3VKY0A Deyt+q02QA9erYPR0K16nLr3j+8z44McPugHWNNmAzQzMXjFIVE1g7kBmqzBLAst tStrog73ol1xgor5xl1wIqEfGQNGOroNuPEpdlU7NQf4c6aFJ7cWFMJnaOtfZuNB 7eTtQV32lQkqns7ho3wJFsv7k2sWjpBrjrXCNnii+LH/MUUm8KsAQ+w2JVCRHEDK AEmnniEoCSLMbwZFOiKGT0J5291dZgeK6dbH2NVJ8jYhTWaDGuIldXtGyMvBkvpi +YZF4Z3vvOPFeT+zl0DPh0xHsaXcblfZbe3pe4aDDlgjmsUAbyNIsTsAIn/+eq0y WwjuEj76bR8Y+RbJgkQ1VP6mCDqQLaOAiKWniZZ1wjZMBmE3vCwsmi3CNEVMxJOD XOnhQIEOw5kautUlcnQ3
=1nFS
-----END PGP SIGNATURE-----

#65 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 8 lines · Paolo Bonzini

Il 26/05/2013 11:02, Jan Kiszka ha scritto:

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Nice patches. Only one thing, how is .impl.unaligned different from the existing .valid.unaligned?

See memory.h: valid controls is an unaligned access traps or gets processed, impl manages if it is passed as-is to the device or broken up and aligned first.

Quoted 8 lines

I took the following patches:

exec: Allow unaligned address_space_rw
exec: Resolve subpages in one step except for IOTLB fills
exec: Implement subpage_read/write via address_space_rw

to limit the conflicts and because I realized that TCG should never encounter an IOMMU.

Err, why? Will we emulate IOMMUs for TCG differently?

Quoted 5 lines

Thus I removed the is_write argument from
address_space_translate_internal and moved the IOMMU handling to address_space_translate. I'll push to my iommu branch after some more testing. If you reorganize the old_portio handling along the lines I suggested in my previous email you should have no conflicts when rebasing.

old_portio should rather be eliminated on the long run. But that's a future story.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#66 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Il 27/05/2013 09:23, Jan Kiszka ha scritto:

Quoted 11 lines · Paolo Bonzini
Quoted 10 lines

Il 26/05/2013 11:02, Jan Kiszka ha scritto:

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Nice patches. Only one thing, how is .impl.unaligned different from the existing .valid.unaligned?

See memory.h: valid controls is an unaligned access traps or gets processed, impl manages if it is passed as-is to the device or broken up and aligned first.

I took the following patches:

Quoted 8 lines
Quoted 6 lines

exec: Allow unaligned address_space_rw
exec: Resolve subpages in one step except for IOTLB fills
exec: Implement subpage_read/write via address_space_rw

to limit the conflicts and because I realized that TCG should never encounter an IOMMU.

Err, why? Will we emulate IOMMUs for TCG differently?

Because IOMMUs should never be added to address_space_memory.

TCG should only encounter an IOMMU during device emulation (DMA), not because of reads/writes from the CPU. So the IOTLBs should never point to an IOMMU region.

Paolo

Quoted 11 lines
Quoted 5 lines

Thus I removed the is_write argument from
address_space_translate_internal and moved the IOMMU handling to address_space_translate. I'll push to my iommu branch after some more testing. If you reorganize the old_portio handling along the lines I suggested in my previous email you should have no conflicts when rebasing.

old_portio should rather be eliminated on the long run. But that's a future story.

Jan

#67 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 19 lines · Paolo Bonzini

Il 27/05/2013 09:23, Jan Kiszka ha scritto:

Quoted 18 lines · Paolo Bonzini
Quoted 16 lines

Il 26/05/2013 11:02, Jan Kiszka ha scritto:

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Nice patches. Only one thing, how is .impl.unaligned different from the existing .valid.unaligned?

See memory.h: valid controls is an unaligned access traps or gets processed, impl manages if it is passed as-is to the device or broken up and aligned first.

I took the following patches:

exec: Allow unaligned address_space_rw
exec: Resolve subpages in one step except for IOTLB fills
exec: Implement subpage_read/write via address_space_rw

to limit the conflicts and because I realized that TCG should never encounter an IOMMU.

Quoted 7 lines
Quoted 1 line

Err, why? Will we emulate IOMMUs for TCG differently?

Because IOMMUs should never be added to address_space_memory.

TCG should only encounter an IOMMU during device emulation (DMA), not because of reads/writes from the CPU. So the IOTLBs should never point to an IOMMU region.

OK. That's true for the current QEMU architecture, but may no longer be like this if we once emulate device controllers via TCG (in hybrid system setups). Anyway, I guess it's fine to optimize this for now.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#68 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

Il 27/05/2013 09:23, Jan Kiszka ha scritto:

Quoted 1 line

Err, why? Will we emulate IOMMUs for TCG differently?

Because IOMMUs should never be added to address_space_memory.

TCG should only encounter an IOMMU during device emulation (DMA), not because of reads/writes from the CPU. So the IOTLBs should never point to an IOMMU region.

This seems a slightly dubious assumption to me. For instance here's a sample system diagram that puts a Cortex-M3 CPU behind an IOMMU (the MMU-500 dotted line):
http://www.arm.com/images/CoreLink_MMU-500_in_System.jpg
Admittedly we're a long way from being able to model that since we don't support multiple CPUs in one system yet.

Can we have an assertion if you try to add an IOMMU to the CPU's view of memory, so it's obvious if we ever do run into this case?

thanks
-- PMM

#69 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Il 27/05/2013 12:33, Peter Maydell ha scritto:

Quoted 10 lines · Paolo Bonzini
Quoted 8 lines

Il 27/05/2013 09:23, Jan Kiszka ha scritto:

Quoted 1 line

Err, why? Will we emulate IOMMUs for TCG differently?

Because IOMMUs should never be added to address_space_memory.

TCG should only encounter an IOMMU during device emulation (DMA), not because of reads/writes from the CPU. So the IOTLBs should never point to an IOMMU region.

Quoted 6 lines

This seems a slightly dubious assumption to me. For instance here's a sample system diagram that puts a Cortex-M3 CPU behind an IOMMU (the MMU-500 dotted line):
http://www.arm.com/images/CoreLink_MMU-500_in_System.jpg
Admittedly we're a long way from being able to model that since we don't support multiple CPUs in one system yet.

It is possible to do it.

One way is to add IOMMU handling to the memory dispatch routines. This was present in Avi's patches. With the changes to propagate errors through MMIO dispatch it raises some interesting points WRT time-of-check-to-time-of-use (we need to ensure the translation is only done once and reused), but it should be doable and anyway isn't the biggest problem. The biggest problem is that, I think, this wouldn't work because all accesses including reading code would be treated as MMIO.

A second way is to use the IOMMU notifiers to flush the CPU TLB entry whenever the corresponding IOMMU entry changes. This is probably not what hardware does, but it is faster and doesn't have the problem of code accesses. The previous versions I sent do this, except I didn't have the IOMMU notifiers yet.

As in other cases, I prefer no code to untested code. The design is sane (it isn't mine, so I can say it :)) and we know it can be done.

Quoted 3 lines

Can we have an assertion if you try to add an IOMMU to the CPU's view of memory, so it's obvious if we ever do run into this case?

Almost, I have an assertion that triggers if tlb_set_page would be handed out an IOMMU region.

Paolo

#70 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 4 lines · Paolo Bonzini

It is possible to do it.

As in other cases, I prefer no code to untested code. The design is sane (it isn't mine, so I can say it :)) and we know it can be done.

Agreed; we're a long way from being able to make use of this.

Quoted 2 lines

Almost, I have an assertion that triggers if tlb_set_page would be handed out an IOMMU region.

Sounds good.

thanks
-- PMM

#71 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

In reply to Jan Kiszka

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 25/05/2013 13:30, Jan Kiszka ha scritto:

Quoted 20 lines · Paolo Bonzini
Quoted 19 lines

Il 25/05/2013 12:19, Jan Kiszka ha scritto:

Quoted 2 lines

addr -= section->offset_within_address_space; - len =
MIN(section->size - addr, len);

^^^^^^^^^^^^^ ^^^^

This is the size of a section minus an offset in the section.

Quoted 2 lines

+ diff = int128_sub(section->mr->size,
int128_make64(addr));

^^^^^^^^^^^^^^^^^ ^^^^

This is the size of a region minus the same offset in the section.

Quoted 4 lines

+ len = MIN(int128_get64(diff), len);

/* Compute offset within MemoryRegion */ addr +=
section->offset_within_region;

Quoted 7 lines
Quoted 1 line

So this has to be moved above.

Right, fixed.

Quoted 1 line

Do you have a branch pushed somewhere that I can test against?

git://git.kiszka.org/qemu.git queues/ioport

And another thing... in "ioport: Switch dispatching to memory core layer", could you put memory_region_iorange_read/write in mr->ops->read/write instead of adding an "if" in the dispatch routines?

Paolo

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRoc9MAAoJEBvWZb6bTYbya6IP/Rso0r/qUrRttOZYyC0GX1IL xBXZEyCDrbV49pQw9O85Lpb2BRzbUOy38RZ/gvrUfGzg9iYo+88nxoA4qiTXSKil IBxC/Nu5QCcV01zTWLE7dsjAGVVP6td2UWqtI/41T7H1OmhmN9BR2hD0hHhDT6aO f1KAhzkrdD2aOJHCyWjtehriZFH+J638QuAem8zMIC6kFGOBm475iYBI5l4haXaN k2Rx66EaOuJRFzZVscHnTw3Ohrk2QZEe8EpBScQIf3hxAlDv4wPDzXYjADUmCGhH u5dHXSDMgD3+ll5/XLAq3dX/lZPn6nJCzAqV2DPboAxhjpe5+gt0i50r91Di8T+3 Rn8ycEi5hy7Pck++ijwTDs4JGU1vGvT6xpnCmsTwiU3Tw5Yd8lNEQJyOQAMsdsrH GX+Tsqiowoq8LKVACHSOHouffUB4TM5XdUN0dPGKdohPGyDSlRlkE58mFPeBmMRT SIhaCPkykpRguNFnkxx5iLl66yUsi5Jb0qeXslffKKx6wCr6N5d3jOeb2jqjG/LT AAlQ4U5GdKbKU6rDWmcB8GeV2HZh9+ivIUwC4Q4+s+inlaXXh5V4hnNKQ8aHc2MX wBfq07Zit8bbCUvB+tYf+ORAWHUV1BFWjFRkE4sZ08ouCs45TN7Ug67334GyPvxe K5Zz+3M9ZKkvnK4kNINg
=71ln
-----END PGP SIGNATURE-----

#72 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

Quoted 25 lines · Paolo Bonzini

Il 25/05/2013 13:30, Jan Kiszka ha scritto:

Quoted 23 lines · Paolo Bonzini
Quoted 14 lines

Il 25/05/2013 12:19, Jan Kiszka ha scritto:

Quoted 2 lines

addr -= section->offset_within_address_space; - len =
MIN(section->size - addr, len);

^^^^^^^^^^^^^ ^^^^

This is the size of a section minus an offset in the section.

Quoted 2 lines

+ diff = int128_sub(section->mr->size,
int128_make64(addr));

^^^^^^^^^^^^^^^^^ ^^^^

This is the size of a region minus the same offset in the section.

Quoted 6 lines
Quoted 4 lines

+ len = MIN(int128_get64(diff), len);

/* Compute offset within MemoryRegion */ addr +=
section->offset_within_region;

So this has to be moved above.

Right, fixed.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Do you have a branch pushed somewhere that I can test against?

git://git.kiszka.org/qemu.git queues/ioport

And another thing... in "ioport: Switch dispatching to memory core layer", could you put memory_region_iorange_read/write in mr->ops->read/write instead of adding an "if" in the dispatch routines?

Not trivially because mr->opaque is passed to the read/write handler, but memory_region_iorange_read/write needs the region. Can add more data structures to handles this, but what does it buy us?

Jan

#73 lore raw

Re: [Qemu-devel] [PATCH 17/30] memory: add address_space_translate

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 26/05/2013 11:12, Jan Kiszka ha scritto:

Quoted 32 lines · Paolo Bonzini
Quoted 31 lines

Il 25/05/2013 13:30, Jan Kiszka ha scritto:

Quoted 29 lines · Paolo Bonzini
Quoted 15 lines

Il 25/05/2013 12:19, Jan Kiszka ha scritto:

Quoted 2 lines

addr -= section->offset_within_address_space; - len
= MIN(section->size - addr, len);

^^^^^^^^^^^^^ ^^^^

This is the size of a section minus an offset in the section.

Quoted 2 lines

+ diff = int128_sub(section->mr->size,
int128_make64(addr));

^^^^^^^^^^^^^^^^^ ^^^^

This is the size of a region minus the same offset in the section.

Quoted 6 lines
Quoted 4 lines

+ len = MIN(int128_get64(diff), len);

/* Compute offset within MemoryRegion */ addr +=
section->offset_within_region;

So this has to be moved above.

Right, fixed.

Quoted 2 lines

Do you have a branch pushed somewhere that I can test against?

git://git.kiszka.org/qemu.git queues/ioport

Quoted 8 lines
Quoted 4 lines

And another thing... in "ioport: Switch dispatching to memory core layer", could you put memory_region_iorange_read/write in mr->ops->read/write instead of adding an "if" in the dispatch routines?

Not trivially because mr->opaque is passed to the read/write handler, but memory_region_iorange_read/write needs the region. Can add more data structures to handles this, but what does it buy us?

It's simpler, and I think it lets us remove ops->old_portio too. We can have something like

typedef MemoryRegionPortioList {
    MemoryRegion mr;
    void *piolist_opaque;
    MemoryRegionPortio pio[];
}

then you can set mr->opaque == mr. It's more similar to how other regions are implemented, e.g. subpages.

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRolMGAAoJEBvWZb6bTYbym5wP/izYQtpCSLhiAe6LiyN5cSVj wMe+gSqhqyvi8Ho/GewilHlp1L0UKXQs18KhKqZbEtVETFdvKYoU+sJCPqWbRThW Gj62dfo1Zn814gnv/CxiebslcI5fiU2UrHtpW1oXBKCZWmLJrfZSJqT5K7o1Z8Y3 qRDcR2sIISIQgAxM/n2EbuJWq/v50BUCDOXgpT3eUrq3rvdQHbh/Ym+y8Zd74sXl VhK3UmP17ro0YPLtImForhrFutgavtgKuKCVvMTkD04ZLrWrW+QdXJmGl+LuCp+M L/ib7jaR/4uPJ1RiDPbZexKc956yRsA5gzvJo9kHE0B6IipM+uqVCbrfk96kWrAC Cg3qGn26cT/bc4zNF7NudJxAErVaHf220iJecoqFXNi6OwZTM6IgHlVH58l+yrRE swQlxqQHQoYEzm4ZzsuK9C9Y50Y4C6G0LzkEWeyvYi5UdbOxt/hl1yh0pdSnhSSx 47Hw28nZgceDpcySv/Deb3zBzw/Pxx1Z3wkTxMhVpZ6t3Bot/T9hmkGQyuFqvXWF cdrJEdYmJ2CEkmY2uwKUWCZeMzNtR+qfij+QlcI9RjFoVlmlOySJmBwR55Y7yu5m H4wRZjeQ8PtfpZH/NmmwkNPOxaghDTaUn9k9/rEQzCDM9b17ddovvDI8JYqo9oaa K1di55883LPNfkvycya3
=dJkN
-----END PGP SIGNATURE-----

#74 lore raw

PATCH 18/30 memory: add return value to address_space_rw/read/write

In reply to Paolo Bonzini
---
 exec.c                | 23 ++++++++++-------------
 include/exec/memory.h | 12 +++++++++---
 2 files changed, 19 insertions(+), 16 deletions(-)
exec.c +10 -13
index e5ee8ff..4cc5ecb 100644
@@ -1901,7 +1901,7 @@ static void invalidate_and_set_dirty(hwaddr addr,
     xen_modified_memory(addr, length);
 }
 
-void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
+bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
                       int len, bool is_write)
 {
     hwaddr l;
@@ -1909,10 +1909,12 @@ void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
     uint32_t val;
     hwaddr addr1;
     MemoryRegionSection *section;
+    bool error = false;
 
     while (len > 0) {
         l = len;
         section = address_space_translate(as, addr, &addr1, &l, is_write);
+        error |= (section == &phys_sections[phys_section_unassigned]);
 
         if (is_write) {
             if (!memory_region_is_ram(section->mr)) {
@@ -1971,31 +1973,26 @@ void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
         buf += l;
         addr += l;
     }
+
+    return error;
 }
 
-void address_space_write(AddressSpace *as, hwaddr addr,
+bool address_space_write(AddressSpace *as, hwaddr addr,
                          const uint8_t *buf, int len)
 {
-    address_space_rw(as, addr, (uint8_t *)buf, len, true);
+    return address_space_rw(as, addr, (uint8_t *)buf, len, true);
 }
 
-/**
- * address_space_read: read from an address space.
- *
- * @as: #AddressSpace to be accessed
- * @addr: address within that address space
- * @buf: buffer with the data transferred
- */
-void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
+bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
 {
-    address_space_rw(as, addr, buf, len, false);
+    return address_space_rw(as, addr, buf, len, false);
 }
 
 
 void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
                             int len, int is_write)
 {
-    return address_space_rw(&address_space_memory, addr, buf, len, is_write);
+    address_space_rw(&address_space_memory, addr, buf, len, is_write);
 }
 
 /* used for ROM loading : can write in RAM and ROM */
include/exec/memory.h +9 -3
index 809a958..94709d1 100644
@@ -816,32 +816,38 @@ void address_space_destroy(AddressSpace *as);
 /**
  * address_space_rw: read from or write to an address space.
  *
+ * Return true if the operation hit any unassigned memory.
+ *
  * @as: #AddressSpace to be accessed
  * @addr: address within that address space
  * @buf: buffer with the data transferred
  * @is_write: indicates the transfer direction
  */
-void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
+bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
                       int len, bool is_write);
 
 /**
  * address_space_write: write to address space.
  *
+ * Return true if the operation hit any unassigned memory.
+ *
  * @as: #AddressSpace to be accessed
  * @addr: address within that address space
  * @buf: buffer with the data transferred
  */
-void address_space_write(AddressSpace *as, hwaddr addr,
+bool address_space_write(AddressSpace *as, hwaddr addr,
                          const uint8_t *buf, int len);
 
 /**
  * address_space_read: read from an address space.
  *
+ * Return true if the operation hit any unassigned memory.
+ *
  * @as: #AddressSpace to be accessed
  * @addr: address within that address space
  * @buf: buffer with the data transferred
  */
-void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
+bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
 
 /* address_space_translate: translate an address range into an address space
  * into a MemoryRegionSection and an address range into that section
Signature · 1 line
1.8.1.4
#76 lore raw

PATCH 19/30 memory: Introduce address_space_lookup_region

In reply to Paolo Bonzini

From: Jan Kiszka <[email protected]>

This introduces a wrapper for phys_page_find (before we complicate address_space_translate with IOMMU translation). This function will also encapsulate locking and reference counting when we introduce BQL-free dispatching.

---
 exec.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
exec.c +7 -1
index 4cc5ecb..a29dec5 100644
@@ -198,13 +198,19 @@ static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index
     return &phys_sections[lp.ptr];
 }
 
+static MemoryRegionSection *address_space_lookup_region(AddressSpace *as,
+                                                        hwaddr addr)
+{
+    return phys_page_find(as->dispatch, addr >> TARGET_PAGE_BITS);
+}
+
 MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
                                              hwaddr *xlat, hwaddr *plen,
                                              bool is_write)
 {
     MemoryRegionSection *section;
 
-    section = phys_page_find(as->dispatch, addr >> TARGET_PAGE_BITS);
+    section = address_space_lookup_region(as, addr);
     /* Compute offset within MemoryRegionSection */
     addr -= section->offset_within_address_space;
     *plen = MIN(section->size - addr, *plen);
Signature · 1 line
1.8.1.4
Reviewed-by on v1 19/30
#77 lore raw

Re: [Qemu-devel] [PATCH 19/30] memory: Introduce address_space_lookup_region

Quoted 9 lines · Paolo Bonzini

From: Jan Kiszka <[email protected]>

This introduces a wrapper for phys_page_find (before we complicate address_space_translate with IOMMU translation). This function will also encapsulate locking and reference counting when we introduce BQL-free dispatching.

-- PMM

#78 lore raw

PATCH 20/30 memory: iommu support

In reply to Paolo Bonzini

From: Avi Kivity <[email protected]>

Add a new memory region type that translates addresses it is given, then forwards them to a target address space. This is similar to an alias, except that the mapping is more flexible than a linear translation and trucation, and also less efficient since the translation happens at runtime.

The implementation uses an AddressSpace mapping the target region to avoid hierarchical dispatch all the way to the resolved region; only iommu regions are looked up dynamically.

[Modified to put translation in address_space_translate - Paolo]
---
 exec.c                | 35 ++++++++++++++++++++++-----
 include/exec/memory.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++----
 memory.c              | 21 ++++++++++++++++
 3 files changed, 112 insertions(+), 10 deletions(-)
exec.c +29 -6
index a29dec5..b7266c8 100644
@@ -208,15 +208,38 @@ MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
                                              hwaddr *xlat, hwaddr *plen,
                                              bool is_write)
 {
+    IOMMUTLBEntry iotlb;
     MemoryRegionSection *section;
+    hwaddr len = *plen;
 
-    section = address_space_lookup_region(as, addr);
-    /* Compute offset within MemoryRegionSection */
-    addr -= section->offset_within_address_space;
-    *plen = MIN(section->size - addr, *plen);
+    for (;;) {
+        section = address_space_lookup_region(as, addr);
+
+        /* Compute offset within MemoryRegionSection */
+        addr -= section->offset_within_address_space;
+        len = MIN(section->size - addr, len);
+
+        /* Compute offset within MemoryRegion */
+        addr += section->offset_within_region;
+
+        if (!section->mr->iommu_ops) {
+            break;
+        }
+
+        iotlb = section->mr->iommu_ops->translate(section->mr, addr);
+        addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
+                | (addr & iotlb.addr_mask));
+        len = MIN(len, (addr | iotlb.addr_mask) - addr + 1);
+        if (!(iotlb.perm & (1 << is_write))) {
+            section = &phys_sections[phys_section_unassigned];
+            break;
+        }
+
+        as = section->mr->iommu_target_as;
+    }
 
-    /* Compute offset within MemoryRegion */
-    *xlat = addr + section->offset_within_region;
+    *plen = len;
+    *xlat = addr;
     return section;
 }
 
include/exec/memory.h +62 -4

120 lines not on this page · raw message

memory.c +21 -0
index 99f046d..b25d21d 100644
@@ -787,6 +787,7 @@ void memory_region_init(MemoryRegion *mr,
                         uint64_t size)
 {
     mr->ops = NULL;
+    mr->iommu_ops = NULL;
     mr->parent = NULL;
     mr->size = int128_make64(size);
     if (size == UINT64_MAX) {
@@ -977,6 +978,21 @@ void memory_region_init_rom_device(MemoryRegion *mr,
     mr->ram_addr = qemu_ram_alloc(size, mr);
 }
 
+void memory_region_init_iommu(MemoryRegion *mr,
+                              MemoryRegionIOMMUOps *ops,
+                              AddressSpace *target_as,
+                              const char *name,
+                              uint64_t size)
+{
+    memory_region_init(mr, name, size);
+    mr->ops = NULL;
+    mr->iommu_ops = ops,
+    mr->opaque = mr;
+    mr->terminates = true;  /* then re-forwards */
+    mr->destructor = memory_region_destructor_none;
+    mr->iommu_target_as = target_as;
+}
+
 static uint64_t invalid_read(void *opaque, hwaddr addr,
                              unsigned size)
 {
@@ -1051,6 +1067,11 @@ bool memory_region_is_rom(MemoryRegion *mr)
     return mr->ram && mr->readonly;
 }
 
+bool memory_region_is_iommu(MemoryRegion *mr)
+{
+    return mr->iommu_ops;
+}
+
 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
 {
     uint8_t mask = 1 << client;
Signature · 1 line
1.8.1.4
#79 lore raw

Re: [Qemu-devel] [PATCH 20/30] memory: iommu support

Quoted 8 lines · Paolo Bonzini

From: Avi Kivity <[email protected]>

Add a new memory region type that translates addresses it is given, then forwards them to a target address space. This is similar to an alias, except that the mapping is more flexible than a linear translation and trucation, and also less efficient since the translation happens at runtime.

Quoted 7 lines

The implementation uses an AddressSpace mapping the target region to avoid hierarchical dispatch all the way to the resolved region; only iommu regions are looked up dynamically.

[Modified to put translation in address_space_translate - Paolo]

More overlong lines here, and these are in comments so trivial to avoid.

Quoted 4 lines

/**
+ * memory_region_is_iommu: check whether a memory region is an iommu
+ *
+ * Returns %true is a memory region is an iommu.

"s/is/if/"

memory.c void memory_region_init_rom_device(MemoryRegion *mr, · line 992

Quoted 13 lines
memory.c +12 -0
+void memory_region_init_iommu(MemoryRegion *mr,
+                              MemoryRegionIOMMUOps *ops,
+                              AddressSpace *target_as,
+                              const char *name,
+                              uint64_t size)
+{
+    memory_region_init(mr, name, size);
+    mr->ops = NULL;
+    mr->iommu_ops = ops,
+    mr->opaque = mr;
+    mr->terminates = true;  /* then re-forwards */
+    mr->destructor = memory_region_destructor_none;

destructor_none is the default, you don't need to set it again.

thanks
-- PMM

#80 lore raw

PATCH 21/30 memory: Add iommu map/unmap notifiers

In reply to Paolo Bonzini

From: David Gibson <[email protected]>

This patch adds a NotifierList to MemoryRegions which represent IOMMUs allowing other parts of the code to register interest in mappings or unmappings from the IOMMU. All IOMMU implementations will need to call memory_region_notify_iommu() to inform those waiting on the notifier list, whenever an IOMMU mapping is made or removed.

---
 include/exec/memory.h |  7 +++++++
 memory.c              | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+)
include/exec/memory.h +7 -0
index e5cad03..05b9bb2 100644
@@ -25,6 +25,7 @@
 #include "exec/iorange.h"
 #include "exec/ioport.h"
 #include "qemu/int128.h"
+#include "qemu/notify.h"
 
 #define MAX_PHYS_ADDR_SPACE_BITS 62
 #define MAX_PHYS_ADDR            (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
@@ -172,6 +173,7 @@ struct MemoryRegion {
     unsigned ioeventfd_nb;
     MemoryRegionIoeventfd *ioeventfds;
     struct AddressSpace *iommu_target_as;
+    NotifierList iommu_notify;
 };
 
 struct MemoryRegionPortio {
@@ -423,6 +425,11 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)
  */
 bool memory_region_is_iommu(MemoryRegion *mr);
 
+void memory_region_notify_iommu(MemoryRegion *mr,
+                                IOMMUTLBEntry entry);
+void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
+void memory_region_unregister_iommu_notifier(Notifier *n);
+
 /**
  * memory_region_name: get a memory region's name
  *
memory.c +18 -0
index b25d21d..e3970bf 100644
@@ -991,6 +991,7 @@ void memory_region_init_iommu(MemoryRegion *mr,
     mr->terminates = true;  /* then re-forwards */
     mr->destructor = memory_region_destructor_none;
     mr->iommu_target_as = target_as;
+    notifier_list_init(&mr->iommu_notify);
 }
 
 static uint64_t invalid_read(void *opaque, hwaddr addr,
@@ -1072,6 +1073,23 @@ bool memory_region_is_iommu(MemoryRegion *mr)
     return mr->iommu_ops;
 }
 
+void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
+{
+    notifier_list_add(&mr->iommu_notify, n);
+}
+
+void memory_region_unregister_iommu_notifier(Notifier *n)
+{
+    notifier_remove(n);
+}
+
+void memory_region_notify_iommu(MemoryRegion *mr,
+                                IOMMUTLBEntry entry)
+{
+    assert(memory_region_is_iommu(mr));
+    notifier_list_notify(&mr->iommu_notify, &entry);
+}
+
 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
 {
     uint8_t mask = 1 << client;
Signature · 1 line
1.8.1.4
#81 lore raw

Re: [Qemu-devel] [PATCH 21/30] memory: Add iommu map/unmap notifiers

Quoted 10 lines · Paolo Bonzini

From: David Gibson <[email protected]>

This patch adds a NotifierList to MemoryRegions which represent IOMMUs allowing other parts of the code to register interest in mappings or unmappings from the IOMMU. All IOMMU implementations will need to call memory_region_notify_iommu() to inform those waiting on the notifier list, whenever an IOMMU mapping is made or removed.

include/exec/memory.h static inline bool memory_region_is_romd(MemoryRegion *mr) · line 431

Quoted 7 lines
include/exec/memory.h +4 -0
@@ -423,6 +425,11 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)
  */
 bool memory_region_is_iommu(MemoryRegion *mr);
 
+void memory_region_notify_iommu(MemoryRegion *mr,
+                                IOMMUTLBEntry entry);
+void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
+void memory_region_unregister_iommu_notifier(Notifier *n);

These all need doc comments.

The code looks good.

thanks
-- PMM

#82 lore raw

Re: [Qemu-devel] [PATCH 21/30] memory: Add iommu map/unmap notifiers

Il 23/05/2013 20:27, Peter Maydell ha scritto:

Quoted 14 lines · Paolo Bonzini
Quoted 13 lines

From: David Gibson <[email protected]>

This patch adds a NotifierList to MemoryRegions which represent IOMMUs allowing other parts of the code to register interest in mappings or unmappings from the IOMMU. All IOMMU implementations will need to call memory_region_notify_iommu() to inform those waiting on the notifier list, whenever an IOMMU mapping is made or removed.

include/exec/memory.h +0 -0
@@ -423,6 +425,11 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)
  */
 bool memory_region_is_iommu(MemoryRegion *mr);
 
Quoted 6 lines

include/exec/memory.h static inline bool memory_region_is_romd(MemoryRegion *mr) · line 431

Quoted 4 lines
include/exec/memory.h +4 -0
+void memory_region_notify_iommu(MemoryRegion *mr,
+                                IOMMUTLBEntry entry);
+void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
+void memory_region_unregister_iommu_notifier(Notifier *n);

These all need doc comments.

David, can you provide them? (If not as a patch, just an inline reply to this email will do).

Paolo

#83 lore raw

Re: [Qemu-devel] [PATCH 21/30] memory: Add iommu map/unmap notifiers

Quoted 22 lines · Paolo Bonzini

Il 23/05/2013 20:27, Peter Maydell ha scritto:

Quoted 14 lines · Paolo Bonzini
Quoted 13 lines

From: David Gibson <[email protected]>

This patch adds a NotifierList to MemoryRegions which represent IOMMUs allowing other parts of the code to register interest in mappings or unmappings from the IOMMU. All IOMMU implementations will need to call memory_region_notify_iommu() to inform those waiting on the notifier list, whenever an IOMMU mapping is made or removed.

include/exec/memory.h +0 -0
@@ -423,6 +425,11 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)
  */
 bool memory_region_is_iommu(MemoryRegion *mr);
 
Quoted 6 lines

include/exec/memory.h static inline bool memory_region_is_romd(MemoryRegion *mr) · line 431

Quoted 4 lines
include/exec/memory.h +4 -0
+void memory_region_notify_iommu(MemoryRegion *mr,
+                                IOMMUTLBEntry entry);
+void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
+void memory_region_unregister_iommu_notifier(Notifier *n);

These all need doc comments.

Quoted 2 lines

David, can you provide them? (If not as a patch, just an inline reply to this email will do).

I'll see what I can do. Working on this is no longer my day job, so it may be a little while...

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#84 lore raw

PATCH 22/30 vfio: abort if an emulated iommu is used

In reply to Paolo Bonzini

From: Avi Kivity <[email protected]>

vfio doesn't support guest iommus yet, indicate it to the user by gently depositing a core on their disk.

---
 hw/misc/vfio.c | 2 ++
 1 file changed, 2 insertions(+)
hw/misc/vfio.c +2 -0
index 693a9ff..178dd11 100644
@@ -1939,6 +1939,8 @@ static void vfio_listener_region_add(MemoryListener *listener,
     void *vaddr;
     int ret;
 
+    assert(!memory_region_is_iommu(section->mr));
+
     if (vfio_listener_skipped_section(section)) {
         DPRINTF("SKIPPING region_add %"HWADDR_PRIx" - %"PRIx64"\n",
                 section->offset_within_address_space,
Signature · 1 line
1.8.1.4
#85 lore raw

PATCH 23/30 spapr: convert TCE API to use an opaque type

In reply to Paolo Bonzini

The TCE table is currently returned as a DMAContext, and non-type-safe APIs are called later passing back the DMAContext. Since we want to move away from DMAContext, use an opaque type instead, and add an accessor to retrieve the DMAContext from it.

---
 hw/ppc/spapr_iommu.c        | 54 ++++++++++++++++++---------------------------
 hw/ppc/spapr_pci.c          |  8 +++----
 hw/ppc/spapr_vio.c          | 13 ++++++-----
 include/hw/pci-host/spapr.h |  2 +-
 include/hw/ppc/spapr.h      | 12 +++++-----
 include/hw/ppc/spapr_vio.h  |  1 +
 6 files changed, 42 insertions(+), 48 deletions(-)
hw/ppc/spapr_iommu.c +22 -32
index e1fe941..7a507e0 100644
@@ -36,8 +36,6 @@ enum sPAPRTCEAccess {
     SPAPR_TCE_RW = 3,
 };
 
-typedef struct sPAPRTCETable sPAPRTCETable;
-
 struct sPAPRTCETable {
     DMAContext dma;
     uint32_t liobn;
@@ -122,7 +120,7 @@ static int spapr_tce_translate(DMAContext *dma,
     return 0;
 }
 
-DMAContext *spapr_tce_new_dma_context(uint32_t liobn, size_t window_size)
+sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size)
 {
     sPAPRTCETable *tcet;
 
@@ -155,43 +153,40 @@ DMAContext *spapr_tce_new_dma_context(uint32_t liobn, size_t window_size)
     }
 
 #ifdef DEBUG_TCE
-    fprintf(stderr, "spapr_iommu: New TCE table, liobn=0x%x, context @ %p, "
-            "table @ %p, fd=%d\n", liobn, &tcet->dma, tcet->table, tcet->fd);
+    fprintf(stderr, "spapr_iommu: New TCE table @ %p, liobn=0x%x, "
+            "table @ %p, fd=%d\n", tcet, liobn, tcet->table, tcet->fd);
 #endif
 
     QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
 
-    return &tcet->dma;
+    return tcet;
 }
 
-void spapr_tce_free(DMAContext *dma)
+void spapr_tce_free(sPAPRTCETable *tcet)
 {
+    QLIST_REMOVE(tcet, list);
 
-    if (dma) {
-        sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
-
-        QLIST_REMOVE(tcet, list);
-
-        if (!kvm_enabled() ||
-            (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
-                                     tcet->window_size) != 0)) {
-            g_free(tcet->table);
-        }
-
-        g_free(tcet);
+    if (!kvm_enabled() ||
+        (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
+                                 tcet->window_size) != 0)) {
+        g_free(tcet->table);
     }
+
+    g_free(tcet);
 }
 
-void spapr_tce_set_bypass(DMAContext *dma, bool bypass)
+DMAContext *spapr_tce_get_dma(sPAPRTCETable *tcet)
 {
-    sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
+    return &tcet->dma;
+}
 
+void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
+{
     tcet->bypass = bypass;
 }
 
-void spapr_tce_reset(DMAContext *dma)
+void spapr_tce_reset(sPAPRTCETable *tcet)
 {
-    sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
     size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
         * sizeof(sPAPRTCE);
 
@@ -277,17 +272,12 @@ int spapr_dma_dt(void *fdt, int node_off, const char *propname,
 }
 
 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
-                      DMAContext *iommu)
+                      sPAPRTCETable *tcet)
 {
-    if (!iommu) {
+    if (!tcet) {
         return 0;
     }
 
-    if (iommu->translate == spapr_tce_translate) {
-        sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, iommu);
-        return spapr_dma_dt(fdt, node_off, propname,
-                tcet->liobn, 0, tcet->window_size);
-    }
-
-    return -1;
+    return spapr_dma_dt(fdt, node_off, propname,
+                        tcet->liobn, 0, tcet->window_size);
 }
hw/ppc/spapr_pci.c +4 -4
index 62ff323..eb64a8f 100644
@@ -511,7 +511,7 @@ static DMAContext *spapr_pci_dma_context_fn(PCIBus *bus, void *opaque,
 {
     sPAPRPHBState *phb = opaque;
 
-    return phb->dma;
+    return spapr_tce_get_dma(phb->tcet);
 }
 
 static int spapr_phb_init(SysBusDevice *s)
@@ -646,8 +646,8 @@ static int spapr_phb_init(SysBusDevice *s)
 
     sphb->dma_window_start = 0;
     sphb->dma_window_size = 0x40000000;
-    sphb->dma = spapr_tce_new_dma_context(sphb->dma_liobn, sphb->dma_window_size);
-    if (!sphb->dma) {
+    sphb->tcet = spapr_tce_new_table(sphb->dma_liobn, sphb->dma_window_size);
+    if (!sphb->tcet) {
         fprintf(stderr, "Unable to create TCE table for %s\n", sphb->dtbusname);
         return -1;
     }
@@ -676,7 +676,7 @@ static void spapr_phb_reset(DeviceState *qdev)
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
 
     /* Reset the IOMMU state */
-    spapr_tce_reset(sphb->dma);
+    spapr_tce_reset(sphb->tcet);
 }
 
 static Property spapr_phb_properties[] = {
hw/ppc/spapr_vio.c +7 -6
index 1405c32..a06ac94 100644
@@ -145,7 +145,7 @@ static int vio_make_devnode(VIOsPAPRDevice *dev,
         }
     }
 
-    ret = spapr_tcet_dma_dt(fdt, node_off, "ibm,my-dma-window", dev->dma);
+    ret = spapr_tcet_dma_dt(fdt, node_off, "ibm,my-dma-window", dev->tcet);
     if (ret < 0) {
         return ret;
     }
@@ -319,8 +319,8 @@ int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
 
 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
 {
-    if (dev->dma) {
-        spapr_tce_reset(dev->dma);
+    if (dev->tcet) {
+        spapr_tce_reset(dev->tcet);
     }
     free_crq(dev);
 }
@@ -345,12 +345,12 @@ static void rtas_set_tce_bypass(sPAPREnvironment *spapr, uint32_t token,
         return;
     }
 
-    if (!dev->dma) {
+    if (!dev->tcet) {
         rtas_st(rets, 0, -3);
         return;
     }
 
-    spapr_tce_set_bypass(dev->dma, !!enable);
+    spapr_tce_set_bypass(dev->tcet, !!enable);
 
     rtas_st(rets, 0, 0);
 }
@@ -457,7 +457,8 @@ static int spapr_vio_busdev_init(DeviceState *qdev)
 
     if (pc->rtce_window_size) {
         uint32_t liobn = SPAPR_VIO_BASE_LIOBN | dev->reg;
-        dev->dma = spapr_tce_new_dma_context(liobn, pc->rtce_window_size);
+        dev->tcet = spapr_tce_new_table(liobn, pc->rtce_window_size);
+        dev->dma = spapr_tce_get_dma(dev->tcet);
     }
 
     return pc->init(dev);
include/hw/pci-host/spapr.h +1 -1
index b21080c..653dd40 100644
@@ -49,7 +49,7 @@ typedef struct sPAPRPHBState {
     uint32_t dma_liobn;
     uint64_t dma_window_start;
     uint64_t dma_window_size;
-    DMAContext *dma;
+    sPAPRTCETable *tcet;
 
     struct {
         uint32_t irq;
include/hw/ppc/spapr.h +7 -5
index 864bee9..e8d617b 100644
@@ -342,17 +342,19 @@ typedef struct sPAPRTCE {
 
 #define RTAS_ERROR_LOG_MAX      2048
 
+typedef struct sPAPRTCETable sPAPRTCETable;
 
 void spapr_iommu_init(void);
 void spapr_events_init(sPAPREnvironment *spapr);
 void spapr_events_fdt_skel(void *fdt, uint32_t epow_irq);
-DMAContext *spapr_tce_new_dma_context(uint32_t liobn, size_t window_size);
-void spapr_tce_free(DMAContext *dma);
-void spapr_tce_reset(DMAContext *dma);
-void spapr_tce_set_bypass(DMAContext *dma, bool bypass);
+sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size);
+DMAContext *spapr_tce_get_dma(sPAPRTCETable *tcet);
+void spapr_tce_free(sPAPRTCETable *tcet);
+void spapr_tce_reset(sPAPRTCETable *tcet);
+void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass);
 int spapr_dma_dt(void *fdt, int node_off, const char *propname,
                  uint32_t liobn, uint64_t window, uint32_t size);
 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
-                      DMAContext *dma);
+                      sPAPRTCETable *tcet);
 
 #endif /* !defined (__HW_SPAPR_H__) */
include/hw/ppc/spapr_vio.h +1 -0
index f98ec0a..56f2821 100644
@@ -63,6 +63,7 @@ struct VIOsPAPRDevice {
     uint32_t irq;
     target_ulong signal_state;
     VIOsPAPR_CRQ crq;
+    sPAPRTCETable *tcet;
     DMAContext *dma;
 };
 
Signature · 1 line
1.8.1.4
#86 lore raw

PATCH 24/30 spapr: make IOMMU translation go through IOMMUTLBEntry

In reply to Paolo Bonzini

The next step is to introduce the translation code that will be used for IOMMU MemoryRegions, but still do the actual translation in a DMAContext.

---
 hw/ppc/spapr_iommu.c | 59 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 25 deletions(-)
hw/ppc/spapr_iommu.c +34 -25
index 7a507e0..f6d74fe 100644
@@ -68,15 +68,8 @@ static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
     return NULL;
 }
 
-static int spapr_tce_translate(DMAContext *dma,
-                               dma_addr_t addr,
-                               hwaddr *paddr,
-                               hwaddr *len,
-                               DMADirection dir)
+static IOMMUTLBEntry spapr_tce_translate_iommu(sPAPRTCETable *tcet, hwaddr addr)
 {
-    sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
-    enum sPAPRTCEAccess access = (dir == DMA_DIRECTION_FROM_DEVICE)
-        ? SPAPR_TCE_WO : SPAPR_TCE_RO;
     uint64_t tce;
 
 #ifdef DEBUG_TCE
@@ -85,9 +78,12 @@ static int spapr_tce_translate(DMAContext *dma,
 #endif
 
     if (tcet->bypass) {
-        *paddr = addr;
-        *len = (hwaddr)-1;
-        return 0;
+        return (IOMMUTLBEntry) {
+            .iova = 0,
+            .translated_addr = 0,
+            .addr_mask = ~(hwaddr)0,
+            .perm = IOMMU_RW,
+        };
     }
 
     /* Check if we are in bound */
@@ -95,28 +91,41 @@ static int spapr_tce_translate(DMAContext *dma,
 #ifdef DEBUG_TCE
         fprintf(stderr, "spapr_tce_translate out of bounds\n");
 #endif
-        return -EFAULT;
+        return (IOMMUTLBEntry) { .perm = IOMMU_NONE };
     }
 
     tce = tcet->table[addr >> SPAPR_TCE_PAGE_SHIFT].tce;
 
-    /* Check TCE */
-    if (!(tce & access)) {
-        return -EPERM;
-    }
+#ifdef DEBUG_TCE
+    fprintf(stderr, " ->  *paddr=0x%llx, *len=0x%llx\n",
+            (tce & ~SPAPR_TCE_PAGE_MASK), SPAPR_TCE_PAGE_MASK + 1);
+#endif
 
-    /* How much til end of page ? */
-    *len = ((~addr) & SPAPR_TCE_PAGE_MASK) + 1;
+    return (IOMMUTLBEntry) {
+        .iova = addr & ~SPAPR_TCE_PAGE_MASK,
+        .translated_addr = tce & ~SPAPR_TCE_PAGE_MASK,
+        .addr_mask = SPAPR_TCE_PAGE_MASK,
+        .perm = tce,
+    };
+}
 
-    /* Translate */
-    *paddr = (tce & ~SPAPR_TCE_PAGE_MASK) |
-        (addr & SPAPR_TCE_PAGE_MASK);
+static int spapr_tce_translate(DMAContext *dma,
+                               dma_addr_t addr,
+                               hwaddr *paddr,
+                               hwaddr *len,
+                               DMADirection dir)
+ {
+    sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
+    bool is_write = (dir == DMA_DIRECTION_FROM_DEVICE);
+    IOMMUTLBEntry entry = spapr_tce_translate_iommu(tcet, addr);
 
-#ifdef DEBUG_TCE
-    fprintf(stderr, " ->  *paddr=0x" TARGET_FMT_plx ", *len=0x"
-            TARGET_FMT_plx "\n", *paddr, *len);
-#endif
+    if (!(entry.perm & (1 << is_write))) {
+        return -EPERM;
+    }
 
+    /* Translate */
+    *paddr = entry.translated_addr | (addr & entry.addr_mask);
+    *len = (addr | entry.addr_mask) - addr + 1;
     return 0;
 }
 
Signature · 1 line
1.8.1.4
#87 lore raw

PATCH 25/30 spapr: use memory core for iommu support

In reply to Paolo Bonzini

Now we can stop using a "translating" DMAContext, but we do not yet modify the sPAPRTCETable users to get an AddressSpace; they keep using the table via a DMAContext.

---
 hw/ppc/spapr_iommu.c   | 49 +++++++++++++++++++++++++++----------------------
 include/hw/ppc/spapr.h |  1 +
 2 files changed, 28 insertions(+), 22 deletions(-)
hw/ppc/spapr_iommu.c +27 -22
index f6d74fe..f7b1b18 100644
@@ -37,12 +37,16 @@ enum sPAPRTCEAccess {
 };
 
 struct sPAPRTCETable {
+    /* temporary until everyone has its own AddressSpace */
     DMAContext dma;
+    AddressSpace as;
+
     uint32_t liobn;
     uint32_t window_size;
     sPAPRTCE *table;
     bool bypass;
     int fd;
+    MemoryRegion iommu;
     QLIST_ENTRY(sPAPRTCETable) list;
 };
 
@@ -68,8 +72,9 @@ static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
     return NULL;
 }
 
-static IOMMUTLBEntry spapr_tce_translate_iommu(sPAPRTCETable *tcet, hwaddr addr)
+static IOMMUTLBEntry spapr_tce_translate_iommu(MemoryRegion *iommu, hwaddr addr)
 {
+    sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
     uint64_t tce;
 
 #ifdef DEBUG_TCE
@@ -109,25 +114,9 @@ static IOMMUTLBEntry spapr_tce_translate_iommu(sPAPRTCETable *tcet, hwaddr addr)
     };
 }
 
-static int spapr_tce_translate(DMAContext *dma,
-                               dma_addr_t addr,
-                               hwaddr *paddr,
-                               hwaddr *len,
-                               DMADirection dir)
- {
-    sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
-    bool is_write = (dir == DMA_DIRECTION_FROM_DEVICE);
-    IOMMUTLBEntry entry = spapr_tce_translate_iommu(tcet, addr);
-
-    if (!(entry.perm & (1 << is_write))) {
-        return -EPERM;
-    }
-
-    /* Translate */
-    *paddr = entry.translated_addr | (addr & entry.addr_mask);
-    *len = (addr | entry.addr_mask) - addr + 1;
-    return 0;
-}
+static MemoryRegionIOMMUOps spapr_iommu_ops = {
+    .translate = spapr_tce_translate_iommu,
+};
 
 sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size)
 {
@@ -144,8 +133,6 @@ sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size)
     }
 
     tcet = g_malloc0(sizeof(*tcet));
-    dma_context_init(&tcet->dma, &address_space_memory, spapr_tce_translate, NULL, NULL);
-
     tcet->liobn = liobn;
     tcet->window_size = window_size;
 
@@ -166,6 +153,12 @@ sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size)
             "table @ %p, fd=%d\n", tcet, liobn, tcet->table, tcet->fd);
 #endif
 
+    memory_region_init_iommu(&tcet->iommu, &spapr_iommu_ops,
+                             &address_space_memory,
+                             "iommu-spapr", INT64_MAX);
+    address_space_init(&tcet->as, &tcet->iommu);
+    dma_context_init(&tcet->dma, &tcet->as, NULL, NULL, NULL);
+
     QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
 
     return tcet;
@@ -189,6 +182,11 @@ DMAContext *spapr_tce_get_dma(sPAPRTCETable *tcet)
     return &tcet->dma;
 }
 
+MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
+{
+    return &tcet->iommu;
+}
+
 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass)
 {
     tcet->bypass = bypass;
@@ -207,6 +205,7 @@ static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
                                 target_ulong tce)
 {
     sPAPRTCE *tcep;
+    IOMMUTLBEntry entry;
 
     if (ioba >= tcet->window_size) {
         hcall_dprintf("spapr_vio_put_tce on out-of-bounds IOBA 0x"
@@ -217,6 +216,12 @@ static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
     tcep = tcet->table + (ioba >> SPAPR_TCE_PAGE_SHIFT);
     tcep->tce = tce;
 
+    entry.iova = ioba & ~SPAPR_TCE_PAGE_MASK;
+    entry.translated_addr = tce & ~SPAPR_TCE_PAGE_MASK;
+    entry.addr_mask = SPAPR_TCE_PAGE_MASK;
+    entry.perm = tce;
+    memory_region_notify_iommu(&tcet->iommu, entry);
+
     return H_SUCCESS;
 }
 
include/hw/ppc/spapr.h +1 -0
index e8d617b..142abb7 100644
@@ -349,6 +349,7 @@ void spapr_events_init(sPAPREnvironment *spapr);
 void spapr_events_fdt_skel(void *fdt, uint32_t epow_irq);
 sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size);
 DMAContext *spapr_tce_get_dma(sPAPRTCETable *tcet);
+MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet);
 void spapr_tce_free(sPAPRTCETable *tcet);
 void spapr_tce_reset(sPAPRTCETable *tcet);
 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass);
Signature · 1 line
1.8.1.4
#88 lore raw

PATCH 26/30 dma: eliminate old-style IOMMU support

In reply to Paolo Bonzini

The translate function in the DMAContext is now always NULL. Remove every reference to it.

---
 dma-helpers.c        | 178 +++------------------------------------------------
 exec.c               |   3 +-
 hw/pci/pci.c         |   3 +-
 hw/ppc/spapr_iommu.c |   2 +-
 include/sysemu/dma.h |  79 ++++-------------------
 5 files changed, 26 insertions(+), 239 deletions(-)
dma-helpers.c +10 -168
index 2962b69..c53705a 100644
@@ -14,32 +14,26 @@
 
 /* #define DEBUG_IOMMU */
 
-static void do_dma_memory_set(AddressSpace *as,
-                              dma_addr_t addr, uint8_t c, dma_addr_t len)
+int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len)
 {
+    AddressSpace *as = dma->as;
+
+    dma_barrier(dma, DMA_DIRECTION_FROM_DEVICE);
+
 #define FILLBUF_SIZE 512
     uint8_t fillbuf[FILLBUF_SIZE];
     int l;
+    bool error = false;
 
     memset(fillbuf, c, FILLBUF_SIZE);
     while (len > 0) {
         l = len < FILLBUF_SIZE ? len : FILLBUF_SIZE;
-        address_space_rw(as, addr, fillbuf, l, true);
+        error |= address_space_rw(as, addr, fillbuf, l, true);
         len -= l;
         addr += l;
     }
-}
 
-int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len)
-{
-    dma_barrier(dma, DMA_DIRECTION_FROM_DEVICE);
-
-    if (dma_has_iommu(dma)) {
-        return iommu_dma_memory_set(dma, addr, c, len);
-    }
-    do_dma_memory_set(dma->as, addr, c, len);
-
-    return 0;
+    return error;
 }
 
 void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, DMAContext *dma)
@@ -278,162 +272,10 @@ void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
     bdrv_acct_start(bs, cookie, sg->size, type);
 }
 
-bool iommu_dma_memory_valid(DMAContext *dma, dma_addr_t addr, dma_addr_t len,
-                            DMADirection dir)
+void dma_context_init(DMAContext *dma, AddressSpace *as)
 {
-    hwaddr paddr, plen;
-
 #ifdef DEBUG_IOMMU
-    fprintf(stderr, "dma_memory_check context=%p addr=0x" DMA_ADDR_FMT
-            " len=0x" DMA_ADDR_FMT " dir=%d\n", dma, addr, len, dir);
-#endif
-
-    while (len) {
-        if (dma->translate(dma, addr, &paddr, &plen, dir) != 0) {
-            return false;
-        }
-
-        /* The translation might be valid for larger regions. */
-        if (plen > len) {
-            plen = len;
-        }
-
-        if (!address_space_valid(dma->as, paddr, len,
-                                 dir == DMA_DIRECTION_FROM_DEVICE)) {
-            return false;
-        }
-
-        len -= plen;
-        addr += plen;
-    }
-
-    return true;
-}
-
-int iommu_dma_memory_rw(DMAContext *dma, dma_addr_t addr,
-                        void *buf, dma_addr_t len, DMADirection dir)
-{
-    hwaddr paddr, plen;
-    int err;
-
-#ifdef DEBUG_IOMMU
-    fprintf(stderr, "dma_memory_rw context=%p addr=0x" DMA_ADDR_FMT " len=0x"
-            DMA_ADDR_FMT " dir=%d\n", dma, addr, len, dir);
-#endif
-
-    while (len) {
-        err = dma->translate(dma, addr, &paddr, &plen, dir);
-        if (err) {
-	    /*
-             * In case of failure on reads from the guest, we clean the
-             * destination buffer so that a device that doesn't test
-             * for errors will not expose qemu internal memory.
-	     */
-	    memset(buf, 0, len);
-            return -1;
-        }
-
-        /* The translation might be valid for larger regions. */
-        if (plen > len) {
-            plen = len;
-        }
-
-        address_space_rw(dma->as, paddr, buf, plen, dir == DMA_DIRECTION_FROM_DEVICE);
-
-        len -= plen;
-        addr += plen;
-        buf += plen;
-    }
-
-    return 0;
-}
-
-int iommu_dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c,
-                         dma_addr_t len)
-{
-    hwaddr paddr, plen;
-    int err;
-
-#ifdef DEBUG_IOMMU
-    fprintf(stderr, "dma_memory_set context=%p addr=0x" DMA_ADDR_FMT
-            " len=0x" DMA_ADDR_FMT "\n", dma, addr, len);
-#endif
-
-    while (len) {
-        err = dma->translate(dma, addr, &paddr, &plen,
-                             DMA_DIRECTION_FROM_DEVICE);
-        if (err) {
-            return err;
-        }
-
-        /* The translation might be valid for larger regions. */
-        if (plen > len) {
-            plen = len;
-        }
-
-        do_dma_memory_set(dma->as, paddr, c, plen);
-
-        len -= plen;
-        addr += plen;
-    }
-
-    return 0;
-}
-
-void dma_context_init(DMAContext *dma, AddressSpace *as, DMATranslateFunc translate,
-                      DMAMapFunc map, DMAUnmapFunc unmap)
-{
-#ifdef DEBUG_IOMMU
-    fprintf(stderr, "dma_context_init(%p, %p, %p, %p)\n",
-            dma, translate, map, unmap);
+    fprintf(stderr, "dma_context_init(%p -> %p)\n", dma, as);
 #endif
     dma->as = as;
-    dma->translate = translate;
-    dma->map = map;
-    dma->unmap = unmap;
-}
-
-void *iommu_dma_memory_map(DMAContext *dma, dma_addr_t addr, dma_addr_t *len,
-                           DMADirection dir)
-{
-    int err;
-    hwaddr paddr, plen;
-    void *buf;
-
-    if (dma->map) {
-        return dma->map(dma, addr, len, dir);
-    }
-
-    plen = *len;
-    err = dma->translate(dma, addr, &paddr, &plen, dir);
-    if (err) {
-        return NULL;
-    }
-
-    /*
-     * If this is true, the virtual region is contiguous,
-     * but the translated physical region isn't. We just
-     * clamp *len, much like address_space_map() does.
-     */
-    if (plen < *len) {
-        *len = plen;
-    }
-
-    buf = address_space_map(dma->as, paddr, &plen, dir == DMA_DIRECTION_FROM_DEVICE);
-    *len = plen;
-
-    return buf;
-}
-
-void iommu_dma_memory_unmap(DMAContext *dma, void *buffer, dma_addr_t len,
-                            DMADirection dir, dma_addr_t access_len)
-{
-    if (dma->unmap) {
-        dma->unmap(dma, buffer, len, dir, access_len);
-        return;
-    }
-
-    address_space_unmap(dma->as, buffer, len, dir == DMA_DIRECTION_FROM_DEVICE,
-                        access_len);
-
 }
exec.c +1 -2
index b7266c8..4c43fdd 100644
@@ -1859,8 +1859,7 @@ static void memory_map_init(void)
     memory_listener_register(&io_memory_listener, &address_space_io);
     memory_listener_register(&tcg_memory_listener, &address_space_memory);
 
-    dma_context_init(&dma_context_memory, &address_space_memory,
-                     NULL, NULL, NULL);
+    dma_context_init(&dma_context_memory, &address_space_memory);
 }
 
 MemoryRegion *get_system_memory(void)
hw/pci/pci.c +2 -1
index bb3879b..c9d585c 100644
@@ -814,8 +814,9 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
         memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
         address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
         pci_dev->dma = g_new(DMAContext, 1);
-        dma_context_init(pci_dev->dma, &pci_dev->bus_master_as, NULL, NULL, NULL);
+        dma_context_init(pci_dev->dma, &pci_dev->bus_master_as);
     }
+
     pci_dev->devfn = devfn;
     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
     pci_dev->irq_state = 0;
hw/ppc/spapr_iommu.c +1 -1
index f7b1b18..8971d72 100644
@@ -157,7 +157,7 @@ sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size)
                              &address_space_memory,
                              "iommu-spapr", INT64_MAX);
     address_space_init(&tcet->as, &tcet->iommu);
-    dma_context_init(&tcet->dma, &tcet->as, NULL, NULL, NULL);
+    dma_context_init(&tcet->dma, &tcet->as);
 
     QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
 
include/sysemu/dma.h +12 -67

130 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#90 lore raw

PATCH 27/30 pci: use memory core for iommu support

In reply to Paolo Bonzini

From: Avi Kivity <[email protected]>

Use the new iommu support in the memory core for iommu support. The only user, spapr, is also converted, but it still provides a DMAContext interface until the non-PCI bits switch to AddressSpace.

Cc Michael S. Tsirkin <[email protected]>

[ Do not calls memory_region_del_subregion() on the device's
bus_master_enable_region, it is an alias; return an AddressSpace from the IOMMU hook and remove the destructor hook. - David Gibson ]

---
 hw/pci/pci.c                | 46 ++++++++++++++++++++++-----------------------
 hw/ppc/spapr_pci.c          |  9 +++++----
 include/hw/pci-host/spapr.h |  1 +
 include/hw/pci/pci.h        |  4 ++--
 include/hw/pci/pci_bus.h    |  4 ++--
 5 files changed, 33 insertions(+), 31 deletions(-)
hw/pci/pci.c +23 -23
index c9d585c..9e6bd77 100644
@@ -279,6 +279,12 @@ int pci_find_domain(const PCIBus *bus)
     return -1;
 }
 
+static AddressSpace *pci_default_address_space(PCIBus *bus, void *opaque, int devfn)
+{
+    /* FIXME: inherit memory region from bus creator */
+    return &address_space_memory;
+}
+
 static void pci_bus_init(PCIBus *bus, DeviceState *parent,
                          const char *name,
                          MemoryRegion *address_space_mem,
@@ -289,6 +295,7 @@ static void pci_bus_init(PCIBus *bus, DeviceState *parent,
     bus->devfn_min = devfn_min;
     bus->address_space_mem = address_space_mem;
     bus->address_space_io = address_space_io;
+    pci_setup_iommu(bus, pci_default_address_space, NULL);
 
     /* host bridge */
     QLIST_INIT(&bus->child);
@@ -786,6 +793,7 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
     PCIConfigReadFunc *config_read = pc->config_read;
     PCIConfigWriteFunc *config_write = pc->config_write;
+    AddressSpace *dma_as;
 
     if (devfn < 0) {
         for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
@@ -801,21 +809,15 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
                      PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name);
         return NULL;
     }
+
     pci_dev->bus = bus;
-    if (bus->dma_context_fn) {
-        pci_dev->dma = bus->dma_context_fn(bus, bus->dma_context_opaque, devfn);
-    } else {
-        /* FIXME: Make dma_context_fn use MemoryRegions instead, so this path is
-         * taken unconditionally */
-        /* FIXME: inherit memory region from bus creator */
-        memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
-                                 get_system_memory(), 0,
-                                 memory_region_size(get_system_memory()));
-        memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
-        address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
-        pci_dev->dma = g_new(DMAContext, 1);
-        dma_context_init(pci_dev->dma, &pci_dev->bus_master_as);
-    }
+    dma_as = bus->iommu_fn(bus, bus->iommu_opaque, devfn);
+    memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
+                             dma_as->root, 0, memory_region_size(dma_as->root));
+    memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
+    address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
+    pci_dev->dma = g_new(DMAContext, 1);
+    dma_context_init(pci_dev->dma, &pci_dev->bus_master_as);
 
     pci_dev->devfn = devfn;
     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
@@ -870,12 +872,10 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);
 
-    if (!pci_dev->bus->dma_context_fn) {
-        address_space_destroy(&pci_dev->bus_master_as);
-        memory_region_destroy(&pci_dev->bus_master_enable_region);
-        g_free(pci_dev->dma);
-        pci_dev->dma = NULL;
-    }
+    address_space_destroy(&pci_dev->bus_master_as);
+    memory_region_destroy(&pci_dev->bus_master_enable_region);
+    g_free(pci_dev->dma);
+    pci_dev->dma = NULL;
 }
 
 static void pci_unregister_io_regions(PCIDevice *pci_dev)
@@ -2232,10 +2232,10 @@ static void pci_device_class_init(ObjectClass *klass, void *data)
     k->props = pci_props;
 }
 
-void pci_setup_iommu(PCIBus *bus, PCIDMAContextFunc fn, void *opaque)
+void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
 {
-    bus->dma_context_fn = fn;
-    bus->dma_context_opaque = opaque;
+    bus->iommu_fn = fn;
+    bus->iommu_opaque = opaque;
 }
 
 static const TypeInfo pci_device_type_info = {
hw/ppc/spapr_pci.c +5 -4
index eb64a8f..762db62 100644
@@ -506,12 +506,11 @@ static const MemoryRegionOps spapr_msi_ops = {
 /*
  * PHB PCI device
  */
-static DMAContext *spapr_pci_dma_context_fn(PCIBus *bus, void *opaque,
-                                            int devfn)
+static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
 {
     sPAPRPHBState *phb = opaque;
 
-    return spapr_tce_get_dma(phb->tcet);
+    return &phb->iommu_as;
 }
 
 static int spapr_phb_init(SysBusDevice *s)
@@ -651,7 +650,9 @@ static int spapr_phb_init(SysBusDevice *s)
         fprintf(stderr, "Unable to create TCE table for %s\n", sphb->dtbusname);
         return -1;
     }
-    pci_setup_iommu(bus, spapr_pci_dma_context_fn, sphb);
+    address_space_init(&sphb->iommu_as, spapr_tce_get_iommu(sphb->tcet),
+                       sphb->dtbusname);
+    pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb);
 
     QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
 
include/hw/pci-host/spapr.h +1 -0
index 653dd40..1e23dbf 100644
@@ -50,6 +50,7 @@ typedef struct sPAPRPHBState {
     uint64_t dma_window_start;
     uint64_t dma_window_size;
     sPAPRTCETable *tcet;
+    AddressSpace iommu_as;
 
     struct {
         uint32_t irq;
include/hw/pci/pci.h +2 -2
index 8d075ab..3a85bce 100644
@@ -401,9 +401,9 @@ int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
 
 void pci_device_deassert_intx(PCIDevice *dev);
 
-typedef DMAContext *(*PCIDMAContextFunc)(PCIBus *, void *, int);
+typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);
 
-void pci_setup_iommu(PCIBus *bus, PCIDMAContextFunc fn, void *opaque);
+void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque);
 
 static inline void
 pci_set_byte(uint8_t *config, uint8_t val)
include/hw/pci/pci_bus.h +2 -2
index 6ee443c..66762f6 100644
@@ -10,8 +10,8 @@
 
 struct PCIBus {
     BusState qbus;
-    PCIDMAContextFunc dma_context_fn;
-    void *dma_context_opaque;
+    PCIIOMMUFunc iommu_fn;
+    void *iommu_opaque;
     uint8_t devfn_min;
     pci_set_irq_fn set_irq;
     pci_map_irq_fn map_irq;
Signature · 1 line
1.8.1.4
Reviewed-by on v1 27/30
#91 lore raw

Re: [Qemu-devel] [PATCH 27/30] pci: use memory core for iommu support

Quoted 6 lines · Paolo Bonzini

From: Avi Kivity <[email protected]>

Use the new iommu support in the memory core for iommu support. The only user, spapr, is also converted, but it still provides a DMAContext interface until the non-PCI bits switch to AddressSpace.

Quoted 7 lines
Cc Michael S. Tsirkin <[email protected]>

[ Do not calls memory_region_del_subregion() on the device's
bus_master_enable_region, it is an alias; return an AddressSpace from the IOMMU hook and remove the destructor hook. - David Gibson ]

More >80column excesses. Otherwise

-- PMM

Reviewed-by on v1 27/30
#92 lore raw

Re: [Qemu-devel] [PATCH 27/30] pci: use memory core for iommu support

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini

From: Avi Kivity <[email protected]>

Use the new iommu support in the memory core for iommu support. The only user, spapr, is also converted, but it still provides a DMAContext interface until the non-PCI bits switch to AddressSpace.

Quoted 7 lines
Cc Michael S. Tsirkin <[email protected]>

[ Do not calls memory_region_del_subregion() on the device's
bus_master_enable_region, it is an alias; return an AddressSpace from the IOMMU hook and remove the destructor hook. - David Gibson ]

Looks good to me.

Quoted 178 lines
---
 hw/pci/pci.c                | 46 ++++++++++++++++++++++-----------------------
 hw/ppc/spapr_pci.c          |  9 +++++----
 include/hw/pci-host/spapr.h |  1 +
 include/hw/pci/pci.h        |  4 ++--
 include/hw/pci/pci_bus.h    |  4 ++--
 5 files changed, 33 insertions(+), 31 deletions(-)
hw/pci/pci.c +23 -23
index c9d585c..9e6bd77 100644
@@ -279,6 +279,12 @@ int pci_find_domain(const PCIBus *bus)
     return -1;
 }
 
+static AddressSpace *pci_default_address_space(PCIBus *bus, void *opaque, int devfn)
+{
+    /* FIXME: inherit memory region from bus creator */
+    return &address_space_memory;
+}
+
 static void pci_bus_init(PCIBus *bus, DeviceState *parent,
                          const char *name,
                          MemoryRegion *address_space_mem,
@@ -289,6 +295,7 @@ static void pci_bus_init(PCIBus *bus, DeviceState *parent,
     bus->devfn_min = devfn_min;
     bus->address_space_mem = address_space_mem;
     bus->address_space_io = address_space_io;
+    pci_setup_iommu(bus, pci_default_address_space, NULL);
 
     /* host bridge */
     QLIST_INIT(&bus->child);
@@ -786,6 +793,7 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
     PCIConfigReadFunc *config_read = pc->config_read;
     PCIConfigWriteFunc *config_write = pc->config_write;
+    AddressSpace *dma_as;
 
     if (devfn < 0) {
         for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
@@ -801,21 +809,15 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
                      PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name);
         return NULL;
     }
+
     pci_dev->bus = bus;
-    if (bus->dma_context_fn) {
-        pci_dev->dma = bus->dma_context_fn(bus, bus->dma_context_opaque, devfn);
-    } else {
-        /* FIXME: Make dma_context_fn use MemoryRegions instead, so this path is
-         * taken unconditionally */
-        /* FIXME: inherit memory region from bus creator */
-        memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
-                                 get_system_memory(), 0,
-                                 memory_region_size(get_system_memory()));
-        memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
-        address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
-        pci_dev->dma = g_new(DMAContext, 1);
-        dma_context_init(pci_dev->dma, &pci_dev->bus_master_as);
-    }
+    dma_as = bus->iommu_fn(bus, bus->iommu_opaque, devfn);
+    memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
+                             dma_as->root, 0, memory_region_size(dma_as->root));
+    memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
+    address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
+    pci_dev->dma = g_new(DMAContext, 1);
+    dma_context_init(pci_dev->dma, &pci_dev->bus_master_as);
 
     pci_dev->devfn = devfn;
     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
@@ -870,12 +872,10 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);
 
-    if (!pci_dev->bus->dma_context_fn) {
-        address_space_destroy(&pci_dev->bus_master_as);
-        memory_region_destroy(&pci_dev->bus_master_enable_region);
-        g_free(pci_dev->dma);
-        pci_dev->dma = NULL;
-    }
+    address_space_destroy(&pci_dev->bus_master_as);
+    memory_region_destroy(&pci_dev->bus_master_enable_region);
+    g_free(pci_dev->dma);
+    pci_dev->dma = NULL;
 }
 
 static void pci_unregister_io_regions(PCIDevice *pci_dev)
@@ -2232,10 +2232,10 @@ static void pci_device_class_init(ObjectClass *klass, void *data)
     k->props = pci_props;
 }
 
-void pci_setup_iommu(PCIBus *bus, PCIDMAContextFunc fn, void *opaque)
+void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
 {
-    bus->dma_context_fn = fn;
-    bus->dma_context_opaque = opaque;
+    bus->iommu_fn = fn;
+    bus->iommu_opaque = opaque;
 }
 
 static const TypeInfo pci_device_type_info = {
hw/ppc/spapr_pci.c +5 -4
index eb64a8f..762db62 100644
@@ -506,12 +506,11 @@ static const MemoryRegionOps spapr_msi_ops = {
 /*
  * PHB PCI device
  */
-static DMAContext *spapr_pci_dma_context_fn(PCIBus *bus, void *opaque,
-                                            int devfn)
+static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
 {
     sPAPRPHBState *phb = opaque;
 
-    return spapr_tce_get_dma(phb->tcet);
+    return &phb->iommu_as;
 }
 
 static int spapr_phb_init(SysBusDevice *s)
@@ -651,7 +650,9 @@ static int spapr_phb_init(SysBusDevice *s)
         fprintf(stderr, "Unable to create TCE table for %s\n", sphb->dtbusname);
         return -1;
     }
-    pci_setup_iommu(bus, spapr_pci_dma_context_fn, sphb);
+    address_space_init(&sphb->iommu_as, spapr_tce_get_iommu(sphb->tcet),
+                       sphb->dtbusname);
+    pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb);
 
     QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
 
include/hw/pci-host/spapr.h +1 -0
index 653dd40..1e23dbf 100644
@@ -50,6 +50,7 @@ typedef struct sPAPRPHBState {
     uint64_t dma_window_start;
     uint64_t dma_window_size;
     sPAPRTCETable *tcet;
+    AddressSpace iommu_as;
 
     struct {
         uint32_t irq;
include/hw/pci/pci.h +2 -2
index 8d075ab..3a85bce 100644
@@ -401,9 +401,9 @@ int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
 
 void pci_device_deassert_intx(PCIDevice *dev);
 
-typedef DMAContext *(*PCIDMAContextFunc)(PCIBus *, void *, int);
+typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);
 
-void pci_setup_iommu(PCIBus *bus, PCIDMAContextFunc fn, void *opaque);
+void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque);
 
 static inline void
 pci_set_byte(uint8_t *config, uint8_t val)
include/hw/pci/pci_bus.h +2 -2
index 6ee443c..66762f6 100644
@@ -10,8 +10,8 @@
 
 struct PCIBus {
     BusState qbus;
-    PCIDMAContextFunc dma_context_fn;
-    void *dma_context_opaque;
+    PCIIOMMUFunc iommu_fn;
+    void *iommu_opaque;
     uint8_t devfn_min;
     pci_set_irq_fn set_irq;
     pci_map_irq_fn map_irq;
Signature · 1 line
1.8.1.4
#93 lore raw

PATCH 28/30 spapr_vio: take care of creating our own AddressSpace/DMAContext

In reply to Paolo Bonzini

Fetch the root region from the sPAPRTCETable, and use it to build an AddressSpace and DMAContext.

Now, everywhere we have a DMAContext we also have access to the corresponding AddressSpace (either because we create it just before the DMAContext, or because dma_context_memory's AddressSpace is trivially address_space_memory).

---
 hw/ppc/spapr_iommu.c       | 11 -----------
 hw/ppc/spapr_vio.c         |  3 ++-
 include/hw/ppc/spapr.h     |  1 -
 include/hw/ppc/spapr_vio.h | 21 +++++++++++----------
 4 files changed, 13 insertions(+), 23 deletions(-)
hw/ppc/spapr_iommu.c +0 -11
index 8971d72..1d11e28 100644
@@ -37,10 +37,6 @@ enum sPAPRTCEAccess {
 };
 
 struct sPAPRTCETable {
-    /* temporary until everyone has its own AddressSpace */
-    DMAContext dma;
-    AddressSpace as;
-
     uint32_t liobn;
     uint32_t window_size;
     sPAPRTCE *table;
@@ -156,8 +152,6 @@ sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size)
     memory_region_init_iommu(&tcet->iommu, &spapr_iommu_ops,
                              &address_space_memory,
                              "iommu-spapr", INT64_MAX);
-    address_space_init(&tcet->as, &tcet->iommu);
-    dma_context_init(&tcet->dma, &tcet->as);
 
     QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
 
@@ -177,11 +171,6 @@ void spapr_tce_free(sPAPRTCETable *tcet)
     g_free(tcet);
 }
 
-DMAContext *spapr_tce_get_dma(sPAPRTCETable *tcet)
-{
-    return &tcet->dma;
-}
-
 MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet)
 {
     return &tcet->iommu;
hw/ppc/spapr_vio.c +2 -1
index a06ac94..8d77a36 100644
@@ -458,7 +458,8 @@ static int spapr_vio_busdev_init(DeviceState *qdev)
     if (pc->rtce_window_size) {
         uint32_t liobn = SPAPR_VIO_BASE_LIOBN | dev->reg;
         dev->tcet = spapr_tce_new_table(liobn, pc->rtce_window_size);
-        dev->dma = spapr_tce_get_dma(dev->tcet);
+        address_space_init(&dev->as, spapr_tce_get_iommu(dev->tcet));
+        dma_context_init(&dev->dma, &dev->as);
     }
 
     return pc->init(dev);
include/hw/ppc/spapr.h +0 -1
index 142abb7..a83720e 100644
@@ -348,7 +348,6 @@ void spapr_iommu_init(void);
 void spapr_events_init(sPAPREnvironment *spapr);
 void spapr_events_fdt_skel(void *fdt, uint32_t epow_irq);
 sPAPRTCETable *spapr_tce_new_table(uint32_t liobn, size_t window_size);
-DMAContext *spapr_tce_get_dma(sPAPRTCETable *tcet);
 MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet);
 void spapr_tce_free(sPAPRTCETable *tcet);
 void spapr_tce_reset(sPAPRTCETable *tcet);
include/hw/ppc/spapr_vio.h +11 -10
index 56f2821..2de58f1 100644
@@ -63,8 +63,9 @@ struct VIOsPAPRDevice {
     uint32_t irq;
     target_ulong signal_state;
     VIOsPAPR_CRQ crq;
+    AddressSpace as;
+    DMAContext dma;
     sPAPRTCETable *tcet;
-    DMAContext *dma;
 };
 
 #define DEFINE_SPAPR_PROPERTIES(type, field)           \
@@ -92,35 +93,35 @@ static inline qemu_irq spapr_vio_qirq(VIOsPAPRDevice *dev)
 static inline bool spapr_vio_dma_valid(VIOsPAPRDevice *dev, uint64_t taddr,
                                        uint32_t size, DMADirection dir)
 {
-    return dma_memory_valid(dev->dma, taddr, size, dir);
+    return dma_memory_valid(&dev->dma, taddr, size, dir);
 }
 
 static inline int spapr_vio_dma_read(VIOsPAPRDevice *dev, uint64_t taddr,
                                      void *buf, uint32_t size)
 {
-    return (dma_memory_read(dev->dma, taddr, buf, size) != 0) ?
+    return (dma_memory_read(&dev->dma, taddr, buf, size) != 0) ?
         H_DEST_PARM : H_SUCCESS;
 }
 
 static inline int spapr_vio_dma_write(VIOsPAPRDevice *dev, uint64_t taddr,
                                       const void *buf, uint32_t size)
 {
-    return (dma_memory_write(dev->dma, taddr, buf, size) != 0) ?
+    return (dma_memory_write(&dev->dma, taddr, buf, size) != 0) ?
         H_DEST_PARM : H_SUCCESS;
 }
 
 static inline int spapr_vio_dma_set(VIOsPAPRDevice *dev, uint64_t taddr,
                                     uint8_t c, uint32_t size)
 {
-    return (dma_memory_set(dev->dma, taddr, c, size) != 0) ?
+    return (dma_memory_set(&dev->dma, taddr, c, size) != 0) ?
         H_DEST_PARM : H_SUCCESS;
 }
 
-#define vio_stb(_dev, _addr, _val) (stb_dma((_dev)->dma, (_addr), (_val)))
-#define vio_sth(_dev, _addr, _val) (stw_be_dma((_dev)->dma, (_addr), (_val)))
-#define vio_stl(_dev, _addr, _val) (stl_be_dma((_dev)->dma, (_addr), (_val)))
-#define vio_stq(_dev, _addr, _val) (stq_be_dma((_dev)->dma, (_addr), (_val)))
-#define vio_ldq(_dev, _addr) (ldq_be_dma((_dev)->dma, (_addr)))
+#define vio_stb(_dev, _addr, _val) (stb_dma(&(_dev)->dma, (_addr), (_val)))
+#define vio_sth(_dev, _addr, _val) (stw_be_dma(&(_dev)->dma, (_addr), (_val)))
+#define vio_stl(_dev, _addr, _val) (stl_be_dma(&(_dev)->dma, (_addr), (_val)))
+#define vio_stq(_dev, _addr, _val) (stq_be_dma(&(_dev)->dma, (_addr), (_val)))
+#define vio_ldq(_dev, _addr) (ldq_be_dma(&(_dev)->dma, (_addr)))
 
 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq);
 
Signature · 1 line
1.8.1.4
#94 lore raw

PATCH 29/30 dma: eliminate DMAContext

In reply to Paolo Bonzini

The DMAContext is a simple pointer to an AddressSpace that is now always already available. Make everyone hold the address space directly, and clean up the DMA API to use the AddressSpace directly.

---
 dma-helpers.c              | 24 +++++----------
 exec.c                     |  3 --
 hw/dma/pl330.c             |  8 ++---
 hw/ide/ahci.c              | 18 +++++------
 hw/ide/ahci.h              |  4 +--
 hw/ide/ich.c               |  2 +-
 hw/ide/macio.c             |  4 +--
 hw/pci/pci.c               |  4 ---
 hw/ppc/spapr_vio.c         |  1 -
 hw/scsi/megasas.c          |  4 +--
 hw/scsi/virtio-scsi.c      |  2 +-
 hw/scsi/vmw_pvscsi.c       |  2 +-
 hw/sd/sdhci.c              | 22 +++++++-------
 hw/usb/hcd-ehci-pci.c      |  4 +--
 hw/usb/hcd-ehci-sysbus.c   |  2 +-
 hw/usb/hcd-ehci.c          | 12 ++++----
 hw/usb/hcd-ehci.h          |  2 +-
 hw/usb/hcd-ohci.c          | 30 +++++++++----------
 hw/usb/libhw.c             |  4 +--
 include/hw/pci/pci.h       | 17 +++++------
 include/hw/ppc/spapr_vio.h | 19 ++++++------
 include/sysemu/dma.h       | 75 ++++++++++++++++++++--------------------------
 22 files changed, 116 insertions(+), 147 deletions(-)
dma-helpers.c +7 -17
index c53705a..5afba47 100644
@@ -14,11 +14,9 @@
 
 /* #define DEBUG_IOMMU */
 
-int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len)
+int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len)
 {
-    AddressSpace *as = dma->as;
-
-    dma_barrier(dma, DMA_DIRECTION_FROM_DEVICE);
+    dma_barrier(as, DMA_DIRECTION_FROM_DEVICE);
 
 #define FILLBUF_SIZE 512
     uint8_t fillbuf[FILLBUF_SIZE];
@@ -36,13 +34,13 @@ int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len)
     return error;
 }
 
-void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, DMAContext *dma)
+void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint, AddressSpace *as)
 {
     qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
     qsg->nsg = 0;
     qsg->nalloc = alloc_hint;
     qsg->size = 0;
-    qsg->dma = dma;
+    qsg->as = as;
 }
 
 void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
@@ -102,7 +100,7 @@ static void dma_bdrv_unmap(DMAAIOCB *dbs)
     int i;
 
     for (i = 0; i < dbs->iov.niov; ++i) {
-        dma_memory_unmap(dbs->sg->dma, dbs->iov.iov[i].iov_base,
+        dma_memory_unmap(dbs->sg->as, dbs->iov.iov[i].iov_base,
                          dbs->iov.iov[i].iov_len, dbs->dir,
                          dbs->iov.iov[i].iov_len);
     }
@@ -150,7 +148,7 @@ static void dma_bdrv_cb(void *opaque, int ret)
     while (dbs->sg_cur_index < dbs->sg->nsg) {
         cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte;
         cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte;
-        mem = dma_memory_map(dbs->sg->dma, cur_addr, &cur_len, dbs->dir);
+        mem = dma_memory_map(dbs->sg->as, cur_addr, &cur_len, dbs->dir);
         if (!mem)
             break;
         qemu_iovec_add(&dbs->iov, mem, cur_len);
@@ -247,7 +245,7 @@ static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg,
     while (len > 0) {
         ScatterGatherEntry entry = sg->sg[sg_cur_index++];
         int32_t xfer = MIN(len, entry.len);
-        dma_memory_rw(sg->dma, entry.base, ptr, xfer, dir);
+        dma_memory_rw(sg->as, entry.base, ptr, xfer, dir);
         ptr += xfer;
         len -= xfer;
         resid -= xfer;
@@ -271,11 +269,3 @@ void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
 {
     bdrv_acct_start(bs, cookie, sg->size, type);
 }
-
-void dma_context_init(DMAContext *dma, AddressSpace *as)
-{
-#ifdef DEBUG_IOMMU
-    fprintf(stderr, "dma_context_init(%p -> %p)\n", dma, as);
-#endif
-    dma->as = as;
-}
exec.c +0 -3
index 4c43fdd..f508a59 100644
@@ -64,7 +64,6 @@ static MemoryRegion *system_io;
 
 AddressSpace address_space_io;
 AddressSpace address_space_memory;
-DMAContext dma_context_memory;
 
 MemoryRegion io_mem_ram, io_mem_rom, io_mem_unassigned, io_mem_notdirty;
 static MemoryRegion io_mem_subpage_ram;
@@ -1858,8 +1857,6 @@ static void memory_map_init(void)
     memory_listener_register(&core_memory_listener, &address_space_memory);
     memory_listener_register(&io_memory_listener, &address_space_io);
     memory_listener_register(&tcg_memory_listener, &address_space_memory);
-
-    dma_context_init(&dma_context_memory, &address_space_memory);
 }
 
 MemoryRegion *get_system_memory(void)
hw/dma/pl330.c +4 -4
index 60f5299..044c087 100644
@@ -1074,7 +1074,7 @@ static inline const PL330InsnDesc *pl330_fetch_insn(PL330Chan *ch)
     uint8_t opcode;
     int i;
 
-    dma_memory_read(&dma_context_memory, ch->pc, &opcode, 1);
+    dma_memory_read(&address_space_memory, ch->pc, &opcode, 1);
     for (i = 0; insn_desc[i].size; i++) {
         if ((opcode & insn_desc[i].opmask) == insn_desc[i].opcode) {
             return &insn_desc[i];
@@ -1088,7 +1088,7 @@ static inline void pl330_exec_insn(PL330Chan *ch, const PL330InsnDesc *insn)
     uint8_t buf[PL330_INSN_MAXSIZE];
 
     assert(insn->size <= PL330_INSN_MAXSIZE);
-    dma_memory_read(&dma_context_memory, ch->pc, buf, insn->size);
+    dma_memory_read(&address_space_memory, ch->pc, buf, insn->size);
     insn->exec(ch, buf[0], &buf[1], insn->size - 1);
 }
 
@@ -1153,7 +1153,7 @@ static int pl330_exec_cycle(PL330Chan *channel)
     if (q != NULL && q->len <= pl330_fifo_num_free(&s->fifo)) {
         int len = q->len - (q->addr & (q->len - 1));
 
-        dma_memory_read(&dma_context_memory, q->addr, buf, len);
+        dma_memory_read(&address_space_memory, q->addr, buf, len);
         if (PL330_ERR_DEBUG > 1) {
             DB_PRINT("PL330 read from memory @%08x (size = %08x):\n",
                       q->addr, len);
@@ -1185,7 +1185,7 @@ static int pl330_exec_cycle(PL330Chan *channel)
             fifo_res = pl330_fifo_get(&s->fifo, buf, len, q->tag);
         }
         if (fifo_res == PL330_FIFO_OK || q->z) {
-            dma_memory_write(&dma_context_memory, q->addr, buf, len);
+            dma_memory_write(&address_space_memory, q->addr, buf, len);
             if (PL330_ERR_DEBUG > 1) {
                 DB_PRINT("PL330 read from memory @%08x (size = %08x):\n",
                          q->addr, len);
hw/ide/ahci.c +9 -9
index eab6096..1adfa0b 100644
@@ -597,7 +597,7 @@ static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis)
     if (!cmd_fis) {
         /* map cmd_fis */
         uint64_t tbl_addr = le64_to_cpu(ad->cur_cmd->tbl_addr);
-        cmd_fis = dma_memory_map(ad->hba->dma, tbl_addr, &cmd_len,
+        cmd_fis = dma_memory_map(ad->hba->as, tbl_addr, &cmd_len,
                                  DMA_DIRECTION_TO_DEVICE);
         cmd_mapped = 1;
     }
@@ -630,7 +630,7 @@ static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis)
     ahci_trigger_irq(ad->hba, ad, PORT_IRQ_D2H_REG_FIS);
 
     if (cmd_mapped) {
-        dma_memory_unmap(ad->hba->dma, cmd_fis, cmd_len,
+        dma_memory_unmap(ad->hba->as, cmd_fis, cmd_len,
                          DMA_DIRECTION_TO_DEVICE, cmd_len);
     }
 }
@@ -657,7 +657,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, int offset)
     }
 
     /* map PRDT */
-    if (!(prdt = dma_memory_map(ad->hba->dma, prdt_addr, &prdt_len,
+    if (!(prdt = dma_memory_map(ad->hba->as, prdt_addr, &prdt_len,
                                 DMA_DIRECTION_TO_DEVICE))){
         DPRINTF(ad->port_no, "map failed\n");
         return -1;
@@ -691,7 +691,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, int offset)
             goto out;
         }
 
-        qemu_sglist_init(sglist, (sglist_alloc_hint - off_idx), ad->hba->dma);
+        qemu_sglist_init(sglist, (sglist_alloc_hint - off_idx), ad->hba->as);
         qemu_sglist_add(sglist, le64_to_cpu(tbl[off_idx].addr + off_pos),
                         le32_to_cpu(tbl[off_idx].flags_size) + 1 - off_pos);
 
@@ -703,7 +703,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, int offset)
     }
 
 out:
-    dma_memory_unmap(ad->hba->dma, prdt, prdt_len,
+    dma_memory_unmap(ad->hba->as, prdt, prdt_len,
                      DMA_DIRECTION_TO_DEVICE, prdt_len);
     return r;
 }
@@ -836,7 +836,7 @@ static int handle_cmd(AHCIState *s, int port, int slot)
     tbl_addr = le64_to_cpu(cmd->tbl_addr);
 
     cmd_len = 0x80;
-    cmd_fis = dma_memory_map(s->dma, tbl_addr, &cmd_len,
+    cmd_fis = dma_memory_map(s->as, tbl_addr, &cmd_len,
                              DMA_DIRECTION_FROM_DEVICE);
 
     if (!cmd_fis) {
@@ -963,7 +963,7 @@ static int handle_cmd(AHCIState *s, int port, int slot)
     }
 
 out:
-    dma_memory_unmap(s->dma, cmd_fis, cmd_len, DMA_DIRECTION_FROM_DEVICE,
+    dma_memory_unmap(s->as, cmd_fis, cmd_len, DMA_DIRECTION_FROM_DEVICE,
                      cmd_len);
 
     if (s->dev[port].port.ifs[0].status & (BUSY_STAT|DRQ_STAT)) {
@@ -1145,12 +1145,12 @@ static const IDEDMAOps ahci_dma_ops = {
     .reset = ahci_dma_reset,
 };
 
-void ahci_init(AHCIState *s, DeviceState *qdev, DMAContext *dma, int ports)
+void ahci_init(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports)
 {
     qemu_irq *irqs;
     int i;
 
-    s->dma = dma;
+    s->as = as;
     s->ports = ports;
     s->dev = g_malloc0(sizeof(AHCIDevice) * ports);
     ahci_reg_init(s);
hw/ide/ahci.h +2 -2
index 85f37fe..341a571 100644
@@ -297,7 +297,7 @@ typedef struct AHCIState {
     uint32_t idp_index;     /* Current IDP index */
     int32_t ports;
     qemu_irq irq;
-    DMAContext *dma;
+    AddressSpace *as;
 } AHCIState;
 
 typedef struct AHCIPCIState {
@@ -338,7 +338,7 @@ typedef struct NCQFrame {
     uint8_t reserved10;
 } QEMU_PACKED NCQFrame;
 
-void ahci_init(AHCIState *s, DeviceState *qdev, DMAContext *dma, int ports);
+void ahci_init(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports);
 void ahci_uninit(AHCIState *s);
 
 void ahci_reset(AHCIState *s);
hw/ide/ich.c +1 -1
index ed1f1a2..6c0c0c2 100644
@@ -104,7 +104,7 @@ static int pci_ich9_ahci_init(PCIDevice *dev)
     uint8_t *sata_cap;
     d = DO_UPCAST(struct AHCIPCIState, card, dev);
 
-    ahci_init(&d->ahci, &dev->qdev, pci_dma_context(dev), 6);
+    ahci_init(&d->ahci, &dev->qdev, pci_get_address_space(dev), 6);
 
     pci_config_set_prog_interface(d->card.config, AHCI_PROGMODE_MAJOR_REV_1);
 
hw/ide/macio.c +2 -2
index e1e4f41..a1952b0 100644
@@ -71,7 +71,7 @@ static void pmac_ide_atapi_transfer_cb(void *opaque, int ret)
     s->io_buffer_size = io->len;
 
     qemu_sglist_init(&s->sg, io->len / MACIO_PAGE_SIZE + 1,
-                     &dma_context_memory);
+                     &address_space_memory);
     qemu_sglist_add(&s->sg, io->addr, io->len);
     io->addr += io->len;
     io->len = 0;
@@ -128,7 +128,7 @@ static void pmac_ide_transfer_cb(void *opaque, int ret)
     s->io_buffer_size = io->len;
 
     qemu_sglist_init(&s->sg, io->len / MACIO_PAGE_SIZE + 1,
-                     &dma_context_memory);
+                     &address_space_memory);
     qemu_sglist_add(&s->sg, io->addr, io->len);
     io->addr += io->len;
     io->len = 0;
hw/pci/pci.c +0 -4
index 9e6bd77..2b1bc6d 100644
@@ -816,8 +816,6 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
                              dma_as->root, 0, memory_region_size(dma_as->root));
     memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
     address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
-    pci_dev->dma = g_new(DMAContext, 1);
-    dma_context_init(pci_dev->dma, &pci_dev->bus_master_as);
 
     pci_dev->devfn = devfn;
     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
@@ -874,8 +872,6 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
 
     address_space_destroy(&pci_dev->bus_master_as);
     memory_region_destroy(&pci_dev->bus_master_enable_region);
-    g_free(pci_dev->dma);
-    pci_dev->dma = NULL;
 }
 
 static void pci_unregister_io_regions(PCIDevice *pci_dev)
hw/ppc/spapr_vio.c +0 -1
index 8d77a36..5e72f1b 100644
@@ -459,7 +459,6 @@ static int spapr_vio_busdev_init(DeviceState *qdev)
         uint32_t liobn = SPAPR_VIO_BASE_LIOBN | dev->reg;
         dev->tcet = spapr_tce_new_table(liobn, pc->rtce_window_size);
         address_space_init(&dev->as, spapr_tce_get_iommu(dev->tcet));
-        dma_context_init(&dev->dma, &dev->as);
     }
 
     return pc->init(dev);
hw/scsi/megasas.c +2 -2
index fe6550c..7ee7d97 100644
@@ -232,7 +232,7 @@ static int megasas_map_sgl(MegasasState *s, MegasasCmd *cmd, union mfi_sgl *sgl)
                                          MEGASAS_MAX_SGE);
         return iov_count;
     }
-    qemu_sglist_init(&cmd->qsg, iov_count, pci_dma_context(&s->dev));
+    qemu_sglist_init(&cmd->qsg, iov_count, pci_get_address_space(&s->dev));
     for (i = 0; i < iov_count; i++) {
         dma_addr_t iov_pa, iov_size_p;
 
@@ -628,7 +628,7 @@ static int megasas_map_dcmd(MegasasState *s, MegasasCmd *cmd)
     }
     iov_pa = megasas_sgl_get_addr(cmd, &cmd->frame->dcmd.sgl);
     iov_size = megasas_sgl_get_len(cmd, &cmd->frame->dcmd.sgl);
-    qemu_sglist_init(&cmd->qsg, 1, pci_dma_context(&s->dev));
+    qemu_sglist_init(&cmd->qsg, 1, pci_get_address_space(&s->dev));
     qemu_sglist_add(&cmd->qsg, iov_pa, iov_size);
     cmd->iov_size = iov_size;
     return cmd->iov_size;
hw/scsi/virtio-scsi.c +1 -1
index 08dd3f3..b8a0abf 100644
@@ -80,7 +80,7 @@ static void virtio_scsi_bad_req(void)
 static void qemu_sgl_init_external(QEMUSGList *qsgl, struct iovec *sg,
                                    hwaddr *addr, int num)
 {
-    qemu_sglist_init(qsgl, num, &dma_context_memory);
+    qemu_sglist_init(qsgl, num, &address_space_memory);
     while (num--) {
         qemu_sglist_add(qsgl, *(addr++), (sg++)->iov_len);
     }
hw/scsi/vmw_pvscsi.c +1 -1
index 48d12f4..eb2270f 100644
@@ -617,7 +617,7 @@ pvscsi_build_sglist(PVSCSIState *s, PVSCSIRequest *r)
 {
     PCIDevice *d = PCI_DEVICE(s);
 
-    qemu_sglist_init(&r->sgl, 1, pci_dma_context(d));
+    pci_dma_sglist_init(&r->sgl, d, 1);
     if (r->req.flags & PVSCSI_FLAG_CMD_WITH_SG_LIST) {
         pvscsi_convert_sglist(r);
     } else {
hw/sd/sdhci.c +11 -11
index 91dc9b0..5d9247a 100644
@@ -496,7 +496,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
                     s->blkcnt--;
                 }
             }
-            dma_memory_write(&dma_context_memory, s->sdmasysad,
+            dma_memory_write(&address_space_memory, s->sdmasysad,
                              &s->fifo_buffer[begin], s->data_count - begin);
             s->sdmasysad += s->data_count - begin;
             if (s->data_count == block_size) {
@@ -518,7 +518,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
                 s->data_count = block_size;
                 boundary_count -= block_size - begin;
             }
-            dma_memory_read(&dma_context_memory, s->sdmasysad,
+            dma_memory_read(&address_space_memory, s->sdmasysad,
                             &s->fifo_buffer[begin], s->data_count);
             s->sdmasysad += s->data_count - begin;
             if (s->data_count == block_size) {
@@ -557,10 +557,10 @@ static void sdhci_sdma_transfer_single_block(SDHCIState *s)
         for (n = 0; n < datacnt; n++) {
             s->fifo_buffer[n] = sd_read_data(s->card);
         }
-        dma_memory_write(&dma_context_memory, s->sdmasysad, s->fifo_buffer,
+        dma_memory_write(&address_space_memory, s->sdmasysad, s->fifo_buffer,
                          datacnt);
     } else {
-        dma_memory_read(&dma_context_memory, s->sdmasysad, s->fifo_buffer,
+        dma_memory_read(&address_space_memory, s->sdmasysad, s->fifo_buffer,
                         datacnt);
         for (n = 0; n < datacnt; n++) {
             sd_write_data(s->card, s->fifo_buffer[n]);
@@ -588,7 +588,7 @@ static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
     hwaddr entry_addr = (hwaddr)s->admasysaddr;
     switch (SDHC_DMA_TYPE(s->hostctl)) {
     case SDHC_CTRL_ADMA2_32:
-        dma_memory_read(&dma_context_memory, entry_addr, (uint8_t *)&adma2,
+        dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma2,
                         sizeof(adma2));
         adma2 = le64_to_cpu(adma2);
         /* The spec does not specify endianness of descriptor table.
@@ -600,7 +600,7 @@ static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
         dscr->incr = 8;
         break;
     case SDHC_CTRL_ADMA1_32:
-        dma_memory_read(&dma_context_memory, entry_addr, (uint8_t *)&adma1,
+        dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma1,
                         sizeof(adma1));
         adma1 = le32_to_cpu(adma1);
         dscr->addr = (hwaddr)(adma1 & 0xFFFFF000);
@@ -613,12 +613,12 @@ static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
         }
         break;
     case SDHC_CTRL_ADMA2_64:
-        dma_memory_read(&dma_context_memory, entry_addr,
+        dma_memory_read(&address_space_memory, entry_addr,
                         (uint8_t *)(&dscr->attr), 1);
-        dma_memory_read(&dma_context_memory, entry_addr + 2,
+        dma_memory_read(&address_space_memory, entry_addr + 2,
                         (uint8_t *)(&dscr->length), 2);
         dscr->length = le16_to_cpu(dscr->length);
-        dma_memory_read(&dma_context_memory, entry_addr + 4,
+        dma_memory_read(&address_space_memory, entry_addr + 4,
                         (uint8_t *)(&dscr->addr), 8);
         dscr->attr = le64_to_cpu(dscr->attr);
         dscr->attr &= 0xfffffff8;
@@ -678,7 +678,7 @@ static void sdhci_do_adma(SDHCIState *s)
                         s->data_count = block_size;
                         length -= block_size - begin;
                     }
-                    dma_memory_write(&dma_context_memory, dscr.addr,
+                    dma_memory_write(&address_space_memory, dscr.addr,
                                      &s->fifo_buffer[begin],
                                      s->data_count - begin);
                     dscr.addr += s->data_count - begin;
@@ -702,7 +702,7 @@ static void sdhci_do_adma(SDHCIState *s)
                         s->data_count = block_size;
                         length -= block_size - begin;
                     }
-                    dma_memory_read(&dma_context_memory, dscr.addr,
+                    dma_memory_read(&address_space_memory, dscr.addr,
                                     &s->fifo_buffer[begin], s->data_count);
                     dscr.addr += s->data_count - begin;
                     if (s->data_count == block_size) {
hw/usb/hcd-ehci-pci.c +2 -2
index 0eb7826..f1b5f5d 100644
@@ -63,7 +63,7 @@ static int usb_ehci_pci_initfn(PCIDevice *dev)
     s->caps[0x09] = 0x68;        /* EECP */
 
     s->irq = dev->irq[3];
-    s->dma = pci_dma_context(dev);
+    s->as = pci_get_address_space(dev);
 
     s->capsbase = 0x00;
     s->opregbase = 0x20;
@@ -86,7 +86,7 @@ static void usb_ehci_pci_write_config(PCIDevice *dev, uint32_t addr,
         return;
     }
     busmaster = pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_MASTER;
-    i->ehci.dma = busmaster ? pci_dma_context(dev) : NULL;
+    i->ehci.as = busmaster ? pci_get_address_space(dev) : &address_space_memory;
 }
 
 static Property ehci_pci_properties[] = {
hw/usb/hcd-ehci-sysbus.c +1 -1
index b68a66a..f9e4fd3 100644
@@ -40,7 +40,7 @@ static int usb_ehci_sysbus_initfn(SysBusDevice *dev)
 
     s->capsbase = sec->capsbase;
     s->opregbase = sec->opregbase;
-    s->dma = &dma_context_memory;
+    s->as = &address_space_memory;
 
     usb_ehci_initfn(s, DEVICE(dev));
     sysbus_init_irq(dev, &s->irq);
hw/usb/hcd-ehci.c +6 -6
index 0d3799d..1ad2159 100644
@@ -446,7 +446,7 @@ static inline int get_dwords(EHCIState *ehci, uint32_t addr,
 {
     int i;
 
-    if (!ehci->dma) {
+    if (!ehci->as) {
         ehci_raise_irq(ehci, USBSTS_HSE);
         ehci->usbcmd &= ~USBCMD_RUNSTOP;
         trace_usb_ehci_dma_error();
@@ -454,7 +454,7 @@ static inline int get_dwords(EHCIState *ehci, uint32_t addr,
     }
 
     for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
-        dma_memory_read(ehci->dma, addr, buf, sizeof(*buf));
+        dma_memory_read(ehci->as, addr, buf, sizeof(*buf));
         *buf = le32_to_cpu(*buf);
     }
 
@@ -467,7 +467,7 @@ static inline int put_dwords(EHCIState *ehci, uint32_t addr,
 {
     int i;
 
-    if (!ehci->dma) {
+    if (!ehci->as) {
         ehci_raise_irq(ehci, USBSTS_HSE);
         ehci->usbcmd &= ~USBCMD_RUNSTOP;
         trace_usb_ehci_dma_error();
@@ -476,7 +476,7 @@ static inline int put_dwords(EHCIState *ehci, uint32_t addr,
 
     for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
         uint32_t tmp = cpu_to_le32(*buf);
-        dma_memory_write(ehci->dma, addr, &tmp, sizeof(tmp));
+        dma_memory_write(ehci->as, addr, &tmp, sizeof(tmp));
     }
 
     return num;
@@ -1245,7 +1245,7 @@ static int ehci_init_transfer(EHCIPacket *p)
     cpage  = get_field(p->qtd.token, QTD_TOKEN_CPAGE);
     bytes  = get_field(p->qtd.token, QTD_TOKEN_TBYTES);
     offset = p->qtd.bufptr[0] & ~QTD_BUFPTR_MASK;
-    qemu_sglist_init(&p->sgl, 5, p->queue->ehci->dma);
+    qemu_sglist_init(&p->sgl, 5, p->queue->ehci->as);
 
     while (bytes > 0) {
         if (cpage > 4) {
@@ -1484,7 +1484,7 @@ static int ehci_process_itd(EHCIState *ehci,
                 return -1;
             }
 
-            qemu_sglist_init(&ehci->isgl, 2, ehci->dma);
+            qemu_sglist_init(&ehci->isgl, 2, ehci->as);
             if (off + len > 4096) {
                 /* transfer crosses page border */
                 uint32_t len2 = off + len - 4096;
hw/usb/hcd-ehci.h +1 -1
index e95bb7e..2fcb92f 100644
@@ -261,7 +261,7 @@ struct EHCIState {
     USBBus bus;
     qemu_irq irq;
     MemoryRegion mem;
-    DMAContext *dma;
+    AddressSpace *as;
     MemoryRegion mem_caps;
     MemoryRegion mem_opreg;
     MemoryRegion mem_ports;
hw/usb/hcd-ohci.c +15 -15
index 51241cd..5513924 100644
@@ -62,7 +62,7 @@ typedef struct {
     USBBus bus;
     qemu_irq irq;
     MemoryRegion mem;
-    DMAContext *dma;
+    AddressSpace *as;
     int num_ports;
     const char *name;
 
@@ -508,7 +508,7 @@ static inline int get_dwords(OHCIState *ohci,
     addr += ohci->localmem_base;
 
     for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
-        dma_memory_read(ohci->dma, addr, buf, sizeof(*buf));
+        dma_memory_read(ohci->as, addr, buf, sizeof(*buf));
         *buf = le32_to_cpu(*buf);
     }
 
@@ -525,7 +525,7 @@ static inline int put_dwords(OHCIState *ohci,
 
     for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
         uint32_t tmp = cpu_to_le32(*buf);
-        dma_memory_write(ohci->dma, addr, &tmp, sizeof(tmp));
+        dma_memory_write(ohci->as, addr, &tmp, sizeof(tmp));
     }
 
     return 1;
@@ -540,7 +540,7 @@ static inline int get_words(OHCIState *ohci,
     addr += ohci->localmem_base;
 
     for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
-        dma_memory_read(ohci->dma, addr, buf, sizeof(*buf));
+        dma_memory_read(ohci->as, addr, buf, sizeof(*buf));
         *buf = le16_to_cpu(*buf);
     }
 
@@ -557,7 +557,7 @@ static inline int put_words(OHCIState *ohci,
 
     for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
         uint16_t tmp = cpu_to_le16(*buf);
-        dma_memory_write(ohci->dma, addr, &tmp, sizeof(tmp));
+        dma_memory_write(ohci->as, addr, &tmp, sizeof(tmp));
     }
 
     return 1;
@@ -585,7 +585,7 @@ static inline int ohci_read_iso_td(OHCIState *ohci,
 static inline int ohci_read_hcca(OHCIState *ohci,
                                  dma_addr_t addr, struct ohci_hcca *hcca)
 {
-    dma_memory_read(ohci->dma, addr + ohci->localmem_base, hcca, sizeof(*hcca));
+    dma_memory_read(ohci->as, addr + ohci->localmem_base, hcca, sizeof(*hcca));
     return 1;
 }
 
@@ -617,7 +617,7 @@ static inline int ohci_put_iso_td(OHCIState *ohci,
 static inline int ohci_put_hcca(OHCIState *ohci,
                                 dma_addr_t addr, struct ohci_hcca *hcca)
 {
-    dma_memory_write(ohci->dma,
+    dma_memory_write(ohci->as,
                      addr + ohci->localmem_base + HCCA_WRITEBACK_OFFSET,
                      (char *)hcca + HCCA_WRITEBACK_OFFSET,
                      HCCA_WRITEBACK_SIZE);
@@ -634,12 +634,12 @@ static void ohci_copy_td(OHCIState *ohci, struct ohci_td *td,
     n = 0x1000 - (ptr & 0xfff);
     if (n > len)
         n = len;
-    dma_memory_rw(ohci->dma, ptr + ohci->localmem_base, buf, n, dir);
+    dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf, n, dir);
     if (n == len)
         return;
     ptr = td->be & ~0xfffu;
     buf += n;
-    dma_memory_rw(ohci->dma, ptr + ohci->localmem_base, buf, len - n, dir);
+    dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf, len - n, dir);
 }
 
 /* Read/Write the contents of an ISO TD from/to main memory.  */
@@ -653,12 +653,12 @@ static void ohci_copy_iso_td(OHCIState *ohci,
     n = 0x1000 - (ptr & 0xfff);
     if (n > len)
         n = len;
-    dma_memory_rw(ohci->dma, ptr + ohci->localmem_base, buf, n, dir);
+    dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf, n, dir);
     if (n == len)
         return;
     ptr = end_addr & ~0xfffu;
     buf += n;
-    dma_memory_rw(ohci->dma, ptr + ohci->localmem_base, buf, len - n, dir);
+    dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf, len - n, dir);
 }
 
 static void ohci_process_lists(OHCIState *ohci, int completion);
@@ -1788,11 +1788,11 @@ static USBBusOps ohci_bus_ops = {
 static int usb_ohci_init(OHCIState *ohci, DeviceState *dev,
                          int num_ports, dma_addr_t localmem_base,
                          char *masterbus, uint32_t firstport,
-                         DMAContext *dma)
+                         AddressSpace *as)
 {
     int i;
 
-    ohci->dma = dma;
+    ohci->as = as;
 
     if (usb_frame_time == 0) {
 #ifdef OHCI_TIME_WARP
@@ -1859,7 +1859,7 @@ static int usb_ohci_initfn_pci(struct PCIDevice *dev)
 
     if (usb_ohci_init(&ohci->state, &dev->qdev, ohci->num_ports, 0,
                       ohci->masterbus, ohci->firstport,
-                      pci_dma_context(dev)) != 0) {
+                      pci_get_address_space(dev)) != 0) {
         return -1;
     }
     ohci->state.irq = ohci->pci_dev.irq[0];
@@ -1882,7 +1882,7 @@ static int ohci_init_pxa(SysBusDevice *dev)
 
     /* Cannot fail as we pass NULL for masterbus */
     usb_ohci_init(&s->ohci, &dev->qdev, s->num_ports, s->dma_offset, NULL, 0,
-                  &dma_context_memory);
+                  &address_space_memory);
     sysbus_init_irq(dev, &s->ohci.irq);
     sysbus_init_mmio(dev, &s->ohci.mem);
 
hw/usb/libhw.c +2 -2
index d2d4b51..8df11c4 100644
@@ -37,7 +37,7 @@ int usb_packet_map(USBPacket *p, QEMUSGList *sgl)
 
         while (len) {
             dma_addr_t xlen = len;
-            mem = dma_memory_map(sgl->dma, base, &xlen, dir);
+            mem = dma_memory_map(sgl->as, base, &xlen, dir);
             if (!mem) {
                 goto err;
             }
@@ -63,7 +63,7 @@ void usb_packet_unmap(USBPacket *p, QEMUSGList *sgl)
     int i;
 
     for (i = 0; i < p->iov.niov; i++) {
-        dma_memory_unmap(sgl->dma, p->iov.iov[i].iov_base,
+        dma_memory_unmap(sgl->as, p->iov.iov[i].iov_base,
                          p->iov.iov[i].iov_len, dir,
                          p->iov.iov[i].iov_len);
     }
include/hw/pci/pci.h +8 -9
index 3a85bce..6ef1f97 100644
@@ -242,7 +242,6 @@ struct PCIDevice {
     PCIIORegion io_regions[PCI_NUM_REGIONS];
     AddressSpace bus_master_as;
     MemoryRegion bus_master_enable_region;
-    DMAContext *dma;
 
     /* do not access the following fields */
     PCIConfigReadFunc *config_read;
@@ -639,15 +638,15 @@ static inline uint32_t pci_config_size(const PCIDevice *d)
 }
 
 /* DMA access functions */
-static inline DMAContext *pci_dma_context(PCIDevice *dev)
+static inline AddressSpace *pci_get_address_space(PCIDevice *dev)
 {
-    return dev->dma;
+    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)
 {
-    dma_memory_rw(pci_dma_context(dev), addr, buf, len, dir);
+    dma_memory_rw(pci_get_address_space(dev), addr, buf, len, dir);
     return 0;
 }
 
@@ -667,12 +666,12 @@ static inline int pci_dma_write(PCIDevice *dev, dma_addr_t addr,
     static inline uint##_bits##_t ld##_l##_pci_dma(PCIDevice *dev,      \
                                                    dma_addr_t addr)     \
     {                                                                   \
-        return ld##_l##_dma(pci_dma_context(dev), addr);                \
+        return ld##_l##_dma(pci_get_address_space(dev), addr);          \
     }                                                                   \
     static inline void st##_s##_pci_dma(PCIDevice *dev,                 \
                                         dma_addr_t addr, uint##_bits##_t val) \
     {                                                                   \
-        st##_s##_dma(pci_dma_context(dev), addr, val);                  \
+        st##_s##_dma(pci_get_address_space(dev), addr, val);            \
     }
 
 PCI_DMA_DEFINE_LDST(ub, b, 8);
@@ -690,20 +689,20 @@ static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr,
 {
     void *buf;
 
-    buf = dma_memory_map(pci_dma_context(dev), addr, plen, dir);
+    buf = dma_memory_map(pci_get_address_space(dev), addr, plen, dir);
     return buf;
 }
 
 static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,
                                  DMADirection dir, dma_addr_t access_len)
 {
-    dma_memory_unmap(pci_dma_context(dev), buffer, len, dir, access_len);
+    dma_memory_unmap(pci_get_address_space(dev), buffer, len, dir, access_len);
 }
 
 static inline void pci_dma_sglist_init(QEMUSGList *qsg, PCIDevice *dev,
                                        int alloc_hint)
 {
-    qemu_sglist_init(qsg, alloc_hint, pci_dma_context(dev));
+    qemu_sglist_init(qsg, alloc_hint, pci_get_address_space(dev));
 }
 
 extern const VMStateDescription vmstate_pci_device;
include/hw/ppc/spapr_vio.h +9 -10
index 2de58f1..3609327 100644
@@ -64,7 +64,6 @@ struct VIOsPAPRDevice {
     target_ulong signal_state;
     VIOsPAPR_CRQ crq;
     AddressSpace as;
-    DMAContext dma;
     sPAPRTCETable *tcet;
 };
 
@@ -93,35 +92,35 @@ static inline qemu_irq spapr_vio_qirq(VIOsPAPRDevice *dev)
 static inline bool spapr_vio_dma_valid(VIOsPAPRDevice *dev, uint64_t taddr,
                                        uint32_t size, DMADirection dir)
 {
-    return dma_memory_valid(&dev->dma, taddr, size, dir);
+    return dma_memory_valid(&dev->as, taddr, size, dir);
 }
 
 static inline int spapr_vio_dma_read(VIOsPAPRDevice *dev, uint64_t taddr,
                                      void *buf, uint32_t size)
 {
-    return (dma_memory_read(&dev->dma, taddr, buf, size) != 0) ?
+    return (dma_memory_read(&dev->as, taddr, buf, size) != 0) ?
         H_DEST_PARM : H_SUCCESS;
 }
 
 static inline int spapr_vio_dma_write(VIOsPAPRDevice *dev, uint64_t taddr,
                                       const void *buf, uint32_t size)
 {
-    return (dma_memory_write(&dev->dma, taddr, buf, size) != 0) ?
+    return (dma_memory_write(&dev->as, taddr, buf, size) != 0) ?
         H_DEST_PARM : H_SUCCESS;
 }
 
 static inline int spapr_vio_dma_set(VIOsPAPRDevice *dev, uint64_t taddr,
                                     uint8_t c, uint32_t size)
 {
-    return (dma_memory_set(&dev->dma, taddr, c, size) != 0) ?
+    return (dma_memory_set(&dev->as, taddr, c, size) != 0) ?
         H_DEST_PARM : H_SUCCESS;
 }
 
-#define vio_stb(_dev, _addr, _val) (stb_dma(&(_dev)->dma, (_addr), (_val)))
-#define vio_sth(_dev, _addr, _val) (stw_be_dma(&(_dev)->dma, (_addr), (_val)))
-#define vio_stl(_dev, _addr, _val) (stl_be_dma(&(_dev)->dma, (_addr), (_val)))
-#define vio_stq(_dev, _addr, _val) (stq_be_dma(&(_dev)->dma, (_addr), (_val)))
-#define vio_ldq(_dev, _addr) (ldq_be_dma(&(_dev)->dma, (_addr)))
+#define vio_stb(_dev, _addr, _val) (stb_dma(&(_dev)->as, (_addr), (_val)))
+#define vio_sth(_dev, _addr, _val) (stw_be_dma(&(_dev)->as, (_addr), (_val)))
+#define vio_stl(_dev, _addr, _val) (stl_be_dma(&(_dev)->as, (_addr), (_val)))
+#define vio_stq(_dev, _addr, _val) (stq_be_dma(&(_dev)->as, (_addr), (_val)))
+#define vio_ldq(_dev, _addr) (ldq_be_dma(&(_dev)->as, (_addr)))
 
 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq);
 
include/sysemu/dma.h +32 -43

185 lines not on this page · raw message

Signature · 1 line
1.8.1.4
Reviewed-by on v1 29/30
#95 lore raw

Re: [Qemu-devel] [PATCH 29/30] dma: eliminate DMAContext

Quoted 5 lines · Paolo Bonzini

The DMAContext is a simple pointer to an AddressSpace that is now always already available. Make everyone hold the address space directly, and clean up the DMA API to use the AddressSpace directly.

Hey look, checkpatch is complaining again. Otherwise

-- PMM

#96 lore raw

PATCH 30/30 memory: give name to every AddressSpace

In reply to Paolo Bonzini

From: Alexey Kardashevskiy <[email protected]>

The "info mtree" command in QEMU console prints only "memory" and "I/O" address spaces while there are actually a lot more other AddressSpace structs created by PCI and VIO devices. Those devices do not normally have names and therefore not present in "info mtree" output.

The patch fixes this.

---
 exec.c                | 6 ++----
 hw/pci/pci.c          | 3 ++-
 hw/ppc/spapr_vio.c    | 2 +-
 include/exec/memory.h | 5 +++--
 memory.c              | 8 +++-----
 5 files changed, 11 insertions(+), 13 deletions(-)
exec.c +2 -4
index f508a59..1610604 100644
@@ -1846,13 +1846,11 @@ static void memory_map_init(void)
 {
     system_memory = g_malloc(sizeof(*system_memory));
     memory_region_init(system_memory, "system", INT64_MAX);
-    address_space_init(&address_space_memory, system_memory);
-    address_space_memory.name = "memory";
+    address_space_init(&address_space_memory, system_memory, "memory");
 
     system_io = g_malloc(sizeof(*system_io));
     memory_region_init(system_io, "io", 65536);
-    address_space_init(&address_space_io, system_io);
-    address_space_io.name = "I/O";
+    address_space_init(&address_space_io, system_io, "I/O");
 
     memory_listener_register(&core_memory_listener, &address_space_memory);
     memory_listener_register(&io_memory_listener, &address_space_io);
hw/pci/pci.c +2 -1
index 2b1bc6d..fa30110 100644
@@ -815,7 +815,8 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
     memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
                              dma_as->root, 0, memory_region_size(dma_as->root));
     memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
-    address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
+    address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region,
+                       name);
 
     pci_dev->devfn = devfn;
     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
hw/ppc/spapr_vio.c +1 -1
index 5e72f1b..3cfa326 100644
@@ -458,7 +458,7 @@ static int spapr_vio_busdev_init(DeviceState *qdev)
     if (pc->rtce_window_size) {
         uint32_t liobn = SPAPR_VIO_BASE_LIOBN | dev->reg;
         dev->tcet = spapr_tce_new_table(liobn, pc->rtce_window_size);
-        address_space_init(&dev->as, spapr_tce_get_iommu(dev->tcet));
+        address_space_init(&dev->as, spapr_tce_get_iommu(dev->tcet), qdev->id);
     }
 
     return pc->init(dev);
include/exec/memory.h +3 -2
index 05b9bb2..01b70ed 100644
@@ -191,7 +191,7 @@ struct MemoryRegionPortio {
  */
 struct AddressSpace {
     /* All fields are private. */
-    const char *name;
+    char *name;
     MemoryRegion *root;
     struct FlatView *current_map;
     int ioeventfd_nb;
@@ -858,8 +858,9 @@ void mtree_info(fprintf_function mon_printf, void *f);
  *
  * @as: an uninitialized #AddressSpace
  * @root: a #MemoryRegion that routes addesses for the address space
+ * @name: an address space name
  */
-void address_space_init(AddressSpace *as, MemoryRegion *root);
+void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
 
 
 /**
memory.c +3 -5
index e3970bf..66258e2 100644
@@ -1605,7 +1605,7 @@ void memory_listener_unregister(MemoryListener *listener)
     QTAILQ_REMOVE(&memory_listeners, listener, link);
 }
 
-void address_space_init(AddressSpace *as, MemoryRegion *root)
+void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
 {
     memory_region_transaction_begin();
     as->root = root;
@@ -1614,7 +1614,7 @@ void address_space_init(AddressSpace *as, MemoryRegion *root)
     as->ioeventfd_nb = 0;
     as->ioeventfds = NULL;
     QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
-    as->name = NULL;
+    as->name = g_strdup(name?name:"anonymous");
     address_space_init_dispatch(as);
     memory_region_update_pending |= root->enabled;
     memory_region_transaction_commit();
@@ -1629,6 +1629,7 @@ void address_space_destroy(AddressSpace *as)
     QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
     address_space_destroy_dispatch(as);
     flatview_destroy(as->current_map);
+    g_free(as->name);
     g_free(as->current_map);
     g_free(as->ioeventfds);
 }
@@ -1755,9 +1756,6 @@ void mtree_info(fprintf_function mon_printf, void *f)
     QTAILQ_INIT(&ml_head);
 
     QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
-        if (!as->name) {
-            continue;
-        }
         mon_printf(f, "%s\n", as->name);
         mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head);
     }
Signature · 1 line
1.8.1.4
#97 lore raw

Re: [Qemu-devel] [PATCH 30/30] memory: give name to every AddressSpace

Quoted 8 lines · Paolo Bonzini

From: Alexey Kardashevskiy <[email protected]>

The "info mtree" command in QEMU console prints only "memory" and "I/O" address spaces while there are actually a lot more other AddressSpace structs created by PCI and VIO devices. Those devices do not normally have names and therefore not present in "info mtree" output.

hw/pci/pci.c static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus, · line 819

Quoted 6 lines
hw/pci/pci.c +2 -1
@@ -815,7 +815,8 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
     memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
                              dma_as->root, 0, memory_region_size(dma_as->root));
     memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
-    address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region);
+    address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region,
+                       name);

Overlong line.

include/exec/memory.h void mtree_info(fprintf_function mon_printf, void *f); · line 861

Quoted 5 lines
include/exec/memory.h +1 -0
@@ -858,8 +858,9 @@ void mtree_info(fprintf_function mon_printf, void *f);
  *
  * @as: an uninitialized #AddressSpace
  * @root: a #MemoryRegion that routes addesses for the address space
+ * @name: an address space name

Does this need to be a unique name? Does it matter if it changes? (compare/contrast the documentation of memory_region_init()'s name param)

Quoted 20 lines
include/exec/memory.h +1 -1
  */
-void address_space_init(AddressSpace *as, MemoryRegion *root);
+void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
 
 
 /**
memory.c +1 -1
index e3970bf..66258e2 100644
@@ -1605,7 +1605,7 @@ void memory_listener_unregister(MemoryListener *listener)
     QTAILQ_REMOVE(&memory_listeners, listener, link);
 }
 
-void address_space_init(AddressSpace *as, MemoryRegion *root)
+void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
 {
     memory_region_transaction_begin();
     as->root = root;

memory.c void address_space_init(AddressSpace *as, MemoryRegion *root) · line 1617

Quoted 5 lines
memory.c +1 -1
@@ -1614,7 +1614,7 @@ void address_space_init(AddressSpace *as, MemoryRegion *root)
     as->ioeventfd_nb = 0;
     as->ioeventfds = NULL;
     QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
-    as->name = NULL;
+    as->name = g_strdup(name?name:"anonymous");

This needs spaces round the ? and :.

thanks
-- PMM

#98 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

This is part 1 of the memory API updates for 1.6. (Part 2 is the introduction of get/set_owner, part 3 is the introduction of the RCU-style flatview).

Compared to the first submissions, there are a few extra cleanup patches, and address_space_rw/read/write grew a return value so that the existing DMAContext API doesn't need to change.

Is this series much different from the one in
https://github.com/bonzini/qemu/tree/iommu (hope it is the same :) )?

Signature · 1 line
Alexey
#99 lore raw

Il 22/05/2013 04:30, Alexey Kardashevskiy ha scritto:

Quoted 5 lines · Paolo Bonzini
Quoted 4 lines

This is part 1 of the memory API updates for 1.6. (Part 2 is the introduction of get/set_owner, part 3 is the introduction of the RCU-style flatview).

Quoted 7 lines
Quoted 4 lines

Compared to the first submissions, there are a few extra cleanup patches, and address_space_rw/read/write grew a return value so that the existing DMAContext API doesn't need to change.

Is this series much different from the one in
https://github.com/bonzini/qemu/tree/iommu (hope it is the same :) )?

Not much, I've updated the branch now.

Paolo

#100 lore raw

Il 22/05/2013 11:24, Paolo Bonzini ha scritto:

Quoted 11 lines

Il 22/05/2013 04:30, Alexey Kardashevskiy ha scritto:

Quoted 10 lines · Paolo Bonzini
Quoted 8 lines

This is part 1 of the memory API updates for 1.6. (Part 2 is the introduction of get/set_owner, part 3 is the introduction of the RCU-style flatview).

Compared to the first submissions, there are a few extra cleanup patches, and address_space_rw/read/write grew a return value so that the existing DMAContext API doesn't need to change.

Quoted 4 lines
Quoted 2 lines

Is this series much different from the one in
https://github.com/bonzini/qemu/tree/iommu (hope it is the same :) )?

Not much, I've updated the branch now.

BTW, if you give me your Tested-by it would be good, since most comments have been cosmetic. I'd like to collect all of Peter's Reviewed-by from the RFC and send a pull request next week.

Then we can proceed with the fun stuff. :)

Paolo

#101 lore raw
Quoted 3 lines · Paolo Bonzini

BTW, if you give me your Tested-by it would be good, since most comments have been cosmetic. I'd like to collect all of Peter's Reviewed-by from the RFC and send a pull request next week.

There's been at least one requested fix in code review, so you need to send out another series version first before you can do a pullreq.

I'll have a look at the remaining patches now.

-- PMM

#102 lore raw

Il 23/05/2013 19:25, Peter Maydell ha scritto:

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

BTW, if you give me your Tested-by it would be good, since most comments have been cosmetic. I'd like to collect all of Peter's Reviewed-by from the RFC and send a pull request next week.

There's been at least one requested fix in code review, so you need to send out another series version first before you can do a pullreq.

The only fix is s/address_space_valid/address_space_access_valid/, right?

Paolo

Quoted 1 line

I'll have a look at the remaining patches now.

#103 lore raw
In reply to Peter Maydell
Quoted 1 line · Peter Maydell

I'll have a look at the remaining patches now.

...and done. I haven't reviewed the spapr specific stuff, on the assumption that somebody familiar with that h/w will do so.

thanks
-- PMM

#104 lore raw
In reply to Paolo Bonzini
Quoted 16 lines · Paolo Bonzini

Il 22/05/2013 11:24, Paolo Bonzini ha scritto:

Quoted 15 lines

Il 22/05/2013 04:30, Alexey Kardashevskiy ha scritto:

Quoted 5 lines · Paolo Bonzini
Quoted 4 lines

This is part 1 of the memory API updates for 1.6. (Part 2 is the introduction of get/set_owner, part 3 is the introduction of the RCU-style flatview).

Quoted 7 lines
Quoted 4 lines

Compared to the first submissions, there are a few extra cleanup patches, and address_space_rw/read/write grew a return value so that the existing DMAContext API doesn't need to change.

Is this series much different from the one in
https://github.com/bonzini/qemu/tree/iommu (hope it is the same :) )?

Not much, I've updated the branch now.

Does the memory region ref/unref patch go to series#2?

Quoted 3 lines

BTW, if you give me your Tested-by it would be good, since most comments have been cosmetic. I'd like to collect all of Peter's Reviewed-by from the RFC and send a pull request next week.

With all the stuff I got now (qemu.org/master + your set + David's PCI patches + David's VFIO patches + my VFIO patches), it works for me. Tried for emulated PCI, VFIO, emulated IBM VIO (and in full emulaiton as well), have not found any problem so far.

Quoted 1 line

Then we can proceed with the fun stuff. :)

Go ahead :)

Signature · 1 line
Alexey
#105 lore raw

Il 24/05/2013 16:12, Alexey Kardashevskiy ha scritto:

Quoted 13 lines · Paolo Bonzini
Quoted 12 lines

Il 22/05/2013 11:24, Paolo Bonzini ha scritto:

Quoted 11 lines

Il 22/05/2013 04:30, Alexey Kardashevskiy ha scritto:

Quoted 10 lines · Paolo Bonzini
Quoted 8 lines

This is part 1 of the memory API updates for 1.6. (Part 2 is the introduction of get/set_owner, part 3 is the introduction of the RCU-style flatview).

Compared to the first submissions, there are a few extra cleanup patches, and address_space_rw/read/write grew a return value so that the existing DMAContext API doesn't need to change.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

Is this series much different from the one in
https://github.com/bonzini/qemu/tree/iommu (hope it is the same :) )?

Not much, I've updated the branch now.

Does the memory region ref/unref patch go to series#2?

Now it will be #3, since the first pull request won't have the iommu stuff. :)

Quoted 4 lines

With all the stuff I got now (qemu.org/master + your set + David's PCI patches + David's VFIO patches + my VFIO patches), it works for me. Tried for emulated PCI, VFIO, emulated IBM VIO (and in full emulaiton as well), have not found any problem so far.

Good.

Paolo