[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-09 Thread Subash Patel
Hello Tomasz, Laurent,

I have printed some logs during the dmabuf export and attach for the SGT 
issue below. Please find it in the attachment. I hope it will be useful.

Regards,
Subash

On 05/08/2012 04:45 PM, Subash Patel wrote:
> Hi Laurent,
>
> On 05/08/2012 02:44 PM, Laurent Pinchart wrote:
>> Hi Subash,
>>
>> On Monday 07 May 2012 20:08:25 Subash Patel wrote:
>>> Hello Thomasz, Laurent,
>>>
>>> I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
>>> during the attach, the size of the SGT and size requested mis-matched
>>> (by atleast 8k bytes). Hence I made a small correction to the code as
>>> below. I could then attach the importer properly.
>>
>> Thank you for the report.
>>
>> Could you print the content of the sglist (number of chunks and size
>> of each
>> chunk) before and after your modifications, as well as the values of
>> n_pages,
>> offset and size ?
> I will put back all the printk's and generate this. As of now, my setup
> has changed and will do this when I get sometime.
>>
>>> On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:
>>
>> [snip]
>>
 +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
 + unsigned int n_pages, unsigned long offset, unsigned long size)
 +{
 + struct sg_table *sgt;
 + unsigned int chunks;
 + unsigned int i;
 + unsigned int cur_page;
 + int ret;
 + struct scatterlist *s;
 +
 + sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
 + if (!sgt)
 + return ERR_PTR(-ENOMEM);
 +
 + /* compute number of chunks */
 + chunks = 1;
 + for (i = 1; i< n_pages; ++i)
 + if (pages[i] != pages[i - 1] + 1)
 + ++chunks;
 +
 + ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
 + if (ret) {
 + kfree(sgt);
 + return ERR_PTR(-ENOMEM);
 + }
 +
 + /* merging chunks and putting them into the scatterlist */
 + cur_page = 0;
 + for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
 + unsigned long chunk_size;
 + unsigned int j;
>>>
>>> size = PAGE_SIZE;
>>>
 +
 + for (j = cur_page + 1; j< n_pages; ++j)
>>>
>>> for (j = cur_page + 1; j< n_pages; ++j) {
>>>
 + if (pages[j] != pages[j - 1] + 1)
 + break;
>>>
>>> size += PAGE
>>> }
>>>
 +
 + chunk_size = ((j - cur_page)<< PAGE_SHIFT) - offset;
 + sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
>>>
>>> [DELETE] size -= chunk_size;
>>>
 + offset = 0;
 + cur_page = j;
 + }
 +
 + return sgt;
 +}
>>
> Regards,
> Subash
-- next part --
[  178.545000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.545000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.55] vb2_dc_pages_to_sgt():84 offset=0
[  178.555000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.56] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.565000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.57] vb2_dc_pages_to_sgt():83 cur_page=32
[  178.575000] vb2_dc_pages_to_sgt():84 offset=0
[  178.58] vb2_dc_pages_to_sgt():86 chunk_size=262144
[  178.585000] vb2_dc_pages_to_sgt():89 size=4294574080
[  178.59] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.595000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.60] vb2_dc_pages_to_sgt():84 offset=0
[  178.605000] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.61] vb2_dc_pages_to_sgt():89 size=4294959104
[  178.625000] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.625000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.63] vb2_dc_pages_to_sgt():84 offset=0
[  178.635000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.64] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.645000] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.65] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.655000] vb2_dc_pages_to_sgt():84 offset=0
[  178.66] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.665000] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.67] vb2_dc_mmap: mapped dma addr 0x2006 at 0xb6e01000, size 
131072
[  178.67] vb2_dc_mmap: mapped dma addr 0x2008 at 0xb6de1000, size 
131072
[  178.68] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.685000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.69] vb2_dc_pages_to_sgt():84 offset=0
[  178.695000] vb2_dc_pages_to_sgt():86 chunk_size=4096
[  178.70] vb2_dc_pages_to_sgt():89 size=4294963200
[  178.705000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.71] vb2_dc_pages_to_sgt():83 cur_page=1
[  178.715000] vb2_dc_pages_to_sgt():84 offset=0
[  178.715000] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.72] vb2_dc_pages_to_sgt():89 size=4294955008
[  178.725000] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.73] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.735000] vb2_dc_pages_to_sgt():84 offset=0
[  178.74] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.745000] vb2_dc_pages_to_sgt():89 size=4294959104
[  178.75] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.755000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.76] 

