xref: /rk3399_rockchip-uboot/include/trace.h (revision 9a325614469f82bec4d50a8f9dd0b6288c16be73)
1b2e16a85SSimon Glass /*
2b2e16a85SSimon Glass  * Copyright (c) 2012 The Chromium OS Authors.
3b2e16a85SSimon Glass  *
4*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
5b2e16a85SSimon Glass  */
6b2e16a85SSimon Glass 
7b2e16a85SSimon Glass #ifndef __TRACE_H
8b2e16a85SSimon Glass #define __TRACE_H
9b2e16a85SSimon Glass 
10b2e16a85SSimon Glass enum {
11b2e16a85SSimon Glass 	/*
12b2e16a85SSimon Glass 	 * This affects the granularity of our trace. We can bin function
13b2e16a85SSimon Glass 	 * entry points into groups on the basis that functions typically
14b2e16a85SSimon Glass 	 * have a minimum size, so entry points can't appear any closer
15b2e16a85SSimon Glass 	 * than this to each other.
16b2e16a85SSimon Glass 	 *
17b2e16a85SSimon Glass 	 * The value here assumes a minimum instruction size of 4 bytes,
18b2e16a85SSimon Glass 	 * or that instructions are 2 bytes but there are at least 2 of
19b2e16a85SSimon Glass 	 * them in every function.
20b2e16a85SSimon Glass 	 *
21b2e16a85SSimon Glass 	 * Increasing this value reduces the number of functions we can
22b2e16a85SSimon Glass 	 * resolve, but reduces the size of the uintptr_t array used for
23b2e16a85SSimon Glass 	 * our function list, which is the length of the code divided by
24b2e16a85SSimon Glass 	 * this value.
25b2e16a85SSimon Glass 	 */
26b2e16a85SSimon Glass 	FUNC_SITE_SIZE	= 4,	/* distance between function sites */
27b2e16a85SSimon Glass };
28b2e16a85SSimon Glass 
29b2e16a85SSimon Glass enum trace_chunk_type {
30b2e16a85SSimon Glass 	TRACE_CHUNK_FUNCS,
31b2e16a85SSimon Glass 	TRACE_CHUNK_CALLS,
32b2e16a85SSimon Glass };
33b2e16a85SSimon Glass 
34b2e16a85SSimon Glass /* A trace record for a function, as written to the profile output file */
35b2e16a85SSimon Glass struct trace_output_func {
36b2e16a85SSimon Glass 	uint32_t offset;		/* Function offset into code */
37b2e16a85SSimon Glass 	uint32_t call_count;		/* Number of times called */
38b2e16a85SSimon Glass };
39b2e16a85SSimon Glass 
40b2e16a85SSimon Glass /* A header at the start of the trace output buffer */
41b2e16a85SSimon Glass struct trace_output_hdr {
42b2e16a85SSimon Glass 	enum trace_chunk_type type;	/* Record type */
43b2e16a85SSimon Glass 	uint32_t rec_count;		/* Number of records */
44b2e16a85SSimon Glass };
45b2e16a85SSimon Glass 
46b2e16a85SSimon Glass /* Print statistics about traced function calls */
47b2e16a85SSimon Glass void trace_print_stats(void);
48b2e16a85SSimon Glass 
49b2e16a85SSimon Glass /**
50b2e16a85SSimon Glass  * Dump a list of functions and call counts into a buffer
51b2e16a85SSimon Glass  *
52b2e16a85SSimon Glass  * Each record in the buffer is a struct trace_func_stats. The 'needed'
53b2e16a85SSimon Glass  * parameter returns the number of bytes needed to complete the operation,
54b2e16a85SSimon Glass  * which may be more than buff_size if your buffer is too small.
55b2e16a85SSimon Glass  *
56b2e16a85SSimon Glass  * @param buff		Buffer in which to place data, or NULL to count size
57b2e16a85SSimon Glass  * @param buff_size	Size of buffer
58b2e16a85SSimon Glass  * @param needed	Returns number of bytes used / needed
59b2e16a85SSimon Glass  * @return 0 if ok, -1 on error (buffer exhausted)
60b2e16a85SSimon Glass  */
61b2e16a85SSimon Glass int trace_list_functions(void *buff, int buff_size, unsigned *needed);
62b2e16a85SSimon Glass 
63b2e16a85SSimon Glass /* Flags for ftrace_record */
64b2e16a85SSimon Glass enum ftrace_flags {
65b2e16a85SSimon Glass 	FUNCF_EXIT		= 0UL << 30,
66b2e16a85SSimon Glass 	FUNCF_ENTRY		= 1UL << 30,
67b2e16a85SSimon Glass 	FUNCF_TEXTBASE		= 2UL << 30,
68b2e16a85SSimon Glass 
69b2e16a85SSimon Glass 	FUNCF_TIMESTAMP_MASK	= 0x3fffffff,
70b2e16a85SSimon Glass };
71b2e16a85SSimon Glass 
72b2e16a85SSimon Glass #define TRACE_CALL_TYPE(call)	((call)->flags & 0xc0000000UL)
73b2e16a85SSimon Glass 
74b2e16a85SSimon Glass /* Information about a single function entry/exit */
75b2e16a85SSimon Glass struct trace_call {
76b2e16a85SSimon Glass 	uint32_t func;		/* Function offset */
77b2e16a85SSimon Glass 	uint32_t caller;	/* Caller function offset */
78b2e16a85SSimon Glass 	uint32_t flags;		/* Flags and timestamp */
79b2e16a85SSimon Glass };
80b2e16a85SSimon Glass 
81b2e16a85SSimon Glass int trace_list_calls(void *buff, int buff_size, unsigned int *needed);
82b2e16a85SSimon Glass 
83b2e16a85SSimon Glass /**
84b2e16a85SSimon Glass  * Turn function tracing on and off
85b2e16a85SSimon Glass  *
86b2e16a85SSimon Glass  * Don't enable trace if it has not been initialised.
87b2e16a85SSimon Glass  *
88b2e16a85SSimon Glass  * @param enabled	1 to enable trace, 0 to disable
89b2e16a85SSimon Glass  */
90b2e16a85SSimon Glass void trace_set_enabled(int enabled);
91b2e16a85SSimon Glass 
92b2e16a85SSimon Glass int trace_early_init(void);
93b2e16a85SSimon Glass 
94b2e16a85SSimon Glass /**
95b2e16a85SSimon Glass  * Init the trace system
96b2e16a85SSimon Glass  *
97b2e16a85SSimon Glass  * This should be called after relocation with a suitably large buffer
98b2e16a85SSimon Glass  * (typically as large as the U-Boot text area)
99b2e16a85SSimon Glass  *
100b2e16a85SSimon Glass  * @param buff		Pointer to trace buffer
101b2e16a85SSimon Glass  * @param buff_size	Size of trace buffer
102b2e16a85SSimon Glass  */
103b2e16a85SSimon Glass int trace_init(void *buff, size_t buff_size);
104b2e16a85SSimon Glass 
105b2e16a85SSimon Glass #endif
106