/
v3d_opt.c
2046 lines (1810 loc) · 55.8 KB
/
v3d_opt.c
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
/*******************************************************************************
Copyright 2010 Broadcom Corporation. All rights reserved.
Unless you and Broadcom execute a separate written software license agreement
governing use of this software, this software is licensed to you under the
terms of the GNU General Public License version 2, available at
http://www.gnu.org/copyleft/gpl.html (the "GPL").
Notwithstanding the above, under no circumstances may you combine this software
in any way with any other Broadcom software provided under a license other than
the GPL, without Broadcom's express prior written consent.
*******************************************************************************/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/semaphore.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <mach/hardware.h>
#include <mach/irqs.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/clk.h>
#include <mach/clkmgr.h>
#include <plat/syscfg.h>
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include <linux/kthread.h>
#include <linux/time.h>
#include <linux/proc_fs.h>
#include <linux/broadcom/v3d.h>
#include "reg_v3d.h"
#ifndef V3D_DEV_NAME
#define V3D_DEV_NAME "v3d"
#endif
#ifndef V3D_DEV_MAJOR
#define V3D_DEV_MAJOR 0
#endif
#ifndef BCM_CLK_V3D_POWER_STR_ID
#define BCM_CLK_V3D_POWER_STR_ID ""
#endif
#ifndef BCM21553_V3D_BASE
#define BCM21553_V3D_BASE 0
#endif
#ifndef IRQ_GRAPHICS
#define IRQ_GRAPHICS 0
#endif
#define FLAG0 0x0
#define FLAG1 0x4
#define TURN 0x8
/* Job Error Handling variables */
#define V3D_JOB_CACHE_CLEAR
#ifdef V3D_JOB_CACHE_CLEAR
#define V3D_ISR_TIMEOUT_IN_MS (250)
#define V3D_CACHE_MAX_RETRIES (1)
#define V3D_JOB_TIMEOUT_IN_MS (V3D_ISR_TIMEOUT_IN_MS * (V3D_CACHE_MAX_RETRIES+1))
#else
#define V3D_ISR_TIMEOUT_IN_MS (500)
#define V3D_JOB_TIMEOUT_IN_MS (V3D_ISR_TIMEOUT_IN_MS)
#endif
/* Enable the macro to retry the job on timeout, else will skip the job */
//#define V3D_JOB_RETRY_ON_TIMEOUT
#ifdef V3D_JOB_RETRY_ON_TIMEOUT
#define V3D_JOB_MAX_RETRIES (1)
#endif
typedef struct {
u32 id;
} v3d_t;
v3d_t *v3d_dev;
typedef struct v3d_job_t_ {
v3d_job_type_e job_type;
u32 dev_id;
uint32_t job_id;
uint32_t v3d_ct0ca;
uint32_t v3d_ct0ea;
uint32_t v3d_ct1ca;
uint32_t v3d_ct1ea;
uint32_t v3d_vpm_size;
u32 cache_retry_cnt;
u32 retry_cnt;
volatile v3d_job_status_e job_status;
u32 job_intern_state;
u32 job_wait_state;
wait_queue_head_t v3d_job_done_q;
struct v3d_job_t_ *next;
} v3d_job_t;
/* Driver module init variables */
static int v3d_major = V3D_DEV_MAJOR;
static struct class *v3d_class;
static void __iomem *v3d_base = NULL;
static struct clk *gClkPower, *gClkAHB;
static int irq_err = 1;
struct task_struct *v3d_thread_task;
unsigned int v3d_mempool_phys_base;
extern void *v3d_mempool_base;
extern unsigned long v3d_mempool_size;
static int v3d_bin_oom_block;
static int v3d_bin_oom_size = (3*1024*1024);
static void* v3d_bin_oom_cpuaddr;
static int v3d_bin_oom_block2;
static int v3d_bin_oom_size2 = (3*1024*1024);
static void* v3d_bin_oom_cpuaddr2;
#ifdef CONFIG_CPU_FREQ_GOV_BCM21553
#include <mach/bcm21553_cpufreq_gov.h>
static struct cpufreq_client_desc *cpufreq_client;
#endif
#ifdef CONFIG_BCM21553_V3D_SYNC_ENABLE
static spinlock_t v3d_id_lock = SPIN_LOCK_UNLOCKED;
extern void __iomem *sync_flag_base;
#endif
/* Static variable to check if the V3D power island is ON or OFF */
static int v3d_is_on = 0;
/* v3d driver state variables - shared by ioctl, isr, thread */
static u32 v3d_id = 1;
static volatile int v3d_in_use = 0;
/* event bits 0:rend_done, 1:bin_done, 4:qpu_done, 5:oom_fatal */
static volatile int v3d_flags = 0;
static int v3d_oom_block_used = 0;
v3d_job_t *v3d_job_head = NULL;
volatile v3d_job_t *v3d_job_curr = NULL;
/* Semaphore to lock between ioctl and thread for shared variable access
* WaitQue on which thread will block for job post or isr_completion or timeout
*/
struct semaphore v3d_sem;
wait_queue_head_t v3d_start_q, v3d_isr_done_q;
/* Enable the macro to enable proc file to see v3d usage */
#define ENABLE_PROCFS
#ifdef ENABLE_PROCFS
#define MAX_STR_SIZE (50)
static struct proc_dir_entry *v3d_proc_file;
static struct semaphore v3d_status_sem;
static void v3d_stat_start_point(void);
static void v3d_stat_end_point(void);
#define V3D_PROC_PRINT_D(str, val) \
if (v3d_proc_print_info(str, val, 0, 0, &curr, &len, count)) goto err;
#define V3D_PROC_PRINT_X(str, val) \
if (v3d_proc_print_info(str, val, 0, 1, &curr, &len, count)) goto err;
#define V3D_PROC_PRINT_F(str, val, fract) \
if (v3d_proc_print_info(str, val, fract, 2, &curr, &len, count)) goto err;
#define V3D_PROC_PRINT_HDR(str) \
if (v3d_proc_print_info(str, 0, 0, 3, &curr, &len, count)) goto err;
#ifndef MIN
#define MIN(A,B) ((A) > (B) ? (B) : (A))
#endif
#ifndef MAX
#define MAX(A,B) ((A) < (B) ? (B) : (A))
#endif
#define V3D_STAT_WINDOW_SIZE (5)
#define V3D_STAT_GROUP_SIZE (5)
/* Group stat vars */
typedef struct {
unsigned int start_sec;
unsigned int end_sec;
unsigned int avg_time_spent;
unsigned int min_time_spent;
unsigned int max_time_spent;
}v3d_stat_group_t;
static v3d_stat_group_t v3d_stat_groups[V3D_STAT_GROUP_SIZE];
static unsigned int v3d_stat_group_idx = 0;
/* Curr stat vars */
static unsigned int v3d_stat_time_spent_db[V3D_STAT_WINDOW_SIZE];
static unsigned int v3d_stat_time_spent_idx = 0;
/* Temp stat vars */
static unsigned int v3d_stat_sec = 0;
static unsigned int v3d_stat_time_spent_acc;
static unsigned int v3d_stat_start_usec = 0;
#endif
/* Enable the macro to bypass the job que; blocks from post to wait */
// #define V3D_BLOCKING_DRIVER
#ifdef V3D_BLOCKING_DRIVER
struct semaphore v3d_dbg_sem;
#define INIT_ACQUIRE init_MUTEX(&v3d_dbg_sem)
#define ACQUIRE_V3D down(&v3d_dbg_sem)
#define RELEASE_V3D up(&v3d_dbg_sem)
#else
#define INIT_ACQUIRE do {} while (0)
#define ACQUIRE_V3D do {} while (0)
#define RELEASE_V3D do {} while (0)
#endif
/* Enable the macro to enable counting isr events */
// #define DEBUG_V3D_ISR
#ifdef DEBUG_V3D_ISR
static int dbg_int_count = 0;
static int dbg_bin_int_count = 0;
static int dbg_rend_int_count = 0;
static int dbg_oom_int_count = 0;
static int dbg_osm_int_count = 0;
static int dbg_qpu_int_count = 0;
static int dbg_spurious_int_count = 0;
static int dbg_v3d_oom_fatal_cnt = 0;
static int dbg_v3d_oom_blk1_usage_cnt = 0;
static int dbg_v3d_oom_blk2_usage_cnt = 0;
static int dbg_v3d_prev_oom_blk2_cnt = 0;
#endif
/* Debug count variables for job activities */
// #define V3D_STATUS_PRINT_INTERVAL (10000)
static int dbg_job_post_rend_cnt = 0;
static int dbg_job_post_bin_rend_cnt = 0;
static int dbg_job_post_other_cnt = 0;
static int dbg_job_wait_cnt = 0;
static int dbg_job_timeout_cnt = 0;
#ifdef V3D_STATUS_PRINT_INTERVAL
static int dbg_print_status_loop_cnt = 0;
#endif
/* Dumps the control lists received - Enable only for necessary debug */
// #define DEBUG_DUMP_CL
#ifdef DEBUG_DUMP_CL
#define FILE_DUMP
#define DBG_MAX_NUM_LISTS (100)
#define DBG_MAX_LIST_SIZE (4*1024)
#define DBG_DATA_SIZE (DBG_MAX_NUM_LISTS * DBG_MAX_LIST_SIZE)
typedef struct dbg_list_hdr_t_ {
int valid;
int bin_start;
int bin_size;
int rend_start;
int rend_size;
v3d_job_post_t job_post;
v3d_job_status_e status;
} dbg_list_hdr_t;
static dbg_list_hdr_t dbg_list_hdr [DBG_MAX_NUM_LISTS];
static unsigned char dbg_data[DBG_DATA_SIZE];
static int dbg_list_idx = 0;
static int dbg_list_init_done = 0;
#endif
#define KLOG_TAG "v3d_opt.c"
#ifndef KLOG_TAG
#define KLOG_TAG __FILE__
#endif
/* Error Logs */
#if 1
#define KLOG_E(fmt,args...) \
do { printk(KERN_ERR "Error: [%s:%s:%d] "fmt"\n", KLOG_TAG, __func__, __LINE__, \
##args); } \
while (0)
#else
#define KLOG_E(x...) do {} while (0)
#endif
/* Debug Logs */
#if 1
#define KLOG_D(fmt,args...) \
do { printk(KERN_INFO KLOG_TAG "[%s:%d] "fmt"\n", __func__, __LINE__, \
##args); } \
while (0)
#else
#define KLOG_D(x...) do {} while (0)
#endif
/* Verbose Logs */
#if 0
#define KLOG_V(fmt,args...) \
do { printk(KERN_INFO KLOG_TAG "[%s:%d] "fmt"\n", __func__, __LINE__, \
##args); } \
while (0)
#else
#define KLOG_V(x...) do {} while (0)
#endif
static void v3d_job_kill(v3d_job_t *p_v3d_job, v3d_job_status_e job_status);
#ifdef CONFIG_BCM21553_V3D_SYNC_ENABLE
// Athena B0 V3D APB read back bug workaround
static inline void v3d_clean(void)
{
iowrite32(0, v3d_base + SCRATCH);
if (ioread32(v3d_base + SCRATCH))
iowrite32(0, v3d_base + SCRATCH);
}
void sync_mutex_down(volatile void __iomem *base)
{
iowrite32(1, base + FLAG0);
iowrite32(1, base + TURN);
while ((ioread32(base + FLAG1) == 1) && (ioread32(base + TURN) == 1));
}
void sync_mutex_up(volatile void __iomem *base)
{
iowrite32(0, base + FLAG0);
}
#endif
static inline u32 v3d_read(u32 reg)
{
u32 flags;
#ifdef CONFIG_BCM21553_V3D_SYNC_ENABLE
sync_mutex_down(sync_flag_base);
#endif
flags = ioread32(v3d_base + reg);
#ifdef CONFIG_BCM21553_V3D_SYNC_ENABLE
v3d_clean();
sync_mutex_up(sync_flag_base);
#endif
return flags;
}
static void v3d_power(int flag)
{
KLOG_D("v3d_power [%d] v3d_inuse[%d]", flag, v3d_in_use);
if (flag) {
/* Enable V3D island power */
clk_enable(gClkPower);
} else {
/* Disable V3D island power */
clk_disable(gClkPower);
}
}
static void v3d_turn_all_on(void)
{
KLOG_V("dvfs off, gClkAHB on");
#ifdef CONFIG_CPU_FREQ_GOV_BCM21553
cpufreq_bcm_dvfs_disable(cpufreq_client);
#endif
/* Check if the V3D power island is already ON. If not ON, switch it on */
if (!v3d_is_on) {
v3d_power(1);
v3d_is_on = 1;
}
clk_enable(gClkAHB);
}
static void v3d_turn_all_off(void)
{
KLOG_V("gClkAHB off, dvfs on");
#ifdef ENABLE_PROCFS
v3d_stat_end_point();
#endif
clk_disable(gClkAHB);
#ifdef CONFIG_CPU_FREQ_GOV_BCM21553
cpufreq_bcm_dvfs_enable(cpufreq_client);
#endif
}
#ifdef DEBUG_DUMP_CL
static void dbg_list_init (void)
{
int i;
for (i=0; i<DBG_MAX_NUM_LISTS; i++) {
dbg_list_hdr[i].valid = 0;
memset(&dbg_list_hdr[i].job_post, 0, sizeof(v3d_job_post_t));
}
memset(dbg_data, 0, DBG_DATA_SIZE);
dbg_list_init_done = 1;
}
static int dbg_list_get_next_idx(int idx)
{
int new_idx = idx+1;
if (new_idx >= DBG_MAX_NUM_LISTS) new_idx = 0;
return new_idx;
}
/*
static int dbg_list_get_prev_idx(int idx)
{
int new_idx = idx-1;
if (new_idx < 0) new_idx = DBG_MAX_NUM_LISTS-1;
return new_idx;
}
*/
static int dbg_list_get_slot (v3d_job_post_t *p_job_post)
{
int size;
size = (p_job_post->v3d_ct0ea - p_job_post->v3d_ct0ca);
size += (p_job_post->v3d_ct1ea - p_job_post->v3d_ct1ca);
if (size > DBG_MAX_LIST_SIZE) {
KLOG_E("Skipping storing the CL for job[%d] size[%d]", p_job_post->job_id, size);
return -1;
}
dbg_list_hdr[dbg_list_idx].valid = 0;
return (dbg_list_idx * DBG_MAX_LIST_SIZE);
}
static void dbg_list_store_job (v3d_job_post_t *p_job_post, int start, unsigned char *bin_addr, unsigned char *rend_addr)
{
int idx, bin_size, rend_size;
idx = dbg_list_idx;
bin_size = (p_job_post->v3d_ct0ea - p_job_post->v3d_ct0ca);
rend_size = (p_job_post->v3d_ct1ea - p_job_post->v3d_ct1ca);
dbg_list_hdr[idx].valid = 1;
dbg_list_hdr[idx].bin_start = start;
dbg_list_hdr[idx].bin_size = bin_size;
dbg_list_hdr[idx].rend_start = (start+bin_size);
dbg_list_hdr[idx].rend_size = rend_size;
memcpy(&dbg_list_hdr[idx].job_post, p_job_post, sizeof(v3d_job_post_t));
memcpy(&dbg_data[start], bin_addr, bin_size);
memcpy(&dbg_data[start+bin_size], rend_addr, rend_size);
}
static int dbg_is_dump_needed(void) {
if (dbg_list_idx == 0) {
return 1;
} else {
return 0;
}
}
static int dbg_list_dump_all (void)
{
static int dbg_file_idx = 0;
static int dbg_last_job_written = 0;
int idx, i;
char fname[64];
int wr_sz, job_id, cnt;
unsigned char *addr;
#ifdef FILE_DUMP
mm_segment_t oldfs;
struct file* dbg_filp = NULL;
oldfs = get_fs();
set_fs (KERNEL_DS);
snprintf(fname,64,"/data/dbg_dump/kdbg_list_%d.cl",dbg_file_idx);
dbg_filp = filp_open(fname, (O_WRONLY|O_TRUNC|O_LARGEFILE|O_CREAT), 0666);
if (dbg_filp == NULL) {
KLOG_E( "Failed to create [%s]", fname);
dbg_filp = NULL;
return -1;
}
#endif
idx = dbg_list_idx;
job_id = -1;
cnt = 0;
for (i=0; i<DBG_MAX_NUM_LISTS; i++) {
if (dbg_list_hdr[idx].valid) {
if (dbg_list_hdr[idx].job_post.job_id > dbg_last_job_written) {
#if 0
KLOG_E("i[%d] idx[%d] v[%d]: id[%d] b[%d] bs[%d] r[%d] rs[%d]",
i, idx, dbg_list_hdr[idx].valid, dbg_list_hdr[idx].job_post.job_id,
dbg_list_hdr[idx].bin_start, dbg_list_hdr[idx].bin_size,
dbg_list_hdr[idx].rend_start, dbg_list_hdr[idx].rend_size);
KLOG_E("[%p] [%p] [%p] [%p]",
dbg_list_hdr[idx].job_post.v3d_ct0ca, dbg_list_hdr[idx].job_post.v3d_ct0ea,
dbg_list_hdr[idx].job_post.v3d_ct1ca, dbg_list_hdr[idx].job_post.v3d_ct1ea);
#endif
#ifdef FILE_DUMP
if (dbg_filp != NULL) {
if (dbg_filp->f_op && dbg_filp->f_op->write )
{
addr = (void*)&dbg_list_hdr[idx];
wr_sz = sizeof(dbg_list_hdr_t);
dbg_filp->f_op->write( dbg_filp, addr, wr_sz, &dbg_filp->f_pos );
addr = &dbg_data[dbg_list_hdr[idx].bin_start];
wr_sz = dbg_list_hdr[idx].bin_size + dbg_list_hdr[idx].rend_size;
dbg_filp->f_op->write( dbg_filp, addr, wr_sz, &dbg_filp->f_pos );
}
}
#endif
if (job_id == -1) {
job_id = dbg_list_hdr[idx].job_post.job_id;
}
cnt++;
dbg_last_job_written = dbg_list_hdr[idx].job_post.job_id;
}
}
idx = dbg_list_get_next_idx(idx);
}
#ifdef FILE_DUMP
if (dbg_filp != NULL) {
filp_close( dbg_filp ,NULL );
}
set_fs (oldfs);
if (job_id != -1) {
KLOG_E("Dump [%d]jobs from job id[%d] to file[%s]", cnt, job_id, fname);
}
dbg_file_idx++;
#endif
return cnt;
}
static int dbg_list (v3d_job_post_t *p_job_post, unsigned char *bin_addr, unsigned char *rend_addr)
{
int start;
if (dbg_list_init_done == 0) {
dbg_list_init();
}
start = dbg_list_get_slot(p_job_post);
if (start < 0) {
return -1;
}
dbg_list_store_job(p_job_post, start, bin_addr, rend_addr);
dbg_list_idx = dbg_list_get_next_idx(dbg_list_idx);
return 0;
}
static int dbg_list_kern(v3d_job_t *p_v3d_job, int caller)
{
unsigned char *bin_addr, *rend_addr;
v3d_job_post_t job_post;
v3d_job_post_t *p_job_post = &job_post;
bin_addr = phys_to_virt(p_v3d_job->v3d_ct0ca);
rend_addr = phys_to_virt(p_v3d_job->v3d_ct1ca);
p_job_post->job_type = p_v3d_job->job_type;
p_job_post->job_id = p_v3d_job->job_id;
p_job_post->v3d_ct0ca = p_v3d_job->v3d_ct0ca;
p_job_post->v3d_ct0ea = p_v3d_job->v3d_ct0ea;
p_job_post->v3d_ct1ca = p_v3d_job->v3d_ct1ca;
p_job_post->v3d_ct1ea = p_v3d_job->v3d_ct1ea;
dbg_list (p_job_post, bin_addr, rend_addr);
return 0;
}
#endif
static irqreturn_t v3d_isr(int irq, void *dev_id)
{
u32 flags, flags_qpu, tmp;
int irq_retval = 0;
#ifdef DEBUG_V3D_ISR
dbg_int_count++;
#endif
/* Read the interrupt status registers */
flags = v3d_read(INTCTL);
flags_qpu = v3d_read(DBQITC);
/* Clear interrupts isr is going to handle */
tmp = flags & v3d_read(INTENA);
iowrite32(tmp, v3d_base + INTCTL);
if (flags_qpu) {
iowrite32(flags_qpu, v3d_base + DBQITC);
}
/* Set the bits in shared var for interrupts to be handled outside
* bits 0:rend_done, 1:bin_done, 4:qpu_done, 5:oom_fatal
*/
if (v3d_flags != 0) {
KLOG_E("Assert - v3d_flags not handled, next interrupt has come v3d_flags[0x%x] v3d_flags[0x%x]",
v3d_flags, (flags & 0x7));
}
v3d_flags = (flags & 0x3) | (flags_qpu ? (1 << 4) : 0);
/* Handle oom case */
if (flags & (1 << 2)) {
irq_retval = 1;
if (v3d_oom_block_used == 0) {
#ifdef DEBUG_V3D_ISR
dbg_v3d_oom_blk1_usage_cnt++;
#endif
KLOG_V("v3d oom blk not used: flags[0x%02x] intctl[0x%08x] bpoa[0x%08x] bpos[0x%08x] bpca[0x%08x] bpcs[0x%08x]",
flags, v3d_read(INTCTL), v3d_read(BPOA), v3d_read(BPOS), v3d_read(BPCA), v3d_read(BPCS));
iowrite32(v3d_bin_oom_block, v3d_base + BPOA);
iowrite32(v3d_bin_oom_size, v3d_base + BPOS);
v3d_oom_block_used = 1;
KLOG_V("v3d oom blk 1 given: flags[0x%02x] intctl[0x%08x] bpoa[0x%08x] bpos[0x%08x] bpca[0x%08x] bpcs[0x%08x]",
flags, v3d_read(INTCTL), v3d_read(BPOA), v3d_read(BPOS), v3d_read(BPCA), v3d_read(BPCS));
}else if (v3d_oom_block_used == 1) {
#ifdef DEBUG_V3D_ISR
dbg_v3d_oom_blk2_usage_cnt++;
#endif
KLOG_D("v3d oom blk 1 used: flags[0x%02x] intctl[0x%08x] bpoa[0x%08x] bpos[0x%08x] bpca[0x%08x] bpcs[0x%08x]",
flags, v3d_read(INTCTL), v3d_read(BPOA), v3d_read(BPOS), v3d_read(BPCA), v3d_read(BPCS));
iowrite32(v3d_bin_oom_block2, v3d_base + BPOA);
iowrite32(v3d_bin_oom_size2, v3d_base + BPOS);
v3d_oom_block_used = 2;
KLOG_D("v3d oom blk 2 given: flags[0x%02x] intctl[0x%08x] bpoa[0x%08x] bpos[0x%08x] bpca[0x%08x] bpcs[0x%08x]",
flags, v3d_read(INTCTL), v3d_read(BPOA), v3d_read(BPOS), v3d_read(BPCA), v3d_read(BPCS));
}else {
#ifdef DEBUG_V3D_ISR
dbg_v3d_oom_fatal_cnt++;
#endif
KLOG_E("v3d fatal: oom blk 2 used: flags[0x%02x] intctl[0x%08x] bpoa[0x%08x] bpos[0x%08x] bpca[0x%08x] bpcs[0x%08x]",
flags, v3d_read(INTCTL), v3d_read(BPOA), v3d_read(BPOS), v3d_read(BPCA), v3d_read(BPCS));
v3d_flags |= (1 << 5);
iowrite32(1 << 2, v3d_base + INTDIS);
}
/* Clear the oom interrupt ? */
iowrite32(1 << 2, v3d_base + INTCTL);
}
if (v3d_flags) {
irq_retval = 1;
v3d_in_use = 0;
v3d_oom_block_used = 0;
if ((v3d_flags != 1) && (v3d_flags != 2)) {
KLOG_D("v3d isr signal, v3d_flags[0x%x]", v3d_flags);
}
wake_up_interruptible(&v3d_isr_done_q);
}
#ifdef DEBUG_V3D_ISR
if (flags & (1 << 0)) {
dbg_rend_int_count++;
}
if (flags & (1 << 1)) {
dbg_bin_int_count++;
}
if (flags & (1 << 2)) {
dbg_oom_int_count++;
}
if (flags & (1 << 3)) {
dbg_osm_int_count++;
}
if (flags_qpu) {
dbg_qpu_int_count++;
}
if (!(irq_retval)) {
dbg_spurious_int_count++;
}
#endif
return IRQ_RETVAL(irq_retval);
}
#ifdef ENABLE_PROCFS
static void v3d_stat_update(struct timeval *p_tv)
{
int sec_passed;
/* Update the sliding window */
v3d_stat_time_spent_db[v3d_stat_time_spent_idx] = v3d_stat_time_spent_acc;
v3d_stat_time_spent_idx = (v3d_stat_time_spent_idx + 1) % V3D_STAT_WINDOW_SIZE;
sec_passed = p_tv->tv_sec - v3d_stat_sec;
if (sec_passed < 0) {
sec_passed = -sec_passed;
}
v3d_stat_groups[v3d_stat_group_idx].avg_time_spent
= (v3d_stat_groups[v3d_stat_group_idx].avg_time_spent + v3d_stat_time_spent_acc) >> 1;
v3d_stat_groups[v3d_stat_group_idx].min_time_spent
= MIN(v3d_stat_groups[v3d_stat_group_idx].min_time_spent, v3d_stat_time_spent_acc);
v3d_stat_groups[v3d_stat_group_idx].max_time_spent
= MAX(v3d_stat_groups[v3d_stat_group_idx].max_time_spent, v3d_stat_time_spent_acc);
if (sec_passed != 1) {
/* Update group */
v3d_stat_groups[v3d_stat_group_idx].end_sec = v3d_stat_sec;
v3d_stat_group_idx = (v3d_stat_group_idx + 1) % V3D_STAT_GROUP_SIZE;
v3d_stat_groups[v3d_stat_group_idx].start_sec = p_tv->tv_sec;
v3d_stat_groups[v3d_stat_group_idx].end_sec = 0;
v3d_stat_groups[v3d_stat_group_idx].avg_time_spent = 0;
v3d_stat_groups[v3d_stat_group_idx].min_time_spent = 1000001;
v3d_stat_groups[v3d_stat_group_idx].max_time_spent = 0;
}
// KLOG_D("tv[%d][%d] stat[%d][%d]", (int)p_tv->tv_sec, (int)p_tv->tv_usec, v3d_stat_sec, v3d_stat_time_spent_acc);
}
static void v3d_stat_start_point(void)
{
struct timeval tv;
do_gettimeofday(&tv);
down_interruptible(&v3d_status_sem);
if (v3d_stat_sec != tv.tv_sec) {
v3d_stat_update(&tv);
v3d_stat_sec = tv.tv_sec;
v3d_stat_time_spent_acc = 0;
}
v3d_stat_start_usec = tv.tv_usec;
up(&v3d_status_sem);
}
static void v3d_stat_end_point(void)
{
struct timeval tv;
do_gettimeofday(&tv);
down_interruptible(&v3d_status_sem);
if (v3d_stat_sec != tv.tv_sec) {
v3d_stat_time_spent_acc += 1000000 - v3d_stat_start_usec;
v3d_stat_update(&tv);
v3d_stat_sec = tv.tv_sec;
v3d_stat_time_spent_acc = tv.tv_usec;
}else {
v3d_stat_time_spent_acc += tv.tv_usec - v3d_stat_start_usec;
}
up(&v3d_status_sem);
}
/*
* v3d_proc_print_info()
* Description : Support function used by proc interface to print info on
* the proc memory (proc read).
* 0 - print value in integer format
* 1 - print value in hex format
* 2 - print header
*/
static int v3d_proc_print_info(char *str, int value, int fract, int print_type,
char **curr, int *len, int max_cnt)
{
int str_len = 0;
if (print_type == 0) {
str_len = sprintf(*curr,"\t%-30s: %d \n", str, value);
}
else if (print_type == 1) {
str_len = sprintf(*curr,"\t%-30s: 0x%08x \n", str, value);
}
else if (print_type == 2) {
str_len = sprintf(*curr,"\t%-30s: %d.%d\n", str, value, fract);
}
else if (print_type == 3) {
str_len = sprintf(*curr," %-30s: \n", str);
}
*curr += str_len;
*len += str_len;
if ((max_cnt - *len) < 100) {
KLOG_E("proc size[%d] not sufficient. Only [%d] bytes written", max_cnt, *len);
return 1;
}
return 0;
}
/*
* v3d_proc_get_status()
* Description : proc read callback to print the v3d status
*/
static int v3d_proc_get_status(char *page, char **start,
off_t off, int count, int *eof, void *data)
{
char *curr = page;
int len = 0;
int i, idx, avg;
KLOG_V("proc read has come: page[%p], off[%d], count[%d], data[%p] \n",
page, (int)off, count, data);
if (off != 0) {
goto err;
}
down_interruptible(&v3d_status_sem);
V3D_PROC_PRINT_HDR("Current Usage Info");
avg = 0;
idx = v3d_stat_time_spent_idx;
for (i=0; i<V3D_STAT_WINDOW_SIZE; i++) {
idx = (idx + 1) % V3D_STAT_WINDOW_SIZE;
V3D_PROC_PRINT_D("V3D usage", v3d_stat_time_spent_db[i]);
avg += v3d_stat_time_spent_db[i];
}
avg = avg/V3D_STAT_WINDOW_SIZE;
V3D_PROC_PRINT_D("Average V3D usage", avg);
V3D_PROC_PRINT_HDR("Group Usage Info");
idx = v3d_stat_group_idx;
for (i=0; i<V3D_STAT_GROUP_SIZE; i++) {
idx = (idx + 1) % V3D_STAT_GROUP_SIZE;
V3D_PROC_PRINT_HDR("Group");
V3D_PROC_PRINT_D("Start Sec", v3d_stat_groups[idx].start_sec);
V3D_PROC_PRINT_D("End Sec", v3d_stat_groups[idx].end_sec);
V3D_PROC_PRINT_D("Average", v3d_stat_groups[idx].avg_time_spent);
V3D_PROC_PRINT_D("Min", v3d_stat_groups[idx].min_time_spent);
V3D_PROC_PRINT_D("Max", v3d_stat_groups[idx].max_time_spent);
}
up(&v3d_status_sem);
err:
if (start) {
*start = page;
}
if (eof) {
*eof = 1;
}
return (len < count) ? len : count;
}
static int v3d_parse_string(const char *inputStr, u32 *opCode, u32 *arg)
{
int numArg;
char tempStr[MAX_STR_SIZE];
*opCode = 0;
numArg = sscanf(inputStr, "%s%u", tempStr, arg);
if (numArg < 1) {
return -1;
}
if (strcmp(tempStr, "reset_stat") == 0) {
*opCode = 1;
}
else if (strcmp(tempStr, "debug") == 0) {
*opCode = 2;
}
else if (strcmp(tempStr, "stat") == 0) {
*opCode = 3;
}
return 0;
}
/*
* v3d_proc_set_status()
* Description : proc write callback for multiple purposes
* reset_statistics - clear the min/max values to start statistics fresh
* debug %d - set the debug level
* stat %d - set the statistics level
*/
static int v3d_proc_set_status(struct file *file,
const char *buffer, unsigned long count, void *data)
{
char inputStr[MAX_STR_SIZE];
int len;
u32 opCode = 0;
u32 arg;
if (count > MAX_STR_SIZE) len = MAX_STR_SIZE;
else len = count;
if (copy_from_user(inputStr, buffer, len))
return -EFAULT;
inputStr[MAX_STR_SIZE-3] = '\0';
inputStr[MAX_STR_SIZE-2] = ' ';
inputStr[MAX_STR_SIZE-1] = '0';
v3d_parse_string(inputStr, &opCode, &arg);
switch (opCode) {
case 1:
KLOG_D ("Clearing max/min values in v3d status");
break;
case 2:
KLOG_D ("Setting the debug level to [%d]", arg);
break;
case 3:
KLOG_D ("Setting the statistics level to [%d]", arg);
break;
default:
KLOG_D ("reset_statistics - clear the min/max values to start statistics fresh");
KLOG_D ("debug n - set the debug level");
KLOG_D (" 1 - Print on all alloc and free");
KLOG_D ("stat n - set the statistics level");
KLOG_D (" 1 - Do run-time fragmentation check on all alloc/free");
break;
}
return count;
}
#endif
static void v3d_print_all_jobs(int bp)
{
v3d_job_t *tmp_job;
u32 n=0;
KLOG_V("Job post count rend[%d] bin_rend[%d] other[%d] Job wait count[%d] job timeout count[%d]",
dbg_job_post_rend_cnt, dbg_job_post_bin_rend_cnt, dbg_job_post_other_cnt,
dbg_job_wait_cnt, dbg_job_timeout_cnt);
switch (bp) {
case 0:
KLOG_V("Job queue Status after post...");
break;
case 1:
KLOG_V("Job queue Status after wait...");
break;
}
if (bp > 1) {
KLOG_D("head[0x%08x] curr[0x%08x]", (u32)v3d_job_head, (u32)v3d_job_curr);
tmp_job = v3d_job_head;
while (tmp_job != NULL ){
KLOG_D("\t job[%d] : [0x%08x] dev_id[%d] job_id[%d] type[%d] status[%d] intern[%d] wait[%d] retry[%d][%d]",
n, (u32)tmp_job, tmp_job->dev_id, tmp_job->job_id, tmp_job->job_type,
tmp_job->job_status, tmp_job->job_intern_state, tmp_job->job_wait_state,
tmp_job->cache_retry_cnt, tmp_job->retry_cnt);
tmp_job = tmp_job->next;
n++;
}
}
}
static v3d_job_t *v3d_job_create(struct file *filp, v3d_job_post_t *p_job_post)
{
v3d_t *dev;
v3d_job_t *p_v3d_job;
dev = (v3d_t *)(filp->private_data);
p_v3d_job = kmalloc(sizeof(v3d_job_t), GFP_KERNEL);
if (!p_v3d_job) {
KLOG_E("kmalloc failed in v3d_job_post");
return NULL;
}
p_v3d_job->dev_id = dev->id;
p_v3d_job->job_type = p_job_post->job_type;
p_v3d_job->job_id = p_job_post->job_id;
p_v3d_job->v3d_ct0ca = p_job_post->v3d_ct0ca;
p_v3d_job->v3d_ct0ea = p_job_post->v3d_ct0ea;
p_v3d_job->v3d_ct1ca = p_job_post->v3d_ct1ca;
p_v3d_job->v3d_ct1ea = p_job_post->v3d_ct1ea;
p_v3d_job->v3d_vpm_size = p_job_post->v3d_vpm_size;
p_v3d_job->job_status = V3D_JOB_STATUS_READY;
p_v3d_job->job_intern_state = 0;
p_v3d_job->job_wait_state = 0;
p_v3d_job->cache_retry_cnt = 0;
p_v3d_job->retry_cnt = 0;
p_v3d_job->next = NULL;
KLOG_V("job[0x%08x] dev_id[%d] job_id[%d] job_type[%d] ct0_ca[0x%x] ct0_ea[0x%x] ct1_ca[0x%x] ct1_ea[0x%x]",
(u32)p_v3d_job, dev->id, p_job_post->job_id, p_job_post->job_type, p_job_post->v3d_ct0ca, p_job_post->v3d_ct0ea,
p_job_post->v3d_ct1ca, p_job_post->v3d_ct1ea);
return p_v3d_job;
}
static void v3d_job_add(struct file *filp, v3d_job_t *p_v3d_job, int pos)
{
v3d_t *dev;
v3d_job_t *tmp_job;
dev = (v3d_t *)(filp->private_data);
if (NULL == v3d_job_head) {
KLOG_V("Adding job[0x%08x] to head[NULL]", (u32)p_v3d_job);
v3d_job_head = p_v3d_job;
} else {
tmp_job = v3d_job_head;
while (tmp_job->next != NULL ){
tmp_job = tmp_job->next;
}
KLOG_V("Adding job[0x%08x] to tail[0x%08x]", (u32)p_v3d_job, (u32)tmp_job);
tmp_job->next = p_v3d_job;
}
}
static v3d_job_t *v3d_job_search(struct file *filp, v3d_job_status_t *p_job_status)
{
v3d_t *dev;
v3d_job_t *tmp_job;
v3d_job_t *last_match_job = NULL;
dev = (v3d_t *)(filp->private_data);
tmp_job = v3d_job_head;
while (tmp_job != NULL ){
if (tmp_job->dev_id == dev->id) { // && (tmp_job->job_id == p_job_status->job_id)
last_match_job = tmp_job;
}
tmp_job = tmp_job->next;
}
KLOG_V("Last job to wait for hdl[%d]: [0x%08x]", dev->id, (u32)last_match_job);
return last_match_job;
}
static void v3d_job_free(struct file *filp, v3d_job_t *p_v3d_wait_job)
{
v3d_t *dev;
v3d_job_t *tmp_job, *parent_job;
v3d_job_t *last_match_job = NULL;
int curr_job_killed = 0;
dev = (v3d_t *)(filp->private_data);
KLOG_V("Free upto job[0x%08x] for hdl[%d]: ", (u32)p_v3d_wait_job, dev->id);
if (p_v3d_wait_job == NULL) {
KLOG_V("Free upto job[0x%08x] for hdl[%d]: ", (u32)p_v3d_wait_job, dev->id);
}
if ((v3d_job_head != NULL) && (v3d_job_head != p_v3d_wait_job) ) {
parent_job = v3d_job_head;
tmp_job = v3d_job_head->next;
while (tmp_job != NULL) {
if (tmp_job->dev_id == dev->id) {
last_match_job = tmp_job;
tmp_job = tmp_job->next;
parent_job->next = tmp_job;