[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-09 Thread Tomasz Stanislawski
Hi Subash,
Could you post the code of vb2_dc_pages_to_sgt with all printk in it.
It will help us avoid guessing where and what is debugged in the log.

Moreover, I found a line 'size=4294836224' in the log.
It means that size is equal to -131072 (!?!) or there are some invalid
conversions in printk.

Are you suze that you do not pass size = 0 as the function argument?

Notice that earlier versions of dmabuf-for-vb2 patches has offset2
argument instead of size. It was the offset at the end of the buffer.
In (I guess) 95% of cases this offset was 0.

Could you provide only function arguments that causes the failure?
I mean pages array + size (I assume that offset is zero for your test).

Having the arguments we could reproduce that bug.

Regards,
Tomasz Stanislawski





On 05/09/2012 08:46 AM, Subash Patel wrote:
> Hello Tomasz, Laurent,
> 
> I have printed some logs during the dmabuf export and attach for the SGT 
> issue below. Please find it in the attachment. I hope
> it will be useful.
> 
> Regards,
> Subash
> 
> On 05/08/2012 04:45 PM, Subash Patel wrote:
>> Hi Laurent,
>>
>> On 05/08/2012 02:44 PM, Laurent Pinchart wrote:
>>> Hi Subash,
>>>
>>> On Monday 07 May 2012 20:08:25 Subash Patel wrote:
 Hello Thomasz, Laurent,

 I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
 during the attach, the size of the SGT and size requested mis-matched
 (by atleast 8k bytes). Hence I made a small correction to the code as
 below. I could then attach the importer properly.
>>>
>>> Thank you for the report.
>>>
>>> Could you print the content of the sglist (number of chunks and size
>>> of each
>>> chunk) before and after your modifications, as well as the values of
>>> n_pages,
>>> offset and size ?
>> I will put back all the printk's and generate this. As of now, my setup
>> has changed and will do this when I get sometime.
>>>
 On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:
>>>
>>> [snip]
>>>
> +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
> + unsigned int n_pages, unsigned long offset, unsigned long size)
> +{
> + struct sg_table *sgt;
> + unsigned int chunks;
> + unsigned int i;
> + unsigned int cur_page;
> + int ret;
> + struct scatterlist *s;
> +
> + sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
> + if (!sgt)
> + return ERR_PTR(-ENOMEM);
> +
> + /* compute number of chunks */
> + chunks = 1;
> + for (i = 1; i< n_pages; ++i)
> + if (pages[i] != pages[i - 1] + 1)
> + ++chunks;
> +
> + ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
> + if (ret) {
> + kfree(sgt);
> + return ERR_PTR(-ENOMEM);
> + }
> +
> + /* merging chunks and putting them into the scatterlist */
> + cur_page = 0;
> + for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
> + unsigned long chunk_size;
> + unsigned int j;

 size = PAGE_SIZE;

> +
> + for (j = cur_page + 1; j< n_pages; ++j)

 for (j = cur_page + 1; j< n_pages; ++j) {

> + if (pages[j] != pages[j - 1] + 1)
> + break;

 size += PAGE
 }

> +
> + chunk_size = ((j - cur_page)<< PAGE_SHIFT) - offset;
> + sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);

 [DELETE] size -= chunk_size;

> + offset = 0;
> + cur_page = j;
> + }
> +
> + return sgt;
> +}
>>>
>> Regards,
>> Subash



Re: [PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-09 Thread Subash Patel

Hi Laurent,

On 05/08/2012 02:44 PM, Laurent Pinchart wrote:

Hi Subash,

On Monday 07 May 2012 20:08:25 Subash Patel wrote:

Hello Thomasz, Laurent,

I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
during the attach, the size of the SGT and size requested mis-matched
(by atleast 8k bytes). Hence I made a small correction to the code as
below. I could then attach the importer properly.


Thank you for the report.

Could you print the content of the sglist (number of chunks and size of each
chunk) before and after your modifications, as well as the values of n_pages,
offset and size ?
I will put back all the printk's and generate this. As of now, my setup 
has changed and will do this when I get sometime.



On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:


[snip]


+static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
+   unsigned int n_pages, unsigned long offset, unsigned long size)
+{
+   struct sg_table *sgt;
+   unsigned int chunks;
+   unsigned int i;
+   unsigned int cur_page;
+   int ret;
+   struct scatterlist *s;
+
+   sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
+   if (!sgt)
+   return ERR_PTR(-ENOMEM);
+
+   /* compute number of chunks */
+   chunks = 1;
+   for (i = 1; i   n_pages; ++i)
+   if (pages[i] != pages[i - 1] + 1)
+   ++chunks;
+
+   ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
+   if (ret) {
+   kfree(sgt);
+   return ERR_PTR(-ENOMEM);
+   }
+
+   /* merging chunks and putting them into the scatterlist */
+   cur_page = 0;
+   for_each_sg(sgt-sgl, s, sgt-orig_nents, i) {
+   unsigned long chunk_size;
+   unsigned int j;


size = PAGE_SIZE;


+
+   for (j = cur_page + 1; j   n_pages; ++j)


for (j = cur_page + 1; j  n_pages; ++j) {


+   if (pages[j] != pages[j - 1] + 1)
+   break;


size += PAGE
}


+
+   chunk_size = ((j - cur_page)   PAGE_SHIFT) - offset;
+   sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);


[DELETE] size -= chunk_size;


+   offset = 0;
+   cur_page = j;
+   }
+
+   return sgt;
+}



Regards,
Subash
___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel


Re: [PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-09 Thread Subash Patel

Hello Tomasz, Laurent,

I have printed some logs during the dmabuf export and attach for the SGT 
issue below. Please find it in the attachment. I hope it will be useful.


Regards,
Subash

On 05/08/2012 04:45 PM, Subash Patel wrote:

Hi Laurent,

On 05/08/2012 02:44 PM, Laurent Pinchart wrote:

Hi Subash,

On Monday 07 May 2012 20:08:25 Subash Patel wrote:

Hello Thomasz, Laurent,

I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
during the attach, the size of the SGT and size requested mis-matched
(by atleast 8k bytes). Hence I made a small correction to the code as
below. I could then attach the importer properly.


Thank you for the report.

Could you print the content of the sglist (number of chunks and size
of each
chunk) before and after your modifications, as well as the values of
n_pages,
offset and size ?

I will put back all the printk's and generate this. As of now, my setup
has changed and will do this when I get sometime.



On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:


[snip]


+static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
+ unsigned int n_pages, unsigned long offset, unsigned long size)
+{
+ struct sg_table *sgt;
+ unsigned int chunks;
+ unsigned int i;
+ unsigned int cur_page;
+ int ret;
+ struct scatterlist *s;
+
+ sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
+ if (!sgt)
+ return ERR_PTR(-ENOMEM);
+
+ /* compute number of chunks */
+ chunks = 1;
+ for (i = 1; i n_pages; ++i)
+ if (pages[i] != pages[i - 1] + 1)
+ ++chunks;
+
+ ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
+ if (ret) {
+ kfree(sgt);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ /* merging chunks and putting them into the scatterlist */
+ cur_page = 0;
+ for_each_sg(sgt-sgl, s, sgt-orig_nents, i) {
+ unsigned long chunk_size;
+ unsigned int j;


size = PAGE_SIZE;


+
+ for (j = cur_page + 1; j n_pages; ++j)


for (j = cur_page + 1; j n_pages; ++j) {


+ if (pages[j] != pages[j - 1] + 1)
+ break;


size += PAGE
}


+
+ chunk_size = ((j - cur_page) PAGE_SHIFT) - offset;
+ sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);


[DELETE] size -= chunk_size;


+ offset = 0;
+ cur_page = j;
+ }
+
+ return sgt;
+}



