diff mbox series

media: vimc: Add support for contiguous DMA buffers

Message ID 20210730001939.30769-1-laurent.pinchart+renesas@ideasonboard.com (mailing list archive)
State Superseded
Delegated to: Kieran Bingham
Headers show
Series media: vimc: Add support for contiguous DMA buffers | expand

Commit Message

Laurent Pinchart July 30, 2021, 12:19 a.m. UTC
The vimc driver is used for testing purpose, and some test use cases
involve sharing buffers with a consumer device. Consumers often require
DMA contiguous memory, which vimc doesn't currently support. This leads
in the best case to usage of bounce buffers, which is very slow, and in
the worst case in a complete failure.

Add support for the dma-contig allocator in vimc to support those use
cases properly. The allocator is selected through a new "allocator"
module parameter, which defaults to vmalloc.

Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
---
 drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
 drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
 drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
 3 files changed, 19 insertions(+), 2 deletions(-)

Comments

Dafna Hirschfeld July 30, 2021, 12:35 p.m. UTC | #1
Hi

On 30.07.21 02:19, Laurent Pinchart wrote:
> The vimc driver is used for testing purpose, and some test use cases
> involve sharing buffers with a consumer device. Consumers often require
> DMA contiguous memory, which vimc doesn't currently support. This leads
> in the best case to usage of bounce buffers, which is very slow, and in
> the worst case in a complete failure.
> 
> Add support for the dma-contig allocator in vimc to support those use
> cases properly. The allocator is selected through a new "allocator"
> module parameter, which defaults to vmalloc.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> ---
>   drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
>   drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
>   drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
>   3 files changed, 19 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
> index 5e9fd902cd37..92b69a6529fb 100644
> --- a/drivers/media/test-drivers/vimc/vimc-capture.c
> +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
> @@ -7,6 +7,7 @@
>   
>   #include <media/v4l2-ioctl.h>
>   #include <media/videobuf2-core.h>
> +#include <media/videobuf2-dma-contig.h>
>   #include <media/videobuf2-vmalloc.h>
>   
>   #include "vimc-common.h"
> @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
>   	/* Initialize the vb2 queue */
>   	q = &vcap->queue;
>   	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> -	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
> +	q->io_modes = VB2_MMAP | VB2_DMABUF;

maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?

> +	if (vimc_allocator != 1)

maybe define a macro instead of `1` ?

> +		q->io_modes |= VB2_USERPTR;
>   	q->drv_priv = vcap;
>   	q->buf_struct_size = sizeof(struct vimc_cap_buffer);
>   	q->ops = &vimc_cap_qops;
> -	q->mem_ops = &vb2_vmalloc_memops;
> +	q->mem_ops = vimc_allocator == 1
> +		   ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
>   	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
>   	q->min_buffers_needed = 2;
>   	q->lock = &vcap->lock;
> +	q->dev = v4l2_dev->dev;
>   
>   	ret = vb2_queue_init(q);
>   	if (ret) {
> diff --git a/drivers/media/test-drivers/vimc/vimc-common.h b/drivers/media/test-drivers/vimc/vimc-common.h
> index a289434e75ba..b77939123501 100644
> --- a/drivers/media/test-drivers/vimc/vimc-common.h
> +++ b/drivers/media/test-drivers/vimc/vimc-common.h
> @@ -35,6 +35,8 @@
>   
>   #define VIMC_PIX_FMT_MAX_CODES 8
>   
> +extern unsigned int vimc_allocator;
> +
>   /**
>    * vimc_colorimetry_clamp - Adjust colorimetry parameters
>    *
> diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c
> index 4b0ae6f51d76..7badcecb7aed 100644
> --- a/drivers/media/test-drivers/vimc/vimc-core.c
> +++ b/drivers/media/test-drivers/vimc/vimc-core.c
> @@ -5,6 +5,7 @@
>    * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
>    */
>   
> +#include <linux/dma-mapping.h>
>   #include <linux/font.h>
>   #include <linux/init.h>
>   #include <linux/module.h>
> @@ -15,6 +16,12 @@
>   
>   #include "vimc-common.h"
>   
> +unsigned int vimc_allocator;
> +module_param_named(allocator, vimc_allocator, uint, 0444);
> +MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
> +			     "\t\t    0 == vmalloc\n"
> +			     "\t\t    1 == dma-contig");
> +

There is a section 'Module options' in vimc.rst. So a doc should be added there.

Thanks,
Dafna

>   #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
>   
>   #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) {	\
> @@ -278,6 +285,9 @@ static int vimc_probe(struct platform_device *pdev)
>   
>   	tpg_set_font(font->data);
>   
> +	if (vimc_allocator == 1)
> +		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
> +
>   	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
>   	if (!vimc)
>   		return -ENOMEM;
>
Laurent Pinchart July 30, 2021, 1:11 p.m. UTC | #2
Hi Dafna,

