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