summaryrefslogtreecommitdiff
path: root/drivers/edgetpu/edgetpu-mailbox.c
blob: eedde54d623f11bb8201994e9da33a471ea00e86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
// SPDX-License-Identifier: GPL-2.0
/*
 * Utility functions of mailbox protocol for Edge TPU ML accelerator.
 *
 * Copyright (C) 2019 Google, Inc.
 */

#include <asm/page.h>
#include <linux/bits.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/mmzone.h> /* MAX_ORDER_NR_PAGES */
#include <linux/slab.h>

#include "edgetpu-device-group.h"
#include "edgetpu-iremap-pool.h"
#include "edgetpu-kci.h"
#include "edgetpu-mailbox.h"
#include "edgetpu-mmu.h"
#include "edgetpu-sw-watchdog.h"
#include "edgetpu-wakelock.h"
#include "edgetpu.h"

/*
 * Checks if @size is a valid circular queue size, which should be a positive
 * number and less than or equal to MAX_QUEUE_SIZE.
 */
static inline bool valid_circular_queue_size(u32 size)
{
	if (!size || size > MAX_QUEUE_SIZE)
		return false;
	return true;
}

/* Return context ID for mailbox. */
static inline enum edgetpu_context_id
edgetpu_mailbox_context_id(struct edgetpu_mailbox *mailbox)
{
	if (!mailbox)
		return EDGETPU_CONTEXT_INVALID;
	return EDGETPU_CONTEXT_VII_BASE + mailbox->mailbox_id - 1;
}

/* Sets mailbox->cmd_queue_tail and corresponding CSR on device. */
static void edgetpu_mailbox_set_cmd_queue_tail(struct edgetpu_mailbox *mailbox,
					       u32 value)
{
	mailbox->cmd_queue_tail = value;
	EDGETPU_MAILBOX_CMD_QUEUE_WRITE_SYNC(mailbox, tail, value);
}

/* Sets mailbox->resp_queue_head and corresponding CSR on device. */
static void edgetpu_mailbox_set_resp_queue_head(struct edgetpu_mailbox *mailbox,
						u32 value)
{
	mailbox->resp_queue_head = value;
	EDGETPU_MAILBOX_RESP_QUEUE_WRITE(mailbox, head, value);
}

/*
 * Allocates and returns a mailbox given the index of this mailbox,
 * also enables the mailbox.
 *
 * Caller holds mgr->mailboxes_lock.
 */
static struct edgetpu_mailbox *
edgetpu_mailbox_create_locked(struct edgetpu_mailbox_manager *mgr, uint index)
{
	struct edgetpu_mailbox *mailbox = kzalloc(sizeof(*mailbox), GFP_ATOMIC);

	if (!mailbox)
		return ERR_PTR(-ENOMEM);
	mailbox->mailbox_id = index;
	mailbox->etdev = mgr->etdev;
	mailbox->context_csr_base = mgr->get_context_csr_base(index);
	mailbox->cmd_queue_csr_base = mgr->get_cmd_queue_csr_base(index);
	mailbox->resp_queue_csr_base = mgr->get_resp_queue_csr_base(index);
	edgetpu_mailbox_init_doorbells(mailbox);

	return mailbox;
}

/* Caller must hold @mgr->mailboxes_lock. */
static int edgetpu_mailbox_remove_locked(struct edgetpu_mailbox_manager *mgr,
					 struct edgetpu_mailbox *mailbox)
{
	/* simple security checks */
	if (mailbox->mailbox_id >= mgr->num_mailbox ||
	    mgr->mailboxes[mailbox->mailbox_id] != mailbox) {
		return -EINVAL;
	}

	mgr->mailboxes[mailbox->mailbox_id] = NULL;
	/* KCI mailbox is a special case */
	if (mailbox->mailbox_id == KERNEL_MAILBOX_INDEX)
		edgetpu_kci_release(mgr->etdev, mailbox->internal.kci);
	kfree(mailbox);
	return 0;
}

/*
 * Disables the @index-th mailbox via setting CSR. Doesn't need
 * @mgr->mailboxes[index] be allocated.
 */
static void edgetpu_mailbox_disable_ith(struct edgetpu_mailbox_manager *mgr,
					uint index)
{
	struct edgetpu_mailbox mbox = {
		.etdev = mgr->etdev,
		.context_csr_base = mgr->get_context_csr_base(index),
	};

	edgetpu_mailbox_disable(&mbox);
}

static void edgetpu_vii_irq_handler(struct edgetpu_mailbox *mailbox)
{
	if (mailbox->internal.group)
		edgetpu_group_notify(mailbox->internal.group,
				     EDGETPU_EVENT_RESPDATA);
}

/*
 * Increases the command queue tail by @inc.
 *
 * The queue uses the mirrored circular buffer arrangement. Each index (head and
 * tail) has a wrap bit, represented by the constant CIRCULAR_QUEUE_WRAP_BIT.
 * Whenever an index is increased and will exceed the end of the queue, the wrap
 * bit is xor-ed.
 *
 * This method will update both mailbox->cmd_queue_tail and CSR on device.
 *
 * Caller ensures @inc is less than the space remain in the command queue.
 */
void edgetpu_mailbox_inc_cmd_queue_tail(struct edgetpu_mailbox *mailbox,
					u32 inc)
{
	u32 new_tail;

	new_tail = circular_queue_inc(mailbox->cmd_queue_tail, inc,
				      mailbox->cmd_queue_size);
	edgetpu_mailbox_set_cmd_queue_tail(mailbox, new_tail);
}

