blob: 13c30f679df7af3f43947e89233871efe5812369 [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
Kristofer Jonsson116a6352020-08-20 17:25:23 +020047#define DMA_ADDR_BITS 32 /* Number of address bits */
48
Henrik Hoglind90f50802021-08-17 15:04:57 +020049#define CAPABILITIES_RESP_TIMEOUT_MS 100
Davide Grohmann35ce6c82021-06-01 15:03:51 +020050
Kristofer Jonsson116a6352020-08-20 17:25:23 +020051/****************************************************************************
52 * Types
53 ****************************************************************************/
54
55/****************************************************************************
56 * Functions
57 ****************************************************************************/
58
Davide Grohmann35ce6c82021-06-01 15:03:51 +020059static void ethosu_capabilities_destroy(struct kref *kref)
60{
61 struct ethosu_capabilities *cap =
62 container_of(kref, struct ethosu_capabilities, refcount);
63
64 list_del(&cap->list);
65
66 devm_kfree(cap->edev->dev, cap);
67}
68
69static int ethosu_capabilities_find(struct ethosu_capabilities *cap,
70 struct list_head *capabilties_list)
71{
72 struct ethosu_capabilities *cur;
73
74 list_for_each_entry(cur, capabilties_list, list) {
75 if (cur == cap)
76 return 0;
77 }
78
79 return -EINVAL;
80}
81
82static int ethosu_capability_rsp(struct ethosu_device *edev,
83 struct ethosu_core_msg_capabilities_rsp *msg)
84{
85 struct ethosu_capabilities *cap;
86 struct ethosu_uapi_device_capabilities *capabilities;
87 int ret;
88
89 cap = (struct ethosu_capabilities *)msg->user_arg;
90 ret = ethosu_capabilities_find(cap, &edev->capabilities_list);
91 if (0 != ret) {
92 dev_warn(edev->dev,
93 "Handle not found in capabilities list. handle=0x%p\n",
94 cap);
95
96 /* NOTE: do not call complete or kref_put on invalid data! */
97 return ret;
98 }
99
100 capabilities = cap->capabilities;
101
102 capabilities->hw_id.version_status = msg->version_status;
103 capabilities->hw_id.version_minor = msg->version_minor;
104 capabilities->hw_id.version_major = msg->version_major;
105 capabilities->hw_id.product_major = msg->product_major;
106 capabilities->hw_id.arch_patch_rev = msg->arch_patch_rev;
107 capabilities->hw_id.arch_minor_rev = msg->arch_minor_rev;
108 capabilities->hw_id.arch_major_rev = msg->arch_major_rev;
109 capabilities->driver_patch_rev = msg->driver_patch_rev;
110 capabilities->driver_minor_rev = msg->driver_minor_rev;
111 capabilities->driver_major_rev = msg->driver_major_rev;
112 capabilities->hw_cfg.macs_per_cc = msg->macs_per_cc;
113 capabilities->hw_cfg.cmd_stream_version = msg->cmd_stream_version;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200114 capabilities->hw_cfg.custom_dma = msg->custom_dma;
115
116 complete(&cap->done);
117
118 kref_put(&cap->refcount, ethosu_capabilities_destroy);
119
120 return 0;
121}
122
Jonny Svärd7c24c772021-01-14 19:53:17 +0100123/* Incoming messages */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200124static int ethosu_handle_msg(struct ethosu_device *edev)
125{
Jonny Svärd7c24c772021-01-14 19:53:17 +0100126 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200127 struct ethosu_core_msg header;
128
129 union {
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200130 struct ethosu_core_msg_err error;
131 struct ethosu_core_inference_rsp inf;
132 struct ethosu_core_msg_version version;
133 struct ethosu_core_msg_capabilities_rsp capabilities;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200134 } data;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200135
136 /* Read message */
137 ret = ethosu_mailbox_read(&edev->mailbox, &header, &data, sizeof(data));
138 if (ret)
139 return ret;
140
141 switch (header.type) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100142 case ETHOSU_CORE_MSG_ERR:
143 if (header.length != sizeof(data.error)) {
144 dev_warn(edev->dev,
145 "Msg: Error message of incorrect size. size=%u, expected=%zu\n", header.length,
146 sizeof(data.error));
147 ret = -EBADMSG;
148 break;
149 }
150
151 data.error.msg[sizeof(data.error.msg) - 1] = '\0';
152 dev_warn(edev->dev, "Msg: Error. type=%u, msg=\"%s\"\n",
153 data.error.type, data.error.msg);
154 ret = -EBADMSG;
155 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200156 case ETHOSU_CORE_MSG_PING:
157 dev_info(edev->dev, "Msg: Ping\n");
Jonny Svärd7c24c772021-01-14 19:53:17 +0100158 ret = ethosu_mailbox_pong(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200159 break;
160 case ETHOSU_CORE_MSG_PONG:
161 dev_info(edev->dev, "Msg: Pong\n");
162 break;
163 case ETHOSU_CORE_MSG_INFERENCE_RSP:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100164 if (header.length != sizeof(data.inf)) {
165 dev_warn(edev->dev,
166 "Msg: Inference response of incorrect size. size=%u, expected=%zu\n", header.length,
167 sizeof(data.inf));
168 ret = -EBADMSG;
169 break;
170 }
171
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200172 dev_info(edev->dev,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200173 "Msg: Inference response. user_arg=0x%llx, ofm_count=%u, status=%u\n",
174 data.inf.user_arg, data.inf.ofm_count,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200175 data.inf.status);
176 ethosu_inference_rsp(edev, &data.inf);
177 break;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100178 case ETHOSU_CORE_MSG_VERSION_RSP:
179 if (header.length != sizeof(data.version)) {
180 dev_warn(edev->dev,
181 "Msg: Version response of incorrect size. size=%u, expected=%zu\n", header.length,
182 sizeof(data.version));
183 ret = -EBADMSG;
184 break;
185 }
186
187 dev_info(edev->dev, "Msg: Version response v%u.%u.%u\n",
188 data.version.major, data.version.minor,
189 data.version.patch);
190
191 /* Check major and minor version match, else return error */
192 if (data.version.major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
193 data.version.minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
194 dev_warn(edev->dev, "Msg: Version mismatch detected! ");
195 dev_warn(edev->dev, "Local version: v%u.%u.%u\n",
196 ETHOSU_CORE_MSG_VERSION_MAJOR,
197 ETHOSU_CORE_MSG_VERSION_MINOR,
198 ETHOSU_CORE_MSG_VERSION_PATCH);
199 }
200
201 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200202 case ETHOSU_CORE_MSG_CAPABILITIES_RSP:
203 if (header.length != sizeof(data.capabilities)) {
204 dev_warn(edev->dev,
205 "Msg: Capabilities response of incorrect size. size=%u, expected=%zu\n", header.length,
206 sizeof(data.capabilities));
207 ret = -EBADMSG;
208 break;
209 }
Jonny Svärd7c24c772021-01-14 19:53:17 +0100210
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200211 dev_info(edev->dev,
Davide Grohmann40e23d12021-06-17 09:59:40 +0200212 "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 +0200213 data.capabilities.user_arg,
214 data.capabilities.version_status,
215 data.capabilities.version_major,
216 data.capabilities.version_minor,
217 data.capabilities.product_major,
218 data.capabilities.arch_major_rev,
219 data.capabilities.arch_minor_rev,
220 data.capabilities.arch_patch_rev,
221 data.capabilities.driver_major_rev,
222 data.capabilities.driver_minor_rev,
223 data.capabilities.driver_patch_rev,
224 data.capabilities.macs_per_cc,
225 data.capabilities.cmd_stream_version,
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200226 data.capabilities.custom_dma);
227
228 ret = ethosu_capability_rsp(edev, &data.capabilities);
229 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200230 default:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100231 /* This should not happen due to version checks */
232 dev_warn(edev->dev, "Msg: Protocol error\n");
233 ret = -EPROTO;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200234 break;
235 }
236
237 return ret;
238}
239
240static int ethosu_open(struct inode *inode,
241 struct file *file)
242{
243 struct ethosu_device *edev =
244 container_of(inode->i_cdev, struct ethosu_device, cdev);
245
246 file->private_data = edev;
247
248 dev_info(edev->dev, "Opening device node.\n");
249
250 return nonseekable_open(inode, file);
251}
252
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200253static int ethosu_send_capabilities_request(struct ethosu_device *edev,
254 void __user *udata)
255{
256 struct ethosu_uapi_device_capabilities uapi;
257 struct ethosu_capabilities *cap;
258 int ret;
259 int timeout;
260
261 cap = devm_kzalloc(edev->dev, sizeof(struct ethosu_capabilities),
262 GFP_KERNEL);
263 if (!cap)
264 return -ENOMEM;
265
266 cap->edev = edev;
267 cap->capabilities = &uapi;
268 kref_init(&cap->refcount);
269 init_completion(&cap->done);
270 list_add(&cap->list, &edev->capabilities_list);
271
272 ret = ethosu_mailbox_capabilities_request(&edev->mailbox, cap);
273 if (0 != ret)
274 goto put_kref;
275
276 /*
277 * Increase ref counter since we sent the pointer out to
278 * response handler thread. That thread is responsible to
279 * decrease the ref counter before exiting. So the memory
280 * can be freed.
281 *
282 * NOTE: if no response is received back, the memory is leaked.
283 */
284 kref_get(&cap->refcount);
285 /* Unlock the mutex before going to block on the condition */
286 mutex_unlock(&edev->mutex);
287 /* wait for response to arrive back */
288 timeout = wait_for_completion_timeout(&cap->done,
289 msecs_to_jiffies(
290 CAPABILITIES_RESP_TIMEOUT_MS));
291 /* take back the mutex before resuming to do anything */
292 ret = mutex_lock_interruptible(&edev->mutex);
293 if (0 != ret)
294 goto put_kref;
295
296 if (0 == timeout /* timed out*/) {
297 dev_warn(edev->dev,
298 "Msg: Capabilities response lost - timeout\n");
299 ret = -EIO;
300 goto put_kref;
301 }
302
303 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
304
305put_kref:
306 kref_put(&cap->refcount, ethosu_capabilities_destroy);
307
308 return ret;
309}
310
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200311static long ethosu_ioctl(struct file *file,
312 unsigned int cmd,
313 unsigned long arg)
314{
315 struct ethosu_device *edev = file->private_data;
316 void __user *udata = (void __user *)arg;
317 int ret = -EINVAL;
318
319 ret = mutex_lock_interruptible(&edev->mutex);
320 if (ret)
321 return ret;
322
323 dev_info(edev->dev, "Ioctl. cmd=%u, arg=%lu\n", cmd, arg);
324
325 switch (cmd) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100326 case ETHOSU_IOCTL_VERSION_REQ:
327 dev_info(edev->dev, "Ioctl: Send version request\n");
328 ret = ethosu_mailbox_version_request(&edev->mailbox);
329 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200330 case ETHOSU_IOCTL_CAPABILITIES_REQ:
331 dev_info(edev->dev, "Ioctl: Send capabilities request\n");
332 ret = ethosu_send_capabilities_request(edev, udata);
333 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200334 case ETHOSU_IOCTL_PING: {
335 dev_info(edev->dev, "Ioctl: Send ping\n");
336 ret = ethosu_mailbox_ping(&edev->mailbox);
337 break;
338 }
339 case ETHOSU_IOCTL_BUFFER_CREATE: {
340 struct ethosu_uapi_buffer_create uapi;
341
342 dev_info(edev->dev, "Ioctl: Buffer create\n");
343
344 if (copy_from_user(&uapi, udata, sizeof(uapi)))
345 break;
346
347 dev_info(edev->dev, "Ioctl: Buffer. capacity=%u\n",
348 uapi.capacity);
349
350 ret = ethosu_buffer_create(edev, uapi.capacity);
351 break;
352 }
353 case ETHOSU_IOCTL_NETWORK_CREATE: {
354 struct ethosu_uapi_network_create uapi;
355
356 if (copy_from_user(&uapi, udata, sizeof(uapi)))
357 break;
358
359 dev_info(edev->dev, "Ioctl: Network. fd=%u\n", uapi.fd);
360
361 ret = ethosu_network_create(edev, &uapi);
362 break;
363 }
364 default: {
365 dev_err(edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
366 cmd, arg);
367 break;
368 }
369 }
370
371 mutex_unlock(&edev->mutex);
372
373 return ret;
374}
375
376static void ethosu_mbox_rx(void *user_arg)
377{
378 struct ethosu_device *edev = user_arg;
379 int ret;
380
381 mutex_lock(&edev->mutex);
382
383 do {
384 ret = ethosu_handle_msg(edev);
Jonny Svärd7c24c772021-01-14 19:53:17 +0100385 if (ret && ret != -ENOMSG)
386 /* Need to start over in case of error, empty the queue
387 * by fast-forwarding read position to write position.
388 * */
389 ethosu_mailbox_reset(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200390 } while (ret == 0);
391
392 mutex_unlock(&edev->mutex);
393}
394
395int ethosu_dev_init(struct ethosu_device *edev,
396 struct device *dev,
397 struct class *class,
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200398 dev_t devt,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200399 struct resource *in_queue,
400 struct resource *out_queue)
401{
402 static const struct file_operations fops = {
403 .owner = THIS_MODULE,
404 .open = &ethosu_open,
405 .unlocked_ioctl = &ethosu_ioctl,
406#ifdef CONFIG_COMPAT
407 .compat_ioctl = &ethosu_ioctl,
408#endif
409 };
410 struct device *sysdev;
411 int ret;
412
413 edev->dev = dev;
414 edev->class = class;
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200415 edev->devt = devt;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200416 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
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200431 cdev_init(&edev->cdev, &fops);
432 edev->cdev.owner = THIS_MODULE;
433
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200434 ret = cdev_add(&edev->cdev, edev->devt, 1);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200435 if (ret) {
436 dev_err(edev->dev, "Failed to add character device.\n");
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200437 goto deinit_mailbox;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200438 }
439
440 sysdev = device_create(edev->class, NULL, edev->devt, edev,
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200441 "ethosu%d", MINOR(edev->devt));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200442 if (IS_ERR(sysdev)) {
443 dev_err(edev->dev, "Failed to create device.\n");
444 ret = PTR_ERR(sysdev);
445 goto del_cdev;
446 }
447
448 dev_info(edev->dev,
449 "Created Arm Ethos-U device. name=%s, major=%d, minor=%d\n",
450 dev_name(sysdev), MAJOR(edev->devt), MINOR(edev->devt));
451
452 return 0;
453
454del_cdev:
455 cdev_del(&edev->cdev);
456
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200457deinit_mailbox:
458 ethosu_mailbox_deinit(&edev->mailbox);
459
460release_reserved_mem:
461 of_reserved_mem_device_release(edev->dev);
462
463 return ret;
464}
465
466void ethosu_dev_deinit(struct ethosu_device *edev)
467{
468 ethosu_mailbox_deinit(&edev->mailbox);
469 device_destroy(edev->class, edev->cdev.dev);
470 cdev_del(&edev->cdev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200471 of_reserved_mem_device_release(edev->dev);
472
473 dev_info(edev->dev, "%s\n", __FUNCTION__);
474}