xref: /rk3399_ARM-atf/lib/locks/bakery/bakery_lock_normal.c (revision 8c5fe0b5b9f1666b4ddd8f5849de80249cdebe40)
1*8c5fe0b5SSoby Mathew /*
2*8c5fe0b5SSoby Mathew  * Copyright (c) 2015, 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 cacheable and Normal 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, 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 cacheable and Normal.
54*8c5fe0b5SSoby Mathew  *
55*8c5fe0b5SSoby Mathew  * Note that the ARM architecture guarantees single-copy atomicity for aligned
56*8c5fe0b5SSoby Mathew  * accesses regardless of status of address translation.
57*8c5fe0b5SSoby Mathew  */
58*8c5fe0b5SSoby Mathew 
59*8c5fe0b5SSoby Mathew /* Convert a ticket to priority */
60*8c5fe0b5SSoby Mathew #define PRIORITY(t, pos)	(((t) << 8) | (pos))
61*8c5fe0b5SSoby Mathew 
62*8c5fe0b5SSoby Mathew #define CHOOSING_TICKET		0x1
63*8c5fe0b5SSoby Mathew #define CHOOSING_DONE		0x0
64*8c5fe0b5SSoby Mathew 
65*8c5fe0b5SSoby Mathew #define bakery_is_choosing(info)	(info & 0x1)
66*8c5fe0b5SSoby Mathew #define bakery_ticket_number(info)	((info >> 1) & 0x7FFF)
67*8c5fe0b5SSoby Mathew #define make_bakery_data(choosing, number) \
68*8c5fe0b5SSoby Mathew 		(((choosing & 0x1) | (number << 1)) & 0xFFFF)
69*8c5fe0b5SSoby Mathew 
70*8c5fe0b5SSoby Mathew /* This macro assumes that the bakery_info array is located at the offset specified */
71*8c5fe0b5SSoby Mathew #define get_my_bakery_info(offset, id)		\
72*8c5fe0b5SSoby Mathew 	(((bakery_info_t *) (((uint8_t *)_cpu_data()) + offset)) + id)
73*8c5fe0b5SSoby Mathew 
74*8c5fe0b5SSoby Mathew #define get_bakery_info_by_index(offset, id, ix)	\
75*8c5fe0b5SSoby Mathew 	(((bakery_info_t *) (((uint8_t *)_cpu_data_by_index(ix)) + offset)) + id)
76*8c5fe0b5SSoby Mathew 
77*8c5fe0b5SSoby Mathew #define write_cache_op(addr, cached)	\
78*8c5fe0b5SSoby Mathew 				do {	\
79*8c5fe0b5SSoby Mathew 					(cached ? dccvac((uint64_t)addr) :\
80*8c5fe0b5SSoby Mathew 						dcivac((uint64_t)addr));\
81*8c5fe0b5SSoby Mathew 						dsbish();\
82*8c5fe0b5SSoby Mathew 				} while (0)
83*8c5fe0b5SSoby Mathew 
84*8c5fe0b5SSoby Mathew #define read_cache_op(addr, cached)	if (cached) \
85*8c5fe0b5SSoby Mathew 					    dccivac((uint64_t)addr)
86*8c5fe0b5SSoby Mathew 
87*8c5fe0b5SSoby Mathew static unsigned int bakery_get_ticket(int id, unsigned int offset,
88*8c5fe0b5SSoby Mathew 						unsigned int me, int is_cached)
89*8c5fe0b5SSoby Mathew {
90*8c5fe0b5SSoby Mathew 	unsigned int my_ticket, their_ticket;
91*8c5fe0b5SSoby Mathew 	unsigned int they;
92*8c5fe0b5SSoby Mathew 	bakery_info_t *my_bakery_info, *their_bakery_info;
93*8c5fe0b5SSoby Mathew 
94*8c5fe0b5SSoby Mathew 	/*
95*8c5fe0b5SSoby Mathew 	 * Obtain a reference to the bakery information for this cpu and ensure
96*8c5fe0b5SSoby Mathew 	 * it is not NULL.
97*8c5fe0b5SSoby Mathew 	 */
98*8c5fe0b5SSoby Mathew 	my_bakery_info = get_my_bakery_info(offset, id);
99*8c5fe0b5SSoby Mathew 	assert(my_bakery_info);
100*8c5fe0b5SSoby Mathew 
101*8c5fe0b5SSoby Mathew 	/*
102*8c5fe0b5SSoby Mathew 	 * Tell other contenders that we are through the bakery doorway i.e.
103*8c5fe0b5SSoby Mathew 	 * going to allocate a ticket for this cpu.
104*8c5fe0b5SSoby Mathew 	 */
105*8c5fe0b5SSoby Mathew 	my_ticket = 0;
106*8c5fe0b5SSoby Mathew 	my_bakery_info->lock_data = make_bakery_data(CHOOSING_TICKET, my_ticket);
107*8c5fe0b5SSoby Mathew 
108*8c5fe0b5SSoby Mathew 	write_cache_op(my_bakery_info, is_cached);
109*8c5fe0b5SSoby Mathew 
110*8c5fe0b5SSoby Mathew 	/*
111*8c5fe0b5SSoby Mathew 	 * Iterate through the bakery information of each contender to allocate
112*8c5fe0b5SSoby Mathew 	 * the highest ticket number for this cpu.
113*8c5fe0b5SSoby Mathew 	 */
114*8c5fe0b5SSoby Mathew 	for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
115*8c5fe0b5SSoby Mathew 		if (me == they)
116*8c5fe0b5SSoby Mathew 			continue;
117*8c5fe0b5SSoby Mathew 
118*8c5fe0b5SSoby Mathew 		/*
119*8c5fe0b5SSoby Mathew 		 * Get a reference to the other contender's bakery info and
120*8c5fe0b5SSoby Mathew 		 * ensure that a stale copy is not read.
121*8c5fe0b5SSoby Mathew 		 */
122*8c5fe0b5SSoby Mathew 		their_bakery_info = get_bakery_info_by_index(offset, id, they);
123*8c5fe0b5SSoby Mathew 		assert(their_bakery_info);
124*8c5fe0b5SSoby Mathew 
125*8c5fe0b5SSoby Mathew 		read_cache_op(their_bakery_info, is_cached);
126*8c5fe0b5SSoby Mathew 
127*8c5fe0b5SSoby Mathew 		/*
128*8c5fe0b5SSoby Mathew 		 * Update this cpu's ticket number if a higher ticket number is
129*8c5fe0b5SSoby Mathew 		 * seen
130*8c5fe0b5SSoby Mathew 		 */
131*8c5fe0b5SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_info->lock_data);
132*8c5fe0b5SSoby Mathew 		if (their_ticket > my_ticket)
133*8c5fe0b5SSoby Mathew 			my_ticket = their_ticket;
134*8c5fe0b5SSoby Mathew 	}
135*8c5fe0b5SSoby Mathew 
136*8c5fe0b5SSoby Mathew 	/*
137*8c5fe0b5SSoby Mathew 	 * Compute ticket; then signal to other contenders waiting for us to
138*8c5fe0b5SSoby Mathew 	 * finish calculating our ticket value that we're done
139*8c5fe0b5SSoby Mathew 	 */
140*8c5fe0b5SSoby Mathew 	++my_ticket;
141*8c5fe0b5SSoby Mathew 	my_bakery_info->lock_data = make_bakery_data(CHOOSING_DONE, my_ticket);
142*8c5fe0b5SSoby Mathew 
143*8c5fe0b5SSoby Mathew 	write_cache_op(my_bakery_info, is_cached);
144*8c5fe0b5SSoby Mathew 
145*8c5fe0b5SSoby Mathew 	return my_ticket;
146*8c5fe0b5SSoby Mathew }
147*8c5fe0b5SSoby Mathew 
148*8c5fe0b5SSoby Mathew void bakery_lock_get(unsigned int id, unsigned int offset)
149*8c5fe0b5SSoby Mathew {
150*8c5fe0b5SSoby Mathew 	unsigned int they, me, is_cached;
151*8c5fe0b5SSoby Mathew 	unsigned int my_ticket, my_prio, their_ticket;
152*8c5fe0b5SSoby Mathew 	bakery_info_t *their_bakery_info;
153*8c5fe0b5SSoby Mathew 	uint16_t their_bakery_data;
154*8c5fe0b5SSoby Mathew 
155*8c5fe0b5SSoby Mathew 	me = platform_get_core_pos(read_mpidr_el1());
156*8c5fe0b5SSoby Mathew 
157*8c5fe0b5SSoby Mathew 	is_cached = read_sctlr_el3() & SCTLR_C_BIT;
158*8c5fe0b5SSoby Mathew 
159*8c5fe0b5SSoby Mathew 	/* Get a ticket */
160*8c5fe0b5SSoby Mathew 	my_ticket = bakery_get_ticket(id, offset, me, is_cached);
161*8c5fe0b5SSoby Mathew 
162*8c5fe0b5SSoby Mathew 	/*
163*8c5fe0b5SSoby Mathew 	 * Now that we got our ticket, compute our priority value, then compare
164*8c5fe0b5SSoby Mathew 	 * with that of others, and proceed to acquire the lock
165*8c5fe0b5SSoby Mathew 	 */
166*8c5fe0b5SSoby Mathew 	my_prio = PRIORITY(my_ticket, me);
167*8c5fe0b5SSoby Mathew 	for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
168*8c5fe0b5SSoby Mathew 		if (me == they)
169*8c5fe0b5SSoby Mathew 			continue;
170*8c5fe0b5SSoby Mathew 
171*8c5fe0b5SSoby Mathew 		/*
172*8c5fe0b5SSoby Mathew 		 * Get a reference to the other contender's bakery info and
173*8c5fe0b5SSoby Mathew 		 * ensure that a stale copy is not read.
174*8c5fe0b5SSoby Mathew 		 */
175*8c5fe0b5SSoby Mathew 		their_bakery_info = get_bakery_info_by_index(offset, id, they);
176*8c5fe0b5SSoby Mathew 		assert(their_bakery_info);
177*8c5fe0b5SSoby Mathew 		read_cache_op(their_bakery_info, is_cached);
178*8c5fe0b5SSoby Mathew 
179*8c5fe0b5SSoby Mathew 		their_bakery_data = their_bakery_info->lock_data;
180*8c5fe0b5SSoby Mathew 
181*8c5fe0b5SSoby Mathew 		/* Wait for the contender to get their ticket */
182*8c5fe0b5SSoby Mathew 		while (bakery_is_choosing(their_bakery_data)) {
183*8c5fe0b5SSoby Mathew 			read_cache_op(their_bakery_info, is_cached);
184*8c5fe0b5SSoby Mathew 			their_bakery_data = their_bakery_info->lock_data;
185*8c5fe0b5SSoby Mathew 		}
186*8c5fe0b5SSoby Mathew 
187*8c5fe0b5SSoby Mathew 		/*
188*8c5fe0b5SSoby Mathew 		 * If the other party is a contender, they'll have non-zero
189*8c5fe0b5SSoby Mathew 		 * (valid) ticket value. If they do, compare priorities
190*8c5fe0b5SSoby Mathew 		 */
191*8c5fe0b5SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_data);
192*8c5fe0b5SSoby Mathew 		if (their_ticket && (PRIORITY(their_ticket, they) < my_prio)) {
193*8c5fe0b5SSoby Mathew 			/*
194*8c5fe0b5SSoby Mathew 			 * They have higher priority (lower value). Wait for
195*8c5fe0b5SSoby Mathew 			 * their ticket value to change (either release the lock
196*8c5fe0b5SSoby Mathew 			 * to have it dropped to 0; or drop and probably content
197*8c5fe0b5SSoby Mathew 			 * again for the same lock to have an even higher value)
198*8c5fe0b5SSoby Mathew 			 */
199*8c5fe0b5SSoby Mathew 			do {
200*8c5fe0b5SSoby Mathew 				wfe();
201*8c5fe0b5SSoby Mathew 				read_cache_op(their_bakery_info, is_cached);
202*8c5fe0b5SSoby Mathew 			} while (their_ticket
203*8c5fe0b5SSoby Mathew 				== bakery_ticket_number(their_bakery_info->lock_data));
204*8c5fe0b5SSoby Mathew 		}
205*8c5fe0b5SSoby Mathew 	}
206*8c5fe0b5SSoby Mathew }
207*8c5fe0b5SSoby Mathew 
208*8c5fe0b5SSoby Mathew void bakery_lock_release(unsigned int id, unsigned int offset)
209*8c5fe0b5SSoby Mathew {
210*8c5fe0b5SSoby Mathew 	bakery_info_t *my_bakery_info;
211*8c5fe0b5SSoby Mathew 	unsigned int is_cached = read_sctlr_el3() & SCTLR_C_BIT;
212*8c5fe0b5SSoby Mathew 
213*8c5fe0b5SSoby Mathew 	my_bakery_info = get_my_bakery_info(offset, id);
214*8c5fe0b5SSoby Mathew 	my_bakery_info->lock_data = 0;
215*8c5fe0b5SSoby Mathew 	write_cache_op(my_bakery_info, is_cached);
216*8c5fe0b5SSoby Mathew 	sev();
217*8c5fe0b5SSoby Mathew }
218