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