Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright 2020 HabanaLabs, Ltd.
 * All Rights Reserved.
 */

#include "habanalabs.h"

static const char * const hl_glbl_error_cause[HL_MAX_NUM_OF_GLBL_ERR_CAUSE] = {
	"Error due to un-priv read",
	"Error due to un-secure read",
	"Error due to read from unmapped reg",
	"Error due to un-priv write",
	"Error due to un-secure write",
	"Error due to write to unmapped reg",
	"External I/F write sec violation",
	"External I/F write to un-mapped reg",
	"Read to write only",
	"Write to read only"
};

/**
 * hl_get_pb_block - return the relevant block within the block array
 *
 * @hdev: pointer to hl_device structure
 * @mm_reg_addr: register address in the desired block
 * @pb_blocks: blocks array
 * @array_size: blocks array size
 *
 */
static int hl_get_pb_block(struct hl_device *hdev, u32 mm_reg_addr,
		const u32 pb_blocks[], int array_size)
{
	int i;
	u32 start_addr, end_addr;

	for (i = 0 ; i < array_size ; i++) {
		start_addr = pb_blocks[i];
		end_addr = start_addr + HL_BLOCK_SIZE;

		if ((mm_reg_addr >= start_addr) && (mm_reg_addr < end_addr))
			return i;
	}

	dev_err(hdev->dev, "No protection domain was found for 0x%x\n",
			mm_reg_addr);
	return -EDOM;
}

/**
 * hl_unset_pb_in_block - clear a specific protection bit in a block
 *
 * @hdev: pointer to hl_device structure
 * @reg_offset: register offset will be converted to bit offset in pb block
 * @sgs_entry: pb array
 *
 */
static int hl_unset_pb_in_block(struct hl_device *hdev, u32 reg_offset,
				struct hl_block_glbl_sec *sgs_entry)
{
	if ((reg_offset >= HL_BLOCK_SIZE) || (reg_offset & 0x3)) {
		dev_err(hdev->dev,
			"Register offset(%d) is out of range(%d) or invalid\n",
			reg_offset, HL_BLOCK_SIZE);
		return -EINVAL;
	}

	UNSET_GLBL_SEC_BIT(sgs_entry->sec_array,
			 (reg_offset & (HL_BLOCK_SIZE - 1)) >> 2);

	return 0;
}

/**
 * hl_unsecure_register - locate the relevant block for this register and
 *                        remove corresponding protection bit
 *
 * @hdev: pointer to hl_device structure
 * @mm_reg_addr: register address to unsecure
 * @offset: additional offset to the register address
 * @pb_blocks: blocks array
 * @sgs_array: pb array
 * @array_size: blocks array size
 *
 */
int hl_unsecure_register(struct hl_device *hdev, u32 mm_reg_addr, int offset,
		const u32 pb_blocks[], struct hl_block_glbl_sec sgs_array[],
		int array_size)
{
	u32 reg_offset;
	int block_num;

	block_num = hl_get_pb_block(hdev, mm_reg_addr + offset, pb_blocks,
			array_size);
	if (block_num < 0)
		return block_num;

	reg_offset = (mm_reg_addr + offset) - pb_blocks[block_num];

	return hl_unset_pb_in_block(hdev, reg_offset, &sgs_array[block_num]);
}

/**
 * hl_unsecure_register_range - locate the relevant block for this register
 *                              range and remove corresponding protection bit
 *
 * @hdev: pointer to hl_device structure
 * @mm_reg_range: register address range to unsecure
 * @offset: additional offset to the register address
 * @pb_blocks: blocks array
 * @sgs_array: pb array
 * @array_size: blocks array size
 *
 */
static int hl_unsecure_register_range(struct hl_device *hdev,
		struct range mm_reg_range, int offset, const u32 pb_blocks[],
		struct hl_block_glbl_sec sgs_array[],
		int array_size)
{
	u32 reg_offset;
	int i, block_num, rc = 0;

	block_num = hl_get_pb_block(hdev,
			mm_reg_range.start + offset, pb_blocks,
			array_size);
	if (block_num < 0)
		return block_num;

	for (i = mm_reg_range.start ; i <= mm_reg_range.end ; i += 4) {
		reg_offset = (i + offset) - pb_blocks[block_num];
		rc |= hl_unset_pb_in_block(hdev, reg_offset,
					&sgs_array[block_num]);
	}

	return rc;
}

