blob: 448aee1b60d5784364d760d0a791c644e2fb974e [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
15#ifndef MET_DRV
16#define MET_DRV
17
18#include <linux/version.h>
19#include <linux/preempt.h>
20#include <linux/device.h>
21#include <linux/percpu.h>
22#include <linux/hardirq.h>
23#include <linux/clk.h>
24
25extern int met_mode;
26extern int core_plf_init(void);
27extern void core_plf_exit(void);
28
29#define MET_MODE_TRACE_CMD_OFFSET (1)
30#define MET_MODE_TRACE_CMD (1<<MET_MODE_TRACE_CMD_OFFSET)
31
32#ifdef CONFIG_MET_MODULE
33#define my_preempt_enable() preempt_enable()
34#else
35#define my_preempt_enable() preempt_enable_no_resched()
36#endif
37
38#define MET_STRBUF_SIZE 1024
39DECLARE_PER_CPU(char[MET_STRBUF_SIZE], met_strbuf_nmi);
40DECLARE_PER_CPU(char[MET_STRBUF_SIZE], met_strbuf_irq);
41DECLARE_PER_CPU(char[MET_STRBUF_SIZE], met_strbuf_sirq);
42DECLARE_PER_CPU(char[MET_STRBUF_SIZE], met_strbuf);
43
44#ifdef CONFIG_TRACING
45#define TRACE_PUTS(p) \
46 do { \
47 trace_puts(p);; \
48 } while (0)
49#else
50#define TRACE_PUTS(p) do {} while (0)
51#endif
52
53#define GET_MET_TRACE_BUFFER_ENTER_CRITICAL() \
54 ({ \
55 char *pmet_strbuf; \
56 preempt_disable(); \
57 if (in_nmi()) \
58 pmet_strbuf = per_cpu(met_strbuf_nmi, smp_processor_id()); \
59 else if (in_irq()) \
60 pmet_strbuf = per_cpu(met_strbuf_irq, smp_processor_id()); \
61 else if (in_softirq()) \
62 pmet_strbuf = per_cpu(met_strbuf_sirq, smp_processor_id()); \
63 else \
64 pmet_strbuf = per_cpu(met_strbuf, smp_processor_id()); \
65 pmet_strbuf;\
66 })
67
68#define PUT_MET_TRACE_BUFFER_EXIT_CRITICAL(pmet_strbuf) \
69 do {\
70 if (pmet_strbuf)\
71 TRACE_PUTS(pmet_strbuf); \
72 my_preempt_enable(); \
73 } while (0)
74
75#define MET_TRACE(FORMAT, args...) \
76 do { \
77 char *pmet_strbuf; \
78 preempt_disable(); \
79 if (in_nmi()) \
80 pmet_strbuf = per_cpu(met_strbuf_nmi, smp_processor_id()); \
81 else if (in_irq()) \
82 pmet_strbuf = per_cpu(met_strbuf_irq, smp_processor_id()); \
83 else if (in_softirq()) \
84 pmet_strbuf = per_cpu(met_strbuf_sirq, smp_processor_id()); \
85 else \
86 pmet_strbuf = per_cpu(met_strbuf, smp_processor_id()); \
87 if (met_mode & MET_MODE_TRACE_CMD) \
88 snprintf(pmet_strbuf, MET_STRBUF_SIZE, "%s: " FORMAT, __func__, ##args); \
89 else \
90 snprintf(pmet_strbuf, MET_STRBUF_SIZE, FORMAT, ##args); \
91 TRACE_PUTS(pmet_strbuf); \
92 my_preempt_enable(); \
93 } while (0)
94
95/*
96 * SOB: start of buf
97 * EOB: end of buf
98 */
99#define MET_TRACE_GETBUF(pSOB, pEOB) \
100 ({ \
101 preempt_disable(); \
102 if (in_nmi()) \
103 *pSOB = per_cpu(met_strbuf_nmi, smp_processor_id()); \
104 else if (in_irq()) \
105 *pSOB = per_cpu(met_strbuf_irq, smp_processor_id()); \
106 else if (in_softirq()) \
107 *pSOB = per_cpu(met_strbuf_sirq, smp_processor_id()); \
108 else \
109 *pSOB = per_cpu(met_strbuf, smp_processor_id()); \
110 *pEOB = *pSOB; \
111 if (met_mode & MET_MODE_TRACE_CMD) \
112 *pEOB += snprintf(*pEOB, MET_STRBUF_SIZE, "%s: ", __func__); \
113 })
114
115#define MET_TRACE_PUTBUF(SOB, EOB) \
116 ({ \
117 __trace_puts(_THIS_IP_, (SOB), (uintptr_t)((EOB)-(SOB))); \
118 my_preempt_enable(); \
119 })
120
121#define MET_FTRACE_DUMP(TRACE_NAME, args...) \
122 do { \
123 trace_##TRACE_NAME(args);; \
124 } while (0)
125
126
127#define MET_TYPE_PMU 1
128#define MET_TYPE_BUS 2
129#define MET_TYPE_MISC 3
130
131struct metdevice {
132 struct list_head list;
133 int type;
134 const char *name;
135 struct module *owner;
136 struct kobject *kobj;
137
138 int (*create_subfs)(struct kobject *parent);
139 void (*delete_subfs)(void);
140 int mode;
141 int ondiemet_mode; /* new for ondiemet; 1: call ondiemet functions */
142 int cpu_related;
143 int polling_interval;
144 int polling_count_reload;
145 int __percpu *polling_count;
146 int header_read_again; /*for header size > 1 page */
147 void (*start)(void);
148 void (*stop)(void);
149 int (*reset)(void);
150 void (*timed_polling)(unsigned long long stamp, int cpu);
151 void (*tagged_polling)(unsigned long long stamp, int cpu);
152 int (*print_help)(char *buf, int len);
153 int (*print_header)(char *buf, int len);
154 int (*process_argument)(const char *arg, int len);
155
156 void (*ondiemet_start)(void);
157 void (*ondiemet_stop)(void);
158 int (*ondiemet_reset)(void);
159 int (*ondiemet_print_help)(char *buf, int len);
160 int (*ondiemet_print_header)(char *buf, int len);
161 int (*ondiemet_process_argument)(const char *arg, int len);
162 void (*ondiemet_timed_polling)(unsigned long long stamp, int cpu);
163 void (*ondiemet_tagged_polling)(unsigned long long stamp, int cpu);
164
165 struct list_head exlist; /* for linked list before register */
166 void (*suspend)(void);
167 void (*resume)(void);
168
169 unsigned long long prev_stamp;
170 spinlock_t my_lock;
171 void *reversed1;
172};
173
174int met_register(struct metdevice *met);
175int met_deregister(struct metdevice *met);
176int met_set_platform(const char *plf_name, int flag);
177int met_set_topology(const char *topology_name, int flag);
178int met_devlink_add(struct metdevice *met);
179int met_devlink_del(struct metdevice *met);
180int met_devlink_register_all(void);
181int met_devlink_deregister_all(void);
182
183int fs_reg(void);
184void fs_unreg(void);
185
186/******************************************************************************
187 * Tracepoints
188 ******************************************************************************/
189#define MET_DEFINE_PROBE(probe_name, proto) \
190 static void probe_##probe_name(void *data, PARAMS(proto))
191#define MET_REGISTER_TRACE(probe_name) \
192 register_trace_##probe_name(probe_##probe_name, NULL)
193#define MET_UNREGISTER_TRACE(probe_name) \
194 unregister_trace_##probe_name(probe_##probe_name, NULL)
195
196
197/* ====================== Tagging API ================================ */
198
199#define MAX_EVENT_CLASS 31
200#define MAX_TAGNAME_LEN 128
201#define MET_CLASS_ALL 0x80000000
202
203/* IOCTL commands of MET tagging */
204struct mtag_cmd_t {
205 unsigned int class_id;
206 unsigned int value;
207 unsigned int slen;
208 char tname[MAX_TAGNAME_LEN];
209 void *data;
210 unsigned int size;
211};
212
213#define TYPE_START 1
214#define TYPE_END 2
215#define TYPE_ONESHOT 3
216#define TYPE_ENABLE 4
217#define TYPE_DISABLE 5
218#define TYPE_REC_SET 6
219#define TYPE_DUMP 7
220#define TYPE_DUMP_SIZE 8
221#define TYPE_DUMP_SAVE 9
222#define TYPE_USRDATA 10
223#define TYPE_DUMP_AGAIN 11
224#define TYPE_ASYNC_START 12
225#define TYPE_ASYNC_END 13
226#define TYPE_MET_SUSPEND 15
227#define TYPE_MET_RESUME 16
228
229/* Use 'm' as magic number */
230#define MTAG_IOC_MAGIC 'm'
231/* Please use a different 8-bit number in your code */
232#define MTAG_CMD_START _IOW(MTAG_IOC_MAGIC, TYPE_START, struct mtag_cmd_t)
233#define MTAG_CMD_END _IOW(MTAG_IOC_MAGIC, TYPE_END, struct mtag_cmd_t)
234#define MTAG_CMD_ONESHOT _IOW(MTAG_IOC_MAGIC, TYPE_ONESHOT, struct mtag_cmd_t)
235#define MTAG_CMD_ENABLE _IOW(MTAG_IOC_MAGIC, TYPE_ENABLE, int)
236#define MTAG_CMD_DISABLE _IOW(MTAG_IOC_MAGIC, TYPE_DISABLE, int)
237#define MTAG_CMD_REC_SET _IOW(MTAG_IOC_MAGIC, TYPE_REC_SET, int)
238#define MTAG_CMD_DUMP _IOW(MTAG_IOC_MAGIC, TYPE_DUMP, struct mtag_cmd_t)
239#define MTAG_CMD_DUMP_SIZE _IOWR(MTAG_IOC_MAGIC, TYPE_DUMP_SIZE, int)
240#define MTAG_CMD_DUMP_SAVE _IOW(MTAG_IOC_MAGIC, TYPE_DUMP_SAVE, struct mtag_cmd_t)
241#define MTAG_CMD_USRDATA _IOW(MTAG_IOC_MAGIC, TYPE_USRDATA, struct mtag_cmd_t)
242#define MTAG_CMD_DUMP_AGAIN _IOW(MTAG_IOC_MAGIC, TYPE_DUMP_AGAIN, void *)
243#define MTAG_CMD_ASYNC_START _IOW(MTAG_IOC_MAGIC, TYPE_ASYNC_START, struct mtag_cmd_t)
244#define MTAG_CMD_ASYNC_END _IOW(MTAG_IOC_MAGIC, TYPE_ASYNC_END, struct mtag_cmd_t)
245
246/* include file */
247extern int met_tag_start_real(unsigned int class_id, const char *name);
248extern int met_tag_end_real(unsigned int class_id, const char *name);
249extern int met_tag_async_start_real(unsigned int class_id, const char *name, unsigned int cookie);
250extern int met_tag_async_end_real(unsigned int class_id, const char *name, unsigned int cookie);
251extern int met_tag_oneshot_real(unsigned int class_id, const char *name, unsigned int value);
252extern int met_tag_userdata_real(char *pData);
253extern int met_tag_dump_real(unsigned int class_id, const char *name, void *data, unsigned int length);
254extern int met_tag_disable_real(unsigned int class_id);
255extern int met_tag_enable_real(unsigned int class_id);
256extern int met_set_dump_buffer_real(int size);
257extern int met_save_dump_buffer_real(const char *pathname);
258extern int met_save_log_real(const char *pathname);
259extern int met_show_bw_limiter_real(void);
260extern int met_reg_bw_limiter_real(void *fp);
261extern int met_show_clk_tree_real(const char *name, unsigned int addr, unsigned int status);
262extern int met_reg_clk_tree_real(void *fp);
263extern int enable_met_backlight_tag(void);
264extern int output_met_backlight_tag(int level);
265
266#endif /* MET_DRV */