/*
 * Increases the response queue head by @inc.
 *
 * The queue uses the mirrored circular buffer arrangement. Each index (head and
 * tail) has a wrap bit, represented by the constant CIRCULAR_QUEUE_WRAP_BIT.
 * Whenever an index is increased and will exceed the end of the queue, the wrap
 * bit is xor-ed.
 *
 * This method will update both mailbox->resp_queue_head and CSR on device.
 *
 * Caller ensures @inc is less than the distance between resp_head and
 * resp_tail.
 */
void edgetpu_mailbox_inc_resp_queue_head(struct edgetpu_mailbox *mailbox,
					 u32 inc)
{
	u32 new_head;

	new_head = circular_queue_inc(mailbox->resp_queue_head, inc,
				      mailbox->resp_queue_size);
	edgetpu_mailbox_set_resp_queue_head(mailbox, new_head);
}

/*
 * Sets address and size of queue.
 *
 * Sets the queue address with @addr, a 36-bit address, and with size @size in
 * units of number of elements.
 *
 * Returns 0 on success.
 * -EINVAL is returned if @addr or @size is invalid.
 */
int edgetpu_mailbox_set_queue(struct edgetpu_mailbox *mailbox,
			      enum mailbox_queue_type type, u64 addr, u32 size)
{
	u32 low = addr & 0xffffffff;
	u32 high = addr >> 32;

	if (!valid_circular_queue_size(size))
		return -EINVAL;
	/* addr is a 36-bit address, checks if the higher bits are clear */
	if (high & 0xfffffff0)
		return -EINVAL;

	switch (type) {
	case MAILBOX_CMD_QUEUE:
		EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, cmd_queue_address_low,
					      low);
		EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, cmd_queue_address_high,
					      high);
		EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, cmd_queue_size, size);
		mailbox->cmd_queue_size = size;
		edgetpu_mailbox_set_cmd_queue_tail(mailbox, 0);
		EDGETPU_MAILBOX_CMD_QUEUE_WRITE(mailbox, head, 0);
		break;
	case MAILBOX_RESP_QUEUE:
		EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, resp_queue_address_low,
					      low);
		EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, resp_queue_address_high,
					      high);
		EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, resp_queue_size, size);
		mailbox->resp_queue_size = size;
		edgetpu_mailbox_set_resp_queue_head(mailbox, 0);
		EDGETPU_MAILBOX_RESP_QUEUE_WRITE(mailbox, tail, 0);
		break;
	}

	return 0;
}

/* Reset mailbox queues, clear out any commands/responses left from before. */
void edgetpu_mailbox_reset(struct edgetpu_mailbox *mailbox)
{
	edgetpu_mailbox_disable(mailbox);
	EDGETPU_MAILBOX_CMD_QUEUE_WRITE(mailbox, head, 0);
	edgetpu_mailbox_set_cmd_queue_tail(mailbox, 0);
	edgetpu_mailbox_set_resp_queue_head(mailbox, 0);
	EDGETPU_MAILBOX_RESP_QUEUE_WRITE(mailbox, tail, 0);
	edgetpu_mailbox_enable(mailbox);
}

/* Sets the priority of @mailbox. */
void edgetpu_mailbox_set_priority(struct edgetpu_mailbox *mailbox, u32 priority)
{
	EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, priority, priority);
}

struct edgetpu_mailbox *
edgetpu_mailbox_vii_add(struct edgetpu_mailbox_manager *mgr, uint id)
{
	struct edgetpu_mailbox *mailbox = NULL;
	unsigned long flags;

	write_lock_irqsave(&mgr->mailboxes_lock, flags);
	if (id == 0) {
		uint i;

		for (i = mgr->vii_index_from; i < mgr->vii_index_to; i++) {
			if (!mgr->mailboxes[i]) {
				id = i;
				break;
			}
		}
	} else {
		/* no mailbox available - returns busy */
		if (id < mgr->vii_index_from || id >= mgr->vii_index_to ||
		    mgr->mailboxes[id])
			id = 0;
	}

	/* no empty slot found */
	if (id == 0) {
		mailbox = ERR_PTR(-EBUSY);
	} else {
		mailbox = edgetpu_mailbox_create_locked(mgr, id);
		if (!IS_ERR(mailbox)) {
			mgr->mailboxes[id] = mailbox;
			mailbox->handle_irq = edgetpu_vii_irq_handler;
		}
	}
	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);
	return mailbox;
}

/*
 * Requests the first @n P2P mailboxes, where @n should be the number of devices
 * in a virtual device group.
 *
 * The array of requested mailboxes will be assigned to @mailboxes on success.
 * @mailboxes must have dimension @n, @mailboxes[@skip_i] will be set to NULL.
 *
 * Returns 0 on success, or a negative errno on error.
 * Returns -EBUSY if any mailbox is using.
 *
 * Caller calls edgetpu_mailbox_enable() to enable the returned mailboxes.
 */
int edgetpu_mailbox_p2p_batch(struct edgetpu_mailbox_manager *mgr, uint n,
			      uint skip_i, struct edgetpu_mailbox **mailboxes)
{
	uint i, j;
	int ret;
	struct edgetpu_mailbox *mailbox;
	unsigned long flags;

	if (mgr->p2p_index_to - mgr->p2p_index_from < n)
		return -EINVAL;

	write_lock_irqsave(&mgr->mailboxes_lock, flags);

	memset(mailboxes, 0, sizeof(*mailboxes) * n);
	for (i = mgr->p2p_index_from, j = 0; j < n; i++, j++) {
		if (mgr->mailboxes[i]) {
			ret = -EBUSY;
			goto release;
		}
	}

	for (i = mgr->p2p_index_from, j = 0; j < n; i++, j++) {
		if (j == skip_i) {
			edgetpu_mailbox_disable_ith(mgr, i);
			continue;
		}

		mailbox = edgetpu_mailbox_create_locked(mgr, i);
		if (IS_ERR(mailbox)) {
			ret = PTR_ERR(mailbox);
			goto release;
		}
		mailboxes[j] = mgr->mailboxes[i] = mailbox;
	}

	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);
	return 0;

