bcm27xx: update patches from RPi foundation
[oweals/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0733-dma-buf-heaps-Add-heap-helpers.patch
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
5
6 Commit 5248eb12fea890a03b4cdc3ef546d6319d4d9b73 upstream.
7
8 Add generic helper dmabuf ops for dma heaps, so we can reduce
9 the amount of duplicative code for the exported dmabufs.
10
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!
14
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
40 ---
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
49
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
60 --- /dev/null
61 +++ b/drivers/dma-buf/heaps/Makefile
62 @@ -0,0 +1,2 @@
63 +# SPDX-License-Identifier: GPL-2.0
64 +obj-y                                  += heap-helpers.o
65 --- /dev/null
66 +++ b/drivers/dma-buf/heaps/heap-helpers.c
67 @@ -0,0 +1,271 @@
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>
79 +
80 +#include "heap-helpers.h"
81 +
82 +void init_heap_helper_buffer(struct heap_helper_buffer *buffer,
83 +                            void (*free)(struct heap_helper_buffer *))
84 +{
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;
93 +}
94 +
95 +struct dma_buf *heap_helper_export_dmabuf(struct heap_helper_buffer *buffer,
96 +                                         int fd_flags)
97 +{
98 +       DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
99 +
100 +       exp_info.ops = &heap_helper_ops;
101 +       exp_info.size = buffer->size;
102 +       exp_info.flags = fd_flags;
103 +       exp_info.priv = buffer;
104 +
105 +       return dma_buf_export(&exp_info);
106 +}
107 +
108 +static void *dma_heap_map_kernel(struct heap_helper_buffer *buffer)
109 +{
110 +       void *vaddr;
111 +
112 +       vaddr = vmap(buffer->pages, buffer->pagecount, VM_MAP, PAGE_KERNEL);
113 +       if (!vaddr)
114 +               return ERR_PTR(-ENOMEM);
115 +
116 +       return vaddr;
117 +}
118 +
119 +static void dma_heap_buffer_destroy(struct heap_helper_buffer *buffer)
120 +{
121 +       if (buffer->vmap_cnt > 0) {
122 +               WARN(1, "%s: buffer still mapped in the kernel\n", __func__);
123 +               vunmap(buffer->vaddr);
124 +       }
125 +
126 +       buffer->free(buffer);
127 +}
128 +
129 +static void *dma_heap_buffer_vmap_get(struct heap_helper_buffer *buffer)
130 +{
131 +       void *vaddr;
132 +
133 +       if (buffer->vmap_cnt) {
134 +               buffer->vmap_cnt++;
135 +               return buffer->vaddr;
136 +       }
137 +       vaddr = dma_heap_map_kernel(buffer);
138 +       if (IS_ERR(vaddr))
139 +               return vaddr;
140 +       buffer->vaddr = vaddr;
141 +       buffer->vmap_cnt++;
142 +       return vaddr;
143 +}
144 +
145 +static void dma_heap_buffer_vmap_put(struct heap_helper_buffer *buffer)
146 +{
147 +       if (!--buffer->vmap_cnt) {
148 +               vunmap(buffer->vaddr);
149 +               buffer->vaddr = NULL;
150 +       }
151 +}
152 +
153 +struct dma_heaps_attachment {
154 +       struct device *dev;
155 +       struct sg_table table;
156 +       struct list_head list;
157 +};
158 +
159 +static int dma_heap_attach(struct dma_buf *dmabuf,
160 +                          struct dma_buf_attachment *attachment)
161 +{
162 +       struct dma_heaps_attachment *a;
163 +       struct heap_helper_buffer *buffer = dmabuf->priv;
164 +       int ret;
165 +
166 +       a = kzalloc(sizeof(*a), GFP_KERNEL);
167 +       if (!a)
168 +               return -ENOMEM;
169 +
170 +       ret = sg_alloc_table_from_pages(&a->table, buffer->pages,
171 +                                       buffer->pagecount, 0,
172 +                                       buffer->pagecount << PAGE_SHIFT,
173 +                                       GFP_KERNEL);
174 +       if (ret) {
175 +               kfree(a);
176 +               return ret;
177 +       }
178 +
179 +       a->dev = attachment->dev;
180 +       INIT_LIST_HEAD(&a->list);
181 +
182 +       attachment->priv = a;
183 +
184 +       mutex_lock(&buffer->lock);
185 +       list_add(&a->list, &buffer->attachments);
186 +       mutex_unlock(&buffer->lock);
187 +
188 +       return 0;
189 +}
190 +
191 +static void dma_heap_detach(struct dma_buf *dmabuf,
192 +                           struct dma_buf_attachment *attachment)
193 +{
194 +       struct dma_heaps_attachment *a = attachment->priv;
195 +       struct heap_helper_buffer *buffer = dmabuf->priv;
196 +
197 +       mutex_lock(&buffer->lock);
198 +       list_del(&a->list);
199 +       mutex_unlock(&buffer->lock);
200 +
201 +       sg_free_table(&a->table);
202 +       kfree(a);
203 +}
204 +
205 +static
206 +struct sg_table *dma_heap_map_dma_buf(struct dma_buf_attachment *attachment,
207 +                                     enum dma_data_direction direction)
208 +{
209 +       struct dma_heaps_attachment *a = attachment->priv;
210 +       struct sg_table *table;
211 +
212 +       table = &a->table;
213 +
214 +       if (!dma_map_sg(attachment->dev, table->sgl, table->nents,
215 +                       direction))
216 +               table = ERR_PTR(-ENOMEM);
217 +       return table;
218 +}
219 +
220 +static void dma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
221 +                                  struct sg_table *table,
222 +                                  enum dma_data_direction direction)
223 +{
224 +       dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction);
225 +}
226 +
227 +static vm_fault_t dma_heap_vm_fault(struct vm_fault *vmf)
228 +{
229 +       struct vm_area_struct *vma = vmf->vma;
230 +       struct heap_helper_buffer *buffer = vma->vm_private_data;
231 +
232 +       if (vmf->pgoff > buffer->pagecount)
233 +               return VM_FAULT_SIGBUS;
234 +
235 +       vmf->page = buffer->pages[vmf->pgoff];
236 +       get_page(vmf->page);
237 +
238 +       return 0;
239 +}
240 +
241 +static const struct vm_operations_struct dma_heap_vm_ops = {
242 +       .fault = dma_heap_vm_fault,
243 +};
244 +
245 +static int dma_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
246 +{
247 +       struct heap_helper_buffer *buffer = dmabuf->priv;
248 +
249 +       if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
250 +               return -EINVAL;
251 +
252 +       vma->vm_ops = &dma_heap_vm_ops;
253 +       vma->vm_private_data = buffer;
254 +
255 +       return 0;
256 +}
257 +
258 +static void dma_heap_dma_buf_release(struct dma_buf *dmabuf)
259 +{
260 +       struct heap_helper_buffer *buffer = dmabuf->priv;
261 +
262 +       dma_heap_buffer_destroy(buffer);
263 +}
264 +
265 +static int dma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
266 +                                            enum dma_data_direction direction)
267 +{
268 +       struct heap_helper_buffer *buffer = dmabuf->priv;
269 +       struct dma_heaps_attachment *a;
270 +       int ret = 0;
271 +
272 +       mutex_lock(&buffer->lock);
273 +
274 +       if (buffer->vmap_cnt)
275 +               invalidate_kernel_vmap_range(buffer->vaddr, buffer->size);
276 +
277 +       list_for_each_entry(a, &buffer->attachments, list) {
278 +               dma_sync_sg_for_cpu(a->dev, a->table.sgl, a->table.nents,
279 +                                   direction);
280 +       }
281 +       mutex_unlock(&buffer->lock);
282 +
283 +       return ret;
284 +}
285 +
286 +static int dma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
287 +                                          enum dma_data_direction direction)
288 +{
289 +       struct heap_helper_buffer *buffer = dmabuf->priv;
290 +       struct dma_heaps_attachment *a;
291 +
292 +       mutex_lock(&buffer->lock);
293 +
294 +       if (buffer->vmap_cnt)
295 +               flush_kernel_vmap_range(buffer->vaddr, buffer->size);
296 +
297 +       list_for_each_entry(a, &buffer->attachments, list) {
298 +               dma_sync_sg_for_device(a->dev, a->table.sgl, a->table.nents,
299 +                                      direction);
300 +       }
301 +       mutex_unlock(&buffer->lock);
302 +
303 +       return 0;
304 +}
305 +
306 +static void *dma_heap_dma_buf_vmap(struct dma_buf *dmabuf)
307 +{
308 +       struct heap_helper_buffer *buffer = dmabuf->priv;
309 +       void *vaddr;
310 +
311 +       mutex_lock(&buffer->lock);
312 +       vaddr = dma_heap_buffer_vmap_get(buffer);
313 +       mutex_unlock(&buffer->lock);
314 +
315 +       return vaddr;
316 +}
317 +
318 +static void dma_heap_dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
319 +{
320 +       struct heap_helper_buffer *buffer = dmabuf->priv;
321 +
322 +       mutex_lock(&buffer->lock);
323 +       dma_heap_buffer_vmap_put(buffer);
324 +       mutex_unlock(&buffer->lock);
325 +}
326 +
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,
338 +};
339 --- /dev/null
340 +++ b/drivers/dma-buf/heaps/heap-helpers.h
341 @@ -0,0 +1,53 @@
342 +/* SPDX-License-Identifier: GPL-2.0 */
343 +/*
344 + * DMABUF Heaps helper code
345 + *
346 + * Copyright (C) 2011 Google, Inc.
347 + * Copyright (C) 2019 Linaro Ltd.
348 + */
349 +
350 +#ifndef _HEAP_HELPERS_H
351 +#define _HEAP_HELPERS_H
352 +
353 +#include <linux/dma-heap.h>
354 +#include <linux/list.h>
355 +
356 +/**
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
368 + *
369 + * @free               heap callback to free the buffer
370 + */
371 +struct heap_helper_buffer {
372 +       struct dma_heap *heap;
373 +       struct dma_buf *dmabuf;
374 +       size_t size;
375 +
376 +       void *priv_virt;
377 +       struct mutex lock;
378 +       int vmap_cnt;
379 +       void *vaddr;
380 +       pgoff_t pagecount;
381 +       struct page **pages;
382 +       struct list_head attachments;
383 +
384 +       void (*free)(struct heap_helper_buffer *buffer);
385 +};
386 +
387 +void init_heap_helper_buffer(struct heap_helper_buffer *buffer,
388 +                            void (*free)(struct heap_helper_buffer *));
389 +
390 +struct dma_buf *heap_helper_export_dmabuf(struct heap_helper_buffer *buffer,
391 +                                         int fd_flags);
392 +
393 +extern const struct dma_buf_ops heap_helper_ops;
394 +#endif /* _HEAP_HELPERS_H */