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; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 26 | struct page **pages; |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 27 | struct vm_area_struct *vma; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame^] | 28 | enum dma_data_direction dma_dir; |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 29 | unsigned long size; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 30 | unsigned int n_pages; |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 31 | atomic_t refcount; |
| 32 | struct vb2_vmarea_handler handler; |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 33 | struct dma_buf *dbuf; |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static void vb2_vmalloc_put(void *buf_priv); |
| 37 | |
Hans Verkuil | b6ba205 | 2013-03-01 15:44:20 -0300 | [diff] [blame] | 38 | static void *vb2_vmalloc_alloc(void *alloc_ctx, unsigned long size, 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); |
| 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; |
| 77 | unsigned long first, last; |
| 78 | int n_pages, offset; |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 79 | struct vm_area_struct *vma; |
| 80 | dma_addr_t physp; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 81 | |
| 82 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
| 83 | if (!buf) |
| 84 | return NULL; |
| 85 | |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame^] | 86 | buf->dma_dir = dma_dir; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 87 | offset = vaddr & ~PAGE_MASK; |
| 88 | buf->size = size; |
| 89 | |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 90 | |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 91 | vma = find_vma(current->mm, vaddr); |
| 92 | if (vma && (vma->vm_flags & VM_PFNMAP) && (vma->vm_pgoff)) { |
| 93 | if (vb2_get_contig_userptr(vaddr, size, &vma, &physp)) |
| 94 | goto fail_pages_array_alloc; |
| 95 | buf->vma = vma; |
| 96 | buf->vaddr = ioremap_nocache(physp, size); |
| 97 | if (!buf->vaddr) |
| 98 | goto fail_pages_array_alloc; |
| 99 | } else { |
| 100 | first = vaddr >> PAGE_SHIFT; |
| 101 | last = (vaddr + size - 1) >> PAGE_SHIFT; |
| 102 | buf->n_pages = last - first + 1; |
| 103 | buf->pages = kzalloc(buf->n_pages * sizeof(struct page *), |
| 104 | GFP_KERNEL); |
| 105 | if (!buf->pages) |
| 106 | goto fail_pages_array_alloc; |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 107 | |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 108 | /* current->mm->mmap_sem is taken by videobuf2 core */ |
| 109 | n_pages = get_user_pages(current, current->mm, |
| 110 | vaddr & PAGE_MASK, buf->n_pages, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame^] | 111 | dma_dir == DMA_FROM_DEVICE, |
| 112 | 1, /* force */ |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 113 | buf->pages, NULL); |
| 114 | if (n_pages != buf->n_pages) |
| 115 | goto fail_get_user_pages; |
| 116 | |
| 117 | buf->vaddr = vm_map_ram(buf->pages, buf->n_pages, -1, |
| 118 | PAGE_KERNEL); |
| 119 | if (!buf->vaddr) |
| 120 | goto fail_get_user_pages; |
| 121 | } |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 122 | |
| 123 | buf->vaddr += offset; |
| 124 | return buf; |
| 125 | |
| 126 | fail_get_user_pages: |
| 127 | pr_debug("get_user_pages requested/got: %d/%d]\n", n_pages, |
| 128 | buf->n_pages); |
| 129 | while (--n_pages >= 0) |
| 130 | put_page(buf->pages[n_pages]); |
| 131 | kfree(buf->pages); |
| 132 | |
| 133 | fail_pages_array_alloc: |
| 134 | kfree(buf); |
| 135 | |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | static void vb2_vmalloc_put_userptr(void *buf_priv) |
| 140 | { |
| 141 | struct vb2_vmalloc_buf *buf = buf_priv; |
| 142 | unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK; |
| 143 | unsigned int i; |
| 144 | |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 145 | if (buf->pages) { |
| 146 | if (vaddr) |
| 147 | vm_unmap_ram((void *)vaddr, buf->n_pages); |
| 148 | for (i = 0; i < buf->n_pages; ++i) { |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame^] | 149 | if (buf->dma_dir == DMA_FROM_DEVICE) |
Javier Martin | 570d2a4 | 2012-02-16 12:19:08 -0300 | [diff] [blame] | 150 | set_page_dirty_lock(buf->pages[i]); |
| 151 | put_page(buf->pages[i]); |
| 152 | } |
| 153 | kfree(buf->pages); |
| 154 | } else { |
| 155 | if (buf->vma) |
| 156 | vb2_put_vma(buf->vma); |
| 157 | iounmap(buf->vaddr); |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 158 | } |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 159 | kfree(buf); |
| 160 | } |
| 161 | |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 162 | static void *vb2_vmalloc_vaddr(void *buf_priv) |
| 163 | { |
| 164 | struct vb2_vmalloc_buf *buf = buf_priv; |
| 165 | |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 166 | if (!buf->vaddr) { |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 167 | pr_err("Address of an unallocated plane requested " |
| 168 | "or cannot map user pointer\n"); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | return buf->vaddr; |
| 173 | } |
| 174 | |
| 175 | static unsigned int vb2_vmalloc_num_users(void *buf_priv) |
| 176 | { |
| 177 | struct vb2_vmalloc_buf *buf = buf_priv; |
| 178 | return atomic_read(&buf->refcount); |
| 179 | } |
| 180 | |
| 181 | static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma) |
| 182 | { |
| 183 | struct vb2_vmalloc_buf *buf = buf_priv; |
| 184 | int ret; |
| 185 | |
| 186 | if (!buf) { |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 187 | pr_err("No memory to map\n"); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 188 | return -EINVAL; |
| 189 | } |
| 190 | |
| 191 | ret = remap_vmalloc_range(vma, buf->vaddr, 0); |
| 192 | if (ret) { |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 193 | pr_err("Remapping vmalloc memory, error: %d\n", ret); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Make sure that vm_areas for 2 buffers won't be merged together |
| 199 | */ |
| 200 | vma->vm_flags |= VM_DONTEXPAND; |
| 201 | |
| 202 | /* |
| 203 | * Use common vm_area operations to track buffer refcount. |
| 204 | */ |
| 205 | vma->vm_private_data = &buf->handler; |
| 206 | vma->vm_ops = &vb2_common_vm_ops; |
| 207 | |
| 208 | vma->vm_ops->open(vma); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 213 | /*********************************************/ |
| 214 | /* callbacks for DMABUF buffers */ |
| 215 | /*********************************************/ |
| 216 | |
| 217 | static int vb2_vmalloc_map_dmabuf(void *mem_priv) |
| 218 | { |
| 219 | struct vb2_vmalloc_buf *buf = mem_priv; |
| 220 | |
| 221 | buf->vaddr = dma_buf_vmap(buf->dbuf); |
| 222 | |
| 223 | return buf->vaddr ? 0 : -EFAULT; |
| 224 | } |
| 225 | |
| 226 | static void vb2_vmalloc_unmap_dmabuf(void *mem_priv) |
| 227 | { |
| 228 | struct vb2_vmalloc_buf *buf = mem_priv; |
| 229 | |
| 230 | dma_buf_vunmap(buf->dbuf, buf->vaddr); |
| 231 | buf->vaddr = NULL; |
| 232 | } |
| 233 | |
| 234 | static void vb2_vmalloc_detach_dmabuf(void *mem_priv) |
| 235 | { |
| 236 | struct vb2_vmalloc_buf *buf = mem_priv; |
| 237 | |
| 238 | if (buf->vaddr) |
| 239 | dma_buf_vunmap(buf->dbuf, buf->vaddr); |
| 240 | |
| 241 | kfree(buf); |
| 242 | } |
| 243 | |
| 244 | 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^] | 245 | unsigned long size, enum dma_data_direction dma_dir) |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 246 | { |
| 247 | struct vb2_vmalloc_buf *buf; |
| 248 | |
| 249 | if (dbuf->size < size) |
| 250 | return ERR_PTR(-EFAULT); |
| 251 | |
| 252 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
| 253 | if (!buf) |
| 254 | return ERR_PTR(-ENOMEM); |
| 255 | |
| 256 | buf->dbuf = dbuf; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame^] | 257 | buf->dma_dir = dma_dir; |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 258 | buf->size = size; |
| 259 | |
| 260 | return buf; |
| 261 | } |
| 262 | |
| 263 | |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 264 | const struct vb2_mem_ops vb2_vmalloc_memops = { |
| 265 | .alloc = vb2_vmalloc_alloc, |
| 266 | .put = vb2_vmalloc_put, |
Andrzej Pietrasiewicz | 4419b8a | 2011-10-13 07:30:51 -0300 | [diff] [blame] | 267 | .get_userptr = vb2_vmalloc_get_userptr, |
| 268 | .put_userptr = vb2_vmalloc_put_userptr, |
Tomasz Stanislawski | 89d2ee0 | 2012-06-14 10:37:46 -0300 | [diff] [blame] | 269 | .map_dmabuf = vb2_vmalloc_map_dmabuf, |
| 270 | .unmap_dmabuf = vb2_vmalloc_unmap_dmabuf, |
| 271 | .attach_dmabuf = vb2_vmalloc_attach_dmabuf, |
| 272 | .detach_dmabuf = vb2_vmalloc_detach_dmabuf, |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 273 | .vaddr = vb2_vmalloc_vaddr, |
| 274 | .mmap = vb2_vmalloc_mmap, |
| 275 | .num_users = vb2_vmalloc_num_users, |
| 276 | }; |
| 277 | EXPORT_SYMBOL_GPL(vb2_vmalloc_memops); |
| 278 | |
| 279 | MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 280 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>"); |
Pawel Osciak | 3c18ff0 | 2010-10-11 10:58:53 -0300 | [diff] [blame] | 281 | MODULE_LICENSE("GPL"); |