On Fri, Jul 30, 2021 at 02:35:20PM +0200, Dafna Hirschfeld wrote:
> On 30.07.21 02:19, Laurent Pinchart wrote:
> > The vimc driver is used for testing purpose, and some test use cases
> > involve sharing buffers with a consumer device. Consumers often require
> > DMA contiguous memory, which vimc doesn't currently support. This leads
> > in the best case to usage of bounce buffers, which is very slow, and in
> > the worst case in a complete failure.
> > 
> > Add support for the dma-contig allocator in vimc to support those use
> > cases properly. The allocator is selected through a new "allocator"
> > module parameter, which defaults to vmalloc.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > ---
> >   drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
> >   drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
> >   drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
> >   3 files changed, 19 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
> > index 5e9fd902cd37..92b69a6529fb 100644
> > --- a/drivers/media/test-drivers/vimc/vimc-capture.c
> > +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
> > @@ -7,6 +7,7 @@
> >   
> >   #include <media/v4l2-ioctl.h>
> >   #include <media/videobuf2-core.h>
> > +#include <media/videobuf2-dma-contig.h>
> >   #include <media/videobuf2-vmalloc.h>
> >   
> >   #include "vimc-common.h"
> > @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
> >   	/* Initialize the vb2 queue */
> >   	q = &vcap->queue;
> >   	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> > -	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
> > +	q->io_modes = VB2_MMAP | VB2_DMABUF;
> 
> maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?

Why so ? vb2-vmalloc can import dma-bufs.

> > +	if (vimc_allocator != 1)
> 
> maybe define a macro instead of `1` ?

Good idea.

> > +		q->io_modes |= VB2_USERPTR;
> >   	q->drv_priv = vcap;
> >   	q->buf_struct_size = sizeof(struct vimc_cap_buffer);
> >   	q->ops = &vimc_cap_qops;
> > -	q->mem_ops = &vb2_vmalloc_memops;
> > +	q->mem_ops = vimc_allocator == 1
> > +		   ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
> >   	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> >   	q->min_buffers_needed = 2;
> >   	q->lock = &vcap->lock;
> > +	q->dev = v4l2_dev->dev;
> >   
> >   	ret = vb2_queue_init(q);
> >   	if (ret) {
> > diff --git a/drivers/media/test-drivers/vimc/vimc-common.h b/drivers/media/test-drivers/vimc/vimc-common.h
> > index a289434e75ba..b77939123501 100644
> > --- a/drivers/media/test-drivers/vimc/vimc-common.h
> > +++ b/drivers/media/test-drivers/vimc/vimc-common.h
> > @@ -35,6 +35,8 @@
> >   
> >   #define VIMC_PIX_FMT_MAX_CODES 8
> >   
> > +extern unsigned int vimc_allocator;
> > +
> >   /**
> >    * vimc_colorimetry_clamp - Adjust colorimetry parameters
> >    *
> > diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c
> > index 4b0ae6f51d76..7badcecb7aed 100644
> > --- a/drivers/media/test-drivers/vimc/vimc-core.c
> > +++ b/drivers/media/test-drivers/vimc/vimc-core.c
> > @@ -5,6 +5,7 @@
> >    * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
> >    */
> >   
> > +#include <linux/dma-mapping.h>
> >   #include <linux/font.h>
> >   #include <linux/init.h>
> >   #include <linux/module.h>
> > @@ -15,6 +16,12 @@
> >   
> >   #include "vimc-common.h"
> >   
> > +unsigned int vimc_allocator;
> > +module_param_named(allocator, vimc_allocator, uint, 0444);
> > +MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
> > +			     "\t\t    0 == vmalloc\n"
> > +			     "\t\t    1 == dma-contig");
> > +
> 
> There is a section 'Module options' in vimc.rst. So a doc should be added there.

OK, I'll update that.

> >   #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
> >   
> >   #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) {	\
> > @@ -278,6 +285,9 @@ static int vimc_probe(struct platform_device *pdev)
> >   
> >   	tpg_set_font(font->data);
> >   
> > +	if (vimc_allocator == 1)
> > +		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
> > +
> >   	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
> >   	if (!vimc)
> >   		return -ENOMEM;
> >
Dafna Hirschfeld July 30, 2021, 2:08 p.m. UTC | #3
On 30.07.21 15:11, Laurent Pinchart wrote:
> Hi Dafna,
> 
> On Fri, Jul 30, 2021 at 02:35:20PM +0200, Dafna Hirschfeld wrote:
>> On 30.07.21 02:19, Laurent Pinchart wrote:
>>> The vimc driver is used for testing purpose, and some test use cases
>>> involve sharing buffers with a consumer device. Consumers often require
>>> DMA contiguous memory, which vimc doesn't currently support. This leads
>>> in the best case to usage of bounce buffers, which is very slow, and in
>>> the worst case in a complete failure.
>>>
>>> Add support for the dma-contig allocator in vimc to support those use
>>> cases properly. The allocator is selected through a new "allocator"
>>> module parameter, which defaults to vmalloc.
>>>
>>> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
>>> ---
>>>    drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
>>>    drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
>>>    drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
>>>    3 files changed, 19 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
>>> index 5e9fd902cd37..92b69a6529fb 100644
>>> --- a/drivers/media/test-drivers/vimc/vimc-capture.c
>>> +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
>>> @@ -7,6 +7,7 @@
>>>    
>>>    #include <media/v4l2-ioctl.h>
>>>    #include <media/videobuf2-core.h>
>>> +#include <media/videobuf2-dma-contig.h>
>>>    #include <media/videobuf2-vmalloc.h>
>>>    
>>>    #include "vimc-common.h"
>>> @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
>>>    	/* Initialize the vb2 queue */
>>>    	q = &vcap->queue;
>>>    	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
>>> -	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
>>> +	q->io_modes = VB2_MMAP | VB2_DMABUF;
>>
>> maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?
> 
> Why so ? vb2-vmalloc can import dma-bufs.

oh, I meant that exporting should not be supported.
I see that vimc set ".vidioc_expbuf = vb2_ioctl_expbuf", maybe remove that if allocator is vmalloc?

Thanks,
Dafna