release:
	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);

	for (i = 0; i < n; i++) {
		if (mailboxes[i])
			edgetpu_mailbox_remove(mgr, mailboxes[i]);
		mailboxes[i] = NULL;
	}

	return ret;
}

/*
 * Every mailbox manager can allocate one mailbox for KCI to use.
 * -EBUSY is returned if the KCI mailbox is allocated and hasn't been removed
 * via edgetpu_mailbox_remove().
 */
struct edgetpu_mailbox *edgetpu_mailbox_kci(struct edgetpu_mailbox_manager *mgr)
{
	struct edgetpu_mailbox *mailbox;
	unsigned long flags;

	write_lock_irqsave(&mgr->mailboxes_lock, flags);
	if (mgr->mailboxes[KERNEL_MAILBOX_INDEX]) {
		mailbox = ERR_PTR(-EBUSY);
		goto out;
	}

	mailbox = edgetpu_mailbox_create_locked(mgr, KERNEL_MAILBOX_INDEX);
	if (!IS_ERR(mailbox))
		mgr->mailboxes[KERNEL_MAILBOX_INDEX] = mailbox;

out:
	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);
	return mailbox;
}

/*
 * Removes a mailbox from the manager.
 * Returns 0 on success.
 */
int edgetpu_mailbox_remove(struct edgetpu_mailbox_manager *mgr, struct edgetpu_mailbox *mailbox)
{
	unsigned long flags;
	int ret;

	write_lock_irqsave(&mgr->mailboxes_lock, flags);
	ret = edgetpu_mailbox_remove_locked(mgr, mailbox);
	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);

	return ret;
}

/*
 * The queue size of edgetpu_mailbox_attr has units in KB, convert it to use the
 * element size here.
 *
 * Returns a negative errno on error, or the converted size.
 */
static int convert_runtime_queue_size_to_fw(u32 queue_size, u32 element_size)
{
	const u32 runtime_unit = 1024;
	u32 ret;

	/* zero size is not allowed */
	if (queue_size == 0 || element_size == 0)
		return -EINVAL;
	/* A quick check to prevent the queue allocation failure. */
	if (queue_size > (MAX_ORDER_NR_PAGES << PAGE_SHIFT) / runtime_unit)
		return -ENOMEM;
	/*
	 * Kernel doesn't care whether queue_size * runtime_unit is a multiple
	 * of element_size.
	 */
	ret = queue_size * runtime_unit / element_size;
	/* hardware limitation */
	if (ret == 0 || ret > MAX_QUEUE_SIZE)
		return -EINVAL;
	return ret;
}

int edgetpu_mailbox_validate_attr(const struct edgetpu_mailbox_attr *attr)
{
	int size;

	size = convert_runtime_queue_size_to_fw(attr->cmd_queue_size,
						attr->sizeof_cmd);
	if (size < 0)
		return size;
	size = convert_runtime_queue_size_to_fw(attr->resp_queue_size,
						attr->sizeof_resp);
	if (size < 0)
		return size;
	return 0;
}

int edgetpu_mailbox_init_vii(struct edgetpu_vii *vii,
			     struct edgetpu_device_group *group)
{
	int cmd_queue_size, resp_queue_size;
	struct edgetpu_mailbox_manager *mgr = group->etdev->mailbox_manager;
	struct edgetpu_mailbox *mailbox;
	const struct edgetpu_mailbox_attr *attr = &group->mbox_attr;
	int ret;

	if (!group->etdomain || group->etdomain->pasid == IOMMU_PASID_INVALID)
		mailbox = edgetpu_mailbox_vii_add(mgr, 0);
	else
		mailbox = edgetpu_mailbox_vii_add(mgr, group->etdomain->pasid);
	if (IS_ERR(mailbox))
		return PTR_ERR(mailbox);

	cmd_queue_size = convert_runtime_queue_size_to_fw(attr->cmd_queue_size,
							  attr->sizeof_cmd);
	resp_queue_size = convert_runtime_queue_size_to_fw(
		attr->resp_queue_size, attr->sizeof_resp);

	edgetpu_mailbox_set_priority(mailbox, attr->priority);
	EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox,
				      cmd_queue_tail_doorbell_enable,
				      attr->cmdq_tail_doorbell);

	ret = edgetpu_mailbox_alloc_queue(group->etdev, mailbox, cmd_queue_size,
					  attr->sizeof_cmd, MAILBOX_CMD_QUEUE,
					  &vii->cmd_queue_mem);
	if (ret) {
		edgetpu_mailbox_remove(mgr, mailbox);
		return ret;
	}

	etdev_dbg(group->etdev,
		  "%s: mbox %u cmdq iova=0x%llx dma=%pad\n",
		  __func__, mailbox->mailbox_id, vii->cmd_queue_mem.tpu_addr,
		  &vii->cmd_queue_mem.dma_addr);
	ret = edgetpu_mailbox_alloc_queue(group->etdev, mailbox,
					  resp_queue_size, attr->sizeof_resp,
					  MAILBOX_RESP_QUEUE,
					  &vii->resp_queue_mem);

	if (ret) {
		edgetpu_mailbox_free_queue(group->etdev, mailbox,
					   &vii->cmd_queue_mem);
		edgetpu_mailbox_remove(mgr, mailbox);
		return ret;
	}

	etdev_dbg(group->etdev,
		  "%s: mbox %u rspq iova=0x%llx dma=%pad\n",
		  __func__, mailbox->mailbox_id, vii->resp_queue_mem.tpu_addr,
		  &vii->resp_queue_mem.dma_addr);
	mailbox->internal.group = edgetpu_device_group_get(group);
	vii->etdev = group->etdev;
	vii->mailbox = mailbox;
	edgetpu_mailbox_enable(mailbox);
	return 0;
}

