Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*	$NetBSD: atomic.h,v 1.20 2019/01/27 02:08:43 pgoyette Exp $	*/

/*-
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Taylor R. Campbell.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _LINUX_ATOMIC_H_
#define _LINUX_ATOMIC_H_

#include <sys/atomic.h>

#include <machine/limits.h>

#if defined(MULTIPROCESSOR) && !defined(__HAVE_ATOMIC_AS_MEMBAR)
#  define	smp_mb__before_atomic()		membar_exit()
#  define	smp_mb__after_atomic()		membar_enter()
#else
#  define	smp_mb__before_atomic()		__insn_barrier()
#  define	smp_mb__after_atomic()		__insn_barrier()
#endif

/*
 * atomic (u)int operations
 *
 *	Atomics that return a value, other than atomic_read, imply a
 *	full memory_sync barrier.  Those that do not return a value
 *	imply no memory barrier.
 */

struct atomic {
	union {
		volatile int au_int;
		volatile unsigned int au_uint;
	} a_u;
};

#define	ATOMIC_INIT(i)	{ .a_u = { .au_int = (i) } }

typedef struct atomic atomic_t;

static inline int
atomic_read(atomic_t *atomic)
{
	/* no membar */
	return atomic->a_u.au_int;
}

static inline void
atomic_set(atomic_t *atomic, int value)
{
	/* no membar */
	atomic->a_u.au_int = value;
}

static inline void
atomic_add(int addend, atomic_t *atomic)
{
	/* no membar */
	atomic_add_int(&atomic->a_u.au_uint, addend);
}

static inline void
atomic_sub(int subtrahend, atomic_t *atomic)
{
	/* no membar */
	atomic_add_int(&atomic->a_u.au_uint, -subtrahend);
}

static inline int
atomic_add_return(int addend, atomic_t *atomic)
{
	int v;

	smp_mb__before_atomic();
	v = (int)atomic_add_int_nv(&atomic->a_u.au_uint, addend);
	smp_mb__after_atomic();

	return v;
}

static inline void
atomic_inc(atomic_t *atomic)
{
	/* no membar */
	atomic_inc_uint(&atomic->a_u.au_uint);
}

static inline void
atomic_dec(atomic_t *atomic)
{
	/* no membar */
	atomic_dec_uint(&atomic->a_u.au_uint);
}

static inline int
atomic_inc_return(atomic_t *atomic)
{
	int v;

	smp_mb__before_atomic();
	v = (int)atomic_inc_uint_nv(&atomic->a_u.au_uint);
	smp_mb__after_atomic();

	return v;
}

static inline int
atomic_dec_return(atomic_t *atomic)
{
	int v;

	smp_mb__before_atomic();
	v = (int)atomic_dec_uint_nv(&atomic->a_u.au_uint);
	smp_mb__after_atomic();

	return v;
}

static inline int
atomic_dec_and_test(atomic_t *atomic)
{
	/* membar implied by atomic_dec_return */
	return atomic_dec_return(atomic) == 0;
}

static inline void
atomic_or(int value, atomic_t *atomic)
{
	/* no membar */
	atomic_or_uint(&atomic->a_u.au_uint, value);
}

static inline void
atomic_set_mask(unsigned long mask, atomic_t *atomic)
{
	/* no membar */
	atomic_or_uint(&atomic->a_u.au_uint, mask);
}

static inline void
atomic_clear_mask(unsigned long mask, atomic_t *atomic)
{
	/* no membar */
	atomic_and_uint(&atomic->a_u.au_uint, ~mask);
}

static inline int
atomic_add_unless(atomic_t *atomic, int addend, int zero)
{
	int value;

	smp_mb__before_atomic();
	do {
		value = atomic->a_u.au_int;
		if (value == zero)
			break;
	} while (atomic_cas_uint(&atomic->a_u.au_uint, value, (value + addend))
	    != value);
	smp_mb__after_atomic();

	return value != zero;
}

static inline int
atomic_inc_not_zero(atomic_t *atomic)
{
	/* membar implied by atomic_add_unless */
	return atomic_add_unless(atomic, 1, 0);
}

static inline int
atomic_xchg(atomic_t *atomic, int new)
{
	int old;

	smp_mb__before_atomic();
	old = (int)atomic_swap_uint(&atomic->a_u.au_uint, (unsigned)new);
	smp_mb__after_atomic();

	return old;
}

static inline int
atomic_cmpxchg(atomic_t *atomic, int expect, int new)
{
	int old;

	/*
	 * XXX As an optimization, under Linux's semantics we are
	 * allowed to skip the memory barrier if the comparison fails,
	 * but taking advantage of that is not convenient here.
	 */
	smp_mb__before_atomic();
	old = (int)atomic_cas_uint(&atomic->a_u.au_uint, (unsigned)expect,
	    (unsigned)new);
	smp_mb__after_atomic();

	return old;
}

struct atomic64 {
	volatile uint64_t	a_v;
};

typedef struct atomic64 atomic64_t;

#define	ATOMIC64_INIT(v)	{ .a_v = (v) }

int		linux_atomic64_init(void);
void		linux_atomic64_fini(void);

#ifdef __HAVE_ATOMIC64_OPS

static inline uint64_t
atomic64_read(const struct atomic64 *a)
{
	/* no membar */
	return a->a_v;
}

static inline void
atomic64_set(struct atomic64 *a, uint64_t v)
{
	/* no membar */
	a->a_v = v;
}

static inline void
atomic64_add(int64_t d, struct atomic64 *a)
{
	/* no membar */
	atomic_add_64(&a->a_v, d);
}

static inline void
atomic64_sub(int64_t d, struct atomic64 *a)
{
	/* no membar */
	atomic_add_64(&a->a_v, -d);
}