Regards,
Subash
[  178.545000] vb2_dc_pages_to_sgt() sgt-orig_nents=2
[  178.545000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.55] vb2_dc_pages_to_sgt():84 offset=0
[  178.555000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.56] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.565000] vb2_dc_pages_to_sgt() sgt-orig_nents=2
[  178.57] vb2_dc_pages_to_sgt():83 cur_page=32
[  178.575000] vb2_dc_pages_to_sgt():84 offset=0
[  178.58] vb2_dc_pages_to_sgt():86 chunk_size=262144
[  178.585000] vb2_dc_pages_to_sgt():89 size=4294574080
[  178.59] vb2_dc_pages_to_sgt() sgt-orig_nents=1
[  178.595000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.60] vb2_dc_pages_to_sgt():84 offset=0
[  178.605000] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.61] vb2_dc_pages_to_sgt():89 size=4294959104
[  178.625000] vb2_dc_pages_to_sgt() sgt-orig_nents=1
[  178.625000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.63] vb2_dc_pages_to_sgt():84 offset=0
[  178.635000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.64] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.645000] vb2_dc_pages_to_sgt() sgt-orig_nents=1
[  178.65] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.655000] vb2_dc_pages_to_sgt():84 offset=0
[  178.66] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.665000] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.67] vb2_dc_mmap: mapped dma addr 0x2006 at 0xb6e01000, size 
131072
[  178.67] vb2_dc_mmap: mapped dma addr 0x2008 at 0xb6de1000, size 
131072
[  178.68] vb2_dc_pages_to_sgt() sgt-orig_nents=2
[  178.685000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.69] vb2_dc_pages_to_sgt():84 offset=0
[  178.695000] vb2_dc_pages_to_sgt():86 chunk_size=4096
[  178.70] vb2_dc_pages_to_sgt():89 size=4294963200
[  178.705000] vb2_dc_pages_to_sgt() sgt-orig_nents=2
[  178.71] vb2_dc_pages_to_sgt():83 cur_page=1
[  178.715000] vb2_dc_pages_to_sgt():84 offset=0
[  178.715000] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.72] vb2_dc_pages_to_sgt():89 size=4294955008
[  178.725000] vb2_dc_pages_to_sgt() sgt-orig_nents=1
[  178.73] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.735000] vb2_dc_pages_to_sgt():84 offset=0
[  178.74] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.745000] vb2_dc_pages_to_sgt():89 size=4294959104
[  178.75] vb2_dc_pages_to_sgt() sgt-orig_nents=1
[  178.755000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.76] vb2_dc_pages_to_sgt():84 offset=0
[  178.765000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.77] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.78] vb2_dc_pages_to_sgt() sgt-orig_nents=2
[  178.78] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.785000] vb2_dc_pages_to_sgt():84 offset=0
[  178.79] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  178.795000] 

Re: [PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-09 Thread Tomasz Stanislawski
Hi Subash,
Could you post the code of vb2_dc_pages_to_sgt with all printk in it.
It will help us avoid guessing where and what is debugged in the log.

Moreover, I found a line 'size=4294836224' in the log.
It means that size is equal to -131072 (!?!) or there are some invalid
conversions in printk.

Are you suze that you do not pass size = 0 as the function argument?

Notice that earlier versions of dmabuf-for-vb2 patches has offset2
argument instead of size. It was the offset at the end of the buffer.
In (I guess) 95% of cases this offset was 0.

Could you provide only function arguments that causes the failure?
I mean pages array + size (I assume that offset is zero for your test).

Having the arguments we could reproduce that bug.

Regards,
Tomasz Stanislawski





On 05/09/2012 08:46 AM, Subash Patel wrote:
 Hello Tomasz, Laurent,
 
 I have printed some logs during the dmabuf export and attach for the SGT 
 issue below. Please find it in the attachment. I hope
 it will be useful.
 
 Regards,
 Subash
 
 On 05/08/2012 04:45 PM, Subash Patel wrote:
 Hi Laurent,

 On 05/08/2012 02:44 PM, Laurent Pinchart wrote:
 Hi Subash,

 On Monday 07 May 2012 20:08:25 Subash Patel wrote:
 Hello Thomasz, Laurent,

 I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
 during the attach, the size of the SGT and size requested mis-matched
 (by atleast 8k bytes). Hence I made a small correction to the code as
 below. I could then attach the importer properly.

 Thank you for the report.

 Could you print the content of the sglist (number of chunks and size
 of each
 chunk) before and after your modifications, as well as the values of
 n_pages,
 offset and size ?
 I will put back all the printk's and generate this. As of now, my setup
 has changed and will do this when I get sometime.

 On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:

 [snip]

 +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
 + unsigned int n_pages, unsigned long offset, unsigned long size)
 +{
 + struct sg_table *sgt;
 + unsigned int chunks;
 + unsigned int i;
 + unsigned int cur_page;
 + int ret;
 + struct scatterlist *s;
 +
 + sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
 + if (!sgt)
 + return ERR_PTR(-ENOMEM);
 +
 + /* compute number of chunks */
 + chunks = 1;
 + for (i = 1; i n_pages; ++i)
 + if (pages[i] != pages[i - 1] + 1)
 + ++chunks;
 +
 + ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
 + if (ret) {
 + kfree(sgt);
 + return ERR_PTR(-ENOMEM);
 + }
 +
 + /* merging chunks and putting them into the scatterlist */
 + cur_page = 0;
 + for_each_sg(sgt-sgl, s, sgt-orig_nents, i) {
 + unsigned long chunk_size;
 + unsigned int j;

 size = PAGE_SIZE;

 +
 + for (j = cur_page + 1; j n_pages; ++j)

 for (j = cur_page + 1; j n_pages; ++j) {

 + if (pages[j] != pages[j - 1] + 1)
 + break;

 size += PAGE
 }

 +
 + chunk_size = ((j - cur_page) PAGE_SHIFT) - offset;
 + sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);

 [DELETE] size -= chunk_size;

 + offset = 0;
 + cur_page = j;
 + }
 +
 + return sgt;
 +}

 Regards,
 Subash

