blob: 94354ed1165b6de21ebe663769ab0677449820b8 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Ledion Dajaedd25502023-10-17 09:15:32 +02002 * SPDX-FileCopyrightText: Copyright 2020,2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
3 * SPDX-License-Identifier: GPL-2.0-only
Kristofer Jonsson116a6352020-08-20 17:25:23 +02004 *
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
8 * of such GNU licence.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
18 *
Kristofer Jonsson116a6352020-08-20 17:25:23 +020019 */
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
Ledion Dajaedd25502023-10-17 09:15:32 +020072 dev_dbg(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
Ledion Dajaedd25502023-10-17 09:15:32 +020087 dev_dbg(dev, "Network release. file=0x%pK, net=0x%pK\n",
88 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 Jonsson116a6352020-08-20 17:25:23 +0200108 switch (cmd) {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100109 case ETHOSU_IOCTL_NETWORK_INFO: {
Mikael Olssonec902232023-08-24 13:28:19 +0200110 struct ethosu_uapi_network_info uapi = { 0 };
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100111
Ledion Dajaedd25502023-10-17 09:15:32 +0200112 dev_dbg(dev, "Network ioctl: Network info. net=0x%pK\n", net);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100113
Mikael Olsson52c563d2023-08-24 13:28:35 +0200114 ret = ethosu_network_info_request(dev, net->mailbox, net,
115 &uapi);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100116 if (ret)
117 break;
118
119 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
120 break;
121 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200122 case ETHOSU_IOCTL_INFERENCE_CREATE: {
123 struct ethosu_uapi_inference_create uapi;
124
Mikael Olsson891156d2023-08-24 13:20:31 +0200125 if (copy_from_user(&uapi, udata, sizeof(uapi))) {
126 ret = -EFAULT;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200127 break;
Mikael Olsson891156d2023-08-24 13:20:31 +0200128 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200129
Ledion Dajaedd25502023-10-17 09:15:32 +0200130 dev_dbg(dev,
131 "Network ioctl: Inference. ifm_fd=%u, ofm_fd=%u\n",
132 uapi.ifm_fd[0], uapi.ofm_fd[0]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200133
Kristofer Jonssonec477042023-01-20 13:38:13 +0100134 ret = ethosu_inference_create(dev, net->mailbox, net, &uapi);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200135 break;
136 }
137 default: {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100138 dev_err(dev, "Invalid ioctl. cmd=%u, arg=%lu",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200139 cmd, arg);
Mikael Olsson891156d2023-08-24 13:20:31 +0200140 ret = -ENOIOCTLCMD;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200141 break;
142 }
143 }
144
Kristofer Jonssonec477042023-01-20 13:38:13 +0100145 device_unlock(net->dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200146
147 return ret;
148}
149
Kristofer Jonssonec477042023-01-20 13:38:13 +0100150int ethosu_network_create(struct device *dev,
151 struct ethosu_mailbox *mailbox,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200152 struct ethosu_uapi_network_create *uapi)
153{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200154 struct ethosu_network *net;
155 int ret = -ENOMEM;
156
Kristofer Jonssonec477042023-01-20 13:38:13 +0100157 net = devm_kzalloc(dev, sizeof(*net), GFP_KERNEL);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100158 if (!net)
159 return -ENOMEM;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200160
Kristofer Jonssonec477042023-01-20 13:38:13 +0100161 net->dev = dev;
162 net->mailbox = mailbox;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100163 net->buf = NULL;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200164 kref_init(&net->kref);
165
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100166 if (uapi->type == ETHOSU_UAPI_NETWORK_BUFFER) {
167 net->buf = ethosu_buffer_get_from_fd(uapi->fd);
168 if (IS_ERR(net->buf)) {
169 ret = PTR_ERR(net->buf);
170 goto free_net;
171 }
172 } else {
173 net->index = uapi->index;
174 }
175
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200176 ret = anon_inode_getfd("ethosu-network", &ethosu_network_fops, net,
177 O_RDWR | O_CLOEXEC);
178 if (ret < 0)
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100179 goto put_buf;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200180
181 net->file = fget(ret);
182 fput(net->file);
183
Ledion Dajaedd25502023-10-17 09:15:32 +0200184 dev_dbg(dev,
185 "Network create. file=0x%pK, fd=%d, net=0x%pK, buf=0x%pK, index=%u",
186 net->file, ret, net, net->buf, net->index);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200187
188 return ret;
189
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100190put_buf:
191 if (net->buf != NULL)
192 ethosu_buffer_put(net->buf);
193
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200194free_net:
Mikael Olsson1182f382023-08-10 13:25:44 +0200195 memset(net, 0, sizeof(*net));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100196 devm_kfree(dev, net);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200197
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200198 return ret;
199}
200
201struct ethosu_network *ethosu_network_get_from_fd(int fd)
202{
203 struct ethosu_network *net;
204 struct file *file;
205
206 file = fget(fd);
207 if (!file)
208 return ERR_PTR(-EINVAL);
209
210 if (!ethosu_network_verify(file)) {
211 fput(file);
212
213 return ERR_PTR(-EINVAL);
214 }
215
216 net = file->private_data;
217 ethosu_network_get(net);
218 fput(file);
219
220 return net;
221}
222
223void ethosu_network_get(struct ethosu_network *net)
224{
225 kref_get(&net->kref);
226}
227
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100228int ethosu_network_put(struct ethosu_network *net)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200229{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100230 return kref_put(&net->kref, ethosu_network_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200231}