void edgetpu_mailbox_remove_vii(struct edgetpu_vii *vii)
{
	struct edgetpu_dev *etdev;

	etdev = vii->etdev;
	edgetpu_mailbox_free_queue(etdev, vii->mailbox, &vii->cmd_queue_mem);
	edgetpu_mailbox_free_queue(etdev, vii->mailbox, &vii->resp_queue_mem);
	if (vii->mailbox) {
		if (!vii->mailbox->internal.group->dev_inaccessible)
			edgetpu_mailbox_disable(vii->mailbox);
		edgetpu_device_group_put(vii->mailbox->internal.group);
		edgetpu_mailbox_remove(etdev->mailbox_manager, vii->mailbox);
		vii->mailbox = NULL;
	}
}

/*
 * Allocates memory for a queue.
 *
 * The total size (in bytes) of queue is @queue_size * @unit.
 * CSRs of @mailbox include queue_size and queue_address will be set on success.
 * @mem->dma_addr, @mem->vaddr, and @mem->size will be set.
 *
 * Returns 0 on success, or a negative errno on error.
 */
int edgetpu_mailbox_alloc_queue(struct edgetpu_dev *etdev,
				struct edgetpu_mailbox *mailbox, u32 queue_size,
				u32 unit, enum mailbox_queue_type type,
				edgetpu_queue_mem *mem)
{
	u32 size = unit * queue_size;
	int ret;

	/* checks integer overflow */
	if (queue_size > SIZE_MAX / unit)
		return -ENOMEM;

	/* Align queue size to page size for TPU MMU map. */
	size = __ALIGN_KERNEL(size, PAGE_SIZE);
	ret = edgetpu_iremap_alloc(etdev, size, mem,
				   edgetpu_mailbox_context_id(mailbox));
	if (ret)
		return ret;

	ret = edgetpu_mailbox_set_queue(mailbox, type, mem->tpu_addr,
					queue_size);
	if (ret) {
		edgetpu_iremap_free(etdev, mem,
				    edgetpu_mailbox_context_id(mailbox));
		return ret;
	}
	return 0;
}

/*
 * Releases the queue memory previously allocated with
 * edgetpu_mailbox_alloc_queue().
 *
 * Does nothing if @mem->vaddr is NULL.
 */
void edgetpu_mailbox_free_queue(struct edgetpu_dev *etdev,
				struct edgetpu_mailbox *mailbox,
				edgetpu_queue_mem *mem)
{
	if (!mem->vaddr)
		return;

	edgetpu_iremap_free(etdev, mem, edgetpu_mailbox_context_id(mailbox));
}

/*
 * Creates a mailbox manager, one edgetpu device has one manager.
 */
struct edgetpu_mailbox_manager *
edgetpu_mailbox_create_mgr(struct edgetpu_dev *etdev,
			   const struct edgetpu_mailbox_manager_desc *desc)
{
	struct edgetpu_mailbox_manager *mgr;
	uint total = 0;

	total += 1; /* KCI mailbox */
	total += desc->num_vii_mailbox;
	total += desc->num_p2p_mailbox;
	total += desc->num_ext_mailbox;
	if (total > desc->num_mailbox)
		return ERR_PTR(-EINVAL);
	mgr = devm_kzalloc(etdev->dev, sizeof(*mgr), GFP_KERNEL);
	if (!mgr)
		return ERR_PTR(-ENOMEM);

	mgr->etdev = etdev;
	mgr->num_mailbox = desc->num_mailbox;
	/* index 0 is reserved for KCI */
	mgr->vii_index_from = 1;
	mgr->vii_index_to = mgr->vii_index_from + desc->num_vii_mailbox;
	mgr->p2p_index_from = mgr->vii_index_to;
	mgr->p2p_index_to = mgr->p2p_index_from + desc->num_p2p_mailbox;
	mgr->ext_index_from = mgr->p2p_index_to;
	mgr->ext_index_to = mgr->ext_index_from + desc->num_ext_mailbox;

	mgr->get_context_csr_base = desc->get_context_csr_base;
	mgr->get_cmd_queue_csr_base = desc->get_cmd_queue_csr_base;
	mgr->get_resp_queue_csr_base = desc->get_resp_queue_csr_base;

	mgr->mailboxes = devm_kcalloc(etdev->dev, mgr->num_mailbox,
				      sizeof(*mgr->mailboxes), GFP_KERNEL);
	if (!mgr->mailboxes)
		return ERR_PTR(-ENOMEM);
	rwlock_init(&mgr->mailboxes_lock);
	mutex_init(&mgr->open_devices.lock);

	return mgr;
}

