summaryrefslogtreecommitdiff
path: root/drivers/nvdla/scheduler.c
blob: f36a5c789c4b068c4049de7c6c8a2641c07113e6 (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
/*
 * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of NVIDIA CORPORATION nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <opendla.h>
#include <dla_debug.h>
#include <dla_engine.h>
#include <dla_err.h>
#include <dla_interface.h>

#include "dla_engine_internal.h"
#include "engine_debug.h"

#define MAX_NUM_ADDRESSES	256

static uint64_t roi_array_length __aligned(8);
static struct dla_network_desc network;

static int
dla_update_consumers(struct dla_processor_group *group,
			struct dla_common_op_desc *op, uint8_t event);

static int32_t
dla_read_address_list(struct dla_engine *engine)
{
	RETURN(0);
}

int32_t
dla_read_lut(struct dla_engine *engine, int16_t index, void *dst)
{
	int32_t ret = 0;
	uint64_t src_addr;

	if (index == -1) {
		ret = ERR(INVALID_INPUT);
		goto exit;
	}

	src_addr = engine->task->lut_data_addr;

	ret = dla_data_read(engine->driver_context,
			engine->task->task_data,
			src_addr, (void *)dst,
			sizeof(struct dla_lut_param),
			(sizeof(struct dla_lut_param) * (uint64_t)index));

exit:
	RETURN(ret);
}

static int
dla_op_enabled(struct dla_processor_group *group)
{
	int32_t ret;
	struct dla_common_op_desc *op_desc;

	dla_debug("Enter: %s\n", __func__);
	op_desc = group->op_desc;

	group->active = 1;

	/* update dependency graph for this task */
	ret = dla_update_consumers(group, op_desc, DLA_EVENT_OP_ENABLED);
	dla_debug("Exit: %s\n", __func__);

	RETURN(ret);
}

static int
dla_op_programmed(struct dla_processor *processor,
		  struct dla_processor_group *group,
		  uint8_t rdma_id)
{
	int32_t ret;
	struct dla_common_op_desc *op_desc;

	dla_debug("Enter: %s\n", __func__);
	op_desc = group->op_desc;

	group->pending = 0;

	/* update dependency graph for this task */
	ret = dla_update_consumers(group, op_desc, DLA_EVENT_OP_PROGRAMMED);
	dla_debug("Exit: %s\n", __func__);

	RETURN(ret);
}

static int32_t
dla_read_config(struct dla_task *task, struct dla_processor *processor,
					struct dla_processor_group *group)
{
	int32_t ret;
	uint64_t base;
	int16_t index;
	uint8_t roi_index;
	struct dla_engine *engine;

	dla_debug("Enter: %s\n", __func__);

	engine = dla_get_engine();

	roi_index = group->roi_index;
	index = group->op_desc->index;

	base = (sizeof(union dla_operation_container) *
			(uint64_t)engine->network->num_operations *
			(uint64_t)roi_index);
	base = base + (sizeof(union dla_operation_container) *
			(uint64_t)index);

	LOG_EVENT(roi_index, group->id, processor->op_type,
					LOG_READ_OP_CONFIG_START);

	ret = dla_data_read(engine->driver_context, task->task_data,
				task->operation_desc_addr,
				(void *)group->operation_desc,
				sizeof(union dla_operation_container),
				base);
	if (ret)
		goto exit;

	LOG_EVENT(roi_index, group->id, processor->op_type,
					LOG_READ_OP_CONFIG_END);

	base = (sizeof(union dla_surface_container) *
			(uint64_t)engine->network->num_operations *
			(uint64_t)roi_index);

	base = base + (sizeof(union dla_surface_container) *
			(uint64_t)index);

	LOG_EVENT(roi_index, group->id, processor->op_type,
					LOG_READ_SURF_CONFIG_START);

	ret = dla_data_read(engine->driver_context, task->task_data,
				task->surface_desc_addr,
				(void *)group->surface_desc,
				sizeof(union dla_surface_container), base);
	if (ret)
		goto exit;

	LOG_EVENT(roi_index, group->id, processor->op_type,
					LOG_READ_SURF_CONFIG_END);

	processor->dump_config(group);

exit:
	dla_debug("Exit: %s\n", __func__);
	RETURN(ret);
}

