Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/cma: Use dma_mmap_writecombine() to mmap buffer

The GEM CMA helpers uses a custom mmap implementation based on
remap_pfn_range(). While this works when the buffer DMA and physical
addresses are identical, it fails to take IOMMU into account and tries
to mmap the buffer to userspace using the DMA virtual address instead of
the physical address. This results in mapping random physical pages when
the device is behind an IOMMU.

Use the DMA mapping dma_mmap_writecombine() function instead.

Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
Tested-by: Philipp Zabel <philipp.zabel@gmail.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>

authored by

Laurent Pinchart and committed by
Dave Airlie
b65e64f7 145bccd2

+11 -2
+11 -2
drivers/gpu/drm/drm_gem_cma_helper.c
··· 234 234 { 235 235 int ret; 236 236 237 - ret = remap_pfn_range(vma, vma->vm_start, cma_obj->paddr >> PAGE_SHIFT, 238 - vma->vm_end - vma->vm_start, vma->vm_page_prot); 237 + /* 238 + * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the 239 + * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map 240 + * the whole buffer. 241 + */ 242 + vma->vm_flags &= ~VM_PFNMAP; 243 + vma->vm_pgoff = 0; 244 + 245 + ret = dma_mmap_writecombine(cma_obj->base.dev->dev, vma, 246 + cma_obj->vaddr, cma_obj->paddr, 247 + vma->vm_end - vma->vm_start); 239 248 if (ret) 240 249 drm_gem_vm_close(vma); 241 250