> 
>>> +	if (vimc_allocator != 1)
>>
>> maybe define a macro instead of `1` ?
> 
> Good idea.
> 
>>> +		q->io_modes |= VB2_USERPTR;
>>>    	q->drv_priv = vcap;
>>>    	q->buf_struct_size = sizeof(struct vimc_cap_buffer);
>>>    	q->ops = &vimc_cap_qops;
>>> -	q->mem_ops = &vb2_vmalloc_memops;
>>> +	q->mem_ops = vimc_allocator == 1
>>> +		   ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
>>>    	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
>>>    	q->min_buffers_needed = 2;
>>>    	q->lock = &vcap->lock;
>>> +	q->dev = v4l2_dev->dev;
>>>    
>>>    	ret = vb2_queue_init(q);
>>>    	if (ret) {
>>> diff --git a/drivers/media/test-drivers/vimc/vimc-common.h b/drivers/media/test-drivers/vimc/vimc-common.h
>>> index a289434e75ba..b77939123501 100644
>>> --- a/drivers/media/test-drivers/vimc/vimc-common.h
>>> +++ b/drivers/media/test-drivers/vimc/vimc-common.h
>>> @@ -35,6 +35,8 @@
>>>    
>>>    #define VIMC_PIX_FMT_MAX_CODES 8
>>>    
>>> +extern unsigned int vimc_allocator;
>>> +
>>>    /**
>>>     * vimc_colorimetry_clamp - Adjust colorimetry parameters
>>>     *
>>> diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c
>>> index 4b0ae6f51d76..7badcecb7aed 100644
>>> --- a/drivers/media/test-drivers/vimc/vimc-core.c
>>> +++ b/drivers/media/test-drivers/vimc/vimc-core.c
>>> @@ -5,6 +5,7 @@
>>>     * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
>>>     */
>>>    
>>> +#include <linux/dma-mapping.h>
>>>    #include <linux/font.h>
>>>    #include <linux/init.h>
>>>    #include <linux/module.h>
>>> @@ -15,6 +16,12 @@
>>>    
>>>    #include "vimc-common.h"
>>>    
>>> +unsigned int vimc_allocator;
>>> +module_param_named(allocator, vimc_allocator, uint, 0444);
>>> +MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
>>> +			     "\t\t    0 == vmalloc\n"
>>> +			     "\t\t    1 == dma-contig");
>>> +
>>
>> There is a section 'Module options' in vimc.rst. So a doc should be added there.
> 
> OK, I'll update that.
> 
>>>    #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
>>>    
>>>    #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) {	\
>>> @@ -278,6 +285,9 @@ static int vimc_probe(struct platform_device *pdev)
>>>    
>>>    	tpg_set_font(font->data);
>>>    
>>> +	if (vimc_allocator == 1)
>>> +		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
>>> +
>>>    	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
>>>    	if (!vimc)
>>>    		return -ENOMEM;
>>>
>
Laurent Pinchart July 30, 2021, 2:15 p.m. UTC | #4
Hi Dafna,

On Fri, Jul 30, 2021 at 04:08:11PM +0200, Dafna Hirschfeld wrote:
> On 30.07.21 15:11, Laurent Pinchart wrote:
> > On Fri, Jul 30, 2021 at 02:35:20PM +0200, Dafna Hirschfeld wrote:
> >> On 30.07.21 02:19, Laurent Pinchart wrote:
> >>> The vimc driver is used for testing purpose, and some test use cases
> >>> involve sharing buffers with a consumer device. Consumers often require
> >>> DMA contiguous memory, which vimc doesn't currently support. This leads
> >>> in the best case to usage of bounce buffers, which is very slow, and in
> >>> the worst case in a complete failure.
> >>>
> >>> Add support for the dma-contig allocator in vimc to support those use
> >>> cases properly. The allocator is selected through a new "allocator"
> >>> module parameter, which defaults to vmalloc.
> >>>
> >>> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> >>> ---
> >>>    drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
> >>>    drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
> >>>    drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
> >>>    3 files changed, 19 insertions(+), 2 deletions(-)
> >>>
> >>> diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
> >>> index 5e9fd902cd37..92b69a6529fb 100644
> >>> --- a/drivers/media/test-drivers/vimc/vimc-capture.c
> >>> +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
> >>> @@ -7,6 +7,7 @@
> >>>    
> >>>    #include <media/v4l2-ioctl.h>
> >>>    #include <media/videobuf2-core.h>
> >>> +#include <media/videobuf2-dma-contig.h>
> >>>    #include <media/videobuf2-vmalloc.h>
> >>>    
> >>>    #include "vimc-common.h"
> >>> @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
> >>>    	/* Initialize the vb2 queue */
> >>>    	q = &vcap->queue;
> >>>    	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> >>> -	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
> >>> +	q->io_modes = VB2_MMAP | VB2_DMABUF;
> >>
> >> maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?
> > 
> > Why so ? vb2-vmalloc can import dma-bufs.
> 
> oh, I meant that exporting should not be supported.
> I see that vimc set ".vidioc_expbuf = vb2_ioctl_expbuf", maybe remove that if allocator is vmalloc?

If the importer support non-contiguous buffers, vb2-vmalloc can be used
as an exporter. I've successfully used this to test sharing buffers
between vimc in vmalloc mode and the R-Car H3 display driver with an
IOMMU.