static void
dla_reset_group(struct dla_processor_group *group)
{
	int32_t i;

	for (i = 0; i < DLA_OP_NUM; i++) {
		dla_put_op_desc(group->consumers[i]);
		group->consumers[i] = NULL;
	}

	dla_put_op_desc(group->fused_parent);
	group->fused_parent = NULL;

	dla_put_op_desc(group->op_desc);
	group->op_desc = NULL;
}

static int
dla_prepare_operation(struct dla_processor *processor,
			struct dla_common_op_desc *op_desc,
			uint8_t roi_index, uint32_t *group_number)
{
	int32_t ret = 0;
	uint8_t group_id;
	uint8_t rdma_id;
	struct dla_processor_group *group;
	struct dla_engine *engine = dla_get_engine();

	dla_debug("Enter: %s\n", __func__);

	/*
	 * If not already programmed then find out if
	 * processor is free and which group is free
	 */
	ret = utils_get_free_group(processor, &group_id, &rdma_id);
	if (ret) {
		dla_debug("processor:%s register groups are busy\n",
			processor->name);
		goto exit;
	} else {
		dla_info("processor:%s group:%d, rdma_group:%d available\n",
				processor->name, group_id, rdma_id);
	}
	*group_number = group_id;
	group = &processor->groups[group_id];

	/*
	 * update operation descriptor
	 */
	group->op_desc = op_desc;
	dla_get_refcount(op_desc);
	group->id = group_id;
	group->roi_index = roi_index;
	group->rdma_id = rdma_id;

	ret = dla_read_config(engine->task, processor, group);
	if (ret)
		goto exit;

	group->pending = 1;

	processor->group_status |= (1 << group->id);

	processor->rdma_check(group);
	if (group->is_rdma_needed) {
		group->rdma_id = rdma_id;
		processor->rdma_status |= (1 << rdma_id);
	}

	processor->tail_op = op_desc;
exit:
	dla_debug("Exit: %s status=%d\n", __func__, ret);
	RETURN(ret);
}

static int
dla_program_operation(struct dla_processor *processor,
			struct dla_processor_group *group)
{
	int32_t i;
	int32_t ret = 0;
	struct dla_common_op_desc *op_desc;
	struct dla_engine *engine = dla_get_engine();

	dla_debug("Enter: %s\n", __func__);

	dla_info("Program %s operation index %d ROI %d Group[%d]\n",
					processor->name,
					group->op_desc->index,
					group->roi_index,
					group->id);

	group->programming = 1;

	op_desc = group->op_desc;

	processor->set_producer(group->id, group->rdma_id);

	LOG_EVENT(group->roi_index, group->id, processor->op_type,
						LOG_PROGRAM_START);

	ret = processor->program(group);
	if (ret)
		goto exit;

	LOG_EVENT(group->roi_index, group->id, processor->op_type,
						LOG_PROGRAM_END);

	/**
	 * Pre-fetch consumers
	 */
	for (i = 0; i < DLA_OP_NUM; i++) {
		group->consumers[i] = dla_get_op_desc(engine->task,
					op_desc->consumers[i].index, i,
					group->roi_index);
	}

	group->fused_parent = dla_get_op_desc(engine->task,
					op_desc->fused_parent.index,
					op_desc->op_type - 1,
					group->roi_index);

	if (group->fused_parent != NULL) {
		if (group->fused_parent->op_type != (op_desc->op_type - 1)) {
			dla_warn("Invalid fused op type");
			ret = ERR(INVALID_INPUT);
			goto exit;
		}
	}

	ret = dla_op_programmed(processor, group, group->rdma_id);
	if (!ret)
		goto exit;

exit:
	group->programming = 0;
	dla_debug("Exit: %s status=%d\n", __func__, ret);
	RETURN(ret);
}

static int
dla_enable_operation(struct dla_processor *processor,
			struct dla_common_op_desc *op_desc)
{
	int32_t ret = 0;
	int32_t group_id;
	struct dla_engine *engine;
	struct dla_processor_group *group;

	dla_debug("Enter: %s\n", __func__);
	assert(op_desc->dependency_count == 0);

	/**
	 * If some operation has reported error then skip
	 * enabling next operations
	 */
	engine = dla_get_engine();
	if (engine->status)
		goto exit;

	/**
	 * Find out if operation is already programmed
	 */
	group_id = 0;
	group = &processor->groups[group_id];
	if ((processor->group_status & (1 << group_id)) &&
			group->op_desc->index == op_desc->index &&
			group->roi_index == op_desc->roi_index &&
			!group->pending)
		goto enable_op;

