blob: 40b214c79e7481602626ce218008ff6eafcc893f [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
2 * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
3 *
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"
29#include "ethosu_inference.h"
30#include "ethosu_network.h"
31#include "uapi/ethosu.h"
32
33#include <linux/delay.h>
34#include <linux/dma-mapping.h>
35#include <linux/errno.h>
36#include <linux/fs.h>
37#include <linux/interrupt.h>
38#include <linux/io.h>
39#include <linux/of_reserved_mem.h>
40#include <linux/uaccess.h>
Davide Grohmann35ce6c82021-06-01 15:03:51 +020041#include <linux/slab.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020042
43/****************************************************************************
44 * Defines
45 ****************************************************************************/
46
47#define MINOR_VERSION 0 /* Minor version starts at 0 */
48#define MINOR_COUNT 1 /* Allocate 1 minor version */
49#define DMA_ADDR_BITS 32 /* Number of address bits */
50
Henrik Hoglind90f50802021-08-17 15:04:57 +020051#define CAPABILITIES_RESP_TIMEOUT_MS 100
Davide Grohmann35ce6c82021-06-01 15:03:51 +020052
Kristofer Jonsson116a6352020-08-20 17:25:23 +020053/****************************************************************************
54 * Types
55 ****************************************************************************/
56
57/****************************************************************************
58 * Functions
59 ****************************************************************************/
60
Davide Grohmann35ce6c82021-06-01 15:03:51 +020061static void ethosu_capabilities_destroy(struct kref *kref)
62{
63 struct ethosu_capabilities *cap =
64 container_of(kref, struct ethosu_capabilities, refcount);
65
66 list_del(&cap->list);
67
68 devm_kfree(cap->edev->dev, cap);
69}
70
71static int ethosu_capabilities_find(struct ethosu_capabilities *cap,
72 struct list_head *capabilties_list)
73{
74 struct ethosu_capabilities *cur;
75
76 list_for_each_entry(cur, capabilties_list, list) {
77 if (cur == cap)
78 return 0;
79 }
80
81 return -EINVAL;
82}
83
84static int ethosu_capability_rsp(struct ethosu_device *edev,
85 struct ethosu_core_msg_capabilities_rsp *msg)
86{
87 struct ethosu_capabilities *cap;
88 struct ethosu_uapi_device_capabilities *capabilities;
89 int ret;
90
91 cap = (struct ethosu_capabilities *)msg->user_arg;
92 ret = ethosu_capabilities_find(cap, &edev->capabilities_list);
93 if (0 != ret) {
94 dev_warn(edev->dev,
95 "Handle not found in capabilities list. handle=0x%p\n",
96 cap);
97
98 /* NOTE: do not call complete or kref_put on invalid data! */
99 return ret;
100 }
101
102 capabilities = cap->capabilities;
103
104 capabilities->hw_id.version_status = msg->version_status;
105 capabilities->hw_id.version_minor = msg->version_minor;
106 capabilities->hw_id.version_major = msg->version_major;
107 capabilities->hw_id.product_major = msg->product_major;
108 capabilities->hw_id.arch_patch_rev = msg->arch_patch_rev;
109 capabilities->hw_id.arch_minor_rev = msg->arch_minor_rev;
110 capabilities->hw_id.arch_major_rev = msg->arch_major_rev;
111 capabilities->driver_patch_rev = msg->driver_patch_rev;
112 capabilities->driver_minor_rev = msg->driver_minor_rev;
113 capabilities->driver_major_rev = msg->driver_major_rev;
114 capabilities->hw_cfg.macs_per_cc = msg->macs_per_cc;
115 capabilities->hw_cfg.cmd_stream_version = msg->cmd_stream_version;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200116 capabilities->hw_cfg.custom_dma = msg->custom_dma;
117
118 complete(&cap->done);
119
120 kref_put(&cap->refcount, ethosu_capabilities_destroy);
121
122 return 0;
123}
124
Jonny Svärd7c24c772021-01-14 19:53:17 +0100125/* Incoming messages */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200126static int ethosu_handle_msg(struct ethosu_device *edev)
127{
Jonny Svärd7c24c772021-01-14 19:53:17 +0100128 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200129 struct ethosu_core_msg header;
130
131 union {
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200132 struct ethosu_core_msg_err error;
133 struct ethosu_core_inference_rsp inf;
134 struct ethosu_core_msg_version version;
135 struct ethosu_core_msg_capabilities_rsp capabilities;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200136 } data;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200137
138 /* Read message */
139 ret = ethosu_mailbox_read(&edev->mailbox, &header, &data, sizeof(data));
140 if (ret)
141 return ret;
142
143 switch (header.type) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100144 case ETHOSU_CORE_MSG_ERR:
145 if (header.length != sizeof(data.error)) {
146 dev_warn(edev->dev,
147 "Msg: Error message of incorrect size. size=%u, expected=%zu\n", header.length,
148 sizeof(data.error));
149 ret = -EBADMSG;
150 break;
151 }
152
153 data.error.msg[sizeof(data.error.msg) - 1] = '\0';
154 dev_warn(edev->dev, "Msg: Error. type=%u, msg=\"%s\"\n",
155 data.error.type, data.error.msg);
156 ret = -EBADMSG;
157 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200158 case ETHOSU_CORE_MSG_PING:
159 dev_info(edev->dev, "Msg: Ping\n");
Jonny Svärd7c24c772021-01-14 19:53:17 +0100160 ret = ethosu_mailbox_pong(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200161 break;
162 case ETHOSU_CORE_MSG_PONG:
163 dev_info(edev->dev, "Msg: Pong\n");
164 break;
165 case ETHOSU_CORE_MSG_INFERENCE_RSP:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100166 if (header.length != sizeof(data.inf)) {
167 dev_warn(edev->dev,
168 "Msg: Inference response of incorrect size. size=%u, expected=%zu\n", header.length,
169 sizeof(data.inf));
170 ret = -EBADMSG;
171 break;
172 }
173
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200174 dev_info(edev->dev,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200175 "Msg: Inference response. user_arg=0x%llx, ofm_count=%u, status=%u\n",
176 data.inf.user_arg, data.inf.ofm_count,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200177 data.inf.status);
178 ethosu_inference_rsp(edev, &data.inf);
179 break;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100180 case ETHOSU_CORE_MSG_VERSION_RSP:
181 if (header.length != sizeof(data.version)) {
182 dev_warn(edev->dev,
183 "Msg: Version response of incorrect size. size=%u, expected=%zu\n", header.length,
184 sizeof(data.version));
185 ret = -EBADMSG;
186 break;
187 }
188
189 dev_info(edev->dev, "Msg: Version response v%u.%u.%u\n",
190 data.version.major, data.version.minor,
191 data.version.patch);
192
193 /* Check major and minor version match, else return error */
194 if (data.version.major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
195 data.version.minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
196 dev_warn(edev->dev, "Msg: Version mismatch detected! ");
197 dev_warn(edev->dev, "Local version: v%u.%u.%u\n",
198 ETHOSU_CORE_MSG_VERSION_MAJOR,
199 ETHOSU_CORE_MSG_VERSION_MINOR,
200 ETHOSU_CORE_MSG_VERSION_PATCH);
201 }
202
203 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200204 case ETHOSU_CORE_MSG_CAPABILITIES_RSP:
205 if (header.length != sizeof(data.capabilities)) {
206 dev_warn(edev->dev,
207 "Msg: Capabilities response of incorrect size. size=%u, expected=%zu\n", header.length,
208 sizeof(data.capabilities));
209 ret = -EBADMSG;
210 break;
211 }
Jonny Svärd7c24c772021-01-14 19:53:17 +0100212
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200213 dev_info(edev->dev,
Davide Grohmann40e23d12021-06-17 09:59:40 +0200214 "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 +0200215 data.capabilities.user_arg,
216 data.capabilities.version_status,
217 data.capabilities.version_major,
218 data.capabilities.version_minor,
219 data.capabilities.product_major,
220 data.capabilities.arch_major_rev,
221 data.capabilities.arch_minor_rev,
222 data.capabilities.arch_patch_rev,
223 data.capabilities.driver_major_rev,
224 data.capabilities.driver_minor_rev,
225 data.capabilities.driver_patch_rev,
226 data.capabilities.macs_per_cc,
227 data.capabilities.cmd_stream_version,
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200228 data.capabilities.custom_dma);
229
230 ret = ethosu_capability_rsp(edev, &data.capabilities);
231 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200232 default:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100233 /* This should not happen due to version checks */
234 dev_warn(edev->dev, "Msg: Protocol error\n");
235 ret = -EPROTO;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200236 break;
237 }
238
239 return ret;
240}
241
242static int ethosu_open(struct inode *inode,
243 struct file *file)
244{
245 struct ethosu_device *edev =
246 container_of(inode->i_cdev, struct ethosu_device, cdev);
247
248 file->private_data = edev;
249
250 dev_info(edev->dev, "Opening device node.\n");
251
252 return nonseekable_open(inode, file);
253}
254
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200255static int ethosu_send_capabilities_request(struct ethosu_device *edev,
256 void __user *udata)
257{
258 struct ethosu_uapi_device_capabilities uapi;
259 struct ethosu_capabilities *cap;
260 int ret;
261 int timeout;
262
263 cap = devm_kzalloc(edev->dev, sizeof(struct ethosu_capabilities),
264 GFP_KERNEL);
265 if (!cap)
266 return -ENOMEM;
267
268 cap->edev = edev;
269 cap->capabilities = &uapi;
270 kref_init(&cap->refcount);
271 init_completion(&cap->done);
272 list_add(&cap->list, &edev->capabilities_list);
273
274 ret = ethosu_mailbox_capabilities_request(&edev->mailbox, cap);
275 if (0 != ret)
276 goto put_kref;
277
278 /*
279 * Increase ref counter since we sent the pointer out to
280 * response handler thread. That thread is responsible to
281 * decrease the ref counter before exiting. So the memory
282 * can be freed.
283 *
284 * NOTE: if no response is received back, the memory is leaked.
285 */
286 kref_get(&cap->refcount);
287 /* Unlock the mutex before going to block on the condition */
288 mutex_unlock(&edev->mutex);
289 /* wait for response to arrive back */
290 timeout = wait_for_completion_timeout(&cap->done,
291 msecs_to_jiffies(
292 CAPABILITIES_RESP_TIMEOUT_MS));
293 /* take back the mutex before resuming to do anything */
294 ret = mutex_lock_interruptible(&edev->mutex);
295 if (0 != ret)
296 goto put_kref;
297
298 if (0 == timeout /* timed out*/) {
299 dev_warn(edev->dev,
300 "Msg: Capabilities response lost - timeout\n");
301 ret = -EIO;
302 goto put_kref;
303 }
304
305 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
306
307put_kref:
308 kref_put(&cap->refcount, ethosu_capabilities_destroy);
309
310 return ret;
311}
312
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200313static long ethosu_ioctl(struct file *file,
314 unsigned int cmd,
315 unsigned long arg)
316{
317 struct ethosu_device *edev = file->private_data;
318 void __user *udata = (void __user *)arg;
319 int ret = -EINVAL;
320
321 ret = mutex_lock_interruptible(&edev->mutex);
322 if (ret)
323 return ret;
324
325 dev_info(edev->dev, "Ioctl. cmd=%u, arg=%lu\n", cmd, arg);
326
327 switch (cmd) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100328 case ETHOSU_IOCTL_VERSION_REQ:
329 dev_info(edev->dev, "Ioctl: Send version request\n");
330 ret = ethosu_mailbox_version_request(&edev->mailbox);
331 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200332 case ETHOSU_IOCTL_CAPABILITIES_REQ:
333 dev_info(edev->dev, "Ioctl: Send capabilities request\n");
334 ret = ethosu_send_capabilities_request(edev, udata);
335 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200336 case ETHOSU_IOCTL_PING: {
337 dev_info(edev->dev, "Ioctl: Send ping\n");
338 ret = ethosu_mailbox_ping(&edev->mailbox);
339 break;
340 }
341 case ETHOSU_IOCTL_BUFFER_CREATE: {
342 struct ethosu_uapi_buffer_create uapi;
343
344 dev_info(edev->dev, "Ioctl: Buffer create\n");
345
346 if (copy_from_user(&uapi, udata, sizeof(uapi)))
347 break;
348
349 dev_info(edev->dev, "Ioctl: Buffer. capacity=%u\n",
350 uapi.capacity);
351
352 ret = ethosu_buffer_create(edev, uapi.capacity);
353 break;
354 }
355 case ETHOSU_IOCTL_NETWORK_CREATE: {
356 struct ethosu_uapi_network_create uapi;
357
358 if (copy_from_user(&uapi, udata, sizeof(uapi)))
359 break;
360
361 dev_info(edev->dev, "Ioctl: Network. fd=%u\n", uapi.fd);
362
363 ret = ethosu_network_create(edev, &uapi);
364 break;
365 }
366 default: {
367 dev_err(edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
368 cmd, arg);
369 break;
370 }
371 }
372
373 mutex_unlock(&edev->mutex);
374
375 return ret;
376}
377
378static void ethosu_mbox_rx(void *user_arg)
379{
380 struct ethosu_device *edev = user_arg;
381 int ret;
382
383 mutex_lock(&edev->mutex);
384
385 do {
386 ret = ethosu_handle_msg(edev);
Jonny Svärd7c24c772021-01-14 19:53:17 +0100387 if (ret && ret != -ENOMSG)
388 /* Need to start over in case of error, empty the queue
389 * by fast-forwarding read position to write position.
390 * */
391 ethosu_mailbox_reset(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200392 } while (ret == 0);
393
394 mutex_unlock(&edev->mutex);
395}
396
397int ethosu_dev_init(struct ethosu_device *edev,
398 struct device *dev,
399 struct class *class,
400 struct resource *in_queue,
401 struct resource *out_queue)
402{
403 static const struct file_operations fops = {
404 .owner = THIS_MODULE,
405 .open = &ethosu_open,
406 .unlocked_ioctl = &ethosu_ioctl,
407#ifdef CONFIG_COMPAT
408 .compat_ioctl = &ethosu_ioctl,
409#endif
410 };
411 struct device *sysdev;
412 int ret;
413
414 edev->dev = dev;
415 edev->class = class;
416 mutex_init(&edev->mutex);
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200417 INIT_LIST_HEAD(&edev->capabilities_list);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200418 INIT_LIST_HEAD(&edev->inference_list);
419
420 ret = of_reserved_mem_device_init(edev->dev);
421 if (ret)
422 return ret;
423
424 dma_set_mask_and_coherent(edev->dev, DMA_BIT_MASK(DMA_ADDR_BITS));
425
426 ret = ethosu_mailbox_init(&edev->mailbox, dev, in_queue, out_queue,
427 ethosu_mbox_rx, edev);
428 if (ret)
429 goto release_reserved_mem;
430
431 ret = alloc_chrdev_region(&edev->devt, MINOR_VERSION, MINOR_COUNT,
432 "ethosu");
433 if (ret) {
434 dev_err(edev->dev, "Failed to allocate chrdev region.\n");
435 goto deinit_mailbox;
436 }
437
438 cdev_init(&edev->cdev, &fops);
439 edev->cdev.owner = THIS_MODULE;
440
441 ret = cdev_add(&edev->cdev, edev->devt, MINOR_COUNT);
442 if (ret) {
443 dev_err(edev->dev, "Failed to add character device.\n");
444 goto region_unregister;
445 }
446
447 sysdev = device_create(edev->class, NULL, edev->devt, edev,
448 "ethosu%d", MAJOR(edev->devt));
449 if (IS_ERR(sysdev)) {
450 dev_err(edev->dev, "Failed to create device.\n");
451 ret = PTR_ERR(sysdev);
452 goto del_cdev;
453 }
454
455 dev_info(edev->dev,
456 "Created Arm Ethos-U device. name=%s, major=%d, minor=%d\n",
457 dev_name(sysdev), MAJOR(edev->devt), MINOR(edev->devt));
458
459 return 0;
460
461del_cdev:
462 cdev_del(&edev->cdev);
463
464region_unregister:
465 unregister_chrdev_region(edev->devt, 1);
466
467deinit_mailbox:
468 ethosu_mailbox_deinit(&edev->mailbox);
469
470release_reserved_mem:
471 of_reserved_mem_device_release(edev->dev);
472
473 return ret;
474}
475
476void ethosu_dev_deinit(struct ethosu_device *edev)
477{
478 ethosu_mailbox_deinit(&edev->mailbox);
479 device_destroy(edev->class, edev->cdev.dev);
480 cdev_del(&edev->cdev);
481 unregister_chrdev_region(edev->devt, MINOR_COUNT);
482 of_reserved_mem_device_release(edev->dev);
483
484 dev_info(edev->dev, "%s\n", __FUNCTION__);
485}