blob: 0fcbf3be01a5db97c082eefc02ff168b1c7d5891 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonssond779a082023-01-04 17:09:47 +01002 * Copyright 2020-2023 Arm Limited and/or its affiliates
Kristofer Jonsson116a6352020-08-20 17:25:23 +02003 *
4 * This program is free software and is provided to you under the terms of the
5 * GNU General Public License version 2 as published by the Free Software
6 * Foundation, and any use by you of this program is subject to the terms
7 * of such GNU licence.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * SPDX-License-Identifier: GPL-2.0-only
19 */
20
21/****************************************************************************
22 * Includes
23 ****************************************************************************/
24
25#include "ethosu_buffer.h"
26
27#include "ethosu_device.h"
28#include "uapi/ethosu.h"
29
30#include <linux/anon_inodes.h>
31#include <linux/dma-mapping.h>
32#include <linux/of_address.h>
33#include <linux/file.h>
34#include <linux/fs.h>
Kristofer Jonssond779a082023-01-04 17:09:47 +010035#include <linux/remoteproc.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020036#include <linux/uaccess.h>
37
38/****************************************************************************
39 * Variables
40 ****************************************************************************/
41
42static int ethosu_buffer_release(struct inode *inode,
43 struct file *file);
44
45static int ethosu_buffer_mmap(struct file *file,
46 struct vm_area_struct *vma);
47
48static long ethosu_buffer_ioctl(struct file *file,
49 unsigned int cmd,
50 unsigned long arg);
51
52static const struct file_operations ethosu_buffer_fops = {
53 .release = &ethosu_buffer_release,
54 .mmap = &ethosu_buffer_mmap,
55 .unlocked_ioctl = &ethosu_buffer_ioctl,
56#ifdef CONFIG_COMPAT
57 .compat_ioctl = &ethosu_buffer_ioctl,
58#endif
59};
60
61/****************************************************************************
62 * Functions
63 ****************************************************************************/
64
Kristofer Jonssond779a082023-01-04 17:09:47 +010065__attribute__((used))
66static dma_addr_t ethosu_pa_to_da(struct device *dev,
67 phys_addr_t pa,
68 size_t len)
Kristofer Jonsson116a6352020-08-20 17:25:23 +020069{
Kristofer Jonssond779a082023-01-04 17:09:47 +010070 struct rproc *rproc = rproc_get_by_child(dev);
71 struct rproc_mem_entry *mem;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020072
Kristofer Jonssond779a082023-01-04 17:09:47 +010073 list_for_each_entry(mem, &rproc->carveouts, node) {
74 ssize_t offset = pa - mem->dma;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020075
Kristofer Jonssond779a082023-01-04 17:09:47 +010076 if (offset >= 0 && offset + len <= mem->len)
77 return mem->da + offset;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020078 }
79
Kristofer Jonssond779a082023-01-04 17:09:47 +010080 return (dma_addr_t)-1;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020081}
82
83static bool ethosu_buffer_verify(struct file *file)
84{
85 return file->f_op == &ethosu_buffer_fops;
86}
87
88static void ethosu_buffer_destroy(struct kref *kref)
89{
90 struct ethosu_buffer *buf =
91 container_of(kref, struct ethosu_buffer, kref);
Kristofer Jonssonec477042023-01-20 13:38:13 +010092 struct device *dev = buf->dev;
93 struct rproc *rproc = rproc_get_by_child(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020094
Kristofer Jonssonec477042023-01-20 13:38:13 +010095 dev_info(dev, "Buffer destroy. buf=0x%pK\n", buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020096
Kristofer Jonssonec477042023-01-20 13:38:13 +010097 dma_free_coherent(rproc->dev.parent, buf->capacity, buf->cpu_addr,
Kristofer Jonssond779a082023-01-04 17:09:47 +010098 buf->dma_addr);
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020099
Kristofer Jonssonec477042023-01-20 13:38:13 +0100100 devm_kfree(dev, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200101}
102
103static int ethosu_buffer_release(struct inode *inode,
104 struct file *file)
105{
106 struct ethosu_buffer *buf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100107 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200108
Kristofer Jonssonec477042023-01-20 13:38:13 +0100109 dev_info(dev, "Buffer release. file=0x%pK, buf=0x%pK\n",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200110 file, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200111
112 ethosu_buffer_put(buf);
113
114 return 0;
115}
116
117static int ethosu_buffer_mmap(struct file *file,
118 struct vm_area_struct *vma)
119{
120 struct ethosu_buffer *buf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100121 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200122 int ret;
123
Kristofer Jonssonec477042023-01-20 13:38:13 +0100124 dev_info(dev, "Buffer mmap. file=0x%pK, buf=0x%pK\n",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200125 file, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200126
Kristofer Jonssonec477042023-01-20 13:38:13 +0100127 ret = dma_mmap_coherent(dev, vma, buf->cpu_addr,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100128 buf->dma_addr, buf->capacity);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200129
130 return ret;
131}
132
133static long ethosu_buffer_ioctl(struct file *file,
134 unsigned int cmd,
135 unsigned long arg)
136{
137 struct ethosu_buffer *buf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100138 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200139 void __user *udata = (void __user *)arg;
140 int ret = -EINVAL;
141
Kristofer Jonssonec477042023-01-20 13:38:13 +0100142 ret = device_lock_interruptible(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200143 if (ret)
144 return ret;
145
Kristofer Jonssonec477042023-01-20 13:38:13 +0100146 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200147 "Buffer ioctl. file=0x%pK, buf=0x%pK, cmd=0x%x, arg=%lu\n",
148 file, buf, cmd, arg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200149
150 switch (cmd) {
151 case ETHOSU_IOCTL_BUFFER_SET: {
152 struct ethosu_uapi_buffer uapi;
153
154 if (copy_from_user(&uapi, udata, sizeof(uapi)))
155 break;
156
Kristofer Jonssonec477042023-01-20 13:38:13 +0100157 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200158 "Buffer ioctl: Buffer set. size=%u, offset=%u\n",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200159 uapi.size, uapi.offset);
160
161 ret = ethosu_buffer_resize(buf, uapi.size, uapi.offset);
162 break;
163 }
164 case ETHOSU_IOCTL_BUFFER_GET: {
165 struct ethosu_uapi_buffer uapi;
166
167 uapi.size = buf->size;
168 uapi.offset = buf->offset;
169
Kristofer Jonssonec477042023-01-20 13:38:13 +0100170 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200171 "Buffer ioctl: Buffer get. size=%u, offset=%u\n",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200172 uapi.size, uapi.offset);
173
174 if (copy_to_user(udata, &uapi, sizeof(uapi)))
175 break;
176
177 ret = 0;
178 break;
179 }
180 default: {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100181 dev_err(dev, "Invalid ioctl. cmd=%u, arg=%lu",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200182 cmd, arg);
183 break;
184 }
185 }
186
Kristofer Jonssonec477042023-01-20 13:38:13 +0100187 device_unlock(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200188
189 return ret;
190}
191
Kristofer Jonssonec477042023-01-20 13:38:13 +0100192int ethosu_buffer_create(struct device *dev,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200193 size_t capacity)
194{
Kristofer Jonssonec477042023-01-20 13:38:13 +0100195 struct rproc *rproc = rproc_get_by_child(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200196 struct ethosu_buffer *buf;
197 int ret = -ENOMEM;
198
Per Åstrandf50f25e2022-05-18 09:12:15 +0200199 if (!capacity)
200 return -EINVAL;
201
Kristofer Jonssonec477042023-01-20 13:38:13 +0100202 buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200203 if (!buf)
204 return -ENOMEM;
205
Kristofer Jonssond779a082023-01-04 17:09:47 +0100206 buf->dev = dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200207 buf->capacity = capacity;
208 buf->offset = 0;
209 buf->size = 0;
210 kref_init(&buf->kref);
211
Kristofer Jonssonec477042023-01-20 13:38:13 +0100212 buf->cpu_addr = dma_alloc_coherent(rproc->dev.parent, capacity,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100213 &buf->dma_addr, GFP_KERNEL);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200214 if (!buf->cpu_addr)
215 goto free_buf;
216
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200217 ret = anon_inode_getfd("ethosu-buffer", &ethosu_buffer_fops, buf,
218 O_RDWR | O_CLOEXEC);
219 if (ret < 0)
220 goto free_dma;
221
222 buf->file = fget(ret);
223 fput(buf->file);
224
Kristofer Jonssonec477042023-01-20 13:38:13 +0100225 dev_info(dev,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100226 "Buffer create. file=0x%pK, fd=%d, buf=0x%pK, capacity=%zu, cpu_addr=0x%pK, dma_addr=0x%llx, phys_addr=0x%llx\n",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200227 buf->file, ret, buf, capacity, buf->cpu_addr, buf->dma_addr,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100228 virt_to_phys(buf->cpu_addr));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200229
230 return ret;
231
232free_dma:
Kristofer Jonssonec477042023-01-20 13:38:13 +0100233 dma_free_coherent(rproc->dev.parent, buf->capacity, buf->cpu_addr,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100234 buf->dma_addr);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200235
236free_buf:
Kristofer Jonssonec477042023-01-20 13:38:13 +0100237 devm_kfree(dev, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200238
239 return ret;
240}
241
242struct ethosu_buffer *ethosu_buffer_get_from_fd(int fd)
243{
244 struct ethosu_buffer *buf;
245 struct file *file;
246
247 file = fget(fd);
248 if (!file)
249 return ERR_PTR(-EINVAL);
250
251 if (!ethosu_buffer_verify(file)) {
252 fput(file);
253
254 return ERR_PTR(-EINVAL);
255 }
256
257 buf = file->private_data;
258 ethosu_buffer_get(buf);
259 fput(file);
260
261 return buf;
262}
263
264void ethosu_buffer_get(struct ethosu_buffer *buf)
265{
266 kref_get(&buf->kref);
267}
268
269void ethosu_buffer_put(struct ethosu_buffer *buf)
270{
271 kref_put(&buf->kref, ethosu_buffer_destroy);
272}
273
274int ethosu_buffer_resize(struct ethosu_buffer *buf,
275 size_t size,
276 size_t offset)
277{
278 if ((size + offset) > buf->capacity)
279 return -EINVAL;
280
281 buf->size = size;
282 buf->offset = offset;
283
284 return 0;
285}