blob: 52f6144c6783ae088744416fbed45aeaa315dec0 [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;
102 int ret = -EINVAL;
103
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
116 if (copy_from_user(&uapi, udata, sizeof(uapi)))
117 break;
118
Kristofer Jonssonec477042023-01-20 13:38:13 +0100119 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200120 "Network ioctl: Network info. net=0x%pK\n",
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100121 net);
122
Kristofer Jonssonec477042023-01-20 13:38:13 +0100123 ret =
124 ethosu_network_info_request(dev, net->mailbox, net,
125 &uapi);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100126 if (ret)
127 break;
128
129 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
130 break;
131 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200132 case ETHOSU_IOCTL_INFERENCE_CREATE: {
133 struct ethosu_uapi_inference_create uapi;
134
135 if (copy_from_user(&uapi, udata, sizeof(uapi)))
136 break;
137
Kristofer Jonssonec477042023-01-20 13:38:13 +0100138 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200139 "Network ioctl: Inference. ifm_fd=%u, ofm_fd=%u\n",
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200140 uapi.ifm_fd[0], uapi.ofm_fd[0]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200141
Kristofer Jonssonec477042023-01-20 13:38:13 +0100142 ret = ethosu_inference_create(dev, net->mailbox, net, &uapi);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200143 break;
144 }
145 default: {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100146 dev_err(dev, "Invalid ioctl. cmd=%u, arg=%lu",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200147 cmd, arg);
148 break;
149 }
150 }
151
Kristofer Jonssonec477042023-01-20 13:38:13 +0100152 device_unlock(net->dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200153
154 return ret;
155}
156
Kristofer Jonssonec477042023-01-20 13:38:13 +0100157int ethosu_network_create(struct device *dev,
158 struct ethosu_mailbox *mailbox,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200159 struct ethosu_uapi_network_create *uapi)
160{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200161 struct ethosu_network *net;
162 int ret = -ENOMEM;
163
Kristofer Jonssonec477042023-01-20 13:38:13 +0100164 net = devm_kzalloc(dev, sizeof(*net), GFP_KERNEL);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100165 if (!net)
166 return -ENOMEM;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200167
Kristofer Jonssonec477042023-01-20 13:38:13 +0100168 net->dev = dev;
169 net->mailbox = mailbox;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100170 net->buf = NULL;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200171 kref_init(&net->kref);
172
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100173 if (uapi->type == ETHOSU_UAPI_NETWORK_BUFFER) {
174 net->buf = ethosu_buffer_get_from_fd(uapi->fd);
175 if (IS_ERR(net->buf)) {
176 ret = PTR_ERR(net->buf);
177 goto free_net;
178 }
179 } else {
180 net->index = uapi->index;
181 }
182
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200183 ret = anon_inode_getfd("ethosu-network", &ethosu_network_fops, net,
184 O_RDWR | O_CLOEXEC);
185 if (ret < 0)
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100186 goto put_buf;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200187
188 net->file = fget(ret);
189 fput(net->file);
190
Kristofer Jonssonec477042023-01-20 13:38:13 +0100191 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200192 "Network create. file=0x%pK, fd=%d, net=0x%pK, buf=0x%pK, index=%u",
193 net->file, ret, net, net->buf, net->index);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200194
195 return ret;
196
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100197put_buf:
198 if (net->buf != NULL)
199 ethosu_buffer_put(net->buf);
200
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200201free_net:
Mikael Olsson1182f382023-08-10 13:25:44 +0200202 memset(net, 0, sizeof(*net));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100203 devm_kfree(dev, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200204
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200205 return ret;
206}
207
208struct ethosu_network *ethosu_network_get_from_fd(int fd)
209{
210 struct ethosu_network *net;
211 struct file *file;
212
213 file = fget(fd);
214 if (!file)
215 return ERR_PTR(-EINVAL);
216
217 if (!ethosu_network_verify(file)) {
218 fput(file);
219
220 return ERR_PTR(-EINVAL);
221 }
222
223 net = file->private_data;
224 ethosu_network_get(net);
225 fput(file);
226
227 return net;
228}
229
230void ethosu_network_get(struct ethosu_network *net)
231{
232 kref_get(&net->kref);
233}
234
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100235int ethosu_network_put(struct ethosu_network *net)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200236{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100237 return kref_put(&net->kref, ethosu_network_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200238}