summaryrefslogtreecommitdiffstats
path: root/drivers/misc/eventlib/tracebuf.h
blob: daf4d6487f08e358d1f8970871b885ac6b0c5369 (plain) (blame)
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
/*
 * Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef TRACEBUF_H
#define TRACEBUF_H

#include <linux/stddef.h>
#include <stdbool.h>

struct tracectx {
	volatile struct tracebuf *shared;
	uintptr_t                 begin;
	uintptr_t                 end;
	uint32_t                  length;
	uint32_t                  maxsize;
};

struct tracebuf {
	uint64_t version;
	uint64_t position;
	uint64_t seqid;
	uint32_t length;
	uint32_t maxsize;
} __packed;

struct tracehdr {
	uint64_t params;
	uint64_t seqid;
	uint32_t length;
	uint32_t reserved;
} __packed;

struct pullstate {
	uint64_t wrapcnt;
	uint64_t current;
	bool     wrapped;
	bool     copos;
};

/*
 * Description for pull_init()
 *   - Initialize a new pullstate structure used to mark the location
 *     from which subsequent reads will occur.
 *   - Subsequent reads will advance the read position within the
 *     pullstate structure.
 * Parameters
 *   - Param `ctx` is provided by the caller.
 *   - Param `state` is filled by the callee.
 * Return values
 *   - Operation never fails.
 */

void pull_init(struct tracectx *ctx, struct pullstate *state);

/*
 * Description for tracebuf_init()
 *   - Initialize or re-initialize memory to prepare for binding.
 *   - Memory address and length must be aligned to MIN_WORD_SIZE and
 *     of "sufficient" size, where "sufficient" is currently some
 *     implementation-defined value not to be smaller than 4KB.
 * Parameters
 *   - Param `ctx` is filled by the callee on success.
 *   - Param `buffer` and `length` are provided by the caller.
 * Return values
 *   - Returns 0 on success.
 */

int tracebuf_init(struct tracectx *ctx, void *buffer, uint32_t length);

/*
 * Description for tracebuf_bind()
 *   - Binds to the specified region of memory. The specified region
 *     of memory must have been previously initialized with the exact
 *     same parameters for memory address and length.
 *   - Memory address and length must be aligned to MIN_WORD_SIZE and
 *     of "sufficient" size, where "sufficient" is currently some
 *     implementation-defined value not to be smaller than 4KB.
 * Parameters
 *   - Param `ctx` is filled by the callee on success.
 *   - Param `buffer` and `length` are provided by the caller.
 * Return values
 *   - Returns 0 on success.
 */

int tracebuf_bind(struct tracectx *ctx, void *buffer, uint32_t length);

/*
 * Description for tracebuf_push()
 *   - Add a message to the buffer; this operation never fails.
 *   - If there is insufficient message space available, the oldest
 *     message(s) will be overwritten.
 *   - Payloads may get truncated if its size is larger than
 *     tracebuf.maxsize, or padded up to at least MIN_WORD_SIZE.
 * Parameters
 *   - Param `ctx` is provided by the caller.
 *   - Param `hdr.params` is provided by the caller.
 *   - Param `hdr.seqid` and `hdr.length` is filled by the callee.
 *   - Param `payload` and `paylen` are provided by the caller.
 * Return values
 *   - Operation never fails.
 */

void tracebuf_push(struct tracectx *ctx, struct tracehdr *hdr,
	void *payload, uint32_t paylen);

/*
 * Description for tracebuf_pull()
 *   - Attempt to get a message from the buffer; may fail for many
 *     different reasons. See possible return values below.
 *   - Read position is determined by state specified in the pullstate
 *     structure; this state is implicitly updated after each request.
 *   - Value in *paylen specifies bytes available on input, and
 *     is updated to bytes written after a successful request.
 *   - If input space is insufficient, the payload will be truncated.
 * Parameters
 *   - Param `ctx` is provided by the caller.
 *   - Param `state` is provided by the caller.
 *   - Param `hdr` is filled by the callee on success.
 *   - Param `payload` is provided by the caller.
 *   - Param `paylen` is provided by the caller and updated by the callee.
 * Return values
 *   - Returns -EAGAIN when the current read request should be retried.
 *   - Returns -EINTR when the overall read sequence needs to be restarted.
 *   - Returns -ENOBUFS when there is no more data available.
 *   - Returns 0 on success.
 */

int tracebuf_pull(struct tracectx *ctx, struct pullstate *state,
	struct tracehdr *hdr, void *payload, uint32_t *paylen);

#ifdef ENABLE_DEBUG_HOOK
void debug_callback(struct tracectx *ctx, char *prefix);
#endif

#endif