___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel


[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-08 Thread Laurent Pinchart
Hi Subash,

On Monday 07 May 2012 20:08:25 Subash Patel wrote:
> Hello Thomasz, Laurent,
> 
> I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
> during the attach, the size of the SGT and size requested mis-matched
> (by atleast 8k bytes). Hence I made a small correction to the code as
> below. I could then attach the importer properly.

Thank you for the report.

Could you print the content of the sglist (number of chunks and size of each 
chunk) before and after your modifications, as well as the values of n_pages, 
offset and size ?

> On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:

[snip]

> > +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
> > +   unsigned int n_pages, unsigned long offset, unsigned long size)
> > +{
> > +   struct sg_table *sgt;
> > +   unsigned int chunks;
> > +   unsigned int i;
> > +   unsigned int cur_page;
> > +   int ret;
> > +   struct scatterlist *s;
> > +
> > +   sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
> > +   if (!sgt)
> > +   return ERR_PTR(-ENOMEM);
> > +
> > +   /* compute number of chunks */
> > +   chunks = 1;
> > +   for (i = 1; i<  n_pages; ++i)
> > +   if (pages[i] != pages[i - 1] + 1)
> > +   ++chunks;
> > +
> > +   ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
> > +   if (ret) {
> > +   kfree(sgt);
> > +   return ERR_PTR(-ENOMEM);
> > +   }
> > +
> > +   /* merging chunks and putting them into the scatterlist */
> > +   cur_page = 0;
> > +   for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
> > +   unsigned long chunk_size;
> > +   unsigned int j;
> 
>   size = PAGE_SIZE;
> 
> > +
> > +   for (j = cur_page + 1; j<  n_pages; ++j)
> 
>   for (j = cur_page + 1; j < n_pages; ++j) {
> 
> > +   if (pages[j] != pages[j - 1] + 1)
> > +   break;
> 
>   size += PAGE
>   }
> 
> > +
> > +   chunk_size = ((j - cur_page)<<  PAGE_SHIFT) - offset;
> > +   sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
> 
>   [DELETE] size -= chunk_size;
> 
> > +   offset = 0;
> > +   cur_page = j;
> > +   }
> > +
> > +   return sgt;
> > +}

-- 
Regards,

Laurent Pinchart



