Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 1 | /* |
| 2 | * videobuf2-dma-contig.c - DMA contig 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 | 1a758d4 | 2010-10-11 10:59:36 -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 | |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 13 | #include <linux/dma-buf.h> |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 14 | #include <linux/module.h> |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
| 16 | #include <linux/sched.h> |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 20 | #include <media/videobuf2-v4l2.h> |
H Hartley Sweeten | d0df3c3 | 2012-04-24 19:08:12 -0300 | [diff] [blame] | 21 | #include <media/videobuf2-dma-contig.h> |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 22 | #include <media/videobuf2-memops.h> |
| 23 | |
| 24 | struct vb2_dc_conf { |
| 25 | struct device *dev; |
| 26 | }; |
| 27 | |
| 28 | struct vb2_dc_buf { |
Tomasz Stanislawski | 72f86bf | 2012-06-14 10:37:40 -0300 | [diff] [blame] | 29 | struct device *dev; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 30 | void *vaddr; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 31 | unsigned long size; |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 32 | dma_addr_t dma_addr; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 33 | enum dma_data_direction dma_dir; |
| 34 | struct sg_table *dma_sgt; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 35 | struct frame_vector *vec; |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 36 | |
| 37 | /* MMAP related */ |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 38 | struct vb2_vmarea_handler handler; |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 39 | atomic_t refcount; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 40 | struct sg_table *sgt_base; |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 41 | |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 42 | /* DMABUF related */ |
| 43 | struct dma_buf_attachment *db_attach; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 44 | }; |
| 45 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 46 | /*********************************************/ |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 47 | /* scatterlist table functions */ |
| 48 | /*********************************************/ |
| 49 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 50 | static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt) |
| 51 | { |
| 52 | struct scatterlist *s; |
| 53 | dma_addr_t expected = sg_dma_address(sgt->sgl); |
| 54 | unsigned int i; |
| 55 | unsigned long size = 0; |
| 56 | |
| 57 | for_each_sg(sgt->sgl, s, sgt->nents, i) { |
| 58 | if (sg_dma_address(s) != expected) |
| 59 | break; |
| 60 | expected = sg_dma_address(s) + sg_dma_len(s); |
| 61 | size += sg_dma_len(s); |
| 62 | } |
| 63 | return size; |
| 64 | } |
| 65 | |
| 66 | /*********************************************/ |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 67 | /* callbacks for all buffers */ |
| 68 | /*********************************************/ |
| 69 | |
| 70 | static void *vb2_dc_cookie(void *buf_priv) |
| 71 | { |
| 72 | struct vb2_dc_buf *buf = buf_priv; |
| 73 | |
| 74 | return &buf->dma_addr; |
| 75 | } |
| 76 | |
| 77 | static void *vb2_dc_vaddr(void *buf_priv) |
| 78 | { |
| 79 | struct vb2_dc_buf *buf = buf_priv; |
| 80 | |
Philipp Zabel | 6bbd4fe | 2014-05-26 11:17:32 -0300 | [diff] [blame] | 81 | if (!buf->vaddr && buf->db_attach) |
| 82 | buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf); |
| 83 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 84 | return buf->vaddr; |
| 85 | } |
| 86 | |
| 87 | static unsigned int vb2_dc_num_users(void *buf_priv) |
| 88 | { |
| 89 | struct vb2_dc_buf *buf = buf_priv; |
| 90 | |
| 91 | return atomic_read(&buf->refcount); |
| 92 | } |
| 93 | |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 94 | static void vb2_dc_prepare(void *buf_priv) |
| 95 | { |
| 96 | struct vb2_dc_buf *buf = buf_priv; |
| 97 | struct sg_table *sgt = buf->dma_sgt; |
| 98 | |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 99 | /* DMABUF exporter will flush the cache for us */ |
| 100 | if (!sgt || buf->db_attach) |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 101 | return; |
| 102 | |
Tiffany Lin | d9a9858 | 2015-09-24 06:02:36 -0300 | [diff] [blame] | 103 | dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents, |
| 104 | buf->dma_dir); |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void vb2_dc_finish(void *buf_priv) |
| 108 | { |
| 109 | struct vb2_dc_buf *buf = buf_priv; |
| 110 | struct sg_table *sgt = buf->dma_sgt; |
| 111 | |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 112 | /* DMABUF exporter will flush the cache for us */ |
| 113 | if (!sgt || buf->db_attach) |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 114 | return; |
| 115 | |
Tiffany Lin | d9a9858 | 2015-09-24 06:02:36 -0300 | [diff] [blame] | 116 | dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 117 | } |
| 118 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 119 | /*********************************************/ |
| 120 | /* callbacks for MMAP buffers */ |
| 121 | /*********************************************/ |
| 122 | |
| 123 | static void vb2_dc_put(void *buf_priv) |
| 124 | { |
| 125 | struct vb2_dc_buf *buf = buf_priv; |
| 126 | |
| 127 | if (!atomic_dec_and_test(&buf->refcount)) |
| 128 | return; |
| 129 | |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 130 | if (buf->sgt_base) { |
| 131 | sg_free_table(buf->sgt_base); |
| 132 | kfree(buf->sgt_base); |
| 133 | } |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 134 | dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr); |
Tomasz Stanislawski | 67a5d0c | 2012-08-07 13:19:49 -0300 | [diff] [blame] | 135 | put_device(buf->dev); |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 136 | kfree(buf); |
| 137 | } |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 138 | |
Hans Verkuil | d935c57 | 2014-11-18 09:50:59 -0300 | [diff] [blame] | 139 | static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size, |
| 140 | enum dma_data_direction dma_dir, gfp_t gfp_flags) |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 141 | { |
| 142 | struct vb2_dc_conf *conf = alloc_ctx; |
Tomasz Stanislawski | 72f86bf | 2012-06-14 10:37:40 -0300 | [diff] [blame] | 143 | struct device *dev = conf->dev; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 144 | struct vb2_dc_buf *buf; |
| 145 | |
| 146 | buf = kzalloc(sizeof *buf, GFP_KERNEL); |
| 147 | if (!buf) |
| 148 | return ERR_PTR(-ENOMEM); |
| 149 | |
Hans Verkuil | b6ba205 | 2013-03-01 15:44:20 -0300 | [diff] [blame] | 150 | buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, |
| 151 | GFP_KERNEL | gfp_flags); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 152 | if (!buf->vaddr) { |
Tomasz Stanislawski | 72f86bf | 2012-06-14 10:37:40 -0300 | [diff] [blame] | 153 | dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 154 | kfree(buf); |
| 155 | return ERR_PTR(-ENOMEM); |
| 156 | } |
| 157 | |
Tomasz Stanislawski | 67a5d0c | 2012-08-07 13:19:49 -0300 | [diff] [blame] | 158 | /* Prevent the device from being released while the buffer is used */ |
| 159 | buf->dev = get_device(dev); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 160 | buf->size = size; |
Hans Verkuil | d935c57 | 2014-11-18 09:50:59 -0300 | [diff] [blame] | 161 | buf->dma_dir = dma_dir; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 162 | |
| 163 | buf->handler.refcount = &buf->refcount; |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 164 | buf->handler.put = vb2_dc_put; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 165 | buf->handler.arg = buf; |
| 166 | |
| 167 | atomic_inc(&buf->refcount); |
| 168 | |
| 169 | return buf; |
| 170 | } |
| 171 | |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 172 | static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma) |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 173 | { |
| 174 | struct vb2_dc_buf *buf = buf_priv; |
Marek Szyprowski | c60520f | 2012-06-14 11:32:21 -0300 | [diff] [blame] | 175 | int ret; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 176 | |
| 177 | if (!buf) { |
| 178 | printk(KERN_ERR "No buffer to map\n"); |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
Marek Szyprowski | c60520f | 2012-06-14 11:32:21 -0300 | [diff] [blame] | 182 | /* |
| 183 | * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to |
| 184 | * map whole buffer |
| 185 | */ |
| 186 | vma->vm_pgoff = 0; |
| 187 | |
| 188 | ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr, |
| 189 | buf->dma_addr, buf->size); |
| 190 | |
| 191 | if (ret) { |
| 192 | pr_err("Remapping memory failed, error: %d\n", ret); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
| 197 | vma->vm_private_data = &buf->handler; |
| 198 | vma->vm_ops = &vb2_common_vm_ops; |
| 199 | |
| 200 | vma->vm_ops->open(vma); |
| 201 | |
| 202 | pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n", |
| 203 | __func__, (unsigned long)buf->dma_addr, vma->vm_start, |
| 204 | buf->size); |
| 205 | |
| 206 | return 0; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 207 | } |
| 208 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 209 | /*********************************************/ |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 210 | /* DMABUF ops for exporters */ |
| 211 | /*********************************************/ |
| 212 | |
| 213 | struct vb2_dc_attachment { |
| 214 | struct sg_table sgt; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 215 | enum dma_data_direction dma_dir; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev, |
| 219 | struct dma_buf_attachment *dbuf_attach) |
| 220 | { |
| 221 | struct vb2_dc_attachment *attach; |
| 222 | unsigned int i; |
| 223 | struct scatterlist *rd, *wr; |
| 224 | struct sg_table *sgt; |
| 225 | struct vb2_dc_buf *buf = dbuf->priv; |
| 226 | int ret; |
| 227 | |
| 228 | attach = kzalloc(sizeof(*attach), GFP_KERNEL); |
| 229 | if (!attach) |
| 230 | return -ENOMEM; |
| 231 | |
| 232 | sgt = &attach->sgt; |
| 233 | /* Copy the buf->base_sgt scatter list to the attachment, as we can't |
| 234 | * map the same scatter list to multiple attachments at the same time. |
| 235 | */ |
| 236 | ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL); |
| 237 | if (ret) { |
| 238 | kfree(attach); |
| 239 | return -ENOMEM; |
| 240 | } |
| 241 | |
| 242 | rd = buf->sgt_base->sgl; |
| 243 | wr = sgt->sgl; |
| 244 | for (i = 0; i < sgt->orig_nents; ++i) { |
| 245 | sg_set_page(wr, sg_page(rd), rd->length, rd->offset); |
| 246 | rd = sg_next(rd); |
| 247 | wr = sg_next(wr); |
| 248 | } |
| 249 | |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 250 | attach->dma_dir = DMA_NONE; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 251 | dbuf_attach->priv = attach; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf, |
| 257 | struct dma_buf_attachment *db_attach) |
| 258 | { |
| 259 | struct vb2_dc_attachment *attach = db_attach->priv; |
| 260 | struct sg_table *sgt; |
| 261 | |
| 262 | if (!attach) |
| 263 | return; |
| 264 | |
| 265 | sgt = &attach->sgt; |
| 266 | |
| 267 | /* release the scatterlist cache */ |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 268 | if (attach->dma_dir != DMA_NONE) |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 269 | dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 270 | attach->dma_dir); |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 271 | sg_free_table(sgt); |
| 272 | kfree(attach); |
| 273 | db_attach->priv = NULL; |
| 274 | } |
| 275 | |
| 276 | static struct sg_table *vb2_dc_dmabuf_ops_map( |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 277 | struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir) |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 278 | { |
| 279 | struct vb2_dc_attachment *attach = db_attach->priv; |
| 280 | /* stealing dmabuf mutex to serialize map/unmap operations */ |
| 281 | struct mutex *lock = &db_attach->dmabuf->lock; |
| 282 | struct sg_table *sgt; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 283 | |
| 284 | mutex_lock(lock); |
| 285 | |
| 286 | sgt = &attach->sgt; |
| 287 | /* return previously mapped sg table */ |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 288 | if (attach->dma_dir == dma_dir) { |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 289 | mutex_unlock(lock); |
| 290 | return sgt; |
| 291 | } |
| 292 | |
| 293 | /* release any previous cache */ |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 294 | if (attach->dma_dir != DMA_NONE) { |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 295 | dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 296 | attach->dma_dir); |
| 297 | attach->dma_dir = DMA_NONE; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* mapping to the client with new direction */ |
Ricardo Ribalda | 60a4719 | 2015-04-29 09:00:46 -0300 | [diff] [blame] | 301 | sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, |
| 302 | dma_dir); |
| 303 | if (!sgt->nents) { |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 304 | pr_err("failed to map scatterlist\n"); |
| 305 | mutex_unlock(lock); |
| 306 | return ERR_PTR(-EIO); |
| 307 | } |
| 308 | |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 309 | attach->dma_dir = dma_dir; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 310 | |
| 311 | mutex_unlock(lock); |
| 312 | |
| 313 | return sgt; |
| 314 | } |
| 315 | |
| 316 | static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 317 | struct sg_table *sgt, enum dma_data_direction dma_dir) |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 318 | { |
| 319 | /* nothing to be done here */ |
| 320 | } |
| 321 | |
| 322 | static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf) |
| 323 | { |
| 324 | /* drop reference obtained in vb2_dc_get_dmabuf */ |
| 325 | vb2_dc_put(dbuf->priv); |
| 326 | } |
| 327 | |
| 328 | static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum) |
| 329 | { |
| 330 | struct vb2_dc_buf *buf = dbuf->priv; |
| 331 | |
| 332 | return buf->vaddr + pgnum * PAGE_SIZE; |
| 333 | } |
| 334 | |
| 335 | static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf) |
| 336 | { |
| 337 | struct vb2_dc_buf *buf = dbuf->priv; |
| 338 | |
| 339 | return buf->vaddr; |
| 340 | } |
| 341 | |
| 342 | static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf, |
| 343 | struct vm_area_struct *vma) |
| 344 | { |
| 345 | return vb2_dc_mmap(dbuf->priv, vma); |
| 346 | } |
| 347 | |
| 348 | static struct dma_buf_ops vb2_dc_dmabuf_ops = { |
| 349 | .attach = vb2_dc_dmabuf_ops_attach, |
| 350 | .detach = vb2_dc_dmabuf_ops_detach, |
| 351 | .map_dma_buf = vb2_dc_dmabuf_ops_map, |
| 352 | .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap, |
| 353 | .kmap = vb2_dc_dmabuf_ops_kmap, |
| 354 | .kmap_atomic = vb2_dc_dmabuf_ops_kmap, |
| 355 | .vmap = vb2_dc_dmabuf_ops_vmap, |
| 356 | .mmap = vb2_dc_dmabuf_ops_mmap, |
| 357 | .release = vb2_dc_dmabuf_ops_release, |
| 358 | }; |
| 359 | |
| 360 | static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf) |
| 361 | { |
| 362 | int ret; |
| 363 | struct sg_table *sgt; |
| 364 | |
| 365 | sgt = kmalloc(sizeof(*sgt), GFP_KERNEL); |
| 366 | if (!sgt) { |
| 367 | dev_err(buf->dev, "failed to alloc sg table\n"); |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr, |
| 372 | buf->size); |
| 373 | if (ret < 0) { |
| 374 | dev_err(buf->dev, "failed to get scatterlist from DMA API\n"); |
| 375 | kfree(sgt); |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | return sgt; |
| 380 | } |
| 381 | |
Philipp Zabel | c1b96a2 | 2013-05-21 05:11:35 -0300 | [diff] [blame] | 382 | static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags) |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 383 | { |
| 384 | struct vb2_dc_buf *buf = buf_priv; |
| 385 | struct dma_buf *dbuf; |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 386 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 387 | |
| 388 | exp_info.ops = &vb2_dc_dmabuf_ops; |
| 389 | exp_info.size = buf->size; |
| 390 | exp_info.flags = flags; |
| 391 | exp_info.priv = buf; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 392 | |
| 393 | if (!buf->sgt_base) |
| 394 | buf->sgt_base = vb2_dc_get_base_sgt(buf); |
| 395 | |
| 396 | if (WARN_ON(!buf->sgt_base)) |
| 397 | return NULL; |
| 398 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 399 | dbuf = dma_buf_export(&exp_info); |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 400 | if (IS_ERR(dbuf)) |
| 401 | return NULL; |
| 402 | |
| 403 | /* dmabuf keeps reference to vb2 buffer */ |
| 404 | atomic_inc(&buf->refcount); |
| 405 | |
| 406 | return dbuf; |
| 407 | } |
| 408 | |
| 409 | /*********************************************/ |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 410 | /* callbacks for USERPTR buffers */ |
| 411 | /*********************************************/ |
| 412 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 413 | static void vb2_dc_put_userptr(void *buf_priv) |
| 414 | { |
| 415 | struct vb2_dc_buf *buf = buf_priv; |
| 416 | struct sg_table *sgt = buf->dma_sgt; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 417 | int i; |
| 418 | struct page **pages; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 419 | |
Marek Szyprowski | 774d230 | 2013-06-19 08:56:46 -0300 | [diff] [blame] | 420 | if (sgt) { |
Hans Verkuil | 251a79f | 2014-11-18 09:51:08 -0300 | [diff] [blame] | 421 | DEFINE_DMA_ATTRS(attrs); |
| 422 | |
| 423 | dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs); |
| 424 | /* |
| 425 | * No need to sync to CPU, it's already synced to the CPU |
| 426 | * since the finish() memop will have been called before this. |
| 427 | */ |
| 428 | dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, |
| 429 | buf->dma_dir, &attrs); |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 430 | pages = frame_vector_pages(buf->vec); |
| 431 | /* sgt should exist only if vector contains pages... */ |
| 432 | BUG_ON(IS_ERR(pages)); |
| 433 | for (i = 0; i < frame_vector_count(buf->vec); i++) |
| 434 | set_page_dirty_lock(pages[i]); |
Marek Szyprowski | 774d230 | 2013-06-19 08:56:46 -0300 | [diff] [blame] | 435 | sg_free_table(sgt); |
| 436 | kfree(sgt); |
| 437 | } |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 438 | vb2_destroy_framevec(buf->vec); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 439 | kfree(buf); |
| 440 | } |
| 441 | |
Marek Szyprowski | 774d230 | 2013-06-19 08:56:46 -0300 | [diff] [blame] | 442 | /* |
| 443 | * For some kind of reserved memory there might be no struct page available, |
| 444 | * so all that can be done to support such 'pages' is to try to convert |
| 445 | * pfn to dma address or at the last resort just assume that |
| 446 | * dma address == physical address (like it has been assumed in earlier version |
| 447 | * of videobuf2-dma-contig |
| 448 | */ |
| 449 | |
| 450 | #ifdef __arch_pfn_to_dma |
| 451 | static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn) |
| 452 | { |
| 453 | return (dma_addr_t)__arch_pfn_to_dma(dev, pfn); |
| 454 | } |
| 455 | #elif defined(__pfn_to_bus) |
| 456 | static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn) |
| 457 | { |
| 458 | return (dma_addr_t)__pfn_to_bus(pfn); |
| 459 | } |
| 460 | #elif defined(__pfn_to_phys) |
| 461 | static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn) |
| 462 | { |
| 463 | return (dma_addr_t)__pfn_to_phys(pfn); |
| 464 | } |
| 465 | #else |
| 466 | static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn) |
| 467 | { |
| 468 | /* really, we cannot do anything better at this point */ |
| 469 | return (dma_addr_t)(pfn) << PAGE_SHIFT; |
| 470 | } |
| 471 | #endif |
| 472 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 473 | static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 474 | unsigned long size, enum dma_data_direction dma_dir) |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 475 | { |
| 476 | struct vb2_dc_conf *conf = alloc_ctx; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 477 | struct vb2_dc_buf *buf; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 478 | struct frame_vector *vec; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 479 | unsigned long offset; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 480 | int n_pages, i; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 481 | int ret = 0; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 482 | struct sg_table *sgt; |
| 483 | unsigned long contig_size; |
Marek Szyprowski | d81e870 | 2012-06-12 10:18:16 -0300 | [diff] [blame] | 484 | unsigned long dma_align = dma_get_cache_alignment(); |
Hans Verkuil | 251a79f | 2014-11-18 09:51:08 -0300 | [diff] [blame] | 485 | DEFINE_DMA_ATTRS(attrs); |
| 486 | |
| 487 | dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs); |
Marek Szyprowski | d81e870 | 2012-06-12 10:18:16 -0300 | [diff] [blame] | 488 | |
| 489 | /* Only cache aligned DMA transfers are reliable */ |
| 490 | if (!IS_ALIGNED(vaddr | size, dma_align)) { |
| 491 | pr_debug("user data must be aligned to %lu bytes\n", dma_align); |
| 492 | return ERR_PTR(-EINVAL); |
| 493 | } |
| 494 | |
| 495 | if (!size) { |
| 496 | pr_debug("size is zero\n"); |
| 497 | return ERR_PTR(-EINVAL); |
| 498 | } |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 499 | |
| 500 | buf = kzalloc(sizeof *buf, GFP_KERNEL); |
| 501 | if (!buf) |
| 502 | return ERR_PTR(-ENOMEM); |
| 503 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 504 | buf->dev = conf->dev; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 505 | buf->dma_dir = dma_dir; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 506 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 507 | offset = vaddr & ~PAGE_MASK; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 508 | vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE); |
| 509 | if (IS_ERR(vec)) { |
| 510 | ret = PTR_ERR(vec); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 511 | goto fail_buf; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 512 | } |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 513 | buf->vec = vec; |
| 514 | n_pages = frame_vector_count(vec); |
| 515 | ret = frame_vector_to_pages(vec); |
| 516 | if (ret < 0) { |
| 517 | unsigned long *nums = frame_vector_pfns(vec); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 518 | |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 519 | /* |
| 520 | * Failed to convert to pages... Check the memory is physically |
| 521 | * contiguous and use direct mapping |
| 522 | */ |
| 523 | for (i = 1; i < n_pages; i++) |
| 524 | if (nums[i-1] + 1 != nums[i]) |
| 525 | goto fail_pfnvec; |
| 526 | buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, nums[0]); |
| 527 | goto out; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 528 | } |
| 529 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 530 | sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); |
| 531 | if (!sgt) { |
| 532 | pr_err("failed to allocate sg table\n"); |
| 533 | ret = -ENOMEM; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 534 | goto fail_pfnvec; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 535 | } |
| 536 | |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 537 | ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages, |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 538 | offset, size, GFP_KERNEL); |
| 539 | if (ret) { |
| 540 | pr_err("failed to initialize sg table\n"); |
| 541 | goto fail_sgt; |
| 542 | } |
| 543 | |
Hans Verkuil | 251a79f | 2014-11-18 09:51:08 -0300 | [diff] [blame] | 544 | /* |
| 545 | * No need to sync to the device, this will happen later when the |
| 546 | * prepare() memop is called. |
| 547 | */ |
| 548 | sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, |
| 549 | buf->dma_dir, &attrs); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 550 | if (sgt->nents <= 0) { |
| 551 | pr_err("failed to map scatterlist\n"); |
| 552 | ret = -EIO; |
| 553 | goto fail_sgt_init; |
| 554 | } |
| 555 | |
| 556 | contig_size = vb2_dc_get_contiguous_size(sgt); |
| 557 | if (contig_size < size) { |
| 558 | pr_err("contiguous mapping is too small %lu/%lu\n", |
| 559 | contig_size, size); |
| 560 | ret = -EFAULT; |
| 561 | goto fail_map_sg; |
| 562 | } |
| 563 | |
| 564 | buf->dma_addr = sg_dma_address(sgt->sgl); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 565 | buf->dma_sgt = sgt; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 566 | out: |
| 567 | buf->size = size; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 568 | |
| 569 | return buf; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 570 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 571 | fail_map_sg: |
Hans Verkuil | 251a79f | 2014-11-18 09:51:08 -0300 | [diff] [blame] | 572 | dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, |
| 573 | buf->dma_dir, &attrs); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 574 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 575 | fail_sgt_init: |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 576 | sg_free_table(sgt); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 577 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 578 | fail_sgt: |
| 579 | kfree(sgt); |
| 580 | |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 581 | fail_pfnvec: |
| 582 | vb2_destroy_framevec(vec); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 583 | |
| 584 | fail_buf: |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 585 | kfree(buf); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 586 | |
| 587 | return ERR_PTR(ret); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 588 | } |
| 589 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 590 | /*********************************************/ |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 591 | /* callbacks for DMABUF buffers */ |
| 592 | /*********************************************/ |
| 593 | |
| 594 | static int vb2_dc_map_dmabuf(void *mem_priv) |
| 595 | { |
| 596 | struct vb2_dc_buf *buf = mem_priv; |
| 597 | struct sg_table *sgt; |
| 598 | unsigned long contig_size; |
| 599 | |
| 600 | if (WARN_ON(!buf->db_attach)) { |
| 601 | pr_err("trying to pin a non attached buffer\n"); |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | if (WARN_ON(buf->dma_sgt)) { |
| 606 | pr_err("dmabuf buffer is already pinned\n"); |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /* get the associated scatterlist for this buffer */ |
| 611 | sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir); |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 612 | if (IS_ERR(sgt)) { |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 613 | pr_err("Error getting dmabuf scatterlist\n"); |
| 614 | return -EINVAL; |
| 615 | } |
| 616 | |
| 617 | /* checking if dmabuf is big enough to store contiguous chunk */ |
| 618 | contig_size = vb2_dc_get_contiguous_size(sgt); |
| 619 | if (contig_size < buf->size) { |
| 620 | pr_err("contiguous chunk is too small %lu/%lu b\n", |
| 621 | contig_size, buf->size); |
| 622 | dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir); |
| 623 | return -EFAULT; |
| 624 | } |
| 625 | |
| 626 | buf->dma_addr = sg_dma_address(sgt->sgl); |
| 627 | buf->dma_sgt = sgt; |
Philipp Zabel | 6bbd4fe | 2014-05-26 11:17:32 -0300 | [diff] [blame] | 628 | buf->vaddr = NULL; |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | static void vb2_dc_unmap_dmabuf(void *mem_priv) |
| 634 | { |
| 635 | struct vb2_dc_buf *buf = mem_priv; |
| 636 | struct sg_table *sgt = buf->dma_sgt; |
| 637 | |
| 638 | if (WARN_ON(!buf->db_attach)) { |
| 639 | pr_err("trying to unpin a not attached buffer\n"); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | if (WARN_ON(!sgt)) { |
| 644 | pr_err("dmabuf buffer is already unpinned\n"); |
| 645 | return; |
| 646 | } |
| 647 | |
Philipp Zabel | 6bbd4fe | 2014-05-26 11:17:32 -0300 | [diff] [blame] | 648 | if (buf->vaddr) { |
| 649 | dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr); |
| 650 | buf->vaddr = NULL; |
| 651 | } |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 652 | dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir); |
| 653 | |
| 654 | buf->dma_addr = 0; |
| 655 | buf->dma_sgt = NULL; |
| 656 | } |
| 657 | |
| 658 | static void vb2_dc_detach_dmabuf(void *mem_priv) |
| 659 | { |
| 660 | struct vb2_dc_buf *buf = mem_priv; |
| 661 | |
| 662 | /* if vb2 works correctly you should never detach mapped buffer */ |
| 663 | if (WARN_ON(buf->dma_addr)) |
| 664 | vb2_dc_unmap_dmabuf(buf); |
| 665 | |
| 666 | /* detach this attachment */ |
| 667 | dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach); |
| 668 | kfree(buf); |
| 669 | } |
| 670 | |
| 671 | static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 672 | unsigned long size, enum dma_data_direction dma_dir) |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 673 | { |
| 674 | struct vb2_dc_conf *conf = alloc_ctx; |
| 675 | struct vb2_dc_buf *buf; |
| 676 | struct dma_buf_attachment *dba; |
| 677 | |
| 678 | if (dbuf->size < size) |
| 679 | return ERR_PTR(-EFAULT); |
| 680 | |
| 681 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
| 682 | if (!buf) |
| 683 | return ERR_PTR(-ENOMEM); |
| 684 | |
| 685 | buf->dev = conf->dev; |
| 686 | /* create attachment for the dmabuf with the user device */ |
| 687 | dba = dma_buf_attach(dbuf, buf->dev); |
| 688 | if (IS_ERR(dba)) { |
| 689 | pr_err("failed to attach dmabuf\n"); |
| 690 | kfree(buf); |
| 691 | return dba; |
| 692 | } |
| 693 | |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 694 | buf->dma_dir = dma_dir; |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 695 | buf->size = size; |
| 696 | buf->db_attach = dba; |
| 697 | |
| 698 | return buf; |
| 699 | } |
| 700 | |
| 701 | /*********************************************/ |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 702 | /* DMA CONTIG exported functions */ |
| 703 | /*********************************************/ |
| 704 | |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 705 | const struct vb2_mem_ops vb2_dma_contig_memops = { |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 706 | .alloc = vb2_dc_alloc, |
| 707 | .put = vb2_dc_put, |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 708 | .get_dmabuf = vb2_dc_get_dmabuf, |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 709 | .cookie = vb2_dc_cookie, |
| 710 | .vaddr = vb2_dc_vaddr, |
| 711 | .mmap = vb2_dc_mmap, |
| 712 | .get_userptr = vb2_dc_get_userptr, |
| 713 | .put_userptr = vb2_dc_put_userptr, |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 714 | .prepare = vb2_dc_prepare, |
| 715 | .finish = vb2_dc_finish, |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 716 | .map_dmabuf = vb2_dc_map_dmabuf, |
| 717 | .unmap_dmabuf = vb2_dc_unmap_dmabuf, |
| 718 | .attach_dmabuf = vb2_dc_attach_dmabuf, |
| 719 | .detach_dmabuf = vb2_dc_detach_dmabuf, |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 720 | .num_users = vb2_dc_num_users, |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 721 | }; |
| 722 | EXPORT_SYMBOL_GPL(vb2_dma_contig_memops); |
| 723 | |
| 724 | void *vb2_dma_contig_init_ctx(struct device *dev) |
| 725 | { |
| 726 | struct vb2_dc_conf *conf; |
| 727 | |
| 728 | conf = kzalloc(sizeof *conf, GFP_KERNEL); |
| 729 | if (!conf) |
| 730 | return ERR_PTR(-ENOMEM); |
| 731 | |
| 732 | conf->dev = dev; |
| 733 | |
| 734 | return conf; |
| 735 | } |
| 736 | EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx); |
| 737 | |
| 738 | void vb2_dma_contig_cleanup_ctx(void *alloc_ctx) |
| 739 | { |
Hans Verkuil | e5ae8fa | 2014-11-18 09:51:00 -0300 | [diff] [blame] | 740 | if (!IS_ERR_OR_NULL(alloc_ctx)) |
| 741 | kfree(alloc_ctx); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 742 | } |
| 743 | EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx); |
| 744 | |
| 745 | MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 746 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>"); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 747 | MODULE_LICENSE("GPL"); |