/**
 * hl_unsecure_registers - locate the relevant block for all registers and
 *                        remove corresponding protection bit
 *
 * @hdev: pointer to hl_device structure
 * @mm_reg_array: register address array to unsecure
 * @mm_array_size: register array size
 * @offset: additional offset to the register address
 * @pb_blocks: blocks array
 * @sgs_array: pb array
 * @blocks_array_size: blocks array size
 *
 */
int hl_unsecure_registers(struct hl_device *hdev, const u32 mm_reg_array[],
		int mm_array_size, int offset, const u32 pb_blocks[],
		struct hl_block_glbl_sec sgs_array[], int blocks_array_size)
{
	int i, rc = 0;

	for (i = 0 ; i < mm_array_size ; i++) {
		rc = hl_unsecure_register(hdev, mm_reg_array[i], offset,
				pb_blocks, sgs_array, blocks_array_size);

		if (rc)
			return rc;
	}

	return rc;
}

/**
 * hl_unsecure_registers_range - locate the relevant block for all register
 *                        ranges and remove corresponding protection bit
 *
 * @hdev: pointer to hl_device structure
 * @mm_reg_range_array: register address range array to unsecure
 * @mm_array_size: register array size
 * @offset: additional offset to the register address
 * @pb_blocks: blocks array
 * @sgs_array: pb array
 * @blocks_array_size: blocks array size
 *
 */
static int hl_unsecure_registers_range(struct hl_device *hdev,
		const struct range mm_reg_range_array[], int mm_array_size,
		int offset, const u32 pb_blocks[],
		struct hl_block_glbl_sec sgs_array[], int blocks_array_size)
{
	int i, rc = 0;

	for (i = 0 ; i < mm_array_size ; i++) {
		rc = hl_unsecure_register_range(hdev, mm_reg_range_array[i],
			offset, pb_blocks, sgs_array, blocks_array_size);

		if (rc)
			return rc;
	}

	return rc;
}

/**
 * hl_ack_pb_security_violations - Ack security violation
 *
 * @hdev: pointer to hl_device structure
 * @pb_blocks: blocks array
 * @block_offset: additional offset to the block
 * @array_size: blocks array size
 *
 */
static void hl_ack_pb_security_violations(struct hl_device *hdev,
		const u32 pb_blocks[], u32 block_offset, int array_size)
{
	int i;
	u32 cause, addr, block_base;

	for (i = 0 ; i < array_size ; i++) {
		block_base = pb_blocks[i] + block_offset;
		cause = RREG32(block_base + HL_BLOCK_GLBL_ERR_CAUSE);
		if (cause) {
			addr = RREG32(block_base + HL_BLOCK_GLBL_ERR_ADDR);
			hdev->asic_funcs->pb_print_security_errors(hdev,
					block_base, cause, addr);
			WREG32(block_base + HL_BLOCK_GLBL_ERR_CAUSE, cause);
		}
	}
}

/**
 * hl_config_glbl_sec - set pb in HW according to given pb array
 *
 * @hdev: pointer to hl_device structure
 * @pb_blocks: blocks array
 * @sgs_array: pb array
 * @block_offset: additional offset to the block
 * @array_size: blocks array size
 *
 */
void hl_config_glbl_sec(struct hl_device *hdev, const u32 pb_blocks[],
		struct hl_block_glbl_sec sgs_array[], u32 block_offset,
		int array_size)
{
	int i, j;
	u32 sgs_base;

	if (hdev->pldm)
		usleep_range(100, 1000);

	for (i = 0 ; i < array_size ; i++) {
		sgs_base = block_offset + pb_blocks[i] +
				HL_BLOCK_GLBL_SEC_OFFS;

		for (j = 0 ; j < HL_BLOCK_GLBL_SEC_LEN ; j++)
			WREG32(sgs_base + j * sizeof(u32),
				sgs_array[i].sec_array[j]);
	}
}

/**
 * hl_secure_block - locally memsets a block to 0
 *
 * @hdev: pointer to hl_device structure
 * @sgs_array: pb array to clear
 * @array_size: blocks array size
 *
 */
