blob: 7adc7a662c33db39058373f97ba7fe5e3c0bdeee [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
Davide Grohmann35ce6c82021-06-01 15:03:51 +020051#define CAPABILITIES_RESP_TIMEOUT_MS 50
52
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;
116 capabilities->hw_cfg.shram_size = msg->shram_size;
117 capabilities->hw_cfg.custom_dma = msg->custom_dma;
118
119 complete(&cap->done);
120
121 kref_put(&cap->refcount, ethosu_capabilities_destroy);
122
123 return 0;
124}
125
Jonny Svärd7c24c772021-01-14 19:53:17 +0100126/* Incoming messages */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200127static int ethosu_handle_msg(struct ethosu_device *edev)
128{
Jonny Svärd7c24c772021-01-14 19:53:17 +0100129 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200130 struct ethosu_core_msg header;
131
132 union {
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200133 struct ethosu_core_msg_err error;
134 struct ethosu_core_inference_rsp inf;
135 struct ethosu_core_msg_version version;
136 struct ethosu_core_msg_capabilities_rsp capabilities;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200137 } data;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200138
139 /* Read message */
140 ret = ethosu_mailbox_read(&edev->mailbox, &header, &data, sizeof(data));
141 if (ret)
142 return ret;
143
144 switch (header.type) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100145 case ETHOSU_CORE_MSG_ERR:
146 if (header.length != sizeof(data.error)) {
147 dev_warn(edev->dev,
148 "Msg: Error message of incorrect size. size=%u, expected=%zu\n", header.length,
149 sizeof(data.error));
150 ret = -EBADMSG;
151 break;
152 }
153
154 data.error.msg[sizeof(data.error.msg) - 1] = '\0';
155 dev_warn(edev->dev, "Msg: Error. type=%u, msg=\"%s\"\n",
156 data.error.type, data.error.msg);
157 ret = -EBADMSG;
158 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200159 case ETHOSU_CORE_MSG_PING:
160 dev_info(edev->dev, "Msg: Ping\n");
Jonny Svärd7c24c772021-01-14 19:53:17 +0100161 ret = ethosu_mailbox_pong(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200162 break;
163 case ETHOSU_CORE_MSG_PONG:
164 dev_info(edev->dev, "Msg: Pong\n");
165 break;
166 case ETHOSU_CORE_MSG_INFERENCE_RSP:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100167 if (header.length != sizeof(data.inf)) {
168 dev_warn(edev->dev,
169 "Msg: Inference response of incorrect size. size=%u, expected=%zu\n", header.length,
170 sizeof(data.inf));
171 ret = -EBADMSG;
172 break;
173 }
174
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200175 dev_info(edev->dev,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200176 "Msg: Inference response. user_arg=0x%llx, ofm_count=%u, status=%u\n",
177 data.inf.user_arg, data.inf.ofm_count,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200178 data.inf.status);
179 ethosu_inference_rsp(edev, &data.inf);
180 break;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100181 case ETHOSU_CORE_MSG_VERSION_RSP:
182 if (header.length != sizeof(data.version)) {
183 dev_warn(edev->dev,
184 "Msg: Version response of incorrect size. size=%u, expected=%zu\n", header.length,
185 sizeof(data.version));
186 ret = -EBADMSG;
187 break;
188 }
189
190 dev_info(edev->dev, "Msg: Version response v%u.%u.%u\n",
191 data.version.major, data.version.minor,
192 data.version.patch);
193
194 /* Check major and minor version match, else return error */
195 if (data.version.major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
196 data.version.minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
197 dev_warn(edev->dev, "Msg: Version mismatch detected! ");
198 dev_warn(edev->dev, "Local version: v%u.%u.%u\n",
199 ETHOSU_CORE_MSG_VERSION_MAJOR,
200 ETHOSU_CORE_MSG_VERSION_MINOR,
201 ETHOSU_CORE_MSG_VERSION_PATCH);
202 }
203
204 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200205 case ETHOSU_CORE_MSG_CAPABILITIES_RSP:
206 if (header.length != sizeof(data.capabilities)) {
207 dev_warn(edev->dev,
208 "Msg: Capabilities response of incorrect size. size=%u, expected=%zu\n", header.length,
209 sizeof(data.capabilities));
210 ret = -EBADMSG;
211 break;
212 }
Jonny Svärd7c24c772021-01-14 19:53:17 +0100213
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200214 dev_info(edev->dev,
215 "Msg: Capabilities response ua%llx vs%hhu v%hhu.%hhu p%hhu av%hhu.%hhu.%hhu dv%hhu.%hhu.%hhu mcc%hhu csv%hhu ss%hhu cd%hhu\n",
216 data.capabilities.user_arg,
217 data.capabilities.version_status,
218 data.capabilities.version_major,
219 data.capabilities.version_minor,
220 data.capabilities.product_major,
221 data.capabilities.arch_major_rev,
222 data.capabilities.arch_minor_rev,
223 data.capabilities.arch_patch_rev,
224 data.capabilities.driver_major_rev,
225 data.capabilities.driver_minor_rev,
226 data.capabilities.driver_patch_rev,
227 data.capabilities.macs_per_cc,
228 data.capabilities.cmd_stream_version,
229 data.capabilities.shram_size,
230 data.capabilities.custom_dma);
231
232 ret = ethosu_capability_rsp(edev, &data.capabilities);
233 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200234 default:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100235 /* This should not happen due to version checks */
236 dev_warn(edev->dev, "Msg: Protocol error\n");
237 ret = -EPROTO;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200238 break;
239 }
240
241 return ret;
242}
243
244static int ethosu_open(struct inode *inode,
245 struct file *file)
246{
247 struct ethosu_device *edev =
248 container_of(inode->i_cdev, struct ethosu_device, cdev);
249
250 file->private_data = edev;
251
252 dev_info(edev->dev, "Opening device node.\n");
253
254 return nonseekable_open(inode, file);
255}
256
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200257static int ethosu_send_capabilities_request(struct ethosu_device *edev,
258 void __user *udata)
259{
260 struct ethosu_uapi_device_capabilities uapi;
261 struct ethosu_capabilities *cap;
262 int ret;
263 int timeout;
264
265 cap = devm_kzalloc(edev->dev, sizeof(struct ethosu_capabilities),
266 GFP_KERNEL);
267 if (!cap)
268 return -ENOMEM;
269
270 cap->edev = edev;
271 cap->capabilities = &uapi;
272 kref_init(&cap->refcount);
273 init_completion(&cap->done);
274 list_add(&cap->list, &edev->capabilities_list);
275
276 ret = ethosu_mailbox_capabilities_request(&edev->mailbox, cap);
277 if (0 != ret)
278 goto put_kref;
279
280 /*
281 * Increase ref counter since we sent the pointer out to
282 * response handler thread. That thread is responsible to
283 * decrease the ref counter before exiting. So the memory
284 * can be freed.
285 *
286 * NOTE: if no response is received back, the memory is leaked.
287 */
288 kref_get(&cap->refcount);
289 /* Unlock the mutex before going to block on the condition */
290 mutex_unlock(&edev->mutex);
291 /* wait for response to arrive back */
292 timeout = wait_for_completion_timeout(&cap->done,
293 msecs_to_jiffies(
294 CAPABILITIES_RESP_TIMEOUT_MS));
295 /* take back the mutex before resuming to do anything */
296 ret = mutex_lock_interruptible(&edev->mutex);
297 if (0 != ret)
298 goto put_kref;
299
300 if (0 == timeout /* timed out*/) {
301 dev_warn(edev->dev,
302 "Msg: Capabilities response lost - timeout\n");
303 ret = -EIO;
304 goto put_kref;
305 }
306
307 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
308
309put_kref:
310 kref_put(&cap->refcount, ethosu_capabilities_destroy);
311
312 return ret;
313}
314
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200315static long ethosu_ioctl(struct file *file,
316 unsigned int cmd,
317 unsigned long arg)
318{
319 struct ethosu_device *edev = file->private_data;
320 void __user *udata = (void __user *)arg;
321 int ret = -EINVAL;
322
323 ret = mutex_lock_interruptible(&edev->mutex);
324 if (ret)
325 return ret;
326
327 dev_info(edev->dev, "Ioctl. cmd=%u, arg=%lu\n", cmd, arg);
328
329 switch (cmd) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100330 case ETHOSU_IOCTL_VERSION_REQ:
331 dev_info(edev->dev, "Ioctl: Send version request\n");
332 ret = ethosu_mailbox_version_request(&edev->mailbox);
333 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200334 case ETHOSU_IOCTL_CAPABILITIES_REQ:
335 dev_info(edev->dev, "Ioctl: Send capabilities request\n");
336 ret = ethosu_send_capabilities_request(edev, udata);
337 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200338 case ETHOSU_IOCTL_PING: {
339 dev_info(edev->dev, "Ioctl: Send ping\n");
340 ret = ethosu_mailbox_ping(&edev->mailbox);
341 break;
342 }
343 case ETHOSU_IOCTL_BUFFER_CREATE: {
344 struct ethosu_uapi_buffer_create uapi;
345
346 dev_info(edev->dev, "Ioctl: Buffer create\n");
347
348 if (copy_from_user(&uapi, udata, sizeof(uapi)))
349 break;
350
351 dev_info(edev->dev, "Ioctl: Buffer. capacity=%u\n",
352 uapi.capacity);
353
354 ret = ethosu_buffer_create(edev, uapi.capacity);
355 break;
356 }
357 case ETHOSU_IOCTL_NETWORK_CREATE: {
358 struct ethosu_uapi_network_create uapi;
359
360 if (copy_from_user(&uapi, udata, sizeof(uapi)))
361 break;
362
363 dev_info(edev->dev, "Ioctl: Network. fd=%u\n", uapi.fd);
364
365 ret = ethosu_network_create(edev, &uapi);
366 break;
367 }
368 default: {
369 dev_err(edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
370 cmd, arg);
371 break;
372 }
373 }
374
375 mutex_unlock(&edev->mutex);
376
377 return ret;
378}
379
380static void ethosu_mbox_rx(void *user_arg)
381{
382 struct ethosu_device *edev = user_arg;
383 int ret;
384
385 mutex_lock(&edev->mutex);
386
387 do {
388 ret = ethosu_handle_msg(edev);
Jonny Svärd7c24c772021-01-14 19:53:17 +0100389 if (ret && ret != -ENOMSG)
390 /* Need to start over in case of error, empty the queue
391 * by fast-forwarding read position to write position.
392 * */
393 ethosu_mailbox_reset(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200394 } while (ret == 0);
395
396 mutex_unlock(&edev->mutex);
397}
398
399int ethosu_dev_init(struct ethosu_device *edev,
400 struct device *dev,
401 struct class *class,
402 struct resource *in_queue,
403 struct resource *out_queue)
404{
405 static const struct file_operations fops = {
406 .owner = THIS_MODULE,
407 .open = &ethosu_open,
408 .unlocked_ioctl = &ethosu_ioctl,
409#ifdef CONFIG_COMPAT
410 .compat_ioctl = &ethosu_ioctl,
411#endif
412 };
413 struct device *sysdev;
414 int ret;
415
416 edev->dev = dev;
417 edev->class = class;
418 mutex_init(&edev->mutex);
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200419 INIT_LIST_HEAD(&edev->capabilities_list);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200420 INIT_LIST_HEAD(&edev->inference_list);
421
422 ret = of_reserved_mem_device_init(edev->dev);
423 if (ret)
424 return ret;
425
426 dma_set_mask_and_coherent(edev->dev, DMA_BIT_MASK(DMA_ADDR_BITS));
427
428 ret = ethosu_mailbox_init(&edev->mailbox, dev, in_queue, out_queue,
429 ethosu_mbox_rx, edev);
430 if (ret)
431 goto release_reserved_mem;
432
433 ret = alloc_chrdev_region(&edev->devt, MINOR_VERSION, MINOR_COUNT,
434 "ethosu");
435 if (ret) {
436 dev_err(edev->dev, "Failed to allocate chrdev region.\n");
437 goto deinit_mailbox;
438 }
439
440 cdev_init(&edev->cdev, &fops);
441 edev->cdev.owner = THIS_MODULE;
442
443 ret = cdev_add(&edev->cdev, edev->devt, MINOR_COUNT);
444 if (ret) {
445 dev_err(edev->dev, "Failed to add character device.\n");
446 goto region_unregister;
447 }
448
449 sysdev = device_create(edev->class, NULL, edev->devt, edev,
450 "ethosu%d", MAJOR(edev->devt));
451 if (IS_ERR(sysdev)) {
452 dev_err(edev->dev, "Failed to create device.\n");
453 ret = PTR_ERR(sysdev);
454 goto del_cdev;
455 }
456
457 dev_info(edev->dev,
458 "Created Arm Ethos-U device. name=%s, major=%d, minor=%d\n",
459 dev_name(sysdev), MAJOR(edev->devt), MINOR(edev->devt));
460
461 return 0;
462
463del_cdev:
464 cdev_del(&edev->cdev);
465
466region_unregister:
467 unregister_chrdev_region(edev->devt, 1);
468
469deinit_mailbox:
470 ethosu_mailbox_deinit(&edev->mailbox);
471
472release_reserved_mem:
473 of_reserved_mem_device_release(edev->dev);
474
475 return ret;
476}
477
478void ethosu_dev_deinit(struct ethosu_device *edev)
479{
480 ethosu_mailbox_deinit(&edev->mailbox);
481 device_destroy(edev->class, edev->cdev.dev);
482 cdev_del(&edev->cdev);
483 unregister_chrdev_region(edev->devt, MINOR_COUNT);
484 of_reserved_mem_device_release(edev->dev);
485
486 dev_info(edev->dev, "%s\n", __FUNCTION__);
487}