summaryrefslogtreecommitdiff
path: root/mali_kbase/debug/mali_kbase_debug_ktrace_defs.h
blob: 8d9e11eb5200f2cece0dd0eda6dbd2a48416be6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
/*
 *
 * (C) COPYRIGHT 2020-2022 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 */

#ifndef _KBASE_DEBUG_KTRACE_DEFS_H_
#define _KBASE_DEBUG_KTRACE_DEFS_H_

/* Enable SW tracing when set */
#if defined(CONFIG_MALI_MIDGARD_ENABLE_TRACE) || defined(CONFIG_MALI_SYSTEM_TRACE)
#define KBASE_KTRACE_ENABLE 1
#endif

#ifndef KBASE_KTRACE_ENABLE
#ifdef CONFIG_MALI_DEBUG
#define KBASE_KTRACE_ENABLE 1
#else /* CONFIG_MALI_DEBUG */
#define KBASE_KTRACE_ENABLE 0
#endif /* CONFIG_MALI_DEBUG */
#endif /* KBASE_KTRACE_ENABLE */

/* Select targets for recording of trace:
 *
 */
#if KBASE_KTRACE_ENABLE

#ifdef CONFIG_MALI_SYSTEM_TRACE
#define KBASE_KTRACE_TARGET_FTRACE 1
#else /* CONFIG_MALI_SYSTEM_TRACE */
#define KBASE_KTRACE_TARGET_FTRACE 0
#endif /* CONFIG_MALI_SYSTEM_TRACE */

#ifdef CONFIG_MALI_MIDGARD_ENABLE_TRACE
#define KBASE_KTRACE_TARGET_RBUF 1
#else /* CONFIG_MALI_MIDGARD_ENABLE_TRACE*/
#define KBASE_KTRACE_TARGET_RBUF 0
#endif /* CONFIG_MALI_MIDGARD_ENABLE_TRACE */

#else /* KBASE_KTRACE_ENABLE */
#define KBASE_KTRACE_TARGET_FTRACE 0
#define KBASE_KTRACE_TARGET_RBUF 0
#endif /* KBASE_KTRACE_ENABLE */

/*
 * Note: Some backends define flags in this type even if the RBUF target is
 * disabled (they get discarded with CSTD_UNUSED(), but they're still
 * referenced)
 */
typedef u8 kbase_ktrace_flag_t;

#if KBASE_KTRACE_TARGET_RBUF
typedef u8 kbase_ktrace_code_t;

/*
 * NOTE: KBASE_KTRACE_VERSION_MAJOR, KBASE_KTRACE_VERSION_MINOR are kept in
 * the backend, since updates can be made to one backend in a way that doesn't
 * affect the other.
 *
 * However, modifying the common part could require both backend versions to be
 * updated.
 */

/*
 * union kbase_ktrace_backend - backend specific part of a trace message.
 * At the very least, this must contain a kbase_ktrace_code_t 'code' member
 * and a kbase_ktrace_flag_t 'flags' inside a "gpu" sub-struct. Should a
 * backend need several sub structs in its union to optimize the data storage
 * for different message types, then it can use a "common initial sequence" to
 * allow 'flags' and 'code' to pack optimally without corrupting them.
 * Different backends need not share common initial sequences between them, they
 * only need to ensure they have gpu.flags and gpu.code members, it
 * is up to the backend then how to order these.
 */
union kbase_ktrace_backend;

#endif /* KBASE_KTRACE_TARGET_RBUF */

#if MALI_USE_CSF
#include "debug/backend/mali_kbase_debug_ktrace_defs_csf.h"
#else
#include "debug/backend/mali_kbase_debug_ktrace_defs_jm.h"
#endif

#if KBASE_KTRACE_TARGET_RBUF
/* Indicates if the trace message has backend related info.
 *
 * If not set, consider the &kbase_ktrace_backend part of a &kbase_ktrace_msg
 * as uninitialized, apart from the mandatory parts:
 * - code
 * - flags
 */
#define KBASE_KTRACE_FLAG_BACKEND     (((kbase_ktrace_flag_t)1) << 7)

/* Collect all the common flags together for debug checking */
#define KBASE_KTRACE_FLAG_COMMON_ALL \
		(KBASE_KTRACE_FLAG_BACKEND)

#define KBASE_KTRACE_FLAG_ALL \
		(KBASE_KTRACE_FLAG_COMMON_ALL | KBASE_KTRACE_FLAG_BACKEND_ALL)

#define KBASE_KTRACE_SHIFT (9) /* 512 entries */
#define KBASE_KTRACE_SIZE (1 << KBASE_KTRACE_SHIFT)
#define KBASE_KTRACE_MASK ((1 << KBASE_KTRACE_SHIFT)-1)

#define KBASE_KTRACE_CODE(X) KBASE_KTRACE_CODE_ ## X

/* Note: compiletime_assert() about this against kbase_ktrace_code_t is in
 * kbase_ktrace_init()
 */
enum kbase_ktrace_code {
	/*
	 * IMPORTANT: USE OF SPECIAL #INCLUDE OF NON-STANDARD HEADER FILE
	 * THIS MUST BE USED AT THE START OF THE ENUM
	 */
#define KBASE_KTRACE_CODE_MAKE_CODE(X) KBASE_KTRACE_CODE(X)
#include <debug/mali_kbase_debug_ktrace_codes.h>
#undef  KBASE_KTRACE_CODE_MAKE_CODE
	/* Comma on its own, to extend the list */
	,
	/* Must be the last in the enum */
	KBASE_KTRACE_CODE_COUNT
};

/**
 * struct kbase_ktrace_msg - object representing a trace message added to trace
 *                           buffer trace_rbuf in &kbase_device
 * @timestamp: CPU timestamp at which the trace message was added.
 * @thread_id: id of the thread in the context of which trace message was
 *             added.
 * @cpu:       indicates which CPU the @thread_id was scheduled on when the
 *             trace message was added.
 * @kctx_tgid: Thread group ID of the &kbase_context associated with the
 *             message, or 0 if none associated.
 * @kctx_id:   Unique identifier of the &kbase_context associated with the
 *             message. Only valid if @kctx_tgid != 0.
 * @info_val:  value specific to the type of event being traced. Refer to the
 *             specific code in enum kbase_ktrace_code.
 * @backend:   backend-specific trace information. All backends must implement
 *             a minimum common set of members.
 */
struct kbase_ktrace_msg {
	struct timespec64 timestamp;
	u32 thread_id;
	u32 cpu;
	pid_t kctx_tgid;
	u32 kctx_id;
	u64 info_val;
	union kbase_ktrace_backend backend;
};

struct kbase_ktrace {
	spinlock_t              lock;
	u16                     first_out;
	u16                     next_in;
	struct kbase_ktrace_msg *rbuf;
};


static inline void kbase_ktrace_compiletime_asserts(void)
{
	/* See also documentation of enum kbase_ktrace_code */
	compiletime_assert(sizeof(kbase_ktrace_code_t) == sizeof(unsigned long long) ||
			KBASE_KTRACE_CODE_COUNT <= (1ull << (sizeof(kbase_ktrace_code_t) * BITS_PER_BYTE)),
			"kbase_ktrace_code_t not wide enough for KBASE_KTRACE_CODE_COUNT");
	compiletime_assert((KBASE_KTRACE_FLAG_BACKEND_ALL & KBASE_KTRACE_FLAG_COMMON_ALL) == 0,
			"KTrace backend flags intersect with KTrace common flags");

}

#endif /* KBASE_KTRACE_TARGET_RBUF */
#endif /* _KBASE_DEBUG_KTRACE_DEFS_H_ */