-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexynos4210_mct.c
1554 lines (1300 loc) · 44.6 KB
/
exynos4210_mct.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
/*
* Samsung exynos4210 Multi Core timer
*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
* All rights reserved.
*
* Evgeny Voevodin <[email protected]>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Global Timer:
*
* Consists of two timers. First represents Free Running Counter and second
* is used to measure interval from FRC to nearest comparator.
*
* 0 UINT64_MAX
* | timer0 |
* | <-------------------------------------------------------------- |
* | --------------------------------------------frc---------------> |
* |______________________________________________|__________________|
* CMP0 CMP1 CMP2 | CMP3
* __| |_
* | timer1 |
* | -------------> |
* frc CMPx
*
* Problem: when implementing global timer as is, overflow arises.
* next_time = cur_time + period * count;
* period and count are 64 bits width.
* Lets arm timer for MCT_GT_COUNTER_STEP count and update internal G_CNT
* register during each event.
*
* Problem: both timers need to be implemented using MCT_XT_COUNTER_STEP because
* local timer contains two counters: TCNT and ICNT. TCNT == 0 -> ICNT--.
* IRQ is generated when ICNT riches zero. Implementation where TCNT == 0
* generates IRQs suffers from too frequently events. Better to have one
* uint64_t counter equal to TCNT*ICNT and arm ptimer.c for a minimum(TCNT*ICNT,
* MCT_GT_COUNTER_STEP); (yes, if target tunes ICNT * TCNT to be too low values,
* there is no way to avoid frequently events).
*/
#include "qemu/osdep.h"
#include "qemu/log.h"
#include "hw/sysbus.h"
#include "migration/vmstate.h"
#include "qemu/timer.h"
#include "qemu/module.h"
#include "hw/ptimer.h"
#include "hw/arm/exynos4210.h"
#include "hw/irq.h"
//#define DEBUG_MCT
#ifdef DEBUG_MCT
#define DPRINTF(fmt, ...) \
do { fprintf(stdout, "MCT: [%24s:%5d] " fmt, __func__, __LINE__, \
## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...) do {} while (0)
#endif
#define MCT_CFG 0x000
#define G_CNT_L 0x100
#define G_CNT_U 0x104
#define G_CNT_WSTAT 0x110
#define G_COMP0_L 0x200
#define G_COMP0_U 0x204
#define G_COMP0_ADD_INCR 0x208
#define G_COMP1_L 0x210
#define G_COMP1_U 0x214
#define G_COMP1_ADD_INCR 0x218
#define G_COMP2_L 0x220
#define G_COMP2_U 0x224
#define G_COMP2_ADD_INCR 0x228
#define G_COMP3_L 0x230
#define G_COMP3_U 0x234
#define G_COMP3_ADD_INCR 0x238
#define G_TCON 0x240
#define G_INT_CSTAT 0x244
#define G_INT_ENB 0x248
#define G_WSTAT 0x24C
#define L0_TCNTB 0x300
#define L0_TCNTO 0x304
#define L0_ICNTB 0x308
#define L0_ICNTO 0x30C
#define L0_FRCNTB 0x310
#define L0_FRCNTO 0x314
#define L0_TCON 0x320
#define L0_INT_CSTAT 0x330
#define L0_INT_ENB 0x334
#define L0_WSTAT 0x340
#define L1_TCNTB 0x400
#define L1_TCNTO 0x404
#define L1_ICNTB 0x408
#define L1_ICNTO 0x40C
#define L1_FRCNTB 0x410
#define L1_FRCNTO 0x414
#define L1_TCON 0x420
#define L1_INT_CSTAT 0x430
#define L1_INT_ENB 0x434
#define L1_WSTAT 0x440
#define MCT_CFG_GET_PRESCALER(x) ((x) & 0xFF)
#define MCT_CFG_GET_DIVIDER(x) (1 << ((x) >> 8 & 7))
#define GET_G_COMP_IDX(offset) (((offset) - G_COMP0_L) / 0x10)
#define GET_G_COMP_ADD_INCR_IDX(offset) (((offset) - G_COMP0_ADD_INCR) / 0x10)
#define G_COMP_L(x) (G_COMP0_L + (x) * 0x10)
#define G_COMP_U(x) (G_COMP0_U + (x) * 0x10)
#define G_COMP_ADD_INCR(x) (G_COMP0_ADD_INCR + (x) * 0x10)
/* MCT bits */
#define G_TCON_COMP_ENABLE(x) (1 << 2 * (x))
#define G_TCON_AUTO_ICREMENT(x) (1 << (2 * (x) + 1))
#define G_TCON_TIMER_ENABLE (1 << 8)
#define G_INT_ENABLE(x) (1 << (x))
#define G_INT_CSTAT_COMP(x) (1 << (x))
#define G_CNT_WSTAT_L 1
#define G_CNT_WSTAT_U 2
#define G_WSTAT_COMP_L(x) (1 << 4 * (x))
#define G_WSTAT_COMP_U(x) (1 << ((4 * (x)) + 1))
#define G_WSTAT_COMP_ADDINCR(x) (1 << ((4 * (x)) + 2))
#define G_WSTAT_TCON_WRITE (1 << 16)
#define GET_L_TIMER_IDX(offset) ((((offset) & 0xF00) - L0_TCNTB) / 0x100)
#define GET_L_TIMER_CNT_REG_IDX(offset, lt_i) \
(((offset) - (L0_TCNTB + 0x100 * (lt_i))) >> 2)
#define L_ICNTB_MANUAL_UPDATE (1 << 31)
#define L_TCON_TICK_START (1)
#define L_TCON_INT_START (1 << 1)
#define L_TCON_INTERVAL_MODE (1 << 2)
#define L_TCON_FRC_START (1 << 3)
#define L_INT_CSTAT_INTCNT (1 << 0)
#define L_INT_CSTAT_FRCCNT (1 << 1)
#define L_INT_INTENB_ICNTEIE (1 << 0)
#define L_INT_INTENB_FRCEIE (1 << 1)
#define L_WSTAT_TCNTB_WRITE (1 << 0)
#define L_WSTAT_ICNTB_WRITE (1 << 1)
#define L_WSTAT_FRCCNTB_WRITE (1 << 2)
#define L_WSTAT_TCON_WRITE (1 << 3)
enum LocalTimerRegCntIndexes {
L_REG_CNT_TCNTB,
L_REG_CNT_TCNTO,
L_REG_CNT_ICNTB,
L_REG_CNT_ICNTO,
L_REG_CNT_FRCCNTB,
L_REG_CNT_FRCCNTO,
L_REG_CNT_AMOUNT
};
#define MCT_SFR_SIZE 0x444
#define MCT_GT_CMP_NUM 4
#define MCT_GT_COUNTER_STEP 0x100000000ULL
#define MCT_LT_COUNTER_STEP 0x100000000ULL
#define MCT_LT_CNT_LOW_LIMIT 0x100
/* global timer */
typedef struct {
qemu_irq irq[MCT_GT_CMP_NUM];
struct gregs {
uint64_t cnt;
uint32_t cnt_wstat;
uint32_t tcon;
uint32_t int_cstat;
uint32_t int_enb;
uint32_t wstat;
uint64_t comp[MCT_GT_CMP_NUM];
uint32_t comp_add_incr[MCT_GT_CMP_NUM];
} reg;
uint64_t count; /* Value FRC was armed with */
int32_t curr_comp; /* Current comparator FRC is running to */
ptimer_state *ptimer_frc; /* FRC timer */
} Exynos4210MCTGT;
/* local timer */
typedef struct {
int id; /* timer id */
qemu_irq irq; /* local timer irq */
struct tick_timer {
uint32_t cnt_run; /* cnt timer is running */
uint32_t int_run; /* int timer is running */
uint32_t last_icnto;
uint32_t last_tcnto;
uint32_t tcntb; /* initial value for TCNTB */
uint32_t icntb; /* initial value for ICNTB */
/* for step mode */
uint64_t distance; /* distance to count to the next event */
uint64_t progress; /* progress when counting by steps */
uint64_t count; /* count to arm timer with */
ptimer_state *ptimer_tick; /* timer for tick counter */
} tick_timer;
/* use ptimer.c to represent count down timer */
ptimer_state *ptimer_frc; /* timer for free running counter */
/* registers */
struct lregs {
uint32_t cnt[L_REG_CNT_AMOUNT];
uint32_t tcon;
uint32_t int_cstat;
uint32_t int_enb;
uint32_t wstat;
} reg;
} Exynos4210MCTLT;
#define TYPE_EXYNOS4210_MCT "exynos4210.mct"
#define EXYNOS4210_MCT(obj) \
OBJECT_CHECK(Exynos4210MCTState, (obj), TYPE_EXYNOS4210_MCT)
typedef struct Exynos4210MCTState {
SysBusDevice parent_obj;
MemoryRegion iomem;
/* Registers */
uint32_t reg_mct_cfg;
Exynos4210MCTLT l_timer[2];
Exynos4210MCTGT g_timer;
uint32_t freq; /* all timers tick frequency, TCLK */
} Exynos4210MCTState;
/*** VMState ***/
static const VMStateDescription vmstate_tick_timer = {
.name = "exynos4210.mct.tick_timer",
.version_id = 1,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_UINT32(cnt_run, struct tick_timer),
VMSTATE_UINT32(int_run, struct tick_timer),
VMSTATE_UINT32(last_icnto, struct tick_timer),
VMSTATE_UINT32(last_tcnto, struct tick_timer),
VMSTATE_UINT32(tcntb, struct tick_timer),
VMSTATE_UINT32(icntb, struct tick_timer),
VMSTATE_UINT64(distance, struct tick_timer),
VMSTATE_UINT64(progress, struct tick_timer),
VMSTATE_UINT64(count, struct tick_timer),
VMSTATE_PTIMER(ptimer_tick, struct tick_timer),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_lregs = {
.name = "exynos4210.mct.lregs",
.version_id = 1,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_UINT32_ARRAY(cnt, struct lregs, L_REG_CNT_AMOUNT),
VMSTATE_UINT32(tcon, struct lregs),
VMSTATE_UINT32(int_cstat, struct lregs),
VMSTATE_UINT32(int_enb, struct lregs),
VMSTATE_UINT32(wstat, struct lregs),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_exynos4210_mct_lt = {
.name = "exynos4210.mct.lt",
.version_id = 1,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_INT32(id, Exynos4210MCTLT),
VMSTATE_STRUCT(tick_timer, Exynos4210MCTLT, 0,
vmstate_tick_timer,
struct tick_timer),
VMSTATE_PTIMER(ptimer_frc, Exynos4210MCTLT),
VMSTATE_STRUCT(reg, Exynos4210MCTLT, 0,
vmstate_lregs,
struct lregs),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_gregs = {
.name = "exynos4210.mct.lregs",
.version_id = 1,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_UINT64(cnt, struct gregs),
VMSTATE_UINT32(cnt_wstat, struct gregs),
VMSTATE_UINT32(tcon, struct gregs),
VMSTATE_UINT32(int_cstat, struct gregs),
VMSTATE_UINT32(int_enb, struct gregs),
VMSTATE_UINT32(wstat, struct gregs),
VMSTATE_UINT64_ARRAY(comp, struct gregs, MCT_GT_CMP_NUM),
VMSTATE_UINT32_ARRAY(comp_add_incr, struct gregs,
MCT_GT_CMP_NUM),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_exynos4210_mct_gt = {
.name = "exynos4210.mct.lt",
.version_id = 1,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_STRUCT(reg, Exynos4210MCTGT, 0, vmstate_gregs,
struct gregs),
VMSTATE_UINT64(count, Exynos4210MCTGT),
VMSTATE_INT32(curr_comp, Exynos4210MCTGT),
VMSTATE_PTIMER(ptimer_frc, Exynos4210MCTGT),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_exynos4210_mct_state = {
.name = "exynos4210.mct",
.version_id = 1,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_UINT32(reg_mct_cfg, Exynos4210MCTState),
VMSTATE_STRUCT_ARRAY(l_timer, Exynos4210MCTState, 2, 0,
vmstate_exynos4210_mct_lt, Exynos4210MCTLT),
VMSTATE_STRUCT(g_timer, Exynos4210MCTState, 0,
vmstate_exynos4210_mct_gt, Exynos4210MCTGT),
VMSTATE_UINT32(freq, Exynos4210MCTState),
VMSTATE_END_OF_LIST()
}
};
static void exynos4210_mct_update_freq(Exynos4210MCTState *s);
/*
* Set counter of FRC global timer.
* Must be called within exynos4210_gfrc_tx_begin/commit block.
*/
static void exynos4210_gfrc_set_count(Exynos4210MCTGT *s, uint64_t count)
{
s->count = count;
DPRINTF("global timer frc set count 0x%llx\n", count);
ptimer_set_count(s->ptimer_frc, count);
}
/*
* Get counter of FRC global timer.
*/
static uint64_t exynos4210_gfrc_get_count(Exynos4210MCTGT *s)
{
uint64_t count = 0;
count = ptimer_get_count(s->ptimer_frc);
count = s->count - count;
return s->reg.cnt + count;
}
/*
* Stop global FRC timer
* Must be called within exynos4210_gfrc_tx_begin/commit block.
*/
static void exynos4210_gfrc_stop(Exynos4210MCTGT *s)
{
DPRINTF("global timer frc stop\n");
ptimer_stop(s->ptimer_frc);
}
/*
* Start global FRC timer
* Must be called within exynos4210_gfrc_tx_begin/commit block.
*/
static void exynos4210_gfrc_start(Exynos4210MCTGT *s)
{
DPRINTF("global timer frc start\n");
ptimer_run(s->ptimer_frc, 1);
}
/*
* Start ptimer transaction for global FRC timer; this is just for
* consistency with the way we wrap operations like stop and run.
*/
static void exynos4210_gfrc_tx_begin(Exynos4210MCTGT *s)
{
ptimer_transaction_begin(s->ptimer_frc);
}
/* Commit ptimer transaction for global FRC timer. */
static void exynos4210_gfrc_tx_commit(Exynos4210MCTGT *s)
{
ptimer_transaction_commit(s->ptimer_frc);
}
/*
* Find next nearest Comparator. If current Comparator value equals to other
* Comparator value, skip them both
*/
static int32_t exynos4210_gcomp_find(Exynos4210MCTState *s)
{
int res;
int i;
int enabled;
uint64_t min;
int min_comp_i;
uint64_t gfrc;
uint64_t distance;
uint64_t distance_min;
int comp_i;
/* get gfrc count */
gfrc = exynos4210_gfrc_get_count(&s->g_timer);
min = UINT64_MAX;
distance_min = UINT64_MAX;
comp_i = MCT_GT_CMP_NUM;
min_comp_i = MCT_GT_CMP_NUM;
enabled = 0;
/* lookup for nearest comparator */
for (i = 0; i < MCT_GT_CMP_NUM; i++) {
if (s->g_timer.reg.tcon & G_TCON_COMP_ENABLE(i)) {
enabled = 1;
if (s->g_timer.reg.comp[i] > gfrc) {
/* Comparator is upper then FRC */
distance = s->g_timer.reg.comp[i] - gfrc;
if (distance <= distance_min) {
distance_min = distance;
comp_i = i;
}
} else {
/* Comparator is below FRC, find the smallest */
if (s->g_timer.reg.comp[i] <= min) {
min = s->g_timer.reg.comp[i];
min_comp_i = i;
}
}
}
}
if (!enabled) {
/* All Comparators disabled */
res = -1;
} else if (comp_i < MCT_GT_CMP_NUM) {
/* Found upper Comparator */
res = comp_i;
} else {
/* All Comparators are below or equal to FRC */
res = min_comp_i;
}
DPRINTF("found comparator %d: comp 0x%llx distance 0x%llx, gfrc 0x%llx\n",
res,
s->g_timer.reg.comp[res],
distance_min,
gfrc);
return res;
}
/*
* Get distance to nearest Comparator
*/
static uint64_t exynos4210_gcomp_get_distance(Exynos4210MCTState *s, int32_t id)
{
if (id == -1) {
/* no enabled Comparators, choose max distance */
return MCT_GT_COUNTER_STEP;
}
if (s->g_timer.reg.comp[id] - s->g_timer.reg.cnt < MCT_GT_COUNTER_STEP) {
return s->g_timer.reg.comp[id] - s->g_timer.reg.cnt;
} else {
return MCT_GT_COUNTER_STEP;
}
}
/*
* Restart global FRC timer
* Must be called within exynos4210_gfrc_tx_begin/commit block.
*/
static void exynos4210_gfrc_restart(Exynos4210MCTState *s)
{
uint64_t distance;
exynos4210_gfrc_stop(&s->g_timer);
s->g_timer.curr_comp = exynos4210_gcomp_find(s);
distance = exynos4210_gcomp_get_distance(s, s->g_timer.curr_comp);
if (distance > MCT_GT_COUNTER_STEP || !distance) {
distance = MCT_GT_COUNTER_STEP;
}
exynos4210_gfrc_set_count(&s->g_timer, distance);
exynos4210_gfrc_start(&s->g_timer);
}
/*
* Raise global timer CMP IRQ
*/
static void exynos4210_gcomp_raise_irq(void *opaque, uint32_t id)
{
Exynos4210MCTGT *s = opaque;
/* If CSTAT is pending and IRQ is enabled */
if ((s->reg.int_cstat & G_INT_CSTAT_COMP(id)) &&
(s->reg.int_enb & G_INT_ENABLE(id))) {
DPRINTF("gcmp timer[%d] IRQ\n", id);
qemu_irq_raise(s->irq[id]);
}
}
/*
* Lower global timer CMP IRQ
*/
static void exynos4210_gcomp_lower_irq(void *opaque, uint32_t id)
{
Exynos4210MCTGT *s = opaque;
qemu_irq_lower(s->irq[id]);
}
/*
* Global timer FRC event handler.
* Each event occurs when internal counter reaches counter + MCT_GT_COUNTER_STEP
* Every time we arm global FRC timer to count for MCT_GT_COUNTER_STEP value
*/
static void exynos4210_gfrc_event(void *opaque)
{
Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
int i;
uint64_t distance;
DPRINTF("\n");
s->g_timer.reg.cnt += s->g_timer.count;
/* Process all comparators */
for (i = 0; i < MCT_GT_CMP_NUM; i++) {
if (s->g_timer.reg.cnt == s->g_timer.reg.comp[i]) {
/* reached nearest comparator */
s->g_timer.reg.int_cstat |= G_INT_CSTAT_COMP(i);
/* Auto increment */
if (s->g_timer.reg.tcon & G_TCON_AUTO_ICREMENT(i)) {
s->g_timer.reg.comp[i] += s->g_timer.reg.comp_add_incr[i];
}
/* IRQ */
exynos4210_gcomp_raise_irq(&s->g_timer, i);
}
}
/* Reload FRC to reach nearest comparator */
s->g_timer.curr_comp = exynos4210_gcomp_find(s);
distance = exynos4210_gcomp_get_distance(s, s->g_timer.curr_comp);
if (distance > MCT_GT_COUNTER_STEP || !distance) {
distance = MCT_GT_COUNTER_STEP;
}
exynos4210_gfrc_set_count(&s->g_timer, distance);
exynos4210_gfrc_start(&s->g_timer);
}
/*
* Get counter of FRC local timer.
*/
static uint64_t exynos4210_lfrc_get_count(Exynos4210MCTLT *s)
{
return ptimer_get_count(s->ptimer_frc);
}
/*
* Set counter of FRC local timer.
* Must be called from within exynos4210_lfrc_tx_begin/commit block.
*/
static void exynos4210_lfrc_update_count(Exynos4210MCTLT *s)
{
if (!s->reg.cnt[L_REG_CNT_FRCCNTB]) {
ptimer_set_count(s->ptimer_frc, MCT_LT_COUNTER_STEP);
} else {
ptimer_set_count(s->ptimer_frc, s->reg.cnt[L_REG_CNT_FRCCNTB]);
}
}
/*
* Start local FRC timer
* Must be called from within exynos4210_lfrc_tx_begin/commit block.
*/
static void exynos4210_lfrc_start(Exynos4210MCTLT *s)
{
ptimer_run(s->ptimer_frc, 1);
}
/*
* Stop local FRC timer
* Must be called from within exynos4210_lfrc_tx_begin/commit block.
*/
static void exynos4210_lfrc_stop(Exynos4210MCTLT *s)
{
ptimer_stop(s->ptimer_frc);
}
/* Start ptimer transaction for local FRC timer */
static void exynos4210_lfrc_tx_begin(Exynos4210MCTLT *s)
{
ptimer_transaction_begin(s->ptimer_frc);
}
/* Commit ptimer transaction for local FRC timer */
static void exynos4210_lfrc_tx_commit(Exynos4210MCTLT *s)
{
ptimer_transaction_commit(s->ptimer_frc);
}
/*
* Local timer free running counter tick handler
*/
static void exynos4210_lfrc_event(void *opaque)
{
Exynos4210MCTLT * s = (Exynos4210MCTLT *)opaque;
/* local frc expired */
DPRINTF("\n");
s->reg.int_cstat |= L_INT_CSTAT_FRCCNT;
/* update frc counter */
exynos4210_lfrc_update_count(s);
/* raise irq */
if (s->reg.int_enb & L_INT_INTENB_FRCEIE) {
qemu_irq_raise(s->irq);
}
/* we reached here, this means that timer is enabled */
exynos4210_lfrc_start(s);
}
static uint32_t exynos4210_ltick_int_get_cnto(struct tick_timer *s);
static uint32_t exynos4210_ltick_cnt_get_cnto(struct tick_timer *s);
static void exynos4210_ltick_recalc_count(struct tick_timer *s);
/*
* Action on enabling local tick int timer
*/
static void exynos4210_ltick_int_start(struct tick_timer *s)
{
if (!s->int_run) {
s->int_run = 1;
}
}
/*
* Action on disabling local tick int timer
*/
static void exynos4210_ltick_int_stop(struct tick_timer *s)
{
if (s->int_run) {
s->last_icnto = exynos4210_ltick_int_get_cnto(s);
s->int_run = 0;
}
}
/*
* Get count for INT timer
*/
static uint32_t exynos4210_ltick_int_get_cnto(struct tick_timer *s)
{
uint32_t icnto;
uint64_t remain;
uint64_t count;
uint64_t counted;
uint64_t cur_progress;
count = ptimer_get_count(s->ptimer_tick);
if (count) {
/* timer is still counting, called not from event */
counted = s->count - ptimer_get_count(s->ptimer_tick);
cur_progress = s->progress + counted;
} else {
/* timer expired earlier */
cur_progress = s->progress;
}
remain = s->distance - cur_progress;
if (!s->int_run) {
/* INT is stopped. */
icnto = s->last_icnto;
} else {
/* Both are counting */
icnto = remain / s->tcntb;
}
return icnto;
}
/*
* Start local tick cnt timer.
* Must be called within exynos4210_ltick_tx_begin/commit block.
*/
static void exynos4210_ltick_cnt_start(struct tick_timer *s)
{
if (!s->cnt_run) {
exynos4210_ltick_recalc_count(s);
ptimer_set_count(s->ptimer_tick, s->count);
ptimer_run(s->ptimer_tick, 1);
s->cnt_run = 1;
}
}
/*
* Stop local tick cnt timer.
* Must be called within exynos4210_ltick_tx_begin/commit block.
*/
static void exynos4210_ltick_cnt_stop(struct tick_timer *s)
{
if (s->cnt_run) {
s->last_tcnto = exynos4210_ltick_cnt_get_cnto(s);
if (s->int_run) {
exynos4210_ltick_int_stop(s);
}
ptimer_stop(s->ptimer_tick);
s->cnt_run = 0;
}
}
/* Start ptimer transaction for local tick timer */
static void exynos4210_ltick_tx_begin(struct tick_timer *s)
{
ptimer_transaction_begin(s->ptimer_tick);
}
/* Commit ptimer transaction for local tick timer */
static void exynos4210_ltick_tx_commit(struct tick_timer *s)
{
ptimer_transaction_commit(s->ptimer_tick);
}
/*
* Get counter for CNT timer
*/
static uint32_t exynos4210_ltick_cnt_get_cnto(struct tick_timer *s)
{
uint32_t tcnto;
uint32_t icnto;
uint64_t remain;
uint64_t counted;
uint64_t count;
uint64_t cur_progress;
count = ptimer_get_count(s->ptimer_tick);
if (count) {
/* timer is still counting, called not from event */
counted = s->count - ptimer_get_count(s->ptimer_tick);
cur_progress = s->progress + counted;
} else {
/* timer expired earlier */
cur_progress = s->progress;
}
remain = s->distance - cur_progress;
if (!s->cnt_run) {
/* Both are stopped. */
tcnto = s->last_tcnto;
} else if (!s->int_run) {
/* INT counter is stopped, progress is by CNT timer */
tcnto = remain % s->tcntb;
} else {
/* Both are counting */
icnto = remain / s->tcntb;
if (icnto) {
tcnto = remain % (icnto * s->tcntb);
} else {
tcnto = remain % s->tcntb;
}
}
return tcnto;
}
/*
* Set new values of counters for CNT and INT timers
* Must be called within exynos4210_ltick_tx_begin/commit block.
*/
static void exynos4210_ltick_set_cntb(struct tick_timer *s, uint32_t new_cnt,
uint32_t new_int)
{
uint32_t cnt_stopped = 0;
uint32_t int_stopped = 0;
if (s->cnt_run) {
exynos4210_ltick_cnt_stop(s);
cnt_stopped = 1;
}
if (s->int_run) {
exynos4210_ltick_int_stop(s);
int_stopped = 1;
}
s->tcntb = new_cnt + 1;
s->icntb = new_int + 1;
if (cnt_stopped) {
exynos4210_ltick_cnt_start(s);
}
if (int_stopped) {
exynos4210_ltick_int_start(s);
}
}
/*
* Calculate new counter value for tick timer
*/
static void exynos4210_ltick_recalc_count(struct tick_timer *s)
{
uint64_t to_count;
if ((s->cnt_run && s->last_tcnto) || (s->int_run && s->last_icnto)) {
/*
* one or both timers run and not counted to the end;
* distance is not passed, recalculate with last_tcnto * last_icnto
*/
if (s->last_tcnto) {
to_count = (uint64_t)s->last_tcnto * s->last_icnto;
} else {
to_count = s->last_icnto;
}
} else {
/* distance is passed, recalculate with tcnto * icnto */
if (s->icntb) {
s->distance = (uint64_t)s->tcntb * s->icntb;
} else {
s->distance = s->tcntb;
}
to_count = s->distance;
s->progress = 0;
}
if (to_count > MCT_LT_COUNTER_STEP) {
/* count by step */
s->count = MCT_LT_COUNTER_STEP;
} else {
s->count = to_count;
}
}
/*
* Initialize tick_timer
*/
static void exynos4210_ltick_timer_init(struct tick_timer *s)
{
exynos4210_ltick_int_stop(s);
exynos4210_ltick_tx_begin(s);
exynos4210_ltick_cnt_stop(s);
exynos4210_ltick_tx_commit(s);
s->count = 0;
s->distance = 0;
s->progress = 0;
s->icntb = 0;
s->tcntb = 0;
}
/*
* tick_timer event.
* Raises when abstract tick_timer expires.
*/
static void exynos4210_ltick_timer_event(struct tick_timer *s)
{
s->progress += s->count;
}
/*
* Local timer tick counter handler.
* Don't use reloaded timers. If timer counter = zero
* then handler called but after handler finished no
* timer reload occurs.
*/
static void exynos4210_ltick_event(void *opaque)
{
Exynos4210MCTLT * s = (Exynos4210MCTLT *)opaque;
uint32_t tcnto;
uint32_t icnto;
#ifdef DEBUG_MCT
static uint64_t time1[2] = {0};
static uint64_t time2[2] = {0};
#endif
/* Call tick_timer event handler, it will update its tcntb and icntb. */
exynos4210_ltick_timer_event(&s->tick_timer);
/* get tick_timer cnt */
tcnto = exynos4210_ltick_cnt_get_cnto(&s->tick_timer);
/* get tick_timer int */
icnto = exynos4210_ltick_int_get_cnto(&s->tick_timer);
/* raise IRQ if needed */
if (!icnto && s->reg.tcon & L_TCON_INT_START) {
/* INT counter enabled and expired */
s->reg.int_cstat |= L_INT_CSTAT_INTCNT;
/* raise interrupt if enabled */
if (s->reg.int_enb & L_INT_INTENB_ICNTEIE) {
#ifdef DEBUG_MCT
time2[s->id] = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
DPRINTF("local timer[%d] IRQ: %llx\n", s->id,
time2[s->id] - time1[s->id]);
time1[s->id] = time2[s->id];
#endif
qemu_irq_raise(s->irq);
}
/* reload ICNTB */
if (s->reg.tcon & L_TCON_INTERVAL_MODE) {
exynos4210_ltick_set_cntb(&s->tick_timer,
s->reg.cnt[L_REG_CNT_TCNTB],
s->reg.cnt[L_REG_CNT_ICNTB]);
}
} else {
/* reload TCNTB */
if (!tcnto) {
exynos4210_ltick_set_cntb(&s->tick_timer,
s->reg.cnt[L_REG_CNT_TCNTB],
icnto);
}
}
/* start tick_timer cnt */
exynos4210_ltick_cnt_start(&s->tick_timer);
/* start tick_timer int */
exynos4210_ltick_int_start(&s->tick_timer);
}
static void tx_ptimer_set_freq(ptimer_state *s, uint32_t freq)
{
/*
* callers of exynos4210_mct_update_freq() never do anything
* else that needs to be in the same ptimer transaction, so
* to avoid a lot of repetition we have a convenience function
* for begin/set_freq/commit.
*/
ptimer_transaction_begin(s);
ptimer_set_freq(s, freq);
ptimer_transaction_commit(s);
}
/* update timer frequency */
static void exynos4210_mct_update_freq(Exynos4210MCTState *s)
{
uint32_t freq = s->freq;