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 | |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 20 | #include <media/videobuf2-v4l2.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 | |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 36 | static void *vb2_vmalloc_alloc(struct device *dev, unsigned long attrs, |
Hans Verkuil | d16e832 | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 37 | unsigned long size, enum dma_data_direction dma_dir, |
| 38 | gfp_t gfp_flags) |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 39 | { |
| 40 | struct vb2_vmalloc_buf *buf; |
| 41 | |
Hans Verkuil | b6ba205 | 2013-03-01 15:44:20 -0300 | [diff] [blame] | 42 | buf = kzalloc(sizeof(*buf), GFP_KERNEL | gfp_flags); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 43 | if (!buf) |
| 44 | return NULL; |
| 45 | |
| 46 | buf->size = size; |
| 47 | buf->vaddr = vmalloc_user(buf->size); |
Hans Verkuil | d935c57 | 2014-11-18 09:50:59 -0300 | [diff] [blame] | 48 | buf->dma_dir = dma_dir; |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 49 | buf->handler.refcount = &buf->refcount; |
| 50 | buf->handler.put = vb2_vmalloc_put; |
| 51 | buf->handler.arg = buf; |
| 52 | |
| 53 | if (!buf->vaddr) { |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 54 | pr_debug("vmalloc of size %ld failed\n", buf->size); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 55 | kfree(buf); |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | atomic_inc(&buf->refcount); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 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)) { |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 68 | vfree(buf->vaddr); |
| 69 | kfree(buf); |
| 70 | } |
| 71 | } |
| 72 | |
Hans Verkuil | 36c0f8b | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 73 | static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 74 | unsigned long size, |
| 75 | enum dma_data_direction dma_dir) |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 76 | { |
| 77 | struct vb2_vmalloc_buf *buf; |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 78 | struct frame_vector *vec; |
| 79 | int n_pages, offset, i; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 80 | |
| 81 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
| 82 | if (!buf) |
| 83 | return NULL; |
| 84 | |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 85 | buf->dma_dir = dma_dir; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 86 | offset = vaddr & ~PAGE_MASK; |
| 87 | buf->size = size; |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 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); |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 95 | |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 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); |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 105 | } else { |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 106 | buf->vaddr = vm_map_ram(frame_vector_pages(vec), n_pages, -1, |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 107 | PAGE_KERNEL); |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 108 | } |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 109 | |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 110 | if (!buf->vaddr) |
| 111 | goto fail_map; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 112 | buf->vaddr += offset; |
| 113 | return buf; |
| 114 | |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 115 | fail_map: |
| 116 | vb2_destroy_framevec(vec); |
| 117 | fail_pfnvec_create: |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 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; |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 128 | struct page **pages; |
| 129 | unsigned int n_pages; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 130 | |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 131 | if (!buf->vec->is_pfns) { |
| 132 | n_pages = frame_vector_count(buf->vec); |
| 133 | pages = frame_vector_pages(buf->vec); |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 134 | if (vaddr) |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 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]); |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 139 | } else { |
Hans Verkuil | 7c424dd | 2014-12-13 08:52:54 -0300 | [diff] [blame] | 140 | iounmap((__force void __iomem *)buf->vaddr); |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 141 | } |
Jan Kara | 5a9e4de | 2015-07-13 11:55:48 -0300 | [diff] [blame] | 142 | vb2_destroy_framevec(buf->vec); |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 143 | kfree(buf); |
| 144 | } |
| 145 | |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 146 | static void *vb2_vmalloc_vaddr(void *buf_priv) |
| 147 | { |
| 148 | struct vb2_vmalloc_buf *buf = buf_priv; |
| 149 | |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 150 | if (!buf->vaddr) { |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 151 | pr_err("Address of an unallocated plane requested " |
| 152 | "or cannot map user pointer\n"); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 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) { |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 171 | pr_err("No memory to map\n"); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | ret = remap_vmalloc_range(vma, buf->vaddr, 0); |
| 176 | if (ret) { |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 177 | pr_err("Remapping vmalloc memory, error: %d\n", ret); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 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 | |
Geert Uytterhoeven | 99f3cd5 | 2014-12-15 10:40:28 -0300 | [diff] [blame] | 197 | #ifdef CONFIG_HAS_DMA |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 198 | /*********************************************/ |
Hans Verkuil | 4650635 | 2014-11-18 09:51:05 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 4650635 | 2014-11-18 09:51:05 -0300 | [diff] [blame] | 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 */ |
Ricardo Ribalda | ba81c6e | 2015-04-29 09:00:47 -0300 | [diff] [blame] | 291 | sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, |
| 292 | dma_dir); |
| 293 | if (!sgt->nents) { |
Hans Verkuil | 4650635 | 2014-11-18 09:51:05 -0300 | [diff] [blame] | 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; |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 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; |
Hans Verkuil | 4650635 | 2014-11-18 09:51:05 -0300 | [diff] [blame] | 360 | |
| 361 | if (WARN_ON(!buf->vaddr)) |
| 362 | return NULL; |
| 363 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 364 | dbuf = dma_buf_export(&exp_info); |
Hans Verkuil | 4650635 | 2014-11-18 09:51:05 -0300 | [diff] [blame] | 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 | } |
Geert Uytterhoeven | 99f3cd5 | 2014-12-15 10:40:28 -0300 | [diff] [blame] | 373 | #endif /* CONFIG_HAS_DMA */ |
| 374 | |
Hans Verkuil | 4650635 | 2014-11-18 09:51:05 -0300 | [diff] [blame] | 375 | |
| 376 | /*********************************************/ |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | 36c0f8b | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 407 | static void *vb2_vmalloc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 408 | unsigned long size, enum dma_data_direction dma_dir) |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 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; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 420 | buf->dma_dir = dma_dir; |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 421 | buf->size = size; |
| 422 | |
| 423 | return buf; |
| 424 | } |
| 425 | |
| 426 | |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 427 | const struct vb2_mem_ops vb2_vmalloc_memops = { |
| 428 | .alloc = vb2_vmalloc_alloc, |
| 429 | .put = vb2_vmalloc_put, |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 430 | .get_userptr = vb2_vmalloc_get_userptr, |
| 431 | .put_userptr = vb2_vmalloc_put_userptr, |
Geert Uytterhoeven | 99f3cd5 | 2014-12-15 10:40:28 -0300 | [diff] [blame] | 432 | #ifdef CONFIG_HAS_DMA |
Hans Verkuil | 4650635 | 2014-11-18 09:51:05 -0300 | [diff] [blame] | 433 | .get_dmabuf = vb2_vmalloc_get_dmabuf, |
Geert Uytterhoeven | 99f3cd5 | 2014-12-15 10:40:28 -0300 | [diff] [blame] | 434 | #endif |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 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, |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 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"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 446 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>"); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 447 | MODULE_LICENSE("GPL"); |