void hl_secure_block(struct hl_device *hdev,
		struct hl_block_glbl_sec sgs_array[], int array_size)
{
	int i;

	for (i = 0 ; i < array_size ; i++)
		memset((char *)(sgs_array[i].sec_array), 0,
			HL_BLOCK_GLBL_SEC_SIZE);
}

/**
 * hl_init_pb_with_mask - set selected pb instances with mask in HW according
 *                        to given configuration
 *
 * @hdev: pointer to hl_device structure
 * @num_dcores: number of decores to apply configuration to
 *              set to HL_PB_SHARED if need to apply only once
 * @dcore_offset: offset between dcores
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 * @user_regs_array: unsecured register array
 * @user_regs_array_size: unsecured register array size
 * @mask: enabled instances mask: 1- enabled, 0- disabled
 */
int hl_init_pb_with_mask(struct hl_device *hdev, u32 num_dcores,
		u32 dcore_offset, u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const u32 *user_regs_array, u32 user_regs_array_size, u64 mask)
{
	int i, j;
	struct hl_block_glbl_sec *glbl_sec;

	glbl_sec = kcalloc(blocks_array_size,
			sizeof(struct hl_block_glbl_sec),
			GFP_KERNEL);
	if (!glbl_sec)
		return -ENOMEM;

	hl_secure_block(hdev, glbl_sec, blocks_array_size);
	hl_unsecure_registers(hdev, user_regs_array, user_regs_array_size, 0,
			pb_blocks, glbl_sec, blocks_array_size);

	/* Fill all blocks with the same configuration */
	for (i = 0 ; i < num_dcores ; i++) {
		for (j = 0 ; j < num_instances ; j++) {
			int seq = i * num_instances + j;

			if (!(mask & BIT_ULL(seq)))
				continue;

			hl_config_glbl_sec(hdev, pb_blocks, glbl_sec,
					i * dcore_offset + j * instance_offset,
					blocks_array_size);
		}
	}

	kfree(glbl_sec);

	return 0;
}

/**
 * hl_init_pb - set pb in HW according to given configuration
 *
 * @hdev: pointer to hl_device structure
 * @num_dcores: number of decores to apply configuration to
 *              set to HL_PB_SHARED if need to apply only once
 * @dcore_offset: offset between dcores
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 * @user_regs_array: unsecured register array
 * @user_regs_array_size: unsecured register array size
 *
 */
int hl_init_pb(struct hl_device *hdev, u32 num_dcores, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const u32 *user_regs_array, u32 user_regs_array_size)
{
	return hl_init_pb_with_mask(hdev, num_dcores, dcore_offset,
			num_instances, instance_offset, pb_blocks,
			blocks_array_size, user_regs_array,
			user_regs_array_size, ULLONG_MAX);
}

/**
 * hl_init_pb_ranges_with_mask - set pb instances using mask in HW according to
 *                               given configuration unsecurring registers
 *                               ranges instead of specific registers
 *
 * @hdev: pointer to hl_device structure
 * @num_dcores: number of decores to apply configuration to
 *              set to HL_PB_SHARED if need to apply only once
 * @dcore_offset: offset between dcores
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 * @user_regs_range_array: unsecured register range array
 * @user_regs_range_array_size: unsecured register range array size
 * @mask: enabled instances mask: 1- enabled, 0- disabled
 */
int hl_init_pb_ranges_with_mask(struct hl_device *hdev, u32 num_dcores,
		u32 dcore_offset, u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const struct range *user_regs_range_array,
		u32 user_regs_range_array_size, u64 mask)
{
	int i, j, rc = 0;
	struct hl_block_glbl_sec *glbl_sec;

	glbl_sec = kcalloc(blocks_array_size,
			sizeof(struct hl_block_glbl_sec),
			GFP_KERNEL);
	if (!glbl_sec)
		return -ENOMEM;

	hl_secure_block(hdev, glbl_sec, blocks_array_size);
	rc = hl_unsecure_registers_range(hdev, user_regs_range_array,
			user_regs_range_array_size, 0, pb_blocks, glbl_sec,
			blocks_array_size);
	if (rc)
		goto free_glbl_sec;

	/* Fill all blocks with the same configuration */
	for (i = 0 ; i < num_dcores ; i++) {
		for (j = 0 ; j < num_instances ; j++) {
			int seq = i * num_instances + j;

			if (!(mask & BIT_ULL(seq)))
				continue;

			hl_config_glbl_sec(hdev, pb_blocks, glbl_sec,
					i * dcore_offset + j * instance_offset,
					blocks_array_size);
		}
	}

free_glbl_sec:
	kfree(glbl_sec);

	return rc;
}