/* All requested mailboxes will be disabled and freed. */
void edgetpu_mailbox_remove_all(struct edgetpu_mailbox_manager *mgr)
{
	uint i;
	struct edgetpu_mailbox *kci_mailbox = NULL;
	unsigned long flags;

	if (IS_ERR_OR_NULL(mgr))
		return;
	write_lock_irqsave(&mgr->mailboxes_lock, flags);
	for (i = 0; i < mgr->num_mailbox; i++) {
		struct edgetpu_mailbox *mailbox = mgr->mailboxes[i];

		if (mailbox) {
			edgetpu_mailbox_disable(mailbox);
			/* KCI needs special handling */
			if (i == KERNEL_MAILBOX_INDEX)
				kci_mailbox = mailbox;
			else
				kfree(mailbox);
			mgr->mailboxes[i] = NULL;
		}
	}
	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);

	/* Cancel KCI worker outside the lock, then free KCI mailbox. */
	if (kci_mailbox) {
		edgetpu_kci_release(mgr->etdev,
				    kci_mailbox->internal.kci);
		kfree(kci_mailbox);
	}
}

/*
 * The interrupt handler for KCI and VII mailboxes.
 *
 * This handler loops through such mailboxes with an interrupt pending and
 *  invokes their IRQ handlers.
 */
irqreturn_t edgetpu_mailbox_handle_irq(struct edgetpu_mailbox_manager *mgr)
{
	struct edgetpu_mailbox *mailbox;
	uint i;

	if (!mgr)
		return IRQ_NONE;

	read_lock(&mgr->mailboxes_lock);
	for (i = 0; i < mgr->vii_index_to; i++) {
		mailbox = mgr->mailboxes[i];
		if (!mailbox)
			continue;
		if (!EDGETPU_MAILBOX_RESP_QUEUE_READ(mailbox, doorbell_status))
			continue;
		EDGETPU_MAILBOX_RESP_QUEUE_WRITE(mailbox, doorbell_clear, 1);
		etdev_dbg(mgr->etdev, "mbox %u resp doorbell irq tail=%u\n",
			  i, EDGETPU_MAILBOX_RESP_QUEUE_READ(mailbox, tail));
		if (mailbox->handle_irq)
			mailbox->handle_irq(mailbox);
	}
	read_unlock(&mgr->mailboxes_lock);

	return IRQ_HANDLED;
}

void edgetpu_mailbox_init_doorbells(struct edgetpu_mailbox *mailbox)
{
	/* Clear any stale doorbells requested */
	EDGETPU_MAILBOX_RESP_QUEUE_WRITE(mailbox, doorbell_clear, 1);
	EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, cmd_queue_doorbell_clear, 1);
	/* Enable the command and response doorbell interrupts */
	EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, cmd_queue_doorbell_enable, 1);
	EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, resp_queue_doorbell_enable, 1);
}

void edgetpu_mailbox_reset_vii(struct edgetpu_mailbox_manager *mgr)
{
	uint i;
	unsigned long flags;

	write_lock_irqsave(&mgr->mailboxes_lock, flags);
	for (i = mgr->vii_index_from; i < mgr->vii_index_to; i++) {
		struct edgetpu_mailbox *mbox = mgr->mailboxes[i];

		if (!mbox)
			continue;
		edgetpu_mailbox_reset(mbox);
		edgetpu_mailbox_disable(mbox);
		edgetpu_mailbox_init_doorbells(mbox);
	}
	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);
}

void edgetpu_mailbox_reinit_vii(struct edgetpu_device_group *group)
{
	int cmd_queue_size, resp_queue_size;
	struct edgetpu_mailbox *mailbox = group->vii.mailbox;
	const struct edgetpu_mailbox_attr *attr = &group->mbox_attr;

	cmd_queue_size = convert_runtime_queue_size_to_fw(attr->cmd_queue_size,
							  attr->sizeof_cmd);
	resp_queue_size = convert_runtime_queue_size_to_fw(
		attr->resp_queue_size, attr->sizeof_resp);

	etdev_dbg(group->etdev, "Restoring vii. workload_id=%u mbox_id=%u\n",
		  group->workload_id, mailbox->mailbox_id);

	etdev_dbg(group->etdev, "Priority: %d\n", attr->priority);
	etdev_dbg(group->etdev, "Tail doorbell %s",
		  attr->cmdq_tail_doorbell ? "enabled" : "disabled");
	etdev_dbg(group->etdev, "cmd queue: addr=%llX size=%u\n",
		  group->vii.cmd_queue_mem.tpu_addr, cmd_queue_size);
	etdev_dbg(group->etdev, "resp queue: addr=%llX size=%u\n",
		  group->vii.resp_queue_mem.tpu_addr, resp_queue_size);

	edgetpu_mailbox_set_priority(mailbox, attr->priority);
	EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, cmd_queue_tail_doorbell_enable,
				      attr->cmdq_tail_doorbell);
	edgetpu_mailbox_set_queue(mailbox, MAILBOX_CMD_QUEUE,
				  group->vii.cmd_queue_mem.tpu_addr,
				  cmd_queue_size);
	edgetpu_mailbox_set_queue(mailbox, MAILBOX_RESP_QUEUE,
				  group->vii.resp_queue_mem.tpu_addr,
				  resp_queue_size);
	edgetpu_mailbox_enable(mailbox);
}

