For devices that need to do DMA-like operations, the memory subsystem
provides three possible approaches:

(1) simple direct reads and writes, via address_space_read()
    and address_space_write(). Completely flexible, but every
    individual access will go through the whole memory system
    codepath, so not very fast.

(2) address_space_map(), which gets you a host pointer to the
    RAM backing the guest address space, which you can then
    directly access Subject to some limitations: can only use for
    reads or writes, not for a read-modify-write; you might not get
    the whole range you asked for; will use a bounce buffer if it's
    not operating on RAM. Has a dma.h wrapper dma_memory_map().
    Used by about a dozen devices.

(3) address_space_cache_init(), which initializes a MemoryRegionCache
    which you can then use for hopefully faster read and write
    operations via address_space_read_cached() and
    address_space_write_cached(). Again, subject to limitations:
    must operate on RAM; you might not be able to access the whole
    range you wanted. This currently seems to be used solely by
    virtio.

The documentation in include/system/memory.h tells you the
mechanics of the APIs, but it gives no guidance as to when you
should use one or the other. Any suggestions ?

In particular, I'm working on a GICv5 model. This device puts a
lot of its working data structures into guest memory, so we're going
to be accessing guest memory a lot. The device spec says if you point
it at not-RAM you get to keep both pieces, and requires the guest
not to try to change the contents of that memory underfoot without
notifying it, so this seems like it ought to be a good candidate
for some kind of "act like you have this memory cached so you don't
need to keep looking it up every time" API...

Does the MemoryRegionCache API cover all the use cases we use
address_space_map() and dma_memory_map() for? (i.e. could we
deprecate the latter and transition code over to the new API?)

Incidentally, on the subject of the dma.h wrappers -- I've never
really been very clear why we have these. Some devices use them,
but a lot do not. The fact that the dma wrappers put in smp_mb()
barriers leaves me wondering if all those other devices that
don't use them have subtle bugs, but OTOH I've never noticed
any problems...

-- PMM

Reply via email to