1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3 *
4 * (C) COPYRIGHT 2020-2022 ARM Limited. All rights reserved.
5 *
6 * This program is free software and is provided to you under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation, and any use by you of this program is subject to the terms
9 * of such GNU license.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
19 *
20 */
21
22 /*
23 * DOC: Kbase's own trace, 'KTrace'
24 *
25 * Low overhead trace specific to kbase, aimed at:
26 * - common use-cases for tracing kbase specific functionality to do with
27 * running work on the GPU
28 * - easy 1-line addition of new types of trace
29 *
30 * KTrace can be recorded in one or more of the following targets:
31 * - KBASE_KTRACE_TARGET_RBUF: low overhead ringbuffer protected by an
32 * irq-spinlock, output available via dev_dbg() and debugfs file
33 * - KBASE_KTRACE_TARGET_FTRACE: ftrace based tracepoints under 'mali' events
34 */
35
36 #ifndef _KBASE_DEBUG_KTRACE_H_
37 #define _KBASE_DEBUG_KTRACE_H_
38
39 #if KBASE_KTRACE_TARGET_FTRACE
40 #include "mali_linux_trace.h"
41 #endif
42
43 #if MALI_USE_CSF
44 #include "debug/backend/mali_kbase_debug_ktrace_csf.h"
45 #else
46 #include "debug/backend/mali_kbase_debug_ktrace_jm.h"
47 #endif
48
49 /**
50 * kbase_ktrace_init - initialize kbase ktrace.
51 * @kbdev: kbase device
52 * Return: 0 if successful or a negative error code on failure.
53 */
54 int kbase_ktrace_init(struct kbase_device *kbdev);
55
56 /**
57 * kbase_ktrace_term - terminate kbase ktrace.
58 * @kbdev: kbase device
59 */
60 void kbase_ktrace_term(struct kbase_device *kbdev);
61
62 /**
63 * kbase_ktrace_hook_wrapper - wrapper so that dumping ktrace can be done via a
64 * callback.
65 * @param: kbase device, cast to void pointer
66 */
67 void kbase_ktrace_hook_wrapper(void *param);
68
69 #if IS_ENABLED(CONFIG_DEBUG_FS)
70 /**
71 * kbase_ktrace_debugfs_init - initialize kbase ktrace for debugfs usage, if
72 * the selected targets support it.
73 * @kbdev: kbase device
74 *
75 * There is no matching 'term' call, debugfs_remove_recursive() is sufficient.
76 */
77 void kbase_ktrace_debugfs_init(struct kbase_device *kbdev);
78 #endif /* CONFIG_DEBUG_FS */
79
80 /*
81 * KTrace target for internal ringbuffer
82 */
83 #if KBASE_KTRACE_TARGET_RBUF
84 /**
85 * kbasep_ktrace_initialized - Check whether kbase ktrace is initialized
86 *
87 * @ktrace: ktrace of kbase device.
88 *
89 * Return: true if ktrace has been initialized.
90 */
kbasep_ktrace_initialized(struct kbase_ktrace * ktrace)91 static inline bool kbasep_ktrace_initialized(struct kbase_ktrace *ktrace)
92 {
93 return ktrace->rbuf != NULL;
94 }
95
96 /**
97 * kbasep_ktrace_add - internal function to add trace to the ringbuffer.
98 * @kbdev: kbase device
99 * @code: ktrace code
100 * @kctx: kbase context, or NULL if no context
101 * @flags: flags about the message
102 * @info_val: generic information about @code to add to the trace
103 *
104 * PRIVATE: do not use directly. Use KBASE_KTRACE_ADD() instead.
105 */
106 void kbasep_ktrace_add(struct kbase_device *kbdev, enum kbase_ktrace_code code,
107 struct kbase_context *kctx, kbase_ktrace_flag_t flags,
108 u64 info_val);
109
110 /**
111 * kbasep_ktrace_clear - clear the trace ringbuffer
112 * @kbdev: kbase device
113 *
114 * PRIVATE: do not use directly. Use KBASE_KTRACE_CLEAR() instead.
115 */
116 void kbasep_ktrace_clear(struct kbase_device *kbdev);
117
118 /**
119 * kbasep_ktrace_dump - dump ktrace ringbuffer to dev_dbg(), then clear it
120 * @kbdev: kbase device
121 *
122 * PRIVATE: do not use directly. Use KBASE_KTRACE_DUMP() instead.
123 */
124 void kbasep_ktrace_dump(struct kbase_device *kbdev);
125
126 #define KBASE_KTRACE_RBUF_ADD(kbdev, code, kctx, info_val) \
127 kbasep_ktrace_add(kbdev, KBASE_KTRACE_CODE(code), kctx, 0, \
128 info_val) \
129
130 #define KBASE_KTRACE_RBUF_CLEAR(kbdev) \
131 kbasep_ktrace_clear(kbdev)
132
133 #define KBASE_KTRACE_RBUF_DUMP(kbdev) \
134 kbasep_ktrace_dump(kbdev)
135
136 #else /* KBASE_KTRACE_TARGET_RBUF */
137
138 #define KBASE_KTRACE_RBUF_ADD(kbdev, code, kctx, info_val) \
139 do { \
140 CSTD_UNUSED(kbdev); \
141 CSTD_NOP(code); \
142 CSTD_UNUSED(kctx); \
143 CSTD_UNUSED(info_val); \
144 CSTD_NOP(0); \
145 } while (0)
146
147 #define KBASE_KTRACE_RBUF_CLEAR(kbdev) \
148 do { \
149 CSTD_UNUSED(kbdev); \
150 CSTD_NOP(0); \
151 } while (0)
152 #define KBASE_KTRACE_RBUF_DUMP(kbdev) \
153 do { \
154 CSTD_UNUSED(kbdev); \
155 CSTD_NOP(0); \
156 } while (0)
157 #endif /* KBASE_KTRACE_TARGET_RBUF */
158
159 /*
160 * KTrace target for Linux's ftrace
161 */
162 #if KBASE_KTRACE_TARGET_FTRACE
163
164 #define KBASE_KTRACE_FTRACE_ADD(kbdev, code, kctx, info_val) \
165 trace_mali_##code(kctx, info_val)
166
167 #else /* KBASE_KTRACE_TARGET_FTRACE */
168 #define KBASE_KTRACE_FTRACE_ADD(kbdev, code, kctx, info_val) \
169 do { \
170 CSTD_UNUSED(kbdev); \
171 CSTD_NOP(code); \
172 CSTD_UNUSED(kctx); \
173 CSTD_UNUSED(info_val); \
174 CSTD_NOP(0); \
175 } while (0)
176 #endif /* KBASE_KTRACE_TARGET_FTRACE */
177
178 /* No 'clear' implementation for ftrace yet */
179 #define KBASE_KTRACE_FTRACE_CLEAR(kbdev) \
180 do { \
181 CSTD_UNUSED(kbdev); \
182 CSTD_NOP(0); \
183 } while (0)
184
185 /* No 'dump' implementation for ftrace yet */
186 #define KBASE_KTRACE_FTRACE_DUMP(kbdev) \
187 do { \
188 CSTD_UNUSED(kbdev); \
189 CSTD_NOP(0); \
190 } while (0)
191
192 /*
193 * Master set of macros to route KTrace to any of the targets
194 */
195
196 /**
197 * KBASE_KTRACE_ADD - Add trace values
198 * @kbdev: kbase device
199 * @code: trace code
200 * @kctx: kbase context, or NULL if no context
201 * @info_val: generic information about @code to add to the trace
202 *
203 * Note: Any functions called through this macro will still be evaluated in
204 * Release builds (CONFIG_MALI_BIFROST_DEBUG not defined). Therefore, when
205 * KBASE_KTRACE_ENABLE == 0 any functions called to get the parameters supplied
206 * to this macro must:
207 * a) be static or static inline, and
208 * b) just return 0 and have no other statements present in the body.
209 */
210 #define KBASE_KTRACE_ADD(kbdev, code, kctx, info_val) \
211 do { \
212 /* capture values that could come from non-pure function calls */ \
213 u64 __info_val = info_val; \
214 KBASE_KTRACE_RBUF_ADD(kbdev, code, kctx, __info_val); \
215 KBASE_KTRACE_FTRACE_ADD(kbdev, code, kctx, __info_val); \
216 } while (0)
217
218 /**
219 * KBASE_KTRACE_CLEAR - Clear the trace, if applicable to the target(s)
220 * @kbdev: kbase device
221 */
222 #define KBASE_KTRACE_CLEAR(kbdev) \
223 do { \
224 KBASE_KTRACE_RBUF_CLEAR(kbdev); \
225 KBASE_KTRACE_FTRACE_CLEAR(kbdev); \
226 } while (0)
227
228 /**
229 * KBASE_KTRACE_DUMP - Dump the trace, if applicable to the target(s)
230 * @kbdev: kbase device
231 */
232 #define KBASE_KTRACE_DUMP(kbdev) \
233 do { \
234 KBASE_KTRACE_RBUF_DUMP(kbdev); \
235 KBASE_KTRACE_FTRACE_DUMP(kbdev); \
236 } while (0)
237
238 #endif /* _KBASE_DEBUG_KTRACE_H_ */
239