	group_id = 1;
	group = &processor->groups[group_id];
	if ((processor->group_status & (1 << group_id)) &&
			group->op_desc->index == op_desc->index &&
			group->roi_index == op_desc->roi_index &&
			!group->pending)
		goto enable_op;

	/**
	 * Operation is not programmed yet, ignore
	 */
	dla_debug("exit %s without actual enable due to processor "
				"hasn't been programmed\n", __func__);
	goto exit;

enable_op:
	/**
	 * If this event is triggered as part of programming same
	 * group then skip enable, it will get enabled after programming
	 * is complete
	 */
	if (group->programming)
		goto exit;

	if (group->active) {
		dla_debug("Processor:%s already enabled on group:%d\n",
			processor->name, group_id);
		goto exit;
	}

	dla_info("Enable %s operation index %d ROI %d\n",
					processor->name,
					group->op_desc->index,
					group->roi_index);

	processor->set_producer(group->id, group->rdma_id);

	LOG_EVENT(group->roi_index, group->id, processor->op_type,
						LOG_OPERATION_START);

	ret = processor->enable(group);
	if (ret)
		goto exit;

	ret = dla_op_enabled(group);
exit:
	dla_debug("Exit: %s status=%d\n", __func__, ret);
	RETURN(ret);
}

static int
dla_submit_operation(struct dla_processor *processor,
			struct dla_common_op_desc *op_desc,
			uint8_t roi_index)
{
	int32_t err;
	uint32_t group_id = 0;

	dla_debug("Enter: %s\n", __func__);

	dla_info("Prepare %s operation index %d ROI %d dep_count %d\n",
			processor->name, op_desc->index, roi_index,
			op_desc->dependency_count);
	err = dla_prepare_operation(processor, op_desc, roi_index, &group_id);
	if (err)
		goto exit;

	if (!processor->is_ready(processor, &processor->groups[group_id]))
		goto exit;

	err = dla_program_operation(processor, &processor->groups[group_id]);
	if (err)
		goto exit;

	if (op_desc->dependency_count == 0)
		err = dla_enable_operation(processor, op_desc);

exit:
	dla_debug("Exit: %s\n", __func__);
	RETURN(err);
}

/**
 * Dequeue next operation of same type from list of operations
 */
static int32_t
dla_dequeue_operation(struct dla_engine *engine,
			struct dla_processor *processor)
{
	int32_t ret = 0;
	int16_t index;
	struct dla_common_op_desc *consumer;

	dla_debug("Enter: %s\n", __func__);

	if (engine->status) {
		dla_debug("Skip dequeue op as engine has reported error\n");
		goto exit;
	}

	/**
	 * If we are done processing all ROIs for current op then
	 * load next op of same type otherwise reload same op for
	 * next ROI.
	 */
	if (processor->roi_index == (engine->network->num_rois - 1)) {
		index = processor->tail_op->consumers[processor->op_type].index;
		if (-1 == index) {
			/**
			 * It means we are done processing
			 * all ops of this type
			 */
			dla_debug("exit %s as there's no further operation\n",
				processor->name);
			goto exit;
		}
		processor->roi_index = 0;
	} else {
		processor->roi_index++;
		index = processor->tail_op->index;
	}

	dla_debug("Dequeue op from %s processor, index=%d ROI=%d\n",
			processor->name, index, processor->roi_index);

	/**
	 * Get operation descriptor
	 */
	consumer = dla_get_op_desc(engine->task, index,
				processor->op_type, processor->roi_index);
	if (consumer == NULL) {
		ret = ERR(NO_MEM);
		dla_error("Failed to allocate op_desc");
		goto exit;
	}

	ret = dla_submit_operation(processor, consumer, processor->roi_index);
	dla_put_op_desc(consumer);

exit:
	dla_debug("Exit: %s\n", __func__);
	RETURN(ret);
}