[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-08 Thread Subash Patel
Hi Thomasz,

I have extended the MFC-FIMC testcase posted by Kamil sometime ago to 
sanity test the UMM patches. This test is multi-threaded(further 
explanation for developers who may not have seen it yet), where thread 
one parses the encoded stream and feeds into the codec  IP driver(aka 
MFC driver). Second thread will dequeue the buffer from MFC driver 
(DMA_BUF export) and queues it into a CSC IP(aka FIMC) driver(DMA_BUF 
import). Third thread dequeues the frame from FIMC driver and either 
pushes it into LCD driver for display or dumps to a flat file for analysis.

MFC driver exports the fd's and FIMC driver imports and attaches it. 
During FIMC QBUF (thats when the attach and map happens), it is observed 
that in the function vb2_dc_map_dmabuf() call to 
vb2_dc_get_contiguous_size() fails. This is because contig_size < 
buf->size. contig_size is calculated from the SGT which we would have 
constructed in the function vb2_dc_pages_to_sgt().

Let me know if you need more details.

Regards,
Subash

On 05/07/2012 08:41 PM, Tomasz Stanislawski wrote:
> Hi Subash,
> Could you provide a detailed description of a test case
> that causes a failure of vb2_dc_pages_to_sgt?
>
> Regards,
> Tomasz Stanislawski


Re: [PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-08 Thread Subash Patel

Hello Thomasz, Laurent,

I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that 
during the attach, the size of the SGT and size requested mis-matched 
(by atleast 8k bytes). Hence I made a small correction to the code as 
below. I could then attach the importer properly.


Regards,
Subash

On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:

From: Andrzej Pietrasiewiczandrze...@samsung.com

This patch introduces usage of dma_map_sg to map memory behind
a userspace pointer to a device as dma-contiguous mapping.

Signed-off-by: Andrzej Pietrasiewiczandrze...@samsung.com
Signed-off-by: Marek Szyprowskim.szyprow...@samsung.com
[bugfixing]
Signed-off-by: Kamil Debskik.deb...@samsung.com
[bugfixing]
Signed-off-by: Tomasz Stanislawskit.stanisl...@samsung.com
[add sglist subroutines/code refactoring]
Signed-off-by: Kyungmin Parkkyungmin.p...@samsung.com
---
  drivers/media/video/videobuf2-dma-contig.c |  279 ++--
  1 files changed, 262 insertions(+), 17 deletions(-)

diff --git a/drivers/media/video/videobuf2-dma-contig.c 
b/drivers/media/video/videobuf2-dma-contig.c
index 476e536..9cbc8d4 100644
--- a/drivers/media/video/videobuf2-dma-contig.c
+++ b/drivers/media/video/videobuf2-dma-contig.c
@@ -11,6 +11,8 @@
   */

  #includelinux/module.h
+#includelinux/scatterlist.h
+#includelinux/sched.h
  #includelinux/slab.h
  #includelinux/dma-mapping.h

@@ -22,6 +24,8 @@ struct vb2_dc_buf {
void*vaddr;
unsigned long   size;
dma_addr_t  dma_addr;
+   enum dma_data_direction dma_dir;
+   struct sg_table *dma_sgt;

/* MMAP related */
struct vb2_vmarea_handler   handler;
@@ -32,6 +36,95 @@ struct vb2_dc_buf {
  };

  /*/
+/*scatterlist table functions*/
+/*/
+
+static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
+   unsigned int n_pages, unsigned long offset, unsigned long size)
+{
+   struct sg_table *sgt;
+   unsigned int chunks;
+   unsigned int i;
+   unsigned int cur_page;
+   int ret;
+   struct scatterlist *s;
+
+   sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
+   if (!sgt)
+   return ERR_PTR(-ENOMEM);
+
+   /* compute number of chunks */
+   chunks = 1;
+   for (i = 1; i  n_pages; ++i)
+   if (pages[i] != pages[i - 1] + 1)
+   ++chunks;
+
+   ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
+   if (ret) {
+   kfree(sgt);
+   return ERR_PTR(-ENOMEM);
+   }
+
+   /* merging chunks and putting them into the scatterlist */
+   cur_page = 0;
+   for_each_sg(sgt-sgl, s, sgt-orig_nents, i) {
+   unsigned long chunk_size;
+   unsigned int j;

size = PAGE_SIZE;


+
+   for (j = cur_page + 1; j  n_pages; ++j)

for (j = cur_page + 1; j  n_pages; ++j) {

+   if (pages[j] != pages[j - 1] + 1)
+   break;

size += PAGE
}

+
+   chunk_size = ((j - cur_page)  PAGE_SHIFT) - offset;
+   sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);

[DELETE] size -= chunk_size;

+   offset = 0;
+   cur_page = j;
+   }
+
+   return sgt;
+}
+
+static void vb2_dc_release_sgtable(struct sg_table *sgt)
+{
+   sg_free_table(sgt);
+   kfree(sgt);
+}
+
+static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
+   void (*cb)(struct page *pg))
+{
+   struct scatterlist *s;
+   unsigned int i;
+
+   for_each_sg(sgt-sgl, s, sgt-nents, i) {
+   struct page *page = sg_page(s);
+   unsigned int n_pages = PAGE_ALIGN(s-offset + s-length)
+ PAGE_SHIFT;
+   unsigned int j;
+
+   for (j = 0; j  n_pages; ++j, ++page)
+   cb(page);
+   }
+}
+
+static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
+{
+   struct scatterlist *s;
+   dma_addr_t expected = sg_dma_address(sgt-sgl);
+   unsigned int i;
+   unsigned long size = 0;
+
+   for_each_sg(sgt-sgl, s, sgt-nents, i) {
+   if (sg_dma_address(s) != expected)
+   break;
+   expected = sg_dma_address(s) + sg_dma_len(s);
+   size += sg_dma_len(s);
+   }
+   return size;
+}
+
+/*/
  /* callbacks for all buffers */
  /*/

@@ -116,42 +209,194 @@ static int vb2_dc_mmap(void *buf_priv, struct 
vm_area_struct *vma)
  /*   callbacks for USERPTR buffers   */
  /*/

+static 

Re: [PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-08 Thread Subash Patel

Hi Thomasz,

I have extended the MFC-FIMC testcase posted by Kamil sometime ago to 
sanity test the UMM patches. This test is multi-threaded(further 
explanation for developers who may not have seen it yet), where thread 
one parses the encoded stream and feeds into the codec  IP driver(aka 
MFC driver). Second thread will dequeue the buffer from MFC driver 
(DMA_BUF export) and queues it into a CSC IP(aka FIMC) driver(DMA_BUF 
import). Third thread dequeues the frame from FIMC driver and either 
pushes it into LCD driver for display or dumps to a flat file for analysis.


MFC driver exports the fd's and FIMC driver imports and attaches it. 
During FIMC QBUF (thats when the attach and map happens), it is observed 
that in the function vb2_dc_map_dmabuf() call to 
vb2_dc_get_contiguous_size() fails. This is because contig_size  
buf-size. contig_size is calculated from the SGT which we would have 
constructed in the function vb2_dc_pages_to_sgt().


Let me know if you need more details.

Regards,
Subash

On 05/07/2012 08:41 PM, Tomasz Stanislawski wrote:

Hi Subash,
Could you provide a detailed description of a test case
that causes a failure of vb2_dc_pages_to_sgt?

Regards,
Tomasz Stanislawski

___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel


Re: [PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-08 Thread Laurent Pinchart
Hi Subash,

On Monday 07 May 2012 20:08:25 Subash Patel wrote:
 Hello Thomasz, Laurent,
 
 I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
 during the attach, the size of the SGT and size requested mis-matched
 (by atleast 8k bytes). Hence I made a small correction to the code as
 below. I could then attach the importer properly.

Thank you for the report.

Could you print the content of the sglist (number of chunks and size of each 
chunk) before and after your modifications, as well as the values of n_pages, 
offset and size ?

 On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:

[snip]

  +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
  +   unsigned int n_pages, unsigned long offset, unsigned long size)
  +{
  +   struct sg_table *sgt;
  +   unsigned int chunks;
  +   unsigned int i;
  +   unsigned int cur_page;
  +   int ret;
  +   struct scatterlist *s;
  +
  +   sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
  +   if (!sgt)
  +   return ERR_PTR(-ENOMEM);
  +
  +   /* compute number of chunks */
  +   chunks = 1;
  +   for (i = 1; i  n_pages; ++i)
  +   if (pages[i] != pages[i - 1] + 1)
  +   ++chunks;
  +
  +   ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
  +   if (ret) {
  +   kfree(sgt);
  +   return ERR_PTR(-ENOMEM);
  +   }
  +
  +   /* merging chunks and putting them into the scatterlist */
  +   cur_page = 0;
  +   for_each_sg(sgt-sgl, s, sgt-orig_nents, i) {
  +   unsigned long chunk_size;
  +   unsigned int j;
 
   size = PAGE_SIZE;
 
  +
  +   for (j = cur_page + 1; j  n_pages; ++j)
 
   for (j = cur_page + 1; j  n_pages; ++j) {
 
  +   if (pages[j] != pages[j - 1] + 1)
  +   break;
 
   size += PAGE
   }
 
  +
  +   chunk_size = ((j - cur_page)  PAGE_SHIFT) - offset;
  +   sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
 
   [DELETE] size -= chunk_size;
 
  +   offset = 0;
  +   cur_page = j;
  +   }
  +
  +   return sgt;
  +}

