blob: 6dc0ce4a22f8dfede03a1b7469899a2691daa93b [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonssonec477042023-01-20 13:38:13 +01002 * Copyright 2020,2022-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_network.h"
26
27#include "ethosu_buffer.h"
28#include "ethosu_device.h"
29#include "ethosu_inference.h"
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010030#include "ethosu_network_info.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020031#include "uapi/ethosu.h"
32
33#include <linux/anon_inodes.h>
34#include <linux/file.h>
35#include <linux/fs.h>
36#include <linux/uaccess.h>
37
38/****************************************************************************
39 * Variables
40 ****************************************************************************/
41
42static int ethosu_network_release(struct inode *inode,
43 struct file *file);
44
45static long ethosu_network_ioctl(struct file *file,
46 unsigned int cmd,
47 unsigned long arg);
48
49static const struct file_operations ethosu_network_fops = {
50 .release = &ethosu_network_release,
51 .unlocked_ioctl = &ethosu_network_ioctl,
52#ifdef CONFIG_COMPAT
53 .compat_ioctl = &ethosu_network_ioctl,
54#endif
55};
56
57/****************************************************************************
58 * Functions
59 ****************************************************************************/
60
61static bool ethosu_network_verify(struct file *file)
62{
63 return file->f_op == &ethosu_network_fops;
64}
65
66static void ethosu_network_destroy(struct kref *kref)
67{
68 struct ethosu_network *net =
69 container_of(kref, struct ethosu_network, kref);
Kristofer Jonssonec477042023-01-20 13:38:13 +010070 struct device *dev = net->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020071
Kristofer Jonssonec477042023-01-20 13:38:13 +010072 dev_info(dev, "Network destroy. net=0x%pK\n", net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020073
Kristofer Jonsson35de9e62022-03-08 13:25:45 +010074 if (net->buf != NULL)
75 ethosu_buffer_put(net->buf);
76
Mikael Olsson1182f382023-08-10 13:25:44 +020077 memset(net, 0, sizeof(*net));
Kristofer Jonssonec477042023-01-20 13:38:13 +010078 devm_kfree(dev, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020079}
80
81static int ethosu_network_release(struct inode *inode,
82 struct file *file)
83{
84 struct ethosu_network *net = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +010085 struct device *dev = net->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020086
Kristofer Jonssonec477042023-01-20 13:38:13 +010087 dev_info(dev, "Network release. file=0x%pK, net=0x%pK\n",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020088 file, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020089
90 ethosu_network_put(net);
91
92 return 0;
93}
94
95static long ethosu_network_ioctl(struct file *file,
96 unsigned int cmd,
97 unsigned long arg)
98{
99 struct ethosu_network *net = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100100 struct device *dev = net->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200101 void __user *udata = (void __user *)arg;
Mikael Olsson891156d2023-08-24 13:20:31 +0200102 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200103
Kristofer Jonssonec477042023-01-20 13:38:13 +0100104 ret = device_lock_interruptible(net->dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200105 if (ret)
106 return ret;
107
Kristofer Jonssonec477042023-01-20 13:38:13 +0100108 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200109 "Network ioctl: file=0x%pK, net=0x%pK, cmd=0x%x, arg=0x%lx\n",
110 file, net, cmd, arg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200111
112 switch (cmd) {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100113 case ETHOSU_IOCTL_NETWORK_INFO: {
114 struct ethosu_uapi_network_info uapi;
115
Mikael Olsson891156d2023-08-24 13:20:31 +0200116 if (copy_from_user(&uapi, udata, sizeof(uapi))) {
117 ret = -EFAULT;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100118 break;
Mikael Olsson891156d2023-08-24 13:20:31 +0200119 }
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100120
Kristofer Jonssonec477042023-01-20 13:38:13 +0100121 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200122 "Network ioctl: Network info. net=0x%pK\n",
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100123 net);
124
Kristofer Jonssonec477042023-01-20 13:38:13 +0100125 ret =
126 ethosu_network_info_request(dev, net->mailbox, net,
127 &uapi);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100128 if (ret)
129 break;
130
131 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
132 break;
133 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200134 case ETHOSU_IOCTL_INFERENCE_CREATE: {
135 struct ethosu_uapi_inference_create uapi;
136
Mikael Olsson891156d2023-08-24 13:20:31 +0200137 if (copy_from_user(&uapi, udata, sizeof(uapi))) {
138 ret = -EFAULT;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200139 break;
Mikael Olsson891156d2023-08-24 13:20:31 +0200140 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200141
Kristofer Jonssonec477042023-01-20 13:38:13 +0100142 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200143 "Network ioctl: Inference. ifm_fd=%u, ofm_fd=%u\n",
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200144 uapi.ifm_fd[0], uapi.ofm_fd[0]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200145
Kristofer Jonssonec477042023-01-20 13:38:13 +0100146 ret = ethosu_inference_create(dev, net->mailbox, net, &uapi);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200147 break;
148 }
149 default: {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100150 dev_err(dev, "Invalid ioctl. cmd=%u, arg=%lu",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200151 cmd, arg);
Mikael Olsson891156d2023-08-24 13:20:31 +0200152 ret = -ENOIOCTLCMD;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200153 break;
154 }
155 }
156
Kristofer Jonssonec477042023-01-20 13:38:13 +0100157 device_unlock(net->dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200158
159 return ret;
160}
161
Kristofer Jonssonec477042023-01-20 13:38:13 +0100162int ethosu_network_create(struct device *dev,
163 struct ethosu_mailbox *mailbox,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200164 struct ethosu_uapi_network_create *uapi)
165{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200166 struct ethosu_network *net;
167 int ret = -ENOMEM;
168
Kristofer Jonssonec477042023-01-20 13:38:13 +0100169 net = devm_kzalloc(dev, sizeof(*net), GFP_KERNEL);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100170 if (!net)
171 return -ENOMEM;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200172
Kristofer Jonssonec477042023-01-20 13:38:13 +0100173 net->dev = dev;
174 net->mailbox = mailbox;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100175 net->buf = NULL;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200176 kref_init(&net->kref);
177
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100178 if (uapi->type == ETHOSU_UAPI_NETWORK_BUFFER) {
179 net->buf = ethosu_buffer_get_from_fd(uapi->fd);
180 if (IS_ERR(net->buf)) {
181 ret = PTR_ERR(net->buf);
182 goto free_net;
183 }
184 } else {
185 net->index = uapi->index;
186 }
187
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200188 ret = anon_inode_getfd("ethosu-network", &ethosu_network_fops, net,
189 O_RDWR | O_CLOEXEC);
190 if (ret < 0)
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100191 goto put_buf;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200192
193 net->file = fget(ret);
194 fput(net->file);
195
Kristofer Jonssonec477042023-01-20 13:38:13 +0100196 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200197 "Network create. file=0x%pK, fd=%d, net=0x%pK, buf=0x%pK, index=%u",
198 net->file, ret, net, net->buf, net->index);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200199
200 return ret;
201
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100202put_buf:
203 if (net->buf != NULL)
204 ethosu_buffer_put(net->buf);
205
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200206free_net:
Mikael Olsson1182f382023-08-10 13:25:44 +0200207 memset(net, 0, sizeof(*net));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100208 devm_kfree(dev, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200209
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200210 return ret;
211}
212
213struct ethosu_network *ethosu_network_get_from_fd(int fd)
214{
215 struct ethosu_network *net;
216 struct file *file;
217
218 file = fget(fd);
219 if (!file)
220 return ERR_PTR(-EINVAL);
221
222 if (!ethosu_network_verify(file)) {
223 fput(file);
224
225 return ERR_PTR(-EINVAL);
226 }
227
228 net = file->private_data;
229 ethosu_network_get(net);
230 fput(file);
231
232 return net;
233}
234
235void ethosu_network_get(struct ethosu_network *net)
236{
237 kref_get(&net->kref);
238}
239
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100240int ethosu_network_put(struct ethosu_network *net)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200241{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100242 return kref_put(&net->kref, ethosu_network_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200243}