1 From adde2d6532428cdcaeb60081abb299ce6e5aa76b Mon Sep 17 00:00:00 2001
2 From: John Stultz <john.stultz@linaro.org>
3 Date: Tue, 3 Dec 2019 17:26:38 +0000
4 Subject: [PATCH] dma-buf: heaps: Add heap helpers
6 Commit 5248eb12fea890a03b4cdc3ef546d6319d4d9b73 upstream.
8 Add generic helper dmabuf ops for dma heaps, so we can reduce
9 the amount of duplicative code for the exported dmabufs.
11 This code is an evolution of the Android ION implementation, so
12 thanks to its original authors and maintainters:
13 Rebecca Schultz Zavin, Colin Cross, Laura Abbott, and others!
15 Cc: Laura Abbott <labbott@redhat.com>
16 Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>
17 Cc: Sumit Semwal <sumit.semwal@linaro.org>
18 Cc: Liam Mark <lmark@codeaurora.org>
19 Cc: Pratik Patel <pratikp@codeaurora.org>
20 Cc: Brian Starkey <Brian.Starkey@arm.com>
21 Cc: Vincent Donnefort <Vincent.Donnefort@arm.com>
22 Cc: Sudipto Paul <Sudipto.Paul@arm.com>
23 Cc: Andrew F. Davis <afd@ti.com>
24 Cc: Christoph Hellwig <hch@infradead.org>
25 Cc: Chenbo Feng <fengc@google.com>
26 Cc: Alistair Strachan <astrachan@google.com>
27 Cc: Hridya Valsaraju <hridya@google.com>
28 Cc: Sandeep Patil <sspatil@google.com>
29 Cc: Hillf Danton <hdanton@sina.com>
30 Cc: Dave Airlie <airlied@gmail.com>
31 Cc: dri-devel@lists.freedesktop.org
32 Reviewed-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
33 Reviewed-by: Brian Starkey <brian.starkey@arm.com>
34 Acked-by: Sandeep Patil <sspatil@android.com>
35 Acked-by: Laura Abbott <labbott@redhat.com>
36 Tested-by: Ayan Kumar Halder <ayan.halder@arm.com>
37 Signed-off-by: John Stultz <john.stultz@linaro.org>
38 Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
39 Link: https://patchwork.freedesktop.org/patch/msgid/20191203172641.66642-3-john.stultz@linaro.org
41 drivers/dma-buf/Makefile | 1 +
42 drivers/dma-buf/heaps/Makefile | 2 +
43 drivers/dma-buf/heaps/heap-helpers.c | 271 +++++++++++++++++++++++++++
44 drivers/dma-buf/heaps/heap-helpers.h | 53 ++++++
45 4 files changed, 327 insertions(+)
46 create mode 100644 drivers/dma-buf/heaps/Makefile
47 create mode 100644 drivers/dma-buf/heaps/heap-helpers.c
48 create mode 100644 drivers/dma-buf/heaps/heap-helpers.h
50 --- a/drivers/dma-buf/Makefile
51 +++ b/drivers/dma-buf/Makefile
52 @@ -4,6 +4,7 @@ obj-$(CONFIG_DMA_SHARED_BUFFER) := dma-s
53 dma-buf-objs-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
54 dma-resv.o seqno-fence.o
55 obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
56 +obj-$(CONFIG_DMABUF_HEAPS) += heaps/
57 dma-buf-objs-$(CONFIG_SYNC_FILE) += sync_file.o
58 dma-buf-objs-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o
59 dma-buf-objs-$(CONFIG_UDMABUF) += udmabuf.o
61 +++ b/drivers/dma-buf/heaps/Makefile
63 +# SPDX-License-Identifier: GPL-2.0
64 +obj-y += heap-helpers.o
66 +++ b/drivers/dma-buf/heaps/heap-helpers.c
68 +// SPDX-License-Identifier: GPL-2.0
69 +#include <linux/device.h>
70 +#include <linux/dma-buf.h>
71 +#include <linux/err.h>
72 +#include <linux/highmem.h>
73 +#include <linux/idr.h>
74 +#include <linux/list.h>
75 +#include <linux/slab.h>
76 +#include <linux/uaccess.h>
77 +#include <linux/vmalloc.h>
78 +#include <uapi/linux/dma-heap.h>
80 +#include "heap-helpers.h"
82 +void init_heap_helper_buffer(struct heap_helper_buffer *buffer,
83 + void (*free)(struct heap_helper_buffer *))
85 + buffer->priv_virt = NULL;
86 + mutex_init(&buffer->lock);
87 + buffer->vmap_cnt = 0;
88 + buffer->vaddr = NULL;
89 + buffer->pagecount = 0;
90 + buffer->pages = NULL;
91 + INIT_LIST_HEAD(&buffer->attachments);
92 + buffer->free = free;
95 +struct dma_buf *heap_helper_export_dmabuf(struct heap_helper_buffer *buffer,
98 + DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
100 + exp_info.ops = &heap_helper_ops;
101 + exp_info.size = buffer->size;
102 + exp_info.flags = fd_flags;
103 + exp_info.priv = buffer;
105 + return dma_buf_export(&exp_info);
108 +static void *dma_heap_map_kernel(struct heap_helper_buffer *buffer)
112 + vaddr = vmap(buffer->pages, buffer->pagecount, VM_MAP, PAGE_KERNEL);
114 + return ERR_PTR(-ENOMEM);
119 +static void dma_heap_buffer_destroy(struct heap_helper_buffer *buffer)
121 + if (buffer->vmap_cnt > 0) {
122 + WARN(1, "%s: buffer still mapped in the kernel\n", __func__);
123 + vunmap(buffer->vaddr);
126 + buffer->free(buffer);
129 +static void *dma_heap_buffer_vmap_get(struct heap_helper_buffer *buffer)
133 + if (buffer->vmap_cnt) {
134 + buffer->vmap_cnt++;
135 + return buffer->vaddr;
137 + vaddr = dma_heap_map_kernel(buffer);
140 + buffer->vaddr = vaddr;
141 + buffer->vmap_cnt++;
145 +static void dma_heap_buffer_vmap_put(struct heap_helper_buffer *buffer)
147 + if (!--buffer->vmap_cnt) {
148 + vunmap(buffer->vaddr);
149 + buffer->vaddr = NULL;
153 +struct dma_heaps_attachment {
154 + struct device *dev;
155 + struct sg_table table;
156 + struct list_head list;
159 +static int dma_heap_attach(struct dma_buf *dmabuf,
160 + struct dma_buf_attachment *attachment)
162 + struct dma_heaps_attachment *a;
163 + struct heap_helper_buffer *buffer = dmabuf->priv;
166 + a = kzalloc(sizeof(*a), GFP_KERNEL);
170 + ret = sg_alloc_table_from_pages(&a->table, buffer->pages,
171 + buffer->pagecount, 0,
172 + buffer->pagecount << PAGE_SHIFT,
179 + a->dev = attachment->dev;
180 + INIT_LIST_HEAD(&a->list);
182 + attachment->priv = a;
184 + mutex_lock(&buffer->lock);
185 + list_add(&a->list, &buffer->attachments);
186 + mutex_unlock(&buffer->lock);
191 +static void dma_heap_detach(struct dma_buf *dmabuf,
192 + struct dma_buf_attachment *attachment)
194 + struct dma_heaps_attachment *a = attachment->priv;
195 + struct heap_helper_buffer *buffer = dmabuf->priv;
197 + mutex_lock(&buffer->lock);
198 + list_del(&a->list);
199 + mutex_unlock(&buffer->lock);
201 + sg_free_table(&a->table);
206 +struct sg_table *dma_heap_map_dma_buf(struct dma_buf_attachment *attachment,
207 + enum dma_data_direction direction)
209 + struct dma_heaps_attachment *a = attachment->priv;
210 + struct sg_table *table;
214 + if (!dma_map_sg(attachment->dev, table->sgl, table->nents,
216 + table = ERR_PTR(-ENOMEM);
220 +static void dma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
221 + struct sg_table *table,
222 + enum dma_data_direction direction)
224 + dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction);
227 +static vm_fault_t dma_heap_vm_fault(struct vm_fault *vmf)
229 + struct vm_area_struct *vma = vmf->vma;
230 + struct heap_helper_buffer *buffer = vma->vm_private_data;
232 + if (vmf->pgoff > buffer->pagecount)
233 + return VM_FAULT_SIGBUS;
235 + vmf->page = buffer->pages[vmf->pgoff];
236 + get_page(vmf->page);
241 +static const struct vm_operations_struct dma_heap_vm_ops = {
242 + .fault = dma_heap_vm_fault,
245 +static int dma_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
247 + struct heap_helper_buffer *buffer = dmabuf->priv;
249 + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
252 + vma->vm_ops = &dma_heap_vm_ops;
253 + vma->vm_private_data = buffer;
258 +static void dma_heap_dma_buf_release(struct dma_buf *dmabuf)
260 + struct heap_helper_buffer *buffer = dmabuf->priv;
262 + dma_heap_buffer_destroy(buffer);
265 +static int dma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
266 + enum dma_data_direction direction)
268 + struct heap_helper_buffer *buffer = dmabuf->priv;
269 + struct dma_heaps_attachment *a;
272 + mutex_lock(&buffer->lock);
274 + if (buffer->vmap_cnt)
275 + invalidate_kernel_vmap_range(buffer->vaddr, buffer->size);
277 + list_for_each_entry(a, &buffer->attachments, list) {
278 + dma_sync_sg_for_cpu(a->dev, a->table.sgl, a->table.nents,
281 + mutex_unlock(&buffer->lock);
286 +static int dma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
287 + enum dma_data_direction direction)
289 + struct heap_helper_buffer *buffer = dmabuf->priv;
290 + struct dma_heaps_attachment *a;
292 + mutex_lock(&buffer->lock);
294 + if (buffer->vmap_cnt)
295 + flush_kernel_vmap_range(buffer->vaddr, buffer->size);
297 + list_for_each_entry(a, &buffer->attachments, list) {
298 + dma_sync_sg_for_device(a->dev, a->table.sgl, a->table.nents,
301 + mutex_unlock(&buffer->lock);
306 +static void *dma_heap_dma_buf_vmap(struct dma_buf *dmabuf)
308 + struct heap_helper_buffer *buffer = dmabuf->priv;
311 + mutex_lock(&buffer->lock);
312 + vaddr = dma_heap_buffer_vmap_get(buffer);
313 + mutex_unlock(&buffer->lock);
318 +static void dma_heap_dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
320 + struct heap_helper_buffer *buffer = dmabuf->priv;
322 + mutex_lock(&buffer->lock);
323 + dma_heap_buffer_vmap_put(buffer);
324 + mutex_unlock(&buffer->lock);
327 +const struct dma_buf_ops heap_helper_ops = {
328 + .map_dma_buf = dma_heap_map_dma_buf,
329 + .unmap_dma_buf = dma_heap_unmap_dma_buf,
330 + .mmap = dma_heap_mmap,
331 + .release = dma_heap_dma_buf_release,
332 + .attach = dma_heap_attach,
333 + .detach = dma_heap_detach,
334 + .begin_cpu_access = dma_heap_dma_buf_begin_cpu_access,
335 + .end_cpu_access = dma_heap_dma_buf_end_cpu_access,
336 + .vmap = dma_heap_dma_buf_vmap,
337 + .vunmap = dma_heap_dma_buf_vunmap,
340 +++ b/drivers/dma-buf/heaps/heap-helpers.h
342 +/* SPDX-License-Identifier: GPL-2.0 */
344 + * DMABUF Heaps helper code
346 + * Copyright (C) 2011 Google, Inc.
347 + * Copyright (C) 2019 Linaro Ltd.
350 +#ifndef _HEAP_HELPERS_H
351 +#define _HEAP_HELPERS_H
353 +#include <linux/dma-heap.h>
354 +#include <linux/list.h>
357 + * struct heap_helper_buffer - helper buffer metadata
358 + * @heap: back pointer to the heap the buffer came from
359 + * @dmabuf: backing dma-buf for this buffer
360 + * @size: size of the buffer
361 + * @priv_virt pointer to heap specific private value
362 + * @lock mutext to protect the data in this structure
363 + * @vmap_cnt count of vmap references on the buffer
364 + * @vaddr vmap'ed virtual address
365 + * @pagecount number of pages in the buffer
366 + * @pages list of page pointers
367 + * @attachments list of device attachments
369 + * @free heap callback to free the buffer
371 +struct heap_helper_buffer {
372 + struct dma_heap *heap;
373 + struct dma_buf *dmabuf;
381 + struct page **pages;
382 + struct list_head attachments;
384 + void (*free)(struct heap_helper_buffer *buffer);
387 +void init_heap_helper_buffer(struct heap_helper_buffer *buffer,
388 + void (*free)(struct heap_helper_buffer *));
390 +struct dma_buf *heap_helper_export_dmabuf(struct heap_helper_buffer *buffer,
393 +extern const struct dma_buf_ops heap_helper_ops;
394 +#endif /* _HEAP_HELPERS_H */