> >>> +	if (vimc_allocator != 1)
> >>
> >> maybe define a macro instead of `1` ?
> > 
> > Good idea.
> > 
> >>> +		q->io_modes |= VB2_USERPTR;
> >>>    	q->drv_priv = vcap;
> >>>    	q->buf_struct_size = sizeof(struct vimc_cap_buffer);
> >>>    	q->ops = &vimc_cap_qops;
> >>> -	q->mem_ops = &vb2_vmalloc_memops;
> >>> +	q->mem_ops = vimc_allocator == 1
> >>> +		   ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
> >>>    	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> >>>    	q->min_buffers_needed = 2;
> >>>    	q->lock = &vcap->lock;
> >>> +	q->dev = v4l2_dev->dev;
> >>>    
> >>>    	ret = vb2_queue_init(q);
> >>>    	if (ret) {
> >>> diff --git a/drivers/media/test-drivers/vimc/vimc-common.h b/drivers/media/test-drivers/vimc/vimc-common.h
> >>> index a289434e75ba..b77939123501 100644
> >>> --- a/drivers/media/test-drivers/vimc/vimc-common.h
> >>> +++ b/drivers/media/test-drivers/vimc/vimc-common.h
> >>> @@ -35,6 +35,8 @@
> >>>    
> >>>    #define VIMC_PIX_FMT_MAX_CODES 8
> >>>    
> >>> +extern unsigned int vimc_allocator;
> >>> +
> >>>    /**
> >>>     * vimc_colorimetry_clamp - Adjust colorimetry parameters
> >>>     *
> >>> diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c
> >>> index 4b0ae6f51d76..7badcecb7aed 100644
> >>> --- a/drivers/media/test-drivers/vimc/vimc-core.c
> >>> +++ b/drivers/media/test-drivers/vimc/vimc-core.c
> >>> @@ -5,6 +5,7 @@
> >>>     * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
> >>>     */
> >>>    
> >>> +#include <linux/dma-mapping.h>
> >>>    #include <linux/font.h>
> >>>    #include <linux/init.h>
> >>>    #include <linux/module.h>
> >>> @@ -15,6 +16,12 @@
> >>>    
> >>>    #include "vimc-common.h"
> >>>    
> >>> +unsigned int vimc_allocator;
> >>> +module_param_named(allocator, vimc_allocator, uint, 0444);
> >>> +MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
> >>> +			     "\t\t    0 == vmalloc\n"
> >>> +			     "\t\t    1 == dma-contig");
> >>> +
> >>
> >> There is a section 'Module options' in vimc.rst. So a doc should be added there.
> > 
> > OK, I'll update that.
> > 
> >>>    #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
> >>>    
> >>>    #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) {	\
> >>> @@ -278,6 +285,9 @@ static int vimc_probe(struct platform_device *pdev)
> >>>    
> >>>    	tpg_set_font(font->data);
> >>>    
> >>> +	if (vimc_allocator == 1)
> >>> +		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
> >>> +
> >>>    	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
> >>>    	if (!vimc)
> >>>    		return -ENOMEM;
> >>>
Nicolas Dufresne July 30, 2021, 3:56 p.m. UTC | #5
Le vendredi 30 juillet 2021 à 16:11 +0300, Laurent Pinchart a écrit :
> Hi Dafna,
> 
> On Fri, Jul 30, 2021 at 02:35:20PM +0200, Dafna Hirschfeld wrote:
> > On 30.07.21 02:19, Laurent Pinchart wrote:
> > > The vimc driver is used for testing purpose, and some test use cases
> > > involve sharing buffers with a consumer device. Consumers often require
> > > DMA contiguous memory, which vimc doesn't currently support. This leads
> > > in the best case to usage of bounce buffers, which is very slow, and in
> > > the worst case in a complete failure.
> > > 
> > > Add support for the dma-contig allocator in vimc to support those use
> > > cases properly. The allocator is selected through a new "allocator"
> > > module parameter, which defaults to vmalloc.
> > > 
> > > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > > ---
> > >   drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
> > >   drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
> > >   drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
> > >   3 files changed, 19 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
> > > index 5e9fd902cd37..92b69a6529fb 100644
> > > --- a/drivers/media/test-drivers/vimc/vimc-capture.c
> > > +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
> > > @@ -7,6 +7,7 @@
> > >   
> > >   #include <media/v4l2-ioctl.h>
> > >   #include <media/videobuf2-core.h>
> > > +#include <media/videobuf2-dma-contig.h>
> > >   #include <media/videobuf2-vmalloc.h>
> > >   
> > >   #include "vimc-common.h"
> > > @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
> > >   	/* Initialize the vb2 queue */
> > >   	q = &vcap->queue;
> > >   	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> > > -	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
> > > +	q->io_modes = VB2_MMAP | VB2_DMABUF;
> > 
> > maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?
> 
> Why so ? vb2-vmalloc can import dma-bufs.

Indeed, should be safe with both allocator, the CMA one will validate that the
pages are contiguous and fail synchronously as expected for CMA. The known
issues are mostly for reading importers (consumers), for writing importer
(producers) it is likely still a bit buggy on cache management.