/**
 * hl_init_pb_ranges - set pb in HW according to given configuration unsecurring
 *                     registers ranges instead of specific registers
 *
 * @hdev: pointer to hl_device structure
 * @num_dcores: number of decores to apply configuration to
 *              set to HL_PB_SHARED if need to apply only once
 * @dcore_offset: offset between dcores
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 * @user_regs_range_array: unsecured register range array
 * @user_regs_range_array_size: unsecured register range array size
 *
 */
int hl_init_pb_ranges(struct hl_device *hdev, u32 num_dcores,
		u32 dcore_offset, u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const struct range *user_regs_range_array,
		u32 user_regs_range_array_size)
{
	return hl_init_pb_ranges_with_mask(hdev, num_dcores, dcore_offset,
			num_instances, instance_offset, pb_blocks,
			blocks_array_size, user_regs_range_array,
			user_regs_range_array_size, ULLONG_MAX);
}

/**
 * hl_init_pb_single_dcore - set pb for a single docre in HW
 * according to given configuration
 *
 * @hdev: pointer to hl_device structure
 * @dcore_offset: offset from the dcore0
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 * @user_regs_array: unsecured register array
 * @user_regs_array_size: unsecured register array size
 *
 */
int hl_init_pb_single_dcore(struct hl_device *hdev, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const u32 *user_regs_array, u32 user_regs_array_size)
{
	int i, rc = 0;
	struct hl_block_glbl_sec *glbl_sec;

	glbl_sec = kcalloc(blocks_array_size,
			sizeof(struct hl_block_glbl_sec),
			GFP_KERNEL);
	if (!glbl_sec)
		return -ENOMEM;

	hl_secure_block(hdev, glbl_sec, blocks_array_size);
	rc = hl_unsecure_registers(hdev, user_regs_array, user_regs_array_size,
			0, pb_blocks, glbl_sec, blocks_array_size);
	if (rc)
		goto free_glbl_sec;

	/* Fill all blocks with the same configuration */
	for (i = 0 ; i < num_instances ; i++)
		hl_config_glbl_sec(hdev, pb_blocks, glbl_sec,
				dcore_offset + i * instance_offset,
				blocks_array_size);

free_glbl_sec:
	kfree(glbl_sec);

	return rc;
}

/**
 * hl_init_pb_ranges_single_dcore - set pb for a single docre in HW according
 *                                  to given configuration unsecurring
 *                                  registers ranges instead of specific
 *                                  registers
 *
 * @hdev: pointer to hl_device structure
 * @dcore_offset: offset from the dcore0
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 * @user_regs_range_array: unsecured register range array
 * @user_regs_range_array_size: unsecured register range array size
 *
 */
int hl_init_pb_ranges_single_dcore(struct hl_device *hdev, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size,
		const struct range *user_regs_range_array, u32 user_regs_range_array_size)
{
	int i;
	struct hl_block_glbl_sec *glbl_sec;

	glbl_sec = kcalloc(blocks_array_size,
			sizeof(struct hl_block_glbl_sec),
			GFP_KERNEL);
	if (!glbl_sec)
		return -ENOMEM;

	hl_secure_block(hdev, glbl_sec, blocks_array_size);
	hl_unsecure_registers_range(hdev, user_regs_range_array,
			user_regs_range_array_size, 0, pb_blocks, glbl_sec,
			blocks_array_size);

	/* Fill all blocks with the same configuration */
	for (i = 0 ; i < num_instances ; i++)
		hl_config_glbl_sec(hdev, pb_blocks, glbl_sec,
				dcore_offset + i * instance_offset,
				blocks_array_size);

	kfree(glbl_sec);

	return 0;
}