static int
dla_update_dependency(struct dla_consumer *consumer,
			struct dla_common_op_desc *op_desc,
			uint8_t event, uint8_t roi_index)
{
	int32_t ret = 0;
	struct dla_processor *processor;
	struct dla_engine *engine = dla_get_engine();

	if (consumer->index == -1)
		goto exit;

	/* Update dependency only if event matches */
	if (event != consumer->event)
		goto exit;

	/**
	 * If consumer index is valid but op desc is NULL means
	 * op desc for consumer was not pre-fetched
	 */
	if (op_desc == NULL) {
		ret = ERR(INVALID_INPUT);
		dla_error("Operation descriptor is NULL, consumer index %d",
				consumer->index);
		goto exit;
	}

	assert(op_desc->dependency_count > 0);

	dla_debug("Update dependency operation index %d ROI %d DEP_COUNT=%d\n",
					op_desc->index, op_desc->roi_index,
					op_desc->dependency_count);
	op_desc->dependency_count--;

	if (op_desc->dependency_count == 0) {
		processor = &engine->processors[op_desc->op_type];
		dla_debug("enable %s in %s as depdency are resolved\n",
			processor->name, __func__);

		ret = dla_enable_operation(processor, op_desc);
		if (ret)
			goto exit;
	}
exit:
	RETURN(ret);
}

static int
dla_update_consumers(struct dla_processor_group *group,
		     struct dla_common_op_desc *op,
		     uint8_t event)
{
	int32_t i;
	int32_t ret = 0;
	struct dla_engine *engine = dla_get_engine();

	if (engine->status) {
		dla_debug("Skip update as engine has reported error\n");
		goto exit;
	}

	for (i = 0; i < DLA_OP_NUM; i++) {
		ret = dla_update_dependency(&op->consumers[i],
						group->consumers[i],
						event, group->roi_index);
		if (ret) {
			dla_error("Failed to update dependency for "
				"consumer %d, ROI %d", i, group->roi_index);
			goto exit;
		}
	}

	ret = dla_update_dependency(&op->fused_parent,
					group->fused_parent,
					event, group->roi_index);
	if (ret) {
		dla_error("Failed to update dependency for "
			"fused parent, ROI %d", group->roi_index);
		goto exit;
	}

exit:
	RETURN(ret);
}

/**
 * Handle operation completion notification
 */
int
dla_op_completion(struct dla_processor *processor,
		  struct dla_processor_group *group)
{
	int32_t ret;
#if STAT_ENABLE
	uint64_t stat_data_address;
	uint64_t stat_base;
#endif /* STAT_ENABLE */
	struct dla_task *task;
	struct dla_common_op_desc *op_desc;
	struct dla_processor_group *next_group;
	struct dla_engine *engine = dla_get_engine();

	dla_debug("Enter:%s processor %s group%u\n", __func__,
					processor->name, group->id);

	dla_info("Completed %s operation index %d ROI %d\n",
					processor->name,
					group->op_desc->index,
					group->roi_index);

	task = engine->task;

	/**
	 * Mark OP as done only when all ROIs are done for that
	 * operation
	 */
	if (group->roi_index == (engine->network->num_rois - 1))
		engine->num_proc_hwl++;

	op_desc = group->op_desc;

#if STAT_ENABLE
	if (engine->stat_enable == (uint32_t)1) {
		processor->get_stat_data(processor, group);

		processor->dump_stat(processor);

		stat_data_address = (uint64_t)(engine->task->stat_data_addr +
				(sizeof(union dla_stat_container) *
				(uint64_t)(engine->network->num_operations) *
				(uint64_t)(op_desc->roi_index)));

		stat_base = (stat_data_address +
				(sizeof(union dla_stat_container) *
				(uint64_t)op_desc->index));

		/**
		 * Flush stat descriptor to DRAM
		 */
		ret = dla_data_write(engine->driver_context, task->task_data,
					(void *)(processor->stat_data_desc),
					stat_base,
					sizeof(union dla_stat_container),
					0);
		if (ret < 0)
			dla_error("Failed to write stats to DMA memory\n");
	}
#endif /* STAT_ENABLE */

	/**
	 * Get an extra reference count to keep op descriptor
	 * in cache until this operation completes
	 */
	dla_get_refcount(op_desc);

	LOG_EVENT(group->roi_index, group->id, processor->op_type,
						LOG_OPERATION_END);

	processor->group_status &= ~(1 << group->id);
	if (group->is_rdma_needed) {
		group->is_rdma_needed = 0;
		processor->rdma_status &= ~(1 << group->rdma_id);
		group->rdma_id = 0;
	}
	group->active = 0;
	group->lut_index = -1;
	processor->last_group = group->id;

	/**
	 * Switch consumer pointer to next group
	 */
	processor->consumer_ptr = !group->id;

	/**
	 * update dependency graph for this task
	 * TODO: Add proper error handling
	 */
	ret = dla_update_consumers(group, op_desc, DLA_EVENT_OP_COMPLETED);
	if (ret)
		goto exit;

	dla_info("%d HWLs done, totally %d layers\n",
				engine->num_proc_hwl,
				engine->network->num_operations);

	/* free operation descriptor from cache */
	dla_reset_group(group);

	/* if not hwl pending, means network completed */
	if (engine->network->num_operations == engine->num_proc_hwl) {
		dla_put_op_desc(op_desc);
		goto exit;
	}

	next_group = &processor->groups[!group->id];
	if (next_group->pending && !engine->status) {
		/**
		 * Next group must be ready here for programming,
		 * if not means it is an error
		 */
		if (!processor->is_ready(processor, next_group))
			goto dequeue_op;

		ret = dla_program_operation(processor, next_group);
		if (ret)
			goto exit;

		if (next_group->op_desc->dependency_count != 0)
			goto dequeue_op;

		ret = dla_enable_operation(processor,
					   next_group->op_desc);
		if (ret)
			goto exit;
	}

dequeue_op:
	/* dequeue operation from this processor */
	ret = dla_dequeue_operation(engine, processor);

exit:
	dla_put_op_desc(op_desc);
	dla_debug("Exit:%s processor %s group%u status=%d\n",
				__func__, processor->name,
				group->id, ret);

	RETURN(ret);
}

/**
 * Read network configuration from DRAM, network descriptor address
 * is always first in the address list. Network configuration contains
 * offset in address list for addresses of other lists used to
 * execute network
 *
 * @engine: Engine instance
 * @return: 0 for success
 */
static int
dla_read_network_config(struct dla_engine *engine)
{
	int32_t ret;
	uint64_t network_addr;
	struct dla_task *task = engine->task;

	dla_debug("Enter:%s\n", __func__);

	/**
	 * Read address list from DRAM to DMEM
	 */
	ret = dla_read_address_list(engine);
	if (ret) {
		dla_error("Failed to read address list");
		goto exit;
	}

	/**
	 * Read network descriptor address from address list. It is always
	 * at index 0.
	 */
	ret = dla_get_dma_address(engine->driver_context, task->task_data,
						0, (void *)&network_addr,
						DESTINATION_PROCESSOR);
	if (ret) {
		dla_error("Failed to read network desc address");
		goto exit;
	}

	/**
	 * Read network descriptor, it has information for a network
	 * such as all address indexes.
	 */
	ret = dla_data_read(engine->driver_context, task->task_data,
				network_addr, (void *)&network,
				sizeof(struct dla_network_desc),
				0);
	if (ret) {
		dla_error("Failed to read network descriptor");
		goto exit;
	}

	dla_debug_network_desc(&network);

	if (network.num_operations == 0)
		goto exit;

	/**
	 * Read operation descriptor list address from address list
	 */
	ret = dla_get_dma_address(engine->driver_context, task->task_data,
				network.operation_desc_index,
				(void *)&task->operation_desc_addr,
				DESTINATION_PROCESSOR);
	if (ret) {
		dla_error("Failed to read operation desc list address");
		goto exit;
	}

	/**
	 * Read surface descriptor list address from address list
	 */
	ret = dla_get_dma_address(engine->driver_context, task->task_data,
				network.surface_desc_index,
				(void *)&task->surface_desc_addr,
				DESTINATION_PROCESSOR);
	if (ret) {
		dla_error("Failed to read surface desc list address");
		goto exit;
	}

	/**
	 * Read dependency graph address from address list
	 */
	ret = dla_get_dma_address(engine->driver_context, task->task_data,
				network.dependency_graph_index,
				(void *)&task->dependency_graph_addr,
				DESTINATION_PROCESSOR);
	if (ret) {
		dla_error("Failed to ready dependency graph address");
		goto exit;
	}

	/**
	 * Read LUT data list address from address list
	 */
	if (network.num_luts) {
		ret = dla_get_dma_address(engine->driver_context,
					task->task_data,
					network.lut_data_index,
					(void *)&task->lut_data_addr,
					DESTINATION_PROCESSOR);
		if (ret) {
			dla_error("Failed to read LUT list address");
			goto exit;
		}
	}

	/**
	 * Read address for ROI information
	 */
	if (network.dynamic_roi) {
		/**
		 * Read ROI array address from address list
		 */
		ret = dla_get_dma_address(engine->driver_context,
					task->task_data,
					network.roi_array_index,
					(void *)&task->roi_array_addr,
					DESTINATION_PROCESSOR);
		if (ret) {
			dla_error("Failed to read ROI array address");
			goto exit;
		}

		ret = dla_data_read(engine->driver_context, task->task_data,
					task->roi_array_addr,
					(void *)&roi_array_length,
					sizeof(uint64_t),
					0);
		if (ret) {
			dla_error("Failed to read ROI array length");
			goto exit;
		}

		/**
		 * Number of ROIs detected can't be greater than maximum number
		 * ROIs this network can process
		 */
		if (roi_array_length > network.num_rois) {
			dla_error("Invalid number of ROIs detected");
			ret = ERR(INVALID_INPUT);
			goto exit;
		}

		network.num_rois = roi_array_length;

		/**
		 * Read surface address from address list
		 */
		ret = dla_get_dma_address(engine->driver_context,
						task->task_data,
						network.surface_index,
						(void *)&task->surface_addr,
						DESTINATION_DMA);
		if (ret) {
			dla_error("Failed to read surface address");
			goto exit;
		}
	}

#if STAT_ENABLE
	if (network.stat_list_index != -1) {
		ret = dla_get_dma_address(engine->driver_context,
						task->task_data,
						network.stat_list_index,
						(void *)&task->stat_data_addr,
						DESTINATION_PROCESSOR);
		if (ret) {
			dla_error("Failed to read stat address");
			goto exit;
		}
	}
#endif /* STAT_ENABLE */

exit:
	dla_debug("Exit:%s status=%d\n", __func__, ret);
	RETURN(ret);
}