static inline int64_t
atomic64_add_return(int64_t d, struct atomic64 *a)
{
	int64_t v;

	smp_mb__before_atomic();
	v = (int64_t)atomic_add_64_nv(&a->a_v, d);
	smp_mb__after_atomic();

	return v;
}

static inline uint64_t
atomic64_xchg(struct atomic64 *a, uint64_t new)
{
	uint64_t old;

	smp_mb__before_atomic();
	old = atomic_swap_64(&a->a_v, new);
	smp_mb__after_atomic();

	return old;
}

static inline uint64_t
atomic64_cmpxchg(struct atomic64 *atomic, uint64_t expect, uint64_t new)
{
	uint64_t old;

	/*
	 * XXX As an optimization, under Linux's semantics we are
	 * allowed to skip the memory barrier if the comparison fails,
	 * but taking advantage of that is not convenient here.
	 */
	smp_mb__before_atomic();
	old = atomic_cas_64(&atomic->a_v, expect, new);
	smp_mb__after_atomic();

	return old;
}

#else  /* !defined(__HAVE_ATOMIC64_OPS) */

#define	atomic64_add		linux_atomic64_add
#define	atomic64_add_return	linux_atomic64_add_return
#define	atomic64_cmpxchg	linux_atomic64_cmpxchg
#define	atomic64_read		linux_atomic64_read
#define	atomic64_set		linux_atomic64_set
#define	atomic64_sub		linux_atomic64_sub
#define	atomic64_xchg		linux_atomic64_xchg

uint64_t	atomic64_read(const struct atomic64 *);
void		atomic64_set(struct atomic64 *, uint64_t);
void		atomic64_add(int64_t, struct atomic64 *);
void		atomic64_sub(int64_t, struct atomic64 *);
int64_t		atomic64_add_return(int64_t, struct atomic64 *);
uint64_t	atomic64_xchg(struct atomic64 *, uint64_t);
uint64_t	atomic64_cmpxchg(struct atomic64 *, uint64_t, uint64_t);

#endif

static inline int64_t
atomic64_inc_return(struct atomic64 *a)
{
	return atomic64_add_return(1, a);
}

struct atomic_long {
	volatile unsigned long	al_v;
};

typedef struct atomic_long atomic_long_t;

static inline long
atomic_long_read(struct atomic_long *a)
{
	/* no membar */
	return (unsigned long)a->al_v;
}

static inline void
atomic_long_set(struct atomic_long *a, long v)
{
	/* no membar */
	a->al_v = v;
}

static inline long
atomic_long_add_unless(struct atomic_long *a, long addend, long zero)
{
	long value;

	smp_mb__before_atomic();
	do {
		value = (long)a->al_v;
		if (value == zero)
			break;
	} while (atomic_cas_ulong(&a->al_v, (unsigned long)value,
		(unsigned long)(value + addend)) != (unsigned long)value);
	smp_mb__after_atomic();

	return value != zero;
}

static inline long
atomic_long_inc_not_zero(struct atomic_long *a)
{
	/* membar implied by atomic_long_add_unless */
	return atomic_long_add_unless(a, 1, 0);
}

static inline long
atomic_long_cmpxchg(struct atomic_long *a, long expect, long new)
{
	long old;

	/*
	 * XXX As an optimization, under Linux's semantics we are
	 * allowed to skip the memory barrier if the comparison fails,
	 * but taking advantage of that is not convenient here.
	 */
	smp_mb__before_atomic();
	old = (long)atomic_cas_ulong(&a->al_v, (unsigned long)expect,
	    (unsigned long)new);
	smp_mb__after_atomic();

	return old;
}

static inline void
set_bit(unsigned int bit, volatile unsigned long *ptr)
{
	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);

	/* no memory barrier */
	atomic_or_ulong(&ptr[bit / units], (1UL << (bit % units)));
}

static inline void
clear_bit(unsigned int bit, volatile unsigned long *ptr)
{
	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);

	/* no memory barrier */
	atomic_and_ulong(&ptr[bit / units], ~(1UL << (bit % units)));
}

static inline void
change_bit(unsigned int bit, volatile unsigned long *ptr)
{
	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
	volatile unsigned long *const p = &ptr[bit / units];
	const unsigned long mask = (1UL << (bit % units));
	unsigned long v;

	/* no memory barrier */
	do v = *p; while (atomic_cas_ulong(p, v, (v ^ mask)) != v);
}

static inline int
test_and_set_bit(unsigned int bit, volatile unsigned long *ptr)
{
	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
	volatile unsigned long *const p = &ptr[bit / units];
	const unsigned long mask = (1UL << (bit % units));
	unsigned long v;

	smp_mb__before_atomic();
	do v = *p; while (atomic_cas_ulong(p, v, (v | mask)) != v);
	smp_mb__after_atomic();

	return ((v & mask) != 0);
}

static inline int
test_and_clear_bit(unsigned int bit, volatile unsigned long *ptr)
{
	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
	volatile unsigned long *const p = &ptr[bit / units];
	const unsigned long mask = (1UL << (bit % units));
	unsigned long v;

	smp_mb__before_atomic();
	do v = *p; while (atomic_cas_ulong(p, v, (v & ~mask)) != v);
	smp_mb__after_atomic();

	return ((v & mask) != 0);
}

static inline int
test_and_change_bit(unsigned int bit, volatile unsigned long *ptr)
{
	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
	volatile unsigned long *const p = &ptr[bit / units];
	const unsigned long mask = (1UL << (bit % units));
	unsigned long v;

	smp_mb__before_atomic();
	do v = *p; while (atomic_cas_ulong(p, v, (v ^ mask)) != v);
	smp_mb__after_atomic();

	return ((v & mask) != 0);
}

#endif  /* _LINUX_ATOMIC_H_ */