Re: [PATCHv4 8/8] videobuf2: handle non-contiguous DMA allocations

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 03/08/2021 10:33, Hans Verkuil wrote:
> On 27/07/2021 09:05, Sergey Senozhatsky wrote:
>> This adds support for new noncontiguous DMA API, which
> 
> for -> for the
> 
>> requires allocators to have two execution branches: one
>> for the current API, and one for the new one.
>>
>> Signed-off-by: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
>> Acked-by: Christoph Hellwig <hch@xxxxxx>
>> ---
>>  .../common/videobuf2/videobuf2-dma-contig.c   | 142 +++++++++++++++---
>>  1 file changed, 117 insertions(+), 25 deletions(-)
>>
>> diff --git a/drivers/media/common/videobuf2/videobuf2-dma-contig.c b/drivers/media/common/videobuf2/videobuf2-dma-contig.c
>> index 1e218bc440c6..10f73e27d694 100644
>> --- a/drivers/media/common/videobuf2/videobuf2-dma-contig.c
>> +++ b/drivers/media/common/videobuf2/videobuf2-dma-contig.c
>> @@ -17,6 +17,7 @@
>>  #include <linux/sched.h>
>>  #include <linux/slab.h>
>>  #include <linux/dma-mapping.h>
>> +#include <linux/highmem.h>
>>  
>>  #include <media/videobuf2-v4l2.h>
>>  #include <media/videobuf2-dma-contig.h>
>> @@ -42,6 +43,7 @@ struct vb2_dc_buf {
>>  	struct dma_buf_attachment	*db_attach;
>>  
>>  	struct vb2_buffer		*vb;
>> +	bool				coherent_mem;
> 
> I think that this as well should be renamed to non_coherent_mem.
> 
>>  };
>>  
>>  /*********************************************/
>> @@ -78,14 +80,22 @@ static void *vb2_dc_cookie(struct vb2_buffer *vb, void *buf_priv)
>>  static void *vb2_dc_vaddr(struct vb2_buffer *vb, void *buf_priv)
>>  {
>>  	struct vb2_dc_buf *buf = buf_priv;
>> -	struct dma_buf_map map;
>> -	int ret;
>>  
>> -	if (!buf->vaddr && buf->db_attach) {
>> -		ret = dma_buf_vmap(buf->db_attach->dmabuf, &map);
>> -		buf->vaddr = ret ? NULL : map.vaddr;
>> +	if (buf->vaddr)
>> +		return buf->vaddr;
>> +
>> +	if (buf->db_attach) {
>> +		struct dma_buf_map map;
>> +
>> +		if (!dma_buf_vmap(buf->db_attach->dmabuf, &map))
>> +			buf->vaddr = map.vaddr;
>> +
>> +		return buf->vaddr;
>>  	}
>>  
>> +	if (!buf->coherent_mem)
>> +		buf->vaddr = dma_vmap_noncontiguous(buf->dev, buf->size,
>> +						    buf->dma_sgt);
>>  	return buf->vaddr;
>>  }
>>  
>> @@ -101,13 +111,26 @@ static void vb2_dc_prepare(void *buf_priv)
>>  	struct vb2_dc_buf *buf = buf_priv;
>>  	struct sg_table *sgt = buf->dma_sgt;
>>  
>> +	/* This takes care of DMABUF and user-enforced cache sync hint */
>>  	if (buf->vb->skip_cache_sync_on_prepare)
>>  		return;
>>  
>> +	/*
>> +	 * Coherent MMAP buffers do not need to be synced, unlike USERPTR
>> +	 * and non-coherent MMAP buffers.
>> +	 */
>> +	if (buf->vb->memory == V4L2_MEMORY_MMAP && buf->coherent_mem)
>> +		return;
>> +
>>  	if (!sgt)
>>  		return;
>>  
>> +	/* For both USERPTR and non-coherent MMAP */
>>  	dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir);
>> +
>> +	/* Non-coherent MMAP only */
>> +	if (!buf->coherent_mem && buf->vaddr)
>> +		flush_kernel_vmap_range(buf->vaddr, buf->size);
>>  }
>>  
>>  static void vb2_dc_finish(void *buf_priv)
>> @@ -115,13 +138,26 @@ static void vb2_dc_finish(void *buf_priv)
>>  	struct vb2_dc_buf *buf = buf_priv;
>>  	struct sg_table *sgt = buf->dma_sgt;
>>  
>> +	/* This takes care of DMABUF and user-enforced cache sync hint */
>>  	if (buf->vb->skip_cache_sync_on_finish)
>>  		return;
>>  
>> +	/*
>> +	 * Coherent MMAP buffers do not need to be synced, unlike USERPTR
>> +	 * and non-coherent MMAP buffers.
>> +	 */
>> +	if (buf->vb->memory == V4L2_MEMORY_MMAP && buf->coherent_mem)
>> +		return;
>> +
>>  	if (!sgt)
>>  		return;
>>  
>> +	/* For both USERPTR and non-coherent MMAP */
>>  	dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir);
>> +
>> +	/* Non-coherent MMAP only */
>> +	if (!buf->coherent_mem && buf->vaddr)
>> +		invalidate_kernel_vmap_range(buf->vaddr, buf->size);
>>  }
>>  
>>  /*********************************************/
>> @@ -139,17 +175,66 @@ static void vb2_dc_put(void *buf_priv)
>>  		sg_free_table(buf->sgt_base);
>>  		kfree(buf->sgt_base);
>>  	}
>> -	dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr,
>> -		       buf->attrs);
>> +
>> +	if (buf->coherent_mem) {
>> +		dma_free_attrs(buf->dev, buf->size, buf->cookie,
>> +			       buf->dma_addr, buf->attrs);
>> +	} else {
>> +		if (buf->vaddr)
>> +			dma_vunmap_noncontiguous(buf->dev, buf->vaddr);
>> +		dma_free_noncontiguous(buf->dev, buf->size,
>> +				       buf->dma_sgt, buf->dma_dir);
>> +	}
>>  	put_device(buf->dev);
>>  	kfree(buf);
>>  }
>>  
>> +static int vb2_dc_alloc_coherent(struct vb2_dc_buf *buf)
>> +{
>> +	struct vb2_queue *q = buf->vb->vb2_queue;
>> +
>> +	buf->cookie = dma_alloc_attrs(buf->dev,
>> +				      buf->size,
>> +				      &buf->dma_addr,
>> +				      GFP_KERNEL | q->gfp_flags,
>> +				      buf->attrs);
>> +	if (!buf->cookie)
>> +		return -ENOMEM;
>> +
>> +	if (q->dma_attrs & DMA_ATTR_NO_KERNEL_MAPPING)
>> +		return 0;
>> +
>> +	buf->vaddr = buf->cookie;
>> +	return 0;
>> +}
>> +
>> +static int vb2_dc_alloc_non_coherent(struct vb2_dc_buf *buf)
>> +{
>> +	struct vb2_queue *q = buf->vb->vb2_queue;
>> +
>> +	buf->dma_sgt = dma_alloc_noncontiguous(buf->dev,
>> +					       buf->size,
>> +					       buf->dma_dir,
>> +					       GFP_KERNEL | q->gfp_flags,
>> +					       buf->attrs);
>> +	if (!buf->dma_sgt)
>> +		return -ENOMEM;
>> +
>> +	buf->dma_addr = sg_dma_address(buf->dma_sgt->sgl);
>> +
>> +	/*
>> +	 * For requests that need kernel mapping (DMA_ATTR_NO_KERNEL_MAPPING
>> +	 * bit is cleared) we perform dma_vmap_noncontiguous() in vb2_dc_vadd().

Typo: vb2_dc_vadd -> vb2_dc_vaddr

Regards,

	Hans



[Index of Archives]     [Linux Input]     [Video for Linux]     [Gstreamer Embedded]     [Mplayer Users]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux