blob: d64d6d736ea1a78c6f5ff365ad47fda89e0cc012 [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
Kristofer Jonssonec477042023-01-20 13:38:13 +010077 devm_kfree(dev, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020078}
79
80static int ethosu_network_release(struct inode *inode,
81 struct file *file)
82{
83 struct ethosu_network *net = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +010084 struct device *dev = net->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020085
Kristofer Jonssonec477042023-01-20 13:38:13 +010086 dev_info(dev, "Network release. file=0x%pK, net=0x%pK\n",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020087 file, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020088
89 ethosu_network_put(net);
90
91 return 0;
92}
93
94static long ethosu_network_ioctl(struct file *file,
95 unsigned int cmd,
96 unsigned long arg)
97{
98 struct ethosu_network *net = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +010099 struct device *dev = net->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200100 void __user *udata = (void __user *)arg;
101 int ret = -EINVAL;
102
Kristofer Jonssonec477042023-01-20 13:38:13 +0100103 ret = device_lock_interruptible(net->dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200104 if (ret)
105 return ret;
106
Kristofer Jonssonec477042023-01-20 13:38:13 +0100107 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200108 "Network ioctl: file=0x%pK, net=0x%pK, cmd=0x%x, arg=0x%lx\n",
109 file, net, cmd, arg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200110
111 switch (cmd) {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100112 case ETHOSU_IOCTL_NETWORK_INFO: {
113 struct ethosu_uapi_network_info uapi;
114
115 if (copy_from_user(&uapi, udata, sizeof(uapi)))
116 break;
117
Kristofer Jonssonec477042023-01-20 13:38:13 +0100118 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200119 "Network ioctl: Network info. net=0x%pK\n",
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100120 net);
121
Kristofer Jonssonec477042023-01-20 13:38:13 +0100122 ret =
123 ethosu_network_info_request(dev, net->mailbox, net,
124 &uapi);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100125 if (ret)
126 break;
127
128 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
129 break;
130 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200131 case ETHOSU_IOCTL_INFERENCE_CREATE: {
132 struct ethosu_uapi_inference_create uapi;
133
134 if (copy_from_user(&uapi, udata, sizeof(uapi)))
135 break;
136
Kristofer Jonssonec477042023-01-20 13:38:13 +0100137 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200138 "Network ioctl: Inference. ifm_fd=%u, ofm_fd=%u\n",
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200139 uapi.ifm_fd[0], uapi.ofm_fd[0]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200140
Kristofer Jonssonec477042023-01-20 13:38:13 +0100141 ret = ethosu_inference_create(dev, net->mailbox, net, &uapi);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200142 break;
143 }
144 default: {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100145 dev_err(dev, "Invalid ioctl. cmd=%u, arg=%lu",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200146 cmd, arg);
147 break;
148 }
149 }
150
Kristofer Jonssonec477042023-01-20 13:38:13 +0100151 device_unlock(net->dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200152
153 return ret;
154}
155
Kristofer Jonssonec477042023-01-20 13:38:13 +0100156int ethosu_network_create(struct device *dev,
157 struct ethosu_mailbox *mailbox,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200158 struct ethosu_uapi_network_create *uapi)
159{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200160 struct ethosu_network *net;
161 int ret = -ENOMEM;
162
Kristofer Jonssonec477042023-01-20 13:38:13 +0100163 net = devm_kzalloc(dev, sizeof(*net), GFP_KERNEL);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100164 if (!net)
165 return -ENOMEM;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200166
Kristofer Jonssonec477042023-01-20 13:38:13 +0100167 net->dev = dev;
168 net->mailbox = mailbox;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100169 net->buf = NULL;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200170 kref_init(&net->kref);
171
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100172 if (uapi->type == ETHOSU_UAPI_NETWORK_BUFFER) {
173 net->buf = ethosu_buffer_get_from_fd(uapi->fd);
174 if (IS_ERR(net->buf)) {
175 ret = PTR_ERR(net->buf);
176 goto free_net;
177 }
178 } else {
179 net->index = uapi->index;
180 }
181
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200182 ret = anon_inode_getfd("ethosu-network", &ethosu_network_fops, net,
183 O_RDWR | O_CLOEXEC);
184 if (ret < 0)
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100185 goto put_buf;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200186
187 net->file = fget(ret);
188 fput(net->file);
189
Kristofer Jonssonec477042023-01-20 13:38:13 +0100190 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200191 "Network create. file=0x%pK, fd=%d, net=0x%pK, buf=0x%pK, index=%u",
192 net->file, ret, net, net->buf, net->index);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200193
194 return ret;
195
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100196put_buf:
197 if (net->buf != NULL)
198 ethosu_buffer_put(net->buf);
199
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200200free_net:
Kristofer Jonssonec477042023-01-20 13:38:13 +0100201 devm_kfree(dev, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200202
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200203 return ret;
204}
205
206struct ethosu_network *ethosu_network_get_from_fd(int fd)
207{
208 struct ethosu_network *net;
209 struct file *file;
210
211 file = fget(fd);
212 if (!file)
213 return ERR_PTR(-EINVAL);
214
215 if (!ethosu_network_verify(file)) {
216 fput(file);
217
218 return ERR_PTR(-EINVAL);
219 }
220
221 net = file->private_data;
222 ethosu_network_get(net);
223 fput(file);
224
225 return net;
226}
227
228void ethosu_network_get(struct ethosu_network *net)
229{
230 kref_get(&net->kref);
231}
232
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100233int ethosu_network_put(struct ethosu_network *net)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200234{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100235 return kref_put(&net->kref, ethosu_network_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200236}