void edgetpu_mailbox_restore_active_vii_queues(struct edgetpu_dev *etdev)
{
	struct edgetpu_list_group *l;
	struct edgetpu_device_group *group;
	struct edgetpu_device_group **groups;
	size_t i, n = 0;

	mutex_lock(&etdev->groups_lock);
	groups = kmalloc_array(etdev->n_groups, sizeof(*groups), GFP_KERNEL);
	if (unlikely(!groups)) {
		/*
		 * Either the runtime is misbehaving (creates tons of groups),
		 * or the system is indeed OOM - we give up this restore
		 * process, which makes the runtime unable to communicate with
		 * the device through VII.
		 */
		mutex_unlock(&etdev->groups_lock);
		return;
	}
	/*
	 * Fetch the groups into an array to restore the VII without holding
	 * etdev->groups_lock. To prevent the potential deadlock that
	 * edgetpu_device_group_add() holds group->lock then etdev->groups_lock.
	 */
	etdev_for_each_group(etdev, l, group) {
		/*
		 * Quick skip without holding group->lock.
		 * Disbanded groups can never go back to the normal state.
		 */
		if (edgetpu_device_group_is_disbanded(group))
			continue;
		/*
		 * Increase the group reference to prevent the group being
		 * released after we release groups_lock.
		 */
		groups[n++] = edgetpu_device_group_get(group);
	}
	mutex_unlock(&etdev->groups_lock);

	/*
	 * We are not holding @etdev->groups_lock, what may race is:
	 *   1. The group is disbanding and being removed from @etdev.
	 *   2. A new group is adding to @etdev
	 *
	 * For (1.) the group will be marked as DISBANDED, so we check whether
	 * the group is finalized before performing VII re-init.
	 *
	 * For (2.), adding group to @etdev (edgetpu_device_group_add()) has
	 * nothing to do with VII, its VII will be set when the group is
	 * finalized.
	 */
	for (i = 0; i < n; i++) {
		group = groups[i];
		mutex_lock(&group->lock);
		/*
		 * If the group is just finalized or has mailbox attached in
		 * another process, this re-init is redundant but isn't harmful.
		 */
		if (edgetpu_group_finalized_and_attached(group))
			edgetpu_mailbox_reinit_vii(group);
		mutex_unlock(&group->lock);
		edgetpu_device_group_put(group);
	}
	kfree(groups);
}

static int edgetpu_mailbox_external_alloc_queue_batch(struct edgetpu_external_mailbox *ext_mailbox)
{
	int ret, i;
	struct edgetpu_mailbox *mailbox;
	struct edgetpu_mailbox_attr attr;
	struct edgetpu_mailbox_descriptor *desc;

	attr = ext_mailbox->attr;

	for (i = 0; i < ext_mailbox->count; i++) {
		desc = &ext_mailbox->descriptors[i];
		mailbox = desc->mailbox;
		ret = edgetpu_mailbox_alloc_queue(ext_mailbox->etdev, mailbox, attr.cmd_queue_size,
						  attr.sizeof_cmd, MAILBOX_CMD_QUEUE,
						  &desc->cmd_queue_mem);
		if (ret)
			goto undo;

		ret = edgetpu_mailbox_alloc_queue(ext_mailbox->etdev, mailbox, attr.resp_queue_size,
						  attr.sizeof_resp, MAILBOX_RESP_QUEUE,
						  &desc->resp_queue_mem);
		if (ret) {
			edgetpu_mailbox_free_queue(ext_mailbox->etdev, mailbox,
						   &desc->cmd_queue_mem);
			goto undo;
		}
	}
	return 0;
undo:
	while (i--) {
		desc = &ext_mailbox->descriptors[i];
		mailbox = desc->mailbox;
		edgetpu_mailbox_free_queue(ext_mailbox->etdev, mailbox, &desc->cmd_queue_mem);
		edgetpu_mailbox_free_queue(ext_mailbox->etdev, mailbox, &desc->resp_queue_mem);
	}
	return ret;
}

static void edgetpu_mailbox_external_free_queue_batch(struct edgetpu_external_mailbox *ext_mailbox)
{
	u32 i;
	struct edgetpu_mailbox *mailbox;
	struct edgetpu_mailbox_descriptor *desc;

	for (i = 0; i < ext_mailbox->count; i++) {
		desc = &ext_mailbox->descriptors[i];
		mailbox = desc->mailbox;
		edgetpu_mailbox_free_queue(ext_mailbox->etdev, mailbox, &desc->cmd_queue_mem);
		edgetpu_mailbox_free_queue(ext_mailbox->etdev, mailbox, &desc->resp_queue_mem);
	}
}

/*
 * Checks if the indexes given for external mailboxes are in range of mailbox
 * manager(@mgr) managing the external mailboxes.
 */
static bool edgetpu_mailbox_external_check_range(struct edgetpu_mailbox_manager *mgr,
						 const int start, const int end)
{
	return (start <= end) && (mgr->ext_index_from <= start && mgr->ext_index_to > end);
}

static int edgetpu_mailbox_external_alloc(struct edgetpu_device_group *group,
					  struct edgetpu_external_mailbox_req *ext_mailbox_req)
{
	u32 i, j = 0;
	struct edgetpu_mailbox_manager *mgr = group->etdev->mailbox_manager;
	struct edgetpu_mailbox *mailbox;
	int ret = 0, c = 0, count;
	struct edgetpu_external_mailbox *ext_mailbox;
	struct edgetpu_mailbox_attr attr;
	unsigned long flags;

	if (!ext_mailbox_req)
		return -EINVAL;

	count = ext_mailbox_req->count;
	attr = ext_mailbox_req->attr;

	if (!edgetpu_mailbox_external_check_range(mgr, ext_mailbox_req->start,
						  ext_mailbox_req->end))
		return -ERANGE;

	ext_mailbox = kzalloc(sizeof(*ext_mailbox), GFP_KERNEL);
	if (!ext_mailbox)
		return -ENOMEM;

