blob: 36b776f6108c6bc2f6ae813e3a497d8044f7623e [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonssonb42bc0b2023-01-04 17:09:28 +01002 * Copyright 2020-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_device.h"
26
27#include "ethosu_buffer.h"
28#include "ethosu_core_interface.h"
Davide Grohmann32660f92022-04-27 16:49:07 +020029#include "ethosu_capabilities.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020030#include "ethosu_inference.h"
Davide Grohmann7e8f5082022-03-23 12:48:45 +010031#include "ethosu_cancel_inference.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020032#include "ethosu_network.h"
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010033#include "ethosu_network_info.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020034#include "uapi/ethosu.h"
35
Kristofer Jonsson116a6352020-08-20 17:25:23 +020036#include <linux/dma-mapping.h>
37#include <linux/errno.h>
38#include <linux/fs.h>
39#include <linux/interrupt.h>
40#include <linux/io.h>
41#include <linux/of_reserved_mem.h>
Davide Grohmann35ce6c82021-06-01 15:03:51 +020042#include <linux/slab.h>
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010043#include <linux/uaccess.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020044
45/****************************************************************************
46 * Defines
47 ****************************************************************************/
48
Kristofer Jonsson116a6352020-08-20 17:25:23 +020049#define DMA_ADDR_BITS 32 /* Number of address bits */
50
51/****************************************************************************
Kristofer Jonsson116a6352020-08-20 17:25:23 +020052 * Functions
53 ****************************************************************************/
54
Jonny Svärd7c24c772021-01-14 19:53:17 +010055/* Incoming messages */
Kristofer Jonsson116a6352020-08-20 17:25:23 +020056static int ethosu_handle_msg(struct ethosu_device *edev)
57{
Jonny Svärd7c24c772021-01-14 19:53:17 +010058 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020059 struct ethosu_core_msg header;
60
61 union {
Davide Grohmann35ce6c82021-06-01 15:03:51 +020062 struct ethosu_core_msg_err error;
63 struct ethosu_core_inference_rsp inf;
64 struct ethosu_core_msg_version version;
65 struct ethosu_core_msg_capabilities_rsp capabilities;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010066 struct ethosu_core_network_info_rsp network_info;
Davide Grohmann7e8f5082022-03-23 12:48:45 +010067 struct ethosu_core_cancel_inference_rsp cancellation;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020068 } data;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020069
70 /* Read message */
71 ret = ethosu_mailbox_read(&edev->mailbox, &header, &data, sizeof(data));
72 if (ret)
73 return ret;
74
75 switch (header.type) {
Jonny Svärd7c24c772021-01-14 19:53:17 +010076 case ETHOSU_CORE_MSG_ERR:
77 if (header.length != sizeof(data.error)) {
78 dev_warn(edev->dev,
79 "Msg: Error message of incorrect size. size=%u, expected=%zu\n", header.length,
80 sizeof(data.error));
81 ret = -EBADMSG;
82 break;
83 }
84
85 data.error.msg[sizeof(data.error.msg) - 1] = '\0';
86 dev_warn(edev->dev, "Msg: Error. type=%u, msg=\"%s\"\n",
87 data.error.type, data.error.msg);
88 ret = -EBADMSG;
89 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020090 case ETHOSU_CORE_MSG_PING:
91 dev_info(edev->dev, "Msg: Ping\n");
Jonny Svärd7c24c772021-01-14 19:53:17 +010092 ret = ethosu_mailbox_pong(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020093 break;
94 case ETHOSU_CORE_MSG_PONG:
95 dev_info(edev->dev, "Msg: Pong\n");
96 break;
97 case ETHOSU_CORE_MSG_INFERENCE_RSP:
Jonny Svärd7c24c772021-01-14 19:53:17 +010098 if (header.length != sizeof(data.inf)) {
99 dev_warn(edev->dev,
100 "Msg: Inference response of incorrect size. size=%u, expected=%zu\n", header.length,
101 sizeof(data.inf));
102 ret = -EBADMSG;
103 break;
104 }
105
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200106 dev_info(edev->dev,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200107 "Msg: Inference response. user_arg=0x%llx, ofm_count=%u, status=%u\n",
108 data.inf.user_arg, data.inf.ofm_count,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200109 data.inf.status);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100110
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200111 ethosu_inference_rsp(edev, &data.inf);
112 break;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100113 case ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP:
114 if (header.length != sizeof(data.cancellation)) {
115 dev_warn(edev->dev,
116 "Msg: Cancel Inference response of incorrect size. size=%u, expected=%zu\n", header.length,
117 sizeof(data.cancellation));
118 ret = -EBADMSG;
119 break;
120 }
121
122 dev_info(edev->dev,
123 "Msg: Cancel Inference response. user_arg=0x%llx, status=%u\n",
124 data.cancellation.user_arg, data.cancellation.status);
125 ethosu_cancel_inference_rsp(edev, &data.cancellation);
126 break;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100127 case ETHOSU_CORE_MSG_VERSION_RSP:
128 if (header.length != sizeof(data.version)) {
129 dev_warn(edev->dev,
130 "Msg: Version response of incorrect size. size=%u, expected=%zu\n", header.length,
131 sizeof(data.version));
132 ret = -EBADMSG;
133 break;
134 }
135
136 dev_info(edev->dev, "Msg: Version response v%u.%u.%u\n",
137 data.version.major, data.version.minor,
138 data.version.patch);
139
140 /* Check major and minor version match, else return error */
141 if (data.version.major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
142 data.version.minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
143 dev_warn(edev->dev, "Msg: Version mismatch detected! ");
144 dev_warn(edev->dev, "Local version: v%u.%u.%u\n",
145 ETHOSU_CORE_MSG_VERSION_MAJOR,
146 ETHOSU_CORE_MSG_VERSION_MINOR,
147 ETHOSU_CORE_MSG_VERSION_PATCH);
148 }
149
150 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200151 case ETHOSU_CORE_MSG_CAPABILITIES_RSP:
152 if (header.length != sizeof(data.capabilities)) {
153 dev_warn(edev->dev,
154 "Msg: Capabilities response of incorrect size. size=%u, expected=%zu\n", header.length,
155 sizeof(data.capabilities));
156 ret = -EBADMSG;
157 break;
158 }
Jonny Svärd7c24c772021-01-14 19:53:17 +0100159
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200160 dev_info(edev->dev,
Davide Grohmann40e23d12021-06-17 09:59:40 +0200161 "Msg: Capabilities response ua%llx vs%hhu v%hhu.%hhu p%hhu av%hhu.%hhu.%hhu dv%hhu.%hhu.%hhu mcc%hhu csv%hhu cd%hhu\n",
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200162 data.capabilities.user_arg,
163 data.capabilities.version_status,
164 data.capabilities.version_major,
165 data.capabilities.version_minor,
166 data.capabilities.product_major,
167 data.capabilities.arch_major_rev,
168 data.capabilities.arch_minor_rev,
169 data.capabilities.arch_patch_rev,
170 data.capabilities.driver_major_rev,
171 data.capabilities.driver_minor_rev,
172 data.capabilities.driver_patch_rev,
173 data.capabilities.macs_per_cc,
174 data.capabilities.cmd_stream_version,
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200175 data.capabilities.custom_dma);
176
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100177 ethosu_capability_rsp(edev, &data.capabilities);
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200178 break;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100179 case ETHOSU_CORE_MSG_NETWORK_INFO_RSP:
180 if (header.length != sizeof(data.network_info)) {
181 dev_warn(edev->dev,
182 "Msg: Network info response of incorrect size. size=%u, expected=%zu\n", header.length,
183 sizeof(data.network_info));
184 ret = -EBADMSG;
185 break;
186 }
187
188 dev_info(edev->dev,
189 "Msg: Network info response. user_arg=0x%llx, status=%u",
190 data.network_info.user_arg,
191 data.network_info.status);
192
193 ethosu_network_info_rsp(edev, &data.network_info);
194
195 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200196 default:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100197 /* This should not happen due to version checks */
198 dev_warn(edev->dev, "Msg: Protocol error\n");
199 ret = -EPROTO;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200200 break;
201 }
202
203 return ret;
204}
205
206static int ethosu_open(struct inode *inode,
207 struct file *file)
208{
209 struct ethosu_device *edev =
210 container_of(inode->i_cdev, struct ethosu_device, cdev);
211
212 file->private_data = edev;
213
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200214 dev_info(edev->dev, "Device open. file=0x%pK\n", file);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200215
216 return nonseekable_open(inode, file);
217}
218
219static long ethosu_ioctl(struct file *file,
220 unsigned int cmd,
221 unsigned long arg)
222{
223 struct ethosu_device *edev = file->private_data;
224 void __user *udata = (void __user *)arg;
225 int ret = -EINVAL;
226
227 ret = mutex_lock_interruptible(&edev->mutex);
228 if (ret)
229 return ret;
230
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200231 dev_info(edev->dev, "Device ioctl. file=0x%pK, cmd=0x%x, arg=0x%lx\n",
232 file, cmd, arg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200233
234 switch (cmd) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100235 case ETHOSU_IOCTL_VERSION_REQ:
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200236 dev_info(edev->dev, "Device ioctl: Send version request\n");
Jonny Svärd7c24c772021-01-14 19:53:17 +0100237 ret = ethosu_mailbox_version_request(&edev->mailbox);
238 break;
Davide Grohmann32660f92022-04-27 16:49:07 +0200239 case ETHOSU_IOCTL_CAPABILITIES_REQ: {
240 struct ethosu_uapi_device_capabilities uapi;
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200241
242 dev_info(edev->dev,
243 "Device ioctl: Send capabilities request\n");
244
Davide Grohmann32660f92022-04-27 16:49:07 +0200245 ret = ethosu_capabilities_request(edev, &uapi);
246 if (ret)
247 break;
248
249 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200250 break;
Davide Grohmann32660f92022-04-27 16:49:07 +0200251 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200252 case ETHOSU_IOCTL_PING: {
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200253 dev_info(edev->dev, "Device ioctl: Send ping\n");
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200254 ret = ethosu_mailbox_ping(&edev->mailbox);
255 break;
256 }
257 case ETHOSU_IOCTL_BUFFER_CREATE: {
258 struct ethosu_uapi_buffer_create uapi;
259
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200260 if (copy_from_user(&uapi, udata, sizeof(uapi)))
261 break;
262
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200263 dev_info(edev->dev,
264 "Device ioctl: Buffer create. capacity=%u\n",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200265 uapi.capacity);
266
267 ret = ethosu_buffer_create(edev, uapi.capacity);
268 break;
269 }
270 case ETHOSU_IOCTL_NETWORK_CREATE: {
271 struct ethosu_uapi_network_create uapi;
272
273 if (copy_from_user(&uapi, udata, sizeof(uapi)))
274 break;
275
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200276 dev_info(edev->dev,
277 "Device ioctl: Network create. type=%u, fd/index=%u\n",
278 uapi.type, uapi.fd);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200279
280 ret = ethosu_network_create(edev, &uapi);
281 break;
282 }
283 default: {
284 dev_err(edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
285 cmd, arg);
286 break;
287 }
288 }
289
290 mutex_unlock(&edev->mutex);
291
292 return ret;
293}
294
295static void ethosu_mbox_rx(void *user_arg)
296{
297 struct ethosu_device *edev = user_arg;
298 int ret;
299
300 mutex_lock(&edev->mutex);
301
302 do {
303 ret = ethosu_handle_msg(edev);
Jonny Svärd7c24c772021-01-14 19:53:17 +0100304 if (ret && ret != -ENOMSG)
305 /* Need to start over in case of error, empty the queue
306 * by fast-forwarding read position to write position.
307 * */
308 ethosu_mailbox_reset(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200309 } while (ret == 0);
310
311 mutex_unlock(&edev->mutex);
312}
313
314int ethosu_dev_init(struct ethosu_device *edev,
315 struct device *dev,
316 struct class *class,
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200317 dev_t devt,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200318 struct resource *in_queue,
319 struct resource *out_queue)
320{
321 static const struct file_operations fops = {
322 .owner = THIS_MODULE,
323 .open = &ethosu_open,
324 .unlocked_ioctl = &ethosu_ioctl,
325#ifdef CONFIG_COMPAT
326 .compat_ioctl = &ethosu_ioctl,
327#endif
328 };
329 struct device *sysdev;
330 int ret;
331
332 edev->dev = dev;
333 edev->class = class;
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200334 edev->devt = devt;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200335 mutex_init(&edev->mutex);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100336
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200337 ret = of_reserved_mem_device_init(edev->dev);
338 if (ret)
339 return ret;
340
341 dma_set_mask_and_coherent(edev->dev, DMA_BIT_MASK(DMA_ADDR_BITS));
342
Kristofer Jonssonb42bc0b2023-01-04 17:09:28 +0100343 ret = ethosu_mailbox_init(&edev->mailbox, dev, in_queue, out_queue,
344 ethosu_mbox_rx, edev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200345 if (ret)
346 goto release_reserved_mem;
347
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200348 cdev_init(&edev->cdev, &fops);
349 edev->cdev.owner = THIS_MODULE;
350
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200351 ret = cdev_add(&edev->cdev, edev->devt, 1);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200352 if (ret) {
353 dev_err(edev->dev, "Failed to add character device.\n");
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200354 goto deinit_mailbox;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200355 }
356
357 sysdev = device_create(edev->class, NULL, edev->devt, edev,
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200358 "ethosu%d", MINOR(edev->devt));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200359 if (IS_ERR(sysdev)) {
360 dev_err(edev->dev, "Failed to create device.\n");
361 ret = PTR_ERR(sysdev);
362 goto del_cdev;
363 }
364
365 dev_info(edev->dev,
366 "Created Arm Ethos-U device. name=%s, major=%d, minor=%d\n",
367 dev_name(sysdev), MAJOR(edev->devt), MINOR(edev->devt));
368
369 return 0;
370
371del_cdev:
372 cdev_del(&edev->cdev);
373
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200374deinit_mailbox:
375 ethosu_mailbox_deinit(&edev->mailbox);
376
377release_reserved_mem:
378 of_reserved_mem_device_release(edev->dev);
379
380 return ret;
381}
382
383void ethosu_dev_deinit(struct ethosu_device *edev)
384{
385 ethosu_mailbox_deinit(&edev->mailbox);
386 device_destroy(edev->class, edev->cdev.dev);
387 cdev_del(&edev->cdev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200388 of_reserved_mem_device_release(edev->dev);
389
390 dev_info(edev->dev, "%s\n", __FUNCTION__);
391}