xref: /rk3399_ARM-atf/lib/locks/bakery/bakery_lock_coherent.c (revision 8c5fe0b5b9f1666b4ddd8f5849de80249cdebe40)
1*8c5fe0b5SSoby Mathew /*
2*8c5fe0b5SSoby Mathew  * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
3*8c5fe0b5SSoby Mathew  *
4*8c5fe0b5SSoby Mathew  * Redistribution and use in source and binary forms, with or without
5*8c5fe0b5SSoby Mathew  * modification, are permitted provided that the following conditions are met:
6*8c5fe0b5SSoby Mathew  *
7*8c5fe0b5SSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8*8c5fe0b5SSoby Mathew  * list of conditions and the following disclaimer.
9*8c5fe0b5SSoby Mathew  *
10*8c5fe0b5SSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11*8c5fe0b5SSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12*8c5fe0b5SSoby Mathew  * and/or other materials provided with the distribution.
13*8c5fe0b5SSoby Mathew  *
14*8c5fe0b5SSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15*8c5fe0b5SSoby Mathew  * to endorse or promote products derived from this software without specific
16*8c5fe0b5SSoby Mathew  * prior written permission.
17*8c5fe0b5SSoby Mathew  *
18*8c5fe0b5SSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*8c5fe0b5SSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*8c5fe0b5SSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*8c5fe0b5SSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*8c5fe0b5SSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*8c5fe0b5SSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*8c5fe0b5SSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*8c5fe0b5SSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*8c5fe0b5SSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*8c5fe0b5SSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*8c5fe0b5SSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29*8c5fe0b5SSoby Mathew  */
30*8c5fe0b5SSoby Mathew 
31*8c5fe0b5SSoby Mathew #include <arch_helpers.h>
32*8c5fe0b5SSoby Mathew #include <assert.h>
33*8c5fe0b5SSoby Mathew #include <bakery_lock.h>
34*8c5fe0b5SSoby Mathew #include <cpu_data.h>
35*8c5fe0b5SSoby Mathew #include <platform.h>
36*8c5fe0b5SSoby Mathew #include <string.h>
37*8c5fe0b5SSoby Mathew 
38*8c5fe0b5SSoby Mathew /*
39*8c5fe0b5SSoby Mathew  * Functions in this file implement Bakery Algorithm for mutual exclusion with the
40*8c5fe0b5SSoby Mathew  * bakery lock data structures in coherent memory.
41*8c5fe0b5SSoby Mathew  *
42*8c5fe0b5SSoby Mathew  * ARM architecture offers a family of exclusive access instructions to
43*8c5fe0b5SSoby Mathew  * efficiently implement mutual exclusion with hardware support. However, as
44*8c5fe0b5SSoby Mathew  * well as depending on external hardware, the these instructions have defined
45*8c5fe0b5SSoby Mathew  * behavior only on certain memory types (cacheable and Normal memory in
46*8c5fe0b5SSoby Mathew  * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
47*8c5fe0b5SSoby Mathew  * in trusted firmware are such that mutual exclusion implementation cannot
48*8c5fe0b5SSoby Mathew  * expect that accesses to the lock have the specific type required by the
49*8c5fe0b5SSoby Mathew  * architecture for these primitives to function (for example, not all
50*8c5fe0b5SSoby Mathew  * contenders may have address translation enabled).
51*8c5fe0b5SSoby Mathew  *
52*8c5fe0b5SSoby Mathew  * This implementation does not use mutual exclusion primitives. It expects
53*8c5fe0b5SSoby Mathew  * memory regions where the locks reside to be fully ordered and coherent
54*8c5fe0b5SSoby Mathew  * (either by disabling address translation, or by assigning proper attributes
55*8c5fe0b5SSoby Mathew  * when translation is enabled).
56*8c5fe0b5SSoby Mathew  *
57*8c5fe0b5SSoby Mathew  * Note that the ARM architecture guarantees single-copy atomicity for aligned
58*8c5fe0b5SSoby Mathew  * accesses regardless of status of address translation.
59*8c5fe0b5SSoby Mathew  */
60*8c5fe0b5SSoby Mathew 
61*8c5fe0b5SSoby Mathew #define assert_bakery_entry_valid(entry, bakery) do {	\
62*8c5fe0b5SSoby Mathew 	assert(bakery);					\
63*8c5fe0b5SSoby Mathew 	assert(entry < BAKERY_LOCK_MAX_CPUS);		\
64*8c5fe0b5SSoby Mathew } while (0)
65*8c5fe0b5SSoby Mathew 
66*8c5fe0b5SSoby Mathew /* Convert a ticket to priority */
67*8c5fe0b5SSoby Mathew #define PRIORITY(t, pos)	(((t) << 8) | (pos))
68*8c5fe0b5SSoby Mathew 
69*8c5fe0b5SSoby Mathew 
70*8c5fe0b5SSoby Mathew /* Initialize Bakery Lock to reset ownership and all ticket values */
71*8c5fe0b5SSoby Mathew void bakery_lock_init(bakery_lock_t *bakery)
72*8c5fe0b5SSoby Mathew {
73*8c5fe0b5SSoby Mathew 	assert(bakery);
74*8c5fe0b5SSoby Mathew 
75*8c5fe0b5SSoby Mathew 	/* All ticket values need to be 0 */
76*8c5fe0b5SSoby Mathew 	memset(bakery, 0, sizeof(*bakery));
77*8c5fe0b5SSoby Mathew 	bakery->owner = NO_OWNER;
78*8c5fe0b5SSoby Mathew }
79*8c5fe0b5SSoby Mathew 
80*8c5fe0b5SSoby Mathew 
81*8c5fe0b5SSoby Mathew /* Obtain a ticket for a given CPU */
82*8c5fe0b5SSoby Mathew static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me)
83*8c5fe0b5SSoby Mathew {
84*8c5fe0b5SSoby Mathew 	unsigned int my_ticket, their_ticket;
85*8c5fe0b5SSoby Mathew 	unsigned int they;
86*8c5fe0b5SSoby Mathew 
87*8c5fe0b5SSoby Mathew 	/*
88*8c5fe0b5SSoby Mathew 	 * Flag that we're busy getting our ticket. All CPUs are iterated in the
89*8c5fe0b5SSoby Mathew 	 * order of their ordinal position to decide the maximum ticket value
90*8c5fe0b5SSoby Mathew 	 * observed so far. Our priority is set to be greater than the maximum
91*8c5fe0b5SSoby Mathew 	 * observed priority
92*8c5fe0b5SSoby Mathew 	 *
93*8c5fe0b5SSoby Mathew 	 * Note that it's possible that more than one contender gets the same
94*8c5fe0b5SSoby Mathew 	 * ticket value. That's OK as the lock is acquired based on the priority
95*8c5fe0b5SSoby Mathew 	 * value, not the ticket value alone.
96*8c5fe0b5SSoby Mathew 	 */
97*8c5fe0b5SSoby Mathew 	my_ticket = 0;
98*8c5fe0b5SSoby Mathew 	bakery->entering[me] = 1;
99*8c5fe0b5SSoby Mathew 	for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
100*8c5fe0b5SSoby Mathew 		their_ticket = bakery->number[they];
101*8c5fe0b5SSoby Mathew 		if (their_ticket > my_ticket)
102*8c5fe0b5SSoby Mathew 			my_ticket = their_ticket;
103*8c5fe0b5SSoby Mathew 	}
104*8c5fe0b5SSoby Mathew 
105*8c5fe0b5SSoby Mathew 	/*
106*8c5fe0b5SSoby Mathew 	 * Compute ticket; then signal to other contenders waiting for us to
107*8c5fe0b5SSoby Mathew 	 * finish calculating our ticket value that we're done
108*8c5fe0b5SSoby Mathew 	 */
109*8c5fe0b5SSoby Mathew 	++my_ticket;
110*8c5fe0b5SSoby Mathew 	bakery->number[me] = my_ticket;
111*8c5fe0b5SSoby Mathew 	bakery->entering[me] = 0;
112*8c5fe0b5SSoby Mathew 
113*8c5fe0b5SSoby Mathew 	return my_ticket;
114*8c5fe0b5SSoby Mathew }
115*8c5fe0b5SSoby Mathew 
116*8c5fe0b5SSoby Mathew 
117*8c5fe0b5SSoby Mathew /*
118*8c5fe0b5SSoby Mathew  * Acquire bakery lock
119*8c5fe0b5SSoby Mathew  *
120*8c5fe0b5SSoby Mathew  * Contending CPUs need first obtain a non-zero ticket and then calculate
121*8c5fe0b5SSoby Mathew  * priority value. A contending CPU iterate over all other CPUs in the platform,
122*8c5fe0b5SSoby Mathew  * which may be contending for the same lock, in the order of their ordinal
123*8c5fe0b5SSoby Mathew  * position (CPU0, CPU1 and so on). A non-contending CPU will have its ticket
124*8c5fe0b5SSoby Mathew  * (and priority) value as 0. The contending CPU compares its priority with that
125*8c5fe0b5SSoby Mathew  * of others'. The CPU with the highest priority (lowest numerical value)
126*8c5fe0b5SSoby Mathew  * acquires the lock
127*8c5fe0b5SSoby Mathew  */
128*8c5fe0b5SSoby Mathew void bakery_lock_get(bakery_lock_t *bakery)
129*8c5fe0b5SSoby Mathew {
130*8c5fe0b5SSoby Mathew 	unsigned int they, me;
131*8c5fe0b5SSoby Mathew 	unsigned int my_ticket, my_prio, their_ticket;
132*8c5fe0b5SSoby Mathew 
133*8c5fe0b5SSoby Mathew 	me = platform_get_core_pos(read_mpidr_el1());
134*8c5fe0b5SSoby Mathew 
135*8c5fe0b5SSoby Mathew 	assert_bakery_entry_valid(me, bakery);
136*8c5fe0b5SSoby Mathew 
137*8c5fe0b5SSoby Mathew 	/* Prevent recursive acquisition */
138*8c5fe0b5SSoby Mathew 	assert(bakery->owner != me);
139*8c5fe0b5SSoby Mathew 
140*8c5fe0b5SSoby Mathew 	/* Get a ticket */
141*8c5fe0b5SSoby Mathew 	my_ticket = bakery_get_ticket(bakery, me);
142*8c5fe0b5SSoby Mathew 
143*8c5fe0b5SSoby Mathew 	/*
144*8c5fe0b5SSoby Mathew 	 * Now that we got our ticket, compute our priority value, then compare
145*8c5fe0b5SSoby Mathew 	 * with that of others, and proceed to acquire the lock
146*8c5fe0b5SSoby Mathew 	 */
147*8c5fe0b5SSoby Mathew 	my_prio = PRIORITY(my_ticket, me);
148*8c5fe0b5SSoby Mathew 	for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
149*8c5fe0b5SSoby Mathew 		if (me == they)
150*8c5fe0b5SSoby Mathew 			continue;
151*8c5fe0b5SSoby Mathew 
152*8c5fe0b5SSoby Mathew 		/* Wait for the contender to get their ticket */
153*8c5fe0b5SSoby Mathew 		while (bakery->entering[they])
154*8c5fe0b5SSoby Mathew 			;
155*8c5fe0b5SSoby Mathew 
156*8c5fe0b5SSoby Mathew 		/*
157*8c5fe0b5SSoby Mathew 		 * If the other party is a contender, they'll have non-zero
158*8c5fe0b5SSoby Mathew 		 * (valid) ticket value. If they do, compare priorities
159*8c5fe0b5SSoby Mathew 		 */
160*8c5fe0b5SSoby Mathew 		their_ticket = bakery->number[they];
161*8c5fe0b5SSoby Mathew 		if (their_ticket && (PRIORITY(their_ticket, they) < my_prio)) {
162*8c5fe0b5SSoby Mathew 			/*
163*8c5fe0b5SSoby Mathew 			 * They have higher priority (lower value). Wait for
164*8c5fe0b5SSoby Mathew 			 * their ticket value to change (either release the lock
165*8c5fe0b5SSoby Mathew 			 * to have it dropped to 0; or drop and probably content
166*8c5fe0b5SSoby Mathew 			 * again for the same lock to have an even higher value)
167*8c5fe0b5SSoby Mathew 			 */
168*8c5fe0b5SSoby Mathew 			do {
169*8c5fe0b5SSoby Mathew 				wfe();
170*8c5fe0b5SSoby Mathew 			} while (their_ticket == bakery->number[they]);
171*8c5fe0b5SSoby Mathew 		}
172*8c5fe0b5SSoby Mathew 	}
173*8c5fe0b5SSoby Mathew 
174*8c5fe0b5SSoby Mathew 	/* Lock acquired */
175*8c5fe0b5SSoby Mathew 	bakery->owner = me;
176*8c5fe0b5SSoby Mathew }
177*8c5fe0b5SSoby Mathew 
178*8c5fe0b5SSoby Mathew 
179*8c5fe0b5SSoby Mathew /* Release the lock and signal contenders */
180*8c5fe0b5SSoby Mathew void bakery_lock_release(bakery_lock_t *bakery)
181*8c5fe0b5SSoby Mathew {
182*8c5fe0b5SSoby Mathew 	unsigned int me = platform_get_core_pos(read_mpidr_el1());
183*8c5fe0b5SSoby Mathew 
184*8c5fe0b5SSoby Mathew 	assert_bakery_entry_valid(me, bakery);
185*8c5fe0b5SSoby Mathew 	assert(bakery->owner == me);
186*8c5fe0b5SSoby Mathew 
187*8c5fe0b5SSoby Mathew 	/*
188*8c5fe0b5SSoby Mathew 	 * Release lock by resetting ownership and ticket. Then signal other
189*8c5fe0b5SSoby Mathew 	 * waiting contenders
190*8c5fe0b5SSoby Mathew 	 */
191*8c5fe0b5SSoby Mathew 	bakery->owner = NO_OWNER;
192*8c5fe0b5SSoby Mathew 	bakery->number[me] = 0;
193*8c5fe0b5SSoby Mathew 	dsb();
194*8c5fe0b5SSoby Mathew 	sev();
195*8c5fe0b5SSoby Mathew }
196