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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* 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 EVENTLIB_H
#define EVENTLIB_H
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/stddef.h>
/* Possible init flags */
#define EVENTLIB_FLAG_INIT_FILTERING (1 << 0)
/* These are used to ensure binary compatibility between library and caller
* If eventlib_ctx is ever changed in incompatible way, EVENTLIB_CTX_VERSION
* must be increased.
*/
#define EVENTLIB_CTX_VERSION 1
#define EVENTLIB_CTX_SIZE (sizeof(struct eventlib_ctx))
/* Mask size is aligned to 4-byte boundary */
#define EVENTLIB_FLT_MASK_SIZE(bit_count) ((((bit_count) + 31u) / 32u) * 4u)
enum eventlib_direction {
EVENTLIB_DIRECTION_READER = 1,
EVENTLIB_DIRECTION_WRITER = 2
};
enum eventlib_filter_domain {
EVENTLIB_FILTER_DOMAIN_EVENT_TYPE,
EVENTLIB_FILTER_DOMAIN_CUSTOM,
EVENTLIB_FILTER_DOMAIN_MAX
};
typedef uint32_t event_type_t;
typedef uint64_t event_timestamp_t;
typedef uint8_t *eventlib_bitmask_t;
/* ========================================
* Initialization and finalization
* ========================================
* Caller must allocate context structure, completely zero it, then fill
* mandatory fields, and call eventlib_init().
*/
struct eventlib_init;
struct eventlib_ctx {
/* Direction. Mandatory. */
enum eventlib_direction direction;
/* Init flags. Possible flags defined below. */
uint32_t flags;
/* Private storage space used for internal use; must not be touched.
* Below value corresponds with size of struct eventlib_init.
*/
char local_mem[0x400] __aligned(8);
/* W2R shared memory. Mandatory. */
void *w2r_shm;
uint32_t w2r_shm_size;
/* R2W shared memory.
* Mandatory if using subsystem that needs it, otherwise unneeded.
*/
void *r2w_shm;
uint32_t r2w_shm_size;
/* Private pointer must be NULL on init. */
struct eventlib_init *priv;
/* Filtering parameters */
uint16_t flt_num_bits[EVENTLIB_FILTER_DOMAIN_MAX];
/* Number of trace buffers
* Below value will be adjusted to one if set as zero.
* Reader context value is ignored
*/
uint32_t num_buffers;
};
/* Initialize communication
*
* Arguments:
* ctx - pointer to context with mandatory fields initialized by caller
*
* Possible return values:
* 0 - ok
* -EINVAL - invalid data found
* -EFAULT - binary incompatibility between library and caller detected
* -ENOMEM - not enough space in local memory
* -ENOSPC (writer) - not enough space in shared memory
* -ENOSPC (reader) - size of shared memory is too small to cover all
* data expected to be in shared memory
* -EPROTONOSUPPORT (reader) - incompatibility with writer detected
* -EIO (reader) - inconsistent state of shared memory detected
* -EBUSY (reader) - unable to reserve filtering slot
*/
int _eventlib_init(struct eventlib_ctx *ctx, uint32_t ctx_version,
uint32_t ctx_size);
#define eventlib_init(ctx) \
_eventlib_init(ctx, EVENTLIB_CTX_VERSION, EVENTLIB_CTX_SIZE)
/* De-initialize communication.
* This operation never fails.
*
* Arguments:
* ctx - library context object
*
* Context object must no longer be used after call to this routine.
*/
void eventlib_close(struct eventlib_ctx *ctx);
/* ========================================
* Read and write
* ========================================
* Add an event to the trace buffer. To be called on writer side.
* Event is added unconditionally, any filtering should be applied before
* calling this.
*
* Arguments:
* ctx - library context
* idx - trace buffer id [indexed from 0]
* type - event type, not anyhow interpreted, just passed to the reader
* ts - event timestamp, not anyhow interpreted, just passed to the reader
* data - event data
* size - size of the data.
*
* This operation never fails.
* Data may be truncated if too large. Old events may get overwritten.
*/
void eventlib_write(struct eventlib_ctx *ctx, uint32_t idx,
event_type_t type, event_timestamp_t ts, void *data, uint32_t size);
/* Try to extract many events from trace buffer. To be called at reader side.
* It is not guaranteed that any particular event will be delivered.
* Delivery order is always preserved with newest events first (LIFO).
* The other thing guaranteed is - same event won't be delivered to same
* reader more than once.
*
* Arguments:
* ctx - library context
* buffer - where to store event data, represented as list of `record`,
* with event data following each `record` with size `record.size`
* size - on entry, available buffer size, on successful return, final size
* buffer size should match `w2r_shm_size` passed to eventlib_init()
* num_lost_events - where to store number of new lost events detected
* within this call (NULL = do not store)
*
* Possible return values:
* 0 - successfully extracted events, see size output parameter
* -EPROTO - protocol usage violation (called on reader side)
* -EIO - encountered an internal data inconsistency, this should
* not happen, error is not expected to be recoverable
* -EINTR - attempt was repeatedly interrupted by a fast writer, this is
* not expected (perhaps peer is misbehaving), caller can retry later
*/
struct record {
uint32_t size;
uint32_t type;
uint64_t ts;
uint8_t data[0];
} __packed;
int eventlib_read(struct eventlib_ctx *ctx, void *buffer, uint32_t *size,
uint64_t *lost);
/* ========================================
* Filtering feature
* ========================================
* Check how many readers have slots currently reserved.
*
* Arguments:
* ctx - library context
*
* Possible return values:
* >=0 - success, returned value is the result
* -EPROTO - protocol violation (not writer side, not inited)
*
* Note that due to readers are completely asynchronous with writer, returned
* value can become obsolete an any moment, it can even be already obsolete
* at return time.
*/
int eventlib_get_num_attached_readers(struct eventlib_ctx *ctx);
/* Get current mask for the given domain.
* For reader, returns mask of this reader
* For writer, returns ORed masks from all connected readers.
*
* Arguments:
* ctx - library context
* domain - domain in question
* mask - where to store mask, buffer size should be at least
* EVENTLIB_FLT_MASK_SIZE(<bit in domain>)
*
* Possible return values:
* 0 - success, mask stored
* -EINVAL - invalid argument
* -EPROTO - filtering not inited at writer side
*
* Note that on writer side, mask is refreshed in all calls that use it,
* thus returned mask could become obsolete soon.
*/
int eventlib_get_filter_mask(struct eventlib_ctx *ctx,
enum eventlib_filter_domain domain, eventlib_bitmask_t mask);
/* Check if given bit is set in the current mask for the given domain.
* For reader, uses current mask for this reader
* For writer, uses ORed masks from all connected readers.
*
* Arguments:
* ctx - library context
* domain - domain in question
* bit - bit to check, bits count from zero
*
* Possible return values:
* 0 or 1 - success, returned bit value
* -EINVAL - invalid argument
* -EPROTO - filtering not inited at writer side
*/
int eventlib_check_filter_bit(struct eventlib_ctx *ctx,
enum eventlib_filter_domain domain, uint16_t bit);
/* Check if any of set bits in the given mask is set in the current mask for
* the given domain.
* For reader, uses current mask for this reader
* For writer, uses ORed masks from all connected readers.
*
* Arguments:
* ctx - library context
* domain - domain in question
* mask - mask with bits to check, size should be
* EVENTLIB_FLT_MASK_SIZE(<bit in domain>)
*
* Possible return values:
* 0 - success, no bit is set
* 1 - success, some bits are set
* -EINVAL - invalid argument
* -EPROTO - filtering not inited at writer side
*/
int eventlib_check_filter_mask(struct eventlib_ctx *ctx,
enum eventlib_filter_domain domain, eventlib_bitmask_t mask);
/* Update single bit in reader's current mask for the given domain
* Changed mask is immediately synced to the writer.
*
* Arguments:
* ctx - library context
* domain - domain in question
* bit - bit to write to, bits count from zero
* val - value to write into bit
*
* Possible return values:
* 0 - success,
* -EINVAL - invalid argument
* -EPROTO - called on writer side
*/
int eventlib_set_filter_bit(struct eventlib_ctx *ctx,
enum eventlib_filter_domain domain, uint16_t bit, int val);
/* Replace entire reader's mask for the given domain
* Changed mask is immediately synced to the writer.
*
* Arguments:
* ctx - library context
* domain - domain in question
* mask - new mask, size should be EVENTLIB_FLT_MASK_SIZE(<bits in domain>)
*
* Possible return values:
* 0 - success,
* -EINVAL - invalid argument
* -EPROTO - called on writer side
*/
int eventlib_set_filter_mask(struct eventlib_ctx *ctx,
enum eventlib_filter_domain domain, eventlib_bitmask_t mask);
/* ========================================
* Timestamp access for CPU
* ========================================
*/
#if defined(__aarch64__)
/* Returns the current system timer counter value.
* This operation never fails.
*/
static inline event_timestamp_t eventlib_get_timer_counter(void)
{
uint64_t value;
asm volatile("mrs %0, CNTPCT_EL0" : "=r"(value) :: );
return (event_timestamp_t)value;
}
/* Returns the system timer frequency in hertz.
* This operation never fails.
*/
static inline uint32_t eventlib_get_timer_freq(void)
{
uint64_t value;
asm volatile("mrs %0, CNTFRQ_EL0" : "=r"(value) :: );
return (uint32_t)value;
}
#elif defined(__arm__)
/* Returns the current system timer counter value.
* This operation never fails.
*/
static inline event_timestamp_t eventlib_get_timer_counter(void)
{
uint32_t value1, value2;
asm volatile("mrrc p15, 0, %0, %1, c14"
: "=r"(value1), "=r"(value2) :: );
return ((event_timestamp_t)(
(((uint64_t)value1) | ((uint64_t)value2 << 32))));
}
/* Returns the system timer frequency in hertz.
* This operation never fails.
*/
static inline uint32_t eventlib_get_timer_freq(void)
{
uint32_t value;
asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(value) :: );
return value;
}
#endif
#endif
|