static int
dla_initiate_processors(struct dla_engine *engine)
{
	int32_t i;
	int32_t ret = 0;
	int16_t index;
	struct dla_processor *processor;
	struct dla_common_op_desc *consumer;
	struct dla_network_desc *nw;

	dla_debug("Enter: %s\n", __func__);

	if (!engine) {
		ret = ERR(INVALID_INPUT);
		goto exit;
	}

	nw = engine->network;

	/* Validate operation heads before initiating processors */
	for (i = 0; i < DLA_OP_NUM; i++) {
		if (nw->op_head[i] >= nw->num_operations) {
			ret = ERR(INVALID_INPUT);
			dla_error("Invalid op_head %d for op %d",
						nw->op_head[i], i);
			goto exit;
		}
	}

	for (i = 0; i < DLA_OP_NUM; i++) {
		index = nw->op_head[i];

		/* If there is no op for this type then continue */
		if (-1 == index)
			continue;

		consumer = dla_get_op_desc(engine->task, index, i, 0);
		/*
		 * if consumer is NULL, it means either data copy error
		 * or cache insufficient - we should fix it
		 **/
		if (consumer == NULL) {
			dla_error("Failed to allocate memory for op_head[%d]=%d",
							i, index);
			ret = ERR(NO_MEM);
			goto exit;
		}

		processor = &engine->processors[consumer->op_type];

		ret = dla_submit_operation(processor, consumer, 0);
		dla_put_op_desc(consumer);
		if (ret && ret != ERR(PROCESSOR_BUSY)) {
			dla_error("Failed to submit %s op from index %u\n",
						processor->name, index);
			goto exit;
		}

		ret = dla_dequeue_operation(engine, processor);
		if (ret) {
			dla_error("Failed to dequeue op for %s processor",
							processor->name);
			goto exit;
		}
	}
exit:
	dla_debug("Exit: %s status=%d\n", __func__, ret);
	RETURN(ret);
}