/**
 * hl_ack_pb_with_mask - ack pb with mask in HW according to given configuration
 *
 * @hdev: pointer to hl_device structure
 * @num_dcores: number of decores to apply configuration to
 *              set to HL_PB_SHARED if need to apply only once
 * @dcore_offset: offset between dcores
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 * @mask: enabled instances mask: 1- enabled, 0- disabled
 *
 */
void hl_ack_pb_with_mask(struct hl_device *hdev, u32 num_dcores,
		u32 dcore_offset, u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size, u64 mask)
{
	int i, j;

	/* ack all blocks */
	for (i = 0 ; i < num_dcores ; i++) {
		for (j = 0 ; j < num_instances ; j++) {
			int seq = i * num_instances + j;

			if (!(mask & BIT_ULL(seq)))
				continue;

			hl_ack_pb_security_violations(hdev, pb_blocks,
					i * dcore_offset + j * instance_offset,
					blocks_array_size);
		}
	}
}

/**
 * hl_ack_pb - ack pb in HW according to given configuration
 *
 * @hdev: pointer to hl_device structure
 * @num_dcores: number of decores to apply configuration to
 *              set to HL_PB_SHARED if need to apply only once
 * @dcore_offset: offset between dcores
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 *
 */
void hl_ack_pb(struct hl_device *hdev, u32 num_dcores, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size)
{
	hl_ack_pb_with_mask(hdev, num_dcores, dcore_offset, num_instances,
			instance_offset, pb_blocks, blocks_array_size,
			ULLONG_MAX);
}

/**
 * hl_ack_pb_single_dcore - ack pb for single docre in HW
 * according to given configuration
 *
 * @hdev: pointer to hl_device structure
 * @dcore_offset: offset from dcore0
 * @num_instances: number of instances to apply configuration to
 * @instance_offset: offset between instances
 * @pb_blocks: blocks array
 * @blocks_array_size: blocks array size
 *
 */
void hl_ack_pb_single_dcore(struct hl_device *hdev, u32 dcore_offset,
		u32 num_instances, u32 instance_offset,
		const u32 pb_blocks[], u32 blocks_array_size)
{
	int i;

	/* ack all blocks */
	for (i = 0 ; i < num_instances ; i++)
		hl_ack_pb_security_violations(hdev, pb_blocks,
				dcore_offset + i * instance_offset,
				blocks_array_size);

}

static u32 hl_automated_get_block_base_addr(struct hl_device *hdev,
		struct hl_special_block_info *block_info,
		u32 major, u32 minor, u32 sub_minor)
{
	u32 fw_block_base_address = block_info->base_addr +
			major * block_info->major_offset +
			minor * block_info->minor_offset +
			sub_minor * block_info->sub_minor_offset;
	struct asic_fixed_properties *prop = &hdev->asic_prop;

	/* Calculation above returns an address for FW use, and therefore should
	 * be casted for driver use.
	 */
	return (fw_block_base_address - lower_32_bits(prop->cfg_base_address));
}

static bool hl_check_block_type_exclusion(struct hl_skip_blocks_cfg *skip_blocks_cfg,
		int block_type)
{
	int i;

	/* Check if block type is listed in the exclusion list of block types */
	for (i = 0 ; i < skip_blocks_cfg->block_types_len ; i++)
		if (block_type == skip_blocks_cfg->block_types[i])
			return true;

	return false;
}

static bool hl_check_block_range_exclusion(struct hl_device *hdev,
		struct hl_skip_blocks_cfg *skip_blocks_cfg,
		struct hl_special_block_info *block_info,
		u32 major, u32 minor, u32 sub_minor)
{
	u32 blocks_in_range, block_base_addr_in_range, block_base_addr;
	int i, j;

	block_base_addr = hl_automated_get_block_base_addr(hdev, block_info,
			major, minor, sub_minor);

	for (i = 0 ; i < skip_blocks_cfg->block_ranges_len ; i++) {
		blocks_in_range = (skip_blocks_cfg->block_ranges[i].end -
				skip_blocks_cfg->block_ranges[i].start) /
				HL_BLOCK_SIZE + 1;
		for (j = 0 ; j < blocks_in_range ; j++) {
			block_base_addr_in_range = skip_blocks_cfg->block_ranges[i].start +
					j * HL_BLOCK_SIZE;
			if (block_base_addr == block_base_addr_in_range)
				return true;
		}
	}

	return false;
}

