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 	 * Compute ticket; then signal to other contenders waiting for us to
72 	 * finish calculating our ticket value that we're done
73 	 */
74 	++my_ticket;
75 	bakery->lock_data[me] = make_bakery_data(CHOSEN_TICKET, my_ticket);
76 
77 	return my_ticket;
78 }
79 
80 
81 /*
82  * Acquire bakery lock
83  *
84  * Contending CPUs need first obtain a non-zero ticket and then calculate
85  * priority value. A contending CPU iterate over all other CPUs in the platform,
86  * which may be contending for the same lock, in the order of their ordinal
87  * position (CPU0, CPU1 and so on). A non-contending CPU will have its ticket
88  * (and priority) value as 0. The contending CPU compares its priority with that
89  * of others'. The CPU with the highest priority (lowest numerical value)
90  * acquires the lock
91  */
bakery_lock_get(bakery_lock_t * bakery)92 void bakery_lock_get(bakery_lock_t *bakery)
93 {
94 	unsigned int they, me;
95 	unsigned int my_ticket, my_prio, their_ticket;
96 	unsigned int their_bakery_data;
97 
98 	me = plat_my_core_pos();
99 
100 	assert_bakery_entry_valid(me, bakery);
101 
102 	/* Get a ticket */
103 	my_ticket = bakery_get_ticket(bakery, me);
104 
105 	/*
106 	 * Now that we got our ticket, compute our priority value, then compare
107 	 * with that of others, and proceed to acquire the lock
108 	 */
109 	my_prio = bakery_get_priority(my_ticket, me);
110 	for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
111 		if (me == they)
112 			continue;
113 
114 		/* Wait for the contender to get their ticket */
115 		do {
116 			their_bakery_data = bakery->lock_data[they];
117 		} while (bakery_is_choosing(their_bakery_data));
118 
119 		/*
120 		 * If the other party is a contender, they'll have non-zero
121 		 * (valid) ticket value. If they do, compare priorities
122 		 */
123 		their_ticket = bakery_ticket_number(their_bakery_data);
124 		if ((their_ticket != 0U) &&
125 		    (bakery_get_priority(their_ticket, they) < my_prio)) {
126 			/*
127 			 * They have higher priority (lower value). Wait for
128 			 * their ticket value to change (either release the lock
129 			 * to have it dropped to 0; or drop and probably content
130 			 * again for the same lock to have an even higher value)
131 			 */
132 			do {
133 				wfe();
134 			} while (their_ticket ==
135 				bakery_ticket_number(bakery->lock_data[they]));
136 		}
137 	}
138 
139 	/*
140 	 * Lock acquired. Ensure that any reads and writes from a shared
141 	 * resource in the critical section read/write values after the lock is
142 	 * acquired.
143 	 */
144 	dmbish();
145 }
146 
147 
148 /* Release the lock and signal contenders */
bakery_lock_release(bakery_lock_t * bakery)149 void bakery_lock_release(bakery_lock_t *bakery)
150 {
151 	unsigned int me = plat_my_core_pos();
152 
153 	assert_bakery_entry_valid(me, bakery);
154 	assert(bakery_ticket_number(bakery->lock_data[me]) != 0U);
155 
156 	/*
157 	 * Ensure that other observers see any stores in the critical section
158 	 * before releasing the lock. Also ensure all loads in the critical
159 	 * section are complete before releasing the lock. Release the lock by
160 	 * resetting ticket. Then signal other waiting contenders.
161 	 */
162 	dmbish();
163 	bakery->lock_data[me] = 0U;
164 
165 	/* Required to ensure ordering of the following sev */
166 	dsb();
167 	sev();
168 }
169