xref: /rk3399_ARM-atf/lib/locks/bakery/bakery_lock_normal.c (revision 61531a274d3c3f47af9f5ae4c3220a4868e6a3c6)
18c5fe0b5SSoby Mathew /*
2*61531a27SSoby Mathew  * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
38c5fe0b5SSoby Mathew  *
48c5fe0b5SSoby Mathew  * Redistribution and use in source and binary forms, with or without
58c5fe0b5SSoby Mathew  * modification, are permitted provided that the following conditions are met:
68c5fe0b5SSoby Mathew  *
78c5fe0b5SSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
88c5fe0b5SSoby Mathew  * list of conditions and the following disclaimer.
98c5fe0b5SSoby Mathew  *
108c5fe0b5SSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
118c5fe0b5SSoby Mathew  * this list of conditions and the following disclaimer in the documentation
128c5fe0b5SSoby Mathew  * and/or other materials provided with the distribution.
138c5fe0b5SSoby Mathew  *
148c5fe0b5SSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
158c5fe0b5SSoby Mathew  * to endorse or promote products derived from this software without specific
168c5fe0b5SSoby Mathew  * prior written permission.
178c5fe0b5SSoby Mathew  *
188c5fe0b5SSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
198c5fe0b5SSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
208c5fe0b5SSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
218c5fe0b5SSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
228c5fe0b5SSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
238c5fe0b5SSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
248c5fe0b5SSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
258c5fe0b5SSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
268c5fe0b5SSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
278c5fe0b5SSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
288c5fe0b5SSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
298c5fe0b5SSoby Mathew  */
308c5fe0b5SSoby Mathew 
318c5fe0b5SSoby Mathew #include <arch_helpers.h>
328c5fe0b5SSoby Mathew #include <assert.h>
338c5fe0b5SSoby Mathew #include <bakery_lock.h>
348c5fe0b5SSoby Mathew #include <cpu_data.h>
358c5fe0b5SSoby Mathew #include <platform.h>
368c5fe0b5SSoby Mathew #include <string.h>
378c5fe0b5SSoby Mathew 
388c5fe0b5SSoby Mathew /*
398c5fe0b5SSoby Mathew  * Functions in this file implement Bakery Algorithm for mutual exclusion with the
408c5fe0b5SSoby Mathew  * bakery lock data structures in cacheable and Normal memory.
418c5fe0b5SSoby Mathew  *
428c5fe0b5SSoby Mathew  * ARM architecture offers a family of exclusive access instructions to
438c5fe0b5SSoby Mathew  * efficiently implement mutual exclusion with hardware support. However, as
448c5fe0b5SSoby Mathew  * well as depending on external hardware, these instructions have defined
458c5fe0b5SSoby Mathew  * behavior only on certain memory types (cacheable and Normal memory in
468c5fe0b5SSoby Mathew  * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
478c5fe0b5SSoby Mathew  * in trusted firmware are such that mutual exclusion implementation cannot
488c5fe0b5SSoby Mathew  * expect that accesses to the lock have the specific type required by the
498c5fe0b5SSoby Mathew  * architecture for these primitives to function (for example, not all
508c5fe0b5SSoby Mathew  * contenders may have address translation enabled).
518c5fe0b5SSoby Mathew  *
528c5fe0b5SSoby Mathew  * This implementation does not use mutual exclusion primitives. It expects
538c5fe0b5SSoby Mathew  * memory regions where the locks reside to be cacheable and Normal.
548c5fe0b5SSoby Mathew  *
558c5fe0b5SSoby Mathew  * Note that the ARM architecture guarantees single-copy atomicity for aligned
568c5fe0b5SSoby Mathew  * accesses regardless of status of address translation.
578c5fe0b5SSoby Mathew  */
588c5fe0b5SSoby Mathew 
59ee7b35c4SAndrew Thoelke #ifdef PLAT_PERCPU_BAKERY_LOCK_SIZE
60ee7b35c4SAndrew Thoelke /*
61ee7b35c4SAndrew Thoelke  * Verify that the platform defined value for the per-cpu space for bakery locks is
62ee7b35c4SAndrew Thoelke  * a multiple of the cache line size, to prevent multiple CPUs writing to the same
63ee7b35c4SAndrew Thoelke  * bakery lock cache line
64ee7b35c4SAndrew Thoelke  *
65ee7b35c4SAndrew Thoelke  * Using this value, if provided, rather than the linker generated value results in
66ee7b35c4SAndrew Thoelke  * more efficient code
67ee7b35c4SAndrew Thoelke  */
68ee7b35c4SAndrew Thoelke CASSERT((PLAT_PERCPU_BAKERY_LOCK_SIZE & (CACHE_WRITEBACK_GRANULE - 1)) == 0, \
69ee7b35c4SAndrew Thoelke 	PLAT_PERCPU_BAKERY_LOCK_SIZE_not_cacheline_multiple);
70ee7b35c4SAndrew Thoelke #define PERCPU_BAKERY_LOCK_SIZE (PLAT_PERCPU_BAKERY_LOCK_SIZE)
71ee7b35c4SAndrew Thoelke #else
72ee7b35c4SAndrew Thoelke /*
73ee7b35c4SAndrew Thoelke  * Use the linker defined symbol which has evaluated the size reqiurement.
74ee7b35c4SAndrew Thoelke  * This is not as efficient as using a platform defined constant
75ee7b35c4SAndrew Thoelke  */
76ee7b35c4SAndrew Thoelke extern void *__PERCPU_BAKERY_LOCK_SIZE__;
77ee7b35c4SAndrew Thoelke #define PERCPU_BAKERY_LOCK_SIZE ((uintptr_t)&__PERCPU_BAKERY_LOCK_SIZE__)
78ee7b35c4SAndrew Thoelke #endif
798c5fe0b5SSoby Mathew 
80ee7b35c4SAndrew Thoelke #define get_bakery_info(cpu_ix, lock)	\
81ee7b35c4SAndrew Thoelke 	(bakery_info_t *)((uintptr_t)lock + cpu_ix * PERCPU_BAKERY_LOCK_SIZE)
828c5fe0b5SSoby Mathew 
838c5fe0b5SSoby Mathew #define write_cache_op(addr, cached)	\
848c5fe0b5SSoby Mathew 				do {	\
854c0d0390SSoby Mathew 					(cached ? dccvac((uintptr_t)addr) :\
864c0d0390SSoby Mathew 						dcivac((uintptr_t)addr));\
878c5fe0b5SSoby Mathew 						dsbish();\
888c5fe0b5SSoby Mathew 				} while (0)
898c5fe0b5SSoby Mathew 
908c5fe0b5SSoby Mathew #define read_cache_op(addr, cached)	if (cached) \
914c0d0390SSoby Mathew 					    dccivac((uintptr_t)addr)
928c5fe0b5SSoby Mathew 
9395c12559SSoby Mathew /* Helper function to check if the lock is acquired */
9495c12559SSoby Mathew static inline int is_lock_acquired(const bakery_info_t *my_bakery_info,
9595c12559SSoby Mathew 							int is_cached)
9695c12559SSoby Mathew {
9795c12559SSoby Mathew 	/*
9895c12559SSoby Mathew 	 * Even though lock data is updated only by the owning cpu and
9995c12559SSoby Mathew 	 * appropriate cache maintenance operations are performed,
10095c12559SSoby Mathew 	 * if the previous update was done when the cpu was not participating
10195c12559SSoby Mathew 	 * in coherency, then there is a chance that cache maintenance
10295c12559SSoby Mathew 	 * operations were not propagated to all the caches in the system.
10395c12559SSoby Mathew 	 * Hence do a `read_cache_op()` prior to read.
10495c12559SSoby Mathew 	 */
10595c12559SSoby Mathew 	read_cache_op(my_bakery_info, is_cached);
10695c12559SSoby Mathew 	return !!(bakery_ticket_number(my_bakery_info->lock_data));
10795c12559SSoby Mathew }
10895c12559SSoby Mathew 
109ee7b35c4SAndrew Thoelke static unsigned int bakery_get_ticket(bakery_lock_t *lock,
1108c5fe0b5SSoby Mathew 						unsigned int me, int is_cached)
1118c5fe0b5SSoby Mathew {
1128c5fe0b5SSoby Mathew 	unsigned int my_ticket, their_ticket;
1138c5fe0b5SSoby Mathew 	unsigned int they;
1148c5fe0b5SSoby Mathew 	bakery_info_t *my_bakery_info, *their_bakery_info;
1158c5fe0b5SSoby Mathew 
1168c5fe0b5SSoby Mathew 	/*
1178c5fe0b5SSoby Mathew 	 * Obtain a reference to the bakery information for this cpu and ensure
1188c5fe0b5SSoby Mathew 	 * it is not NULL.
1198c5fe0b5SSoby Mathew 	 */
120ee7b35c4SAndrew Thoelke 	my_bakery_info = get_bakery_info(me, lock);
1218c5fe0b5SSoby Mathew 	assert(my_bakery_info);
1228c5fe0b5SSoby Mathew 
12395c12559SSoby Mathew 	/* Prevent recursive acquisition.*/
12495c12559SSoby Mathew 	assert(!is_lock_acquired(my_bakery_info, is_cached));
125548579f5SSoby Mathew 
126548579f5SSoby Mathew 	/*
1278c5fe0b5SSoby Mathew 	 * Tell other contenders that we are through the bakery doorway i.e.
1288c5fe0b5SSoby Mathew 	 * going to allocate a ticket for this cpu.
1298c5fe0b5SSoby Mathew 	 */
1308c5fe0b5SSoby Mathew 	my_ticket = 0;
1318c5fe0b5SSoby Mathew 	my_bakery_info->lock_data = make_bakery_data(CHOOSING_TICKET, my_ticket);
1328c5fe0b5SSoby Mathew 
1338c5fe0b5SSoby Mathew 	write_cache_op(my_bakery_info, is_cached);
1348c5fe0b5SSoby Mathew 
1358c5fe0b5SSoby Mathew 	/*
1368c5fe0b5SSoby Mathew 	 * Iterate through the bakery information of each contender to allocate
1378c5fe0b5SSoby Mathew 	 * the highest ticket number for this cpu.
1388c5fe0b5SSoby Mathew 	 */
1398c5fe0b5SSoby Mathew 	for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
1408c5fe0b5SSoby Mathew 		if (me == they)
1418c5fe0b5SSoby Mathew 			continue;
1428c5fe0b5SSoby Mathew 
1438c5fe0b5SSoby Mathew 		/*
1448c5fe0b5SSoby Mathew 		 * Get a reference to the other contender's bakery info and
1458c5fe0b5SSoby Mathew 		 * ensure that a stale copy is not read.
1468c5fe0b5SSoby Mathew 		 */
147ee7b35c4SAndrew Thoelke 		their_bakery_info = get_bakery_info(they, lock);
1488c5fe0b5SSoby Mathew 		assert(their_bakery_info);
1498c5fe0b5SSoby Mathew 
1508c5fe0b5SSoby Mathew 		read_cache_op(their_bakery_info, is_cached);
1518c5fe0b5SSoby Mathew 
1528c5fe0b5SSoby Mathew 		/*
1538c5fe0b5SSoby Mathew 		 * Update this cpu's ticket number if a higher ticket number is
1548c5fe0b5SSoby Mathew 		 * seen
1558c5fe0b5SSoby Mathew 		 */
1568c5fe0b5SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_info->lock_data);
1578c5fe0b5SSoby Mathew 		if (their_ticket > my_ticket)
1588c5fe0b5SSoby Mathew 			my_ticket = their_ticket;
1598c5fe0b5SSoby Mathew 	}
1608c5fe0b5SSoby Mathew 
1618c5fe0b5SSoby Mathew 	/*
1628c5fe0b5SSoby Mathew 	 * Compute ticket; then signal to other contenders waiting for us to
1638c5fe0b5SSoby Mathew 	 * finish calculating our ticket value that we're done
1648c5fe0b5SSoby Mathew 	 */
1658c5fe0b5SSoby Mathew 	++my_ticket;
1661c9573a1SSoby Mathew 	my_bakery_info->lock_data = make_bakery_data(CHOSEN_TICKET, my_ticket);
1678c5fe0b5SSoby Mathew 
1688c5fe0b5SSoby Mathew 	write_cache_op(my_bakery_info, is_cached);
1698c5fe0b5SSoby Mathew 
1708c5fe0b5SSoby Mathew 	return my_ticket;
1718c5fe0b5SSoby Mathew }
1728c5fe0b5SSoby Mathew 
173ee7b35c4SAndrew Thoelke void bakery_lock_get(bakery_lock_t *lock)
1748c5fe0b5SSoby Mathew {
1758c5fe0b5SSoby Mathew 	unsigned int they, me, is_cached;
1768c5fe0b5SSoby Mathew 	unsigned int my_ticket, my_prio, their_ticket;
1778c5fe0b5SSoby Mathew 	bakery_info_t *their_bakery_info;
1781c9573a1SSoby Mathew 	unsigned int their_bakery_data;
1798c5fe0b5SSoby Mathew 
18085a181ceSSoby Mathew 	me = plat_my_core_pos();
181*61531a27SSoby Mathew #ifdef AARCH32
182*61531a27SSoby Mathew 	is_cached = read_sctlr() & SCTLR_C_BIT;
183*61531a27SSoby Mathew #else
1848c5fe0b5SSoby Mathew 	is_cached = read_sctlr_el3() & SCTLR_C_BIT;
185*61531a27SSoby Mathew #endif
1868c5fe0b5SSoby Mathew 
1878c5fe0b5SSoby Mathew 	/* Get a ticket */
188ee7b35c4SAndrew Thoelke 	my_ticket = bakery_get_ticket(lock, me, is_cached);
1898c5fe0b5SSoby Mathew 
1908c5fe0b5SSoby Mathew 	/*
1918c5fe0b5SSoby Mathew 	 * Now that we got our ticket, compute our priority value, then compare
1928c5fe0b5SSoby Mathew 	 * with that of others, and proceed to acquire the lock
1938c5fe0b5SSoby Mathew 	 */
1948c5fe0b5SSoby Mathew 	my_prio = PRIORITY(my_ticket, me);
1958c5fe0b5SSoby Mathew 	for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
1968c5fe0b5SSoby Mathew 		if (me == they)
1978c5fe0b5SSoby Mathew 			continue;
1988c5fe0b5SSoby Mathew 
1998c5fe0b5SSoby Mathew 		/*
2008c5fe0b5SSoby Mathew 		 * Get a reference to the other contender's bakery info and
2018c5fe0b5SSoby Mathew 		 * ensure that a stale copy is not read.
2028c5fe0b5SSoby Mathew 		 */
203ee7b35c4SAndrew Thoelke 		their_bakery_info = get_bakery_info(they, lock);
2048c5fe0b5SSoby Mathew 		assert(their_bakery_info);
2058c5fe0b5SSoby Mathew 
2068c5fe0b5SSoby Mathew 		/* Wait for the contender to get their ticket */
2071c9573a1SSoby Mathew 		do {
2088c5fe0b5SSoby Mathew 			read_cache_op(their_bakery_info, is_cached);
2098c5fe0b5SSoby Mathew 			their_bakery_data = their_bakery_info->lock_data;
2101c9573a1SSoby Mathew 		} while (bakery_is_choosing(their_bakery_data));
2118c5fe0b5SSoby Mathew 
2128c5fe0b5SSoby Mathew 		/*
2138c5fe0b5SSoby Mathew 		 * If the other party is a contender, they'll have non-zero
2148c5fe0b5SSoby Mathew 		 * (valid) ticket value. If they do, compare priorities
2158c5fe0b5SSoby Mathew 		 */
2168c5fe0b5SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_data);
2178c5fe0b5SSoby Mathew 		if (their_ticket && (PRIORITY(their_ticket, they) < my_prio)) {
2188c5fe0b5SSoby Mathew 			/*
2198c5fe0b5SSoby Mathew 			 * They have higher priority (lower value). Wait for
2208c5fe0b5SSoby Mathew 			 * their ticket value to change (either release the lock
2218c5fe0b5SSoby Mathew 			 * to have it dropped to 0; or drop and probably content
2228c5fe0b5SSoby Mathew 			 * again for the same lock to have an even higher value)
2238c5fe0b5SSoby Mathew 			 */
2248c5fe0b5SSoby Mathew 			do {
2258c5fe0b5SSoby Mathew 				wfe();
2268c5fe0b5SSoby Mathew 				read_cache_op(their_bakery_info, is_cached);
2278c5fe0b5SSoby Mathew 			} while (their_ticket
2288c5fe0b5SSoby Mathew 				== bakery_ticket_number(their_bakery_info->lock_data));
2298c5fe0b5SSoby Mathew 		}
2308c5fe0b5SSoby Mathew 	}
231548579f5SSoby Mathew 	/* Lock acquired */
2328c5fe0b5SSoby Mathew }
2338c5fe0b5SSoby Mathew 
234ee7b35c4SAndrew Thoelke void bakery_lock_release(bakery_lock_t *lock)
2358c5fe0b5SSoby Mathew {
2368c5fe0b5SSoby Mathew 	bakery_info_t *my_bakery_info;
237*61531a27SSoby Mathew #ifdef AARCH32
238*61531a27SSoby Mathew 	unsigned int is_cached = read_sctlr() & SCTLR_C_BIT;
239*61531a27SSoby Mathew #else
2408c5fe0b5SSoby Mathew 	unsigned int is_cached = read_sctlr_el3() & SCTLR_C_BIT;
241*61531a27SSoby Mathew #endif
2428c5fe0b5SSoby Mathew 
243ee7b35c4SAndrew Thoelke 	my_bakery_info = get_bakery_info(plat_my_core_pos(), lock);
24495c12559SSoby Mathew 
24595c12559SSoby Mathew 	assert(is_lock_acquired(my_bakery_info, is_cached));
246548579f5SSoby Mathew 
2478c5fe0b5SSoby Mathew 	my_bakery_info->lock_data = 0;
2488c5fe0b5SSoby Mathew 	write_cache_op(my_bakery_info, is_cached);
2498c5fe0b5SSoby Mathew 	sev();
2508c5fe0b5SSoby Mathew }
251