blob: 71ae48401605e467601d61e14b05325df4f7ab8d [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 Olsson52c563d2023-08-24 13:28:35 +0200116 dev_info(dev, "Network ioctl: Network info. net=0x%pK\n", net);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100117
Mikael Olsson52c563d2023-08-24 13:28:35 +0200118 ret = ethosu_network_info_request(dev, net->mailbox, net,
119 &uapi);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100120 if (ret)
121 break;
122
123 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
124 break;
125 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200126 case ETHOSU_IOCTL_INFERENCE_CREATE: {
127 struct ethosu_uapi_inference_create uapi;
128
Mikael Olsson891156d2023-08-24 13:20:31 +0200129 if (copy_from_user(&uapi, udata, sizeof(uapi))) {
130 ret = -EFAULT;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200131 break;
Mikael Olsson891156d2023-08-24 13:20:31 +0200132 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200133
Kristofer Jonssonec477042023-01-20 13:38:13 +0100134 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200135 "Network ioctl: Inference. ifm_fd=%u, ofm_fd=%u\n",
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200136 uapi.ifm_fd[0], uapi.ofm_fd[0]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200137
Kristofer Jonssonec477042023-01-20 13:38:13 +0100138 ret = ethosu_inference_create(dev, net->mailbox, net, &uapi);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200139 break;
140 }
141 default: {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100142 dev_err(dev, "Invalid ioctl. cmd=%u, arg=%lu",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200143 cmd, arg);
Mikael Olsson891156d2023-08-24 13:20:31 +0200144 ret = -ENOIOCTLCMD;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200145 break;
146 }
147 }
148
Kristofer Jonssonec477042023-01-20 13:38:13 +0100149 device_unlock(net->dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200150
151 return ret;
152}
153
Kristofer Jonssonec477042023-01-20 13:38:13 +0100154int ethosu_network_create(struct device *dev,
155 struct ethosu_mailbox *mailbox,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200156 struct ethosu_uapi_network_create *uapi)
157{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200158 struct ethosu_network *net;
159 int ret = -ENOMEM;
160
Kristofer Jonssonec477042023-01-20 13:38:13 +0100161 net = devm_kzalloc(dev, sizeof(*net), GFP_KERNEL);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100162 if (!net)
163 return -ENOMEM;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200164
Kristofer Jonssonec477042023-01-20 13:38:13 +0100165 net->dev = dev;
166 net->mailbox = mailbox;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100167 net->buf = NULL;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200168 kref_init(&net->kref);
169
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100170 if (uapi->type == ETHOSU_UAPI_NETWORK_BUFFER) {
171 net->buf = ethosu_buffer_get_from_fd(uapi->fd);
172 if (IS_ERR(net->buf)) {
173 ret = PTR_ERR(net->buf);
174 goto free_net;
175 }
176 } else {
177 net->index = uapi->index;
178 }
179
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200180 ret = anon_inode_getfd("ethosu-network", &ethosu_network_fops, net,
181 O_RDWR | O_CLOEXEC);
182 if (ret < 0)
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100183 goto put_buf;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200184
185 net->file = fget(ret);
186 fput(net->file);
187
Kristofer Jonssonec477042023-01-20 13:38:13 +0100188 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200189 "Network create. file=0x%pK, fd=%d, net=0x%pK, buf=0x%pK, index=%u",
190 net->file, ret, net, net->buf, net->index);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200191
192 return ret;
193
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100194put_buf:
195 if (net->buf != NULL)
196 ethosu_buffer_put(net->buf);
197
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200198free_net:
Mikael Olsson1182f382023-08-10 13:25:44 +0200199 memset(net, 0, sizeof(*net));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100200 devm_kfree(dev, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200201
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200202 return ret;
203}
204
205struct ethosu_network *ethosu_network_get_from_fd(int fd)
206{
207 struct ethosu_network *net;
208 struct file *file;
209
210 file = fget(fd);
211 if (!file)
212 return ERR_PTR(-EINVAL);
213
214 if (!ethosu_network_verify(file)) {
215 fput(file);
216
217 return ERR_PTR(-EINVAL);
218 }
219
220 net = file->private_data;
221 ethosu_network_get(net);
222 fput(file);
223
224 return net;
225}
226
227void ethosu_network_get(struct ethosu_network *net)
228{
229 kref_get(&net->kref);
230}
231
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100232int ethosu_network_put(struct ethosu_network *net)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200233{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100234 return kref_put(&net->kref, ethosu_network_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200235}