-- 
Regards,

Laurent Pinchart

___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel


[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-07 Thread Subash Patel
Hello Thomasz, Laurent,

I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that 
during the attach, the size of the SGT and size requested mis-matched 
(by atleast 8k bytes). Hence I made a small correction to the code as 
below. I could then attach the importer properly.

Regards,
Subash

On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:
> From: Andrzej Pietrasiewicz
>
> This patch introduces usage of dma_map_sg to map memory behind
> a userspace pointer to a device as dma-contiguous mapping.
>
> Signed-off-by: Andrzej Pietrasiewicz
> Signed-off-by: Marek Szyprowski
>   [bugfixing]
> Signed-off-by: Kamil Debski
>   [bugfixing]
> Signed-off-by: Tomasz Stanislawski
>   [add sglist subroutines/code refactoring]
> Signed-off-by: Kyungmin Park
> ---
>   drivers/media/video/videobuf2-dma-contig.c |  279 
> ++--
>   1 files changed, 262 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/media/video/videobuf2-dma-contig.c 
> b/drivers/media/video/videobuf2-dma-contig.c
> index 476e536..9cbc8d4 100644
> --- a/drivers/media/video/videobuf2-dma-contig.c
> +++ b/drivers/media/video/videobuf2-dma-contig.c
> @@ -11,6 +11,8 @@
>*/
>
>   #include
> +#include
> +#include
>   #include
>   #include
>
> @@ -22,6 +24,8 @@ struct vb2_dc_buf {
>   void*vaddr;
>   unsigned long   size;
>   dma_addr_t  dma_addr;
> + enum dma_data_direction dma_dir;
> + struct sg_table *dma_sgt;
>
>   /* MMAP related */
>   struct vb2_vmarea_handler   handler;
> @@ -32,6 +36,95 @@ struct vb2_dc_buf {
>   };
>
>   /*/
> +/*scatterlist table functions*/
> +/*/
> +
> +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
> + unsigned int n_pages, unsigned long offset, unsigned long size)
> +{
> + struct sg_table *sgt;
> + unsigned int chunks;
> + unsigned int i;
> + unsigned int cur_page;
> + int ret;
> + struct scatterlist *s;
> +
> + sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
> + if (!sgt)
> + return ERR_PTR(-ENOMEM);
> +
> + /* compute number of chunks */
> + chunks = 1;
> + for (i = 1; i<  n_pages; ++i)
> + if (pages[i] != pages[i - 1] + 1)
> + ++chunks;
> +
> + ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
> + if (ret) {
> + kfree(sgt);
> + return ERR_PTR(-ENOMEM);
> + }
> +
> + /* merging chunks and putting them into the scatterlist */
> + cur_page = 0;
> + for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
> + unsigned long chunk_size;
> + unsigned int j;
size = PAGE_SIZE;

> +
> + for (j = cur_page + 1; j<  n_pages; ++j)
for (j = cur_page + 1; j < n_pages; ++j) {
> + if (pages[j] != pages[j - 1] + 1)
> + break;
size += PAGE
}
> +
> + chunk_size = ((j - cur_page)<<  PAGE_SHIFT) - offset;
> + sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
[DELETE] size -= chunk_size;
> + offset = 0;
> + cur_page = j;
> + }
> +
> + return sgt;
> +}
> +
> +static void vb2_dc_release_sgtable(struct sg_table *sgt)
> +{
> + sg_free_table(sgt);
> + kfree(sgt);
> +}
> +
> +static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
> + void (*cb)(struct page *pg))
> +{
> + struct scatterlist *s;
> + unsigned int i;
> +
> + for_each_sg(sgt->sgl, s, sgt->nents, i) {
> + struct page *page = sg_page(s);
> + unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
> + >>  PAGE_SHIFT;
> + unsigned int j;
> +
> + for (j = 0; j<  n_pages; ++j, ++page)
> + cb(page);
> + }
> +}
> +
> +static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
> +{
> + struct scatterlist *s;
> + dma_addr_t expected = sg_dma_address(sgt->sgl);
> + unsigned int i;
> + unsigned long size = 0;
> +
> + for_each_sg(sgt->sgl, s, sgt->nents, i) {
> + if (sg_dma_address(s) != expected)
> + break;
> + expected = sg_dma_address(s) + sg_dma_len(s);
> + size += sg_dma_len(s);
> + }
> + return size;
> +}
> +
> +/*/
>   /* callbacks for all buffers */
>   /*/
>
> @@ -116,42 +209,194 @@ static int vb2_dc_mmap(void *buf_priv, struct 
> vm_area_struct *vma)
>   /*   callbacks for USERPTR buffers   */
>   /*/
>
> +static inline int vma_is_io(struct vm_area_struct *vma)
> +{
> + return 

[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-07 Thread Tomasz Stanislawski
Hi Subash,
Could you provide a detailed description of a test case
that causes a failure of vb2_dc_pages_to_sgt?

Regards,
Tomasz Stanislawski


Re: [PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-05-07 Thread Tomasz Stanislawski
Hi Subash,
Could you provide a detailed description of a test case
that causes a failure of vb2_dc_pages_to_sgt?

Regards,
Tomasz Stanislawski
___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel


[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-04-21 Thread Laurent Pinchart
Hi Tomasz,

Thanks for the patch.

On Friday 20 April 2012 16:45:29 Tomasz Stanislawski wrote:
> From: Andrzej Pietrasiewicz 
> 
> This patch introduces usage of dma_map_sg to map memory behind
> a userspace pointer to a device as dma-contiguous mapping.
> 
> Signed-off-by: Andrzej Pietrasiewicz 
> Signed-off-by: Marek Szyprowski 
>   [bugfixing]
> Signed-off-by: Kamil Debski 
>   [bugfixing]
> Signed-off-by: Tomasz Stanislawski 
>   [add sglist subroutines/code refactoring]
> Signed-off-by: Kyungmin Park 

Acked-by: Laurent Pinchart 

-- 
Regards,

Laurent Pinchart



Re: [PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-04-21 Thread Laurent Pinchart
Hi Tomasz,

Thanks for the patch.

On Friday 20 April 2012 16:45:29 Tomasz Stanislawski wrote:
 From: Andrzej Pietrasiewicz andrze...@samsung.com
 
 This patch introduces usage of dma_map_sg to map memory behind
 a userspace pointer to a device as dma-contiguous mapping.
 
 Signed-off-by: Andrzej Pietrasiewicz andrze...@samsung.com
 Signed-off-by: Marek Szyprowski m.szyprow...@samsung.com
   [bugfixing]
 Signed-off-by: Kamil Debski k.deb...@samsung.com
   [bugfixing]
 Signed-off-by: Tomasz Stanislawski t.stanisl...@samsung.com
   [add sglist subroutines/code refactoring]
 Signed-off-by: Kyungmin Park kyungmin.p...@samsung.com

Acked-by: Laurent Pinchart laurent.pinch...@ideasonboard.com

-- 
Regards,

Laurent Pinchart

___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel


[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-04-20 Thread Tomasz Stanislawski
From: Andrzej Pietrasiewicz 

This patch introduces usage of dma_map_sg to map memory behind
a userspace pointer to a device as dma-contiguous mapping.

Signed-off-by: Andrzej Pietrasiewicz 
Signed-off-by: Marek Szyprowski 
[bugfixing]
Signed-off-by: Kamil Debski 
[bugfixing]
Signed-off-by: Tomasz Stanislawski 
[add sglist subroutines/code refactoring]
Signed-off-by: Kyungmin Park 
---
 drivers/media/video/videobuf2-dma-contig.c |  279 ++--
 1 files changed, 262 insertions(+), 17 deletions(-)

diff --git a/drivers/media/video/videobuf2-dma-contig.c 
b/drivers/media/video/videobuf2-dma-contig.c
index 476e536..9cbc8d4 100644
--- a/drivers/media/video/videobuf2-dma-contig.c
+++ b/drivers/media/video/videobuf2-dma-contig.c
@@ -11,6 +11,8 @@
  */

 #include 