static int hl_read_glbl_errors(struct hl_device *hdev,
		u32 blk_idx, u32 major, u32 minor, u32 sub_minor, void *data)
{
	struct hl_special_block_info *special_blocks = hdev->asic_prop.special_blocks;
	struct hl_special_block_info *current_block = &special_blocks[blk_idx];
	u32 glbl_err_addr, glbl_err_cause, addr_val, cause_val, block_base,
		base = current_block->base_addr - lower_32_bits(hdev->asic_prop.cfg_base_address);
	int i;

	block_base = base + major * current_block->major_offset +
			minor * current_block->minor_offset +
			sub_minor * current_block->sub_minor_offset;

	glbl_err_cause = block_base + HL_GLBL_ERR_CAUSE_OFFSET;
	cause_val = RREG32(glbl_err_cause);
	if (!cause_val)
		return 0;

	glbl_err_addr = block_base + HL_GLBL_ERR_ADDR_OFFSET;
	addr_val = RREG32(glbl_err_addr);

	for (i = 0 ; i < hdev->asic_prop.glbl_err_cause_num ; i++) {
		if (cause_val & BIT(i))
			dev_err_ratelimited(hdev->dev,
				"%s, addr %#llx\n",
				hl_glbl_error_cause[i],
				hdev->asic_prop.cfg_base_address + block_base +
				FIELD_GET(HL_GLBL_ERR_ADDRESS_MASK, addr_val));
	}

	WREG32(glbl_err_cause, cause_val);

	return 0;
}

void hl_check_for_glbl_errors(struct hl_device *hdev)
{
	struct asic_fixed_properties *prop = &hdev->asic_prop;
	struct hl_special_blocks_cfg special_blocks_cfg;
	struct iterate_special_ctx glbl_err_iter;
	int rc;

	memset(&special_blocks_cfg, 0, sizeof(special_blocks_cfg));
	special_blocks_cfg.skip_blocks_cfg = &prop->skip_special_blocks_cfg;

	glbl_err_iter.fn = &hl_read_glbl_errors;
	glbl_err_iter.data = &special_blocks_cfg;

	rc = hl_iterate_special_blocks(hdev, &glbl_err_iter);
	if (rc)
		dev_err_ratelimited(hdev->dev,
			"Could not iterate special blocks, glbl error check failed\n");
}

int hl_iterate_special_blocks(struct hl_device *hdev, struct iterate_special_ctx *ctx)
{
	struct hl_special_blocks_cfg *special_blocks_cfg =
			(struct hl_special_blocks_cfg *)ctx->data;
	struct hl_skip_blocks_cfg *skip_blocks_cfg =
			special_blocks_cfg->skip_blocks_cfg;
	u32 major, minor, sub_minor, blk_idx, num_blocks;
	struct hl_special_block_info *block_info_arr;
	int rc;

	block_info_arr = hdev->asic_prop.special_blocks;
	if (!block_info_arr)
		return -EINVAL;

	num_blocks = hdev->asic_prop.num_of_special_blocks;

	for (blk_idx = 0 ; blk_idx < num_blocks ; blk_idx++, block_info_arr++) {
		if (hl_check_block_type_exclusion(skip_blocks_cfg, block_info_arr->block_type))
			continue;

		for (major = 0 ; major < block_info_arr->major ; major++) {
			minor = 0;
			do {
				sub_minor = 0;
				do {
					if ((hl_check_block_range_exclusion(hdev,
							skip_blocks_cfg, block_info_arr,
							major, minor, sub_minor)) ||
						(skip_blocks_cfg->skip_block_hook &&
						skip_blocks_cfg->skip_block_hook(hdev,
							special_blocks_cfg,
							blk_idx, major, minor, sub_minor))) {
						sub_minor++;
						continue;
					}

					rc = ctx->fn(hdev, blk_idx, major, minor,
								sub_minor, ctx->data);
					if (rc)
						return rc;

					sub_minor++;
				} while (sub_minor < block_info_arr->sub_minor);

				minor++;
			} while (minor < block_info_arr->minor);
		}
	}

	return 0;
}