dma-mapping: use unsigned long for dma_attrs
[linux-2.6-block.git] / drivers / media / v4l2-core / videobuf2-vmalloc.c
1 /*
2  * videobuf2-vmalloc.c - vmalloc memory allocator for videobuf2
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  *
6  * Author: Pawel Osciak <pawel@osciak.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation.
11  */
12
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19
20 #include <media/videobuf2-v4l2.h>
21 #include <media/videobuf2-vmalloc.h>
22 #include <media/videobuf2-memops.h>
23
24 struct vb2_vmalloc_buf {
25         void                            *vaddr;
26         struct frame_vector             *vec;
27         enum dma_data_direction         dma_dir;
28         unsigned long                   size;
29         atomic_t                        refcount;
30         struct vb2_vmarea_handler       handler;
31         struct dma_buf                  *dbuf;
32 };
33
34 static void vb2_vmalloc_put(void *buf_priv);
35
36 static void *vb2_vmalloc_alloc(struct device *dev, unsigned long attrs,
37                                unsigned long size, enum dma_data_direction dma_dir,
38                                gfp_t gfp_flags)
39 {
40         struct vb2_vmalloc_buf *buf;
41
42         buf = kzalloc(sizeof(*buf), GFP_KERNEL | gfp_flags);
43         if (!buf)
44                 return NULL;
45
46         buf->size = size;
47         buf->vaddr = vmalloc_user(buf->size);
48         buf->dma_dir = dma_dir;
49         buf->handler.refcount = &buf->refcount;
50         buf->handler.put = vb2_vmalloc_put;
51         buf->handler.arg = buf;
52
53         if (!buf->vaddr) {
54                 pr_debug("vmalloc of size %ld failed\n", buf->size);
55                 kfree(buf);
56                 return NULL;
57         }
58
59         atomic_inc(&buf->refcount);
60         return buf;
61 }
62
63 static void vb2_vmalloc_put(void *buf_priv)
64 {
65         struct vb2_vmalloc_buf *buf = buf_priv;
66
67         if (atomic_dec_and_test(&buf->refcount)) {
68                 vfree(buf->vaddr);
69                 kfree(buf);
70         }
71 }
72
73 static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr,
74                                      unsigned long size,
75                                      enum dma_data_direction dma_dir)
76 {
77         struct vb2_vmalloc_buf *buf;
78         struct frame_vector *vec;
79         int n_pages, offset, i;
80
81         buf = kzalloc(sizeof(*buf), GFP_KERNEL);
82         if (!buf)
83                 return NULL;
84
85         buf->dma_dir = dma_dir;
86         offset = vaddr & ~PAGE_MASK;
87         buf->size = size;
88         vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
89         if (IS_ERR(vec))
90                 goto fail_pfnvec_create;
91         buf->vec = vec;
92         n_pages = frame_vector_count(vec);
93         if (frame_vector_to_pages(vec) < 0) {
94                 unsigned long *nums = frame_vector_pfns(vec);
95
96                 /*
97                  * We cannot get page pointers for these pfns. Check memory is
98                  * physically contiguous and use direct mapping.
99                  */
100                 for (i = 1; i < n_pages; i++)
101                         if (nums[i-1] + 1 != nums[i])
102                                 goto fail_map;
103                 buf->vaddr = (__force void *)
104                                 ioremap_nocache(nums[0] << PAGE_SHIFT, size);
105         } else {
106                 buf->vaddr = vm_map_ram(frame_vector_pages(vec), n_pages, -1,
107                                         PAGE_KERNEL);
108         }
109
110         if (!buf->vaddr)
111                 goto fail_map;
112         buf->vaddr += offset;
113         return buf;
114
115 fail_map:
116         vb2_destroy_framevec(vec);
117 fail_pfnvec_create:
118         kfree(buf);
119
120         return NULL;
121 }
122
123 static void vb2_vmalloc_put_userptr(void *buf_priv)
124 {
125         struct vb2_vmalloc_buf *buf = buf_priv;
126         unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK;
127         unsigned int i;
128         struct page **pages;
129         unsigned int n_pages;
130
131         if (!buf->vec->is_pfns) {
132                 n_pages = frame_vector_count(buf->vec);
133                 pages = frame_vector_pages(buf->vec);
134                 if (vaddr)
135                         vm_unmap_ram((void *)vaddr, n_pages);
136                 if (buf->dma_dir == DMA_FROM_DEVICE)
137                         for (i = 0; i < n_pages; i++)
138                                 set_page_dirty_lock(pages[i]);
139         } else {
140                 iounmap((__force void __iomem *)buf->vaddr);
141         }
142         vb2_destroy_framevec(buf->vec);
143         kfree(buf);
144 }
145
146 static void *vb2_vmalloc_vaddr(void *buf_priv)
147 {
148         struct vb2_vmalloc_buf *buf = buf_priv;
149
150         if (!buf->vaddr) {
151                 pr_err("Address of an unallocated plane requested "
152                        "or cannot map user pointer\n");
153                 return NULL;
154         }
155
156         return buf->vaddr;
157 }
158
159 static unsigned int vb2_vmalloc_num_users(void *buf_priv)
160 {
161         struct vb2_vmalloc_buf *buf = buf_priv;
162         return atomic_read(&buf->refcount);
163 }
164
165 static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma)
166 {
167         struct vb2_vmalloc_buf *buf = buf_priv;
168         int ret;
169
170         if (!buf) {
171                 pr_err("No memory to map\n");
172                 return -EINVAL;
173         }
174
175         ret = remap_vmalloc_range(vma, buf->vaddr, 0);
176         if (ret) {
177                 pr_err("Remapping vmalloc memory, error: %d\n", ret);
178                 return ret;
179         }
180
181         /*
182          * Make sure that vm_areas for 2 buffers won't be merged together
183          */
184         vma->vm_flags           |= VM_DONTEXPAND;
185
186         /*
187          * Use common vm_area operations to track buffer refcount.
188          */
189         vma->vm_private_data    = &buf->handler;
190         vma->vm_ops             = &vb2_common_vm_ops;
191
192         vma->vm_ops->open(vma);
193
194         return 0;
195 }
196
197 #ifdef CONFIG_HAS_DMA
198 /*********************************************/
199 /*         DMABUF ops for exporters          */
200 /*********************************************/
201
202 struct vb2_vmalloc_attachment {
203         struct sg_table sgt;
204         enum dma_data_direction dma_dir;
205 };
206
207 static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
208         struct dma_buf_attachment *dbuf_attach)
209 {
210         struct vb2_vmalloc_attachment *attach;
211         struct vb2_vmalloc_buf *buf = dbuf->priv;
212         int num_pages = PAGE_ALIGN(buf->size) / PAGE_SIZE;
213         struct sg_table *sgt;
214         struct scatterlist *sg;
215         void *vaddr = buf->vaddr;
216         int ret;
217         int i;
218
219         attach = kzalloc(sizeof(*attach), GFP_KERNEL);
220         if (!attach)
221                 return -ENOMEM;
222
223         sgt = &attach->sgt;
224         ret = sg_alloc_table(sgt, num_pages, GFP_KERNEL);
225         if (ret) {
226                 kfree(attach);
227                 return ret;
228         }
229         for_each_sg(sgt->sgl, sg, sgt->nents, i) {
230                 struct page *page = vmalloc_to_page(vaddr);
231
232                 if (!page) {
233                         sg_free_table(sgt);
234                         kfree(attach);
235                         return -ENOMEM;
236                 }
237                 sg_set_page(sg, page, PAGE_SIZE, 0);
238                 vaddr += PAGE_SIZE;
239         }
240
241         attach->dma_dir = DMA_NONE;
242         dbuf_attach->priv = attach;
243         return 0;
244 }
245
246 static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf *dbuf,
247         struct dma_buf_attachment *db_attach)
248 {
249         struct vb2_vmalloc_attachment *attach = db_attach->priv;
250         struct sg_table *sgt;
251
252         if (!attach)
253                 return;
254
255         sgt = &attach->sgt;
256
257         /* release the scatterlist cache */
258         if (attach->dma_dir != DMA_NONE)
259                 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
260                         attach->dma_dir);
261         sg_free_table(sgt);
262         kfree(attach);
263         db_attach->priv = NULL;
264 }
265
266 static struct sg_table *vb2_vmalloc_dmabuf_ops_map(
267         struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
268 {
269         struct vb2_vmalloc_attachment *attach = db_attach->priv;
270         /* stealing dmabuf mutex to serialize map/unmap operations */
271         struct mutex *lock = &db_attach->dmabuf->lock;
272         struct sg_table *sgt;
273
274         mutex_lock(lock);
275
276         sgt = &attach->sgt;
277         /* return previously mapped sg table */
278         if (attach->dma_dir == dma_dir) {
279                 mutex_unlock(lock);
280                 return sgt;
281         }
282
283         /* release any previous cache */
284         if (attach->dma_dir != DMA_NONE) {
285                 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
286                         attach->dma_dir);
287                 attach->dma_dir = DMA_NONE;
288         }
289
290         /* mapping to the client with new direction */
291         sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
292                                 dma_dir);
293         if (!sgt->nents) {
294                 pr_err("failed to map scatterlist\n");
295                 mutex_unlock(lock);
296                 return ERR_PTR(-EIO);
297         }
298
299         attach->dma_dir = dma_dir;
300
301         mutex_unlock(lock);
302
303         return sgt;
304 }
305
306 static void vb2_vmalloc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
307         struct sg_table *sgt, enum dma_data_direction dma_dir)
308 {
309         /* nothing to be done here */
310 }
311
312 static void vb2_vmalloc_dmabuf_ops_release(struct dma_buf *dbuf)
313 {
314         /* drop reference obtained in vb2_vmalloc_get_dmabuf */
315         vb2_vmalloc_put(dbuf->priv);
316 }
317
318 static void *vb2_vmalloc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
319 {
320         struct vb2_vmalloc_buf *buf = dbuf->priv;
321
322         return buf->vaddr + pgnum * PAGE_SIZE;
323 }
324
325 static void *vb2_vmalloc_dmabuf_ops_vmap(struct dma_buf *dbuf)
326 {
327         struct vb2_vmalloc_buf *buf = dbuf->priv;
328
329         return buf->vaddr;
330 }
331
332 static int vb2_vmalloc_dmabuf_ops_mmap(struct dma_buf *dbuf,
333         struct vm_area_struct *vma)
334 {
335         return vb2_vmalloc_mmap(dbuf->priv, vma);
336 }
337
338 static struct dma_buf_ops vb2_vmalloc_dmabuf_ops = {
339         .attach = vb2_vmalloc_dmabuf_ops_attach,
340         .detach = vb2_vmalloc_dmabuf_ops_detach,
341         .map_dma_buf = vb2_vmalloc_dmabuf_ops_map,
342         .unmap_dma_buf = vb2_vmalloc_dmabuf_ops_unmap,
343         .kmap = vb2_vmalloc_dmabuf_ops_kmap,
344         .kmap_atomic = vb2_vmalloc_dmabuf_ops_kmap,
345         .vmap = vb2_vmalloc_dmabuf_ops_vmap,
346         .mmap = vb2_vmalloc_dmabuf_ops_mmap,
347         .release = vb2_vmalloc_dmabuf_ops_release,
348 };
349
350 static struct dma_buf *vb2_vmalloc_get_dmabuf(void *buf_priv, unsigned long flags)
351 {
352         struct vb2_vmalloc_buf *buf = buf_priv;
353         struct dma_buf *dbuf;
354         DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
355
356         exp_info.ops = &vb2_vmalloc_dmabuf_ops;
357         exp_info.size = buf->size;
358         exp_info.flags = flags;
359         exp_info.priv = buf;
360
361         if (WARN_ON(!buf->vaddr))
362                 return NULL;
363
364         dbuf = dma_buf_export(&exp_info);
365         if (IS_ERR(dbuf))
366                 return NULL;
367
368         /* dmabuf keeps reference to vb2 buffer */
369         atomic_inc(&buf->refcount);
370
371         return dbuf;
372 }
373 #endif /* CONFIG_HAS_DMA */
374
375
376 /*********************************************/
377 /*       callbacks for DMABUF buffers        */
378 /*********************************************/
379
380 static int vb2_vmalloc_map_dmabuf(void *mem_priv)
381 {
382         struct vb2_vmalloc_buf *buf = mem_priv;
383
384         buf->vaddr = dma_buf_vmap(buf->dbuf);
385
386         return buf->vaddr ? 0 : -EFAULT;
387 }
388
389 static void vb2_vmalloc_unmap_dmabuf(void *mem_priv)
390 {
391         struct vb2_vmalloc_buf *buf = mem_priv;
392
393         dma_buf_vunmap(buf->dbuf, buf->vaddr);
394         buf->vaddr = NULL;
395 }
396
397 static void vb2_vmalloc_detach_dmabuf(void *mem_priv)
398 {
399         struct vb2_vmalloc_buf *buf = mem_priv;
400
401         if (buf->vaddr)
402                 dma_buf_vunmap(buf->dbuf, buf->vaddr);
403
404         kfree(buf);
405 }
406
407 static void *vb2_vmalloc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
408         unsigned long size, enum dma_data_direction dma_dir)
409 {
410         struct vb2_vmalloc_buf *buf;
411
412         if (dbuf->size < size)
413                 return ERR_PTR(-EFAULT);
414
415         buf = kzalloc(sizeof(*buf), GFP_KERNEL);
416         if (!buf)
417                 return ERR_PTR(-ENOMEM);
418
419         buf->dbuf = dbuf;
420         buf->dma_dir = dma_dir;
421         buf->size = size;
422
423         return buf;
424 }
425
426
427 const struct vb2_mem_ops vb2_vmalloc_memops = {
428         .alloc          = vb2_vmalloc_alloc,
429         .put            = vb2_vmalloc_put,
430         .get_userptr    = vb2_vmalloc_get_userptr,
431         .put_userptr    = vb2_vmalloc_put_userptr,
432 #ifdef CONFIG_HAS_DMA
433         .get_dmabuf     = vb2_vmalloc_get_dmabuf,
434 #endif
435         .map_dmabuf     = vb2_vmalloc_map_dmabuf,
436         .unmap_dmabuf   = vb2_vmalloc_unmap_dmabuf,
437         .attach_dmabuf  = vb2_vmalloc_attach_dmabuf,
438         .detach_dmabuf  = vb2_vmalloc_detach_dmabuf,
439         .vaddr          = vb2_vmalloc_vaddr,
440         .mmap           = vb2_vmalloc_mmap,
441         .num_users      = vb2_vmalloc_num_users,
442 };
443 EXPORT_SYMBOL_GPL(vb2_vmalloc_memops);
444
445 MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
446 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
447 MODULE_LICENSE("GPL");