> > >   	if (ret) {
> > > diff --git a/drivers/media/test-drivers/vimc/vimc-common.h b/drivers/media/test-drivers/vimc/vimc-common.h
> > > index a289434e75ba..b77939123501 100644
> > > --- a/drivers/media/test-drivers/vimc/vimc-common.h
> > > +++ b/drivers/media/test-drivers/vimc/vimc-common.h
> > > @@ -35,6 +35,8 @@
> > >   
> > >   #define VIMC_PIX_FMT_MAX_CODES 8
> > >   
> > > +extern unsigned int vimc_allocator;
> > > +
> > >   /**
> > >    * vimc_colorimetry_clamp - Adjust colorimetry parameters
> > >    *
> > > diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c
> > > index 4b0ae6f51d76..7badcecb7aed 100644
> > > --- a/drivers/media/test-drivers/vimc/vimc-core.c
> > > +++ b/drivers/media/test-drivers/vimc/vimc-core.c
> > > @@ -5,6 +5,7 @@
> > >    * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
> > >    */
> > >   
> > > +#include <linux/dma-mapping.h>
> > >   #include <linux/font.h>
> > >   #include <linux/init.h>
> > >   #include <linux/module.h>
> > > @@ -15,6 +16,12 @@
> > >   
> > >   #include "vimc-common.h"
> > >   
> > > +unsigned int vimc_allocator;
> > > +module_param_named(allocator, vimc_allocator, uint, 0444);
> > > +MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
> > > +			     "\t\t    0 == vmalloc\n"
> > > +			     "\t\t    1 == dma-contig");
> > > +
> > 
> > There is a section 'Module options' in vimc.rst. So a doc should be added there.
> 
> OK, I'll update that.
> 
> > >   #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
> > >   
> > >   #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) {	\
> > > @@ -278,6 +285,9 @@ static int vimc_probe(struct platform_device *pdev)
> > >   
> > >   	tpg_set_font(font->data);
> > >   
> > > +	if (vimc_allocator == 1)
> > > +		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
> > > +
> > >   	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
> > >   	if (!vimc)
> > >   		return -ENOMEM;
> > > 
>
Nicolas Dufresne July 30, 2021, 3:59 p.m. UTC | #6
Le vendredi 30 juillet 2021 à 17:15 +0300, Laurent Pinchart a écrit :
> Hi Dafna,
> 
> On Fri, Jul 30, 2021 at 04:08:11PM +0200, Dafna Hirschfeld wrote:
> > On 30.07.21 15:11, Laurent Pinchart wrote:
> > > On Fri, Jul 30, 2021 at 02:35:20PM +0200, Dafna Hirschfeld wrote:
> > > > On 30.07.21 02:19, Laurent Pinchart wrote:
> > > > > The vimc driver is used for testing purpose, and some test use cases
> > > > > involve sharing buffers with a consumer device. Consumers often require
> > > > > DMA contiguous memory, which vimc doesn't currently support. This leads
> > > > > in the best case to usage of bounce buffers, which is very slow, and in
> > > > > the worst case in a complete failure.
> > > > > 
> > > > > Add support for the dma-contig allocator in vimc to support those use
> > > > > cases properly. The allocator is selected through a new "allocator"
> > > > > module parameter, which defaults to vmalloc.
> > > > > 
> > > > > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > > > > ---
> > > > >    drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
> > > > >    drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
> > > > >    drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
> > > > >    3 files changed, 19 insertions(+), 2 deletions(-)
> > > > > 
> > > > > diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
> > > > > index 5e9fd902cd37..92b69a6529fb 100644
> > > > > --- a/drivers/media/test-drivers/vimc/vimc-capture.c
> > > > > +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
> > > > > @@ -7,6 +7,7 @@
> > > > >    
> > > > >    #include <media/v4l2-ioctl.h>
> > > > >    #include <media/videobuf2-core.h>
> > > > > +#include <media/videobuf2-dma-contig.h>
> > > > >    #include <media/videobuf2-vmalloc.h>
> > > > >    
> > > > >    #include "vimc-common.h"
> > > > > @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
> > > > >    	/* Initialize the vb2 queue */
> > > > >    	q = &vcap->queue;
> > > > >    	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> > > > > -	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
> > > > > +	q->io_modes = VB2_MMAP | VB2_DMABUF;
> > > > 
> > > > maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?
> > > 
> > > Why so ? vb2-vmalloc can import dma-bufs.
> > 
> > oh, I meant that exporting should not be supported.
> > I see that vimc set ".vidioc_expbuf = vb2_ioctl_expbuf", maybe remove that if allocator is vmalloc?
> 
> If the importer support non-contiguous buffers, vb2-vmalloc can be used
> as an exporter. I've successfully used this to test sharing buffers
> between vimc in vmalloc mode and the R-Car H3 display driver with an
> IOMMU.

Having an IOMMU is not sufficient, as this is shown with Intel DRM. The DRM
driver needs to support CPU cache. Note that in GStreamer this is used a lot
from UVC camera to GL (but it breaks, corrupted images, with kmssink).

