RFC: Support for guest-visible IOMMUs
cover RFC: Support for guest-visible IOMMUs
This patch series introduces a general DMA infrastructure which allows the emulation of guest-visible IOMMUs. That is, it provides a framework by which an IOMMU device can be implemented, such that DMA from other device emulations will be translated according to the mappings provided by the IOMMU.
One example IOMMU implementation is included, for the para-virtualized TCE tables specified by PAPR and used in the pseries machine for both virtual IO and PCI devices.
This series is an updated and cleaned-up version of patches posted by Eduard - Gabriel Munteanu some time ago. Those prompted some discussion at the time, but no resolution was reached. These patches are now pretty polished although I'd like to get some comment from people working with other IOMMUs to make sure the infrastructure is sufficient to cover those.
Specifically, Eduard - Gabriel, if you have an updated version of your AMD IOMMU driver, it would be good to see if that can work with this infrastructure.
The series also converts a number of existing device models to use the new DMA infrastructure, so they can be used with IOMMUs. Along with the pci_dma_*() wrapper functions which are already in, this means that with this series applied, essentially all PCI devices should work with an emulated IOMMU, as well as pseries VIO devices. Other types of devices would need some further conversion to work with the new framework, but that should be quite straightforward.
PATCH 1/13 Use DMADirection type for dma_bdrv_io
Currently dma_bdrv_io() takes a 'to_dev' boolean parameter to determine the direction of DMA it is emulating. We already have a DMADirection enum designed specifically to encode DMA directions. This patch uses it for dma_bdrv_io() as well. This involves removing the DMADirection definition from the #ifdef it was inside, but since that only existed to protect the definition of dma_addr_t from places where config.h is not included, there wasn't any reason for it to be there in the first place.
--- dma-helpers.c | 20 ++++++++++++-------- dma.h | 12 ++++++------ hw/ide/core.c | 3 ++- hw/ide/macio.c | 3 ++- 4 files changed, 22 insertions(+), 16 deletions(-)
dma-helpers.c +12 -8
@@ -42,7 +42,7 @@ typedef struct {BlockDriverAIOCB *acb;QEMUSGList *sg;uint64_t sector_num; -bool to_dev; +DMADirection dir;bool in_cancel;int sg_cur_index;dma_addr_t sg_cur_byte;
@@ -76,7 +76,8 @@ static void dma_bdrv_unmap(DMAAIOCB *dbs)for (i =0 ; i < dbs->iov.niov; ++i) {cpu_physical_memory_unmap (dbs->iov.iov[i].iov_base, - dbs->iov.iov[i].iov_len, !dbs->to_dev, + dbs->iov.iov[i].iov_len, + dbs->dir !=DMA_DIRECTION_TO_DEVICE , dbs->iov.iov[i].iov_len); }qemu_iovec_reset (&dbs->iov);
@@ -123,7 +124,8 @@ 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 =cpu_physical_memory_map (cur_addr, &cur_len, !dbs->to_dev); + mem =cpu_physical_memory_map (cur_addr, &cur_len, + dbs->dir !=DMA_DIRECTION_TO_DEVICE );if (!mem)break ;qemu_iovec_add (&dbs->iov, mem, cur_len);
@@ -170,11 +172,11 @@ static AIOPool dma_aio_pool = {BlockDriverAIOCB *dma_bdrv_io (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector_num,DMAIOFunc *io_func,BlockDriverCompletionFunc *cb, -void *opaque,bool to_dev) +void *opaque,DMADirection dir) {DMAAIOCB *dbs =qemu_aio_get (&dma_aio_pool, bs, cb, opaque); -trace_dma_bdrv_io (dbs, bs, sector_num, to_dev); +trace_dma_bdrv_io (dbs, bs, sector_num, (dir ==DMA_DIRECTION_TO_DEVICE )); dbs->acb =NULL ; dbs->bs = bs;
@@ -182,7 +184,7 @@ BlockDriverAIOCB *dma_bdrv_io( dbs->sector_num = sector_num; dbs->sg_cur_index =0 ; dbs->sg_cur_byte =0 ; - dbs->to_dev = to_dev; + dbs->dir = dir; dbs->io_func = io_func; dbs->bh =NULL ;qemu_iovec_init (&dbs->iov, sg->nsg);
@@ -195,14 +197,16 @@ BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs, QEMUSGList *sg, uint64_t sector, void (*cb)(void *opaque, int ret), void *opaque) { - return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque, false); + return dma_bdrv_io(bs, sg, sector, bdrv_aio_readv, cb, opaque, +DMA_DIRECTION_FROM_DEVICE ); }BlockDriverAIOCB *dma_bdrv_write (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector,void (*cb)(void *opaque,int ret),void *opaque) { -return dma_bdrv_io (bs, sg, sector, bdrv_aio_writev, cb, opaque, true); +return dma_bdrv_io (bs, sg, sector, bdrv_aio_writev, cb, opaque, +DMA_DIRECTION_TO_DEVICE ); }
dma.h +6 -6
@@ -17,6 +17,11 @@typedef struct ScatterGatherEntry ScatterGatherEntry ; +typedef enum { +DMA_DIRECTION_TO_DEVICE =0 , +DMA_DIRECTION_FROM_DEVICE =1 , +}DMADirection ; +struct QEMUSGList {ScatterGatherEntry *sg;int nsg;
@@ -29,11 +34,6 @@ typedef target_phys_addr_t dma_addr_t;#define DMA_ADDR_FMT TARGET_FMT_plx -typedef enum { -DMA_DIRECTION_TO_DEVICE =0 , -DMA_DIRECTION_FROM_DEVICE =1 , -}DMADirection ; -struct ScatterGatherEntry {dma_addr_t base;dma_addr_t len;
@@ -51,7 +51,7 @@ typedef BlockDriverAIOCB *DMAIOFunc(BlockDriverState *bs, int64_t sector_num,BlockDriverAIOCB *dma_bdrv_io (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector_num,DMAIOFunc *io_func,BlockDriverCompletionFunc *cb, -void *opaque,bool to_dev); +void *opaque,DMADirection dir);BlockDriverAIOCB *dma_bdrv_read (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector,BlockDriverCompletionFunc *cb,void *opaque);
hw/ide/core.c +2 -1
@@ -604,7 +604,8 @@ void ide_dma_cb(void *opaque, int ret)break ;case IDE_DMA_TRIM : s->bus->dma->aiocb =dma_bdrv_io (s->bs, &s->sg, sector_num, - ide_issue_trim, ide_dma_cb, s, true); + ide_issue_trim, ide_dma_cb, s, +DMA_DIRECTION_TO_DEVICE );break ; }return ;
hw/ide/macio.c +2 -1
@@ -149,7 +149,8 @@ static void pmac_ide_transfer_cb(void *opaque, int ret)break ;case IDE_DMA_TRIM : m->aiocb =dma_bdrv_io (s->bs, &s->sg, sector_num, - ide_issue_trim, pmac_ide_transfer_cb, s, true); + ide_issue_trim, pmac_ide_transfer_cb, s, +DMA_DIRECTION_TO_DEVICE );break ; }return ;
Signature · 1 line
1.7.9
PATCH 2/13 Better support for dma_addr_t variables
In reply to David GibsonA while back, we introduced the dma_addr_t type, which is supposed to be used for bus visible memory addresses. At present, this is an alias for target_phys_addr_t, but this will change when we eventually add support for guest visible IOMMUs.
There are some instances of target_phys_addr_t in the code now which should really be dma_addr_t, but can't be trivially converted due to missing features which this patch corrects.
* We add DMA_ADDR_BITS analagous to TARGET_PHYS_ADDR_BITS. This is important where we need to make a compile-time (#if) based on the size of dma_addr_t.
* We add a new helper macro to create device properties which take a dma_addr_t. This lets us correctly convert code which cuurrently has DEFINE_PROP_TADDR() for variables which should be dma_addr_t instead of target_phys_addr_t.
--- Makefile.objs | 2 +- dma.h | 1 + hw/qdev-dma.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/qdev-dma.h | 8 ++++++ 4 files changed, 87 insertions(+), 1 deletions(-) create mode 100644 hw/qdev-dma.c create mode 100644 hw/qdev-dma.h
Makefile.objs +1 -1
@@ -287,7 +287,7 @@ hw-obj-$(CONFIG_LSI_SCSI_PCI) += lsi53c895a.o hw-obj-$(CONFIG_ESP) += esp.o hw-obj-y += dma-helpers.o sysbus.o isa-bus.o -hw-obj-y += qdev-addr.o +hw-obj-y += qdev-addr.o qdev-dma.o # VGA hw-obj-$(CONFIG_VGA_PCI) += vga-pci.o
dma.h +1 -0
@@ -32,6 +32,7 @@ struct QEMUSGList {#if defined(TARGET_PHYS_ADDR_BITS )typedef target_phys_addr_t dma_addr_t ; +#define DMA_ADDR_BITS TARGET_PHYS_ADDR_BITS#define DMA_ADDR_FMT TARGET_FMT_plx struct ScatterGatherEntry {
hw/qdev-dma.c new file +77 -0
77 lines not on this page · raw message
hw/qdev-dma.h new file +8 -0
@@ -0,0 +1,8 @@ +#include "dma.h" + +#define DEFINE_PROP_DMAADDR (_n, _s, _f, _d) \ + DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_dmaaddr, dma_addr_t) + +extern PropertyInfo qdev_prop_dmaaddr; +void qdev_prop_set_dmaaddr (DeviceState *dev,const char *name, +dma_addr_t value);
Signature · 1 line
1.7.9
PATCH 3/13 usb-xhci: Use PCI DMA helper functions
In reply to David GibsonShortly before 1.0, we added helper functions / wrappers for doing PCI DMA from individual devices. This makes what's going on clearer and means that when we add IOMMU support somewhere in the future, only the general PCI code will have to change, not every device that uses PCI DMA.
However, usb-xhci is not using these wrappers, despite being a PCI only device. This patch remedies the situation, using the pci dma functions instead of direct calls to cpu_physical_memory_{read,write}(). Likewise address parameters for DMA are changed to dma_addr_t instead of target_phys_addr_t.
--- hw/usb-xhci.c | 176 +++++++++++++++++++++++++++----------------------------- 1 files changed, 85 insertions(+), 91 deletions(-)
hw/usb-xhci.c +85 -91
@@ -22,7 +22,6 @@#include "qemu-timer.h" #include "usb.h" #include "pci.h" -#include "qdev-addr.h" #include "msi.h" //#define DEBUG_XHCI
@@ -140,7 +139,7 @@ typedef struct XHCITRB {uint64_t parameter;uint32_t status;uint32_t control; -target_phys_addr_t addr; +dma_addr_t addr;bool ccs; }XHCITRB ;
@@ -291,8 +290,8 @@ typedef enum EPType { } EPType;typedef struct XHCIRing { -target_phys_addr_t base; -target_phys_addr_t dequeue; +dma_addr_t base; +dma_addr_t dequeue;bool ccs; }XHCIRing ;
@@ -345,7 +344,7 @@ typedef struct XHCIEPContext {unsigned int next_bg;XHCITransfer bg_transfers[BG_XFERS ];EPType type; -target_phys_addr_t pctx; +dma_addr_t pctx;unsigned int max_psize;bool has_bg;uint32_t state;
@@ -353,7 +352,7 @@ typedef struct XHCIEPContext { typedef struct XHCISlot { bool enabled; - target_phys_addr_t ctx; + dma_addr_t ctx; unsigned int port; unsigned int devaddr; XHCIEPContext * eps[31];
@@ -402,7 +401,7 @@ struct XHCIState {uint32_t erdp_low;uint32_t erdp_high; -target_phys_addr_t er_start; +dma_addr_t er_start;uint32_t er_size;bool er_pcs;unsigned int er_ep_idx;
@@ -479,18 +478,18 @@ static const char *trb_name(XHCITRB *trb)static void xhci_kick_ep (XHCIState *xhci,unsigned int slotid,unsigned int epid); -static inline target_phys_addr_t xhci_addr64 (uint32_t low,uint32_t high) +static inline dma_addr_t xhci_addr64 (uint32_t low,uint32_t high) { -#if TARGET_PHYS_ADDR_BITS >32 -return low | ((target_phys_addr_t )high <<32 ); +#if DMA_ADDR_BITS >32 +return low | ((dma_addr_t )high <<32 );#else return low;#endif } -static inline target_phys_addr_t xhci_mask64 (uint64_t addr) +static inline dma_addr_t xhci_mask64 (uint64_t addr) { -#if TARGET_PHYS_ADDR_BITS >32 +#if DMA_ADDR_BITS >32 return addr;#else return addr &0xffffffff ;
@@ -532,7 +531,7 @@ static void xhci_die(XHCIState *xhci)static void xhci_write_event (XHCIState *xhci,XHCIEvent *event) {XHCITRB ev_trb; -target_phys_addr_t addr; +dma_addr_t addr; ev_trb.parameter =cpu_to_le64 (event->ptr); ev_trb.status =cpu_to_le32 (event->length | (event->ccode <<24 ));
@@ -548,7 +547,7 @@ static void xhci_write_event(XHCIState *xhci, XHCIEvent *event) trb_name(&ev_trb)); addr = xhci->er_start +TRB_SIZE *xhci->er_ep_idx; -cpu_physical_memory_write (addr, (uint8_t *) &ev_trb,TRB_SIZE ); +pci_dma_write (&xhci->pci_dev, addr, &ev_trb,TRB_SIZE ); xhci->er_ep_idx++;if (xhci->er_ep_idx >= xhci->er_size) {
@@ -559,7 +558,7 @@ static void xhci_write_event(XHCIState *xhci, XHCIEvent *event)static void xhci_events_update (XHCIState *xhci) { -target_phys_addr_t erdp; +dma_addr_t erdp;unsigned int dp_idx;bool do_irq =0 ;
@@ -570,8 +569,8 @@ static void xhci_events_update(XHCIState *xhci) erdp =xhci_addr64 (xhci->erdp_low, xhci->erdp_high);if (erdp < xhci->er_start || erdp >= (xhci->er_start +TRB_SIZE *xhci->er_size)) { -fprintf (stderr,"xhci: ERDP out of bounds: " TARGET_FMT_plx"\n" , erdp); -fprintf (stderr,"xhci: ER at " TARGET_FMT_plx" len %d\n" , +fprintf (stderr,"xhci: ERDP out of bounds: " DMA_ADDR_FMT "\n" , erdp); +fprintf (stderr,"xhci: ER at " DMA_ADDR_FMT " len %d\n" , xhci->er_start, xhci->er_size);xhci_die (xhci);return ;
@@ -630,7 +629,7 @@ static void xhci_events_update(XHCIState *xhci)static void xhci_event (XHCIState *xhci,XHCIEvent *event) { -target_phys_addr_t erdp; +dma_addr_t erdp;unsigned int dp_idx;if (xhci->er_full) {
@@ -649,8 +648,8 @@ static void xhci_event(XHCIState *xhci, XHCIEvent *event) erdp =xhci_addr64 (xhci->erdp_low, xhci->erdp_high);if (erdp < xhci->er_start || erdp >= (xhci->er_start +TRB_SIZE *xhci->er_size)) { -fprintf (stderr,"xhci: ERDP out of bounds: " TARGET_FMT_plx"\n" , erdp); -fprintf (stderr,"xhci: ER at " TARGET_FMT_plx" len %d\n" , +fprintf (stderr,"xhci: ERDP out of bounds: " DMA_ADDR_FMT "\n" , erdp); +fprintf (stderr,"xhci: ER at " DMA_ADDR_FMT " len %d\n" , xhci->er_start, xhci->er_size);xhci_die (xhci);return ;
@@ -686,7 +685,7 @@ static void xhci_event(XHCIState *xhci, XHCIEvent *event) }static void xhci_ring_init (XHCIState *xhci,XHCIRing *ring, -target_phys_addr_t base) +dma_addr_t base) { ring->base = base; ring->dequeue = base;
@@ -694,18 +693,18 @@ static void xhci_ring_init(XHCIState *xhci, XHCIRing *ring, }static TRBType xhci_ring_fetch (XHCIState *xhci,XHCIRing *ring,XHCITRB *trb, -target_phys_addr_t *addr) +dma_addr_t *addr) {while (1 ) {TRBType type; -cpu_physical_memory_read (ring->dequeue, (uint8_t *) trb,TRB_SIZE ); +pci_dma_read (&xhci->pci_dev, ring->dequeue, trb,TRB_SIZE ); trb->addr = ring->dequeue; trb->ccs = ring->ccs;le64_to_cpus (&trb->parameter);le32_to_cpus (&trb->status);le32_to_cpus (&trb->control); -DPRINTF ("xhci: TRB fetched [" TARGET_FMT_plx"]: " +DPRINTF ("xhci: TRB fetched [" DMA_ADDR_FMT "]: " "%016" PRIx64" %08x %08x %s\n" , ring->dequeue, trb->parameter, trb->status, trb->control,trb_name (trb));
@@ -735,19 +734,19 @@ static int xhci_ring_chain_length(XHCIState *xhci, const XHCIRing *ring) {XHCITRB trb;int length =0 ; -target_phys_addr_t dequeue = ring->dequeue; +dma_addr_t dequeue = ring->dequeue;bool ccs = ring->ccs;/* hack to bundle together the two/three TDs that make a setup transfer */ bool control_td_set =0 ;while (1 ) {TRBType type; -cpu_physical_memory_read (dequeue, (uint8_t *) &trb,TRB_SIZE ); +pci_dma_read (&xhci->pci_dev, dequeue, &trb,TRB_SIZE );le64_to_cpus (&trb.parameter);le32_to_cpus (&trb.status);le32_to_cpus (&trb.control); -DPRINTF ("xhci: TRB peeked [" TARGET_FMT_plx"]: " +DPRINTF ("xhci: TRB peeked [" DMA_ADDR_FMT "]: " "%016" PRIx64" %08x %08x\n" , dequeue, trb.parameter, trb.status, trb.control);
@@ -790,8 +789,8 @@ static void xhci_er_reset(XHCIState *xhci)xhci_die (xhci);return ; } -target_phys_addr_t erstba =xhci_addr64 (xhci->erstba_low, xhci->erstba_high); -cpu_physical_memory_read (erstba, (uint8_t *) &seg,sizeof (seg)); +dma_addr_t erstba =xhci_addr64 (xhci->erstba_low, xhci->erstba_high); +pci_dma_read (&xhci->pci_dev, erstba, &seg,sizeof (seg));le32_to_cpus (&seg.addr_low);le32_to_cpus (&seg.addr_high);le32_to_cpus (&seg.size);
@@ -807,7 +806,7 @@ static void xhci_er_reset(XHCIState *xhci) xhci->er_pcs =1 ; xhci->er_full =0 ; -DPRINTF ("xhci: event ring:" TARGET_FMT_plx" [%d]\n" , +DPRINTF ("xhci: event ring:" DMA_ADDR_FMT " [%d]\n" , xhci->er_start, xhci->er_size); }
@@ -833,24 +832,24 @@ static void xhci_set_ep_state(XHCIState *xhci, XHCIEPContext *epctx,return ; } -cpu_physical_memory_read (epctx->pctx, (uint8_t *) ctx,sizeof (ctx)); +pci_dma_read (&xhci->pci_dev, epctx->pctx, ctx,sizeof (ctx)); ctx[0 ] &= ~EP_STATE_MASK ; ctx[0 ] |= state; ctx[2 ] = epctx->ring.dequeue | epctx->ring.ccs; ctx[3 ] = (epctx->ring.dequeue >>16 ) >>16 ; -DPRINTF ("xhci: set epctx: " TARGET_FMT_plx" state=%d dequeue=%08x%08x\n" , +DPRINTF ("xhci: set epctx: " DMA_ADDR_FMT " state=%d dequeue=%08x%08x\n" , epctx->pctx, state, ctx[3 ], ctx[2 ]); -cpu_physical_memory_write (epctx->pctx, (uint8_t *) ctx,sizeof (ctx)); +pci_dma_write (&xhci->pci_dev, epctx->pctx, ctx,sizeof (ctx)); epctx->state = state; }static TRBCCode xhci_enable_ep (XHCIState *xhci,unsigned int slotid, -unsigned int epid,target_phys_addr_t pctx, +unsigned int epid,dma_addr_t pctx,uint32_t *ctx) {XHCISlot *slot;XHCIEPContext *epctx; -target_phys_addr_t dequeue; +dma_addr_t dequeue;int i;assert (slotid >=1 && slotid <=MAXSLOTS );
@@ -1087,7 +1086,7 @@ static TRBCCode xhci_set_ep_dequeue(XHCIState *xhci, unsigned int slotid, {XHCISlot *slot;XHCIEPContext *epctx; -target_phys_addr_t dequeue; +dma_addr_t dequeue;assert (slotid >=1 && slotid <=MAXSLOTS );
@@ -1142,7 +1141,7 @@ static int xhci_xfer_data(XHCITransfer *xfer, uint8_t *data,for (i =0 ; i < xfer->trb_count; i++) {XHCITRB *trb = &xfer->trbs[i]; -target_phys_addr_t addr; +dma_addr_t addr;unsigned int chunk =0 ;switch (TRB_TYPE (*trb)) {
@@ -1173,11 +1172,11 @@ static int xhci_xfer_data(XHCITransfer *xfer, uint8_t *data,memcpy (data, &idata, chunk); }else {DPRINTF ("xhci_xfer_data: r/w(%d) %d bytes at " - TARGET_FMT_plx"\n" , in_xfer, chunk, addr); +DMA_ADDR_FMT "\n" , in_xfer, chunk, addr);if (in_xfer) { -cpu_physical_memory_write (addr, data, chunk); +pci_dma_write (&xhci->pci_dev, addr, data, chunk); }else { -cpu_physical_memory_read (addr, data, chunk); +pci_dma_read (&xhci->pci_dev, addr, data, chunk); }#ifdef DEBUG_DATA unsigned int count = chunk;
@@ -1240,7 +1239,7 @@ static void xhci_stall_ep(XHCITransfer *xfer) epctx->ring.ccs = xfer->trbs[0 ].ccs;xhci_set_ep_state (xhci, epctx,EP_HALTED );DPRINTF ("xhci: stalled slot %d ep %d\n" , xfer->slotid, xfer->epid); -DPRINTF ("xhci: will continue at " TARGET_FMT_plx"\n" , epctx->ring.dequeue); +DPRINTF ("xhci: will continue at " DMA_ADDR_FMT "\n" , epctx->ring.dequeue); } static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer,
@@ -1805,7 +1804,7 @@ static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid, {XHCISlot *slot;USBDevice *dev; -target_phys_addr_t ictx, octx, dcbaap; +dma_addr_t ictx, octx, dcbaap;uint64_t poctx;uint32_t ictl_ctx[2 ];uint32_t slot_ctx[4 ];
@@ -1818,15 +1817,14 @@ static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid,DPRINTF ("xhci_address_slot(%d)\n" , slotid); dcbaap =xhci_addr64 (xhci->dcbaap_low, xhci->dcbaap_high); -cpu_physical_memory_read (dcbaap +8 *slotid, - (uint8_t *) &poctx,sizeof (poctx)); +pci_dma_read (&xhci->pci_dev, dcbaap +8 *slotid, &poctx,sizeof (poctx)); ictx =xhci_mask64 (pictx); octx =xhci_mask64 (le64_to_cpu (poctx)); -DPRINTF ("xhci: input context at " TARGET_FMT_plx"\n" , ictx); -DPRINTF ("xhci: output context at " TARGET_FMT_plx"\n" , octx); +DPRINTF ("xhci: input context at " DMA_ADDR_FMT "\n" , ictx); +DPRINTF ("xhci: output context at " DMA_ADDR_FMT "\n" , octx); -cpu_physical_memory_read (ictx, (uint8_t *) ictl_ctx,sizeof (ictl_ctx)); +pci_dma_read (&xhci->pci_dev, ictx, ictl_ctx,sizeof (ictl_ctx));if (ictl_ctx[0 ] !=0x0 || ictl_ctx[1 ] !=0x3 ) { fprintf(stderr, "xhci: invalid input context control %08x %08x\n",
@@ -1834,8 +1832,8 @@ static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid,return CC_TRB_ERROR ; } -cpu_physical_memory_read (ictx+32 , (uint8_t *) slot_ctx,sizeof (slot_ctx)); -cpu_physical_memory_read (ictx+64 , (uint8_t *) ep0_ctx,sizeof (ep0_ctx)); +pci_dma_read (&xhci->pci_dev, ictx+32 , slot_ctx,sizeof (slot_ctx)); +pci_dma_read (&xhci->pci_dev, ictx+64 , ep0_ctx,sizeof (ep0_ctx));DPRINTF ("xhci: input slot context: %08x %08x %08x %08x\n" , slot_ctx[0 ], slot_ctx[1 ], slot_ctx[2 ], slot_ctx[3 ]);
@@ -1884,8 +1882,8 @@ static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid,DPRINTF ("xhci: output ep0 context: %08x %08x %08x %08x %08x\n" , ep0_ctx[0 ], ep0_ctx[1 ], ep0_ctx[2 ], ep0_ctx[3 ], ep0_ctx[4 ]); -cpu_physical_memory_write (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); -cpu_physical_memory_write (octx+32 , (uint8_t *) ep0_ctx,sizeof (ep0_ctx)); +pci_dma_write (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx)); +pci_dma_write (&xhci->pci_dev, octx+32 , ep0_ctx,sizeof (ep0_ctx));return res; }
@@ -1894,7 +1892,7 @@ static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid,static TRBCCode xhci_configure_slot (XHCIState *xhci,unsigned int slotid,uint64_t pictx,bool dc) { -target_phys_addr_t ictx, octx; +dma_addr_t ictx, octx;uint32_t ictl_ctx[2 ];uint32_t slot_ctx[4 ];uint32_t islot_ctx[4 ];
@@ -1908,8 +1906,8 @@ static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid, ictx =xhci_mask64 (pictx); octx = xhci->slots[slotid-1 ].ctx; -DPRINTF ("xhci: input context at " TARGET_FMT_plx"\n" , ictx); -DPRINTF ("xhci: output context at " TARGET_FMT_plx"\n" , octx); +DPRINTF ("xhci: input context at " DMA_ADDR_FMT "\n" , ictx); +DPRINTF ("xhci: output context at " DMA_ADDR_FMT "\n" , octx);if (dc) {for (i =2 ; i <=31 ; i++) {
@@ -1918,17 +1916,17 @@ static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid, } } -cpu_physical_memory_read (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); +pci_dma_read (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx)); slot_ctx[3 ] &= ~(SLOT_STATE_MASK <<SLOT_STATE_SHIFT ); slot_ctx[3 ] |=SLOT_ADDRESSED <<SLOT_STATE_SHIFT ;DPRINTF ("xhci: output slot context: %08x %08x %08x %08x\n" , slot_ctx[0 ], slot_ctx[1 ], slot_ctx[2 ], slot_ctx[3 ]); -cpu_physical_memory_write (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); +pci_dma_write (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx));return CC_SUCCESS ; } -cpu_physical_memory_read (ictx, (uint8_t *) ictl_ctx,sizeof (ictl_ctx)); +pci_dma_read (&xhci->pci_dev, ictx, ictl_ctx,sizeof (ictl_ctx));if ((ictl_ctx[0 ] &0x3 ) !=0x0 || (ictl_ctx[1 ] &0x3 ) !=0x1 ) { fprintf(stderr, "xhci: invalid input context control %08x %08x\n",
@@ -1936,8 +1934,8 @@ static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid,return CC_TRB_ERROR ; } -cpu_physical_memory_read (ictx+32 , (uint8_t *) islot_ctx,sizeof (islot_ctx)); -cpu_physical_memory_read (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); +pci_dma_read (&xhci->pci_dev, ictx+32 , islot_ctx,sizeof (islot_ctx)); +pci_dma_read (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx));if (SLOT_STATE (slot_ctx[3 ]) <SLOT_ADDRESSED ) {fprintf (stderr,"xhci: invalid slot state %08x\n" , slot_ctx[3 ]);
@@ -1949,8 +1947,8 @@ static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid,xhci_disable_ep (xhci, slotid, i); }if (ictl_ctx[1 ] & (1 <<i)) { -cpu_physical_memory_read (ictx+32 +(32 *i), - (uint8_t *) ep_ctx,sizeof (ep_ctx)); +pci_dma_read (&xhci->pci_dev, ictx+32 +(32 *i), ep_ctx, +sizeof (ep_ctx));DPRINTF ("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n" , i/2 , i%2 , ep_ctx[0 ], ep_ctx[1 ], ep_ctx[2 ], ep_ctx[3 ], ep_ctx[4 ]);
@@ -1962,8 +1960,7 @@ static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid,DPRINTF ("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n" , i/2 , i%2 , ep_ctx[0 ], ep_ctx[1 ], ep_ctx[2 ], ep_ctx[3 ], ep_ctx[4 ]); -cpu_physical_memory_write (octx+(32 *i), - (uint8_t *) ep_ctx,sizeof (ep_ctx)); +pci_dma_write (&xhci->pci_dev, octx+(32 *i), ep_ctx,sizeof (ep_ctx)); } }
@@ -1975,7 +1972,7 @@ static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid,DPRINTF ("xhci: output slot context: %08x %08x %08x %08x\n" , slot_ctx[0 ], slot_ctx[1 ], slot_ctx[2 ], slot_ctx[3 ]); -cpu_physical_memory_write (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); +pci_dma_write (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx));return CC_SUCCESS ; }
@@ -1984,7 +1981,7 @@ static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid,static TRBCCode xhci_evaluate_slot (XHCIState *xhci,unsigned int slotid,uint64_t pictx) { -target_phys_addr_t ictx, octx; +dma_addr_t ictx, octx;uint32_t ictl_ctx[2 ];uint32_t iep0_ctx[5 ];uint32_t ep0_ctx[5 ];
@@ -1997,10 +1994,10 @@ static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid, ictx =xhci_mask64 (pictx); octx = xhci->slots[slotid-1 ].ctx; -DPRINTF ("xhci: input context at " TARGET_FMT_plx"\n" , ictx); -DPRINTF ("xhci: output context at " TARGET_FMT_plx"\n" , octx); +DPRINTF ("xhci: input context at " DMA_ADDR_FMT "\n" , ictx); +DPRINTF ("xhci: output context at " DMA_ADDR_FMT "\n" , octx); -cpu_physical_memory_read (ictx, (uint8_t *) ictl_ctx,sizeof (ictl_ctx)); +pci_dma_read (&xhci->pci_dev, ictx, ictl_ctx,sizeof (ictl_ctx));if (ictl_ctx[0 ] !=0x0 || ictl_ctx[1 ] & ~0x3 ) { fprintf(stderr, "xhci: invalid input context control %08x %08x\n",
@@ -2009,13 +2006,12 @@ static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid, }if (ictl_ctx[1 ] &0x1 ) { -cpu_physical_memory_read (ictx+32 , - (uint8_t *) islot_ctx,sizeof (islot_ctx)); +pci_dma_read (&xhci->pci_dev, ictx+32 , islot_ctx,sizeof (islot_ctx));DPRINTF ("xhci: input slot context: %08x %08x %08x %08x\n" , islot_ctx[0 ], islot_ctx[1 ], islot_ctx[2 ], islot_ctx[3 ]); -cpu_physical_memory_read (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); +pci_dma_read (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx)); slot_ctx[1 ] &= ~0xFFFF ;/* max exit latency */ slot_ctx[1 ] |= islot_ctx[1 ] &0xFFFF ;
@@ -2025,18 +2021,17 @@ static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid,DPRINTF ("xhci: output slot context: %08x %08x %08x %08x\n" , slot_ctx[0 ], slot_ctx[1 ], slot_ctx[2 ], slot_ctx[3 ]); -cpu_physical_memory_write (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); +pci_dma_write (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx)); }if (ictl_ctx[1 ] &0x2 ) { -cpu_physical_memory_read (ictx+64 , - (uint8_t *) iep0_ctx,sizeof (iep0_ctx)); +pci_dma_read (&xhci->pci_dev, ictx+64 , iep0_ctx,sizeof (iep0_ctx));DPRINTF ("xhci: input ep0 context: %08x %08x %08x %08x %08x\n" , iep0_ctx[0 ], iep0_ctx[1 ], iep0_ctx[2 ], iep0_ctx[3 ], iep0_ctx[4 ]); -cpu_physical_memory_read (octx+32 , (uint8_t *) ep0_ctx,sizeof (ep0_ctx)); +pci_dma_read (&xhci->pci_dev, octx+32 , ep0_ctx,sizeof (ep0_ctx)); ep0_ctx[1 ] &= ~0xFFFF0000 ;/* max packet size*/ ep0_ctx[1 ] |= iep0_ctx[1 ] &0xFFFF0000 ;
@@ -2044,8 +2039,7 @@ static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid,DPRINTF ("xhci: output ep0 context: %08x %08x %08x %08x %08x\n" , ep0_ctx[0 ], ep0_ctx[1 ], ep0_ctx[2 ], ep0_ctx[3 ], ep0_ctx[4 ]); -cpu_physical_memory_write (octx+32 , - (uint8_t *) ep0_ctx,sizeof (ep0_ctx)); +pci_dma_write (&xhci->pci_dev, octx+32 , ep0_ctx,sizeof (ep0_ctx)); }return CC_SUCCESS ;
@@ -2054,7 +2048,7 @@ static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid,static TRBCCode xhci_reset_slot (XHCIState *xhci,unsigned int slotid) {uint32_t slot_ctx[4 ]; -target_phys_addr_t octx; +dma_addr_t octx;int i;assert (slotid >=1 && slotid <=MAXSLOTS );
@@ -2062,7 +2056,7 @@ static TRBCCode xhci_reset_slot(XHCIState *xhci, unsigned int slotid) octx = xhci->slots[slotid-1 ].ctx; -DPRINTF ("xhci: output context at " TARGET_FMT_plx"\n" , octx); +DPRINTF ("xhci: output context at " DMA_ADDR_FMT "\n" , octx);for (i =2 ; i <=31 ; i++) {if (xhci->slots[slotid-1 ].eps[i-1 ]) {
@@ -2070,12 +2064,12 @@ static TRBCCode xhci_reset_slot(XHCIState *xhci, unsigned int slotid) } } -cpu_physical_memory_read (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); +pci_dma_read (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx)); slot_ctx[3 ] &= ~(SLOT_STATE_MASK <<SLOT_STATE_SHIFT ); slot_ctx[3 ] |=SLOT_DEFAULT <<SLOT_STATE_SHIFT ;DPRINTF ("xhci: output slot context: %08x %08x %08x %08x\n" , slot_ctx[0 ], slot_ctx[1 ], slot_ctx[2 ], slot_ctx[3 ]); -cpu_physical_memory_write (octx, (uint8_t *) slot_ctx,sizeof (slot_ctx)); +pci_dma_write (&xhci->pci_dev, octx, slot_ctx,sizeof (slot_ctx));return CC_SUCCESS ; }
@@ -2098,19 +2092,19 @@ static unsigned int xhci_get_slot(XHCIState *xhci, XHCIEvent *event, XHCITRB *trstatic TRBCCode xhci_get_port_bandwidth (XHCIState *xhci,uint64_t pctx) { -target_phys_addr_t ctx; +dma_addr_t ctx;uint8_t bw_ctx[MAXPORTS +1 ];DPRINTF ("xhci_get_port_bandwidth()\n" ); ctx =xhci_mask64 (pctx); -DPRINTF ("xhci: bandwidth context at " TARGET_FMT_plx"\n" , ctx); +DPRINTF ("xhci: bandwidth context at " DMA_ADDR_FMT "\n" , ctx);/* TODO: actually implement real values here */ bw_ctx[0 ] =0 ;memset (&bw_ctx[1 ],80 ,MAXPORTS );/* 80% */ -cpu_physical_memory_write (ctx, bw_ctx,sizeof (bw_ctx)); +pci_dma_write (&xhci->pci_dev, ctx, bw_ctx,sizeof (bw_ctx));return CC_SUCCESS ; }
@@ -2131,13 +2125,13 @@ static uint32_t xhci_nec_challenge(uint32_t hi, uint32_t lo)return ~val; } -static void xhci_via_challenge (uint64_t addr) +static void xhci_via_challenge (XHCIState *xhci,uint64_t addr) {uint32_t buf[8 ];uint32_t obuf[8 ]; -target_phys_addr_t paddr =xhci_mask64 (addr); +dma_addr_t paddr =xhci_mask64 (addr); -cpu_physical_memory_read (paddr, (uint8_t *) &buf,32 ); +pci_dma_read (&xhci->pci_dev, paddr, &buf,32 );memcpy (obuf, buf,sizeof (obuf));
@@ -2153,7 +2147,7 @@ static void xhci_via_challenge(uint64_t addr) obuf[7 ] = obuf[2 ] ^ obuf[3 ] ^0x65866593 ; } -cpu_physical_memory_write (paddr, (uint8_t *) &obuf,32 ); +pci_dma_write (&xhci->pci_dev, paddr, &obuf,32 ); }static void xhci_process_commands (XHCIState *xhci)
@@ -2161,7 +2155,7 @@ static void xhci_process_commands(XHCIState *xhci)XHCITRB trb;TRBType type;XHCIEvent event = {ER_COMMAND_COMPLETE ,CC_SUCCESS }; -target_phys_addr_t addr; +dma_addr_t addr;unsigned int i, slotid =0 ;DPRINTF ("xhci_process_commands()\n" );
@@ -2250,7 +2244,7 @@ static void xhci_process_commands(XHCIState *xhci) event.ccode =xhci_get_port_bandwidth (xhci, trb.parameter);break ;case CR_VENDOR_VIA_CHALLENGE_RESPONSE : -xhci_via_challenge (trb.parameter); +xhci_via_challenge (xhci, trb.parameter);break ;case CR_VENDOR_NEC_FIRMWARE_REVISION : event.type =48 ;/* NEC reply */
@@ -2540,7 +2534,7 @@ static void xhci_oper_write(XHCIState *xhci, uint32_t reg, uint32_t val)xhci_event (xhci, &event);DPRINTF ("xhci: command ring stopped (CRCR=%08x)\n" , xhci->crcr_low); }else { -target_phys_addr_t base =xhci_addr64 (xhci->crcr_low & ~0x3f , val); +dma_addr_t base =xhci_addr64 (xhci->crcr_low & ~0x3f , val);xhci_ring_init (xhci, &xhci->cmd_ring, base); } xhci->crcr_low &= ~(CRCR_CA |CRCR_CS );
Signature · 1 line
1.7.9
PATCH 4/13 Implement cpu_physical_memory_zero()
In reply to David GibsonThis patch adds cpu_physical_memory_zero() function. This is equivalent to calling cpu_physical_memory_write() with a buffer full of zeroes, but avoids actually allocating such a buffer along the way.
--- cpu-common.h | 1 + exec.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 0 deletions(-)
cpu-common.h +1 -0
@@ -53,6 +53,7 @@ void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev);void cpu_physical_memory_rw (target_phys_addr_t addr,uint8_t *buf,int len,int is_write); +void cpu_physical_memory_zero (target_phys_addr_t addr,int len);static inline void cpu_physical_memory_read (target_phys_addr_t addr,void *buf,int len) {
exec.c +56 -0
62 lines not on this page · raw message
Signature · 1 line
1.7.9
PATCH 5/13 iommu: Add universal DMA helper functions
In reply to David GibsonNot that long ago, every device implementation using DMA directly accessed guest memory using cpu_physical_memory_*(). This meant that adding support for a guest visible IOMMU would require changing every one of these devices to go through IOMMU translation.
Shortly before qemu 1.0, I made a start on fixing this by providing helper functions for PCI DMA. These are currently just stubs which call the direct access functions, but mean that an IOMMU can be implemented in one place, rather than for every PCI device.
Clearly, this doesn't help for non PCI devices, which could also be IOMMU translated on some platforms. It is also problematic for the devices which have both PCI and non-PCI version (e.g. OHCI, AHCI) - we cannot use the the pci_dma_*() functions, because they assume the presence of a PCIDevice, but we don't want to have to check between pci_dma_*() and cpu_physical_memory_*() every time we do a DMA in the device code.
This patch makes the first step on addressing both these problems, by introducing new (stub) dma helper functions which can be used for any DMA capable device.
These dma functions take a DMAContext *, a new (currently empty) variable describing the DMA address space in which the operation is to take place. NULL indicates untranslated DMA directly into guest physical address space. The intention is that in future non-NULL values will given information about any necessary IOMMU translation.
DMA using devices must obtain a DMAContext (or, potentially, contexts) from their bus or platform. For now this patch just converts the PCI wrappers to be implemented in terms of the universal wrappers, converting other drivers can take place over time.
--- dma.h | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/pci.h | 22 +++++++----- qemu-common.h | 1 + 3 files changed, 117 insertions(+), 9 deletions(-)
dma.h +103 -0
@@ -35,6 +35,109 @@ typedef target_phys_addr_t dma_addr_t;#define DMA_ADDR_BITS TARGET_PHYS_ADDR_BITS#define DMA_ADDR_FMT TARGET_FMT_plx +typedef void DMAInvalidateMapFunc (void *); + +/* Checks that the given range of addresses is valid for DMA. This is +* useful for certain cases, but usually you should just use +* dma_memory_{read,write}() and check for errors */ +static inline bool dma_memory_valid (DMAContext *dma,dma_addr_t addr, +dma_addr_t len,DMADirection dir) +{ +/* Stub version, with no iommu we assume all bus addresses are valid */ +return true; +} + +static inline int dma_memory_rw (DMAContext *dma,dma_addr_t addr, +void *buf,dma_addr_t len,DMADirection dir) +{ +/* Stub version when we have no iommu support */ +cpu_physical_memory_rw (addr, buf, (target_phys_addr_t )len, + dir ==DMA_DIRECTION_FROM_DEVICE ); +return 0 ; +} + +static inline int dma_memory_read (DMAContext *dma,dma_addr_t addr, +void *buf,dma_addr_t len) +{ +return dma_memory_rw (dma, addr, buf, len,DMA_DIRECTION_TO_DEVICE ); +} + +static inline int dma_memory_write (DMAContext *dma,dma_addr_t addr, +const void *buf,dma_addr_t len) +{ +return dma_memory_rw (dma, addr, (void *)buf, len, +DMA_DIRECTION_FROM_DEVICE ); +} + +static inline int dma_memory_zero (DMAContext *dma,dma_addr_t addr, +dma_addr_t len) +{ +/* Stub version when we have no iommu support */ +cpu_physical_memory_zero (addr, len); +return 0 ; +} + +static inline void *dma_memory_map (DMAContext *dma, +DMAInvalidateMapFunc *cb,void *opaque, +dma_addr_t addr,dma_addr_t *len, +DMADirection dir) +{ +target_phys_addr_t xlen = *len; +void *p; + + p =cpu_physical_memory_map (addr, &xlen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + *len = xlen; +return p; +} + +static inline void dma_memory_unmap (DMAContext *dma, +void *buffer,dma_addr_t len, +DMADirection dir,dma_addr_t access_len) +{ +return cpu_physical_memory_unmap (buffer, (target_phys_addr_t )len, + dir ==DMA_DIRECTION_FROM_DEVICE , + access_len); +} + +#define DEFINE_LDST_DMA (_lname, _sname, _bits, _end) \ + static inline uint##_bits##_t ld##_lname##_##_end##_dma(DMAContext *dma, \ + dma_addr_t addr) \ + { \ + uint##_bits##_t val; \ + dma_memory_read(dma, addr, &val, (_bits) / 8); \ + return _end##_bits##_to_cpu(val); \ + } \ + static inline void st##_sname##_##_end##_dma(DMAContext *dma, \ + dma_addr_t addr, \ + uint##_bits##_t val) \ + { \ + val = cpu_to_##_end##_bits(val); \ + dma_memory_write(dma, addr, &val, (_bits) / 8); \ + } + +static inline uint8_t ldub_dma (DMAContext *dma,dma_addr_t addr) +{ +uint8_t val; + +dma_memory_read (dma, addr, &val,1 ); +return val; +} + +static inline void stb_dma (DMAContext *dma,dma_addr_t addr,uint8_t val) +{ +dma_memory_write (dma, addr, &val,1 ); +} + +DEFINE_LDST_DMA (uw, w,16 , le); +DEFINE_LDST_DMA (l, l,32 , le); +DEFINE_LDST_DMA (q, q,64 , le); +DEFINE_LDST_DMA (uw, w,16 , be); +DEFINE_LDST_DMA (l, l,32 , be); +DEFINE_LDST_DMA (q, q,64 , be); + +#undef DEFINE_LDST_DMA +struct ScatterGatherEntry {dma_addr_t base;dma_addr_t len;
hw/pci.h +13 -9
@@ -545,10 +545,16 @@ static inline uint32_t pci_config_size(const PCIDevice *d) }/* DMA access functions */ +static inline DMAContext *pci_dma_context (PCIDevice *dev) +{ +/* Stub for when we have no PCI iommu support */ +return NULL ; +} +static inline int pci_dma_rw (PCIDevice *dev,dma_addr_t addr,void *buf,dma_addr_t len,DMADirection dir) { -cpu_physical_memory_rw (addr, buf, len, dir ==DMA_DIRECTION_FROM_DEVICE ); +dma_memory_rw (pci_dma_context (dev), addr, buf, len, dir);return 0 ; }
@@ -568,12 +574,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##_phys(addr); \ + return ld##_l##_dma(pci_dma_context(dev), addr); \ } \ static inline void st##_s##_pci_dma(PCIDevice *dev, \ - dma_addr_t addr, uint##_bits##_t val) \ + dma_addr_t addr, uint##_bits##_t val) \ { \ - st##_s##_phys(addr, val); \ + st##_s##_dma(pci_dma_context(dev), addr, val); \ }PCI_DMA_DEFINE_LDST (ub, b,8 );
@@ -587,21 +593,19 @@ PCI_DMA_DEFINE_LDST(q_be, q_be, 64);#undef PCI_DMA_DEFINE_LDSTstatic inline void *pci_dma_map (PCIDevice *dev,dma_addr_t addr, +DMAInvalidateMapFunc *cb,void *opaque,dma_addr_t *plen,DMADirection dir) { -target_phys_addr_t len = *plen;void *buf; - buf =cpu_physical_memory_map (addr, &len, dir ==DMA_DIRECTION_FROM_DEVICE ); - *plen = len; + buf =dma_memory_map (pci_dma_context (dev), cb, opaque, 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) { -cpu_physical_memory_unmap (buffer, len, dir ==DMA_DIRECTION_FROM_DEVICE , - access_len); +dma_memory_unmap (pci_dma_context (dev), buffer, len, dir, access_len); } static inline void pci_dma_sglist_init(QEMUSGList *qsg, PCIDevice *dev,
qemu-common.h +1 -0
@@ -247,6 +247,7 @@ typedef struct SSIBus SSIBus;typedef struct EventNotifier EventNotifier ;typedef struct VirtIODevice VirtIODevice ;typedef struct QEMUSGList QEMUSGList ; +typedef struct DMAContext DMAContext ;typedef uint64_t pcibus_t ;
Signature · 1 line
1.7.9
PATCH 6/13 usb-ohci: Use universal DMA helper functions
In reply to David GibsonThe OHCI device emulation can provide both PCI and SysBus OHCI implementations. Because of this, it was not previously converted to use the PCI DMA helper functions.
This patch converts it to use the new universal DMA helper functions. In the PCI case, it obtains its DMAContext from pci_dma_context(), in the SysBus case, it uses NULL - i.e. assumes for now that there will be no IOMMU translation for a SysBus OHCI.
--- hw/usb-ohci.c | 93 +++++++++++++++++++++++++++++++------------------------- 1 files changed, 51 insertions(+), 42 deletions(-)
hw/usb-ohci.c +51 -42
@@ -32,7 +32,7 @@#include "pci.h" #include "usb-ohci.h" #include "sysbus.h" -#include "qdev-addr.h" +#include "qdev-dma.h" //#define DEBUG_OHCI /* Dump packet contents. */
@@ -63,6 +63,7 @@ typedef struct {USBBus bus;qemu_irq irq;MemoryRegion mem; +DMAContext *dma;int num_ports;const char *name;
@@ -105,7 +106,7 @@ typedef struct {uint32_t htest;/* SM501 local memory offset */ -target_phys_addr_t localmem_base; +dma_addr_t localmem_base;/* Active packets. */ uint32_t old_ctl;
@@ -483,14 +484,14 @@ static void ohci_reset(void *opaque)/* Get an array of dwords from main memory */ static inline int get_dwords (OHCIState *ohci, -uint32_t addr,uint32_t *buf,int num) +dma_addr_t addr,uint32_t *buf,int num) {int i; addr += ohci->localmem_base;for (i =0 ; i < num; i++, buf++, addr +=sizeof (*buf)) { -cpu_physical_memory_read (addr, buf,sizeof (*buf)); +dma_memory_read (ohci->dma, addr, buf,sizeof (*buf)); *buf =le32_to_cpu (*buf); }
@@ -499,7 +500,7 @@ static inline int get_dwords(OHCIState *ohci,/* Put an array of dwords in to main memory */ static inline int put_dwords (OHCIState *ohci, -uint32_t addr,uint32_t *buf,int num) +dma_addr_t addr,uint32_t *buf,int num) {int i;
@@ -507,7 +508,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); -cpu_physical_memory_write (addr, &tmp,sizeof (tmp)); +dma_memory_write (ohci->dma, addr, &tmp,sizeof (tmp)); }return 1 ;
@@ -515,14 +516,14 @@ static inline int put_dwords(OHCIState *ohci,/* Get an array of words from main memory */ static inline int get_words (OHCIState *ohci, -uint32_t addr,uint16_t *buf,int num) +dma_addr_t addr,uint16_t *buf,int num) {int i; addr += ohci->localmem_base;for (i =0 ; i < num; i++, buf++, addr +=sizeof (*buf)) { -cpu_physical_memory_read (addr, buf,sizeof (*buf)); +dma_memory_read (ohci->dma, addr, buf,sizeof (*buf)); *buf =le16_to_cpu (*buf); }
@@ -531,7 +532,7 @@ static inline int get_words(OHCIState *ohci,/* Put an array of words in to main memory */ static inline int put_words (OHCIState *ohci, -uint32_t addr,uint16_t *buf,int num) +dma_addr_t addr,uint16_t *buf,int num) {int i;
@@ -539,40 +540,40 @@ static inline int put_words(OHCIState *ohci,for (i =0 ; i < num; i++, buf++, addr +=sizeof (*buf)) {uint16_t tmp =cpu_to_le16 (*buf); -cpu_physical_memory_write (addr, &tmp,sizeof (tmp)); +dma_memory_write (ohci->dma, addr, &tmp,sizeof (tmp)); }return 1 ; }static inline int ohci_read_ed (OHCIState *ohci, -uint32_t addr,struct ohci_ed *ed) +dma_addr_t addr,struct ohci_ed *ed) {return get_dwords (ohci, addr, (uint32_t *)ed,sizeof (*ed) >>2 ); }static inline int ohci_read_td (OHCIState *ohci, -uint32_t addr,struct ohci_td *td) +dma_addr_t addr,struct ohci_td *td) {return get_dwords (ohci, addr, (uint32_t *)td,sizeof (*td) >>2 ); }static inline int ohci_read_iso_td (OHCIState *ohci, -uint32_t addr,struct ohci_iso_td *td) +dma_addr_t addr,struct ohci_iso_td *td) {return (get_dwords (ohci, addr, (uint32_t *)td,4 ) &&get_words (ohci, addr +16 , td->offset,8 )); }static inline int ohci_read_hcca (OHCIState *ohci, -uint32_t addr,struct ohci_hcca *hcca) +dma_addr_t addr,struct ohci_hcca *hcca) { -cpu_physical_memory_read (addr + ohci->localmem_base, hcca,sizeof (*hcca)); +dma_memory_read (ohci->dma, addr + ohci->localmem_base, hcca,sizeof (*hcca));return 1 ; }static inline int ohci_put_ed (OHCIState *ohci, -uint32_t addr,struct ohci_ed *ed) +dma_addr_t addr,struct ohci_ed *ed) {/* ed->tail is under control of the HCD. * Since just ed->head is changed by HC, just write back this
@@ -584,64 +585,63 @@ static inline int ohci_put_ed(OHCIState *ohci, }static inline int ohci_put_td (OHCIState *ohci, -uint32_t addr,struct ohci_td *td) +dma_addr_t addr,struct ohci_td *td) {return put_dwords (ohci, addr, (uint32_t *)td,sizeof (*td) >>2 ); }static inline int ohci_put_iso_td (OHCIState *ohci, -uint32_t addr,struct ohci_iso_td *td) +dma_addr_t addr,struct ohci_iso_td *td) {return (put_dwords (ohci, addr, (uint32_t *)td,4 ) &&put_words (ohci, addr +16 , td->offset,8 )); }static inline int ohci_put_hcca (OHCIState *ohci, -uint32_t addr,struct ohci_hcca *hcca) +dma_addr_t addr,struct ohci_hcca *hcca) { -cpu_physical_memory_write (addr + ohci->localmem_base +HCCA_WRITEBACK_OFFSET , - (char *)hcca +HCCA_WRITEBACK_OFFSET , -HCCA_WRITEBACK_SIZE ); +dma_memory_write (ohci->dma, + addr + ohci->localmem_base +HCCA_WRITEBACK_OFFSET , + (char *)hcca +HCCA_WRITEBACK_OFFSET , +HCCA_WRITEBACK_SIZE );return 1 ; }/* Read/Write the contents of a TD from/to main memory. */ static void ohci_copy_td (OHCIState *ohci,struct ohci_td *td, -uint8_t *buf,int len,int write) +uint8_t *buf,int len,DMADirection dir) { -uint32_t ptr; -uint32_t n; +dma_addr_t ptr, n; ptr = td->cbp; n =0x1000 - (ptr &0xfff );if (n > len) n = len; -cpu_physical_memory_rw (ptr + ohci->localmem_base, buf, n, write); +dma_memory_rw (ohci->dma, ptr + ohci->localmem_base, buf, n, dir);if (n == len)return ; ptr = td->be & ~0xfffu ; buf += n; -cpu_physical_memory_rw (ptr + ohci->localmem_base, buf, len - n, write); +dma_memory_rw (ohci->dma, ptr + ohci->localmem_base, buf, len - n, dir); }/* Read/Write the contents of an ISO TD from/to main memory. */ static void ohci_copy_iso_td (OHCIState *ohci,uint32_t start_addr,uint32_t end_addr, -uint8_t *buf,int len,int write) +uint8_t *buf,int len,DMADirection dir) { -uint32_t ptr; -uint32_t n; +dma_addr_t ptr, n; ptr = start_addr; n =0x1000 - (ptr &0xfff );if (n > len) n = len; -cpu_physical_memory_rw (ptr + ohci->localmem_base, buf, n, write); +dma_memory_rw (ohci->dma, ptr + ohci->localmem_base, buf, n, dir);if (n == len)return ; ptr = end_addr & ~0xfffu ; buf += n; -cpu_physical_memory_rw (ptr + ohci->localmem_base, buf, len - n, write); +dma_memory_rw (ohci->dma, ptr + ohci->localmem_base, buf, len - n, dir); }static void ohci_process_lists (OHCIState *ohci,int completion);
@@ -804,7 +804,8 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed, }if (len && dir !=OHCI_TD_DIR_IN ) { -ohci_copy_iso_td (ohci, start_addr, end_addr, ohci->usb_buf, len,0 ); +ohci_copy_iso_td (ohci, start_addr, end_addr, ohci->usb_buf, len, +DMA_DIRECTION_TO_DEVICE ); }if (completion) {
@@ -828,7 +829,8 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,/* Writeback */ if (dir ==OHCI_TD_DIR_IN && ret >=0 && ret <= len) {/* IN transfer succeeded */ -ohci_copy_iso_td (ohci, start_addr, end_addr, ohci->usb_buf, ret,1 ); +ohci_copy_iso_td (ohci, start_addr, end_addr, ohci->usb_buf, ret, +DMA_DIRECTION_FROM_DEVICE );OHCI_SET_BM (iso_td.offset[relative_frame_number],TD_PSW_CC ,OHCI_CC_NOERROR );OHCI_SET_BM (iso_td.offset[relative_frame_number],TD_PSW_SIZE , ret);
@@ -971,7 +973,8 @@ static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed) pktlen = len; }if (!completion) { -ohci_copy_td (ohci, &td, ohci->usb_buf, pktlen,0 ); +ohci_copy_td (ohci, &td, ohci->usb_buf, pktlen, +DMA_DIRECTION_TO_DEVICE ); } } }
@@ -1021,7 +1024,8 @@ static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed) }if (ret >=0 ) {if (dir ==OHCI_TD_DIR_IN ) { -ohci_copy_td (ohci, &td, ohci->usb_buf, ret,1 ); +ohci_copy_td (ohci, &td, ohci->usb_buf, ret, +DMA_DIRECTION_FROM_DEVICE );#ifdef DEBUG_PACKET DPRINTF (" data:" );for (i =0 ; i < ret; i++)
@@ -1747,11 +1751,14 @@ static USBBusOps ohci_bus_ops = { };static int usb_ohci_init (OHCIState *ohci,DeviceState *dev, -int num_ports,uint32_t localmem_base, -char *masterbus,uint32_t firstport) +int num_ports,dma_addr_t localmem_base, +char *masterbus,uint32_t firstport, +DMAContext *dma) {int i; + ohci->dma = dma; +if (usb_frame_time ==0 ) {#ifdef OHCI_TIME_WARP usb_frame_time =get_ticks_per_sec ();
@@ -1816,7 +1823,8 @@ static int usb_ohci_initfn_pci(struct PCIDevice *dev) ohci->pci_dev.config[PCI_INTERRUPT_PIN ] =0x01 ;/* interrupt pin A */ if (usb_ohci_init (&ohci->state, &dev->qdev, ohci->num_ports,0 , - ohci->masterbus, ohci->firstport) !=0 ) { + ohci->masterbus, ohci->firstport, +pci_dma_context (dev)) !=0 ) {return -1 ; } ohci->state.irq = ohci->pci_dev.irq[0 ];
@@ -1835,7 +1843,7 @@ typedef struct {SysBusDevice busdev;OHCIState ohci;uint32_t num_ports; -target_phys_addr_t dma_offset; +dma_addr_t dma_offset; } OHCISysBusState;static int ohci_init_pxa (SysBusDevice *dev)
@@ -1843,7 +1851,8 @@ static int ohci_init_pxa(SysBusDevice *dev)OHCISysBusState *s =FROM_SYSBUS (OHCISysBusState, dev);/* Cannot fail as we pass NULL for masterbus */ -usb_ohci_init (&s->ohci, &dev->qdev, s->num_ports, s->dma_offset,NULL ,0 ); +usb_ohci_init (&s->ohci, &dev->qdev, s->num_ports, s->dma_offset,NULL ,0 , +NULL );sysbus_init_irq (dev, &s->ohci.irq);sysbus_init_mmio (dev, &s->ohci.mem);
@@ -1879,7 +1888,7 @@ static TypeInfo ohci_pci_info = {static Property ohci_sysbus_properties[] = {DEFINE_PROP_UINT32 ("num-ports" , OHCISysBusState, num_ports,3 ), -DEFINE_PROP_TADDR ("dma-offset" , OHCISysBusState, dma_offset,3 ), +DEFINE_PROP_DMAADDR ("dma-offset" , OHCISysBusState, dma_offset,3 ),DEFINE_PROP_END_OF_LIST (), };
Signature · 1 line
1.7.9
PATCH 7/13 iommu: Make sglists and dma_bdrv helpers use new universal DMA helpers
In reply to David Gibsondma-helpers.c contains a number of helper functions for doing scatter/gather DMA, and various block device related DMA. Currently, these directly access guest memory using cpu_physical_memory_*(), assuming no IOMMU translation.
This patch updates this code to use the new universal DMA helper functions. qemu_sglist_init() now takes a DMAContext * to describe the DMA address space in which the scatter/gather will take place.
We minimally update the callers qemu_sglist_init() to pass NULL (i.e. no translation, same as current behaviour). Some of those callers should pass something else in some cases to allow proper IOMMU translation in future, but that will be fixed in later patches.
--- dma-helpers.c | 26 ++++++++++++++++++-------- dma.h | 3 ++- hw/ide/ahci.c | 3 ++- hw/ide/macio.c | 4 ++-- hw/pci.h | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-)
dma-helpers.c +18 -8
@@ -11,12 +11,13 @@#include "block_int.h" #include "trace.h" -void qemu_sglist_init (QEMUSGList *qsg,int alloc_hint) +void qemu_sglist_init (QEMUSGList *qsg,int alloc_hint,DMAContext *dma) { qsg->sg =g_malloc (alloc_hint *sizeof (ScatterGatherEntry)); qsg->nsg =0 ; qsg->nalloc = alloc_hint; qsg->size =0 ; + qsg->dma = dma; }void qemu_sglist_add (QEMUSGList *qsg,dma_addr_t base,dma_addr_t len)
@@ -75,10 +76,9 @@ static void dma_bdrv_unmap(DMAAIOCB *dbs)int i;for (i =0 ; i < dbs->iov.niov; ++i) { -cpu_physical_memory_unmap (dbs->iov.iov[i].iov_base, - dbs->iov.iov[i].iov_len, - dbs->dir !=DMA_DIRECTION_TO_DEVICE , - dbs->iov.iov[i].iov_len); +dma_memory_unmap (dbs->sg->dma, dbs->iov.iov[i].iov_base, + dbs->iov.iov[i].iov_len, dbs->dir, + dbs->iov.iov[i].iov_len); }qemu_iovec_reset (&dbs->iov); }
@@ -104,10 +104,20 @@ static void dma_complete(DMAAIOCB *dbs, int ret) } } +static void dma_bdrv_cancel (void *opaque) +{ +DMAAIOCB *dbs = opaque; + +bdrv_aio_cancel (dbs->acb); +dma_bdrv_unmap (dbs); +qemu_iovec_destroy (&dbs->iov); +qemu_aio_release (dbs); +} +static void dma_bdrv_cb (void *opaque,int ret) {DMAAIOCB *dbs = (DMAAIOCB *)opaque; -target_phys_addr_t cur_addr, cur_len; +dma_addr_t cur_addr, cur_len;void *mem;trace_dma_bdrv_cb (dbs, ret);
@@ -124,8 +134,8 @@ 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 =cpu_physical_memory_map (cur_addr, &cur_len, - dbs->dir !=DMA_DIRECTION_TO_DEVICE ); + mem =dma_memory_map (dbs->sg->dma, dma_bdrv_cancel, dbs, + cur_addr, &cur_len, dbs->dir);if (!mem)break ;qemu_iovec_add (&dbs->iov, mem, cur_len);
dma.h +2 -1
@@ -27,6 +27,7 @@ struct QEMUSGList {int nsg;int nalloc;size_t size; +DMAContext *dma; };#if defined(TARGET_PHYS_ADDR_BITS )
@@ -143,7 +144,7 @@ struct ScatterGatherEntry { dma_addr_t len; }; -void qemu_sglist_init (QEMUSGList *qsg,int alloc_hint); +void qemu_sglist_init (QEMUSGList *qsg,int alloc_hint,DMAContext *dma);void qemu_sglist_add (QEMUSGList *qsg,dma_addr_t base,dma_addr_t len);void qemu_sglist_destroy (QEMUSGList *qsg);#endif
hw/ide/ahci.c +2 -1
@@ -667,7 +667,8 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist)if (sglist_alloc_hint >0 ) {AHCI_SG *tbl = (AHCI_SG *)prdt; -qemu_sglist_init (sglist, sglist_alloc_hint); +/* FIXME: pass the correct DMAContext */ +qemu_sglist_init (sglist, sglist_alloc_hint,NULL );for (i =0 ; i < sglist_alloc_hint; i++) {/* flags_size is zero-based */ qemu_sglist_add(sglist, le64_to_cpu(tbl[i].addr),
hw/ide/macio.c +2 -2
@@ -76,7 +76,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 ); +qemu_sglist_init (&s->sg, io->len /MACIO_PAGE_SIZE +1 ,NULL );qemu_sglist_add (&s->sg, io->addr, io->len); io->addr += io->len; io->len =0 ;
@@ -133,7 +133,7 @@ static void pmac_ide_transfer_cb(void *opaque, int ret) s->io_buffer_index =0 ; s->io_buffer_size = io->len; -qemu_sglist_init (&s->sg, io->len /MACIO_PAGE_SIZE +1 ); +qemu_sglist_init (&s->sg, io->len /MACIO_PAGE_SIZE +1 ,NULL );qemu_sglist_add (&s->sg, io->addr, io->len); io->addr += io->len; io->len =0 ;
hw/pci.h +1 -1
@@ -611,7 +611,7 @@ static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,static inline void pci_dma_sglist_init (QEMUSGList *qsg,PCIDevice *dev,int alloc_hint) { -qemu_sglist_init (qsg, alloc_hint); +qemu_sglist_init (qsg, alloc_hint,pci_dma_context (dev)); }extern const VMStateDescription vmstate_pci_device;
Signature · 1 line
1.7.9
PATCH 8/13 ide/ahci: Use universal DMA helper functions
In reply to David GibsonThe AHCI device can provide both PCI and SysBus AHCI device emulations. For this reason, it wasn't previously converted to use the pci_dma_*() helper functions. Now that we have universal DMA helper functions, this converts AHCI to use them.
The DMAContext is obtained from pci_dma_context() in the PCI case and set to NULL in the SysBus case (i.e. we assume for now that a SysBus AHCI has no IOMMU translation).
--- hw/ide/ahci.c | 7 ++++--- hw/ide/ahci.h | 3 ++- hw/ide/ich.c | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-)
hw/ide/ahci.c +4 -3
@@ -668,7 +668,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist)AHCI_SG *tbl = (AHCI_SG *)prdt;/* FIXME: pass the correct DMAContext */ -qemu_sglist_init (sglist, sglist_alloc_hint,NULL ); +qemu_sglist_init (sglist, sglist_alloc_hint, ad->hba->dma);for (i =0 ; i < sglist_alloc_hint; i++) {/* flags_size is zero-based */ qemu_sglist_add(sglist, le64_to_cpu(tbl[i].addr),
@@ -1115,11 +1115,12 @@ static const IDEDMAOps ahci_dma_ops = { .reset = ahci_dma_reset, }; -void ahci_init (AHCIState *s,DeviceState *qdev,int ports) +void ahci_init (AHCIState *s,DeviceState *qdev,DMAContext *dma,int ports) {qemu_irq *irqs;int i; + s->dma = dma; s->ports = ports; s->dev =g_malloc0 (sizeof (AHCIDevice) * ports);ahci_reg_init (s);
@@ -1182,7 +1183,7 @@ static const VMStateDescription vmstate_sysbus_ahci = {static int sysbus_ahci_init (SysBusDevice *dev) {SysbusAHCIState *s =FROM_SYSBUS (SysbusAHCIState, dev); -ahci_init (&s->ahci, &dev->qdev, s->num_ports); +ahci_init (&s->ahci, &dev->qdev,NULL , s->num_ports);sysbus_init_mmio (dev, &s->ahci.mem);sysbus_init_irq (dev, &s->ahci.irq);
hw/ide/ahci.h +2 -1
@@ -299,6 +299,7 @@ typedef struct AHCIState {uint32_t idp_index;/* Current IDP index */ int ports;qemu_irq irq; +DMAContext *dma; } AHCIState; typedef struct AHCIPCIState {
@@ -329,7 +330,7 @@ typedef struct NCQFrame {uint8_t reserved10; }QEMU_PACKED NCQFrame; -void ahci_init (AHCIState *s,DeviceState *qdev,int ports); +void ahci_init (AHCIState *s,DeviceState *qdev,DMAContext *dma,int ports);void ahci_uninit (AHCIState *s);void ahci_reset (void *opaque);
hw/ide/ich.c +1 -1
@@ -91,7 +91,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,6 ); +ahci_init (&d->ahci, &dev->qdev,pci_dma_context (dev),6 );pci_config_set_prog_interface (d->card.config,AHCI_PROGMODE_MAJOR_REV_1 );
Signature · 1 line
1.7.9
PATCH 9/13 usb: Convert usb_packet_{map, unmap} to universal DMA helpers
In reply to David GibsonThe USB UHCI and EHCI drivers were converted some time ago to use the pci_dma_*() helper functions. However, this conversion was not complete because in some places both these drivers do DMA via the usb_packet_map() function in usb-libhw.c. That function directly used cpu_physical_memory_map().
Now that the sglist code uses DMA wrappers properly, we can convert the functions in usb-libhw.c, thus conpleting the conversion of UHCI and EHCI to use the DMA wrappers.
Note that usb_packet_map() invokes dma_memory_map() with a NULL invalidate callback function. When IOMMU support is added, this will mean that usb_packet_map() and the corresponding usb_packet_unmap() must be called in close proximity without dropping the qemu device lock - otherwise the guest might invalidate IOMMU mappings while they are still in use by the device code.
--- hw/usb-ehci.c | 4 ++-- hw/usb-libhw.c | 20 +++++++++++--------- hw/usb-uhci.c | 2 +- hw/usb.h | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-)
hw/usb-ehci.c +2 -2
@@ -1343,7 +1343,7 @@ err:set_field (&q->qh.token, q->tbytes,QTD_TOKEN_TBYTES ); }ehci_finish_transfer (q, q->usb_status); -usb_packet_unmap (&q->packet); +usb_packet_unmap (&q->packet, &q->sgl); q->qh.token ^=QTD_TOKEN_DTOGGLE ; q->qh.token &= ~QTD_TOKEN_ACTIVE ;
@@ -1464,7 +1464,7 @@ static int ehci_process_itd(EHCIState *ehci,usb_packet_map (&ehci->ipacket, &ehci->isgl); ret =usb_handle_packet (dev, &ehci->ipacket);assert (ret !=USB_RET_ASYNC ); -usb_packet_unmap (&ehci->ipacket); +usb_packet_unmap (&ehci->ipacket, &ehci->isgl); }else {DPRINTF ("ISOCH: attempt to addess non-iso endpoint\n" ); ret =USB_RET_NAK ;
hw/usb-libhw.c +11 -9
@@ -26,15 +26,16 @@int usb_packet_map (USBPacket *p,QEMUSGList *sgl) { -int is_write = (p->pid ==USB_TOKEN_IN ); +DMADirection dir = (p->pid ==USB_TOKEN_IN ) ? +DMA_DIRECTION_FROM_DEVICE :DMA_DIRECTION_TO_DEVICE ;target_phys_addr_t len;void *mem;int i;for (i =0 ; i < sgl->nsg; i++) { len = sgl->sg[i].len; - mem =cpu_physical_memory_map (sgl->sg[i].base, &len, - is_write); + mem =dma_memory_map (sgl->dma,NULL ,NULL , + sgl->sg[i].base, &len, dir);if (!mem) { goto err; }
@@ -46,18 +47,19 @@ int usb_packet_map(USBPacket *p, QEMUSGList *sgl)return 0 ; err: -usb_packet_unmap (p); +usb_packet_unmap (p, sgl);return -1 ; } -void usb_packet_unmap (USBPacket *p) +void usb_packet_unmap (USBPacket *p,QEMUSGList *sgl) { -int is_write = (p->pid ==USB_TOKEN_IN ); +DMADirection dir = (p->pid ==USB_TOKEN_IN ) ? +DMA_DIRECTION_FROM_DEVICE :DMA_DIRECTION_TO_DEVICE ;int i;for (i =0 ; i < p->iov.niov; i++) { -cpu_physical_memory_unmap (p->iov.iov[i].iov_base, - p->iov.iov[i].iov_len, is_write, - p->iov.iov[i].iov_len); +dma_memory_unmap (sgl->dma, p->iov.iov[i].iov_base, + p->iov.iov[i].iov_len, dir, + p->iov.iov[i].iov_len); } }
hw/usb-uhci.c +1 -1
@@ -863,7 +863,7 @@ static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *in done: len =uhci_complete_td (s, td, async, int_mask); -usb_packet_unmap (&async->packet); +usb_packet_unmap (&async->packet, &async->sgl);uhci_async_free (async);return len; }
hw/usb.h +1 -1
@@ -336,7 +336,7 @@ void usb_packet_set_state(USBPacket *p, USBPacketState state);void usb_packet_setup (USBPacket *p,int pid,USBEndpoint *ep);void usb_packet_addbuf (USBPacket *p,void *ptr,size_t len);int usb_packet_map (USBPacket *p,QEMUSGList *sgl); -void usb_packet_unmap (USBPacket *p); +void usb_packet_unmap (USBPacket *p,QEMUSGList *sgl);void usb_packet_copy (USBPacket *p,void *ptr,size_t bytes);void usb_packet_skip (USBPacket *p,size_t bytes);void usb_packet_cleanup (USBPacket *p);
Signature · 1 line
1.7.9
PATCH 10/13 iommu: Introduce IOMMU emulation infrastructure
In reply to David GibsonThis patch adds the basic infrastructure necessary to emulate an IOMMU visible to the guest. The DMAContext structure is extended with information and a callback describing the translation, and the various DMA functions used by devices will now perform IOMMU translation using this callback.
--- configure | 12 ++++ dma-helpers.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dma.h | 126 +++++++++++++++++++++++++++++++------- 3 files changed, 305 insertions(+), 22 deletions(-)
configure +12 -0
@@ -138,6 +138,7 @@ linux_aio="" cap_ng="" attr="" libattr="" +iommu="yes" xfs="" vhost_net="no"
@@ -784,6 +785,10 @@ for opt do ;; --enable-vhost-net) vhost_net="yes" ;; + --enable-iommu) iommu="yes" + ;; + --disable-iommu) iommu="no" + ;; --disable-opengl) opengl="no" ;; --enable-opengl) opengl="yes"
@@ -1085,6 +1090,8 @@ echo " --enable-docs enable documentation build" echo " --disable-docs disable documentation build" echo " --disable-vhost-net disable vhost-net acceleration support" echo " --enable-vhost-net enable vhost-net acceleration support" +echo " --disable-iommu disable IOMMU emulation support" +echo " --enable-iommu enable IOMMU emulation support" echo " --enable-trace-backend=B Set trace backend" echo " Available backends:" $("$source_path"/scripts/tracetool --list-backends) echo " --with-trace-file=NAME Full PATH,NAME of file to store traces"
@@ -2935,6 +2942,7 @@ echo "posix_madvise $posix_madvise" echo "uuid support $uuid" echo "libcap-ng support $cap_ng" echo "vhost-net support $vhost_net" +echo "IOMMU support $iommu" echo "Trace backend $trace_backend" echo "Trace output file $trace_file-<pid>" echo "spice support $spice"
@@ -3812,6 +3820,10 @@ if test "$target_softmmu" = "yes" -a \( \ echo "CONFIG_NEED_MMU=y" >> $config_target_mak fi +if test "$iommu" = "yes" ; then + echo "CONFIG_IOMMU=y" >> $config_host_mak +fi + if test "$gprof" = "yes" ; then echo "TARGET_GPROF=yes" >> $config_target_mak if test "$target_linux_user" = "yes" ; then
dma-helpers.c +189 -0
198 lines not on this page · raw message
dma.h +104 -22
181 lines not on this page · raw message
Signature · 1 line
1.7.9
Re: [Qemu-devel] [PATCH 10/13] iommu: Introduce IOMMU emulation infrastructure
Quoted 6 lines · David Gibson
This patch adds the basic infrastructure necessary to emulate an IOMMU visible to the guest. The DMAContext structure is extended with information and a callback describing the translation, and the various DMA functions used by devices will now perform IOMMU translation using this callback.
Quoted 8 lines
Cc Michael S. Tsirkin <[email protected]>Cc Richard Henderson <[email protected]>Signed-off-by Eduard - Gabriel Munteanu <[email protected]>Signed-off-by David Gibson <[email protected]>--- configure | 12 ++++ dma-helpers.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Why not just dma.c?
Quoted 93 lines
dma.h | 126 +++++++++++++++++++++++++++++++------- 3 files changed, 305 insertions(+), 22 deletions(-)configure +12
-0@@ -138,6 +138,7 @@ linux_aio="" cap_ng="" attr="" libattr="" +iommu="yes" xfs="" vhost_net="no"@@ -784,6 +785,10 @@ for opt do ;; --enable-vhost-net) vhost_net="yes" ;; + --enable-iommu) iommu="yes" + ;; + --disable-iommu) iommu="no" + ;; --disable-opengl) opengl="no" ;; --enable-opengl) opengl="yes"@@ -1085,6 +1090,8 @@ echo " --enable-docs enable documentation build" echo " --disable-docs disable documentation build" echo " --disable-vhost-net disable vhost-net acceleration support" echo " --enable-vhost-net enable vhost-net acceleration support" +echo " --disable-iommu disable IOMMU emulation support" +echo " --enable-iommu enable IOMMU emulation support" echo " --enable-trace-backend=B Set trace backend" echo " Available backends:" $("$source_path"/scripts/tracetool --list-backends) echo " --with-trace-file=NAME Full PATH,NAME of file to store traces"@@ -2935,6 +2942,7 @@ echo "posix_madvise $posix_madvise" echo "uuid support $uuid" echo "libcap-ng support $cap_ng" echo "vhost-net support $vhost_net" +echo "IOMMU support $iommu" echo "Trace backend $trace_backend" echo "Trace output file $trace_file-<pid>" echo "spice support $spice"@@ -3812,6 +3820,10 @@ if test "$target_softmmu" = "yes" -a \( \ echo "CONFIG_NEED_MMU=y" >> $config_target_mak fi +if test "$iommu" = "yes" ; then + echo "CONFIG_IOMMU=y" >> $config_host_mak +fi + if test "$gprof" = "yes" ; then echo "TARGET_GPROF=yes" >> $config_target_mak if test "$target_linux_user" = "yes" ; thendma-helpers.c +24
-0@@ -10,6 +10,9 @@#include "dma.h" #include "block_int.h" #include "trace.h" +#include "range.h" + +/*#define DEBUG_IOMMU*/ void qemu_sglist_init (QEMUSGList *qsg,int alloc_hint,DMAContext *dma) {@@ -255,3 +258,189 @@ void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, {bdrv_acct_start (bs, cookie, sg->size, type); } + +#ifdef CONFIG_IOMMU +bool __dma_memory_valid (DMAContext *dma,dma_addr_t addr,dma_addr_t len, +DMADirection dir) +{ +target_phys_addr_t paddr, plen; + +#ifdef DEBUG_DMA +fprintf (stderr,"dma_memory_check iommu=%p addr=0x%llx len=0x%llx dir=%d\n" , + (unsigned long long )addr, (unsigned long long )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; + }
dma-helpers.c void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, · line 291
Quoted 10 lines
dma-helpers.c +10
-0+ + len -= plen; + addr += plen; + } + +return true; +} + +int __dma_memory_rw (DMAContext *dma,dma_addr_t addr, +void *buf,dma_addr_t len,DMADirection dir)
By the way, users still ignore the return code from the pci routines.
I suspect the API of returning errors is just not such a good one. What happens on read errors with real devices? Master abort? What about write errors? Maybe we need a callback
that will set error flags in the device instead.
dma-helpers.c void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, · line 304
Quoted 13 lines
dma-helpers.c +13
-0+{ +target_phys_addr_t paddr, plen; +int err; + +#ifdef DEBUG_IOMMU +fprintf (stderr,"dma_memory_rw iommu=%p addr=0x%llx len=0x%llx dir=%d\n" , + (unsigned long long )addr, (unsigned long long )len, dir); +#endif + +while (len) { + err = dma->translate (dma, addr, &paddr, &plen, dir); +if (err) { +return -1 ;
Why not return err?
Quoted 46 lines
dma-helpers.c +46
-0+ } + +/* The translation might be valid for larger regions. */ +if (plen > len) { + plen = len; + } + +cpu_physical_memory_rw (paddr, buf, plen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + + len -= plen; + addr += plen; + buf += plen; + } + +return 0 ; +} + +int __dma_memory_zero (DMAContext *dma,dma_addr_t addr,dma_addr_t len) +{ +target_phys_addr_t paddr, plen; +int err; + +#ifdef DEBUG_IOMMU +fprintf (stderr,"dma_memory_zero iommu=%p addr=0x%llx len=0x%llx\n" , + (unsigned long long )addr, (unsigned long long )len); +#endif + +while (len) { + err = dma->translate (dma, addr, &paddr, &plen, +DMA_DIRECTION_FROM_DEVICE ); +if (err) { +return -1 ; + } + +/* The translation might be valid for larger regions. */ +if (plen > len) { + plen = len; + } + +cpu_physical_memory_zero (paddr, plen); + + len -= plen; + addr += plen; + } +
dma-helpers.c void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, · line 360
Quoted 10 lines
dma-helpers.c +10
-0+return 0 ; +} + +typedef struct DMAMemoryMap DMAMemoryMap ; +struct DMAMemoryMap { +dma_addr_t addr; +size_t len; +void *buf; +DMAInvalidateMapFunc *invalidate; +void *invalidate_opaque;
Do we absolutely need the opaque value?
Can we let the user allocate the map objects? Then user can use safe container_of to get user data.
Quoted 126 lines
dma-helpers.c +86
-0+ +QLIST_ENTRY (DMAMemoryMap) list; +}; + +void dma_context_init (DMAContext *dma,DMATranslateFunc fn,void *opaque) +{ + dma->translate = fn; + dma->opaque = opaque; +QLIST_INIT (&dma->memory_maps); +} + +void dma_invalidate_memory_range (DMAContext *dma, +dma_addr_t addr,dma_addr_t len) +{ +DMAMemoryMap *map; + + QLIST_FOREACH(map, &dma->memory_maps, list) { +if (ranges_overlap (addr, len, map->addr, map->len)) { + map->invalidate (map->invalidate_opaque); +QLIST_REMOVE (map, list); +free (map); + } + } +} + +void *__dma_memory_map (DMAContext *dma,DMAInvalidateMapFunc *cb, +void *cb_opaque,dma_addr_t addr,dma_addr_t *len, +DMADirection dir) +{ +int err; +target_phys_addr_t paddr, plen; +void *buf; + + 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 cpu_physical_memory_map() does. +*/ +if (plen < *len) { + *len = plen; + } + + buf =cpu_physical_memory_map (paddr, &plen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + *len = plen; + +/* We treat maps as remote TLBs to cope with stuff like AIO. */ +if (cb) { +DMAMemoryMap *map; + + map =g_malloc (sizeof (DMAMemoryMap)); + map->addr = addr; + map->len = *len; + map->buf = buf; + map->invalidate = cb; + map->invalidate_opaque = cb_opaque; + +QLIST_INSERT_HEAD (&dma->memory_maps, map, list); + } + +return buf; +} + +void __dma_memory_unmap (DMAContext *dma,void *buffer,dma_addr_t len, +DMADirection dir,dma_addr_t access_len) +{ +DMAMemoryMap *map; + +cpu_physical_memory_unmap (buffer, len, + dir ==DMA_DIRECTION_FROM_DEVICE , + access_len); + + QLIST_FOREACH(map, &dma->memory_maps, list) { +if ((map->buf == buffer) && (map->len == len)) { +QLIST_REMOVE (map, list); +free (map); + } + } +} +#endif /* CONFIG_IOMMU */ dma.h +21
-0@@ -15,6 +15,7 @@#include "hw/hw.h" #include "block.h" +typedef struct DMAContext DMAContext ;typedef struct ScatterGatherEntry ScatterGatherEntry ; typedef enum {@@ -31,30 +32,83 @@ struct QEMUSGList { };#if defined(TARGET_PHYS_ADDR_BITS ) + +#ifdef CONFIG_IOMMU +/* +* When an IOMMU is present, bus addresses become distinct from +* CPU/memory physical addresses and may be a different size. Because +* the IOVA size depends more on the bus than on the platform, we more +* or less have to treat these as 64-bit always to cover all (or at +* least most) cases. +*/ +typedef uint64_t dma_addr_t ; + +#define DMA_ADDR_BITS 64 +#define DMA_ADDR_FMT "%" PRIx64 +#else typedef target_phys_addr_t dma_addr_t ;#define DMA_ADDR_BITS TARGET_PHYS_ADDR_BITS#define DMA_ADDR_FMT TARGET_FMT_plx +#endif + +typedef int DMATranslateFunc(DMAContext *dma, + dma_addr_t addr, + target_phys_addr_t *paddr, + target_phys_addr_t *len,
dma.h struct QEMUSGList { · line 68
Quoted 10 lines
dma.h +10
-0+ DMADirection dir); + +typedef struct DMAContext { +#ifdef CONFIG_IOMMU +DMATranslateFunc *translate; +void *opaque; + QLIST_HEAD(memory_maps, DMAMemoryMap) memory_maps; +#endif +}DMAContext ; +
Here I suspect opaque is not needed at all.
dma.h struct QEMUSGList { · line 73
Quoted 5 lines
dma.h +5
-0+#ifdef CONFIG_IOMMU +static inline bool dma_has_iommu(DMACon.text *dma) +{ +return (dma !=NULL ); +}
() not necessary here. Neither is != NULL.
Quoted 31 lines
dma.h +18
-4+#else +static inline bool dma_has_iommu (DMAContext *dma) +{ +return false; +} +#endif typedef void DMAInvalidateMapFunc (void *);/* Checks that the given range of addresses is valid for DMA. This is * useful for certain cases, but usually you should just use * dma_memory_{read,write}() and check for errors */ -static inline bool dma_memory_valid (DMAContext *dma,dma_addr_t addr, -dma_addr_t len,DMADirection dir) +bool __dma_memory_valid (DMAContext *dma,dma_addr_t addr,dma_addr_t len, +DMADirection dir); +static inline bool dma_memory_valid (DMAContext *dma, +dma_addr_t addr,dma_addr_t len, +DMADirection dir) { -/* Stub version, with no iommu we assume all bus addresses are valid */ -return true; +if (!dma_has_iommu (dma)) { +return true; + }else { +return __dma_memory_valid (dma, addr, len, dir); + } } +int __dma_memory_rw (DMAContext *dma,dma_addr_t addr, +void *buf,dma_addr_t len,DMADirection dir);
dma.h struct QEMUSGList { · line 106
Quoted 10 lines
dma.h +3
-4static inline int dma_memory_rw (DMAContext *dma,dma_addr_t addr,void *buf,dma_addr_t len,DMADirection dir) { -/* Stub version when we have no iommu support */ -cpu_physical_memory_rw (addr, buf, (target_phys_addr_t )len, - dir ==DMA_DIRECTION_FROM_DEVICE ); -return 0 ; +if (!dma_has_iommu (dma)) { +/* Fast-path for no IOMMU */ + cpu_physical_memory_rw(addr, buf, (target_phys_addr_t)len,
cast not needed, I think.
Quoted 47 lines
dma.h +20
-11+ dir == DMA_DIRECTION_FROM_DEVICE); +return 0 ; + }else { +return __dma_memory_rw (dma, addr, buf, len, dir); + } } static inline int dma_memory_read(DMAContext *dma, dma_addr_t addr,@@ -70,35 +124,55 @@ static inline int dma_memory_write(DMAContext *dma, dma_addr_t addr, DMA_DIRECTION_FROM_DEVICE); } +int __dma_memory_zero (DMAContext *dma,dma_addr_t addr,dma_addr_t len);static inline int dma_memory_zero (DMAContext *dma,dma_addr_t addr,dma_addr_t len) { -/* Stub version when we have no iommu support */ -cpu_physical_memory_zero (addr, len); -return 0 ; +if (!dma_has_iommu (dma)) { +/* Fast-path for no IOMMU */ +cpu_physical_memory_zero (addr, len); +return 0 ; + }else { +return __dma_memory_zero (dma, addr, len); + } } +void *__dma_memory_map (DMAContext *dma, +DMAInvalidateMapFunc *cb,void *opaque, +dma_addr_t addr,dma_addr_t *len, +DMADirection dir);static inline void *dma_memory_map (DMAContext *dma, -DMAInvalidateMapFunc *cb,void *opaque, +DMAInvalidateMapFunc *cb,void *cb_opaque,dma_addr_t addr,dma_addr_t *len,DMADirection dir) { -target_phys_addr_t xlen = *len; -void *p; - - p =cpu_physical_memory_map (addr, &xlen, - dir ==DMA_DIRECTION_FROM_DEVICE ); - *len = xlen; -return p; +if (!dma_has_iommu (dma)) { +target_phys_addr_t xlen = *len;
dma.h static inline int dma_memory_write(DMAContext *dma, dma_addr_t addr, · line 160
Quoted 10 lines
dma.h +9
-0+void *p; + + p =cpu_physical_memory_map (addr, &xlen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + *len = xlen; +return p; + }else { +return __dma_memory_map (dma, cb, cb_opaque, addr, len, dir); + } }
Are callbacks ever useful? I note that without an iommu, they are never invoked. So how can a device use them?
Quoted 38 lines
dma.h +18
-3+void __dma_memory_unmap (DMAContext *dma, +void *buffer,dma_addr_t len, +DMADirection dir,dma_addr_t access_len);static inline void dma_memory_unmap (DMAContext *dma,void *buffer,dma_addr_t len,DMADirection dir,dma_addr_t access_len) { -return cpu_physical_memory_unmap (buffer, (target_phys_addr_t )len, - dir ==DMA_DIRECTION_FROM_DEVICE , - access_len); +if (!dma_has_iommu (dma)) { +return cpu_physical_memory_unmap (buffer, (target_phys_addr_t )len, + dir ==DMA_DIRECTION_FROM_DEVICE , + access_len); + }else { +__dma_memory_unmap (dma, buffer, len, dir, access_len); + } }#define DEFINE_LDST_DMA (_lname, _sname, _bits, _end) \@@ -139,6 +213,14 @@ DEFINE_LDST_DMA(q, q, 64, be);#undef DEFINE_LDST_DMA +#ifdef CONFIG_IOMMU + +void dma_context_init (DMAContext *dma,DMATranslateFunc ,void *opaque); +void dma_invalidate_memory_range (DMAContext *dma, +dma_addr_t addr,dma_addr_t len); + +#endif /* CONFIG_IOMMU */ +struct ScatterGatherEntry {dma_addr_t base;dma_addr_t len;Signature · 1 line
1.7.9
Re: [Qemu-devel] [PATCH 10/13] iommu: Introduce IOMMU emulation infrastructure
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · David Gibson
This patch adds the basic infrastructure necessary to emulate an IOMMU visible to the guest. The DMAContext structure is extended with information and a callback describing the translation, and the various DMA functions used by devices will now perform IOMMU translation using this callback.
Cc Michael S. Tsirkin <[email protected]>Cc Richard Henderson <[email protected]>
Quoted 7 lines
Quoted 5 lines
Signed-off-by Eduard - Gabriel Munteanu <[email protected]>Signed-off-by David Gibson <[email protected]>--- configure | 12 ++++ dma-helpers.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++Why not just
dma.c?
Because dma-helpers.c already includes most of the functions defined in dma.h, and because it would increase risk of confusion with hw/dma.c which contains code specific to the old ISA DMA controller.
Quoted 104 lines
Quoted 93 lines
dma.h | 126 +++++++++++++++++++++++++++++++------- 3 files changed, 305 insertions(+), 22 deletions(-)configure +12
-0@@ -138,6 +138,7 @@ linux_aio="" cap_ng="" attr="" libattr="" +iommu="yes" xfs="" vhost_net="no"@@ -784,6 +785,10 @@ for opt do ;; --enable-vhost-net) vhost_net="yes" ;; + --enable-iommu) iommu="yes" + ;; + --disable-iommu) iommu="no" + ;; --disable-opengl) opengl="no" ;; --enable-opengl) opengl="yes"@@ -1085,6 +1090,8 @@ echo " --enable-docs enable documentation build" echo " --disable-docs disable documentation build" echo " --disable-vhost-net disable vhost-net acceleration support" echo " --enable-vhost-net enable vhost-net acceleration support" +echo " --disable-iommu disable IOMMU emulation support" +echo " --enable-iommu enable IOMMU emulation support" echo " --enable-trace-backend=B Set trace backend" echo " Available backends:" $("$source_path"/scripts/tracetool --list-backends) echo " --with-trace-file=NAME Full PATH,NAME of file to store traces"@@ -2935,6 +2942,7 @@ echo "posix_madvise $posix_madvise" echo "uuid support $uuid" echo "libcap-ng support $cap_ng" echo "vhost-net support $vhost_net" +echo "IOMMU support $iommu" echo "Trace backend $trace_backend" echo "Trace output file $trace_file-<pid>" echo "spice support $spice"@@ -3812,6 +3820,10 @@ if test "$target_softmmu" = "yes" -a \( \ echo "CONFIG_NEED_MMU=y" >> $config_target_mak fi +if test "$iommu" = "yes" ; then + echo "CONFIG_IOMMU=y" >> $config_host_mak +fi + if test "$gprof" = "yes" ; then echo "TARGET_GPROF=yes" >> $config_target_mak if test "$target_linux_user" = "yes" ; thendma-helpers.c +24
-0@@ -10,6 +10,9 @@#include "dma.h" #include "block_int.h" #include "trace.h" +#include "range.h" + +/*#define DEBUG_IOMMU*/ void qemu_sglist_init (QEMUSGList *qsg,int alloc_hint,DMAContext *dma) {@@ -255,3 +258,189 @@ void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, {bdrv_acct_start (bs, cookie, sg->size, type); } + +#ifdef CONFIG_IOMMU +bool __dma_memory_valid (DMAContext *dma,dma_addr_t addr,dma_addr_t len, +DMADirection dir) +{ +target_phys_addr_t paddr, plen; + +#ifdef DEBUG_DMA +fprintf (stderr,"dma_memory_check iommu=%p addr=0x%llx len=0x%llx dir=%d\n" , + (unsigned long long )addr, (unsigned long long )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; + }dma-helpers.c void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, · line 291
Quoted 10 lines
dma-helpers.c +10
-0+ + len -= plen; + addr += plen; + } + +return true; +} + +int __dma_memory_rw (DMAContext *dma,dma_addr_t addr, +void *buf,dma_addr_t len,DMADirection dir)
Quoted 7 lines
By the way, users still ignore the return code from the pci routines.
I suspect the API of returning errors is just not such a good one. What happens on read errors with real devices? Master abort? What about write errors? Maybe we need a callback
that will set error flags in the device instead.
Well, it depends on the device. I think in most cases it would trigger some sort of error inside the device, which would most easily be handled by an error return from here. In some cases it might put the whole bus in an error state.
I don't think treating the error state of these calls as optional information for the driver is necessarily a problem. As long as accesses to not IOMMU mapped addresses don't lead to actual accesses to guest memory (which they don't) we've satisfied the main semantic of the IOMMU.
Whether and how the devices respond to the error condition is then a matter of how complete and accurate we want their modelling of error states to be. That seems to me to be in the nice-to-have but not really that vital category, which we can leave until someone wants it.
Quoted 7 lines
Quoted 7 lines
dma-helpers.c +7
-0+{ +target_phys_addr_t paddr, plen; +int err; + +#ifdef DEBUG_IOMMU +fprintf (stderr,"dma_memory_rw iommu=%p addr=0x%llx len=0x%llx dir=%d\n" , + (unsigned long long )addr, (unsigned long long )len, dir);
Quoted 8 lines
dma-helpers.c void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, · line 304
Quoted 6 lines
dma-helpers.c +6
-0+#endif + +while (len) { + err = dma->translate (dma, addr, &paddr, &plen, dir); +if (err) { +return -1 ;Why not return err?
No good reason, I'll fix that.
Quoted 52 lines
Quoted 52 lines
dma-helpers.c +52
-0+ } + +/* The translation might be valid for larger regions. */ +if (plen > len) { + plen = len; + } + +cpu_physical_memory_rw (paddr, buf, plen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + + len -= plen; + addr += plen; + buf += plen; + } + +return 0 ; +} + +int __dma_memory_zero (DMAContext *dma,dma_addr_t addr,dma_addr_t len) +{ +target_phys_addr_t paddr, plen; +int err; + +#ifdef DEBUG_IOMMU +fprintf (stderr,"dma_memory_zero iommu=%p addr=0x%llx len=0x%llx\n" , + (unsigned long long )addr, (unsigned long long )len); +#endif + +while (len) { + err = dma->translate (dma, addr, &paddr, &plen, +DMA_DIRECTION_FROM_DEVICE ); +if (err) { +return -1 ; + } + +/* The translation might be valid for larger regions. */ +if (plen > len) { + plen = len; + } + +cpu_physical_memory_zero (paddr, plen); + + len -= plen; + addr += plen; + } + +return 0 ; +} + +typedef struct DMAMemoryMap DMAMemoryMap ; +struct DMAMemoryMap { +dma_addr_t addr;
Quoted 8 lines
dma-helpers.c void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, · line 360
Quoted 4 lines
dma-helpers.c +4
-0+size_t len; +void *buf; +DMAInvalidateMapFunc *invalidate; +void *invalidate_opaque;Do we absolutely need the opaque value?
Can we let the user allocate the map objects? Then user can use safecontainer_ofto get user data.
So, I was wondering whether having users allocate the DMAMemoryMap structure might be better. It has some advantages, though it does mean more invasive changes to the callers. I'll take that as a vote in favour of that alternative approach.
Quoted 129 lines
Quoted 129 lines
dma-helpers.c +85
-0+QLIST_ENTRY (DMAMemoryMap) list; +}; + +void dma_context_init (DMAContext *dma,DMATranslateFunc fn,void *opaque) +{ + dma->translate = fn; + dma->opaque = opaque; +QLIST_INIT (&dma->memory_maps); +} + +void dma_invalidate_memory_range (DMAContext *dma, +dma_addr_t addr,dma_addr_t len) +{ +DMAMemoryMap *map; + + QLIST_FOREACH(map, &dma->memory_maps, list) { +if (ranges_overlap (addr, len, map->addr, map->len)) { + map->invalidate (map->invalidate_opaque); +QLIST_REMOVE (map, list); +free (map); + } + } +} + +void *__dma_memory_map (DMAContext *dma,DMAInvalidateMapFunc *cb, +void *cb_opaque,dma_addr_t addr,dma_addr_t *len, +DMADirection dir) +{ +int err; +target_phys_addr_t paddr, plen; +void *buf; + + 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 cpu_physical_memory_map() does. +*/ +if (plen < *len) { + *len = plen; + } + + buf =cpu_physical_memory_map (paddr, &plen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + *len = plen; + +/* We treat maps as remote TLBs to cope with stuff like AIO. */ +if (cb) { +DMAMemoryMap *map; + + map =g_malloc (sizeof (DMAMemoryMap)); + map->addr = addr; + map->len = *len; + map->buf = buf; + map->invalidate = cb; + map->invalidate_opaque = cb_opaque; + +QLIST_INSERT_HEAD (&dma->memory_maps, map, list); + } + +return buf; +} + +void __dma_memory_unmap (DMAContext *dma,void *buffer,dma_addr_t len, +DMADirection dir,dma_addr_t access_len) +{ +DMAMemoryMap *map; + +cpu_physical_memory_unmap (buffer, len, + dir ==DMA_DIRECTION_FROM_DEVICE , + access_len); + + QLIST_FOREACH(map, &dma->memory_maps, list) { +if ((map->buf == buffer) && (map->len == len)) { +QLIST_REMOVE (map, list); +free (map); + } + } +} +#endif /* CONFIG_IOMMU */ dma.h +25
-0@@ -15,6 +15,7 @@#include "hw/hw.h" #include "block.h" +typedef struct DMAContext DMAContext ;typedef struct ScatterGatherEntry ScatterGatherEntry ; typedef enum {@@ -31,30 +32,83 @@ struct QEMUSGList { }; #if defined(TARGET_PHYS_ADDR_BITS) + +#ifdef CONFIG_IOMMU +/* + * When an IOMMU is present, bus addresses become distinct from + * CPU/memory physical addresses and may be a different size. Because + * the IOVA size depends more on the bus than on the platform, we more + * or less have to treat these as 64-bit always to cover all (or at + * least most) cases. + */ +typedef uint64_t dma_addr_t; + +#define DMA_ADDR_BITS 64 +#define DMA_ADDR_FMT "%" PRIx64 +#else typedef target_phys_addr_t dma_addr_t; #define DMA_ADDR_BITS TARGET_PHYS_ADDR_BITS #define DMA_ADDR_FMT TARGET_FMT_plx +#endif + +typedef int DMATranslateFunc(DMAContext *dma, + dma_addr_t addr, + target_phys_addr_t *paddr, + target_phys_addr_t *len, + DMADirection dir); + +typedef struct DMAContext { +#ifdef CONFIG_IOMMU
Quoted 8 lines
dma.h struct QEMUSGList { · line 68
Quoted 6 lines
dma.h +6
-0+DMATranslateFunc *translate; +void *opaque; + QLIST_HEAD(memory_maps, DMAMemoryMap) memory_maps; +#endif +} DMAContext; +Here I suspect opaque is not needed at all.
Ok, so. It's certainly not needed for all cases - I don't use it in the example PAPR IOMMU. The use cases I had in mind for this are for things more like the Intel IOMMU which can be set up to have a different domain (i.e. DMAContext) for each PCI function. I was thinking in that case that opaque could be used to store a pointer to the relevant PCIDevice.
Or alternatively, container_of could be used to access per-context IOMMU specific information, but opaque could be used to get at a global IOMMU state structure.
But now you come to mention it, those cases can probably be handled by extra pointers in the containing structure. So I'll drop this in the next version.
Quoted 7 lines
dma.h struct QEMUSGList { · line 73
Quoted 5 lines
dma.h +5
-0+#ifdef CONFIG_IOMMU +static inline bool dma_has_iommu(DMACon.text *dma) +{ +return (dma !=NULL ); +}() not necessary here. Neither is != NULL.
I know, but I was trying to be clearer and make the pointer -> bool conversion explicit here.
Quoted 29 lines
Quoted 29 lines
dma.h +16
-4+#else +static inline bool dma_has_iommu (DMAContext *dma) +{ +return false; +} +#endif typedef void DMAInvalidateMapFunc (void *);/* Checks that the given range of addresses is valid for DMA. This is * useful for certain cases, but usually you should just use * dma_memory_{read,write}() and check for errors */ -static inline bool dma_memory_valid (DMAContext *dma,dma_addr_t addr, -dma_addr_t len,DMADirection dir) +bool __dma_memory_valid (DMAContext *dma,dma_addr_t addr,dma_addr_t len, +DMADirection dir); +static inline bool dma_memory_valid (DMAContext *dma, +dma_addr_t addr,dma_addr_t len, +DMADirection dir) { -/* Stub version, with no iommu we assume all bus addresses are valid */ -return true; +if (!dma_has_iommu (dma)) { +return true; + }else { +return __dma_memory_valid (dma, addr, len, dir); + } }
Quoted 14 lines
dma.h struct QEMUSGList { · line 106
Quoted 12 lines
dma.h +5
-4+int __dma_memory_rw (DMAContext *dma,dma_addr_t addr, +void *buf,dma_addr_t len,DMADirection dir);static inline int dma_memory_rw (DMAContext *dma,dma_addr_t addr,void *buf,dma_addr_t len,DMADirection dir) { -/* Stub version when we have no iommu support */ -cpu_physical_memory_rw (addr, buf, (target_phys_addr_t )len, - dir ==DMA_DIRECTION_FROM_DEVICE ); -return 0 ; +if (!dma_has_iommu (dma)) { +/* Fast-path for no IOMMU */ + cpu_physical_memory_rw(addr, buf, (target_phys_addr_t)len,cast not needed, I think.
Ah, probably true, I'll remove it.
Quoted 52 lines
Quoted 52 lines
dma.h +25
-11+ dir == DMA_DIRECTION_FROM_DEVICE); +return 0 ; + }else { +return __dma_memory_rw (dma, addr, buf, len, dir); + } } static inline int dma_memory_read(DMAContext *dma, dma_addr_t addr,@@ -70,35 +124,55 @@ static inline int dma_memory_write(DMAContext *dma, dma_addr_t addr, DMA_DIRECTION_FROM_DEVICE); } +int __dma_memory_zero (DMAContext *dma,dma_addr_t addr,dma_addr_t len);static inline int dma_memory_zero (DMAContext *dma,dma_addr_t addr,dma_addr_t len) { -/* Stub version when we have no iommu support */ -cpu_physical_memory_zero (addr, len); -return 0 ; +if (!dma_has_iommu (dma)) { +/* Fast-path for no IOMMU */ +cpu_physical_memory_zero (addr, len); +return 0 ; + }else { +return __dma_memory_zero (dma, addr, len); + } } +void *__dma_memory_map (DMAContext *dma, +DMAInvalidateMapFunc *cb,void *opaque, +dma_addr_t addr,dma_addr_t *len, +DMADirection dir);static inline void *dma_memory_map (DMAContext *dma, -DMAInvalidateMapFunc *cb,void *opaque, +DMAInvalidateMapFunc *cb,void *cb_opaque,dma_addr_t addr,dma_addr_t *len,DMADirection dir) { -target_phys_addr_t xlen = *len; -void *p; - - p =cpu_physical_memory_map (addr, &xlen, - dir ==DMA_DIRECTION_FROM_DEVICE ); - *len = xlen; -return p; +if (!dma_has_iommu (dma)) { +target_phys_addr_t xlen = *len; +void *p; + + p =cpu_physical_memory_map (addr, &xlen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + *len = xlen;
Quoted 8 lines
dma.h static inline int dma_memory_write(DMAContext *dma, dma_addr_t addr, · line 160
Quoted 5 lines
dma.h +4
-0+return p; + }else { +return __dma_memory_map (dma, cb, cb_opaque, addr, len, dir); + } }Are callbacks ever useful? I note that without an iommu, they are never invoked. So how can a device use them?
So, the only real user of callbacks in this series is the dma_bdrv code, where the callback cancels the remainder of the AIO in progress. The semantics of the callback are supposed to be that once the callback is complete, the device code will make no further accesses to the mapped memory.
But this is a somewhat curly issue, and I've certainly thought about other approaches to this. Several ways of handling invalidate-while-mapped have occurred to me:
1) Ignore the problem (no callback, or optional callback). If the guest doesn't quiesce the device to cancel DMAs before removing IOMMU mappins, well, too bad, it shoots itself in the foot, and in-progress DMAs may still clobber no-longer-mapped guest memory.
The problem with that approach is that one use of an IOMMU is to forcibly quiesce a device (including one in a broken state) by prohibiting all its DMAs, then dealing with resetting the device later on. This approach will not faithfully model that use case.
2) The current approach, have a callback with the strong semantic requirement that no further access occur after the callback is completed. The difficulty with that is that synchronization between the callback and the code using the mapping could be hairy, especially if qemu becomes multi-threaded in future.
3) "Lock" the iommu. When maps are in use, mark the IOMMU such that any invalidates must be delayed until the unmap happens.
4) Instead of dma_memory_map() providing direct pointers into guest memory, use mremap() to obtain the new mapping. On invalidate, replace the mapping with a dummy chunk of memory. That way the device code doesn't SEGV and doesn't need synchronization with the invalidate, but won't be able to clobber guest memory after the invalidate. This might entirely cancel the performance reasons for using dma_memory_map() in the first place, though.
Oh, and I suppose for completeness there's"
5) Remove dma_memory_map() entirely, convert all devices to use dma_memory_rw() instead.
So suggestions here welcome.
Quoted 35 lines
Quoted 35 lines
dma.h +18
-3+void __dma_memory_unmap (DMAContext *dma, +void *buffer,dma_addr_t len, +DMADirection dir,dma_addr_t access_len);static inline void dma_memory_unmap (DMAContext *dma,void *buffer,dma_addr_t len,DMADirection dir,dma_addr_t access_len) { -return cpu_physical_memory_unmap (buffer, (target_phys_addr_t )len, - dir ==DMA_DIRECTION_FROM_DEVICE , - access_len); +if (!dma_has_iommu (dma)) { +return cpu_physical_memory_unmap (buffer, (target_phys_addr_t )len, + dir ==DMA_DIRECTION_FROM_DEVICE , + access_len); + }else { +__dma_memory_unmap (dma, buffer, len, dir, access_len); + } }#define DEFINE_LDST_DMA (_lname, _sname, _bits, _end) \@@ -139,6 +213,14 @@ DEFINE_LDST_DMA(q, q, 64, be);#undef DEFINE_LDST_DMA +#ifdef CONFIG_IOMMU + +void dma_context_init (DMAContext *dma,DMATranslateFunc ,void *opaque); +void dma_invalidate_memory_range (DMAContext *dma, +dma_addr_t addr,dma_addr_t len); + +#endif /* CONFIG_IOMMU */ +struct ScatterGatherEntry {dma_addr_t base;dma_addr_t len;
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
PATCH 11/13 pseries: Convert sPAPR TCEs to use generic IOMMU infrastructure
In reply to David GibsonThe pseries platform already contains an IOMMU implementation, since it is essential for the platform's paravirtualized VIO devices. This IOMMU support is currently built into the implementation of the VIO "bus" and the various VIO devices.
This patch converts this code to make use of the new common IOMMU infrastructure.
--- Makefile.target | 2 +- configure | 2 +- hw/spapr.c | 3 + hw/spapr.h | 16 +++ hw/spapr_iommu.c | 238 +++++++++++++++++++++++++++++++++++++++++++++ hw/spapr_llan.c | 61 ++++++------ hw/spapr_vio.c | 282 ++++-------------------------------------------------- hw/spapr_vio.h | 71 +++++++------- hw/spapr_vscsi.c | 24 +++-- target-ppc/kvm.c | 4 +- 10 files changed, 361 insertions(+), 342 deletions(-) create mode 100644 hw/spapr_iommu.c
Makefile.target +1 -1
@@ -256,7 +256,7 @@ obj-ppc-y += ppc_oldworld.o # NewWorld PowerMac obj-ppc-y += ppc_newworld.o # IBM pSeries (sPAPR) -obj-ppc-$(CONFIG_PSERIES) += spapr.o spapr_hcall.o spapr_rtas.o spapr_vio.o +obj-ppc-$(CONFIG_PSERIES) += spapr.o spapr_hcall.o spapr_rtas.o spapr_vio.o spapr_iommu.o obj-ppc-$(CONFIG_PSERIES) += xics.o spapr_vty.o spapr_llan.o spapr_vscsi.o obj-ppc-$(CONFIG_PSERIES) += spapr_pci.o device-hotplug.o pci-hotplug.o # PowerPC 4xx boards
configure +1 -1
@@ -3651,7 +3651,7 @@ case "$target_arch2" in fi fi esac -if test "$target_arch2" = "ppc64" -a "$fdt" = "yes"; then +if test "$target_arch2" = "ppc64" -a "$fdt" = "yes" -a "$iommu" = "yes" ; then echo "CONFIG_PSERIES=y" >> $config_target_mak fi if test "$target_bigendian" = "yes" ; then
hw/spapr.c +3 -0
@@ -618,6 +618,9 @@ static void ppc_spapr_init(ram_addr_t ram_size, spapr->icp =xics_system_init (XICS_IRQS ); spapr->next_irq =16 ; +/* Set up IOMMU */ +spapr_iommu_init (); +/* Set up VIO bus */ spapr->vio_bus =spapr_vio_bus_init ();
hw/spapr.h +16 -0
@@ -318,4 +318,20 @@ target_ulong spapr_rtas_call(sPAPREnvironment *spapr,int spapr_rtas_device_tree_setup (void *fdt,target_phys_addr_t rtas_addr,target_phys_addr_t rtas_size); +#define SPAPR_TCE_PAGE_SHIFT 12 +#define SPAPR_TCE_PAGE_SIZE (1ULL << SPAPR_TCE_PAGE_SHIFT) +#define SPAPR_TCE_PAGE_MASK (SPAPR_TCE_PAGE_SIZE - 1) + +typedef struct sPAPRTCE { +uint64_t tce; +}sPAPRTCE ; + +#define SPAPR_VIO_BASE_LIOBN 0x00000000 + +void spapr_iommu_init (void ); +DMAContext *spapr_tce_new_dma_context (uint32_t liobn,size_t window_size); +void spapr_tce_free (DMAContext *dma); +int spapr_dma_dt (void *fdt,int node_off,const char *propname, +DMAContext *dma); +#endif /* !defined (__HW_SPAPR_H__) */
hw/spapr_iommu.c new file +238 -0
238 lines not on this page · raw message
hw/spapr_llan.c +32 -29
159 lines not on this page · raw message
hw/spapr_vio.c +19 -263
351 lines not on this page · raw message
hw/spapr_vio.h +36 -35
108 lines not on this page · raw message
hw/spapr_vscsi.c +13 -11
@@ -169,7 +169,7 @@ static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,long rc, rc1;/* First copy the SRP */ - rc =spapr_tce_dma_write (&s->vdev, req->crq.s.IU_data_ptr, + rc =spapr_vio_dma_write (&s->vdev, req->crq.s.IU_data_ptr, &req->iu, length);if (rc) {fprintf (stderr,"vscsi_send_iu: DMA write failure !\n" );
@@ -285,9 +285,9 @@ static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req, llen =MIN (len, md->len);if (llen) {if (req->writing) {/* writing = to device = reading from memory */ - rc =spapr_tce_dma_read (&s->vdev, md->va, buf, llen); + rc =spapr_vio_dma_read (&s->vdev, md->va, buf, llen); }else { - rc =spapr_tce_dma_write (&s->vdev, md->va, buf, llen); + rc =spapr_vio_dma_write (&s->vdev, md->va, buf, llen); } } md->len -= llen;
@@ -333,10 +333,11 @@ static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req, md = req->cur_desc = &req->ext_desc;dprintf ("VSCSI: Reading desc from 0x%llx\n" , (unsigned long long )td->va); - rc =spapr_tce_dma_read (&s->vdev, td->va, md, + rc =spapr_vio_dma_read (&s->vdev, td->va, md,sizeof (struct srp_direct_buf ));if (rc) { -dprintf ("VSCSI: tce_dma_read -> %d reading ext_desc\n" , rc); +dprintf ("VSCSI: spapr_vio_dma_read -> %d reading ext_desc\n" , + rc);break ; }vscsi_swap_desc (md);
@@ -349,12 +350,12 @@ static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,/* Perform transfer */ llen =MIN (len, md->len);if (req->writing) {/* writing = to device = reading from memory */ - rc =spapr_tce_dma_read (&s->vdev, md->va, buf, llen); + rc =spapr_vio_dma_read (&s->vdev, md->va, buf, llen); }else { - rc =spapr_tce_dma_write (&s->vdev, md->va, buf, llen); + rc =spapr_vio_dma_write (&s->vdev, md->va, buf, llen); }if (rc) { -dprintf ("VSCSI: tce_dma_r/w(%d) -> %d\n" , req->writing, rc); +dprintf ("VSCSI: spapr_vio_dma_r/w(%d) -> %d\n" , req->writing, rc);break ; } dprintf("VSCSI: data: %02x %02x %02x %02x...\n",
@@ -732,7 +733,7 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) sinfo = &req->iu.mad.adapter_info;#if 0 /* What for ? */ - rc =spapr_tce_dma_read (&s->vdev,be64_to_cpu (sinfo->buffer), + rc =spapr_vio_dma_read (&s->vdev,be64_to_cpu (sinfo->buffer), &info,be16_to_cpu (sinfo->common.length));if (rc) {fprintf (stderr,"vscsi_send_adapter_info: DMA read failure !\n" );
@@ -746,7 +747,7 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) info.os_type =cpu_to_be32 (2 ); info.port_max_txu[0 ] =cpu_to_be32 (VSCSI_MAX_SECTORS <<9 ); - rc =spapr_tce_dma_write (&s->vdev,be64_to_cpu (sinfo->buffer), + rc =spapr_vio_dma_write (&s->vdev,be64_to_cpu (sinfo->buffer), &info,be16_to_cpu (sinfo->common.length));if (rc) {fprintf (stderr,"vscsi_send_adapter_info: DMA write failure !\n" );
@@ -808,7 +809,7 @@ static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq) }/* XXX Handle failure differently ? */ -if (spapr_tce_dma_read (&s->vdev, crq->s.IU_data_ptr, &req->iu, +if (spapr_vio_dma_read (&s->vdev, crq->s.IU_data_ptr, &req->iu, crq->s.IU_length)) {fprintf (stderr,"vscsi_got_payload: DMA read failure !\n" );g_free (req);
@@ -964,6 +965,7 @@ static void spapr_vscsi_class_init(ObjectClass *klass, void *data) k->dt_compatible ="IBM,v-scsi" ; k->signal_mask =0x00000001 ; dc->props = spapr_vscsi_properties; + k->rtce_window_size =0x10000000 ; }static TypeInfo spapr_vscsi_info = {
target-ppc/kvm.c +2 -2
@@ -858,7 +858,7 @@ void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd)return NULL ; } - len = (window_size /SPAPR_VIO_TCE_PAGE_SIZE ) *sizeof (VIOsPAPR_RTCE); + len = (window_size /SPAPR_TCE_PAGE_SIZE ) *sizeof (sPAPRTCE);/* FIXME: round this up to page size */ table =mmap (NULL , len,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 );
@@ -881,7 +881,7 @@ int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t window_size)return -1 ; } - len = (window_size /SPAPR_VIO_TCE_PAGE_SIZE )*sizeof (VIOsPAPR_RTCE); + len = (window_size /SPAPR_TCE_PAGE_SIZE )*sizeof (sPAPRTCE);if ((munmap (table, len) <0 ) || (close (fd) <0 )) { fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
Signature · 1 line
1.7.9
PATCH 12/13 iommu: Allow PCI to use IOMMU infrastructure
In reply to David GibsonThis patch adds some hooks to let PCI devices and busses use the new IOMMU infrastructure. When IOMMU support is enabled, each PCI device now contains a DMAContext * which is used by the pci_dma_*() wrapper functions.
By default, the contexts are initialized to NULL, assuming no IOMMU. However the platform or host bridge code which sets up the PCI bus can use pci_setup_iommu() to set a function which will determine the correct DMAContext for a given PCI device.
--- hw/pci.c | 13 +++++++++++++ hw/pci.h | 13 ++++++++++++- hw/pci_internals.h | 4 ++++ 3 files changed, 29 insertions(+), 1 deletions(-)
hw/pci.c +13 -0
@@ -761,6 +761,11 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,return NULL ; } pci_dev->bus = bus; +#ifdef CONFIG_IOMMU +if (bus->dma_context_fn) { + pci_dev->dma = bus->dma_context_fn (bus, bus->dma_context_opaque, devfn); + } +#endif pci_dev->devfn = devfn;pstrcpy (pci_dev->name,sizeof (pci_dev->name), name); pci_dev->irq_state =0 ;
@@ -2005,6 +2010,14 @@ static void pci_device_class_init(ObjectClass *klass, void *data) k->bus_info = &pci_bus_info; } +#ifdef CONFIG_IOMMU +void pci_setup_iommu (PCIBus *bus,PCIDMAContextFunc fn,void *opaque) +{ + bus->dma_context_fn = fn; + bus->dma_context_opaque = opaque; +} +#endif +static TypeInfo pci_device_type_info = { .name =TYPE_PCI_DEVICE , .parent =TYPE_DEVICE ,
hw/pci.h +12 -1
@@ -170,6 +170,7 @@ typedef struct PCIDeviceClass {struct PCIDevice {DeviceState qdev; +/* PCI config space */ uint8_t *config;
@@ -191,6 +192,9 @@ struct PCIDevice {uint32_t devfn;char name[64 ];PCIIORegion io_regions[PCI_NUM_REGIONS ]; +#ifdef CONFIG_IOMMU +DMAContext *dma; +#endif /* do not access the following fields */ PCIConfigReadFunc *config_read;
@@ -311,6 +315,10 @@ 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 ); + +void pci_setup_iommu (PCIBus *bus,PCIDMAContextFunc fn,void *opaque); +static inline void pci_set_byte (uint8_t *config,uint8_t val) {
@@ -547,8 +555,11 @@ static inline uint32_t pci_config_size(const PCIDevice *d)/* DMA access functions */ static inline DMAContext *pci_dma_context (PCIDevice *dev) { -/* Stub for when we have no PCI iommu support */ +#ifdef CONFIG_IOMMU +return dev->dma; +#else return NULL ; +#endif } static inline int pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
hw/pci_internals.h +4 -0
@@ -16,6 +16,10 @@ extern struct BusInfo pci_bus_info;struct PCIBus {BusState qbus; +#ifdef CONFIG_IOMMU +PCIDMAContextFunc dma_context_fn; +void *dma_context_opaque; +#endif uint8_t devfn_min;pci_set_irq_fn set_irq;pci_map_irq_fn map_irq;
Signature · 1 line
1.7.9
PATCH 13/13 pseries: Implement IOMMU and DMA for PAPR PCI devices
In reply to David GibsonCurrently the pseries machine emulation does not support DMA for emulated PCI devices, because the PAPR spec always requires a (guest visible, paravirtualized) IOMMU which was not implemented. Now that we have infrastructure for IOMMU emulation, we can correct this and allow PCI DMA for pseries.
With the existing PAPR IOMMU code used for VIO devices, this is almost trivial. We use a single DMAContext for each (virtual) PCI host bridge, which is the usual configuration on real PAPR machines (which often have _many_ PCI host bridges).
--- hw/spapr.h | 1 + hw/spapr_pci.c | 15 +++++++++++++++ hw/spapr_pci.h | 1 + 3 files changed, 17 insertions(+), 0 deletions(-)
hw/spapr.h +1 -0
@@ -327,6 +327,7 @@ typedef struct sPAPRTCE { } sPAPRTCE;#define SPAPR_VIO_BASE_LIOBN 0x00000000 +#define SPAPR_PCI_BASE_LIOBN 0x80000000void spapr_iommu_init (void );DMAContext *spapr_tce_new_dma_context (uint32_t liobn,size_t window_size);
hw/spapr_pci.c +15 -0
@@ -201,6 +201,14 @@ static MemoryRegionOps spapr_io_ops = {/* * PHB PCI device */ +static DMAContext *spapr_pci_dma_context_fn (PCIBus *bus,void *opaque, +int devfn) +{ +sPAPRPHBState *phb = opaque; + +return phb->dma; +} +static int spapr_phb_init (SysBusDevice *s) {sPAPRPHBState *phb =FROM_SYSBUS (sPAPRPHBState, s);
@@ -208,6 +216,7 @@ static int spapr_phb_init(SysBusDevice *s)char namebuf[64 ];int i;PCIBus *bus; +uint32_t liobn;sprintf (busname,"pci@%" PRIx64, phb->buid);
@@ -248,6 +257,10 @@ static int spapr_phb_init(SysBusDevice *s)PCI_DEVFN (0 ,0 ), SPAPR_PCI_NUM_LSI); + liobn =SPAPR_PCI_BASE_LIOBN | (pci_find_domain (bus) <<16 ); + phb->dma =spapr_tce_new_dma_context (liobn,0x40000000 ); +pci_setup_iommu (bus, spapr_pci_dma_context_fn, phb); +QLIST_INSERT_HEAD (&spapr->phbs, phb, list);/* Initialize the LSI table */
@@ -407,5 +420,7 @@ int spapr_populate_pci_devices(sPAPRPHBState *phb,_FDT (fdt_setprop (fdt, bus_off,"interrupt-map" , &interrupt_map,7 *sizeof (interrupt_map[0 ]))); +spapr_dma_dt (fdt, bus_off,"ibm,dma-window" , phb->dma); +return 0 ; }
hw/spapr_pci.h +1 -0
@@ -37,6 +37,7 @@ typedef struct sPAPRPHBState {MemoryRegion memspace, iospace;target_phys_addr_t mem_win_addr, mem_win_size, io_win_addr, io_win_size;MemoryRegion memwindow, iowindow; +DMAContext *dma;struct {uint32_t dt_irq;
Signature · 1 line
1.7.9