xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/debug/mali_kbase_debug_ktrace_defs.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 #ifndef _KBASE_DEBUG_KTRACE_DEFS_H_
23 #define _KBASE_DEBUG_KTRACE_DEFS_H_
24 
25 /* Enable SW tracing when set */
26 #if defined(CONFIG_MALI_BIFROST_ENABLE_TRACE) || defined(CONFIG_MALI_BIFROST_SYSTEM_TRACE)
27 #define KBASE_KTRACE_ENABLE 1
28 #endif
29 
30 #ifndef KBASE_KTRACE_ENABLE
31 #ifdef CONFIG_MALI_BIFROST_DEBUG
32 #define KBASE_KTRACE_ENABLE 1
33 #else /* CONFIG_MALI_BIFROST_DEBUG */
34 #define KBASE_KTRACE_ENABLE 0
35 #endif /* CONFIG_MALI_BIFROST_DEBUG */
36 #endif /* KBASE_KTRACE_ENABLE */
37 
38 /* Select targets for recording of trace:
39  *
40  */
41 #if KBASE_KTRACE_ENABLE
42 
43 #ifdef CONFIG_MALI_BIFROST_SYSTEM_TRACE
44 #define KBASE_KTRACE_TARGET_FTRACE 1
45 #else /* CONFIG_MALI_BIFROST_SYSTEM_TRACE */
46 #define KBASE_KTRACE_TARGET_FTRACE 0
47 #endif /* CONFIG_MALI_BIFROST_SYSTEM_TRACE */
48 
49 #ifdef CONFIG_MALI_BIFROST_ENABLE_TRACE
50 #define KBASE_KTRACE_TARGET_RBUF 1
51 #else /* CONFIG_MALI_BIFROST_ENABLE_TRACE*/
52 #define KBASE_KTRACE_TARGET_RBUF 0
53 #endif /* CONFIG_MALI_BIFROST_ENABLE_TRACE */
54 
55 #else /* KBASE_KTRACE_ENABLE */
56 #define KBASE_KTRACE_TARGET_FTRACE 0
57 #define KBASE_KTRACE_TARGET_RBUF 0
58 #endif /* KBASE_KTRACE_ENABLE */
59 
60 /*
61  * Note: Some backends define flags in this type even if the RBUF target is
62  * disabled (they get discarded with CSTD_UNUSED(), but they're still
63  * referenced)
64  */
65 typedef u8 kbase_ktrace_flag_t;
66 
67 #if KBASE_KTRACE_TARGET_RBUF
68 typedef u8 kbase_ktrace_code_t;
69 
70 /*
71  * NOTE: KBASE_KTRACE_VERSION_MAJOR, KBASE_KTRACE_VERSION_MINOR are kept in
72  * the backend, since updates can be made to one backend in a way that doesn't
73  * affect the other.
74  *
75  * However, modifying the common part could require both backend versions to be
76  * updated.
77  */
78 
79 /*
80  * union kbase_ktrace_backend - backend specific part of a trace message.
81  * At the very least, this must contain a kbase_ktrace_code_t 'code' member
82  * and a kbase_ktrace_flag_t 'flags' inside a "gpu" sub-struct. Should a
83  * backend need several sub structs in its union to optimize the data storage
84  * for different message types, then it can use a "common initial sequence" to
85  * allow 'flags' and 'code' to pack optimally without corrupting them.
86  * Different backends need not share common initial sequences between them, they
87  * only need to ensure they have gpu.flags and gpu.code members, it
88  * is up to the backend then how to order these.
89  */
90 union kbase_ktrace_backend;
91 
92 #endif /* KBASE_KTRACE_TARGET_RBUF */
93 
94 #if MALI_USE_CSF
95 #include "debug/backend/mali_kbase_debug_ktrace_defs_csf.h"
96 #else
97 #include "debug/backend/mali_kbase_debug_ktrace_defs_jm.h"
98 #endif
99 
100 #if KBASE_KTRACE_TARGET_RBUF
101 /* Indicates if the trace message has backend related info.
102  *
103  * If not set, consider the &kbase_ktrace_backend part of a &kbase_ktrace_msg
104  * as uninitialized, apart from the mandatory parts:
105  * - code
106  * - flags
107  */
108 #define KBASE_KTRACE_FLAG_BACKEND     (((kbase_ktrace_flag_t)1) << 7)
109 
110 /* Collect all the common flags together for debug checking */
111 #define KBASE_KTRACE_FLAG_COMMON_ALL \
112 		(KBASE_KTRACE_FLAG_BACKEND)
113 
114 #define KBASE_KTRACE_FLAG_ALL \
115 		(KBASE_KTRACE_FLAG_COMMON_ALL | KBASE_KTRACE_FLAG_BACKEND_ALL)
116 
117 #define KBASE_KTRACE_SHIFT (9) /* 512 entries */
118 #define KBASE_KTRACE_SIZE (1 << KBASE_KTRACE_SHIFT)
119 #define KBASE_KTRACE_MASK ((1 << KBASE_KTRACE_SHIFT)-1)
120 
121 #define KBASE_KTRACE_CODE(X) KBASE_KTRACE_CODE_ ## X
122 
123 /* Note: compiletime_assert() about this against kbase_ktrace_code_t is in
124  * kbase_ktrace_init()
125  */
126 enum kbase_ktrace_code {
127 	/*
128 	 * IMPORTANT: USE OF SPECIAL #INCLUDE OF NON-STANDARD HEADER FILE
129 	 * THIS MUST BE USED AT THE START OF THE ENUM
130 	 */
131 #define KBASE_KTRACE_CODE_MAKE_CODE(X) KBASE_KTRACE_CODE(X)
132 #include <debug/mali_kbase_debug_ktrace_codes.h>
133 #undef  KBASE_KTRACE_CODE_MAKE_CODE
134 	/* Comma on its own, to extend the list */
135 	,
136 	/* Must be the last in the enum */
137 	KBASE_KTRACE_CODE_COUNT
138 };
139 
140 /**
141  * struct kbase_ktrace_msg - object representing a trace message added to trace
142  *                           buffer trace_rbuf in &kbase_device
143  * @timestamp: CPU timestamp at which the trace message was added.
144  * @thread_id: id of the thread in the context of which trace message was
145  *             added.
146  * @cpu:       indicates which CPU the @thread_id was scheduled on when the
147  *             trace message was added.
148  * @kctx_tgid: Thread group ID of the &kbase_context associated with the
149  *             message, or 0 if none associated.
150  * @kctx_id:   Unique identifier of the &kbase_context associated with the
151  *             message. Only valid if @kctx_tgid != 0.
152  * @info_val:  value specific to the type of event being traced. Refer to the
153  *             specific code in enum kbase_ktrace_code.
154  * @backend:   backend-specific trace information. All backends must implement
155  *             a minimum common set of members.
156  */
157 struct kbase_ktrace_msg {
158 	struct timespec64 timestamp;
159 	u32 thread_id;
160 	u32 cpu;
161 	pid_t kctx_tgid;
162 	u32 kctx_id;
163 	u64 info_val;
164 	union kbase_ktrace_backend backend;
165 };
166 
167 struct kbase_ktrace {
168 	spinlock_t              lock;
169 	u16                     first_out;
170 	u16                     next_in;
171 	struct kbase_ktrace_msg *rbuf;
172 };
173 
174 
kbase_ktrace_compiletime_asserts(void)175 static inline void kbase_ktrace_compiletime_asserts(void)
176 {
177 	/* See also documentation of enum kbase_ktrace_code */
178 	compiletime_assert(sizeof(kbase_ktrace_code_t) == sizeof(unsigned long long) ||
179 			KBASE_KTRACE_CODE_COUNT <= (1ull << (sizeof(kbase_ktrace_code_t) * BITS_PER_BYTE)),
180 			"kbase_ktrace_code_t not wide enough for KBASE_KTRACE_CODE_COUNT");
181 	compiletime_assert((KBASE_KTRACE_FLAG_BACKEND_ALL & KBASE_KTRACE_FLAG_COMMON_ALL) == 0,
182 			"KTrace backend flags intersect with KTrace common flags");
183 
184 }
185 
186 #endif /* KBASE_KTRACE_TARGET_RBUF */
187 #endif /* _KBASE_DEBUG_KTRACE_DEFS_H_ */
188