bakery_lock.h 2.56 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
8
9
 */

#ifndef __BAKERY_LOCK_H__
#define __BAKERY_LOCK_H__

10
#include <platform_def.h>
11
12
13

#define BAKERY_LOCK_MAX_CPUS		PLATFORM_CORE_COUNT

14
#ifndef __ASSEMBLY__
15
#include <cdefs.h>
16
17
#include <stdint.h>

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*****************************************************************************
 * Internal helper macros used by the bakery lock implementation.
 ****************************************************************************/
/* Convert a ticket to priority */
#define PRIORITY(t, pos)	(((t) << 8) | (pos))

#define CHOOSING_TICKET		0x1
#define CHOSEN_TICKET		0x0

#define bakery_is_choosing(info)	(info & 0x1)
#define bakery_ticket_number(info)	((info >> 1) & 0x7FFF)
#define make_bakery_data(choosing, number) \
		(((choosing & 0x1) | (number << 1)) & 0xFFFF)

/*****************************************************************************
 * External bakery lock interface.
 ****************************************************************************/
35
#if USE_COHERENT_MEM
36
37
38
39
40
/*
 * Bakery locks are stored in coherent memory
 *
 * Each lock's data is contiguous and fully allocated by the compiler
 */
41

42
typedef struct bakery_lock {
43
44
45
46
47
48
49
	/*
	 * The lock_data is a bit-field of 2 members:
	 * Bit[0]       : choosing. This field is set when the CPU is
	 *                choosing its bakery number.
	 * Bits[1 - 15] : number. This is the bakery number allocated.
	 */
	volatile uint16_t lock_data[BAKERY_LOCK_MAX_CPUS];
50
} bakery_lock_t;
51

52
#else
53
54
55
56
57
58
59
60
/*
 * Bakery locks are stored in normal .bss memory
 *
 * Each lock's data is spread across multiple cache lines, one per CPU,
 * but multiple locks can share the same cache line.
 * The compiler will allocate enough memory for one CPU's bakery locks,
 * the remaining cache lines are allocated by the linker script
 */
61
62
63
64
65
66
67
68
69
70
71

typedef struct bakery_info {
	/*
	 * The lock_data is a bit-field of 2 members:
	 * Bit[0]       : choosing. This field is set when the CPU is
	 *                choosing its bakery number.
	 * Bits[1 - 15] : number. This is the bakery number allocated.
	 */
	volatile uint16_t lock_data;
} bakery_info_t;

72
typedef bakery_info_t bakery_lock_t;
73
74

#endif /* __USE_COHERENT_MEM__ */
75

76
static inline void bakery_lock_init(bakery_lock_t *bakery) {}
77
78
79
void bakery_lock_get(bakery_lock_t *bakery);
void bakery_lock_release(bakery_lock_t *bakery);

80
#define DEFINE_BAKERY_LOCK(_name) bakery_lock_t _name __section("bakery_lock")
81
82
83
84

#define DECLARE_BAKERY_LOCK(_name) extern bakery_lock_t _name


85
#endif /* __ASSEMBLY__ */
86
#endif /* __BAKERY_LOCK_H__ */