iommu series
cover iommu series
Here's the latest drop as discussed on the list. 2 patches are gone, the _set variant is folded in the DMA helpers and I ditched all the broken cancellation bits.
PATCH 1/11 Better support for dma_addr_t variables
From: David Gibson <[email protected]>
A 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, currently an alias to DEFINE_PROP_TADDR().
--- dma.h | 1 + hw/qdev-dma.h | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 hw/qdev-dma.h
dma.h +1 -0
@@ -31,6 +31,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.h new file +12 -0
@@ -0,0 +1,12 @@ +/* +* Support for dma_addr_t typed properties +* +* Copyright (C) 2012 David Gibson, IBM Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ +#include "qdev-addr.h" + +#define DEFINE_PROP_DMAADDR (_n, _s, _f, _d) \ + DEFINE_PROP_TADDR(_n, _s, _f, _d)
Signature · 1 line
1.7.9.5
PATCH 2/11 iommu: Add universal DMA helper functions
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
Not 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-helpers.c | 16 ++++++++++ dma.h | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/pci.h | 21 +++++++------ 3 files changed, 123 insertions(+), 9 deletions(-)
dma-helpers.c +16 -0
@@ -10,6 +10,22 @@#include "dma.h" #include "trace.h" +int dma_memory_set (DMAContext *dma,dma_addr_t addr,uint8_t c,dma_addr_t len) +{ +#define FILLBUF_SIZE 512 +uint8_t fillbuf[FILLBUF_SIZE ]; +int l; + +memset (fillbuf, c,FILLBUF_SIZE ); +while (len >0 ) { + l = len <FILLBUF_SIZE ? len :FILLBUF_SIZE ; +cpu_physical_memory_rw (addr, fillbuf, l, true); + len -= len; + addr += len; + } +return 0 ; +} +void qemu_sglist_init (QEMUSGList *qsg,int alloc_hint) { qsg->sg =g_malloc (alloc_hint *sizeof (ScatterGatherEntry));
dma.h +95 -0
107 lines not on this page · raw message
hw/pci.h +12 -9
@@ -558,10 +558,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 ; }
@@ -581,12 +587,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 );
@@ -602,19 +608,16 @@ PCI_DMA_DEFINE_LDST(q_be, q_be, 64);static inline void *pci_dma_map (PCIDevice *dev,dma_addr_t addr,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), 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,
Signature · 1 line
1.7.9.5
PATCH 3/11 usb-ohci: Use universal DMA helper functions
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
The 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/hcd-ohci.c | 93 +++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 42 deletions(-)
hw/usb/hcd-ohci.c +51 -42
@@ -31,7 +31,7 @@#include "hw/usb.h" #include "hw/pci.h" #include "hw/sysbus.h" -#include "hw/qdev-addr.h" +#include "hw/qdev-dma.h" //#define DEBUG_OHCI /* Dump packet contents. */
@@ -62,6 +62,7 @@ typedef struct {USBBus bus;qemu_irq irq;MemoryRegion mem; +DMAContext *dma;int num_ports;const char *name;
@@ -104,7 +105,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;
@@ -482,14 +483,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); }
@@ -498,7 +499,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;
@@ -506,7 +507,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 ;
@@ -514,14 +515,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); }
@@ -530,7 +531,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;
@@ -538,40 +539,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
@@ -583,64 +584,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);
@@ -803,7 +803,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) {
@@ -827,7 +828,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++)
@@ -1748,11 +1752,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 ();
@@ -1817,7 +1824,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 ];
@@ -1831,7 +1839,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)
@@ -1839,7 +1847,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);
@@ -1875,7 +1884,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.5
PATCH 4/11 iommu: Make sglists and dma_bdrv helpers use new universal DMA helpers
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
dma-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 | 24 ++++++++++++------------ dma.h | 3 ++- hw/ide/ahci.c | 3 ++- hw/ide/macio.c | 4 ++-- hw/pci.h | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-)
dma-helpers.c +12 -12
@@ -26,12 +26,13 @@ int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len)return 0 ; } -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)
@@ -90,10 +91,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); }
@@ -122,7 +122,7 @@ static void dma_complete(DMAAIOCB *dbs, int ret)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);
@@ -139,8 +139,7 @@ static void dma_bdrv_cb(void *opaque, int ret)while (dbs->sg_cur_index < dbs->sg->nsg) { cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte; cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte; - mem =cpu_physical_memory_map (cur_addr, &cur_len, - dbs->dir !=DMA_DIRECTION_TO_DEVICE ); + mem =dma_memory_map (dbs->sg->dma, cur_addr, &cur_len, dbs->dir);if (!mem)break ;qemu_iovec_add (&dbs->iov, mem, cur_len);
@@ -225,7 +224,8 @@ BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs, } -static uint64_t dma_buf_rw (uint8_t *ptr,int32_t len,QEMUSGList *sg,bool to_dev) +static uint64_t dma_buf_rw (uint8_t *ptr,int32_t len,QEMUSGList *sg, +DMADirection dir) {uint64_t resid;int sg_cur_index;
@@ -236,7 +236,7 @@ static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg, bool to_dewhile (len >0 ) {ScatterGatherEntry entry = sg->sg[sg_cur_index++];int32_t xfer =MIN (len, entry.len); -cpu_physical_memory_rw (entry.base, ptr, xfer, !to_dev); +dma_memory_rw (sg->dma, entry.base, ptr, xfer, dir); ptr += xfer; len -= xfer; resid -= xfer;
@@ -247,12 +247,12 @@ static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg, bool to_deuint64_t dma_buf_read (uint8_t *ptr,int32_t len,QEMUSGList *sg) { -return dma_buf_rw (ptr, len, sg,0 ); +return dma_buf_rw (ptr, len, sg,DMA_DIRECTION_FROM_DEVICE ); }uint64_t dma_buf_write (uint8_t *ptr,int32_t len,QEMUSGList *sg) { -return dma_buf_rw (ptr, len, sg,1 ); +return dma_buf_rw (ptr, len, sg,DMA_DIRECTION_TO_DEVICE ); } void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
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 )
@@ -134,7 +135,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
@@ -623,7 +623,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.5
PATCH 5/11 ide/ahci: Use universal DMA helper functions
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
The 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 | 34 ++++++++++++++++++++-------------- hw/ide/ahci.h | 3 ++- hw/ide/ich.c | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-)
hw/ide/ahci.c +20 -14
@@ -588,7 +588,7 @@ static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis)AHCIPortRegs *pr = &ad->port_regs;uint8_t *d2h_fis;int i; -target_phys_addr_t cmd_len =0x80 ; +dma_addr_t cmd_len =0x80 ;int cmd_mapped =0 ;if (!ad->res_fis || !(pr->cmd &PORT_CMD_FIS_RX )) {
@@ -598,7 +598,8 @@ static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis)if (!cmd_fis) {/* map cmd_fis */ uint64_t tbl_addr =le64_to_cpu (ad->cur_cmd->tbl_addr); - cmd_fis =cpu_physical_memory_map (tbl_addr, &cmd_len,0 ); + cmd_fis =dma_memory_map (ad->hba->dma, tbl_addr, &cmd_len, +DMA_DIRECTION_TO_DEVICE ); cmd_mapped =1 ; }
@@ -630,7 +631,8 @@ static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis)ahci_trigger_irq (ad->hba, ad,PORT_IRQ_D2H_REG_FIS );if (cmd_mapped) { -cpu_physical_memory_unmap (cmd_fis, cmd_len,0 , cmd_len); +dma_memory_unmap (ad->hba->dma, cmd_fis, cmd_len, +DMA_DIRECTION_TO_DEVICE , cmd_len); } }
@@ -640,8 +642,8 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist)uint32_t opts =le32_to_cpu (cmd->opts);uint64_t prdt_addr =le64_to_cpu (cmd->tbl_addr) +0x80 ;int sglist_alloc_hint = opts >>AHCI_CMD_HDR_PRDT_LEN ; -target_phys_addr_t prdt_len = (sglist_alloc_hint *sizeof (AHCI_SG )); -target_phys_addr_t real_prdt_len = prdt_len; +dma_addr_t prdt_len = (sglist_alloc_hint *sizeof (AHCI_SG )); +dma_addr_t real_prdt_len = prdt_len;uint8_t *prdt;int i;int r =0 ;
@@ -652,7 +654,8 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist) }/* map PRDT */ -if (!(prdt =cpu_physical_memory_map (prdt_addr, &prdt_len,0 ))){ +if (!(prdt =dma_memory_map (ad->hba->dma, prdt_addr, &prdt_len, +DMA_DIRECTION_TO_DEVICE ))){DPRINTF (ad->port_no,"map failed\n" );return -1 ; }
@@ -667,8 +670,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist)if (sglist_alloc_hint >0 ) {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),
@@ -677,7 +679,8 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist) } out: -cpu_physical_memory_unmap (prdt, prdt_len,0 , prdt_len); +dma_memory_unmap (ad->hba->dma, prdt, prdt_len, +DMA_DIRECTION_TO_DEVICE , prdt_len);return r; }
@@ -787,7 +790,7 @@ static int handle_cmd(AHCIState *s, int port, int slot)uint64_t tbl_addr;AHCICmdHdr *cmd;uint8_t *cmd_fis; -target_phys_addr_t cmd_len; +dma_addr_t cmd_len;if (s->dev[port].port.ifs[0 ].status & (BUSY_STAT |DRQ_STAT )) {/* Engine currently busy, try again later */
@@ -809,7 +812,8 @@ static int handle_cmd(AHCIState *s, int port, int slot) tbl_addr =le64_to_cpu (cmd->tbl_addr); cmd_len =0x80 ; - cmd_fis =cpu_physical_memory_map (tbl_addr, &cmd_len,1 ); + cmd_fis =dma_memory_map (s->dma, tbl_addr, &cmd_len, +DMA_DIRECTION_FROM_DEVICE );if (!cmd_fis) {DPRINTF (port,"error: guest passed us an invalid cmd fis\n" );
@@ -935,7 +939,8 @@ static int handle_cmd(AHCIState *s, int port, int slot) } out: -cpu_physical_memory_unmap (cmd_fis, cmd_len,1 , cmd_len); +dma_memory_unmap (s->dma, cmd_fis, cmd_len,DMA_DIRECTION_FROM_DEVICE , + cmd_len);if (s->dev[port].port.ifs[0 ].status & (BUSY_STAT |DRQ_STAT )) {/* async command, complete later */
@@ -1115,11 +1120,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);
@@ -1188,7 +1194,7 @@ static void sysbus_ahci_reset(DeviceState *dev)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 (AHCIState *s);
hw/ide/ich.c +1 -1
@@ -98,7 +98,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.5
PATCH 6/11 usb: Convert usb_packet_{map, unmap} to universal DMA helpers
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
The 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.h | 2 +- hw/usb/hcd-ehci.c | 4 ++-- hw/usb/hcd-uhci.c | 2 +- hw/usb/libhw.c | 21 +++++++++++---------- 4 files changed, 15 insertions(+), 14 deletions(-)
hw/usb.h +1 -1
@@ -345,7 +345,7 @@ void usb_packet_check_state(USBPacket *p, USBPacketState expected);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);
hw/usb/hcd-ehci.c +2 -2
@@ -1422,8 +1422,8 @@ static void ehci_execute_complete(EHCIQueue *q)set_field (&q->qh.token, p->tbytes,QTD_TOKEN_TBYTES ); }ehci_finish_transfer (q, p->usb_status); +usb_packet_unmap (&p->packet, &p->sgl);qemu_sglist_destroy (&p->sgl); -usb_packet_unmap (&p->packet); q->qh.token ^=QTD_TOKEN_DTOGGLE ; q->qh.token &= ~QTD_TOKEN_ACTIVE ;
@@ -1547,7 +1547,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/hcd-uhci.c +1 -1
@@ -871,7 +871,7 @@ static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, 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/libhw.c +11 -10
@@ -26,15 +26,15 @@int usb_packet_map (USBPacket *p,QEMUSGList *sgl) { -int is_write = (p->pid ==USB_TOKEN_IN ); -target_phys_addr_t len; +DMADirection dir = (p->pid ==USB_TOKEN_IN ) ? +DMA_DIRECTION_FROM_DEVICE :DMA_DIRECTION_TO_DEVICE ; +dma_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, sgl->sg[i].base, &len, dir);if (!mem) { goto err; }
@@ -46,18 +46,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); } }
Signature · 1 line
1.7.9.5
PATCH 7/11 iommu: Introduce IOMMU emulation infrastructure
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
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.
--- dma-helpers.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- dma.h | 108 +++++++++++++++++++++++++++++------- hw/qdev-dma.h | 4 +- 3 files changed, 259 insertions(+), 25 deletions(-)
dma-helpers.c +171 -1
@@ -9,8 +9,12 @@#include "dma.h" #include "trace.h" +#include "range.h" +#include "qemu-thread.h" -int dma_memory_set (DMAContext *dma,dma_addr_t addr,uint8_t c,dma_addr_t len) +/* #define DEBUG_IOMMU */ + +static void do_dma_memory_set (dma_addr_t addr,uint8_t c,dma_addr_t len) {#define FILLBUF_SIZE 512uint8_t fillbuf[FILLBUF_SIZE ];
@@ -23,6 +27,15 @@ int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len) len -= len; addr += len; } +} + +int dma_memory_set (DMAContext *dma,dma_addr_t addr,uint8_t c,dma_addr_t len) +{ +if (dma_has_iommu (dma)) { +return iommu_dma_memory_set (dma, addr, c, len); + } +do_dma_memory_set (addr, c, len); +return 0 ; }
@@ -260,3 +273,160 @@ void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, {bdrv_acct_start (bs, cookie, sg->size, type); } + +bool iommu_dma_memory_valid (DMAContext *dma,dma_addr_t addr,dma_addr_t len, +DMADirection dir) +{ +target_phys_addr_t paddr, plen; + +#ifdef DEBUG_IOMMU +fprintf (stderr,"dma_memory_check context=%p addr=0x" DMA_ADDR_FMT +" len=0x" DMA_ADDR_FMT " dir=%d\n" , dma, addr, len, dir); +#endif + +while (len) { +if (dma->translate (dma, addr, &paddr, &plen, dir) !=0 ) { +return false; + } + +/* The translation might be valid for larger regions. */ +if (plen > len) { + plen = len; + } + + len -= plen; + addr += plen; + } + +return true; +} + +int iommu_dma_memory_rw (DMAContext *dma,dma_addr_t addr, +void *buf,dma_addr_t len,DMADirection dir) +{ +target_phys_addr_t paddr, plen; +int err; + +#ifdef DEBUG_IOMMU +fprintf (stderr,"dma_memory_rw context=%p addr=0x" DMA_ADDR_FMT " len=0x" +DMA_ADDR_FMT " dir=%d\n" , dma, addr, len, dir); +#endif + +while (len) { + err = dma->translate (dma, addr, &paddr, &plen, dir); +if (err) { +/* +* In case of failure on reads from the guest, we clean the +* destination buffer so that a device that doesn't test +* for errors will not expose qemu internal memory. +*/ +memset (buf,0 , len); +return -1 ; + } + +/* The translation might be valid for larger regions. */ +if (plen > len) { + plen = len; + } + +cpu_physical_memory_rw (paddr, buf, plen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + + len -= plen; + addr += plen; + buf += plen; + } + +return 0 ; +} + +int iommu_dma_memory_set (DMAContext *dma,dma_addr_t addr,uint8_t c, +dma_addr_t len) +{ +target_phys_addr_t paddr, plen; +int err; + +#ifdef DEBUG_IOMMU +fprintf (stderr,"dma_memory_set context=%p addr=0x" DMA_ADDR_FMT +" len=0x" DMA_ADDR_FMT "\n" , dma, addr, len); +#endif + +while (len) { + err = dma->translate (dma, addr, &paddr, &plen, +DMA_DIRECTION_FROM_DEVICE ); +if (err) { +return err; + } + +/* The translation might be valid for larger regions. */ +if (plen > len) { + plen = len; + } + +do_dma_memory_set (paddr, c, plen); + + len -= plen; + addr += plen; + } + +return 0 ; +} + +void dma_context_init (DMAContext *dma,DMATranslateFunc translate, +DMAMapFunc map,DMAUnmapFunc unmap) +{ +#ifdef DEBUG_IOMMU +fprintf (stderr,"dma_context_init(%p, %p, %p, %p)\n" , + dma, translate, map, unmap); +#endif + dma->translate = translate; + dma->map = map; + dma->unmap = unmap; +} + +void *iommu_dma_memory_map (DMAContext *dma,dma_addr_t addr,dma_addr_t *len, +DMADirection dir) +{ +int err; +target_phys_addr_t paddr, plen; +void *buf; + +if (dma->map) { +return dma->map (dma, addr, len, dir); + } + + plen = *len; + err = dma->translate (dma, addr, &paddr, &plen, dir); +if (err) { +return NULL ; + } + +/* +* If this is true, the virtual region is contiguous, +* but the translated physical region isn't. We just +* clamp *len, much like cpu_physical_memory_map() does. +*/ +if (plen < *len) { + *len = plen; + } + + buf =cpu_physical_memory_map (paddr, &plen, + dir ==DMA_DIRECTION_FROM_DEVICE ); + *len = plen; + +return buf; +} + +void iommu_dma_memory_unmap (DMAContext *dma,void *buffer,dma_addr_t len, +DMADirection dir,dma_addr_t access_len) +{ +if (dma->unmap) { + dma->unmap (dma, buffer, len, dir, access_len); +return ; + } + +cpu_physical_memory_unmap (buffer, len, + dir ==DMA_DIRECTION_FROM_DEVICE , + access_len); + +}
dma.h +87 -21
149 lines not on this page · raw message
hw/qdev-dma.h +1 -3
@@ -6,7 +6,5 @@* This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory. */ -#include "qdev-addr.h" -#define DEFINE_PROP_DMAADDR (_n, _s, _f, _d) \ - DEFINE_PROP_TADDR(_n, _s, _f, _d) + DEFINE_PROP_HEX64(_n, _s, _f, _d)
Signature · 1 line
1.7.9.5
PATCH 8/11 pseries: Convert sPAPR TCEs to use generic IOMMU infrastructure
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
The 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.
We don't yet handle synchronization of map/unmap callbacks vs. invalidations, this will require some complex interaction with the kernel and is not a major concern at this stage.
--- hw/ppc/Makefile.objs | 2 +- hw/spapr.c | 3 + hw/spapr.h | 17 +++ hw/spapr_iommu.c | 242 +++++++++++++++++++++++++++++++++++++++++++ hw/spapr_llan.c | 63 +++++------ hw/spapr_vio.c | 281 ++++---------------------------------------------- hw/spapr_vio.h | 73 ++++++------- hw/spapr_vscsi.c | 26 ++--- hw/spapr_vty.c | 2 +- target-ppc/kvm.c | 4 +- 10 files changed, 370 insertions(+), 343 deletions(-) create mode 100644 hw/spapr_iommu.c
hw/ppc/Makefile.objs +1 -1
@@ -10,7 +10,7 @@ obj-y += ppc_newworld.o # IBM pSeries (sPAPR) obj-$(CONFIG_PSERIES) += spapr.o spapr_hcall.o spapr_rtas.o spapr_vio.o obj-$(CONFIG_PSERIES) += xics.o spapr_vty.o spapr_llan.o spapr_vscsi.o -obj-$(CONFIG_PSERIES) += spapr_pci.o pci-hotplug.o +obj-$(CONFIG_PSERIES) += spapr_pci.o pci-hotplug.o spapr_iommu.o # PowerPC 4xx boards obj-y += ppc4xx_devs.o ppc4xx_pci.o ppc405_uc.o ppc405_boards.o obj-y += ppc440_bamboo.o
hw/spapr.c +3 -0
@@ -628,6 +628,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 +17 -0
@@ -1,6 +1,7 @@#if !defined(__HW_SPAPR_H__)#define __HW_SPAPR_H__ +#include "dma.h" #include "hw/xics.h" struct VIOsPAPRBus ;
@@ -319,4 +320,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 +242 -0
242 lines not on this page · raw message
hw/spapr_llan.c +33 -30
166 lines not on this page · raw message
hw/spapr_vio.c +20 -261
350 lines not on this page · raw message
hw/spapr_vio.h +37 -36
109 lines not on this page · raw message
hw/spapr_vscsi.c +14 -12
@@ -165,7 +165,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" );
@@ -281,9 +281,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;
@@ -329,10 +329,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);
@@ -345,12 +346,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",
@@ -728,7 +729,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" );
@@ -742,7 +743,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" );
@@ -804,7 +805,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);
@@ -945,7 +946,7 @@ static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off) }static Property spapr_vscsi_properties[] = { -DEFINE_SPAPR_PROPERTIES (VSCSIState, vdev,0x10000000 ), +DEFINE_SPAPR_PROPERTIES (VSCSIState, vdev),DEFINE_PROP_END_OF_LIST (), };
@@ -962,6 +963,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 = {
hw/spapr_vty.c +1 -1
@@ -133,7 +133,7 @@ void spapr_vty_create(VIOsPAPRBus *bus, CharDriverState *chardev) }static Property spapr_vty_properties[] = { -DEFINE_SPAPR_PROPERTIES (VIOsPAPRVTYDevice, sdev,0 ), +DEFINE_SPAPR_PROPERTIES (VIOsPAPRVTYDevice, sdev),DEFINE_PROP_CHR ("chardev" , VIOsPAPRVTYDevice, chardev),DEFINE_PROP_END_OF_LIST (), };
target-ppc/kvm.c +2 -2
@@ -859,7 +859,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 );
@@ -882,7 +882,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.5
PATCH 9/11 iommu: Allow PCI to use IOMMU infrastructure
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
This 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 | 9 +++++++++ hw/pci.h | 9 +++++++-- hw/pci_internals.h | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-)
hw/pci.c +9 -0
@@ -775,6 +775,9 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,return NULL ; } pci_dev->bus = bus; +if (bus->dma_context_fn) { + pci_dev->dma = bus->dma_context_fn (bus, bus->dma_context_opaque, devfn); + } pci_dev->devfn = devfn;pstrcpy (pci_dev->name,sizeof (pci_dev->name), name); pci_dev->irq_state =0 ;
@@ -2021,6 +2024,12 @@ static void pci_device_class_init(ObjectClass *klass, void *data) k->props = pci_props; } +void pci_setup_iommu (PCIBus *bus,PCIDMAContextFunc fn,void *opaque) +{ + bus->dma_context_fn = fn; + bus->dma_context_opaque = opaque; +} +static TypeInfo pci_device_type_info = { .name =TYPE_PCI_DEVICE , .parent =TYPE_DEVICE ,
hw/pci.h +7 -2
@@ -179,6 +179,7 @@ typedef void (*MSIVectorReleaseNotifier)(PCIDevice *dev, unsigned int vector);struct PCIDevice {DeviceState qdev; +/* PCI config space */ uint8_t *config;
@@ -200,6 +201,7 @@ struct PCIDevice {int32_t devfn;char name[64 ];PCIIORegion io_regions[PCI_NUM_REGIONS ]; +DMAContext *dma;/* do not access the following fields */ PCIConfigReadFunc *config_read;
@@ -324,6 +326,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) {
@@ -560,8 +566,7 @@ 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 ; +return dev->dma; } static inline int pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
hw/pci_internals.h +2 -0
@@ -17,6 +17,8 @@struct PCIBus {BusState qbus; +PCIDMAContextFunc dma_context_fn; +void *dma_context_opaque;uint8_t devfn_min;pci_set_irq_fn set_irq;pci_map_irq_fn map_irq;
Signature · 1 line
1.7.9.5
PATCH 10/11 pseries: Implement IOMMU and DMA for PAPR PCI devices
In reply to Benjamin HerrenschmidtFrom: David Gibson <[email protected]>
Currently 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_iommu.c | 40 ++++++++++++++++++++++------------------ hw/spapr_pci.c | 15 +++++++++++++++ hw/spapr_pci.h | 1 + 4 files changed, 39 insertions(+), 18 deletions(-)
hw/spapr.h +1 -0
@@ -329,6 +329,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_iommu.c +22 -18
@@ -162,6 +162,22 @@ void spapr_tce_free(DMAContext *dma) } } +static target_ulong put_tce_emu (sPAPRTCETable *tcet,target_ulong ioba, +target_ulong tce) +{ +sPAPRTCE *tcep; + +if (ioba >= tcet->window_size) { +hcall_dprintf ("spapr_vio_put_tce on out-of-boards IOBA 0x" + TARGET_FMT_lx"\n" , ioba); +return H_PARAMETER ; + } + + tcep = tcet->table + (ioba >>SPAPR_TCE_PAGE_SHIFT ); + tcep->tce = tce; + +return H_SUCCESS ; +}static target_ulong h_put_tce (CPUPPCState *env,sPAPREnvironment *spapr,target_ulong opcode,target_ulong *args)
@@ -170,37 +186,25 @@ static target_ulong h_put_tce(CPUPPCState *env, sPAPREnvironment *spapr,target_ulong ioba = args[1 ];target_ulong tce = args[2 ];sPAPRTCETable *tcet =spapr_tce_find_by_liobn (liobn); -sPAPRTCE *tcep;if (liobn &0xFFFFFFFF00000000ULL ) {hcall_dprintf ("spapr_vio_put_tce on out-of-boundsw LIOBN " TARGET_FMT_lx"\n" , liobn);return H_PARAMETER ; } -if (!tcet) { -hcall_dprintf ("spapr_vio_put_tce on non-existent LIOBN " - TARGET_FMT_lx"\n" , liobn); -return H_PARAMETER ; - } ioba &= ~(SPAPR_TCE_PAGE_SIZE -1 ); +if (tcet) { +return put_tce_emu (tcet, ioba, tce); + }#ifdef DEBUG_TCE -fprintf (stderr,"spapr_vio_put_tce on liobn=" TARGET_FMT_lx/*%s*/ +fprintf (stderr,"%s on liobn=" TARGET_FMT_lx/*%s*/ " ioba 0x" TARGET_FMT_lx" TCE 0x" TARGET_FMT_lx"\n" , - liobn,/*dev->qdev.id, */ ioba, tce); + __func__, liobn,/*dev->qdev.id, */ ioba, tce);#endif -if (ioba >= tcet->window_size) { -hcall_dprintf ("spapr_vio_put_tce on out-of-boards IOBA 0x" - TARGET_FMT_lx"\n" , ioba); -return H_PARAMETER ; - } - - tcep = tcet->table + (ioba >>SPAPR_TCE_PAGE_SHIFT ); - tcep->tce = tce; - -return H_SUCCESS ; +return H_PARAMETER ; }void spapr_iommu_init (void )
hw/spapr_pci.c +15 -0
@@ -266,12 +266,21 @@ static const 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);char *namebuf;int i;PCIBus *bus; +uint32_t liobn; phb->dtbusname =g_strdup_printf ("pci@%" PRIx64, phb->buid); namebuf =alloca (strlen (phb->dtbusname) +32 );
@@ -312,6 +321,10 @@ static int spapr_phb_init(SysBusDevice *s) PCI_DEVFN(0, 0), PCI_NUM_PINS); phb->host_state.bus = bus; + 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 */
@@ -472,6 +485,8 @@ int spapr_populate_pci_devices(sPAPRPHBState *phb,_FDT (fdt_setprop (fdt, bus_off,"interrupt-map" , &interrupt_map,sizeof (interrupt_map))); +spapr_dma_dt (fdt, bus_off,"ibm,dma-window" , phb->dma); +return 0 ; }
hw/spapr_pci.h +1 -0
@@ -38,6 +38,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.5
PATCH 11/11 Add a memory barrier to DMA functions
In reply to Benjamin HerrenschmidtThe emulated devices can run simultaneously with the guest, so we need to be careful with ordering of load and stores done by them to the guest system memory, which need to be observed in the right order by the guest operating system.
This adds a barrier call to the basic DMA read/write ops which is currently implemented as a smp_mb(), but could be later improved for more fine grained control of barriers.
Additionally, a _relaxed() variant of the accessors is provided to easily convert devices who would be performance sensitive and negatively impacted by the change.
--- dma-helpers.c | 2 ++ dma.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-)
dma-helpers.c +2 -0
@@ -31,6 +31,8 @@ static void do_dma_memory_set(dma_addr_t addr, uint8_t c, dma_addr_t len)int dma_memory_set (DMAContext *dma,dma_addr_t addr,uint8_t c,dma_addr_t len) { +dma_barrier (dma,DMA_DIRECTION_FROM_DEVICE ); +if (dma_has_iommu (dma)) {return iommu_dma_memory_set (dma, addr, c, len); }
dma.h +50 -2
76 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH 11/11] Add a memory barrier to DMA functions
Quoted 60 lines · Benjamin Herrenschmidt
The emulated devices can run simultaneously with the guest, so we need to be careful with ordering of load and stores done by them to the guest system memory, which need to be observed in the right order by the guest operating system.
This adds a barrier call to the basic DMA read/write ops which is currently implemented as a
smp_mb(), but could be later improved for more fine grained control of barriers.Additionally, a
_relaxed()variant of the accessors is provided to easily convert devices who would be performance sensitive and negatively impacted by the change.Signed-off-by Benjamin Herrenschmidt <[email protected]>---
dma-helpers.c | 2 ++
dma.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++--2 files changed, 52 insertions(+), 2 deletions(-)dma-helpers.c +2
-0@@ -31,6 +31,8 @@ static void do_dma_memory_set(dma_addr_t addr, uint8_t c, dma_addr_t len) int dma_memory_set(DMAContext *dma, dma_addr_t addr, uint8_t c, dma_addr_t len) { + dma_barrier(dma, DMA_DIRECTION_FROM_DEVICE); + if (dma_has_iommu(dma)) { return iommu_dma_memory_set(dma, addr, c, len); }dma.h +13
-0@@ -13,6 +13,7 @@ #include <stdio.h> #include "hw/hw.h" #include "block.h" +#include "kvm.h" typedef struct DMAContext DMAContext; typedef struct ScatterGatherEntry ScatterGatherEntry;@@ -65,6 +66,30 @@ struct DMAContext { DMAUnmapFunc *unmap; }; +static inline void dma_barrier (DMAContext *dma,DMADirection dir) +{ + /* +* This is called before DMA read and write operations +* unless the _relaxed form is used and is responsible +* for providing some sane ordering of accesses vs +* concurrently running VCPUs. +* +* Users of map(), unmap() or lower level st/ld_* +* operations are responsible for providing their own +* ordering via barriers. +*
dma.h struct DMAContext { · line 90
Quoted 10 lines
dma.h +10
-0+* This primitive implementation does a simple smp_mb() +* before each operation which provides pretty much full +* ordering. +* +* A smarter implementation can be devised if needed to +* use lighter barriers based on the direction of the +* transfer, the DMA context, etc... +*/ + if (kvm_enabled()) + smp_mb();
Missing braces. Please use checkpatch.pl to avoid issues like this.
Quoted 50 lines
dma.h +27
-3+} + static inline bool dma_has_iommu(DMAContext *dma) { return !!dma;@@ -88,8 +113,9 @@ static inline bool dma_memory_valid(DMAContext *dma, int iommu_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) +static inline int dma_memory_rw_relaxed (DMAContext *dma,dma_addr_t addr, + void *buf, dma_addr_t len, + DMADirection dir) { if (!dma_has_iommu(dma)) { /* Fast-path for no IOMMU */@@ -101,6 +127,28 @@ static inline int dma_memory_rw(DMAContext *dma, dma_addr_t addr, } } +static inline int dma_memory_read_relaxed (DMAContext *dma,dma_addr_t addr, + void *buf, dma_addr_t len) +{ + return dma_memory_rw_relaxed(dma, addr, buf, len, DMA_DIRECTION_TO_DEVICE); +} + +static inline int dma_memory_write_relaxed (DMAContext *dma,dma_addr_t addr, + const void *buf, dma_addr_t len) +{ + return dma_memory_rw_relaxed(dma, addr, (void *)buf, len, + DMA_DIRECTION_FROM_DEVICE); +} + +static inline int dma_memory_rw (DMAContext *dma,dma_addr_t addr, + void *buf, dma_addr_t len, + DMADirection dir) +{ + dma_barrier(dma, dir); + + return dma_memory_rw_relaxed(dma, addr, buf, len, dir); +} + static inline int dma_memory_read(DMAContext *dma, dma_addr_t addr, void *buf, dma_addr_t len) { --1.7.9.5
Re: [Qemu-devel] [PATCH 11/11] Add a memory barrier to DMA functions
Quoted 8 lines · Blue Swirl
dma.h struct DMAContext { · line 90
Quoted 6 lines
dma.h +6
-0+* A smarter implementation can be devised if needed to +* use lighter barriers based on the direction of the +* transfer, the DMA context, etc... +*/ +if (kvm_enabled ()) +smp_mb ();Missing braces. Please use checkpatch.pl to avoid issues like this.
Ooops, sorry, forgot to make my code butt-ugly ...
I generally run checkpatch lately, for some reason this one went through the cracks.
Anthony, do you need a repost or can you fix it up ?
Cheers,
Ben.