> 
> > > > > +	if (vimc_allocator != 1)
> > > > 
> > > > maybe define a macro instead of `1` ?
> > > 
> > > Good idea.
> > > 
> > > > > +		q->io_modes |= VB2_USERPTR;
> > > > >    	q->drv_priv = vcap;
> > > > >    	q->buf_struct_size = sizeof(struct vimc_cap_buffer);
> > > > >    	q->ops = &vimc_cap_qops;
> > > > > -	q->mem_ops = &vb2_vmalloc_memops;
> > > > > +	q->mem_ops = vimc_allocator == 1
> > > > > +		   ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
> > > > >    	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> > > > >    	q->min_buffers_needed = 2;
> > > > >    	q->lock = &vcap->lock;
> > > > > +	q->dev = v4l2_dev->dev;
> > > > >    
> > > > >    	ret = vb2_queue_init(q);
> > > > >    	if (ret) {
> > > > > diff --git a/drivers/media/test-drivers/vimc/vimc-common.h b/drivers/media/test-drivers/vimc/vimc-common.h
> > > > > index a289434e75ba..b77939123501 100644
> > > > > --- a/drivers/media/test-drivers/vimc/vimc-common.h
> > > > > +++ b/drivers/media/test-drivers/vimc/vimc-common.h
> > > > > @@ -35,6 +35,8 @@
> > > > >    
> > > > >    #define VIMC_PIX_FMT_MAX_CODES 8
> > > > >    
> > > > > +extern unsigned int vimc_allocator;
> > > > > +
> > > > >    /**
> > > > >     * vimc_colorimetry_clamp - Adjust colorimetry parameters
> > > > >     *
> > > > > diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c
> > > > > index 4b0ae6f51d76..7badcecb7aed 100644
> > > > > --- a/drivers/media/test-drivers/vimc/vimc-core.c
> > > > > +++ b/drivers/media/test-drivers/vimc/vimc-core.c
> > > > > @@ -5,6 +5,7 @@
> > > > >     * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
> > > > >     */
> > > > >    
> > > > > +#include <linux/dma-mapping.h>
> > > > >    #include <linux/font.h>
> > > > >    #include <linux/init.h>
> > > > >    #include <linux/module.h>
> > > > > @@ -15,6 +16,12 @@
> > > > >    
> > > > >    #include "vimc-common.h"
> > > > >    
> > > > > +unsigned int vimc_allocator;
> > > > > +module_param_named(allocator, vimc_allocator, uint, 0444);
> > > > > +MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
> > > > > +			     "\t\t    0 == vmalloc\n"
> > > > > +			     "\t\t    1 == dma-contig");
> > > > > +
> > > > 
> > > > There is a section 'Module options' in vimc.rst. So a doc should be added there.
> > > 
> > > OK, I'll update that.
> > > 
> > > > >    #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
> > > > >    
> > > > >    #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) {	\
> > > > > @@ -278,6 +285,9 @@ static int vimc_probe(struct platform_device *pdev)
> > > > >    
> > > > >    	tpg_set_font(font->data);
> > > > >    
> > > > > +	if (vimc_allocator == 1)
> > > > > +		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
> > > > > +
> > > > >    	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
> > > > >    	if (!vimc)
> > > > >    		return -ENOMEM;
> > > > > 
>
Ezequiel Garcia July 30, 2021, 4:13 p.m. UTC | #7
Hi Laurent, Dafna,

On Fri, 30 Jul 2021 at 09:35, Dafna Hirschfeld
<dafna.hirschfeld@collabora.com> wrote:
>
> Hi
>
> On 30.07.21 02:19, Laurent Pinchart wrote:
> > The vimc driver is used for testing purpose, and some test use cases
> > involve sharing buffers with a consumer device. Consumers often require
> > DMA contiguous memory, which vimc doesn't currently support. This leads
> > in the best case to usage of bounce buffers, which is very slow, and in
> > the worst case in a complete failure.
> >
> > Add support for the dma-contig allocator in vimc to support those use
> > cases properly. The allocator is selected through a new "allocator"
> > module parameter, which defaults to vmalloc.
> >
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > ---
> >   drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
> >   drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
> >   drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
> >   3 files changed, 19 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
> > index 5e9fd902cd37..92b69a6529fb 100644
> > --- a/drivers/media/test-drivers/vimc/vimc-capture.c
> > +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
> > @@ -7,6 +7,7 @@
> >
> >   #include <media/v4l2-ioctl.h>
> >   #include <media/videobuf2-core.h>
> > +#include <media/videobuf2-dma-contig.h>
> >   #include <media/videobuf2-vmalloc.h>
> >
> >   #include "vimc-common.h"
> > @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
> >       /* Initialize the vb2 queue */
> >       q = &vcap->queue;
> >       q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> > -     q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
> > +     q->io_modes = VB2_MMAP | VB2_DMABUF;
>
> maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?
>
> > +     if (vimc_allocator != 1)
>
> maybe define a macro instead of `1` ?
>

This is maybe an overkill, but you can make the parameter accept strings
instead of integers, which makes it "modprobe vimc allocator=vmalloc",
and improves a bit user-friendlyness.

See drivers/media/pci/tw686x/tw686x-core.c.

For a test driver, it is worth the trouble, maybe?

Thanks,
Ezequiel
Laurent Pinchart July 30, 2021, 4:50 p.m. UTC | #8
Hi Ezequiel,

On Fri, Jul 30, 2021 at 01:13:20PM -0300, Ezequiel Garcia wrote:
> On Fri, 30 Jul 2021 at 09:35, Dafna Hirschfeld wrote:
> > On 30.07.21 02:19, Laurent Pinchart wrote:
> > > The vimc driver is used for testing purpose, and some test use cases
> > > involve sharing buffers with a consumer device. Consumers often require
> > > DMA contiguous memory, which vimc doesn't currently support. This leads
> > > in the best case to usage of bounce buffers, which is very slow, and in
> > > the worst case in a complete failure.
> > >
> > > Add support for the dma-contig allocator in vimc to support those use
> > > cases properly. The allocator is selected through a new "allocator"
> > > module parameter, which defaults to vmalloc.
> > >
> > > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > > ---
> > >   drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
> > >   drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
> > >   drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
> > >   3 files changed, 19 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
> > > index 5e9fd902cd37..92b69a6529fb 100644
> > > --- a/drivers/media/test-drivers/vimc/vimc-capture.c
> > > +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
> > > @@ -7,6 +7,7 @@
> > >
> > >   #include <media/v4l2-ioctl.h>
> > >   #include <media/videobuf2-core.h>
> > > +#include <media/videobuf2-dma-contig.h>
> > >   #include <media/videobuf2-vmalloc.h>
> > >
> > >   #include "vimc-common.h"
> > > @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
> > >       /* Initialize the vb2 queue */
> > >       q = &vcap->queue;
> > >       q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> > > -     q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
> > > +     q->io_modes = VB2_MMAP | VB2_DMABUF;
> >
> > maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?
> >
> > > +     if (vimc_allocator != 1)
> >
> > maybe define a macro instead of `1` ?
> 
> This is maybe an overkill, but you can make the parameter accept strings
> instead of integers, which makes it "modprobe vimc allocator=vmalloc",
> and improves a bit user-friendlyness.
> 
> See drivers/media/pci/tw686x/tw686x-core.c.
> 
> For a test driver, it is worth the trouble, maybe?

