<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body smarttemplateinserted="true" text="#000000" bgcolor="#FFFFFF">
    <div id="smartTemplate4-template">
      <p>Many thanks!</p>
      <p>mmap succeeds - I removed the checks only to post the code on
        this list. I use this now:<br>
      </p>
      <p>    if ( !ioctl(container, VFIO_IOMMU_MAP_DMA, &dma_map) )<br>
            {    if ( !ioctl(container, VFIO_IOMMU_MAP_DMA,
        &dma_map) )<br>
                 {   perror("mmap ioctl"); return 1; }<br>
            }</p>
      <p>It mostly succeeds only on second try but I never needed more
        than two tries. So it's ok.<br>
      </p>
    </div>
    Re. IOMMU type 1 recognition: You are right: It is independent of
    kernel version. It seems to be related to auto-loading vfio_pci via
    /etc/modules.conf. Installing the module later gets it working.<br>
    <div id="smartTemplate4-quoteHeader"><br>
      <br>
      <hr style="color:#62B3FF"><br>
      <div style="background-color: #DDDDDD; font-size:10pt"><b>From:</b>
        <a title="alex.williamson@redhat.com"
          href="mailto:alex.williamson@redhat.com">Alex Williamson</a><br>
        <b>Sent:</b> Monday, March 12, 2018 6:57AM<br>
        <b>To:</b> <a title="Oliver.Heid@h-next.de"
          href="mailto:Oliver.Heid@h-next.de">Heid, Oliver</a><br>
        <b>Cc:</b> <a title="vfio-users@redhat.com"
          href="mailto:vfio-users@redhat.com">Vfio-users</a><br>
        <b>Subject:</b> Re: [vfio-users] VFIO_IOMMU_MAP_DMA succeeds
        only on second try?</div>
      <br>
      <br>
    </div>
    <span type="cite" cite="mid:20180311235737.07dc4f72@t450s.home"
      style="display: block; word-break: break-all; margin: 8px 0 0 0;
      padding: 0; line-height:0"></span>
    <pre wrap="">On Mon, 5 Mar 2018 12:37:24 +0100
Oliver Heid <a class="moz-txt-link-rfc2396E" href="mailto:Oliver.Heid@h-next.de"><Oliver.Heid@h-next.de></a> wrote:

</pre>
    <blockquote type="cite">
      <pre wrap="">Do I miss something here? I want to allow RW access of a peripheral 
device to a memory region via

     struct vfio_iommu_type1_dma_map dma_map = { .argsz = sizeof(dma_map) };

     __u32* mem = (__u32*)mmap(NULL,size,PROT_READ|PROT_WRITE, 
MAP_SHARED|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);

     dma_map.argsz = sizeof(dma_map);
     dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
     dma_map.vaddr = (__u64)mem;
     dma_map.iova  = 0;
     dma_map.size  = size;

     ioctl(container, VFIO_IOMMU_MAP_DMA, &dma_map);

Any idea why theVFIO_IOMMU_MAP_DMA ioctl fails with EINVAL, but then a 
second, identical call succeeds? Does it actually succeed then? QEMU 
re-tries if the first attempt fails with EBUSY, but not with EINVAL.
</pre>
    </blockquote>
    <pre wrap="">
I don't think the QEMU -EBUSY behavior is related, that handles
previous mappings and may no longer even be necessary.  One of my unit
tests[1] does a similar map with no issues.  Your code excerpt doesn't
check whether mmap succeeds, so for all we know mmap is failing too.

</pre>
    <blockquote type="cite">
      <pre wrap="">The latest 4.15.7 kernel (4.15.6 too IIRC) does not recognize the AMD 
Ryzen 3 1200 as  VFIO_TYPE1_IOMMU so I am using version 4.15.4.
</pre>
    </blockquote>
    <pre wrap="">
This doesn't make sense, anything implementing the kernel's IOMMU API in
the kernel is compatible with type1, that includes anything supporting
AMD-Vi.  There is no whitelist or specific processor support
selection.  There are no iommu or vfio changes between the kernels
you're quoting, perhaps you could do a bisect.  Thanks,

Alex

[1] <a class="moz-txt-link-freetext" href="https://github.com/awilliam/tests/blob/master/vfio-iommu-map-unmap.c">https://github.com/awilliam/tests/blob/master/vfio-iommu-map-unmap.c</a>
</pre>
    <br>
  </body>
</html>