xref: /rk3399_ARM-atf/lib/locks/bakery/bakery_lock_normal.c (revision 402b3cf8766fe2cb4ae462f7ee7761d08a1ba56c)
18c5fe0b5SSoby Mathew /*
29f85f9e3SJoel Hutton  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
38c5fe0b5SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
58c5fe0b5SSoby Mathew  */
68c5fe0b5SSoby Mathew 
78c5fe0b5SSoby Mathew #include <assert.h>
88c5fe0b5SSoby Mathew #include <string.h>
909d40e0eSAntonio Nino Diaz 
1009d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1109d40e0eSAntonio Nino Diaz #include <lib/bakery_lock.h>
1209d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h>
1309d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
1409d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
158c5fe0b5SSoby Mathew 
168c5fe0b5SSoby Mathew /*
178c5fe0b5SSoby Mathew  * Functions in this file implement Bakery Algorithm for mutual exclusion with the
188c5fe0b5SSoby Mathew  * bakery lock data structures in cacheable and Normal memory.
198c5fe0b5SSoby Mathew  *
208c5fe0b5SSoby Mathew  * ARM architecture offers a family of exclusive access instructions to
218c5fe0b5SSoby Mathew  * efficiently implement mutual exclusion with hardware support. However, as
228c5fe0b5SSoby Mathew  * well as depending on external hardware, these instructions have defined
238c5fe0b5SSoby Mathew  * behavior only on certain memory types (cacheable and Normal memory in
248c5fe0b5SSoby Mathew  * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
258c5fe0b5SSoby Mathew  * in trusted firmware are such that mutual exclusion implementation cannot
268c5fe0b5SSoby Mathew  * expect that accesses to the lock have the specific type required by the
278c5fe0b5SSoby Mathew  * architecture for these primitives to function (for example, not all
288c5fe0b5SSoby Mathew  * contenders may have address translation enabled).
298c5fe0b5SSoby Mathew  *
308c5fe0b5SSoby Mathew  * This implementation does not use mutual exclusion primitives. It expects
318c5fe0b5SSoby Mathew  * memory regions where the locks reside to be cacheable and Normal.
328c5fe0b5SSoby Mathew  *
338c5fe0b5SSoby Mathew  * Note that the ARM architecture guarantees single-copy atomicity for aligned
348c5fe0b5SSoby Mathew  * accesses regardless of status of address translation.
358c5fe0b5SSoby Mathew  */
368c5fe0b5SSoby Mathew 
37ee7b35c4SAndrew Thoelke #ifdef PLAT_PERCPU_BAKERY_LOCK_SIZE
38ee7b35c4SAndrew Thoelke /*
39ee7b35c4SAndrew Thoelke  * Verify that the platform defined value for the per-cpu space for bakery locks is
40ee7b35c4SAndrew Thoelke  * a multiple of the cache line size, to prevent multiple CPUs writing to the same
41ee7b35c4SAndrew Thoelke  * bakery lock cache line
42ee7b35c4SAndrew Thoelke  *
43ee7b35c4SAndrew Thoelke  * Using this value, if provided, rather than the linker generated value results in
44ee7b35c4SAndrew Thoelke  * more efficient code
45ee7b35c4SAndrew Thoelke  */
46ee7b35c4SAndrew Thoelke CASSERT((PLAT_PERCPU_BAKERY_LOCK_SIZE & (CACHE_WRITEBACK_GRANULE - 1)) == 0, \
47ee7b35c4SAndrew Thoelke 	PLAT_PERCPU_BAKERY_LOCK_SIZE_not_cacheline_multiple);
48ee7b35c4SAndrew Thoelke #define PERCPU_BAKERY_LOCK_SIZE (PLAT_PERCPU_BAKERY_LOCK_SIZE)
49ee7b35c4SAndrew Thoelke #else
50ee7b35c4SAndrew Thoelke /*
51ee7b35c4SAndrew Thoelke  * Use the linker defined symbol which has evaluated the size reqiurement.
52ee7b35c4SAndrew Thoelke  * This is not as efficient as using a platform defined constant
53ee7b35c4SAndrew Thoelke  */
54596929b9SVarun Wadekar IMPORT_SYM(uintptr_t, __PERCPU_BAKERY_LOCK_START__, BAKERY_LOCK_START);
55596929b9SVarun Wadekar IMPORT_SYM(uintptr_t, __PERCPU_BAKERY_LOCK_END__, BAKERY_LOCK_END);
56596929b9SVarun Wadekar #define PERCPU_BAKERY_LOCK_SIZE (BAKERY_LOCK_END - BAKERY_LOCK_START)
57ee7b35c4SAndrew Thoelke #endif
588c5fe0b5SSoby Mathew 
59f8b30ca8SAntonio Nino Diaz static inline bakery_lock_t *get_bakery_info(unsigned int cpu_ix,
60f8b30ca8SAntonio Nino Diaz 					     bakery_lock_t *lock)
61f8b30ca8SAntonio Nino Diaz {
62f8b30ca8SAntonio Nino Diaz 	return (bakery_info_t *)((uintptr_t)lock +
63f8b30ca8SAntonio Nino Diaz 				cpu_ix * PERCPU_BAKERY_LOCK_SIZE);
64f8b30ca8SAntonio Nino Diaz }
658c5fe0b5SSoby Mathew 
66f8b30ca8SAntonio Nino Diaz static inline void write_cache_op(uintptr_t addr, bool cached)
67f8b30ca8SAntonio Nino Diaz {
68f8b30ca8SAntonio Nino Diaz 	if (cached)
69f8b30ca8SAntonio Nino Diaz 		dccvac(addr);
70f8b30ca8SAntonio Nino Diaz 	else
71f8b30ca8SAntonio Nino Diaz 		dcivac(addr);
728c5fe0b5SSoby Mathew 
73f8b30ca8SAntonio Nino Diaz 	dsbish();
74f8b30ca8SAntonio Nino Diaz }
75f8b30ca8SAntonio Nino Diaz 
76f8b30ca8SAntonio Nino Diaz static inline void read_cache_op(uintptr_t addr, bool cached)
77f8b30ca8SAntonio Nino Diaz {
78f8b30ca8SAntonio Nino Diaz 	if (cached)
79f8b30ca8SAntonio Nino Diaz 		dccivac(addr);
80f8b30ca8SAntonio Nino Diaz }
818c5fe0b5SSoby Mathew 
8295c12559SSoby Mathew /* Helper function to check if the lock is acquired */
83f8b30ca8SAntonio Nino Diaz static inline bool is_lock_acquired(const bakery_info_t *my_bakery_info,
8495c12559SSoby Mathew 							int is_cached)
8595c12559SSoby Mathew {
8695c12559SSoby Mathew 	/*
8795c12559SSoby Mathew 	 * Even though lock data is updated only by the owning cpu and
8895c12559SSoby Mathew 	 * appropriate cache maintenance operations are performed,
8995c12559SSoby Mathew 	 * if the previous update was done when the cpu was not participating
9095c12559SSoby Mathew 	 * in coherency, then there is a chance that cache maintenance
9195c12559SSoby Mathew 	 * operations were not propagated to all the caches in the system.
9295c12559SSoby Mathew 	 * Hence do a `read_cache_op()` prior to read.
9395c12559SSoby Mathew 	 */
94f8b30ca8SAntonio Nino Diaz 	read_cache_op((uintptr_t)my_bakery_info, is_cached);
95f8b30ca8SAntonio Nino Diaz 	return bakery_ticket_number(my_bakery_info->lock_data) != 0U;
9695c12559SSoby Mathew }
9795c12559SSoby Mathew 
98ee7b35c4SAndrew Thoelke static unsigned int bakery_get_ticket(bakery_lock_t *lock,
998c5fe0b5SSoby Mathew 						unsigned int me, int is_cached)
1008c5fe0b5SSoby Mathew {
1018c5fe0b5SSoby Mathew 	unsigned int my_ticket, their_ticket;
1028c5fe0b5SSoby Mathew 	unsigned int they;
1038c5fe0b5SSoby Mathew 	bakery_info_t *my_bakery_info, *their_bakery_info;
1048c5fe0b5SSoby Mathew 
1058c5fe0b5SSoby Mathew 	/*
1068c5fe0b5SSoby Mathew 	 * Obtain a reference to the bakery information for this cpu and ensure
1078c5fe0b5SSoby Mathew 	 * it is not NULL.
1088c5fe0b5SSoby Mathew 	 */
109ee7b35c4SAndrew Thoelke 	my_bakery_info = get_bakery_info(me, lock);
110f8b30ca8SAntonio Nino Diaz 	assert(my_bakery_info != NULL);
1118c5fe0b5SSoby Mathew 
11295c12559SSoby Mathew 	/* Prevent recursive acquisition.*/
11395c12559SSoby Mathew 	assert(!is_lock_acquired(my_bakery_info, is_cached));
114548579f5SSoby Mathew 
115548579f5SSoby Mathew 	/*
1168c5fe0b5SSoby Mathew 	 * Tell other contenders that we are through the bakery doorway i.e.
1178c5fe0b5SSoby Mathew 	 * going to allocate a ticket for this cpu.
1188c5fe0b5SSoby Mathew 	 */
119f8b30ca8SAntonio Nino Diaz 	my_ticket = 0U;
1208c5fe0b5SSoby Mathew 	my_bakery_info->lock_data = make_bakery_data(CHOOSING_TICKET, my_ticket);
1218c5fe0b5SSoby Mathew 
122f8b30ca8SAntonio Nino Diaz 	write_cache_op((uintptr_t)my_bakery_info, is_cached);
1238c5fe0b5SSoby Mathew 
1248c5fe0b5SSoby Mathew 	/*
1258c5fe0b5SSoby Mathew 	 * Iterate through the bakery information of each contender to allocate
1268c5fe0b5SSoby Mathew 	 * the highest ticket number for this cpu.
1278c5fe0b5SSoby Mathew 	 */
128f8b30ca8SAntonio Nino Diaz 	for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
1298c5fe0b5SSoby Mathew 		if (me == they)
1308c5fe0b5SSoby Mathew 			continue;
1318c5fe0b5SSoby Mathew 
1328c5fe0b5SSoby Mathew 		/*
1338c5fe0b5SSoby Mathew 		 * Get a reference to the other contender's bakery info and
1348c5fe0b5SSoby Mathew 		 * ensure that a stale copy is not read.
1358c5fe0b5SSoby Mathew 		 */
136ee7b35c4SAndrew Thoelke 		their_bakery_info = get_bakery_info(they, lock);
137f8b30ca8SAntonio Nino Diaz 		assert(their_bakery_info != NULL);
1388c5fe0b5SSoby Mathew 
139f8b30ca8SAntonio Nino Diaz 		read_cache_op((uintptr_t)their_bakery_info, is_cached);
1408c5fe0b5SSoby Mathew 
1418c5fe0b5SSoby Mathew 		/*
1428c5fe0b5SSoby Mathew 		 * Update this cpu's ticket number if a higher ticket number is
1438c5fe0b5SSoby Mathew 		 * seen
1448c5fe0b5SSoby Mathew 		 */
1458c5fe0b5SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_info->lock_data);
1468c5fe0b5SSoby Mathew 		if (their_ticket > my_ticket)
1478c5fe0b5SSoby Mathew 			my_ticket = their_ticket;
1488c5fe0b5SSoby Mathew 	}
1498c5fe0b5SSoby Mathew 
1508c5fe0b5SSoby Mathew 	/*
1518c5fe0b5SSoby Mathew 	 * Compute ticket; then signal to other contenders waiting for us to
1528c5fe0b5SSoby Mathew 	 * finish calculating our ticket value that we're done
1538c5fe0b5SSoby Mathew 	 */
1548c5fe0b5SSoby Mathew 	++my_ticket;
1551c9573a1SSoby Mathew 	my_bakery_info->lock_data = make_bakery_data(CHOSEN_TICKET, my_ticket);
1568c5fe0b5SSoby Mathew 
157f8b30ca8SAntonio Nino Diaz 	write_cache_op((uintptr_t)my_bakery_info, is_cached);
1588c5fe0b5SSoby Mathew 
1598c5fe0b5SSoby Mathew 	return my_ticket;
1608c5fe0b5SSoby Mathew }
1618c5fe0b5SSoby Mathew 
162ee7b35c4SAndrew Thoelke void bakery_lock_get(bakery_lock_t *lock)
1638c5fe0b5SSoby Mathew {
1648c5fe0b5SSoby Mathew 	unsigned int they, me, is_cached;
1658c5fe0b5SSoby Mathew 	unsigned int my_ticket, my_prio, their_ticket;
1668c5fe0b5SSoby Mathew 	bakery_info_t *their_bakery_info;
1671c9573a1SSoby Mathew 	unsigned int their_bakery_data;
1688c5fe0b5SSoby Mathew 
16985a181ceSSoby Mathew 	me = plat_my_core_pos();
170*402b3cf8SJulius Werner #ifdef __aarch64__
1718c5fe0b5SSoby Mathew 	is_cached = read_sctlr_el3() & SCTLR_C_BIT;
172*402b3cf8SJulius Werner #else
173*402b3cf8SJulius Werner 	is_cached = read_sctlr() & SCTLR_C_BIT;
17461531a27SSoby Mathew #endif
1758c5fe0b5SSoby Mathew 
1768c5fe0b5SSoby Mathew 	/* Get a ticket */
177ee7b35c4SAndrew Thoelke 	my_ticket = bakery_get_ticket(lock, me, is_cached);
1788c5fe0b5SSoby Mathew 
1798c5fe0b5SSoby Mathew 	/*
1808c5fe0b5SSoby Mathew 	 * Now that we got our ticket, compute our priority value, then compare
1818c5fe0b5SSoby Mathew 	 * with that of others, and proceed to acquire the lock
1828c5fe0b5SSoby Mathew 	 */
183f8b30ca8SAntonio Nino Diaz 	my_prio = bakery_get_priority(my_ticket, me);
184f8b30ca8SAntonio Nino Diaz 	for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
1858c5fe0b5SSoby Mathew 		if (me == they)
1868c5fe0b5SSoby Mathew 			continue;
1878c5fe0b5SSoby Mathew 
1888c5fe0b5SSoby Mathew 		/*
1898c5fe0b5SSoby Mathew 		 * Get a reference to the other contender's bakery info and
1908c5fe0b5SSoby Mathew 		 * ensure that a stale copy is not read.
1918c5fe0b5SSoby Mathew 		 */
192ee7b35c4SAndrew Thoelke 		their_bakery_info = get_bakery_info(they, lock);
193f8b30ca8SAntonio Nino Diaz 		assert(their_bakery_info != NULL);
1948c5fe0b5SSoby Mathew 
1958c5fe0b5SSoby Mathew 		/* Wait for the contender to get their ticket */
1961c9573a1SSoby Mathew 		do {
197f8b30ca8SAntonio Nino Diaz 			read_cache_op((uintptr_t)their_bakery_info, is_cached);
1988c5fe0b5SSoby Mathew 			their_bakery_data = their_bakery_info->lock_data;
1991c9573a1SSoby Mathew 		} while (bakery_is_choosing(their_bakery_data));
2008c5fe0b5SSoby Mathew 
2018c5fe0b5SSoby Mathew 		/*
2028c5fe0b5SSoby Mathew 		 * If the other party is a contender, they'll have non-zero
2038c5fe0b5SSoby Mathew 		 * (valid) ticket value. If they do, compare priorities
2048c5fe0b5SSoby Mathew 		 */
2058c5fe0b5SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_data);
206f8b30ca8SAntonio Nino Diaz 		if (their_ticket && (bakery_get_priority(their_ticket, they) < my_prio)) {
2078c5fe0b5SSoby Mathew 			/*
2088c5fe0b5SSoby Mathew 			 * They have higher priority (lower value). Wait for
2098c5fe0b5SSoby Mathew 			 * their ticket value to change (either release the lock
2108c5fe0b5SSoby Mathew 			 * to have it dropped to 0; or drop and probably content
2118c5fe0b5SSoby Mathew 			 * again for the same lock to have an even higher value)
2128c5fe0b5SSoby Mathew 			 */
2138c5fe0b5SSoby Mathew 			do {
2148c5fe0b5SSoby Mathew 				wfe();
215f8b30ca8SAntonio Nino Diaz 				read_cache_op((uintptr_t)their_bakery_info, is_cached);
2168c5fe0b5SSoby Mathew 			} while (their_ticket
2178c5fe0b5SSoby Mathew 				== bakery_ticket_number(their_bakery_info->lock_data));
2188c5fe0b5SSoby Mathew 		}
2198c5fe0b5SSoby Mathew 	}
22024dc9709SJeenu Viswambharan 
22124dc9709SJeenu Viswambharan 	/*
22224dc9709SJeenu Viswambharan 	 * Lock acquired. Ensure that any reads from a shared resource in the
22324dc9709SJeenu Viswambharan 	 * critical section read values after the lock is acquired.
22424dc9709SJeenu Viswambharan 	 */
22524dc9709SJeenu Viswambharan 	dmbld();
2268c5fe0b5SSoby Mathew }
2278c5fe0b5SSoby Mathew 
228ee7b35c4SAndrew Thoelke void bakery_lock_release(bakery_lock_t *lock)
2298c5fe0b5SSoby Mathew {
2308c5fe0b5SSoby Mathew 	bakery_info_t *my_bakery_info;
231*402b3cf8SJulius Werner #ifdef __aarch64__
2328c5fe0b5SSoby Mathew 	unsigned int is_cached = read_sctlr_el3() & SCTLR_C_BIT;
233*402b3cf8SJulius Werner #else
234*402b3cf8SJulius Werner 	unsigned int is_cached = read_sctlr() & SCTLR_C_BIT;
23561531a27SSoby Mathew #endif
2368c5fe0b5SSoby Mathew 
237ee7b35c4SAndrew Thoelke 	my_bakery_info = get_bakery_info(plat_my_core_pos(), lock);
23895c12559SSoby Mathew 
23995c12559SSoby Mathew 	assert(is_lock_acquired(my_bakery_info, is_cached));
240548579f5SSoby Mathew 
24124dc9709SJeenu Viswambharan 	/*
24224dc9709SJeenu Viswambharan 	 * Ensure that other observers see any stores in the critical section
24324dc9709SJeenu Viswambharan 	 * before releasing the lock. Release the lock by resetting ticket.
24424dc9709SJeenu Viswambharan 	 * Then signal other waiting contenders.
24524dc9709SJeenu Viswambharan 	 */
24624dc9709SJeenu Viswambharan 	dmbst();
247f8b30ca8SAntonio Nino Diaz 	my_bakery_info->lock_data = 0U;
248f8b30ca8SAntonio Nino Diaz 	write_cache_op((uintptr_t)my_bakery_info, is_cached);
2498c5fe0b5SSoby Mathew 	sev();
2508c5fe0b5SSoby Mathew }
251