	ext_mailbox->descriptors =
		kcalloc(count, sizeof(struct edgetpu_mailbox_descriptor), GFP_KERNEL);
	if (!ext_mailbox->descriptors) {
		kfree(ext_mailbox);
		return -ENOMEM;
	}

	ext_mailbox->attr = attr;
	ext_mailbox->count = count;
	ext_mailbox->etdev = group->etdev;

	write_lock_irqsave(&mgr->mailboxes_lock, flags);
	for (i = ext_mailbox_req->start; i <= ext_mailbox_req->end; i++) {
		if (!mgr->mailboxes[i])
			c++;
	}
	if (c < count) {
		ret = -EBUSY;
		goto unlock;
	}

	for (i = ext_mailbox_req->start; i <= ext_mailbox_req->end && j < count; i++) {
		if (!mgr->mailboxes[i]) {
			mailbox = edgetpu_mailbox_create_locked(mgr, i);
			if (!IS_ERR(mailbox)) {
				mgr->mailboxes[i] = mailbox;
				ext_mailbox->descriptors[j++].mailbox = mailbox;
			} else {
				goto release;
			}
		}
	}

	ret = edgetpu_mailbox_external_alloc_queue_batch(ext_mailbox);
	if (ret)
		goto release;
	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);

	for (i = 0; i < count; i++) {
		mailbox = ext_mailbox->descriptors[i].mailbox;
		edgetpu_mailbox_set_priority(mailbox, attr.priority);
		EDGETPU_MAILBOX_CONTEXT_WRITE(mailbox, cmd_queue_tail_doorbell_enable,
					      attr.cmdq_tail_doorbell);
		mailbox->internal.group = edgetpu_device_group_get(group);
		edgetpu_mailbox_enable(mailbox);
	}
	group->ext_mailbox = ext_mailbox;
	return 0;
release:
	while (j--)
		edgetpu_mailbox_remove_locked(mgr, ext_mailbox->descriptors[j].mailbox);
unlock:
	write_unlock_irqrestore(&mgr->mailboxes_lock, flags);
	kfree(ext_mailbox->descriptors);
	kfree(ext_mailbox);
	return ret;
}

/* Caller must hold @group->lock. */
static void edgetpu_mailbox_external_free(struct edgetpu_device_group *group)
{
	struct edgetpu_mailbox_manager *mgr;
	struct edgetpu_mailbox *mailbox;
	struct edgetpu_external_mailbox *ext_mailbox;
	u32 i;

	ext_mailbox = group->ext_mailbox;
	if (!ext_mailbox)
		return;

	mgr = ext_mailbox->etdev->mailbox_manager;

	for (i = 0; i < ext_mailbox->count; i++)
		edgetpu_mailbox_disable(ext_mailbox->descriptors[i].mailbox);

	edgetpu_mailbox_external_free_queue_batch(ext_mailbox);

	for (i = 0; i < ext_mailbox->count; i++)  {
		mailbox = ext_mailbox->descriptors[i].mailbox;
		edgetpu_device_group_put(mailbox->internal.group);
		edgetpu_mailbox_remove(mgr, mailbox);
	}

	kfree(ext_mailbox->descriptors);
	kfree(ext_mailbox);
	group->ext_mailbox = NULL;
}

static int edgetpu_mailbox_external_alloc_enable(struct edgetpu_client *client,
						 struct edgetpu_external_mailbox_req *req)
{
	int ret = 0, i, id;
	struct edgetpu_external_mailbox *ext_mailbox = NULL;
	struct edgetpu_device_group *group;
	int vcid;

	mutex_lock(&client->group_lock);
	if (!client->group || !edgetpu_device_group_is_leader(client->group, client)) {
		mutex_unlock(&client->group_lock);
		return -EINVAL;
	}
	group = edgetpu_device_group_get(client->group);
	mutex_unlock(&client->group_lock);

	mutex_lock(&group->lock);
	if (!edgetpu_device_group_is_finalized(group)) {
		ret = -EINVAL;
		goto unlock;
	}

	if (group->ext_mailbox) {
		ret = -EEXIST;
		goto unlock;
	}

	ret = edgetpu_mailbox_external_alloc(group, req);
	if (ret)
		goto unlock;

	ext_mailbox = group->ext_mailbox;
	vcid = group->vcid;

	for (i = 0; i < ext_mailbox->count; i++) {
		id = ext_mailbox->descriptors[i].mailbox->mailbox_id;
		etdev_dbg(group->etdev, "Enabling mailbox: %d\n", id);
		ret = edgetpu_mailbox_activate(group->etdev, id, vcid, false);
		if (ret) {
			etdev_err(group->etdev, "Activate mailbox %d failed: %d", id, ret);
			break;
		}
	}

	if (ret) {
		while (i--) {
			id = ext_mailbox->descriptors[i].mailbox->mailbox_id;
			if (edgetpu_mailbox_deactivate(group->etdev, id))
				etdev_err(group->etdev, "Deactivate mailbox %d failed", id);
		}
		/*
		 * Deactivate only fails if f/w is unresponsive which will put group
		 * in errored state or mailbox physically disabled before requesting
		 * deactivate which will never be the case.
		 */
		edgetpu_mailbox_external_free(group);
	}
unlock:
	mutex_unlock(&group->lock);
	edgetpu_device_group_put(group);
	return ret;
}

