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
|
/*
* Samsung Exynos SoC series NPU driver
*
* Copyright (c) 2017 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/pm_qos.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/clk-provider.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/ion_exynos.h>
#include <linux/dma-direct.h>
#include <linux/dma-mapping.h>
#include <linux/iommu.h>
#include <linux/smc.h>
#include <linux/soc/samsung/exynos-soc.h>
#include <linux/sched/clock.h>
#include <linux/atomic.h>
#include "npu-log.h"
#include "npu-config.h"
#include "npu-common.h"
#include "npu-device.h"
#include "npu-system.h"
#include "npu-util-regs.h"
#include "npu-system-soc.h"
#include "npu-stm-soc.h"
#ifdef CONFIG_NPU_USE_MBR
#include "dsp-npu.h"
#endif
/* EVT0 DSP secure SFR */
#define DSP_CPU_INITVTOR (0x17943000)
#define DSP_CPU_WAIT (0x1794300c)
#define DSP_CPU_RELEASE (0x17943040)
#define DNC_CPU0_CONFIGURATION (0x15863400)
#define SFR_APB (0x020)
#define DRCG_EN (0x104)
#define MEM_CLK (0x108)
#define LH_DRCG_EN (0x40C)
#define OFFSET_END 0xFFFFFFFF
#define TIMEOUT_ITERATION 100
/* Initialzation steps for system_resume */
enum npu_system_resume_soc_steps {
NPU_SYS_RESUME_SOC_CPU_ON,
NPU_SYS_RESUME_SOC_STM_SETUP,
NPU_SYS_RESUME_SOC_COMPLETED
};
#ifdef ENABLE_PWR_ON
static int npu_pwr_on(struct npu_system *system);
#endif
static int npu_cpu_on(struct npu_system *system);
static int npu_cpu_off(struct npu_system *system);
static int npu_memory_alloc_from_heap(struct platform_device *pdev, struct npu_memory_buffer *buffer, dma_addr_t daddr, const char *heapname);
static void npu_memory_free_from_heap(struct npu_memory_buffer *buffer);
#ifdef CONFIG_NPU_HARDWARE
static int init_iomem_area(struct npu_system *system);
#endif
int npu_system_soc_probe(struct npu_system *system, struct platform_device *pdev)
{
int ret = 0;
BUG_ON(!system);
#ifdef CONFIG_NPU_HARDWARE
npu_dbg("system soc probe: ioremap areas\n");
ret = init_iomem_area(system);
if (ret) {
probe_err("fail(%d) in init_iomem_area\n", ret);
goto p_err;
}
ret = npu_stm_probe(system);
if (ret) {
probe_err("npu_stm_probe error : %d\n", ret);
goto p_err;
}
#elif defined(CONFIG_NPU_LOOPBACK)
system->tcu_sram.vaddr = kmalloc(0x80000, GFP_KERNEL);
system->idp_sram.vaddr = kmalloc(0x100000, GFP_KERNEL);
system->sfr_npu[0].vaddr = kmalloc(0x100000, GFP_KERNEL);
system->sfr_npu[1].vaddr = kmalloc(0xf0000, GFP_KERNEL);
system->pmu_npu.vaddr = kmalloc(0x100, GFP_KERNEL);
system->pmu_npu_cpu.vaddr = kmalloc(0x100, GFP_KERNEL);
//system->baaw_npu.vaddr = kmalloc(0x100, GFP_KERNEL);
system->mbox_sfr.vaddr = kmalloc(0x17c, GFP_KERNEL);
system->pwm_npu.vaddr = kmalloc(0x10000, GFP_KERNEL);
#endif
p_err:
return ret;
}
int npu_system_soc_release(struct npu_system *system, struct platform_device *pdev)
{
int ret = 0;
ret = npu_stm_release(system);
if (ret)
npu_err("npu_stm_release error : %d\n", ret);
return 0;
}
static inline void print_iomem_area(const char *pr_name, const struct npu_iomem_area *mem_area)
{
if (mem_area->vaddr)
npu_info(KERN_CONT "(%8s) Phy(0x%08x)-(0x%08llx) Virt(%pK) Size(%llu)\n",
pr_name, mem_area->paddr, mem_area->paddr + mem_area->size,
mem_area->vaddr, mem_area->size);
}
static void print_all_iomem_area(const struct npu_system *system)
{
int i;
char buf[256];
#ifdef CONFIG_NPU_HARDWARE
npu_dbg("start in IOMEM mapping\n");
print_iomem_area("TCUSRAM", &system->tcu_sram);
print_iomem_area("IDPSRAM", &system->idp_sram);
print_iomem_area("SFR_DNC", &system->sfr_dnc);
for(i = 0; i < 2; i++) {
sprintf(buf, "SFR_NPUC%d", i);
print_iomem_area(buf, &system->sfr_npuc[i]);
}
for(i = 0; i < 4; i++) {
sprintf(buf, "SFR_NPU%d", i);
print_iomem_area(buf, &system->sfr_npu[i]);
}
print_iomem_area("PMU_NPU", &system->pmu_npu);
print_iomem_area("PMU_NCPU", &system->pmu_npu_cpu);
print_iomem_area("MBOX_SFR", &system->mbox_sfr);
print_iomem_area("PWM_NPU", &system->pwm_npu);
#ifdef CONFIG_CORESIGHT_STM
print_iomem_area("CORESIGHT", &system->sfr_coresight);
print_iomem_area("STM", &system->sfr_stm);
print_iomem_area("MCT_G", &system->sfr_mct_g);
#endif /* CONFIG_CORESIGHT_STM */
#endif /* CONFIG_NPU_HARDWARE */
npu_dbg("end in IOMEM mapping\n");
}
int npu_system_soc_resume(struct npu_system *system, u32 mode)
{
int ret = 0;
#ifdef CONFIG_NPU_LOOPBACK
return ret;
#endif
BUG_ON(!system);
/* Clear resume steps */
system->resume_soc_steps = 0;
print_all_iomem_area(system);
#ifdef ENABLE_PWR_ON
ret = npu_pwr_on(system);
if (ret) {
npu_err("fail(%d) in npu_pwr_on\n", ret);
goto p_err;
}
#endif
ret = npu_cpu_on(system);
if (ret) {
npu_err("fail(%d) in npu_cpu_on\n", ret);
goto p_err;
}
set_bit(NPU_SYS_RESUME_SOC_CPU_ON, &system->resume_soc_steps);
ret = npu_stm_enable(system);
if (ret) {
npu_err("fail(%d) in npu_stm_enable\n", ret);
goto p_err;
}
set_bit(NPU_SYS_RESUME_SOC_STM_SETUP, &system->resume_soc_steps);
set_bit(NPU_SYS_RESUME_SOC_COMPLETED, &system->resume_soc_steps);
return ret;
p_err:
npu_err("Failure detected[%d].\n", ret);
return ret;
}
int npu_system_soc_suspend(struct npu_system *system)
{
int ret = 0;
#ifdef CONFIG_NPU_LOOPBACK
return ret;
#endif
BUG_ON(!system);
BIT_CHECK_AND_EXECUTE(NPU_SYS_RESUME_SOC_COMPLETED, &system->resume_soc_steps, NULL, ;);
BIT_CHECK_AND_EXECUTE(NPU_SYS_RESUME_SOC_STM_SETUP, &system->resume_soc_steps, NULL, {
ret = npu_stm_disable(system);
if (ret)
npu_err("fail(%d) in npu_stm_disable\n", ret);
});
BIT_CHECK_AND_EXECUTE(NPU_SYS_RESUME_SOC_CPU_ON, &system->resume_soc_steps, "Turn NPU cpu off", {
ret += npu_cpu_off(system);
if (ret)
npu_err("fail(%d) in npu_cpu_off\n", ret);
});
if (system->resume_soc_steps != 0)
npu_warn("Missing clean-up steps [%lu] found.\n", system->resume_soc_steps);
/* Function itself never be failed, even thought there was some error */
return ret;
}
int npu_hwacg(struct npu_system *system, bool on)
{
int ret = 0;
#ifndef FORCE_HWACG_DISABLE
struct reg_set_map_2 *set_map;
size_t map_len;
const struct reg_set_map_2 hwacg_on_regs[] = {
//{&system->sfr_npu[0], 0x0800, 0x10000000, 0x10000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
//{&system->sfr_npu[0], 0x3154, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
//{&system->sfr_npu[0], 0x3158, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
//{&system->sfr_npu[1], 0x0800, 0x10000000, 0x10000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
//{&system->sfr_npu[1], 0x30E8, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
//{&system->sfr_npu[1], 0x30EC, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x0800, 0xF1000000, 0xF1000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
{&system->sfr_npu[2], 0x3154, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x3158, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x0800, 0xF1000000, 0xF1000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
{&system->sfr_npu[3], 0x30E8, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x30EC, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
};
const struct reg_set_map_2 hwacg_off_regs[] = {
//{&system->sfr_npu[0], 0x0800, 0x00000000, 0x10000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
//{&system->sfr_npu[0], 0x3154, 0x00000004, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
//{&system->sfr_npu[0], 0x3158, 0x00000004, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
//{&system->sfr_npu[1], 0x0800, 0x00000000, 0x10000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
//{&system->sfr_npu[1], 0x30E8, 0x00000004, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
//{&system->sfr_npu[1], 0x30EC, 0x00000004, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x0800, 0x00000000, 0xF1000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
{&system->sfr_npu[2], 0x3154, 0x00000000, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x3158, 0x00000000, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x0800, 0x00000000, 0xF1000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
{&system->sfr_npu[3], 0x30E8, 0x00000000, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x30EC, 0x00000000, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
};
if (on) {
set_map = (struct reg_set_map_2 *)hwacg_on_regs;
map_len = ARRAY_SIZE(hwacg_on_regs);
} else {
set_map = (struct reg_set_map_2 *)hwacg_off_regs;
map_len = ARRAY_SIZE(hwacg_off_regs);
}
npu_dbg("hwacg %s\n", on ? "on" : "off");
ret = npu_set_hw_reg_2(set_map, map_len, 0);
if (ret)
npu_err("Failed to write registers on npu_hwacg %s array (%d)\n",
on ? "on" : "off", ret);
#endif
return ret;
}
static int npu_cpu_on(struct npu_system *system)
{
int ret = 0;
const struct reg_set_map_2 cpu_on_regs[] = {
/* 9830: Many of these registers are valid, but are giving exceptions
; hence commented; may be needed later
*/
#ifdef FORCE_HWACG_DISABLE
{&system->sfr_npu[0], 0x800, 0x0, 0xF1000000}, /* NPU00_CMU_NPU00_CONTROLLER_OPTION */
{&system->sfr_npu[0], 0x3154, 0x00000000, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[0], 0x3158, 0x00000000, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[1], 0x800, 0x0, 0xF1000000}, /* NPU01_CMU_NPU01_CONTROLLER_OPTION */
{&system->sfr_npu[1], 0x30E8, 0x00000000, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[1], 0x30EC, 0x00000000, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x800, 0x0, 0xF1000000}, /* NPU10_CMU_NPU10_CONTROLLER_OPTION */
{&system->sfr_npu[2], 0x3154, 0x00000000, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x3158, 0x00000000, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x800, 0x0, 0xF1000000}, /* NPU11_CMU_NPU11_CONTROLLER_OPTION */
{&system->sfr_npu[3], 0x30E8, 0x00000000, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x30EC, 0x00000000, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
#else
{&system->sfr_npu[0], 0x800, 0x0, 0xF1000000}, /* NPU00_CMU_NPU00_CONTROLLER_OPTION */
{&system->sfr_npu[0], 0x3154, 0x00000000, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[0], 0x3158, 0x00000000, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[1], 0x800, 0x0, 0xF1000000}, /* NPU01_CMU_NPU01_CONTROLLER_OPTION */
{&system->sfr_npu[1], 0x30E8, 0x00000000, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[1], 0x30EC, 0x00000000, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x0800, 0xF1000000, 0xF1000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
{&system->sfr_npu[2], 0x3154, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x3158, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x0800, 0xF1000000, 0xF1000000}, /* NPU_CMU_NPU_CONTROLLER_OPTION */
{&system->sfr_npu[3], 0x30E8, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x30EC, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
#endif
//{&system->pmu_npu_cpu, 0x00, 0x01, 0x01}, /* NPU0_CPU_CONFIGURATION */
#ifdef FORCE_WDT_DISABLE
//{&system->pmu_npu_cpu, 0x20, 0x00, 0x02}, /* NPU0_CPU_OUT */
#else
//{&system->pmu_npu_cpu, 0x20, 0x02, 0x02}, /* NPU0_CPU_OUT */
#endif
};
#ifdef CONFIG_NPU_USE_MBR
npu_info("use Master Boot Record\n");
npu_info("ask to jump to 0x%x\n", (u32)system->fw_npu_memory_buffer->daddr);
ret = dsp_npu_release(true, system->fw_npu_memory_buffer->daddr);
if (ret) {
npu_err("Failed to release CPU_SS : %d\n", ret);
goto err_exit;
}
#else
npu_info("exynos_smc: setting DSP_CPU_INITVTOR(0x%x) with (0x%x).\n", DSP_CPU_INITVTOR, (u32)system->fw_npu_memory_buffer->daddr);
ret = exynos_smc(SMC_CMD_REG, SMC_REG_ID_SFR_W(DSP_CPU_INITVTOR), system->fw_npu_memory_buffer->daddr, 0x0);
if (ret) {
npu_err("Failed write register DSP_CPU_INITVTOR using exynos_smc(%d)\n", ret);
goto err_exit;
}
ret = exynos_smc(SMC_CMD_REG, SMC_REG_ID_SFR_W(DSP_CPU_RELEASE), 0, 0x0);
if (ret) {
npu_err("Failed to write register DSP_CPU_RELEASE using exynos_smc(%d))\n", ret);
goto err_exit;
}
ret = exynos_smc(SMC_CMD_REG, SMC_REG_ID_SFR_W(DSP_CPU_RELEASE), 1, 0x0);
if (ret) {
npu_err("Failed to write register SMC_REG_ID_SFR_W using exynos_smc(%d)\n", ret);
goto err_exit;
}
#endif
npu_info("start in npu_cpu_on\n");
ret = npu_set_hw_reg_2(cpu_on_regs, ARRAY_SIZE(cpu_on_regs), 0);
if (ret) {
npu_err("Failed to write registers on cpu_on_regs array (%d)\n", ret);
goto err_exit;
}
npu_info("complete in npu_cpu_on\n");
return 0;
err_exit:
npu_info("error(%d) in npu_cpu_on\n", ret);
return ret;
}
int npu_soc_core_on(struct npu_system *system, int core)
{
int ret = 0;
const struct reg_set_map_2 core0_on_regs[] = {
#ifdef FORCE_HWACG_DISABLE
{&system->sfr_npu[0], 0x800, 0x0, 0x31000000}, /* NPU00_CMU_NPU00_CONTROLLER_OPTION */
{&system->sfr_npu[1], 0x800, 0x0, 0x31000000}, /* NPU01_CMU_NPU01_CONTROLLER_OPTION */
#endif
};
const struct reg_set_map_2 core1_on_regs[] = {
#ifdef FORCE_HWACG_DISABLE
{&system->sfr_npu[2], 0x800, 0x0, 0x31000000}, /* NPU10_CMU_NPU10_CONTROLLER_OPTION */
{&system->sfr_npu[3], 0x800, 0x0, 0x31000000}, /* NPU11_CMU_NPU11_CONTROLLER_OPTION */
#endif
};
const struct reg_set_map_2 *core_on_regs[] = {
core0_on_regs,
core1_on_regs
};
const int core_on_regs_len[] = {
ARRAY_SIZE(core0_on_regs),
ARRAY_SIZE(core1_on_regs)
};
BUG_ON(!system);
npu_info("start %s for core %d (%d)\n", __func__, core, core_on_regs_len[core]);
ret = npu_set_hw_reg_2(core_on_regs[core], core_on_regs_len[core], 0);
if (ret) {
npu_err("Failed to write registers on core_on_regs array (%d)\n", ret);
goto err_exit;
}
npu_info("complete %s for core %d\n", __func__, core);
return 0;
err_exit:
npu_info("error(%d) in %s\n", ret, __func__);
return ret;
}
static int npu_cpu_off(struct npu_system *system)
{
int ret;
const struct reg_set_map_2 cpu_off_regs[] = {
/* 9830: These registers are valid, but are giving exceptions
; hence commented; may be needed later
*/
/* to power off NPU cores, HWACG should be enable */
{&system->sfr_npu[0], 0x800, 0xF1000000, 0xF1000000}, /* NPU00_CMU_NPU00_CONTROLLER_OPTION */
{&system->sfr_npu[0], 0x3154, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[0], 0x3158, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[1], 0x800, 0xF1000000, 0xF1000000}, /* NPU01_CMU_NPU01_CONTROLLER_OPTION */
{&system->sfr_npu[1], 0x30E8, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[1], 0x30EC, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x800, 0xF1000000, 0xF1000000}, /* NPU10_CMU_NPU10_CONTROLLER_OPTION */
{&system->sfr_npu[2], 0x3154, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[2], 0x3158, 0x00000006, 0x00000006}, /* QCH_CON_LHS_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x800, 0xF1000000, 0xF1000000}, /* NPU11_CMU_NPU11_CONTROLLER_OPTION */
{&system->sfr_npu[3], 0x30E8, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
{&system->sfr_npu[3], 0x30EC, 0x00000006, 0x00000006}, /* QCH_CON_LHM_AST_D_NPUC_UNIT0_SETREG_QCH */
};
BUG_ON(!system);
BUG_ON(!system->pdev);
#ifdef CONFIG_NPU_USE_MBR
ret = dsp_npu_release(false, 0);
if (ret) {
npu_err("Failed to release CPU_SS : %d\n", ret);
goto err_exit;
}
#endif
npu_info("start in npu_cpu_off\n");
ret = npu_set_hw_reg_2(cpu_off_regs, ARRAY_SIZE(cpu_off_regs), 0);
if (ret) {
npu_err("fail(%d) in npu_set_hw_reg(cpu_off_regs)\n", ret);
goto err_exit;
}
npu_info("complete in npu_cpu_off\n");
return 0;
err_exit:
npu_info("error(%d) in npu_cpu_off\n", ret);
return ret;
}
int npu_soc_core_off(struct npu_system *system, int core)
{
int ret;
const struct reg_set_map_2 core0_off_regs[] = {
#ifdef FORCE_HWACG_DISABLE
{&system->sfr_npu[0], 0x800, 0x31000000, 0x31000000}, /* NPU00_CMU_NPU00_CONTROLLER_OPTION */
{&system->sfr_npu[1], 0x800, 0x31000000, 0x31000000}, /* NPU01_CMU_NPU01_CONTROLLER_OPTION */
#endif
};
const struct reg_set_map_2 core1_off_regs[] = {
#ifdef FORCE_HWACG_DISABLE
{&system->sfr_npu[2], 0x800, 0x31000000, 0x31000000}, /* NPU10_CMU_NPU10_CONTROLLER_OPTION */
{&system->sfr_npu[3], 0x800, 0x31000000, 0x31000000}, /* NPU11_CMU_NPU11_CONTROLLER_OPTION */
#endif
};
const struct reg_set_map_2 *core_off_regs[] = {
core0_off_regs,
core1_off_regs
};
const int core_off_regs_len[] = {
ARRAY_SIZE(core0_off_regs),
ARRAY_SIZE(core1_off_regs)
};
BUG_ON(!system);
npu_info("start %s for core %d (%d)\n", __func__, core, core_off_regs_len[core]);
ret = npu_set_hw_reg_2(core_off_regs[core], core_off_regs_len[core], 0);
if (ret) {
npu_err("Failed to write registers on core_off_regs array (%d)\n", ret);
goto err_exit;
}
npu_info("complete %s for core %d\n", __func__, core);
return 0;
err_exit:
npu_info("error(%d) in %s\n", ret, __func__);
return ret;
}
#ifdef ENABLE_PWR_ON
static __attribute__((unused)) int npu_pwr_on(struct npu_system *system)
{
int ret = 0;
int check_pass;
u32 v;
size_t i;
int j;
void __iomem *reg_addr;
const static struct reg_set_map pwr_on_regs[] = {
{0x00, 0x01, 0x01}, /* NPU0_CONFIGURATION */
{0x80, 0x01, 0x01}, /* NPU1_CONFIGURATION */
};
const static struct reg_set_map pwr_status_regs[] = {
{0x04, 0x01, 0x01}, /* NPU0_STATUS */
{0x84, 0x01, 0x01}, /* NPU1_STATUS */
};
BUG_ON(!system);
BUG_ON(!system->pdev);
npu_info("start in npu_pwr_on\n");
npu_dbg("set power-on NPU in npu_pwr_on\n");
ret = npu_set_hw_reg(&system->pmu_npu, pwr_on_regs, ARRAY_SIZE(pwr_on_regs), 0);
if (ret) {
npu_err("fail(%d) in npu_set_hw_reg(pwr_on_regs)\n", ret);
goto err_exit;
}
/* Check status */
npu_dbg("check status in npu_pwr_on\n");
for (j = 0, check_pass = 0; j < TIMEOUT_ITERATION && !check_pass; j++) {
check_pass = 1;
for (i = 0; i < ARRAY_SIZE(pwr_status_regs); ++i) {
// Check status flag
reg_addr = system->pmu_npu.vaddr + pwr_status_regs[i].offset;
v = readl(reg_addr);
if ((v & pwr_status_regs[i].mask) != pwr_status_regs[i].val)
check_pass = 0;
npu_dbg("get status NPU[%zu]=0x%08x\n", i, v);
}
mdelay(10);
}
/* Timeout check */
if (j >= TIMEOUT_ITERATION) {
ret = -ETIMEDOUT;
goto err_exit;
}
npu_info("complete in npu_pwr_on\n");
return 0;
err_exit:
npu_info("error(%d) in npu_pwr_on\n", ret);
return ret;
}
#endif
#ifdef CONFIG_NPU_HARDWARE
struct iomem_reg_t {
u32 dummy;
u32 start;
u32 size;
};
static inline void set_mailbox_channel(struct npu_system *system, int ch)
{
BUG_ON(ch > 1);
probe_info("mailbox channel : %d\n", ch);
system->mbox_sfr.vaddr = system->sfr_mbox[ch].vaddr;
system->mbox_sfr.paddr = system->sfr_mbox[ch].paddr;
system->mbox_sfr.size = system->sfr_mbox[ch].size;
}
static inline void set_max_npu_core(struct npu_system *system, s32 num)
{
BUG_ON(num < 0);
probe_info("Max number of npu core : %d\n", num);
system->max_npu_core = num;
}
static inline int get_iomem_data_index(const struct npu_iomem_init_data data[], const char* name)
{
int i;
for(i = 0; data[i].name != NULL; i++) {
if (!strcmp(data[i].name, name))
return i;
}
return -1;
}
static struct npu_memory_buffer *flush_memory_buffer;
static int init_iomem_area(struct npu_system *system)
{
int ret = 0;
int i, k, di;
void __iomem *iomem;
struct device *dev;
int iomem_count;
struct iomem_reg_t *iomem_data;
const char **iomem_name;
struct npu_iomem_area *id;
struct npu_memory_buffer **bd = NULL;
const struct npu_iomem_init_data init_data[] = {
{NULL, "TCUSRAM", (void*)&system->tcu_sram},
{NULL, "IDPSRAM", (void*)&system->idp_sram},
{NULL, "SFR_DNC", (void*)&system->sfr_dnc},
{NULL, "SFR_NPUC0", (void*)&system->sfr_npuc[0]},
{NULL, "SFR_NPUC1", (void*)&system->sfr_npuc[1]},
{NULL, "SFR_NPU0", (void*)&system->sfr_npu[0]},
{NULL, "SFR_NPU1", (void*)&system->sfr_npu[1]},
{NULL, "SFR_NPU2", (void*)&system->sfr_npu[2]},
{NULL, "SFR_NPU3", (void*)&system->sfr_npu[3]},
#ifdef CONFIG_CORESIGHT_STM
{NULL, "SFR_CORESIGHT", (void*)&system->sfr_coresight},
{NULL, "SFR_STM", (void*)&system->sfr_stm},
{NULL, "SFR_MCT_G", (void*)&system->sfr_mct_g},
#endif
{NULL, "PMU", (void*)&system->pmu_npu},
{NULL, "PMUCPU", (void*)&system->pmu_npu_cpu},
{NULL, "MAILBOX0", (void*)&system->sfr_mbox[0]},
{NULL, "MAILBOX1", (void*)&system->sfr_mbox[1]},
{NULL, "PWM", (void*)&system->pwm_npu},
{"npu_fw", "FW_DRAM", (void*)&system->fw_npu_memory_buffer},
{"npu_fw", "FW_UNITTEST", (void*)&system->fw_npu_unittest_buffer},
{"npu_fw", "FW_LOG", (void*)&system->fw_npu_log_buffer},
{NULL, NULL, NULL}
};
BUG_ON(!system);
BUG_ON(!system->pdev);
probe_trace("start in iomem area.\n");
dev = &(system->pdev->dev);
iomem_count = of_property_count_strings(
dev->of_node, "samsung,npumem-names");
if (IS_ERR_VALUE((unsigned long)iomem_count)) {
probe_err("invalid iomem list in %s node", dev->of_node->name);
ret = -EINVAL;
goto err_exit;
}
iomem_name = (const char **)devm_kmalloc(dev,
(iomem_count + 1) * sizeof(const char *),
GFP_KERNEL);
if (!iomem_name) {
probe_err("failed to alloc for iomem names");
ret = -ENOMEM;
goto err_exit;
}
for (i = 0; i < iomem_count; i++) {
ret = of_property_read_string_index(dev->of_node,
"samsung,npumem-names", i, &iomem_name[i]);
if (ret) {
probe_err("failed to read iomem name %d from %s node\n",
i, dev->of_node->name);
goto err_exit;
}
}
iomem_name[iomem_count] = NULL;
iomem_data = (struct iomem_reg_t *)devm_kmalloc(dev,
iomem_count * sizeof(struct iomem_reg_t), GFP_KERNEL);
if (!iomem_data) {
probe_err("failed to alloc for iomem data");
ret = -ENOMEM;
goto err_exit;
}
ret = of_property_read_u32_array(dev->of_node, "samsung,npumem-address", (u32 *)iomem_data,
iomem_count * sizeof(struct iomem_reg_t) / sizeof(u32));
if (ret) {
probe_err("failed to get iomem data");
goto err_exit;
}
for (i = 0; iomem_name[i] != NULL; i++) {
di = get_iomem_data_index(init_data, iomem_name[i]);
if (di < 0) {
probe_err("invalid npumem names : %s\n", iomem_name[i]);
continue;
}
if (init_data[di].heapname) {
bd = (struct npu_memory_buffer **)init_data[di].area_info;
*bd = kcalloc(1, sizeof(struct npu_memory_buffer), GFP_KERNEL);
if (!(*bd)) {
probe_err("*bd is NULL\n");
goto err_exit;
}
(*bd)->size = (iomem_data + i)->size;
ret = npu_memory_alloc_from_heap(system->pdev, *bd,
(iomem_data + i)->start, init_data[di].heapname);
if (ret) {
for (k = 0; init_data[k].name != NULL; k++) {
if (init_data[k].heapname) {
bd = (struct npu_memory_buffer **)init_data[k].area_info;
if (*bd) {
if ((*bd)->vaddr)
npu_memory_free_from_heap(*bd);
kfree(*bd);
*bd = NULL;
}
}
}
probe_err("buffer allocation from %s heap failed w/ err: %d\n",
init_data[di].heapname, ret);
ret = -EFAULT;
goto err_exit;
}
}
else {
id = (struct npu_iomem_area *)init_data[di].area_info;
id->paddr = (iomem_data + i)->start;
id->size = (iomem_data + i)->size;
iomem = devm_ioremap_nocache(&(system->pdev->dev),
(iomem_data + i)->start, (iomem_data + i)->size);
if (IS_ERR_OR_NULL(iomem)) {
probe_err("fail(%pK) in devm_ioremap_nocache(0x%08x, %u)\n",
iomem, id->paddr, (u32)id->size);
ret = -EFAULT;
goto err_exit;
}
id->vaddr = iomem;
probe_info("%s : Paddr[%08x], [%08x] => Mapped @[%pK], Length = %llu\n",
iomem_name[i], (iomem_data + i)->start, (iomem_data + i)->size,
id->vaddr, id->size);
}
}
set_mailbox_channel(system, 0);
set_max_npu_core(system, 2);
flush_memory_buffer = system->fw_npu_memory_buffer;
probe_trace("complete in init_iomem_area\n");
return 0;
err_exit:
if (*bd)
kfree(*bd);
probe_err("error(%d) in init_iomem_area\n", ret);
return ret;
}
void npu_memory_sync_for_cpu(void)
{
dma_sync_single_for_cpu(
flush_memory_buffer->attachment->dev,
(dma_addr_t)(sg_dma_address(flush_memory_buffer->sgt->sgl) + NPU_MAILBOX_BASE),
(size_t)NPU_MAILBOX_SIZE,
DMA_FROM_DEVICE);
}
void npu_memory_sync_for_device(void)
{
dma_sync_single_for_device(
flush_memory_buffer->attachment->dev,
(dma_addr_t)(sg_dma_address(flush_memory_buffer->sgt->sgl) + NPU_MAILBOX_BASE),
(size_t)NPU_MAILBOX_SIZE,
DMA_TO_DEVICE);
}
static int npu_memory_alloc_from_heap(struct platform_device *pdev, struct npu_memory_buffer *buffer, dma_addr_t daddr, const char *heapname)
{
int ret = 0;
bool complete_suc = false;
struct dma_buf *dma_buf;
struct dma_buf_attachment *attachment;
struct scatterlist *sg;
struct sg_table *sgt;
phys_addr_t phys_addr;
void *vaddr;
int i;
int flag;
size_t size;
BUG_ON(!buffer);
buffer->dma_buf = NULL;
buffer->attachment = NULL;
buffer->sgt = NULL;
buffer->daddr = 0;
buffer->vaddr = NULL;
INIT_LIST_HEAD(&buffer->list);
flag = 0;
size = buffer->size;
dma_buf = ion_alloc_dmabuf(heapname, size, flag);
if (IS_ERR_OR_NULL(dma_buf)) {
npu_err("dma_buf_get is fail(%p)\n", dma_buf);
ret = -EINVAL;
goto p_err;
}
buffer->dma_buf = dma_buf;
attachment = dma_buf_attach(dma_buf, &pdev->dev);
if (IS_ERR(attachment)) {
ret = PTR_ERR(attachment);
npu_err("dma_buf_attach is fail(%d)\n", ret);
goto p_err;
}
buffer->attachment = attachment;
sgt = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
npu_err("dma_buf_map_attach is fail(%d)\n", ret);
goto p_err;
}
buffer->sgt = sgt;
for_each_sg(buffer->sgt->sgl, sg, sgt->nents, i) {
phys_addr = dma_to_phys(&pdev->dev, sg->dma_address);
}
buffer->paddr = phys_addr;
if(!daddr) {
daddr = ion_iovmm_map(attachment, 0, size, DMA_BIDIRECTIONAL, 0);
if (IS_ERR_VALUE(daddr)) {
npu_err("fail(err %pad) in ion_iovmm_map\n", &daddr);
ret = -ENOMEM;
goto p_err;
}
} else {
struct iommu_domain *domain = iommu_get_domain_for_dev(&pdev->dev);
ret = iommu_map(domain, daddr, phys_addr, size, 0);
if (ret) {
npu_err("fail(err %pad) in iommu_map\n", &daddr);
ret = -ENOMEM;
goto p_err;
}
}
buffer->daddr = daddr;
vaddr = dma_buf_vmap(dma_buf);
if (IS_ERR(vaddr)) {
npu_err("fail(err %p) in dma_buf_vmap\n", vaddr);
ret = -EFAULT;
goto p_err;
}
buffer->vaddr = vaddr;
complete_suc = true;
npu_dbg("buffer[%p], paddr[%llx], vaddr[%p], daddr[%llx], sgt[%p], attachment[%p]\n",
buffer, buffer->paddr, buffer->vaddr, buffer->daddr, buffer->sgt, buffer->attachment);
p_err:
if (complete_suc != true) {
npu_memory_free_from_heap(buffer);
}
return ret;
}
static void npu_memory_free_from_heap(struct npu_memory_buffer *buffer)
{
if (buffer->vaddr) {
dma_buf_vunmap(buffer->dma_buf, buffer->vaddr);
}
if (buffer->daddr) {
ion_iovmm_unmap(buffer->attachment, buffer->daddr);
}
if (buffer->sgt) {
dma_buf_unmap_attachment(buffer->attachment, buffer->sgt, DMA_BIDIRECTIONAL);
}
if (buffer->attachment) {
dma_buf_detach(buffer->dma_buf, buffer->attachment);
}
if (buffer->dma_buf) {
dma_buf_put(buffer->dma_buf);
}
buffer->dma_buf = NULL;
buffer->attachment = NULL;
buffer->sgt = NULL;
buffer->daddr = 0;
buffer->vaddr = NULL;
}
void npu_soc_status_report(struct npu_system *system)
{
BUG_ON(!system);
npu_info("CA5 PC : CPU0 0x%x, CPU1 0x%x\n",
readl(system->sfr_dnc.vaddr + 0x100cc),
readl(system->sfr_dnc.vaddr + 0x100d0));
npu_info("DSPC DBG STATUS : INTR 0x%x, DINTR 0x%x\n",
readl(system->sfr_dnc.vaddr + 0x110c),
readl(system->sfr_dnc.vaddr + 0x1144));
}
u32 npu_get_hw_info(void)
{
union npu_hw_info hw_info;
memset(&hw_info, 0, sizeof(hw_info));
BUG_ON(exynos_soc_info.product_id != EXYNOS9830_SOC_ID);
hw_info.fields.product_id = 0x9830;
/* DCache disable before EVT 1.1 */
if ( (exynos_soc_info.main_rev >= 2)
|| (exynos_soc_info.main_rev >= 1 && exynos_soc_info.sub_rev >= 1) )
hw_info.fields.dcache_en = 1;
else
hw_info.fields.dcache_en = 0;
npu_info("HW Info = %08x\n", hw_info.value);
return hw_info.value;
}
#endif
|