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

drm: gem_cma_helper.c: Allow importing of contiguous scatterlists with nents > 1

drm_gem_cma_prime_import_sg_table() will fail if the number of entries
in the sg_table > 1. However, you can have a device that uses an IOMMU
engine and can map a discontiguous buffer with multiple entries that
have consecutive sg_dma_addresses, effectively making it contiguous.
Allow for that scenario by testing the entries in the sg_table for
contiguous coverage.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20171110133310.1225-1-Liviu.Dudau@arm.com

authored by

Liviu Dudau and committed by
Noralf Trønnes
998fb1a0 1f2d9bdc

+23 -3
+20 -2
drivers/gpu/drm/drm_gem_cma_helper.c
··· 475 475 { 476 476 struct drm_gem_cma_object *cma_obj; 477 477 478 - if (sgt->nents != 1) 479 - return ERR_PTR(-EINVAL); 478 + if (sgt->nents != 1) { 479 + /* check if the entries in the sg_table are contiguous */ 480 + dma_addr_t next_addr = sg_dma_address(sgt->sgl); 481 + struct scatterlist *s; 482 + unsigned int i; 483 + 484 + for_each_sg(sgt->sgl, s, sgt->nents, i) { 485 + /* 486 + * sg_dma_address(s) is only valid for entries 487 + * that have sg_dma_len(s) != 0 488 + */ 489 + if (!sg_dma_len(s)) 490 + continue; 491 + 492 + if (sg_dma_address(s) != next_addr) 493 + return ERR_PTR(-EINVAL); 494 + 495 + next_addr = sg_dma_address(s) + sg_dma_len(s); 496 + } 497 + } 480 498 481 499 /* Create a CMA GEM buffer. */ 482 500 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size);
+3 -1
include/drm/drm_gem_cma_helper.h
··· 8 8 * struct drm_gem_cma_object - GEM object backed by CMA memory allocations 9 9 * @base: base GEM object 10 10 * @paddr: physical address of the backing memory 11 - * @sgt: scatter/gather table for imported PRIME buffers 11 + * @sgt: scatter/gather table for imported PRIME buffers. The table can have 12 + * more than one entry but they are guaranteed to have contiguous 13 + * DMA addresses. 12 14 * @vaddr: kernel virtual address of the backing memory 13 15 */ 14 16 struct drm_gem_cma_object {