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