blob: 0654a795f9ee70889fc90e4e8ec1268c6d7570c6 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01002 * Copyright (c) 2020,2022 Arm Limited.
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);
70
71 dev_info(net->edev->dev, "Network destroy. handle=0x%pK\n", net);
72
Kristofer Jonsson35de9e62022-03-08 13:25:45 +010073 if (net->buf != NULL)
74 ethosu_buffer_put(net->buf);
75
Kristofer Jonsson116a6352020-08-20 17:25:23 +020076 devm_kfree(net->edev->dev, net);
77}
78
79static int ethosu_network_release(struct inode *inode,
80 struct file *file)
81{
82 struct ethosu_network *net = file->private_data;
83
84 dev_info(net->edev->dev, "Network release. handle=0x%pK\n", net);
85
86 ethosu_network_put(net);
87
88 return 0;
89}
90
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010091static int ethosu_network_info_request(struct ethosu_network *net,
92 struct ethosu_uapi_network_info *uapi)
93{
94 struct ethosu_network_info *info;
95 int ret;
96
97 /* Create network info request */
98 info = ethosu_network_info_create(net->edev, net, uapi);
99 if (IS_ERR(info))
100 return PTR_ERR(info);
101
102 /* Unlock the device mutex and wait for completion */
103 mutex_unlock(&net->edev->mutex);
104 ret = ethosu_network_info_wait(info, 3000);
105 mutex_lock(&net->edev->mutex);
106
107 ethosu_network_info_put(info);
108
109 return ret;
110}
111
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200112static long ethosu_network_ioctl(struct file *file,
113 unsigned int cmd,
114 unsigned long arg)
115{
116 struct ethosu_network *net = file->private_data;
117 void __user *udata = (void __user *)arg;
118 int ret = -EINVAL;
119
120 ret = mutex_lock_interruptible(&net->edev->mutex);
121 if (ret)
122 return ret;
123
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100124 dev_info(net->edev->dev, "Ioctl: cmd=0x%x, arg=0x%lx\n", cmd, arg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200125
126 switch (cmd) {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100127 case ETHOSU_IOCTL_NETWORK_INFO: {
128 struct ethosu_uapi_network_info uapi;
129
130 if (copy_from_user(&uapi, udata, sizeof(uapi)))
131 break;
132
133 dev_info(net->edev->dev,
134 "Ioctl: Network info. handle=%p\n",
135 net);
136
137 ret = ethosu_network_info_request(net, &uapi);
138 if (ret)
139 break;
140
141 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
142 break;
143 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200144 case ETHOSU_IOCTL_INFERENCE_CREATE: {
145 struct ethosu_uapi_inference_create uapi;
146
147 if (copy_from_user(&uapi, udata, sizeof(uapi)))
148 break;
149
150 dev_info(net->edev->dev,
151 "Ioctl: Inference. ifm_fd=%u, ofm_fd=%u\n",
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200152 uapi.ifm_fd[0], uapi.ofm_fd[0]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200153
154 ret = ethosu_inference_create(net->edev, net, &uapi);
155 break;
156 }
157 default: {
158 dev_err(net->edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
159 cmd, arg);
160 break;
161 }
162 }
163
164 mutex_unlock(&net->edev->mutex);
165
166 return ret;
167}
168
169int ethosu_network_create(struct ethosu_device *edev,
170 struct ethosu_uapi_network_create *uapi)
171{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200172 struct ethosu_network *net;
173 int ret = -ENOMEM;
174
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200175 net = devm_kzalloc(edev->dev, sizeof(*net), GFP_KERNEL);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100176 if (!net)
177 return -ENOMEM;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200178
179 net->edev = edev;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100180 net->buf = NULL;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200181 kref_init(&net->kref);
182
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100183 if (uapi->type == ETHOSU_UAPI_NETWORK_BUFFER) {
184 net->buf = ethosu_buffer_get_from_fd(uapi->fd);
185 if (IS_ERR(net->buf)) {
186 ret = PTR_ERR(net->buf);
187 goto free_net;
188 }
189 } else {
190 net->index = uapi->index;
191 }
192
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200193 ret = anon_inode_getfd("ethosu-network", &ethosu_network_fops, net,
194 O_RDWR | O_CLOEXEC);
195 if (ret < 0)
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100196 goto put_buf;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200197
198 net->file = fget(ret);
199 fput(net->file);
200
201 dev_info(edev->dev, "Network create. handle=0x%pK",
202 net);
203
204 return ret;
205
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100206put_buf:
207 if (net->buf != NULL)
208 ethosu_buffer_put(net->buf);
209
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200210free_net:
211 devm_kfree(edev->dev, net);
212
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200213 return ret;
214}
215
216struct ethosu_network *ethosu_network_get_from_fd(int fd)
217{
218 struct ethosu_network *net;
219 struct file *file;
220
221 file = fget(fd);
222 if (!file)
223 return ERR_PTR(-EINVAL);
224
225 if (!ethosu_network_verify(file)) {
226 fput(file);
227
228 return ERR_PTR(-EINVAL);
229 }
230
231 net = file->private_data;
232 ethosu_network_get(net);
233 fput(file);
234
235 return net;
236}
237
238void ethosu_network_get(struct ethosu_network *net)
239{
240 kref_get(&net->kref);
241}
242
243void ethosu_network_put(struct ethosu_network *net)
244{
245 kref_put(&net->kref, ethosu_network_destroy);
246}