bakery_lock.h 2.82 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
 */

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
7
8
#ifndef BAKERY_LOCK_H
#define BAKERY_LOCK_H
9

10
#include <platform_def.h>
11
12
13

#define BAKERY_LOCK_MAX_CPUS		PLATFORM_CORE_COUNT

14
#ifndef __ASSEMBLY__
15
#include <cdefs.h>
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
16
#include <stdbool.h>
17
#include <stdint.h>
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
18
#include <utils_def.h>
19

20
/*****************************************************************************
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
21
 * Internal helpers used by the bakery lock implementation.
22
 ****************************************************************************/
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
23

24
/* Convert a ticket to priority */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
static inline unsigned int bakery_get_priority(unsigned int t, unsigned int pos)
{
	return (t << 8) | pos;
}

#define CHOOSING_TICKET		U(0x1)
#define CHOSEN_TICKET		U(0x0)

static inline bool bakery_is_choosing(unsigned int info)
{
	return (info & 1U) == CHOOSING_TICKET;
}

static inline unsigned int bakery_ticket_number(unsigned int info)
{
	return (info >> 1) & 0x7FFFU;
}
42

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
43
44
45
static inline uint16_t make_bakery_data(unsigned int choosing, unsigned int num)
{
	unsigned int val = (choosing & 0x1U) | (num << 1);
46

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
47
48
	return (uint16_t) val;
}
49
50
51
52

/*****************************************************************************
 * External bakery lock interface.
 ****************************************************************************/
53
#if USE_COHERENT_MEM
54
55
56
57
58
/*
 * Bakery locks are stored in coherent memory
 *
 * Each lock's data is contiguous and fully allocated by the compiler
 */
59

60
typedef struct bakery_lock {
61
62
63
64
65
66
67
	/*
	 * 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];
68
} bakery_lock_t;
69

70
#else
71
72
73
74
75
76
77
78
/*
 * 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
 */
79
80
81
82
83
84
85
86
87
88
89

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;

90
typedef bakery_info_t bakery_lock_t;
91
92

#endif /* __USE_COHERENT_MEM__ */
93

94
static inline void bakery_lock_init(bakery_lock_t *bakery) {}
95
96
97
void bakery_lock_get(bakery_lock_t *bakery);
void bakery_lock_release(bakery_lock_t *bakery);

98
#define DEFINE_BAKERY_LOCK(_name) bakery_lock_t _name __section("bakery_lock")
99
100
101
102

#define DECLARE_BAKERY_LOCK(_name) extern bakery_lock_t _name


103
#endif /* __ASSEMBLY__ */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
104
#endif /* BAKERY_LOCK_H */