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; |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 26 | struct dma_attrs attrs; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | struct vb2_dc_buf { |
Tomasz Stanislawski | 72f86bf | 2012-06-14 10:37:40 -0300 | [diff] [blame] | 30 | struct device *dev; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 31 | void *vaddr; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 32 | unsigned long size; |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 33 | void *cookie; |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 34 | dma_addr_t dma_addr; |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 35 | struct dma_attrs attrs; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 36 | enum dma_data_direction dma_dir; |
| 37 | struct sg_table *dma_sgt; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 38 | struct frame_vector *vec; |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 39 | |
| 40 | /* MMAP related */ |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 41 | struct vb2_vmarea_handler handler; |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 42 | atomic_t refcount; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 43 | struct sg_table *sgt_base; |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 44 | |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 45 | /* DMABUF related */ |
| 46 | struct dma_buf_attachment *db_attach; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 47 | }; |
| 48 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 49 | /*********************************************/ |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 50 | /* scatterlist table functions */ |
| 51 | /*********************************************/ |
| 52 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 53 | static 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 Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 70 | /* callbacks for all buffers */ |
| 71 | /*********************************************/ |
| 72 | |
| 73 | static void *vb2_dc_cookie(void *buf_priv) |
| 74 | { |
| 75 | struct vb2_dc_buf *buf = buf_priv; |
| 76 | |
| 77 | return &buf->dma_addr; |
| 78 | } |
| 79 | |
| 80 | static void *vb2_dc_vaddr(void *buf_priv) |
| 81 | { |
| 82 | struct vb2_dc_buf *buf = buf_priv; |
| 83 | |
Philipp Zabel | 6bbd4fe | 2014-05-26 11:17:32 -0300 | [diff] [blame] | 84 | if (!buf->vaddr && buf->db_attach) |
| 85 | buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf); |
| 86 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 87 | return buf->vaddr; |
| 88 | } |
| 89 | |
| 90 | static 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 Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 97 | static 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 Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 102 | /* DMABUF exporter will flush the cache for us */ |
| 103 | if (!sgt || buf->db_attach) |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 104 | return; |
| 105 | |
Tiffany Lin | d9a9858 | 2015-09-24 06:02:36 -0300 | [diff] [blame] | 106 | dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents, |
| 107 | buf->dma_dir); |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static 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 Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 115 | /* DMABUF exporter will flush the cache for us */ |
| 116 | if (!sgt || buf->db_attach) |
Marek Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 117 | return; |
| 118 | |
Tiffany Lin | d9a9858 | 2015-09-24 06:02:36 -0300 | [diff] [blame] | 119 | 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] | 120 | } |
| 121 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 122 | /*********************************************/ |
| 123 | /* callbacks for MMAP buffers */ |
| 124 | /*********************************************/ |
| 125 | |
| 126 | static 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 Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 133 | if (buf->sgt_base) { |
| 134 | sg_free_table(buf->sgt_base); |
| 135 | kfree(buf->sgt_base); |
| 136 | } |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 137 | dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr, |
| 138 | &buf->attrs); |
Tomasz Stanislawski | 67a5d0c | 2012-08-07 13:19:49 -0300 | [diff] [blame] | 139 | put_device(buf->dev); |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 140 | kfree(buf); |
| 141 | } |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 142 | |
Hans Verkuil | d935c57 | 2014-11-18 09:50:59 -0300 | [diff] [blame] | 143 | static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size, |
| 144 | enum dma_data_direction dma_dir, gfp_t gfp_flags) |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 145 | { |
| 146 | struct vb2_dc_conf *conf = alloc_ctx; |
Tomasz Stanislawski | 72f86bf | 2012-06-14 10:37:40 -0300 | [diff] [blame] | 147 | struct device *dev = conf->dev; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 148 | struct vb2_dc_buf *buf; |
| 149 | |
| 150 | buf = kzalloc(sizeof *buf, GFP_KERNEL); |
| 151 | if (!buf) |
| 152 | return ERR_PTR(-ENOMEM); |
| 153 | |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 154 | 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 Stanislawski | 72f86bf | 2012-06-14 10:37:40 -0300 | [diff] [blame] | 158 | dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 159 | kfree(buf); |
| 160 | return ERR_PTR(-ENOMEM); |
| 161 | } |
| 162 | |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 163 | if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &buf->attrs)) |
| 164 | buf->vaddr = buf->cookie; |
| 165 | |
Tomasz Stanislawski | 67a5d0c | 2012-08-07 13:19:49 -0300 | [diff] [blame] | 166 | /* Prevent the device from being released while the buffer is used */ |
| 167 | buf->dev = get_device(dev); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 168 | buf->size = size; |
Hans Verkuil | d935c57 | 2014-11-18 09:50:59 -0300 | [diff] [blame] | 169 | buf->dma_dir = dma_dir; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 170 | |
| 171 | buf->handler.refcount = &buf->refcount; |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 172 | buf->handler.put = vb2_dc_put; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 173 | buf->handler.arg = buf; |
| 174 | |
| 175 | atomic_inc(&buf->refcount); |
| 176 | |
| 177 | return buf; |
| 178 | } |
| 179 | |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 180 | 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] | 181 | { |
| 182 | struct vb2_dc_buf *buf = buf_priv; |
Marek Szyprowski | c60520f | 2012-06-14 11:32:21 -0300 | [diff] [blame] | 183 | int ret; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 184 | |
| 185 | if (!buf) { |
| 186 | printk(KERN_ERR "No buffer to map\n"); |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | |
Marek Szyprowski | c60520f | 2012-06-14 11:32:21 -0300 | [diff] [blame] | 190 | /* |
| 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 Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 196 | ret = dma_mmap_attrs(buf->dev, vma, buf->cookie, |
| 197 | buf->dma_addr, buf->size, &buf->attrs); |
Marek Szyprowski | c60520f | 2012-06-14 11:32:21 -0300 | [diff] [blame] | 198 | |
| 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 Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 215 | } |
| 216 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 217 | /*********************************************/ |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 218 | /* DMABUF ops for exporters */ |
| 219 | /*********************************************/ |
| 220 | |
| 221 | struct vb2_dc_attachment { |
| 222 | struct sg_table sgt; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 223 | enum dma_data_direction dma_dir; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | static 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 Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 258 | attach->dma_dir = DMA_NONE; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 259 | dbuf_attach->priv = attach; |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static 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 Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 276 | if (attach->dma_dir != DMA_NONE) |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 277 | dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 278 | attach->dma_dir); |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 279 | sg_free_table(sgt); |
| 280 | kfree(attach); |
| 281 | db_attach->priv = NULL; |
| 282 | } |
| 283 | |
| 284 | static struct sg_table *vb2_dc_dmabuf_ops_map( |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 285 | struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir) |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 286 | { |
| 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 Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 291 | |
| 292 | mutex_lock(lock); |
| 293 | |
| 294 | sgt = &attach->sgt; |
| 295 | /* return previously mapped sg table */ |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 296 | if (attach->dma_dir == dma_dir) { |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 297 | mutex_unlock(lock); |
| 298 | return sgt; |
| 299 | } |
| 300 | |
| 301 | /* release any previous cache */ |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 302 | if (attach->dma_dir != DMA_NONE) { |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 303 | dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 304 | attach->dma_dir); |
| 305 | attach->dma_dir = DMA_NONE; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* mapping to the client with new direction */ |
Ricardo Ribalda | 60a4719 | 2015-04-29 09:00:46 -0300 | [diff] [blame] | 309 | sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, |
| 310 | dma_dir); |
| 311 | if (!sgt->nents) { |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 312 | pr_err("failed to map scatterlist\n"); |
| 313 | mutex_unlock(lock); |
| 314 | return ERR_PTR(-EIO); |
| 315 | } |
| 316 | |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 317 | attach->dma_dir = dma_dir; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 318 | |
| 319 | mutex_unlock(lock); |
| 320 | |
| 321 | return sgt; |
| 322 | } |
| 323 | |
| 324 | 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] | 325 | struct sg_table *sgt, enum dma_data_direction dma_dir) |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 326 | { |
| 327 | /* nothing to be done here */ |
| 328 | } |
| 329 | |
| 330 | static 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 | |
| 336 | static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum) |
| 337 | { |
| 338 | struct vb2_dc_buf *buf = dbuf->priv; |
| 339 | |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 340 | return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL; |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static 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 | |
| 350 | static 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 | |
| 356 | static 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 | |
| 368 | static 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 Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 379 | ret = dma_get_sgtable_attrs(buf->dev, sgt, buf->cookie, buf->dma_addr, |
| 380 | buf->size, &buf->attrs); |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 381 | 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 Zabel | c1b96a2 | 2013-05-21 05:11:35 -0300 | [diff] [blame] | 390 | 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] | 391 | { |
| 392 | struct vb2_dc_buf *buf = buf_priv; |
| 393 | struct dma_buf *dbuf; |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 394 | 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 Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 400 | |
| 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 Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 407 | dbuf = dma_buf_export(&exp_info); |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 408 | 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 Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 418 | /* callbacks for USERPTR buffers */ |
| 419 | /*********************************************/ |
| 420 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 421 | static 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 Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 425 | int i; |
| 426 | struct page **pages; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 427 | |
Marek Szyprowski | 774d230 | 2013-06-19 08:56:46 -0300 | [diff] [blame] | 428 | if (sgt) { |
Hans Verkuil | 251a79f | 2014-11-18 09:51:08 -0300 | [diff] [blame] | 429 | 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 Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 438 | 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 Szyprowski | 774d230 | 2013-06-19 08:56:46 -0300 | [diff] [blame] | 443 | sg_free_table(sgt); |
| 444 | kfree(sgt); |
| 445 | } |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 446 | vb2_destroy_framevec(buf->vec); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 447 | kfree(buf); |
| 448 | } |
| 449 | |
Marek Szyprowski | 774d230 | 2013-06-19 08:56:46 -0300 | [diff] [blame] | 450 | /* |
| 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 |
| 459 | static 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) |
| 464 | static 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) |
| 469 | static 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 |
| 474 | static 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 Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 481 | static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 482 | unsigned long size, enum dma_data_direction dma_dir) |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 483 | { |
| 484 | struct vb2_dc_conf *conf = alloc_ctx; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 485 | struct vb2_dc_buf *buf; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 486 | struct frame_vector *vec; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 487 | unsigned long offset; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 488 | int n_pages, i; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 489 | int ret = 0; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 490 | struct sg_table *sgt; |
| 491 | unsigned long contig_size; |
Marek Szyprowski | d81e870 | 2012-06-12 10:18:16 -0300 | [diff] [blame] | 492 | unsigned long dma_align = dma_get_cache_alignment(); |
Hans Verkuil | 251a79f | 2014-11-18 09:51:08 -0300 | [diff] [blame] | 493 | DEFINE_DMA_ATTRS(attrs); |
| 494 | |
| 495 | dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs); |
Marek Szyprowski | d81e870 | 2012-06-12 10:18:16 -0300 | [diff] [blame] | 496 | |
| 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 Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 507 | |
| 508 | buf = kzalloc(sizeof *buf, GFP_KERNEL); |
| 509 | if (!buf) |
| 510 | return ERR_PTR(-ENOMEM); |
| 511 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 512 | buf->dev = conf->dev; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 513 | buf->dma_dir = dma_dir; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 514 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 515 | offset = vaddr & ~PAGE_MASK; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 516 | vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE); |
| 517 | if (IS_ERR(vec)) { |
| 518 | ret = PTR_ERR(vec); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 519 | goto fail_buf; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 520 | } |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 521 | 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 Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 526 | |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 527 | /* |
| 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 Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 536 | } |
| 537 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 538 | sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); |
| 539 | if (!sgt) { |
| 540 | pr_err("failed to allocate sg table\n"); |
| 541 | ret = -ENOMEM; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 542 | goto fail_pfnvec; |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 543 | } |
| 544 | |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 545 | 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] | 546 | offset, size, GFP_KERNEL); |
| 547 | if (ret) { |
| 548 | pr_err("failed to initialize sg table\n"); |
| 549 | goto fail_sgt; |
| 550 | } |
| 551 | |
Hans Verkuil | 251a79f | 2014-11-18 09:51:08 -0300 | [diff] [blame] | 552 | /* |
| 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 Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 558 | 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 Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 573 | buf->dma_sgt = sgt; |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 574 | out: |
| 575 | buf->size = size; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 576 | |
| 577 | return buf; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 578 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 579 | fail_map_sg: |
Hans Verkuil | 251a79f | 2014-11-18 09:51:08 -0300 | [diff] [blame] | 580 | dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, |
| 581 | buf->dma_dir, &attrs); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 582 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 583 | fail_sgt_init: |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 584 | sg_free_table(sgt); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 585 | |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 586 | fail_sgt: |
| 587 | kfree(sgt); |
| 588 | |
Jan Kara | fb639eb | 2015-07-13 11:55:49 -0300 | [diff] [blame] | 589 | fail_pfnvec: |
| 590 | vb2_destroy_framevec(vec); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 591 | |
| 592 | fail_buf: |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 593 | kfree(buf); |
Tomasz Stanislawski | e15dab7 | 2012-06-14 10:37:42 -0300 | [diff] [blame] | 594 | |
| 595 | return ERR_PTR(ret); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 596 | } |
| 597 | |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 598 | /*********************************************/ |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 599 | /* callbacks for DMABUF buffers */ |
| 600 | /*********************************************/ |
| 601 | |
| 602 | static 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 Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 620 | if (IS_ERR(sgt)) { |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 621 | 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 Zabel | 6bbd4fe | 2014-05-26 11:17:32 -0300 | [diff] [blame] | 636 | buf->vaddr = NULL; |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static 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 Zabel | 6bbd4fe | 2014-05-26 11:17:32 -0300 | [diff] [blame] | 656 | if (buf->vaddr) { |
| 657 | dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr); |
| 658 | buf->vaddr = NULL; |
| 659 | } |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 660 | dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir); |
| 661 | |
| 662 | buf->dma_addr = 0; |
| 663 | buf->dma_sgt = NULL; |
| 664 | } |
| 665 | |
| 666 | static 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 | |
| 679 | 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] | 680 | unsigned long size, enum dma_data_direction dma_dir) |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 681 | { |
| 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 Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 702 | buf->dma_dir = dma_dir; |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 703 | buf->size = size; |
| 704 | buf->db_attach = dba; |
| 705 | |
| 706 | return buf; |
| 707 | } |
| 708 | |
| 709 | /*********************************************/ |
Laurent Pinchart | 40d8b76 | 2012-06-14 10:37:41 -0300 | [diff] [blame] | 710 | /* DMA CONTIG exported functions */ |
| 711 | /*********************************************/ |
| 712 | |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 713 | const struct vb2_mem_ops vb2_dma_contig_memops = { |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 714 | .alloc = vb2_dc_alloc, |
| 715 | .put = vb2_dc_put, |
Tomasz Stanislawski | 9ef2cbe | 2012-06-14 11:32:25 -0300 | [diff] [blame] | 716 | .get_dmabuf = vb2_dc_get_dmabuf, |
Laurent Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 717 | .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 Szyprowski | 199d101 | 2012-06-14 10:37:44 -0300 | [diff] [blame] | 722 | .prepare = vb2_dc_prepare, |
| 723 | .finish = vb2_dc_finish, |
Sumit Semwal | 8c417d0 | 2012-06-14 10:37:45 -0300 | [diff] [blame] | 724 | .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 Pinchart | f7f129c | 2012-06-14 10:37:39 -0300 | [diff] [blame] | 728 | .num_users = vb2_dc_num_users, |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 729 | }; |
| 730 | EXPORT_SYMBOL_GPL(vb2_dma_contig_memops); |
| 731 | |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 732 | void *vb2_dma_contig_init_ctx_attrs(struct device *dev, |
| 733 | struct dma_attrs *attrs) |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 734 | { |
| 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 Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 742 | if (attrs) |
| 743 | conf->attrs = *attrs; |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 744 | |
| 745 | return conf; |
| 746 | } |
Tomasz Figa | ccc66e7 | 2016-02-01 22:34:42 +0100 | [diff] [blame] | 747 | EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx_attrs); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 748 | |
| 749 | void vb2_dma_contig_cleanup_ctx(void *alloc_ctx) |
| 750 | { |
Hans Verkuil | e5ae8fa | 2014-11-18 09:51:00 -0300 | [diff] [blame] | 751 | if (!IS_ERR_OR_NULL(alloc_ctx)) |
| 752 | kfree(alloc_ctx); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 753 | } |
| 754 | EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx); |
| 755 | |
| 756 | MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 757 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>"); |
Pawel Osciak | 1a758d4 | 2010-10-11 10:59:36 -0300 | [diff] [blame] | 758 | MODULE_LICENSE("GPL"); |