xref: /rk3399_ARM-atf/lib/locks/bakery/bakery_lock_normal.c (revision 1c9573a157f0d6d76ded5e651bb3f0b9f3a3c9ec)
1 /*
2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <arch_helpers.h>
32 #include <assert.h>
33 #include <bakery_lock.h>
34 #include <cpu_data.h>
35 #include <platform.h>
36 #include <string.h>
37 
38 /*
39  * Functions in this file implement Bakery Algorithm for mutual exclusion with the
40  * bakery lock data structures in cacheable and Normal memory.
41  *
42  * ARM architecture offers a family of exclusive access instructions to
43  * efficiently implement mutual exclusion with hardware support. However, as
44  * well as depending on external hardware, these instructions have defined
45  * behavior only on certain memory types (cacheable and Normal memory in
46  * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
47  * in trusted firmware are such that mutual exclusion implementation cannot
48  * expect that accesses to the lock have the specific type required by the
49  * architecture for these primitives to function (for example, not all
50  * contenders may have address translation enabled).
51  *
52  * This implementation does not use mutual exclusion primitives. It expects
53  * memory regions where the locks reside to be cacheable and Normal.
54  *
55  * Note that the ARM architecture guarantees single-copy atomicity for aligned
56  * accesses regardless of status of address translation.
57  */
58 
59 /* This macro assumes that the bakery_info array is located at the offset specified */
60 #define get_my_bakery_info(offset, id)		\
61 	(((bakery_info_t *) (((uint8_t *)_cpu_data()) + offset)) + id)
62 
63 #define get_bakery_info_by_index(offset, id, ix)	\
64 	(((bakery_info_t *) (((uint8_t *)_cpu_data_by_index(ix)) + offset)) + id)
65 
66 #define write_cache_op(addr, cached)	\
67 				do {	\
68 					(cached ? dccvac((uint64_t)addr) :\
69 						dcivac((uint64_t)addr));\
70 						dsbish();\
71 				} while (0)
72 
73 #define read_cache_op(addr, cached)	if (cached) \
74 					    dccivac((uint64_t)addr)
75 
76 static unsigned int bakery_get_ticket(int id, unsigned int offset,
77 						unsigned int me, int is_cached)
78 {
79 	unsigned int my_ticket, their_ticket;
80 	unsigned int they;
81 	bakery_info_t *my_bakery_info, *their_bakery_info;
82 
83 	/*
84 	 * Obtain a reference to the bakery information for this cpu and ensure
85 	 * it is not NULL.
86 	 */
87 	my_bakery_info = get_my_bakery_info(offset, id);
88 	assert(my_bakery_info);
89 
90 	/*
91 	 * Tell other contenders that we are through the bakery doorway i.e.
92 	 * going to allocate a ticket for this cpu.
93 	 */
94 	my_ticket = 0;
95 	my_bakery_info->lock_data = make_bakery_data(CHOOSING_TICKET, my_ticket);
96 
97 	write_cache_op(my_bakery_info, is_cached);
98 
99 	/*
100 	 * Iterate through the bakery information of each contender to allocate
101 	 * the highest ticket number for this cpu.
102 	 */
103 	for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
104 		if (me == they)
105 			continue;
106 
107 		/*
108 		 * Get a reference to the other contender's bakery info and
109 		 * ensure that a stale copy is not read.
110 		 */
111 		their_bakery_info = get_bakery_info_by_index(offset, id, they);
112 		assert(their_bakery_info);
113 
114 		read_cache_op(their_bakery_info, is_cached);
115 
116 		/*
117 		 * Update this cpu's ticket number if a higher ticket number is
118 		 * seen
119 		 */
120 		their_ticket = bakery_ticket_number(their_bakery_info->lock_data);
121 		if (their_ticket > my_ticket)
122 			my_ticket = their_ticket;
123 	}
124 
125 	/*
126 	 * Compute ticket; then signal to other contenders waiting for us to
127 	 * finish calculating our ticket value that we're done
128 	 */
129 	++my_ticket;
130 	my_bakery_info->lock_data = make_bakery_data(CHOSEN_TICKET, my_ticket);
131 
132 	write_cache_op(my_bakery_info, is_cached);
133 
134 	return my_ticket;
135 }
136 
137 void bakery_lock_get(unsigned int id, unsigned int offset)
138 {
139 	unsigned int they, me, is_cached;
140 	unsigned int my_ticket, my_prio, their_ticket;
141 	bakery_info_t *their_bakery_info;
142 	unsigned int their_bakery_data;
143 
144 	me = platform_get_core_pos(read_mpidr_el1());
145 
146 	is_cached = read_sctlr_el3() & SCTLR_C_BIT;
147 
148 	/* Get a ticket */
149 	my_ticket = bakery_get_ticket(id, offset, me, is_cached);
150 
151 	/*
152 	 * Now that we got our ticket, compute our priority value, then compare
153 	 * with that of others, and proceed to acquire the lock
154 	 */
155 	my_prio = PRIORITY(my_ticket, me);
156 	for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
157 		if (me == they)
158 			continue;
159 
160 		/*
161 		 * Get a reference to the other contender's bakery info and
162 		 * ensure that a stale copy is not read.
163 		 */
164 		their_bakery_info = get_bakery_info_by_index(offset, id, they);
165 		assert(their_bakery_info);
166 
167 		/* Wait for the contender to get their ticket */
168 		do {
169 			read_cache_op(their_bakery_info, is_cached);
170 			their_bakery_data = their_bakery_info->lock_data;
171 		} while (bakery_is_choosing(their_bakery_data));
172 
173 		/*
174 		 * If the other party is a contender, they'll have non-zero
175 		 * (valid) ticket value. If they do, compare priorities
176 		 */
177 		their_ticket = bakery_ticket_number(their_bakery_data);
178 		if (their_ticket && (PRIORITY(their_ticket, they) < my_prio)) {
179 			/*
180 			 * They have higher priority (lower value). Wait for
181 			 * their ticket value to change (either release the lock
182 			 * to have it dropped to 0; or drop and probably content
183 			 * again for the same lock to have an even higher value)
184 			 */
185 			do {
186 				wfe();
187 				read_cache_op(their_bakery_info, is_cached);
188 			} while (their_ticket
189 				== bakery_ticket_number(their_bakery_info->lock_data));
190 		}
191 	}
192 }
193 
194 void bakery_lock_release(unsigned int id, unsigned int offset)
195 {
196 	bakery_info_t *my_bakery_info;
197 	unsigned int is_cached = read_sctlr_el3() & SCTLR_C_BIT;
198 
199 	my_bakery_info = get_my_bakery_info(offset, id);
200 	my_bakery_info->lock_data = 0;
201 	write_cache_op(my_bakery_info, is_cached);
202 	sev();
203 }
204