static int
dla_handle_events(struct dla_processor *processor)
{
	int32_t j;
	int32_t ret = 0;
	uint8_t group_id;
	struct dla_processor_group *group;

	dla_debug("Enter:%s, processor:%s\n", __func__, processor->name);

	group_id = !processor->last_group;

	for (j = 0; j < DLA_NUM_GROUPS; j++) {
		group = &processor->groups[group_id];

		if ((1 << DLA_EVENT_CDMA_WT_DONE) & group->events) {
			dla_info("Handle cdma weight done event, processor %s "
				"group %u\n", processor->name, group->id);

			ret = dla_update_consumers(group,
						   group->op_desc,
						   DLA_EVENT_CDMA_WT_DONE);
			if (ret)
				goto exit;
		}

		if ((1 << DLA_EVENT_CDMA_DT_DONE) & group->events) {
			dla_info("Handle cdma data done event, processor %s "
				"group %u\n", processor->name, group->id);

			ret = dla_update_consumers(group,
						   group->op_desc,
						   DLA_EVENT_CDMA_DT_DONE);
			if (ret)
				goto exit;
		}

		/**
		 * Handle complete after all other events
		 */
		if ((1 << DLA_EVENT_OP_COMPLETED) & group->events) {
			dla_info("Handle op complete event, processor %s "
				"group %u\n", processor->name, group->id);

			ret = dla_op_completion(processor, group);
			if (ret)
				goto exit;
		}

		/**
		 * Clear all events
		 */
		group->events = 0;
		group_id = !group_id;
	}
exit:
	dla_debug("Exit:%s, ret:%x\n", __func__, ret);
	RETURN(ret);
}

int
dla_process_events(void *engine_context, uint32_t *task_complete)
{
	int32_t i;
	int32_t ret = 0;
	struct dla_engine *engine = (struct dla_engine *)engine_context;

	for (i = 0; i < DLA_OP_NUM; i++) {
		struct dla_processor *processor;

		processor = &engine->processors[i];
		ret = dla_handle_events(processor);
		/**
		 * Incase engine status is non-zero, then don't
		 * update the engine status. We should keep its
		 * status for later cleaning of engine.
		 */
		if (!engine->status)
			engine->status = ret;
	}

	if (engine->network->num_operations == engine->num_proc_hwl)
		*task_complete = 1;

	RETURN(ret);
}

/**
 * Execute task selected by task scheduler
 *
 * 1. Read network configuration for the task
 * 2. Initiate processors with head of list for same op
 * 3. Start processing events received
 */
int
dla_execute_task(void *engine_context, void *task_data, void *config_data)
{
	int32_t ret;
	struct dla_engine *engine = (struct dla_engine *)engine_context;

	if (engine == NULL) {
		dla_error("engine is NULL\n");
		ret = ERR(INVALID_INPUT);
		goto complete;
	}

	if (engine->task == NULL) {
		dla_error("task is NULL\n");
		ret = ERR(INVALID_INPUT);
		goto complete;
	}

	if (engine->task->task_data != NULL) {
		/* We have on the fly tasks running */
		dla_warn("Already some task in progress");
		ret = ERR(PROCESSOR_BUSY);
		goto complete;
	}

	engine->task->task_data = task_data;
	engine->config_data = config_data;
	engine->network = &network;
	engine->num_proc_hwl = 0;
	engine->stat_enable = 0;

	LOG_EVENT(0, 0, 0, LOG_TASK_START);

	ret = dla_read_network_config(engine);
	if (ret)
		goto complete;

	dla_debug_address_info(engine->task);

	/**
	 * If no operations in a task means nothing to do, NULL task
	 */
	if (engine->network->num_operations == 0)
		goto complete;

#if STAT_ENABLE
	if (network.stat_list_index != -1)
		engine->stat_enable = 1;
#endif /* STAT_ENABLE */

	ret = dla_initiate_processors(engine);
	engine->status = ret;

complete:
	LOG_EVENT(0, 0, 0, LOG_TASK_END);

	RETURN(ret);
}

void
dla_clear_task(void *engine_context)
{
	int32_t i, j;
	struct dla_engine *engine = (struct dla_engine *)engine_context;

	for (i = 0; i < DLA_OP_NUM; i++) {
		struct dla_processor *processor = &engine->processors[i];

		processor->roi_index = 0;
		processor->group_status = 0;
		processor->rdma_status = 0;

		processor->tail_op = NULL;

		for (j = 0; j < DLA_NUM_GROUPS; j++) {
			struct dla_processor_group *group =
						&processor->groups[j];

			group->rdma_id = group->id;
			group->active = 0;
			group->events = 0;
			group->roi_index = 0;
			group->is_rdma_needed = 0;
			group->lut_index = -1;
		}
	}

	engine->task->task_data = NULL;
	engine->network = NULL;
	engine->num_proc_hwl = 0;
	engine->status = 0;
	engine->stat_enable = 0;

	dla_info("reset engine done\n");
}