I copied the values from vivid, which uses an array of integers. As vimc
needs a single parameter only, a string could make more sense. I'll
submit a v3 with a string if there's a consensus for that.
Laurent Pinchart July 30, 2021, 4:56 p.m. UTC | #9
Hi Nicolas,

On Fri, Jul 30, 2021 at 11:59:27AM -0400, Nicolas Dufresne wrote:
> Le vendredi 30 juillet 2021 à 17:15 +0300, Laurent Pinchart a écrit :
> > On Fri, Jul 30, 2021 at 04:08:11PM +0200, Dafna Hirschfeld wrote:
> > > On 30.07.21 15:11, Laurent Pinchart wrote:
> > > > On Fri, Jul 30, 2021 at 02:35:20PM +0200, Dafna Hirschfeld wrote:
> > > > > On 30.07.21 02:19, Laurent Pinchart wrote:
> > > > > > The vimc driver is used for testing purpose, and some test use cases
> > > > > > involve sharing buffers with a consumer device. Consumers often require
> > > > > > DMA contiguous memory, which vimc doesn't currently support. This leads
> > > > > > in the best case to usage of bounce buffers, which is very slow, and in
> > > > > > the worst case in a complete failure.
> > > > > > 
> > > > > > Add support for the dma-contig allocator in vimc to support those use
> > > > > > cases properly. The allocator is selected through a new "allocator"
> > > > > > module parameter, which defaults to vmalloc.
> > > > > > 
> > > > > > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > > > > > ---
> > > > > >    drivers/media/test-drivers/vimc/vimc-capture.c |  9 +++++++--
> > > > > >    drivers/media/test-drivers/vimc/vimc-common.h  |  2 ++
> > > > > >    drivers/media/test-drivers/vimc/vimc-core.c    | 10 ++++++++++
> > > > > >    3 files changed, 19 insertions(+), 2 deletions(-)
> > > > > > 
> > > > > > diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
> > > > > > index 5e9fd902cd37..92b69a6529fb 100644
> > > > > > --- a/drivers/media/test-drivers/vimc/vimc-capture.c
> > > > > > +++ b/drivers/media/test-drivers/vimc/vimc-capture.c
> > > > > > @@ -7,6 +7,7 @@
> > > > > >    
> > > > > >    #include <media/v4l2-ioctl.h>
> > > > > >    #include <media/videobuf2-core.h>
> > > > > > +#include <media/videobuf2-dma-contig.h>
> > > > > >    #include <media/videobuf2-vmalloc.h>
> > > > > >    
> > > > > >    #include "vimc-common.h"
> > > > > > @@ -423,14 +424,18 @@ static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
> > > > > >    	/* Initialize the vb2 queue */
> > > > > >    	q = &vcap->queue;
> > > > > >    	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> > > > > > -	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
> > > > > > +	q->io_modes = VB2_MMAP | VB2_DMABUF;
> > > > > 
> > > > > maybe to be on the safe side VB2_DMABUF should be set only if vimc_allocator==1 ?
> > > > 
> > > > Why so ? vb2-vmalloc can import dma-bufs.
> > > 
> > > oh, I meant that exporting should not be supported.
> > > I see that vimc set ".vidioc_expbuf = vb2_ioctl_expbuf", maybe remove that if allocator is vmalloc?
> > 
> > If the importer support non-contiguous buffers, vb2-vmalloc can be used
> > as an exporter. I've successfully used this to test sharing buffers
> > between vimc in vmalloc mode and the R-Car H3 display driver with an
> > IOMMU.
> 
> Having an IOMMU is not sufficient, as this is shown with Intel DRM. The DRM
> driver needs to support CPU cache. Note that in GStreamer this is used a lot
> from UVC camera to GL (but it breaks, corrupted images, with kmssink).

That depends on the platform. When the sink device isn't cache-coherent,
it needs to perform a cache sync operation. V4L2 handles this
automatically for codecs (technically speaking for cameras as well, but
cameras are rarely sinks :-)). In DRM/KMS, there's still quite a bit of
work to do. It's not an issue in vimc in any case.

