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