blob: 5361197f3e5741f6225db0393fb416f0dbeab03b [file] [log] [blame]
Pawel Osciak1a758d42010-10-11 10:59:36 -03001/*
2 * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciak1a758d42010-10-11 10:59:36 -03007 *
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
Sumit Semwal8c417d02012-06-14 10:37:45 -030013#include <linux/dma-buf.h>
Pawel Osciak1a758d42010-10-11 10:59:36 -030014#include <linux/module.h>
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -030015#include <linux/scatterlist.h>
16#include <linux/sched.h>
Pawel Osciak1a758d42010-10-11 10:59:36 -030017#include <linux/slab.h>
18#include <linux/dma-mapping.h>
19
Junghak Sungc1399902015-09-22 10:30:29 -030020#include <media/videobuf2-v4l2.h>
H Hartley Sweetend0df3c32012-04-24 19:08:12 -030021#include <media/videobuf2-dma-contig.h>
Pawel Osciak1a758d42010-10-11 10:59:36 -030022#include <media/videobuf2-memops.h>
23
24struct vb2_dc_conf {
25 struct device *dev;
Tomasz Figaccc66e72016-02-01 22:34:42 +010026 struct dma_attrs attrs;
Pawel Osciak1a758d42010-10-11 10:59:36 -030027};
28
29struct vb2_dc_buf {
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -030030 struct device *dev;
Pawel Osciak1a758d42010-10-11 10:59:36 -030031 void *vaddr;
Pawel Osciak1a758d42010-10-11 10:59:36 -030032 unsigned long size;
Tomasz Figaccc66e72016-02-01 22:34:42 +010033 void *cookie;
Laurent Pinchart40d8b762012-06-14 10:37:41 -030034 dma_addr_t dma_addr;
Tomasz Figaccc66e72016-02-01 22:34:42 +010035 struct dma_attrs attrs;
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -030036 enum dma_data_direction dma_dir;
37 struct sg_table *dma_sgt;
Jan Karafb639eb2015-07-13 11:55:49 -030038 struct frame_vector *vec;
Laurent Pinchart40d8b762012-06-14 10:37:41 -030039
40 /* MMAP related */
Pawel Osciak1a758d42010-10-11 10:59:36 -030041 struct vb2_vmarea_handler handler;
Laurent Pinchart40d8b762012-06-14 10:37:41 -030042 atomic_t refcount;
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -030043 struct sg_table *sgt_base;
Laurent Pinchart40d8b762012-06-14 10:37:41 -030044
Sumit Semwal8c417d02012-06-14 10:37:45 -030045 /* DMABUF related */
46 struct dma_buf_attachment *db_attach;
Pawel Osciak1a758d42010-10-11 10:59:36 -030047};
48
Laurent Pinchart40d8b762012-06-14 10:37:41 -030049/*********************************************/
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -030050/* scatterlist table functions */
51/*********************************************/
52
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -030053static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
54{
55 struct scatterlist *s;
56 dma_addr_t expected = sg_dma_address(sgt->sgl);
57 unsigned int i;
58 unsigned long size = 0;
59
60 for_each_sg(sgt->sgl, s, sgt->nents, i) {
61 if (sg_dma_address(s) != expected)
62 break;
63 expected = sg_dma_address(s) + sg_dma_len(s);
64 size += sg_dma_len(s);
65 }
66 return size;
67}
68
69/*********************************************/
Laurent Pinchart40d8b762012-06-14 10:37:41 -030070/* callbacks for all buffers */
71/*********************************************/
72
73static void *vb2_dc_cookie(void *buf_priv)
74{
75 struct vb2_dc_buf *buf = buf_priv;
76
77 return &buf->dma_addr;
78}
79
80static void *vb2_dc_vaddr(void *buf_priv)
81{
82 struct vb2_dc_buf *buf = buf_priv;
83
Philipp Zabel6bbd4fe2014-05-26 11:17:32 -030084 if (!buf->vaddr && buf->db_attach)
85 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
86
Laurent Pinchart40d8b762012-06-14 10:37:41 -030087 return buf->vaddr;
88}
89
90static unsigned int vb2_dc_num_users(void *buf_priv)
91{
92 struct vb2_dc_buf *buf = buf_priv;
93
94 return atomic_read(&buf->refcount);
95}
96
Marek Szyprowski199d1012012-06-14 10:37:44 -030097static void vb2_dc_prepare(void *buf_priv)
98{
99 struct vb2_dc_buf *buf = buf_priv;
100 struct sg_table *sgt = buf->dma_sgt;
101
Sumit Semwal8c417d02012-06-14 10:37:45 -0300102 /* DMABUF exporter will flush the cache for us */
103 if (!sgt || buf->db_attach)
Marek Szyprowski199d1012012-06-14 10:37:44 -0300104 return;
105
Tiffany Lind9a98582015-09-24 06:02:36 -0300106 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
107 buf->dma_dir);
Marek Szyprowski199d1012012-06-14 10:37:44 -0300108}
109
110static void vb2_dc_finish(void *buf_priv)
111{
112 struct vb2_dc_buf *buf = buf_priv;
113 struct sg_table *sgt = buf->dma_sgt;
114
Sumit Semwal8c417d02012-06-14 10:37:45 -0300115 /* DMABUF exporter will flush the cache for us */
116 if (!sgt || buf->db_attach)
Marek Szyprowski199d1012012-06-14 10:37:44 -0300117 return;
118
Tiffany Lind9a98582015-09-24 06:02:36 -0300119 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
Marek Szyprowski199d1012012-06-14 10:37:44 -0300120}
121
Laurent Pinchart40d8b762012-06-14 10:37:41 -0300122/*********************************************/
123/* callbacks for MMAP buffers */
124/*********************************************/
125
126static void vb2_dc_put(void *buf_priv)
127{
128 struct vb2_dc_buf *buf = buf_priv;
129
130 if (!atomic_dec_and_test(&buf->refcount))
131 return;
132
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300133 if (buf->sgt_base) {
134 sg_free_table(buf->sgt_base);
135 kfree(buf->sgt_base);
136 }
Tomasz Figaccc66e72016-02-01 22:34:42 +0100137 dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr,
138 &buf->attrs);
Tomasz Stanislawski67a5d0c2012-08-07 13:19:49 -0300139 put_device(buf->dev);
Laurent Pinchart40d8b762012-06-14 10:37:41 -0300140 kfree(buf);
141}
Pawel Osciak1a758d42010-10-11 10:59:36 -0300142
Hans Verkuild935c572014-11-18 09:50:59 -0300143static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size,
144 enum dma_data_direction dma_dir, gfp_t gfp_flags)
Pawel Osciak1a758d42010-10-11 10:59:36 -0300145{
146 struct vb2_dc_conf *conf = alloc_ctx;
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -0300147 struct device *dev = conf->dev;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300148 struct vb2_dc_buf *buf;
149
150 buf = kzalloc(sizeof *buf, GFP_KERNEL);
151 if (!buf)
152 return ERR_PTR(-ENOMEM);
153
Tomasz Figaccc66e72016-02-01 22:34:42 +0100154 buf->attrs = conf->attrs;
155 buf->cookie = dma_alloc_attrs(dev, size, &buf->dma_addr,
156 GFP_KERNEL | gfp_flags, &buf->attrs);
157 if (!buf->cookie) {
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -0300158 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300159 kfree(buf);
160 return ERR_PTR(-ENOMEM);
161 }
162
Tomasz Figaccc66e72016-02-01 22:34:42 +0100163 if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &buf->attrs))
164 buf->vaddr = buf->cookie;
165
Tomasz Stanislawski67a5d0c2012-08-07 13:19:49 -0300166 /* Prevent the device from being released while the buffer is used */
167 buf->dev = get_device(dev);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300168 buf->size = size;
Hans Verkuild935c572014-11-18 09:50:59 -0300169 buf->dma_dir = dma_dir;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300170
171 buf->handler.refcount = &buf->refcount;
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300172 buf->handler.put = vb2_dc_put;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300173 buf->handler.arg = buf;
174
175 atomic_inc(&buf->refcount);
176
177 return buf;
178}
179
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300180static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
Pawel Osciak1a758d42010-10-11 10:59:36 -0300181{
182 struct vb2_dc_buf *buf = buf_priv;
Marek Szyprowskic60520f2012-06-14 11:32:21 -0300183 int ret;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300184
185 if (!buf) {
186 printk(KERN_ERR "No buffer to map\n");
187 return -EINVAL;
188 }
189
Marek Szyprowskic60520f2012-06-14 11:32:21 -0300190 /*
191 * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
192 * map whole buffer
193 */
194 vma->vm_pgoff = 0;
195
Tomasz Figaccc66e72016-02-01 22:34:42 +0100196 ret = dma_mmap_attrs(buf->dev, vma, buf->cookie,
197 buf->dma_addr, buf->size, &buf->attrs);
Marek Szyprowskic60520f2012-06-14 11:32:21 -0300198
199 if (ret) {
200 pr_err("Remapping memory failed, error: %d\n", ret);
201 return ret;
202 }
203
204 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
205 vma->vm_private_data = &buf->handler;
206 vma->vm_ops = &vb2_common_vm_ops;
207
208 vma->vm_ops->open(vma);
209
210 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
211 __func__, (unsigned long)buf->dma_addr, vma->vm_start,
212 buf->size);
213
214 return 0;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300215}
216
Laurent Pinchart40d8b762012-06-14 10:37:41 -0300217/*********************************************/
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300218/* DMABUF ops for exporters */
219/*********************************************/
220
221struct vb2_dc_attachment {
222 struct sg_table sgt;
Hans Verkuilcd474032014-11-18 09:50:58 -0300223 enum dma_data_direction dma_dir;
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300224};
225
226static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
227 struct dma_buf_attachment *dbuf_attach)
228{
229 struct vb2_dc_attachment *attach;
230 unsigned int i;
231 struct scatterlist *rd, *wr;
232 struct sg_table *sgt;
233 struct vb2_dc_buf *buf = dbuf->priv;
234 int ret;
235
236 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
237 if (!attach)
238 return -ENOMEM;
239
240 sgt = &attach->sgt;
241 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
242 * map the same scatter list to multiple attachments at the same time.
243 */
244 ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
245 if (ret) {
246 kfree(attach);
247 return -ENOMEM;
248 }
249
250 rd = buf->sgt_base->sgl;
251 wr = sgt->sgl;
252 for (i = 0; i < sgt->orig_nents; ++i) {
253 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
254 rd = sg_next(rd);
255 wr = sg_next(wr);
256 }
257
Hans Verkuilcd474032014-11-18 09:50:58 -0300258 attach->dma_dir = DMA_NONE;
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300259 dbuf_attach->priv = attach;
260
261 return 0;
262}
263
264static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
265 struct dma_buf_attachment *db_attach)
266{
267 struct vb2_dc_attachment *attach = db_attach->priv;
268 struct sg_table *sgt;
269
270 if (!attach)
271 return;
272
273 sgt = &attach->sgt;
274
275 /* release the scatterlist cache */
Hans Verkuilcd474032014-11-18 09:50:58 -0300276 if (attach->dma_dir != DMA_NONE)
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300277 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
Hans Verkuilcd474032014-11-18 09:50:58 -0300278 attach->dma_dir);
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300279 sg_free_table(sgt);
280 kfree(attach);
281 db_attach->priv = NULL;
282}
283
284static struct sg_table *vb2_dc_dmabuf_ops_map(
Hans Verkuilcd474032014-11-18 09:50:58 -0300285 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300286{
287 struct vb2_dc_attachment *attach = db_attach->priv;
288 /* stealing dmabuf mutex to serialize map/unmap operations */
289 struct mutex *lock = &db_attach->dmabuf->lock;
290 struct sg_table *sgt;
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300291
292 mutex_lock(lock);
293
294 sgt = &attach->sgt;
295 /* return previously mapped sg table */
Hans Verkuilcd474032014-11-18 09:50:58 -0300296 if (attach->dma_dir == dma_dir) {
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300297 mutex_unlock(lock);
298 return sgt;
299 }
300
301 /* release any previous cache */
Hans Verkuilcd474032014-11-18 09:50:58 -0300302 if (attach->dma_dir != DMA_NONE) {
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300303 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
Hans Verkuilcd474032014-11-18 09:50:58 -0300304 attach->dma_dir);
305 attach->dma_dir = DMA_NONE;
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300306 }
307
308 /* mapping to the client with new direction */
Ricardo Ribalda60a47192015-04-29 09:00:46 -0300309 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
310 dma_dir);
311 if (!sgt->nents) {
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300312 pr_err("failed to map scatterlist\n");
313 mutex_unlock(lock);
314 return ERR_PTR(-EIO);
315 }
316
Hans Verkuilcd474032014-11-18 09:50:58 -0300317 attach->dma_dir = dma_dir;
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300318
319 mutex_unlock(lock);
320
321 return sgt;
322}
323
324static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
Hans Verkuilcd474032014-11-18 09:50:58 -0300325 struct sg_table *sgt, enum dma_data_direction dma_dir)
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300326{
327 /* nothing to be done here */
328}
329
330static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
331{
332 /* drop reference obtained in vb2_dc_get_dmabuf */
333 vb2_dc_put(dbuf->priv);
334}
335
336static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
337{
338 struct vb2_dc_buf *buf = dbuf->priv;
339
Tomasz Figaccc66e72016-02-01 22:34:42 +0100340 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300341}
342
343static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
344{
345 struct vb2_dc_buf *buf = dbuf->priv;
346
347 return buf->vaddr;
348}
349
350static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
351 struct vm_area_struct *vma)
352{
353 return vb2_dc_mmap(dbuf->priv, vma);
354}
355
356static struct dma_buf_ops vb2_dc_dmabuf_ops = {
357 .attach = vb2_dc_dmabuf_ops_attach,
358 .detach = vb2_dc_dmabuf_ops_detach,
359 .map_dma_buf = vb2_dc_dmabuf_ops_map,
360 .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
361 .kmap = vb2_dc_dmabuf_ops_kmap,
362 .kmap_atomic = vb2_dc_dmabuf_ops_kmap,
363 .vmap = vb2_dc_dmabuf_ops_vmap,
364 .mmap = vb2_dc_dmabuf_ops_mmap,
365 .release = vb2_dc_dmabuf_ops_release,
366};
367
368static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
369{
370 int ret;
371 struct sg_table *sgt;
372
373 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
374 if (!sgt) {
375 dev_err(buf->dev, "failed to alloc sg table\n");
376 return NULL;
377 }
378
Tomasz Figaccc66e72016-02-01 22:34:42 +0100379 ret = dma_get_sgtable_attrs(buf->dev, sgt, buf->cookie, buf->dma_addr,
380 buf->size, &buf->attrs);
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300381 if (ret < 0) {
382 dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
383 kfree(sgt);
384 return NULL;
385 }
386
387 return sgt;
388}
389
Philipp Zabelc1b96a22013-05-21 05:11:35 -0300390static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300391{
392 struct vb2_dc_buf *buf = buf_priv;
393 struct dma_buf *dbuf;
Sumit Semwald8fbe342015-01-23 12:53:43 +0530394 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
395
396 exp_info.ops = &vb2_dc_dmabuf_ops;
397 exp_info.size = buf->size;
398 exp_info.flags = flags;
399 exp_info.priv = buf;
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300400
401 if (!buf->sgt_base)
402 buf->sgt_base = vb2_dc_get_base_sgt(buf);
403
404 if (WARN_ON(!buf->sgt_base))
405 return NULL;
406
Sumit Semwald8fbe342015-01-23 12:53:43 +0530407 dbuf = dma_buf_export(&exp_info);
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300408 if (IS_ERR(dbuf))
409 return NULL;
410
411 /* dmabuf keeps reference to vb2 buffer */
412 atomic_inc(&buf->refcount);
413
414 return dbuf;
415}
416
417/*********************************************/
Laurent Pinchart40d8b762012-06-14 10:37:41 -0300418/* callbacks for USERPTR buffers */
419/*********************************************/
420
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300421static void vb2_dc_put_userptr(void *buf_priv)
422{
423 struct vb2_dc_buf *buf = buf_priv;
424 struct sg_table *sgt = buf->dma_sgt;
Jan Karafb639eb2015-07-13 11:55:49 -0300425 int i;
426 struct page **pages;
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300427
Marek Szyprowski774d2302013-06-19 08:56:46 -0300428 if (sgt) {
Hans Verkuil251a79f2014-11-18 09:51:08 -0300429 DEFINE_DMA_ATTRS(attrs);
430
431 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
432 /*
433 * No need to sync to CPU, it's already synced to the CPU
434 * since the finish() memop will have been called before this.
435 */
436 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
437 buf->dma_dir, &attrs);
Jan Karafb639eb2015-07-13 11:55:49 -0300438 pages = frame_vector_pages(buf->vec);
439 /* sgt should exist only if vector contains pages... */
440 BUG_ON(IS_ERR(pages));
441 for (i = 0; i < frame_vector_count(buf->vec); i++)
442 set_page_dirty_lock(pages[i]);
Marek Szyprowski774d2302013-06-19 08:56:46 -0300443 sg_free_table(sgt);
444 kfree(sgt);
445 }
Jan Karafb639eb2015-07-13 11:55:49 -0300446 vb2_destroy_framevec(buf->vec);
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300447 kfree(buf);
448}
449
Marek Szyprowski774d2302013-06-19 08:56:46 -0300450/*
451 * For some kind of reserved memory there might be no struct page available,
452 * so all that can be done to support such 'pages' is to try to convert
453 * pfn to dma address or at the last resort just assume that
454 * dma address == physical address (like it has been assumed in earlier version
455 * of videobuf2-dma-contig
456 */
457
458#ifdef __arch_pfn_to_dma
459static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
460{
461 return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
462}
463#elif defined(__pfn_to_bus)
464static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
465{
466 return (dma_addr_t)__pfn_to_bus(pfn);
467}
468#elif defined(__pfn_to_phys)
469static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
470{
471 return (dma_addr_t)__pfn_to_phys(pfn);
472}
473#else
474static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
475{
476 /* really, we cannot do anything better at this point */
477 return (dma_addr_t)(pfn) << PAGE_SHIFT;
478}
479#endif
480
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300481static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
Hans Verkuilcd474032014-11-18 09:50:58 -0300482 unsigned long size, enum dma_data_direction dma_dir)
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300483{
484 struct vb2_dc_conf *conf = alloc_ctx;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300485 struct vb2_dc_buf *buf;
Jan Karafb639eb2015-07-13 11:55:49 -0300486 struct frame_vector *vec;
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300487 unsigned long offset;
Jan Karafb639eb2015-07-13 11:55:49 -0300488 int n_pages, i;
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300489 int ret = 0;
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300490 struct sg_table *sgt;
491 unsigned long contig_size;
Marek Szyprowskid81e8702012-06-12 10:18:16 -0300492 unsigned long dma_align = dma_get_cache_alignment();
Hans Verkuil251a79f2014-11-18 09:51:08 -0300493 DEFINE_DMA_ATTRS(attrs);
494
495 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
Marek Szyprowskid81e8702012-06-12 10:18:16 -0300496
497 /* Only cache aligned DMA transfers are reliable */
498 if (!IS_ALIGNED(vaddr | size, dma_align)) {
499 pr_debug("user data must be aligned to %lu bytes\n", dma_align);
500 return ERR_PTR(-EINVAL);
501 }
502
503 if (!size) {
504 pr_debug("size is zero\n");
505 return ERR_PTR(-EINVAL);
506 }
Pawel Osciak1a758d42010-10-11 10:59:36 -0300507
508 buf = kzalloc(sizeof *buf, GFP_KERNEL);
509 if (!buf)
510 return ERR_PTR(-ENOMEM);
511
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300512 buf->dev = conf->dev;
Hans Verkuilcd474032014-11-18 09:50:58 -0300513 buf->dma_dir = dma_dir;
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300514
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300515 offset = vaddr & ~PAGE_MASK;
Jan Karafb639eb2015-07-13 11:55:49 -0300516 vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
517 if (IS_ERR(vec)) {
518 ret = PTR_ERR(vec);
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300519 goto fail_buf;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300520 }
Jan Karafb639eb2015-07-13 11:55:49 -0300521 buf->vec = vec;
522 n_pages = frame_vector_count(vec);
523 ret = frame_vector_to_pages(vec);
524 if (ret < 0) {
525 unsigned long *nums = frame_vector_pfns(vec);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300526
Jan Karafb639eb2015-07-13 11:55:49 -0300527 /*
528 * Failed to convert to pages... Check the memory is physically
529 * contiguous and use direct mapping
530 */
531 for (i = 1; i < n_pages; i++)
532 if (nums[i-1] + 1 != nums[i])
533 goto fail_pfnvec;
534 buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, nums[0]);
535 goto out;
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300536 }
537
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300538 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
539 if (!sgt) {
540 pr_err("failed to allocate sg table\n");
541 ret = -ENOMEM;
Jan Karafb639eb2015-07-13 11:55:49 -0300542 goto fail_pfnvec;
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300543 }
544
Jan Karafb639eb2015-07-13 11:55:49 -0300545 ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages,
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300546 offset, size, GFP_KERNEL);
547 if (ret) {
548 pr_err("failed to initialize sg table\n");
549 goto fail_sgt;
550 }
551
Hans Verkuil251a79f2014-11-18 09:51:08 -0300552 /*
553 * No need to sync to the device, this will happen later when the
554 * prepare() memop is called.
555 */
556 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
557 buf->dma_dir, &attrs);
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300558 if (sgt->nents <= 0) {
559 pr_err("failed to map scatterlist\n");
560 ret = -EIO;
561 goto fail_sgt_init;
562 }
563
564 contig_size = vb2_dc_get_contiguous_size(sgt);
565 if (contig_size < size) {
566 pr_err("contiguous mapping is too small %lu/%lu\n",
567 contig_size, size);
568 ret = -EFAULT;
569 goto fail_map_sg;
570 }
571
572 buf->dma_addr = sg_dma_address(sgt->sgl);
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300573 buf->dma_sgt = sgt;
Jan Karafb639eb2015-07-13 11:55:49 -0300574out:
575 buf->size = size;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300576
577 return buf;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300578
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300579fail_map_sg:
Hans Verkuil251a79f2014-11-18 09:51:08 -0300580 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
581 buf->dma_dir, &attrs);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300582
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300583fail_sgt_init:
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300584 sg_free_table(sgt);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300585
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300586fail_sgt:
587 kfree(sgt);
588
Jan Karafb639eb2015-07-13 11:55:49 -0300589fail_pfnvec:
590 vb2_destroy_framevec(vec);
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300591
592fail_buf:
Pawel Osciak1a758d42010-10-11 10:59:36 -0300593 kfree(buf);
Tomasz Stanislawskie15dab72012-06-14 10:37:42 -0300594
595 return ERR_PTR(ret);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300596}
597
Laurent Pinchart40d8b762012-06-14 10:37:41 -0300598/*********************************************/
Sumit Semwal8c417d02012-06-14 10:37:45 -0300599/* callbacks for DMABUF buffers */
600/*********************************************/
601
602static int vb2_dc_map_dmabuf(void *mem_priv)
603{
604 struct vb2_dc_buf *buf = mem_priv;
605 struct sg_table *sgt;
606 unsigned long contig_size;
607
608 if (WARN_ON(!buf->db_attach)) {
609 pr_err("trying to pin a non attached buffer\n");
610 return -EINVAL;
611 }
612
613 if (WARN_ON(buf->dma_sgt)) {
614 pr_err("dmabuf buffer is already pinned\n");
615 return 0;
616 }
617
618 /* get the associated scatterlist for this buffer */
619 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
Colin Crossfee0c542013-12-20 16:43:50 -0800620 if (IS_ERR(sgt)) {
Sumit Semwal8c417d02012-06-14 10:37:45 -0300621 pr_err("Error getting dmabuf scatterlist\n");
622 return -EINVAL;
623 }
624
625 /* checking if dmabuf is big enough to store contiguous chunk */
626 contig_size = vb2_dc_get_contiguous_size(sgt);
627 if (contig_size < buf->size) {
628 pr_err("contiguous chunk is too small %lu/%lu b\n",
629 contig_size, buf->size);
630 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
631 return -EFAULT;
632 }
633
634 buf->dma_addr = sg_dma_address(sgt->sgl);
635 buf->dma_sgt = sgt;
Philipp Zabel6bbd4fe2014-05-26 11:17:32 -0300636 buf->vaddr = NULL;
Sumit Semwal8c417d02012-06-14 10:37:45 -0300637
638 return 0;
639}
640
641static void vb2_dc_unmap_dmabuf(void *mem_priv)
642{
643 struct vb2_dc_buf *buf = mem_priv;
644 struct sg_table *sgt = buf->dma_sgt;
645
646 if (WARN_ON(!buf->db_attach)) {
647 pr_err("trying to unpin a not attached buffer\n");
648 return;
649 }
650
651 if (WARN_ON(!sgt)) {
652 pr_err("dmabuf buffer is already unpinned\n");
653 return;
654 }
655
Philipp Zabel6bbd4fe2014-05-26 11:17:32 -0300656 if (buf->vaddr) {
657 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
658 buf->vaddr = NULL;
659 }
Sumit Semwal8c417d02012-06-14 10:37:45 -0300660 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
661
662 buf->dma_addr = 0;
663 buf->dma_sgt = NULL;
664}
665
666static void vb2_dc_detach_dmabuf(void *mem_priv)
667{
668 struct vb2_dc_buf *buf = mem_priv;
669
670 /* if vb2 works correctly you should never detach mapped buffer */
671 if (WARN_ON(buf->dma_addr))
672 vb2_dc_unmap_dmabuf(buf);
673
674 /* detach this attachment */
675 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
676 kfree(buf);
677}
678
679static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
Hans Verkuilcd474032014-11-18 09:50:58 -0300680 unsigned long size, enum dma_data_direction dma_dir)
Sumit Semwal8c417d02012-06-14 10:37:45 -0300681{
682 struct vb2_dc_conf *conf = alloc_ctx;
683 struct vb2_dc_buf *buf;
684 struct dma_buf_attachment *dba;
685
686 if (dbuf->size < size)
687 return ERR_PTR(-EFAULT);
688
689 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
690 if (!buf)
691 return ERR_PTR(-ENOMEM);
692
693 buf->dev = conf->dev;
694 /* create attachment for the dmabuf with the user device */
695 dba = dma_buf_attach(dbuf, buf->dev);
696 if (IS_ERR(dba)) {
697 pr_err("failed to attach dmabuf\n");
698 kfree(buf);
699 return dba;
700 }
701
Hans Verkuilcd474032014-11-18 09:50:58 -0300702 buf->dma_dir = dma_dir;
Sumit Semwal8c417d02012-06-14 10:37:45 -0300703 buf->size = size;
704 buf->db_attach = dba;
705
706 return buf;
707}
708
709/*********************************************/
Laurent Pinchart40d8b762012-06-14 10:37:41 -0300710/* DMA CONTIG exported functions */
711/*********************************************/
712
Pawel Osciak1a758d42010-10-11 10:59:36 -0300713const struct vb2_mem_ops vb2_dma_contig_memops = {
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300714 .alloc = vb2_dc_alloc,
715 .put = vb2_dc_put,
Tomasz Stanislawski9ef2cbe2012-06-14 11:32:25 -0300716 .get_dmabuf = vb2_dc_get_dmabuf,
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300717 .cookie = vb2_dc_cookie,
718 .vaddr = vb2_dc_vaddr,
719 .mmap = vb2_dc_mmap,
720 .get_userptr = vb2_dc_get_userptr,
721 .put_userptr = vb2_dc_put_userptr,
Marek Szyprowski199d1012012-06-14 10:37:44 -0300722 .prepare = vb2_dc_prepare,
723 .finish = vb2_dc_finish,
Sumit Semwal8c417d02012-06-14 10:37:45 -0300724 .map_dmabuf = vb2_dc_map_dmabuf,
725 .unmap_dmabuf = vb2_dc_unmap_dmabuf,
726 .attach_dmabuf = vb2_dc_attach_dmabuf,
727 .detach_dmabuf = vb2_dc_detach_dmabuf,
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300728 .num_users = vb2_dc_num_users,
Pawel Osciak1a758d42010-10-11 10:59:36 -0300729};
730EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
731
Tomasz Figaccc66e72016-02-01 22:34:42 +0100732void *vb2_dma_contig_init_ctx_attrs(struct device *dev,
733 struct dma_attrs *attrs)
Pawel Osciak1a758d42010-10-11 10:59:36 -0300734{
735 struct vb2_dc_conf *conf;
736
737 conf = kzalloc(sizeof *conf, GFP_KERNEL);
738 if (!conf)
739 return ERR_PTR(-ENOMEM);
740
741 conf->dev = dev;
Tomasz Figaccc66e72016-02-01 22:34:42 +0100742 if (attrs)
743 conf->attrs = *attrs;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300744
745 return conf;
746}
Tomasz Figaccc66e72016-02-01 22:34:42 +0100747EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx_attrs);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300748
749void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
750{
Hans Verkuile5ae8fa2014-11-18 09:51:00 -0300751 if (!IS_ERR_OR_NULL(alloc_ctx))
752 kfree(alloc_ctx);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300753}
754EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
755
756MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
Pawel Osciak95072082011-03-13 15:23:32 -0300757MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
Pawel Osciak1a758d42010-10-11 10:59:36 -0300758MODULE_LICENSE("GPL");