blob: 9033f8b32962ab1daf38344d5b6ef65bd1a01457 [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 Jonsson116a6352020-08-20 17:25:23 +020065static bool ethosu_buffer_verify(struct file *file)
66{
67 return file->f_op == &ethosu_buffer_fops;
68}
69
70static void ethosu_buffer_destroy(struct kref *kref)
71{
72 struct ethosu_buffer *buf =
73 container_of(kref, struct ethosu_buffer, kref);
Kristofer Jonssonec477042023-01-20 13:38:13 +010074 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020075
Kristofer Jonsson074ef902023-01-23 13:05:36 +010076 dev_info(dev, "Buffer destroy. buf=0x%pK", buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020077
Mikael Olsson1182f382023-08-10 13:25:44 +020078 memset(buf->cpu_addr, 0, buf->capacity);
Kristofer Jonsson074ef902023-01-23 13:05:36 +010079 dma_free_coherent(dev, buf->capacity, buf->cpu_addr,
Kristofer Jonssond779a082023-01-04 17:09:47 +010080 buf->dma_addr);
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020081
Mikael Olsson1182f382023-08-10 13:25:44 +020082 memset(buf, 0, sizeof(*buf));
Kristofer Jonssonec477042023-01-20 13:38:13 +010083 devm_kfree(dev, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020084}
85
86static int ethosu_buffer_release(struct inode *inode,
87 struct file *file)
88{
89 struct ethosu_buffer *buf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +010090 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020091
Kristofer Jonssonec477042023-01-20 13:38:13 +010092 dev_info(dev, "Buffer release. file=0x%pK, buf=0x%pK\n",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020093 file, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020094
95 ethosu_buffer_put(buf);
96
97 return 0;
98}
99
100static int ethosu_buffer_mmap(struct file *file,
101 struct vm_area_struct *vma)
102{
103 struct ethosu_buffer *buf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100104 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200105 int ret;
106
Kristofer Jonssonec477042023-01-20 13:38:13 +0100107 dev_info(dev, "Buffer mmap. file=0x%pK, buf=0x%pK\n",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200108 file, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200109
Kristofer Jonssonec477042023-01-20 13:38:13 +0100110 ret = dma_mmap_coherent(dev, vma, buf->cpu_addr,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100111 buf->dma_addr, buf->capacity);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200112
113 return ret;
114}
115
116static long ethosu_buffer_ioctl(struct file *file,
117 unsigned int cmd,
118 unsigned long arg)
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 void __user *udata = (void __user *)arg;
123 int ret = -EINVAL;
124
Kristofer Jonssonec477042023-01-20 13:38:13 +0100125 ret = device_lock_interruptible(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200126 if (ret)
127 return ret;
128
Kristofer Jonssonec477042023-01-20 13:38:13 +0100129 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200130 "Buffer ioctl. file=0x%pK, buf=0x%pK, cmd=0x%x, arg=%lu\n",
131 file, buf, cmd, arg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200132
133 switch (cmd) {
134 case ETHOSU_IOCTL_BUFFER_SET: {
135 struct ethosu_uapi_buffer uapi;
136
137 if (copy_from_user(&uapi, udata, sizeof(uapi)))
138 break;
139
Kristofer Jonssonec477042023-01-20 13:38:13 +0100140 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200141 "Buffer ioctl: Buffer set. size=%u, offset=%u\n",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200142 uapi.size, uapi.offset);
143
144 ret = ethosu_buffer_resize(buf, uapi.size, uapi.offset);
145 break;
146 }
147 case ETHOSU_IOCTL_BUFFER_GET: {
148 struct ethosu_uapi_buffer uapi;
149
150 uapi.size = buf->size;
151 uapi.offset = buf->offset;
152
Kristofer Jonssonec477042023-01-20 13:38:13 +0100153 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200154 "Buffer ioctl: Buffer get. size=%u, offset=%u\n",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200155 uapi.size, uapi.offset);
156
157 if (copy_to_user(udata, &uapi, sizeof(uapi)))
158 break;
159
160 ret = 0;
161 break;
162 }
163 default: {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100164 dev_err(dev, "Invalid ioctl. cmd=%u, arg=%lu",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200165 cmd, arg);
166 break;
167 }
168 }
169
Kristofer Jonssonec477042023-01-20 13:38:13 +0100170 device_unlock(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200171
172 return ret;
173}
174
Kristofer Jonssonec477042023-01-20 13:38:13 +0100175int ethosu_buffer_create(struct device *dev,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200176 size_t capacity)
177{
178 struct ethosu_buffer *buf;
179 int ret = -ENOMEM;
180
Per Åstrandf50f25e2022-05-18 09:12:15 +0200181 if (!capacity)
182 return -EINVAL;
183
Kristofer Jonssonec477042023-01-20 13:38:13 +0100184 buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200185 if (!buf)
186 return -ENOMEM;
187
Kristofer Jonssond779a082023-01-04 17:09:47 +0100188 buf->dev = dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200189 buf->capacity = capacity;
190 buf->offset = 0;
191 buf->size = 0;
192 kref_init(&buf->kref);
193
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100194 buf->cpu_addr = dma_alloc_coherent(dev, capacity,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100195 &buf->dma_addr, GFP_KERNEL);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200196 if (!buf->cpu_addr)
197 goto free_buf;
198
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200199 ret = anon_inode_getfd("ethosu-buffer", &ethosu_buffer_fops, buf,
200 O_RDWR | O_CLOEXEC);
201 if (ret < 0)
202 goto free_dma;
203
204 buf->file = fget(ret);
205 fput(buf->file);
206
Kristofer Jonssonec477042023-01-20 13:38:13 +0100207 dev_info(dev,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100208 "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 +0200209 buf->file, ret, buf, capacity, buf->cpu_addr, buf->dma_addr,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100210 virt_to_phys(buf->cpu_addr));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200211
212 return ret;
213
214free_dma:
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100215 dma_free_coherent(dev, buf->capacity, buf->cpu_addr,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100216 buf->dma_addr);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200217
218free_buf:
Mikael Olsson1182f382023-08-10 13:25:44 +0200219 memset(buf, 0, sizeof(*buf));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100220 devm_kfree(dev, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200221
222 return ret;
223}
224
225struct ethosu_buffer *ethosu_buffer_get_from_fd(int fd)
226{
227 struct ethosu_buffer *buf;
228 struct file *file;
229
230 file = fget(fd);
231 if (!file)
232 return ERR_PTR(-EINVAL);
233
234 if (!ethosu_buffer_verify(file)) {
235 fput(file);
236
237 return ERR_PTR(-EINVAL);
238 }
239
240 buf = file->private_data;
241 ethosu_buffer_get(buf);
242 fput(file);
243
244 return buf;
245}
246
247void ethosu_buffer_get(struct ethosu_buffer *buf)
248{
249 kref_get(&buf->kref);
250}
251
252void ethosu_buffer_put(struct ethosu_buffer *buf)
253{
254 kref_put(&buf->kref, ethosu_buffer_destroy);
255}
256
257int ethosu_buffer_resize(struct ethosu_buffer *buf,
258 size_t size,
259 size_t offset)
260{
261 if ((size + offset) > buf->capacity)
262 return -EINVAL;
263
264 buf->size = size;
265 buf->offset = offset;
266
267 return 0;
268}