bcm27xx: update to latest patches from RPi foundation
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0732-dma-buf-heaps-Add-system-heap-to-dmabuf-heaps.patch
1 From 8392cd87592f31737286ea16f11781a234de3564 Mon Sep 17 00:00:00 2001
2 From: John Stultz <john.stultz@linaro.org>
3 Date: Tue, 3 Dec 2019 17:26:39 +0000
4 Subject: [PATCH] dma-buf: heaps: Add system heap to dmabuf heaps
5
6 Commit efa04fefebbd724ffda7f49e42d057a7217c45b0 upstream.
7
8 This patch adds system heap to the dma-buf heaps framework.
9
10 This allows applications to get a page-allocator backed dma-buf
11 for non-contiguous memory.
12
13 This code is an evolution of the Android ION implementation, so
14 thanks to its original authors and maintainters:
15 Rebecca Schultz Zavin, Colin Cross, Laura Abbott, and others!
16
17 Cc: Laura Abbott <labbott@redhat.com>
18 Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>
19 Cc: Sumit Semwal <sumit.semwal@linaro.org>
20 Cc: Liam Mark <lmark@codeaurora.org>
21 Cc: Pratik Patel <pratikp@codeaurora.org>
22 Cc: Brian Starkey <Brian.Starkey@arm.com>
23 Cc: Vincent Donnefort <Vincent.Donnefort@arm.com>
24 Cc: Sudipto Paul <Sudipto.Paul@arm.com>
25 Cc: Andrew F. Davis <afd@ti.com>
26 Cc: Christoph Hellwig <hch@infradead.org>
27 Cc: Chenbo Feng <fengc@google.com>
28 Cc: Alistair Strachan <astrachan@google.com>
29 Cc: Hridya Valsaraju <hridya@google.com>
30 Cc: Sandeep Patil <sspatil@google.com>
31 Cc: Hillf Danton <hdanton@sina.com>
32 Cc: Dave Airlie <airlied@gmail.com>
33 Cc: dri-devel@lists.freedesktop.org
34 Reviewed-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
35 Reviewed-by: Brian Starkey <brian.starkey@arm.com>
36 Acked-by: Sandeep Patil <sspatil@android.com>
37 Acked-by: Laura Abbott <labbott@redhat.com>
38 Tested-by: Ayan Kumar Halder <ayan.halder@arm.com>
39 Signed-off-by: John Stultz <john.stultz@linaro.org>
40 Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
41 Link: https://patchwork.freedesktop.org/patch/msgid/20191203172641.66642-4-john.stultz@linaro.org
42 ---
43 drivers/dma-buf/Kconfig | 2 +
44 drivers/dma-buf/heaps/Kconfig | 6 ++
45 drivers/dma-buf/heaps/Makefile | 1 +
46 drivers/dma-buf/heaps/system_heap.c | 123 ++++++++++++++++++++++++++++
47 4 files changed, 132 insertions(+)
48 create mode 100644 drivers/dma-buf/heaps/Kconfig
49 create mode 100644 drivers/dma-buf/heaps/system_heap.c
50
51 --- a/drivers/dma-buf/Kconfig
52 +++ b/drivers/dma-buf/Kconfig
53 @@ -53,4 +53,6 @@ menuconfig DMABUF_HEAPS
54 allows userspace to allocate dma-bufs that can be shared
55 between drivers.
56
57 +source "drivers/dma-buf/heaps/Kconfig"
58 +
59 endmenu
60 --- /dev/null
61 +++ b/drivers/dma-buf/heaps/Kconfig
62 @@ -0,0 +1,6 @@
63 +config DMABUF_HEAPS_SYSTEM
64 + bool "DMA-BUF System Heap"
65 + depends on DMABUF_HEAPS
66 + help
67 + Choose this option to enable the system dmabuf heap. The system heap
68 + is backed by pages from the buddy allocator. If in doubt, say Y.
69 --- a/drivers/dma-buf/heaps/Makefile
70 +++ b/drivers/dma-buf/heaps/Makefile
71 @@ -1,2 +1,3 @@
72 # SPDX-License-Identifier: GPL-2.0
73 obj-y += heap-helpers.o
74 +obj-$(CONFIG_DMABUF_HEAPS_SYSTEM) += system_heap.o
75 --- /dev/null
76 +++ b/drivers/dma-buf/heaps/system_heap.c
77 @@ -0,0 +1,123 @@
78 +// SPDX-License-Identifier: GPL-2.0
79 +/*
80 + * DMABUF System heap exporter
81 + *
82 + * Copyright (C) 2011 Google, Inc.
83 + * Copyright (C) 2019 Linaro Ltd.
84 + */
85 +
86 +#include <linux/dma-buf.h>
87 +#include <linux/dma-mapping.h>
88 +#include <linux/dma-heap.h>
89 +#include <linux/err.h>
90 +#include <linux/highmem.h>
91 +#include <linux/mm.h>
92 +#include <linux/module.h>
93 +#include <linux/scatterlist.h>
94 +#include <linux/slab.h>
95 +#include <linux/sched/signal.h>
96 +#include <asm/page.h>
97 +
98 +#include "heap-helpers.h"
99 +
100 +struct dma_heap *sys_heap;
101 +
102 +static void system_heap_free(struct heap_helper_buffer *buffer)
103 +{
104 + pgoff_t pg;
105 +
106 + for (pg = 0; pg < buffer->pagecount; pg++)
107 + __free_page(buffer->pages[pg]);
108 + kfree(buffer->pages);
109 + kfree(buffer);
110 +}
111 +
112 +static int system_heap_allocate(struct dma_heap *heap,
113 + unsigned long len,
114 + unsigned long fd_flags,
115 + unsigned long heap_flags)
116 +{
117 + struct heap_helper_buffer *helper_buffer;
118 + struct dma_buf *dmabuf;
119 + int ret = -ENOMEM;
120 + pgoff_t pg;
121 +
122 + helper_buffer = kzalloc(sizeof(*helper_buffer), GFP_KERNEL);
123 + if (!helper_buffer)
124 + return -ENOMEM;
125 +
126 + init_heap_helper_buffer(helper_buffer, system_heap_free);
127 + helper_buffer->heap = heap;
128 + helper_buffer->size = len;
129 +
130 + helper_buffer->pagecount = len / PAGE_SIZE;
131 + helper_buffer->pages = kmalloc_array(helper_buffer->pagecount,
132 + sizeof(*helper_buffer->pages),
133 + GFP_KERNEL);
134 + if (!helper_buffer->pages) {
135 + ret = -ENOMEM;
136 + goto err0;
137 + }
138 +
139 + for (pg = 0; pg < helper_buffer->pagecount; pg++) {
140 + /*
141 + * Avoid trying to allocate memory if the process
142 + * has been killed by by SIGKILL
143 + */
144 + if (fatal_signal_pending(current))
145 + goto err1;
146 +
147 + helper_buffer->pages[pg] = alloc_page(GFP_KERNEL | __GFP_ZERO);
148 + if (!helper_buffer->pages[pg])
149 + goto err1;
150 + }
151 +
152 + /* create the dmabuf */
153 + dmabuf = heap_helper_export_dmabuf(helper_buffer, fd_flags);
154 + if (IS_ERR(dmabuf)) {
155 + ret = PTR_ERR(dmabuf);
156 + goto err1;
157 + }
158 +
159 + helper_buffer->dmabuf = dmabuf;
160 +
161 + ret = dma_buf_fd(dmabuf, fd_flags);
162 + if (ret < 0) {
163 + dma_buf_put(dmabuf);
164 + /* just return, as put will call release and that will free */
165 + return ret;
166 + }
167 +
168 + return ret;
169 +
170 +err1:
171 + while (pg > 0)
172 + __free_page(helper_buffer->pages[--pg]);
173 + kfree(helper_buffer->pages);
174 +err0:
175 + kfree(helper_buffer);
176 +
177 + return ret;
178 +}
179 +
180 +static const struct dma_heap_ops system_heap_ops = {
181 + .allocate = system_heap_allocate,
182 +};
183 +
184 +static int system_heap_create(void)
185 +{
186 + struct dma_heap_export_info exp_info;
187 + int ret = 0;
188 +
189 + exp_info.name = "system_heap";
190 + exp_info.ops = &system_heap_ops;
191 + exp_info.priv = NULL;
192 +
193 + sys_heap = dma_heap_add(&exp_info);
194 + if (IS_ERR(sys_heap))
195 + ret = PTR_ERR(sys_heap);
196 +
197 + return ret;
198 +}
199 +module_init(system_heap_create);
200 +MODULE_LICENSE("GPL v2");