+#include 
+#include 
 #include 
 #include 

@@ -22,6 +24,8 @@ struct vb2_dc_buf {
void*vaddr;
unsigned long   size;
dma_addr_t  dma_addr;
+   enum dma_data_direction dma_dir;
+   struct sg_table *dma_sgt;

/* MMAP related */
struct vb2_vmarea_handler   handler;
@@ -32,6 +36,95 @@ struct vb2_dc_buf {
 };

 /*/
+/*scatterlist table functions*/
+/*/
+
+static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
+   unsigned int n_pages, unsigned long offset, unsigned long size)
+{
+   struct sg_table *sgt;
+   unsigned int chunks;
+   unsigned int i;
+   unsigned int cur_page;
+   int ret;
+   struct scatterlist *s;
+
+   sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
+   if (!sgt)
+   return ERR_PTR(-ENOMEM);
+
+   /* compute number of chunks */
+   chunks = 1;
+   for (i = 1; i < n_pages; ++i)
+   if (pages[i] != pages[i - 1] + 1)
+   ++chunks;
+
+   ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
+   if (ret) {
+   kfree(sgt);
+   return ERR_PTR(-ENOMEM);
+   }
+
+   /* merging chunks and putting them into the scatterlist */
+   cur_page = 0;
+   for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
+   unsigned long chunk_size;
+   unsigned int j;
+
+   for (j = cur_page + 1; j < n_pages; ++j)
+   if (pages[j] != pages[j - 1] + 1)
+   break;
+
+   chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
+   sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
+   size -= chunk_size;
+   offset = 0;
+   cur_page = j;
+   }
+
+   return sgt;
+}
+
+static void vb2_dc_release_sgtable(struct sg_table *sgt)
+{
+   sg_free_table(sgt);
+   kfree(sgt);
+}
+
+static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
+   void (*cb)(struct page *pg))
+{
+   struct scatterlist *s;
+   unsigned int i;
+
+   for_each_sg(sgt->sgl, s, sgt->nents, i) {
+   struct page *page = sg_page(s);
+   unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
+   >> PAGE_SHIFT;
+   unsigned int j;
+
+   for (j = 0; j < n_pages; ++j, ++page)
+   cb(page);
+   }
+}
+
+static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
+{
+   struct scatterlist *s;
+   dma_addr_t expected = sg_dma_address(sgt->sgl);
+   unsigned int i;
+   unsigned long size = 0;
+
+   for_each_sg(sgt->sgl, s, sgt->nents, i) {
+   if (sg_dma_address(s) != expected)
+   break;
+   expected = sg_dma_address(s) + sg_dma_len(s);
+   size += sg_dma_len(s);
+   }
+   return size;
+}
+
+/*/
 /* callbacks for all buffers */
 /*/

@@ -116,42 +209,194 @@ static int vb2_dc_mmap(void *buf_priv, struct 
vm_area_struct *vma)
 /*   callbacks for USERPTR buffers   */
 /*/

+static inline int vma_is_io(struct vm_area_struct *vma)
+{
+   return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
+}
+
+static struct vm_area_struct *vb2_dc_get_user_vma(
+   unsigned long start, unsigned long size)
+{
+   struct vm_area_struct *vma;
+
+   /* current->mm->mmap_sem is taken by videobuf2 core */
+   vma = find_vma(current->mm, start);
+   if (!vma) {
+   printk(KERN_ERR "no vma for address %lu\n", start);
+   return ERR_PTR(-EFAULT);
+   }
+
+   if (vma->vm_end - vma->vm_start < size) {
+   printk(KERN_ERR "vma at %lu is too small for %lu bytes\n",
+   start, size);
+   return ERR_PTR(-EFAULT);

[PATCHv5 08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

2012-04-20 Thread Tomasz Stanislawski
From: Andrzej Pietrasiewicz andrze...@samsung.com

This patch introduces usage of dma_map_sg to map memory behind
a userspace pointer to a device as dma-contiguous mapping.

Signed-off-by: Andrzej Pietrasiewicz andrze...@samsung.com
Signed-off-by: Marek Szyprowski m.szyprow...@samsung.com
[bugfixing]
Signed-off-by: Kamil Debski k.deb...@samsung.com
[bugfixing]
Signed-off-by: Tomasz Stanislawski t.stanisl...@samsung.com
[add sglist subroutines/code refactoring]
Signed-off-by: Kyungmin Park kyungmin.p...@samsung.com
---
 drivers/media/video/videobuf2-dma-contig.c |  279 ++--
 1 files changed, 262 insertions(+), 17 deletions(-)

diff --git a/drivers/media/video/videobuf2-dma-contig.c 
b/drivers/media/video/videobuf2-dma-contig.c
index 476e536..9cbc8d4 100644
--- a/drivers/media/video/videobuf2-dma-contig.c
+++ b/drivers/media/video/videobuf2-dma-contig.c
@@ -11,6 +11,8 @@
  */
 
 #include linux/module.h
+#include linux/scatterlist.h
+#include linux/sched.h
 #include linux/slab.h
 #include linux/dma-mapping.h
 
@@ -22,6 +24,8 @@ struct vb2_dc_buf {
void*vaddr;
unsigned long   size;
dma_addr_t  dma_addr;
+   enum dma_data_direction dma_dir;
+   struct sg_table *dma_sgt;
 
/* MMAP related */
struct vb2_vmarea_handler   handler;
@@ -32,6 +36,95 @@ struct vb2_dc_buf {
 };
 
 /*/
+/*scatterlist table functions*/
+/*/
+
+static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
+   unsigned int n_pages, unsigned long offset, unsigned long size)
+{
+   struct sg_table *sgt;
+   unsigned int chunks;
+   unsigned int i;
+   unsigned int cur_page;
+   int ret;
+   struct scatterlist *s;
+
+   sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
+   if (!sgt)
+   return ERR_PTR(-ENOMEM);
+
+   /* compute number of chunks */
+   chunks = 1;
+   for (i = 1; i  n_pages; ++i)
+   if (pages[i] != pages[i - 1] + 1)
+   ++chunks;
+
+   ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
+   if (ret) {
+   kfree(sgt);
+   return ERR_PTR(-ENOMEM);
+   }
+
+   /* merging chunks and putting them into the scatterlist */
+   cur_page = 0;
+   for_each_sg(sgt-sgl, s, sgt-orig_nents, i) {
+   unsigned long chunk_size;
+   unsigned int j;
+
+   for (j = cur_page + 1; j  n_pages; ++j)
+   if (pages[j] != pages[j - 1] + 1)
+   break;
+
+   chunk_size = ((j - cur_page)  PAGE_SHIFT) - offset;
+   sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
+   size -= chunk_size;
+   offset = 0;
+   cur_page = j;
+   }
+
+   return sgt;
+}
+
+static void vb2_dc_release_sgtable(struct sg_table *sgt)
+{
+   sg_free_table(sgt);
+   kfree(sgt);
+}
+
+static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
+   void (*cb)(struct page *pg))
+{
+   struct scatterlist *s;
+   unsigned int i;
+
+   for_each_sg(sgt-sgl, s, sgt-nents, i) {
+   struct page *page = sg_page(s);
+   unsigned int n_pages = PAGE_ALIGN(s-offset + s-length)
+PAGE_SHIFT;
+   unsigned int j;
+
+   for (j = 0; j  n_pages; ++j, ++page)
+   cb(page);
+   }
+}
+
+static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
+{
+   struct scatterlist *s;
+   dma_addr_t expected = sg_dma_address(sgt-sgl);
+   unsigned int i;
+   unsigned long size = 0;
+
+   for_each_sg(sgt-sgl, s, sgt-nents, i) {
+   if (sg_dma_address(s) != expected)
+   break;
+   expected = sg_dma_address(s) + sg_dma_len(s);
+   size += sg_dma_len(s);
+   }
+   return size;
+}
+
+/*/
 /* callbacks for all buffers */
 /*/
 
@@ -116,42 +209,194 @@ static int vb2_dc_mmap(void *buf_priv, struct 
vm_area_struct *vma)
 /*   callbacks for USERPTR buffers   */
 /*/
 
+static inline int vma_is_io(struct vm_area_struct *vma)
+{
+   return !!(vma-vm_flags  (VM_IO | VM_PFNMAP));
+}
+
+static struct vm_area_struct *vb2_dc_get_user_vma(
+   unsigned long start, unsigned long size)
+{
+   struct vm_area_struct *vma;
+
+   /* current-mm-mmap_sem is taken by videobuf2 core */
+   vma = find_vma(current-mm, start);
+   if (!vma) {
+   printk(KERN_ERR no vma for address %lu\n, start);
+   return ERR_PTR(-EFAULT);
+   }
+
+   if (vma-vm_end -