blob: 4c768faf408554be9333f357aa3cd4dc534c3a73 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2019 MediaTek Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/sched/clock.h>
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/sched.h>
19#include <asm/div64.h>
20#include <linux/types.h>
21#include <linux/kallsyms.h>
22#include <linux/ktime.h>
23
24#define MET_USER_EVENT_SUPPORT
25#include "met_drv.h"
26#include "trace.h"
27#include "interface.h"
28
29#define MODE_CUSOM_CLKSRC 2
30struct metdevice met_wall_time;
31
32/** */
33/* How to add a new clocksource: */
34/* */
35/* 1. add constant for new clocksource in #define-macro */
36/* 2. declare new weakref function */
37/* 3. implement handler functions: */
38/* (1) clksrc_attr_t::*ready: */
39/* check if ... */
40/* (i) clocksource correctly working */
41/* (ii) weakref function is not null */
42/* (2) clksrc_attr_t::*get_cnt: read clocksource from weakref function */
43/* 4. place attrs of new clocksource into clksrc_attr_tb */
44/* 5. update DEFAULT_CLKSRC_STR */
45/* 6. update help message */
46/** */
47
48#define __SYS_TIMER 0x0
49#define __GPT1 0x1
50#define __GPT2 0x2
51#define __GPT3 0x3
52#define __GPT4 0x4
53#define __GPT5 0x5
54#define __GPT6 0x6
55
56#define DEFAULT_CLKSRC_STR "SYS_TIMER"
57
58extern u64 met_arch_counter_get_cntvct(void);
59extern ktime_t ktime_get(void);
60u64 (*met_arch_counter_get_cntvct_symbol)(void);
61
62int __sys_timer_get_cnt(u8 clksrc, u64 *cycles)
63{
64 if (met_arch_counter_get_cntvct_symbol)
65 *cycles = met_arch_counter_get_cntvct_symbol();
66 return 0;
67}
68
69
70struct clksrc_attr_t {
71 u8 clksrc;
72 const char *clksrc_str;
73 /* checks if clksrc/get_cnt function is working/available */
74 int (*clksrc_ready)(u8 clksrc);
75 int (*clksrc_get_cnt)(u8 clksrc, u64 *cycles);
76};
77
78struct clksrc_attr_t clksrc_attr_tb[] = {
79 {__SYS_TIMER, "SYS_TIMER", NULL, __sys_timer_get_cnt},
80 /* {__GPT1, "GPT1", __gpt_timer_ready, __gpt_timer_get_cnt}, */
81 /* {__GPT2, "GPT2", __gpt_timer_ready, __gpt_timer_get_cnt}, */
82 /* {__GPT3, "GPT3", __gpt_timer_ready, __gpt_timer_get_cnt}, */
83 /* {__GPT4, "GPT4", __gpt_timer_ready, __gpt_timer_get_cnt}, */
84 /* {__GPT5, "GPT5", __gpt_timer_ready, __gpt_timer_get_cnt}, */
85 /* {__GPT6, "GPT6", __gpt_timer_ready, __gpt_timer_get_cnt}, */
86};
87
88static const struct clksrc_attr_t *lookup_clksrc_attr_tb(const char *clksrc_str, int len);
89static const struct clksrc_attr_t *wall_time_attr;
90
91/* definitions for auto-sampling of working freq. of clocksource */
92/* maximum tolerable error percentage(%) of sampled clock freq */
93#define FREQ_ERR_PERCENT 3
94
95/* expected working freq. of clocksources */
96static const u32 freq_level[] = { 32768, 13000000, 26000000 };
97
98static u32 lookup_freq_level(u32 freq);
99
100/* flag indicating whether sampling is on-going */
101static u32 do_sample;
102static u32 freq;
103static u64 start_us_ts;
104static u64 start_wall_time;
105/* end definitions for sampling of freq. */
106
107static void wall_time_start(void)
108{
109 met_arch_counter_get_cntvct_symbol = (void *)symbol_get(met_arch_counter_get_cntvct);
110
111 if (met_wall_time.mode != MODE_CUSOM_CLKSRC) {
112 wall_time_attr = lookup_clksrc_attr_tb(DEFAULT_CLKSRC_STR,
113 strlen(DEFAULT_CLKSRC_STR));
114 }
115
116 freq = 0;
117 do_sample = 1;
118
119 if (wall_time_attr) {
120
121 /* XXX: always use CPU 0 */
122 start_us_ts = cpu_clock(0);
123 wall_time_attr->clksrc_get_cnt(wall_time_attr->clksrc, &start_wall_time);
124
125 /* us_ts = ap_ts/1000; */
126 do_div(start_us_ts, 1000);
127 }
128}
129
130noinline void met_mono_time(void)
131{
132 /* mono time vs local time */
133 u64 cur_local_ns = sched_clock();
134 ktime_t cur_mono_ts = ktime_get();
135
136 MET_TRACE("TS.APTS=%llu TS.MONO=%llu\n", (unsigned long long)cur_local_ns,
137 (unsigned long long)ktime_to_ns(cur_mono_ts));
138}
139
140noinline void met_ap_wall_time(unsigned long long ts, int cpu)
141{
142 u64 ap_ts;
143 u64 us_ts;
144 u64 sec;
145 u64 usec;
146 u64 cycles;
147 u64 f;
148
149 if (wall_time_attr) {
150
151 wall_time_attr->clksrc_get_cnt(wall_time_attr->clksrc, &cycles);
152 ap_ts = cpu_clock(cpu);
153
154 us_ts = ap_ts;
155 do_div(us_ts, 1000); /* us_ts = ap_ts/1000; */
156
157 sec = us_ts;
158 usec = do_div(sec, 1000000); /* sec = us_ts/1000000; usec = us_ts%1000000; */
159 MET_TRACE("TS.APTS=%llu.%06llu WCLK=%llu\n", (unsigned long long)sec,
160 (unsigned long long)usec, (unsigned long long)cycles);
161
162 // print local time vs mono time for gpu time shift
163 met_mono_time();
164
165 if (do_sample) {
166
167 do_sample = 0;
168
169 f = (cycles - start_wall_time) * 1000000;
170 do_div(f, us_ts - start_us_ts);
171
172 /* don't worry about the u64 -> u32 assignment, */
173 /* sampled wall-clock freq is expected to be below 2^32-1 */
174 freq = lookup_freq_level(f);
175
176 /* debug message */
177 /* MET_TRACE("wall_time debug: result: %u," */
178 /* "start cycle: %llu, end cycle: %llu, cycle diff: %llu," */
179 /* "start us: %llu, end us: %llu, us diff: %llu", */
180 /* f, */
181 /* start_wall_time, cycles, cycles - start_wall_time, */
182 /* start_us_ts, us_ts, us_ts - start_us_ts); */
183
184 if (freq != 0)
185 met_tag_oneshot_real(33880, "_WCLK_FREQ_", freq);
186 }
187 }
188}
189
190static const char help[] =
191" --wall_time output wall-clock syncing info in system timer\n";
192/* " --wall_time=SYS_TIMER|GPT[1-6] output wall-clock syncing info in custom clocksource\n"; */
193
194static int wall_time_print_help(char *buf, int len)
195{
196 return snprintf(buf, PAGE_SIZE, help);
197}
198
199static const char *header =
200"met-info [000] 0.0: WCLK: %d\n"
201"met-info [000] 0.0: clocksource: %s\n";
202
203static int wall_time_print_header(char *buf, int len)
204{
205 return snprintf(buf, len, header,
206 freq == 0 ? -1 : freq,
207 wall_time_attr ? wall_time_attr->clksrc_str : "NONE");
208}
209
210static int wall_time_process_argument(const char *arg, int len)
211{
212 /* reset wall-time clocksource */
213 wall_time_attr = lookup_clksrc_attr_tb(arg, len);
214
215 if (!wall_time_attr) {
216 met_wall_time.mode = 0;
217 return -1;
218 }
219
220 met_wall_time.mode = MODE_CUSOM_CLKSRC;
221 return 0;
222}
223
224static const struct clksrc_attr_t *lookup_clksrc_attr_tb(const char *clksrc_str, int len)
225{
226 int i;
227 const struct clksrc_attr_t *attr;
228 int tb_nmemb = sizeof(clksrc_attr_tb) / sizeof(*clksrc_attr_tb);
229
230 for (i = 0; i < tb_nmemb; i++) {
231
232 attr = clksrc_attr_tb + i;
233
234 if (strlen(attr->clksrc_str) == len &&
235 strncmp(clksrc_str, attr->clksrc_str, len) == 0) {
236 return attr;
237 }
238 }
239
240 return NULL;
241}
242
243static u32 lookup_freq_level(u32 freq)
244{
245
246 int ii;
247 int freq_nmemb = sizeof(freq_level) / sizeof(*freq_level);
248 u32 fdiff;
249
250 for (ii = 0; ii < freq_nmemb; ii++) {
251 fdiff = freq_level[ii] > freq ? freq_level[ii] - freq : freq - freq_level[ii];
252 if (fdiff < freq_level[ii] * FREQ_ERR_PERCENT / 100)
253 return freq_level[ii];
254 }
255
256 return 0;
257}
258
259struct metdevice met_wall_time = {
260 .name = "wall_time",
261 .owner = THIS_MODULE,
262 .type = MET_TYPE_BUS,
263 .cpu_related = 0,
264 .start = wall_time_start,
265 .polling_interval = 1000,
266 .timed_polling = met_ap_wall_time,
267 .print_help = wall_time_print_help,
268 .print_header = wall_time_print_header,
269 .process_argument = wall_time_process_argument,
270};
271EXPORT_SYMBOL(met_wall_time);