1 /*
2 * Copyright (c) 2013-2018, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8 #include <string.h>
9
10 #include <arch_helpers.h>
11 #include <lib/bakery_lock.h>
12 #include <lib/el3_runtime/cpu_data.h>
13 #include <plat/common/platform.h>
14
15 /*
16 * Functions in this file implement Bakery Algorithm for mutual exclusion with the
17 * bakery lock data structures in coherent memory.
18 *
19 * ARM architecture offers a family of exclusive access instructions to
20 * efficiently implement mutual exclusion with hardware support. However, as
21 * well as depending on external hardware, the these instructions have defined
22 * behavior only on certain memory types (cacheable and Normal memory in
23 * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
24 * in trusted firmware are such that mutual exclusion implementation cannot
25 * expect that accesses to the lock have the specific type required by the
26 * architecture for these primitives to function (for example, not all
27 * contenders may have address translation enabled).
28 *
29 * This implementation does not use mutual exclusion primitives. It expects
30 * memory regions where the locks reside to be fully ordered and coherent
31 * (either by disabling address translation, or by assigning proper attributes
32 * when translation is enabled).
33 *
34 * Note that the ARM architecture guarantees single-copy atomicity for aligned
35 * accesses regardless of status of address translation.
36 */
37
38 #define assert_bakery_entry_valid(_entry, _bakery) do { \
39 assert((_bakery) != NULL); \
40 assert((_entry) < BAKERY_LOCK_MAX_CPUS); \
41 } while (false)
42
43 /* Obtain a ticket for a given CPU */
bakery_get_ticket(bakery_lock_t * bakery,unsigned int me)44 static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me)
45 {
46 unsigned int my_ticket, their_ticket;
47 unsigned int they;
48
49 /* Prevent recursive acquisition */
50 assert(bakery_ticket_number(bakery->lock_data[me]) == 0U);
51
52 /*
53 * Flag that we're busy getting our ticket. All CPUs are iterated in the
54 * order of their ordinal position to decide the maximum ticket value
55 * observed so far. Our priority is set to be greater than the maximum
56 * observed priority
57 *
58 * Note that it's possible that more than one contender gets the same
59 * ticket value. That's OK as the lock is acquired based on the priority
60 * value, not the ticket value alone.
61 */
62 my_ticket = 0U;
63 bakery->lock_data[me] = make_bakery_data(CHOOSING_TICKET, my_ticket);
64 for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
65 their_ticket = bakery_ticket_number(bakery->lock_data[they]);
66 if (their_ticket > my_ticket) {
67 my_ticket = their_ticket;
68 }
69 }
70
71 /*
72 * Compute ticket; then signal to other contenders waiting for us to
73 * finish calculating our ticket value that we're done
74 */
75 ++my_ticket;
76 bakery->lock_data[me] = make_bakery_data(CHOSEN_TICKET, my_ticket);
77
78 return my_ticket;
79 }
80
81
82 /*
83 * Acquire bakery lock
84 *
85 * Contending CPUs need first obtain a non-zero ticket and then calculate
86 * priority value. A contending CPU iterate over all other CPUs in the platform,
87 * which may be contending for the same lock, in the order of their ordinal
88 * position (CPU0, CPU1 and so on). A non-contending CPU will have its ticket
89 * (and priority) value as 0. The contending CPU compares its priority with that
90 * of others'. The CPU with the highest priority (lowest numerical value)
91 * acquires the lock
92 */
bakery_lock_get(bakery_lock_t * bakery)93 void bakery_lock_get(bakery_lock_t *bakery)
94 {
95 unsigned int they, me;
96 unsigned int my_ticket, my_prio, their_ticket;
97 unsigned int their_bakery_data;
98
99 me = plat_my_core_pos();
100
101 assert_bakery_entry_valid(me, bakery);
102
103 /* Get a ticket */
104 my_ticket = bakery_get_ticket(bakery, me);
105
106 /*
107 * Now that we got our ticket, compute our priority value, then compare
108 * with that of others, and proceed to acquire the lock
109 */
110 my_prio = bakery_get_priority(my_ticket, me);
111 for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
112 if (me == they) {
113 continue;
114 }
115
116 /* Wait for the contender to get their ticket */
117 do {
118 their_bakery_data = bakery->lock_data[they];
119 } while (bakery_is_choosing(their_bakery_data));
120
121 /*
122 * If the other party is a contender, they'll have non-zero
123 * (valid) ticket value. If they do, compare priorities
124 */
125 their_ticket = bakery_ticket_number(their_bakery_data);
126 if ((their_ticket != 0U) &&
127 (bakery_get_priority(their_ticket, they) < my_prio)) {
128 /*
129 * They have higher priority (lower value). Wait for
130 * their ticket value to change (either release the lock
131 * to have it dropped to 0; or drop and probably content
132 * again for the same lock to have an even higher value)
133 */
134 do {
135 wfe();
136 } while (their_ticket ==
137 bakery_ticket_number(bakery->lock_data[they]));
138 }
139 }
140
141 /*
142 * Lock acquired. Ensure that any reads and writes from a shared
143 * resource in the critical section read/write values after the lock is
144 * acquired.
145 */
146 dmbish();
147 }
148
149
150 /* Release the lock and signal contenders */
bakery_lock_release(bakery_lock_t * bakery)151 void bakery_lock_release(bakery_lock_t *bakery)
152 {
153 unsigned int me = plat_my_core_pos();
154
155 assert_bakery_entry_valid(me, bakery);
156 assert(bakery_ticket_number(bakery->lock_data[me]) != 0U);
157
158 /*
159 * Ensure that other observers see any stores in the critical section
160 * before releasing the lock. Also ensure all loads in the critical
161 * section are complete before releasing the lock. Release the lock by
162 * resetting ticket. Then signal other waiting contenders.
163 */
164 dmbish();
165 bakery->lock_data[me] = 0U;
166
167 /* Required to ensure ordering of the following sev */
168 dsb();
169 sev();
170 }
171