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