blob: 316df6f3b4b7096b27fd03f09a30df5fb39a5c4d [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonsson35de9e62022-03-08 13:25:45 +01002 * Copyright (c) 2020-2022 Arm Limited.
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"
29#include "ethosu_inference.h"
30#include "ethosu_network.h"
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010031#include "ethosu_network_info.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020032#include "uapi/ethosu.h"
33
34#include <linux/delay.h>
35#include <linux/dma-mapping.h>
36#include <linux/errno.h>
37#include <linux/fs.h>
38#include <linux/interrupt.h>
39#include <linux/io.h>
40#include <linux/of_reserved_mem.h>
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010041#include <linux/reset.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
Per Åstrandbb175272021-10-25 12:37:36 +020051#define CAPABILITIES_RESP_TIMEOUT_MS 2000
Davide Grohmann35ce6c82021-06-01 15:03:51 +020052
Kristofer Jonsson116a6352020-08-20 17:25:23 +020053/****************************************************************************
Kristofer Jonsson116a6352020-08-20 17:25:23 +020054 * Functions
55 ****************************************************************************/
56
Davide Grohmann35ce6c82021-06-01 15:03:51 +020057static void ethosu_capabilities_destroy(struct kref *kref)
58{
59 struct ethosu_capabilities *cap =
60 container_of(kref, struct ethosu_capabilities, refcount);
61
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010062 dev_info(cap->edev->dev, "Capabilities destroy. handle=0x%pK\n", cap);
63
64 list_del(&cap->msg.list);
Davide Grohmann35ce6c82021-06-01 15:03:51 +020065
66 devm_kfree(cap->edev->dev, cap);
67}
68
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010069static int ethosu_capabilities_send(struct ethosu_capabilities *cap)
Davide Grohmann35ce6c82021-06-01 15:03:51 +020070{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010071 int ret;
Davide Grohmann35ce6c82021-06-01 15:03:51 +020072
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010073 ret = ethosu_mailbox_capabilities_request(&cap->edev->mailbox, cap);
74 if (ret)
75 return ret;
Davide Grohmann35ce6c82021-06-01 15:03:51 +020076
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010077 return 0;
Davide Grohmann35ce6c82021-06-01 15:03:51 +020078}
79
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010080static void ethosu_capabilities_fail(struct ethosu_mailbox_msg *msg)
Davide Grohmann35ce6c82021-06-01 15:03:51 +020081{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010082 struct ethosu_capabilities *cap =
83 container_of(msg, typeof(*cap), msg);
84
85 cap->errno = -EFAULT;
86 complete(&cap->done);
87}
88
89static int ethosu_capabilities_resend(struct ethosu_mailbox_msg *msg)
90{
91 struct ethosu_capabilities *cap =
92 container_of(msg, typeof(*cap), msg);
93 int ret;
94
95 /* Don't resend request if response has already been received */
96 if (completion_done(&cap->done))
97 return 0;
98
99 /* Resend request */
100 ret = ethosu_capabilities_send(cap);
101 if (ret)
102 return ret;
103
104 return 0;
105}
106
107static void ethosu_capability_rsp(struct ethosu_device *edev,
108 struct ethosu_core_msg_capabilities_rsp *msg)
109{
110 struct ethosu_capabilities *cap =
111 (struct ethosu_capabilities *)msg->user_arg;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200112 struct ethosu_uapi_device_capabilities *capabilities;
113 int ret;
114
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100115 ret = ethosu_mailbox_find(&edev->mailbox, &cap->msg);
116 if (ret) {
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200117 dev_warn(edev->dev,
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100118 "Capabilities not found in pending list. handle=0x%p\n",
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200119 cap);
120
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100121 return;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200122 }
123
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100124 if (completion_done(&cap->done))
125 return;
126
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200127 capabilities = cap->capabilities;
128
129 capabilities->hw_id.version_status = msg->version_status;
130 capabilities->hw_id.version_minor = msg->version_minor;
131 capabilities->hw_id.version_major = msg->version_major;
132 capabilities->hw_id.product_major = msg->product_major;
133 capabilities->hw_id.arch_patch_rev = msg->arch_patch_rev;
134 capabilities->hw_id.arch_minor_rev = msg->arch_minor_rev;
135 capabilities->hw_id.arch_major_rev = msg->arch_major_rev;
136 capabilities->driver_patch_rev = msg->driver_patch_rev;
137 capabilities->driver_minor_rev = msg->driver_minor_rev;
138 capabilities->driver_major_rev = msg->driver_major_rev;
139 capabilities->hw_cfg.macs_per_cc = msg->macs_per_cc;
140 capabilities->hw_cfg.cmd_stream_version = msg->cmd_stream_version;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200141 capabilities->hw_cfg.custom_dma = msg->custom_dma;
142
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100143 cap->errno = 0;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200144 complete(&cap->done);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100145}
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200146
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100147static int ethosu_capabilities_request(struct ethosu_device *edev,
148 void __user *udata)
149{
150 struct ethosu_uapi_device_capabilities uapi;
151 struct ethosu_capabilities *cap;
152 int ret;
153 int timeout;
154
155 cap = devm_kzalloc(edev->dev, sizeof(struct ethosu_capabilities),
156 GFP_KERNEL);
157 if (!cap)
158 return -ENOMEM;
159
160 cap->edev = edev;
161 cap->capabilities = &uapi;
162 kref_init(&cap->refcount);
163 init_completion(&cap->done);
164 list_add(&cap->msg.list, &edev->mailbox.pending_list);
165 cap->msg.fail = ethosu_capabilities_fail;
166 cap->msg.resend = ethosu_capabilities_resend;
167
168 ret = ethosu_capabilities_send(cap);
169 if (0 != ret)
170 goto put_kref;
171
172 /* Unlock the mutex before going to block on the condition */
173 mutex_unlock(&edev->mutex);
174
175 /* wait for response to arrive back */
176 timeout = wait_for_completion_timeout(&cap->done,
177 msecs_to_jiffies(
178 CAPABILITIES_RESP_TIMEOUT_MS));
179
180 /* take back the mutex before resuming to do anything */
181 mutex_lock(&edev->mutex);
182
183 if (0 == timeout) {
184 dev_warn(edev->dev, "Capabilities response timeout");
185 ret = -EIO;
186 goto put_kref;
187 }
188
189 if (cap->errno)
190 goto put_kref;
191
192 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
193
194put_kref:
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200195 kref_put(&cap->refcount, ethosu_capabilities_destroy);
196
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100197 return ret;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200198}
199
Jonny Svärd7c24c772021-01-14 19:53:17 +0100200/* Incoming messages */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200201static int ethosu_handle_msg(struct ethosu_device *edev)
202{
Jonny Svärd7c24c772021-01-14 19:53:17 +0100203 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200204 struct ethosu_core_msg header;
205
206 union {
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200207 struct ethosu_core_msg_err error;
208 struct ethosu_core_inference_rsp inf;
209 struct ethosu_core_msg_version version;
210 struct ethosu_core_msg_capabilities_rsp capabilities;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100211 struct ethosu_core_network_info_rsp network_info;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200212 } data;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200213
214 /* Read message */
215 ret = ethosu_mailbox_read(&edev->mailbox, &header, &data, sizeof(data));
216 if (ret)
217 return ret;
218
219 switch (header.type) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100220 case ETHOSU_CORE_MSG_ERR:
221 if (header.length != sizeof(data.error)) {
222 dev_warn(edev->dev,
223 "Msg: Error message of incorrect size. size=%u, expected=%zu\n", header.length,
224 sizeof(data.error));
225 ret = -EBADMSG;
226 break;
227 }
228
229 data.error.msg[sizeof(data.error.msg) - 1] = '\0';
230 dev_warn(edev->dev, "Msg: Error. type=%u, msg=\"%s\"\n",
231 data.error.type, data.error.msg);
232 ret = -EBADMSG;
233 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200234 case ETHOSU_CORE_MSG_PING:
235 dev_info(edev->dev, "Msg: Ping\n");
Jonny Svärd7c24c772021-01-14 19:53:17 +0100236 ret = ethosu_mailbox_pong(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200237 break;
238 case ETHOSU_CORE_MSG_PONG:
239 dev_info(edev->dev, "Msg: Pong\n");
240 break;
241 case ETHOSU_CORE_MSG_INFERENCE_RSP:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100242 if (header.length != sizeof(data.inf)) {
243 dev_warn(edev->dev,
244 "Msg: Inference response of incorrect size. size=%u, expected=%zu\n", header.length,
245 sizeof(data.inf));
246 ret = -EBADMSG;
247 break;
248 }
249
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200250 dev_info(edev->dev,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200251 "Msg: Inference response. user_arg=0x%llx, ofm_count=%u, status=%u\n",
252 data.inf.user_arg, data.inf.ofm_count,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200253 data.inf.status);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100254
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200255 ethosu_inference_rsp(edev, &data.inf);
256 break;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100257 case ETHOSU_CORE_MSG_VERSION_RSP:
258 if (header.length != sizeof(data.version)) {
259 dev_warn(edev->dev,
260 "Msg: Version response of incorrect size. size=%u, expected=%zu\n", header.length,
261 sizeof(data.version));
262 ret = -EBADMSG;
263 break;
264 }
265
266 dev_info(edev->dev, "Msg: Version response v%u.%u.%u\n",
267 data.version.major, data.version.minor,
268 data.version.patch);
269
270 /* Check major and minor version match, else return error */
271 if (data.version.major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
272 data.version.minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
273 dev_warn(edev->dev, "Msg: Version mismatch detected! ");
274 dev_warn(edev->dev, "Local version: v%u.%u.%u\n",
275 ETHOSU_CORE_MSG_VERSION_MAJOR,
276 ETHOSU_CORE_MSG_VERSION_MINOR,
277 ETHOSU_CORE_MSG_VERSION_PATCH);
278 }
279
280 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200281 case ETHOSU_CORE_MSG_CAPABILITIES_RSP:
282 if (header.length != sizeof(data.capabilities)) {
283 dev_warn(edev->dev,
284 "Msg: Capabilities response of incorrect size. size=%u, expected=%zu\n", header.length,
285 sizeof(data.capabilities));
286 ret = -EBADMSG;
287 break;
288 }
Jonny Svärd7c24c772021-01-14 19:53:17 +0100289
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200290 dev_info(edev->dev,
Davide Grohmann40e23d12021-06-17 09:59:40 +0200291 "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 +0200292 data.capabilities.user_arg,
293 data.capabilities.version_status,
294 data.capabilities.version_major,
295 data.capabilities.version_minor,
296 data.capabilities.product_major,
297 data.capabilities.arch_major_rev,
298 data.capabilities.arch_minor_rev,
299 data.capabilities.arch_patch_rev,
300 data.capabilities.driver_major_rev,
301 data.capabilities.driver_minor_rev,
302 data.capabilities.driver_patch_rev,
303 data.capabilities.macs_per_cc,
304 data.capabilities.cmd_stream_version,
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200305 data.capabilities.custom_dma);
306
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100307 ethosu_capability_rsp(edev, &data.capabilities);
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200308 break;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100309 case ETHOSU_CORE_MSG_NETWORK_INFO_RSP:
310 if (header.length != sizeof(data.network_info)) {
311 dev_warn(edev->dev,
312 "Msg: Network info response of incorrect size. size=%u, expected=%zu\n", header.length,
313 sizeof(data.network_info));
314 ret = -EBADMSG;
315 break;
316 }
317
318 dev_info(edev->dev,
319 "Msg: Network info response. user_arg=0x%llx, status=%u",
320 data.network_info.user_arg,
321 data.network_info.status);
322
323 ethosu_network_info_rsp(edev, &data.network_info);
324
325 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200326 default:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100327 /* This should not happen due to version checks */
328 dev_warn(edev->dev, "Msg: Protocol error\n");
329 ret = -EPROTO;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200330 break;
331 }
332
333 return ret;
334}
335
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100336static int ethosu_firmware_reset(struct ethosu_device *edev)
337{
338 int ret;
339
340 /* No reset control for this device */
341 if (IS_ERR(edev->reset))
342 return PTR_ERR(edev->reset);
343
344 dev_info(edev->dev, "Resetting firmware.");
345
346 ret = reset_control_assert(edev->reset);
347 if (ret) {
348 dev_warn(edev->dev, "Failed to reset assert firmware. ret=%d",
349 ret);
350
351 return ret;
352 }
353
354 /* Initialize mailbox header with illegal values */
355 ethosu_mailbox_wait_prepare(&edev->mailbox);
356
357 /* If this call fails we have a problem. We managed halt the firmware,
358 * but not to release the reset.
359 */
360 ret = reset_control_deassert(edev->reset);
361 if (ret) {
362 dev_warn(edev->dev, "Failed to reset deassert firmware. ret=%d",
363 ret);
364 goto fail;
365 }
366
367 /* Wait for firmware to boot up and initialize mailbox */
368 ret = ethosu_mailbox_wait_firmware(&edev->mailbox);
369 if (ret) {
370 dev_warn(edev->dev, "Wait on firmware boot timed out. ret=%d",
371 ret);
372 goto fail;
373 }
374
375 edev->mailbox.ping_count = 0;
376 ethosu_watchdog_reset(&edev->watchdog);
377
378 ret = ethosu_mailbox_ping(&edev->mailbox);
379 if (ret)
380 goto fail;
381
382 /* Resend messages */
383 ethosu_mailbox_resend(&edev->mailbox);
384
385 return ret;
386
387fail:
388 ethosu_mailbox_fail(&edev->mailbox);
389
390 return ret;
391}
392
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100393static void ethosu_watchdog_callback(struct ethosu_watchdog *wdog)
394{
395 struct ethosu_device *edev =
396 container_of(wdog, struct ethosu_device, watchdog);
397
398 mutex_lock(&edev->mutex);
399
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100400 dev_warn(edev->dev, "Device watchdog timeout. ping_count=%u",
401 edev->mailbox.ping_count);
402
403 if (edev->mailbox.ping_count < 1)
404 ethosu_mailbox_ping(&edev->mailbox);
405 else
406 ethosu_firmware_reset(edev);
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100407
408 mutex_unlock(&edev->mutex);
409}
410
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200411static int ethosu_open(struct inode *inode,
412 struct file *file)
413{
414 struct ethosu_device *edev =
415 container_of(inode->i_cdev, struct ethosu_device, cdev);
416
417 file->private_data = edev;
418
419 dev_info(edev->dev, "Opening device node.\n");
420
421 return nonseekable_open(inode, file);
422}
423
424static long ethosu_ioctl(struct file *file,
425 unsigned int cmd,
426 unsigned long arg)
427{
428 struct ethosu_device *edev = file->private_data;
429 void __user *udata = (void __user *)arg;
430 int ret = -EINVAL;
431
432 ret = mutex_lock_interruptible(&edev->mutex);
433 if (ret)
434 return ret;
435
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100436 dev_info(edev->dev, "Ioctl. cmd=0x%x, arg=0x%lx\n", cmd, arg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200437
438 switch (cmd) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100439 case ETHOSU_IOCTL_VERSION_REQ:
440 dev_info(edev->dev, "Ioctl: Send version request\n");
441 ret = ethosu_mailbox_version_request(&edev->mailbox);
442 break;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200443 case ETHOSU_IOCTL_CAPABILITIES_REQ:
444 dev_info(edev->dev, "Ioctl: Send capabilities request\n");
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100445 ret = ethosu_capabilities_request(edev, udata);
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200446 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200447 case ETHOSU_IOCTL_PING: {
448 dev_info(edev->dev, "Ioctl: Send ping\n");
449 ret = ethosu_mailbox_ping(&edev->mailbox);
450 break;
451 }
452 case ETHOSU_IOCTL_BUFFER_CREATE: {
453 struct ethosu_uapi_buffer_create uapi;
454
455 dev_info(edev->dev, "Ioctl: Buffer create\n");
456
457 if (copy_from_user(&uapi, udata, sizeof(uapi)))
458 break;
459
460 dev_info(edev->dev, "Ioctl: Buffer. capacity=%u\n",
461 uapi.capacity);
462
463 ret = ethosu_buffer_create(edev, uapi.capacity);
464 break;
465 }
466 case ETHOSU_IOCTL_NETWORK_CREATE: {
467 struct ethosu_uapi_network_create uapi;
468
469 if (copy_from_user(&uapi, udata, sizeof(uapi)))
470 break;
471
472 dev_info(edev->dev, "Ioctl: Network. fd=%u\n", uapi.fd);
473
474 ret = ethosu_network_create(edev, &uapi);
475 break;
476 }
477 default: {
478 dev_err(edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
479 cmd, arg);
480 break;
481 }
482 }
483
484 mutex_unlock(&edev->mutex);
485
486 return ret;
487}
488
489static void ethosu_mbox_rx(void *user_arg)
490{
491 struct ethosu_device *edev = user_arg;
492 int ret;
493
494 mutex_lock(&edev->mutex);
495
496 do {
497 ret = ethosu_handle_msg(edev);
Jonny Svärd7c24c772021-01-14 19:53:17 +0100498 if (ret && ret != -ENOMSG)
499 /* Need to start over in case of error, empty the queue
500 * by fast-forwarding read position to write position.
501 * */
502 ethosu_mailbox_reset(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200503 } while (ret == 0);
504
505 mutex_unlock(&edev->mutex);
506}
507
508int ethosu_dev_init(struct ethosu_device *edev,
509 struct device *dev,
510 struct class *class,
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200511 dev_t devt,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200512 struct resource *in_queue,
513 struct resource *out_queue)
514{
515 static const struct file_operations fops = {
516 .owner = THIS_MODULE,
517 .open = &ethosu_open,
518 .unlocked_ioctl = &ethosu_ioctl,
519#ifdef CONFIG_COMPAT
520 .compat_ioctl = &ethosu_ioctl,
521#endif
522 };
523 struct device *sysdev;
524 int ret;
525
526 edev->dev = dev;
527 edev->class = class;
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200528 edev->devt = devt;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200529 mutex_init(&edev->mutex);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100530
531 edev->reset = devm_reset_control_get_by_index(edev->dev, 0);
532 if (IS_ERR(edev->reset))
533 dev_warn(edev->dev, "No reset control found for this device.");
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200534
535 ret = of_reserved_mem_device_init(edev->dev);
536 if (ret)
537 return ret;
538
539 dma_set_mask_and_coherent(edev->dev, DMA_BIT_MASK(DMA_ADDR_BITS));
540
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100541 ret = ethosu_watchdog_init(&edev->watchdog, dev,
542 ethosu_watchdog_callback);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200543 if (ret)
544 goto release_reserved_mem;
545
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100546 ret = ethosu_mailbox_init(&edev->mailbox, dev, in_queue, out_queue,
547 ethosu_mbox_rx, edev, &edev->watchdog);
548 if (ret)
549 goto deinit_watchdog;
550
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200551 cdev_init(&edev->cdev, &fops);
552 edev->cdev.owner = THIS_MODULE;
553
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200554 ret = cdev_add(&edev->cdev, edev->devt, 1);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200555 if (ret) {
556 dev_err(edev->dev, "Failed to add character device.\n");
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200557 goto deinit_mailbox;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200558 }
559
560 sysdev = device_create(edev->class, NULL, edev->devt, edev,
Kristofer Jonsson4aec3762021-10-13 17:09:27 +0200561 "ethosu%d", MINOR(edev->devt));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200562 if (IS_ERR(sysdev)) {
563 dev_err(edev->dev, "Failed to create device.\n");
564 ret = PTR_ERR(sysdev);
565 goto del_cdev;
566 }
567
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100568 ethosu_firmware_reset(edev);
569
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200570 dev_info(edev->dev,
571 "Created Arm Ethos-U device. name=%s, major=%d, minor=%d\n",
572 dev_name(sysdev), MAJOR(edev->devt), MINOR(edev->devt));
573
574 return 0;
575
576del_cdev:
577 cdev_del(&edev->cdev);
578
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200579deinit_mailbox:
580 ethosu_mailbox_deinit(&edev->mailbox);
581
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100582deinit_watchdog:
583 ethosu_watchdog_deinit(&edev->watchdog);
584
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200585release_reserved_mem:
586 of_reserved_mem_device_release(edev->dev);
587
588 return ret;
589}
590
591void ethosu_dev_deinit(struct ethosu_device *edev)
592{
593 ethosu_mailbox_deinit(&edev->mailbox);
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100594 ethosu_watchdog_deinit(&edev->watchdog);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200595 device_destroy(edev->class, edev->cdev.dev);
596 cdev_del(&edev->cdev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200597 of_reserved_mem_device_release(edev->dev);
598
599 dev_info(edev->dev, "%s\n", __FUNCTION__);
600}