static int edgetpu_mailbox_external_disable_free(struct edgetpu_client *client)
{
	struct edgetpu_device_group *group;

	mutex_lock(&client->group_lock);
	if (!client->group || !edgetpu_device_group_is_leader(client->group, client)) {
		mutex_unlock(&client->group_lock);
		return -EINVAL;
	}
	group = edgetpu_device_group_get(client->group);
	mutex_unlock(&client->group_lock);

	mutex_lock(&group->lock);
	edgetpu_mailbox_external_disable_free_locked(group);
	mutex_unlock(&group->lock);
	edgetpu_device_group_put(group);
	return 0;
}

void edgetpu_mailbox_external_disable_free_locked(struct edgetpu_device_group *group)
{
	u32 i, id;
	struct edgetpu_external_mailbox *ext_mailbox;

	ext_mailbox = group->ext_mailbox;
	if (!ext_mailbox)
		return;

	for (i = 0; i < ext_mailbox->count; i++) {
		id = ext_mailbox->descriptors[i].mailbox->mailbox_id;
		etdev_dbg(group->etdev, "Disabling mailbox: %d\n", id);
		if (edgetpu_mailbox_deactivate(group->etdev, id))
			etdev_err(group->etdev, "Deactivate mailbox %d failed", id);
	}
	/*
	 * Deactivate only fails if f/w is unresponsive which will put group
	 * in errored state or mailbox physically disabled before requesting
	 * deactivate which will never be the case.
	 */
	edgetpu_mailbox_external_free(group);
}

int edgetpu_mailbox_enable_ext(struct edgetpu_client *client, int mailbox_id,
			       struct edgetpu_external_mailbox_req *ext_mailbox_req)
{
	int ret;

	if (!edgetpu_wakelock_lock(client->wakelock)) {
		etdev_err(client->etdev, "Enabling mailbox %d needs wakelock acquired\n",
			  mailbox_id);
		edgetpu_wakelock_unlock(client->wakelock);
		return -EAGAIN;
	}

	if (mailbox_id == EDGETPU_MAILBOX_ID_USE_ASSOC) {
		ret = edgetpu_mailbox_external_alloc_enable(client, ext_mailbox_req);
		goto out;
	}
	etdev_dbg(client->etdev, "Enabling mailbox: %d\n", mailbox_id);

	ret = edgetpu_mailbox_activate(client->etdev, mailbox_id, -1, false);
	if (ret)
		etdev_err(client->etdev, "Activate mailbox %d failed: %d", mailbox_id, ret);
out:
	if (!ret)
		edgetpu_wakelock_inc_event_locked(client->wakelock,
						  EDGETPU_WAKELOCK_EVENT_EXT_MAILBOX);
	edgetpu_wakelock_unlock(client->wakelock);
	return ret;
}

int edgetpu_mailbox_disable_ext(struct edgetpu_client *client, int mailbox_id)
{
	int ret;

	if (!edgetpu_wakelock_lock(client->wakelock)) {
		etdev_err(client->etdev, "Disabling mailbox %d needs wakelock acquired\n",
			  mailbox_id);
		edgetpu_wakelock_unlock(client->wakelock);
		return -EAGAIN;
	}

	if (mailbox_id == EDGETPU_MAILBOX_ID_USE_ASSOC) {
		ret = edgetpu_mailbox_external_disable_free(client);
		goto out;
	}
	etdev_dbg(client->etdev, "Disabling mailbox: %d\n", mailbox_id);

	ret = edgetpu_mailbox_deactivate(client->etdev, mailbox_id);
	if (ret)
		etdev_err(client->etdev, "Deactivate mailbox %d failed: %d", mailbox_id, ret);

out:
	if (!ret)
		edgetpu_wakelock_dec_event_locked(client->wakelock,
						  EDGETPU_WAKELOCK_EVENT_EXT_MAILBOX);
	edgetpu_wakelock_unlock(client->wakelock);
	return ret;
}

int edgetpu_mailbox_activate(struct edgetpu_dev *etdev, u32 mailbox_id, s16 vcid, bool first_open)
{
	struct edgetpu_handshake *eh = &etdev->mailbox_manager->open_devices;
	const u32 bit = BIT(mailbox_id);
	int ret = 0;

	mutex_lock(&eh->lock);
	if (bit & ~eh->fw_state)
		ret = edgetpu_kci_open_device(etdev->kci, mailbox_id, vcid, first_open);
	if (!ret) {
		eh->state |= bit;
		eh->fw_state |= bit;
	}
	mutex_unlock(&eh->lock);
	/*
	 * We are observing OPEN_DEVICE KCI fails while other KCIs (usage update / shutdown) still
	 * succeed and no firmware crash is reported. Kick off the firmware restart when we are
	 * facing this and hope this can rescue the device from the bad state.
	 */
	if (ret == -ETIMEDOUT)
		edgetpu_watchdog_bite(etdev, false);
	return ret;
}

int edgetpu_mailbox_deactivate(struct edgetpu_dev *etdev, u32 mailbox_id)
{
	struct edgetpu_handshake *eh = &etdev->mailbox_manager->open_devices;
	const u32 bit = BIT(mailbox_id);
	int ret = 0;

	mutex_lock(&eh->lock);
	if (bit & eh->fw_state)
		ret = edgetpu_kci_close_device(etdev->kci, mailbox_id);
	if (!ret) {
		eh->state &= ~bit;
		eh->fw_state &= ~bit;
	}
	mutex_unlock(&eh->lock);
	return ret;
}

void edgetpu_handshake_clear_fw_state(struct edgetpu_handshake *eh)
{
	mutex_lock(&eh->lock);
	eh->fw_state = 0;
	mutex_unlock(&eh->lock);
}