> > > > > > +	if (vimc_allocator != 1)
> > > > > 
> > > > > maybe define a macro instead of `1` ?
> > > > 
> > > > Good idea.
> > > > 
> > > > > > +		q->io_modes |= VB2_USERPTR;
> > > > > >    	q->drv_priv = vcap;
> > > > > >    	q->buf_struct_size = sizeof(struct vimc_cap_buffer);
> > > > > >    	q->ops = &vimc_cap_qops;
> > > > > > -	q->mem_ops = &vb2_vmalloc_memops;
> > > > > > +	q->mem_ops = vimc_allocator == 1
> > > > > > +		   ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
> > > > > >    	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> > > > > >    	q->min_buffers_needed = 2;
> > > > > >    	q->lock = &vcap->lock;
> > > > > > +	q->dev = v4l2_dev->dev;
> > > > > >    
> > > > > >    	ret = vb2_queue_init(q);
> > > > > >    	if (ret) {
> > > > > > diff --git a/drivers/media/test-drivers/vimc/vimc-common.h b/drivers/media/test-drivers/vimc/vimc-common.h
> > > > > > index a289434e75ba..b77939123501 100644
> > > > > > --- a/drivers/media/test-drivers/vimc/vimc-common.h
> > > > > > +++ b/drivers/media/test-drivers/vimc/vimc-common.h
> > > > > > @@ -35,6 +35,8 @@
> > > > > >    
> > > > > >    #define VIMC_PIX_FMT_MAX_CODES 8
> > > > > >    
> > > > > > +extern unsigned int vimc_allocator;
> > > > > > +
> > > > > >    /**
> > > > > >     * vimc_colorimetry_clamp - Adjust colorimetry parameters
> > > > > >     *
> > > > > > diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c
> > > > > > index 4b0ae6f51d76..7badcecb7aed 100644
> > > > > > --- a/drivers/media/test-drivers/vimc/vimc-core.c
> > > > > > +++ b/drivers/media/test-drivers/vimc/vimc-core.c
> > > > > > @@ -5,6 +5,7 @@
> > > > > >     * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
> > > > > >     */
> > > > > >    
> > > > > > +#include <linux/dma-mapping.h>
> > > > > >    #include <linux/font.h>
> > > > > >    #include <linux/init.h>
> > > > > >    #include <linux/module.h>
> > > > > > @@ -15,6 +16,12 @@
> > > > > >    
> > > > > >    #include "vimc-common.h"
> > > > > >    
> > > > > > +unsigned int vimc_allocator;
> > > > > > +module_param_named(allocator, vimc_allocator, uint, 0444);
> > > > > > +MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
> > > > > > +			     "\t\t    0 == vmalloc\n"
> > > > > > +			     "\t\t    1 == dma-contig");
> > > > > > +
> > > > > 
> > > > > There is a section 'Module options' in vimc.rst. So a doc should be added there.
> > > > 
> > > > OK, I'll update that.
> > > > 
> > > > > >    #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
> > > > > >    
> > > > > >    #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) {	\
> > > > > > @@ -278,6 +285,9 @@ static int vimc_probe(struct platform_device *pdev)
> > > > > >    
> > > > > >    	tpg_set_font(font->data);
> > > > > >    
> > > > > > +	if (vimc_allocator == 1)
> > > > > > +		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
> > > > > > +
> > > > > >    	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
> > > > > >    	if (!vimc)
> > > > > >    		return -ENOMEM;
> > > > > >
diff mbox series

Patch

diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c
index 5e9fd902cd37..92b69a6529fb 100644
--- a/drivers/media/test-drivers/vimc/vimc-capture.c
+++ b/drivers/media/test-drivers/vimc/vimc-capture.c
@@ -7,6 +7,7 @@ 
 
 #include <media/v4l2-ioctl.h>
 #include <media/videobuf2-core.h>
+#include <media/videobuf2-dma-contig.h>
 #include <media/videobuf2-vmalloc.h>
 
 #include "vimc-common.h"
@@ -423,14 +424,18 @@  static struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
 	/* Initialize the vb2 queue */
 	q = &vcap->queue;
 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
+	q->io_modes = VB2_MMAP | VB2_DMABUF;
+	if (vimc_allocator != 1)
+		q->io_modes |= VB2_USERPTR;
 	q->drv_priv = vcap;
 	q->buf_struct_size = sizeof(struct vimc_cap_buffer);
 	q->ops = &vimc_cap_qops;
-	q->mem_ops = &vb2_vmalloc_memops;
+	q->mem_ops = vimc_allocator == 1
+		   ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 	q->min_buffers_needed = 2;
 	q->lock = &vcap->lock;
+	q->dev = v4l2_dev->dev;
 
 	ret = vb2_queue_init(q);
 	if (ret) {
diff --git a/drivers/media/test-drivers/vimc/vimc-common.h b/drivers/media/test-drivers/vimc/vimc-common.h
index a289434e75ba..b77939123501 100644
--- a/drivers/media/test-drivers/vimc/vimc-common.h
+++ b/drivers/media/test-drivers/vimc/vimc-common.h
@@ -35,6 +35,8 @@ 
 
 #define VIMC_PIX_FMT_MAX_CODES 8
 
+extern unsigned int vimc_allocator;
+
 /**
  * vimc_colorimetry_clamp - Adjust colorimetry parameters
  *
diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c
index 4b0ae6f51d76..7badcecb7aed 100644
--- a/drivers/media/test-drivers/vimc/vimc-core.c
+++ b/drivers/media/test-drivers/vimc/vimc-core.c
@@ -5,6 +5,7 @@ 
  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
  */
 
+#include <linux/dma-mapping.h>
 #include <linux/font.h>
 #include <linux/init.h>
 #include <linux/module.h>
@@ -15,6 +16,12 @@ 
 
 #include "vimc-common.h"
 
+unsigned int vimc_allocator;
+module_param_named(allocator, vimc_allocator, uint, 0444);
+MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
+			     "\t\t    0 == vmalloc\n"
+			     "\t\t    1 == dma-contig");
+
 #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
 
 #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) {	\
@@ -278,6 +285,9 @@  static int vimc_probe(struct platform_device *pdev)
 
 	tpg_set_font(font->data);
 
+	if (vimc_allocator == 1)
+		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
+
 	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
 	if (!vimc)
 		return -ENOMEM;