xref: /rk3399_ARM-atf/lib/locks/bakery/bakery_lock_normal.c (revision f8b30ca89b3e3a09d96019bf3998255a3f7a4c8a)
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 <arch_helpers.h>
88c5fe0b5SSoby Mathew #include <assert.h>
98c5fe0b5SSoby Mathew #include <bakery_lock.h>
108c5fe0b5SSoby Mathew #include <cpu_data.h>
118c5fe0b5SSoby Mathew #include <platform.h>
128c5fe0b5SSoby Mathew #include <string.h>
139f85f9e3SJoel Hutton #include <utils_def.h>
148c5fe0b5SSoby Mathew 
158c5fe0b5SSoby Mathew /*
168c5fe0b5SSoby Mathew  * Functions in this file implement Bakery Algorithm for mutual exclusion with the
178c5fe0b5SSoby Mathew  * bakery lock data structures in cacheable and Normal memory.
188c5fe0b5SSoby Mathew  *
198c5fe0b5SSoby Mathew  * ARM architecture offers a family of exclusive access instructions to
208c5fe0b5SSoby Mathew  * efficiently implement mutual exclusion with hardware support. However, as
218c5fe0b5SSoby Mathew  * well as depending on external hardware, these instructions have defined
228c5fe0b5SSoby Mathew  * behavior only on certain memory types (cacheable and Normal memory in
238c5fe0b5SSoby Mathew  * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
248c5fe0b5SSoby Mathew  * in trusted firmware are such that mutual exclusion implementation cannot
258c5fe0b5SSoby Mathew  * expect that accesses to the lock have the specific type required by the
268c5fe0b5SSoby Mathew  * architecture for these primitives to function (for example, not all
278c5fe0b5SSoby Mathew  * contenders may have address translation enabled).
288c5fe0b5SSoby Mathew  *
298c5fe0b5SSoby Mathew  * This implementation does not use mutual exclusion primitives. It expects
308c5fe0b5SSoby Mathew  * memory regions where the locks reside to be cacheable and Normal.
318c5fe0b5SSoby Mathew  *
328c5fe0b5SSoby Mathew  * Note that the ARM architecture guarantees single-copy atomicity for aligned
338c5fe0b5SSoby Mathew  * accesses regardless of status of address translation.
348c5fe0b5SSoby Mathew  */
358c5fe0b5SSoby Mathew 
36ee7b35c4SAndrew Thoelke #ifdef PLAT_PERCPU_BAKERY_LOCK_SIZE
37ee7b35c4SAndrew Thoelke /*
38ee7b35c4SAndrew Thoelke  * Verify that the platform defined value for the per-cpu space for bakery locks is
39ee7b35c4SAndrew Thoelke  * a multiple of the cache line size, to prevent multiple CPUs writing to the same
40ee7b35c4SAndrew Thoelke  * bakery lock cache line
41ee7b35c4SAndrew Thoelke  *
42ee7b35c4SAndrew Thoelke  * Using this value, if provided, rather than the linker generated value results in
43ee7b35c4SAndrew Thoelke  * more efficient code
44ee7b35c4SAndrew Thoelke  */
45ee7b35c4SAndrew Thoelke CASSERT((PLAT_PERCPU_BAKERY_LOCK_SIZE & (CACHE_WRITEBACK_GRANULE - 1)) == 0, \
46ee7b35c4SAndrew Thoelke 	PLAT_PERCPU_BAKERY_LOCK_SIZE_not_cacheline_multiple);
47ee7b35c4SAndrew Thoelke #define PERCPU_BAKERY_LOCK_SIZE (PLAT_PERCPU_BAKERY_LOCK_SIZE)
48ee7b35c4SAndrew Thoelke #else
49ee7b35c4SAndrew Thoelke /*
50ee7b35c4SAndrew Thoelke  * Use the linker defined symbol which has evaluated the size reqiurement.
51ee7b35c4SAndrew Thoelke  * This is not as efficient as using a platform defined constant
52ee7b35c4SAndrew Thoelke  */
539f85f9e3SJoel Hutton IMPORT_SYM(uintptr_t, __PERCPU_BAKERY_LOCK_SIZE__, PERCPU_BAKERY_LOCK_SIZE);
54ee7b35c4SAndrew Thoelke #endif
558c5fe0b5SSoby Mathew 
56*f8b30ca8SAntonio Nino Diaz static inline bakery_lock_t *get_bakery_info(unsigned int cpu_ix,
57*f8b30ca8SAntonio Nino Diaz 					     bakery_lock_t *lock)
58*f8b30ca8SAntonio Nino Diaz {
59*f8b30ca8SAntonio Nino Diaz 	return (bakery_info_t *)((uintptr_t)lock +
60*f8b30ca8SAntonio Nino Diaz 				cpu_ix * PERCPU_BAKERY_LOCK_SIZE);
61*f8b30ca8SAntonio Nino Diaz }
628c5fe0b5SSoby Mathew 
63*f8b30ca8SAntonio Nino Diaz static inline void write_cache_op(uintptr_t addr, bool cached)
64*f8b30ca8SAntonio Nino Diaz {
65*f8b30ca8SAntonio Nino Diaz 	if (cached)
66*f8b30ca8SAntonio Nino Diaz 		dccvac(addr);
67*f8b30ca8SAntonio Nino Diaz 	else
68*f8b30ca8SAntonio Nino Diaz 		dcivac(addr);
698c5fe0b5SSoby Mathew 
70*f8b30ca8SAntonio Nino Diaz 	dsbish();
71*f8b30ca8SAntonio Nino Diaz }
72*f8b30ca8SAntonio Nino Diaz 
73*f8b30ca8SAntonio Nino Diaz static inline void read_cache_op(uintptr_t addr, bool cached)
74*f8b30ca8SAntonio Nino Diaz {
75*f8b30ca8SAntonio Nino Diaz 	if (cached)
76*f8b30ca8SAntonio Nino Diaz 		dccivac(addr);
77*f8b30ca8SAntonio Nino Diaz }
788c5fe0b5SSoby Mathew 
7995c12559SSoby Mathew /* Helper function to check if the lock is acquired */
80*f8b30ca8SAntonio Nino Diaz static inline bool is_lock_acquired(const bakery_info_t *my_bakery_info,
8195c12559SSoby Mathew 							int is_cached)
8295c12559SSoby Mathew {
8395c12559SSoby Mathew 	/*
8495c12559SSoby Mathew 	 * Even though lock data is updated only by the owning cpu and
8595c12559SSoby Mathew 	 * appropriate cache maintenance operations are performed,
8695c12559SSoby Mathew 	 * if the previous update was done when the cpu was not participating
8795c12559SSoby Mathew 	 * in coherency, then there is a chance that cache maintenance
8895c12559SSoby Mathew 	 * operations were not propagated to all the caches in the system.
8995c12559SSoby Mathew 	 * Hence do a `read_cache_op()` prior to read.
9095c12559SSoby Mathew 	 */
91*f8b30ca8SAntonio Nino Diaz 	read_cache_op((uintptr_t)my_bakery_info, is_cached);
92*f8b30ca8SAntonio Nino Diaz 	return bakery_ticket_number(my_bakery_info->lock_data) != 0U;
9395c12559SSoby Mathew }
9495c12559SSoby Mathew 
95ee7b35c4SAndrew Thoelke static unsigned int bakery_get_ticket(bakery_lock_t *lock,
968c5fe0b5SSoby Mathew 						unsigned int me, int is_cached)
978c5fe0b5SSoby Mathew {
988c5fe0b5SSoby Mathew 	unsigned int my_ticket, their_ticket;
998c5fe0b5SSoby Mathew 	unsigned int they;
1008c5fe0b5SSoby Mathew 	bakery_info_t *my_bakery_info, *their_bakery_info;
1018c5fe0b5SSoby Mathew 
1028c5fe0b5SSoby Mathew 	/*
1038c5fe0b5SSoby Mathew 	 * Obtain a reference to the bakery information for this cpu and ensure
1048c5fe0b5SSoby Mathew 	 * it is not NULL.
1058c5fe0b5SSoby Mathew 	 */
106ee7b35c4SAndrew Thoelke 	my_bakery_info = get_bakery_info(me, lock);
107*f8b30ca8SAntonio Nino Diaz 	assert(my_bakery_info != NULL);
1088c5fe0b5SSoby Mathew 
10995c12559SSoby Mathew 	/* Prevent recursive acquisition.*/
11095c12559SSoby Mathew 	assert(!is_lock_acquired(my_bakery_info, is_cached));
111548579f5SSoby Mathew 
112548579f5SSoby Mathew 	/*
1138c5fe0b5SSoby Mathew 	 * Tell other contenders that we are through the bakery doorway i.e.
1148c5fe0b5SSoby Mathew 	 * going to allocate a ticket for this cpu.
1158c5fe0b5SSoby Mathew 	 */
116*f8b30ca8SAntonio Nino Diaz 	my_ticket = 0U;
1178c5fe0b5SSoby Mathew 	my_bakery_info->lock_data = make_bakery_data(CHOOSING_TICKET, my_ticket);
1188c5fe0b5SSoby Mathew 
119*f8b30ca8SAntonio Nino Diaz 	write_cache_op((uintptr_t)my_bakery_info, is_cached);
1208c5fe0b5SSoby Mathew 
1218c5fe0b5SSoby Mathew 	/*
1228c5fe0b5SSoby Mathew 	 * Iterate through the bakery information of each contender to allocate
1238c5fe0b5SSoby Mathew 	 * the highest ticket number for this cpu.
1248c5fe0b5SSoby Mathew 	 */
125*f8b30ca8SAntonio Nino Diaz 	for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
1268c5fe0b5SSoby Mathew 		if (me == they)
1278c5fe0b5SSoby Mathew 			continue;
1288c5fe0b5SSoby Mathew 
1298c5fe0b5SSoby Mathew 		/*
1308c5fe0b5SSoby Mathew 		 * Get a reference to the other contender's bakery info and
1318c5fe0b5SSoby Mathew 		 * ensure that a stale copy is not read.
1328c5fe0b5SSoby Mathew 		 */
133ee7b35c4SAndrew Thoelke 		their_bakery_info = get_bakery_info(they, lock);
134*f8b30ca8SAntonio Nino Diaz 		assert(their_bakery_info != NULL);
1358c5fe0b5SSoby Mathew 
136*f8b30ca8SAntonio Nino Diaz 		read_cache_op((uintptr_t)their_bakery_info, is_cached);
1378c5fe0b5SSoby Mathew 
1388c5fe0b5SSoby Mathew 		/*
1398c5fe0b5SSoby Mathew 		 * Update this cpu's ticket number if a higher ticket number is
1408c5fe0b5SSoby Mathew 		 * seen
1418c5fe0b5SSoby Mathew 		 */
1428c5fe0b5SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_info->lock_data);
1438c5fe0b5SSoby Mathew 		if (their_ticket > my_ticket)
1448c5fe0b5SSoby Mathew 			my_ticket = their_ticket;
1458c5fe0b5SSoby Mathew 	}
1468c5fe0b5SSoby Mathew 
1478c5fe0b5SSoby Mathew 	/*
1488c5fe0b5SSoby Mathew 	 * Compute ticket; then signal to other contenders waiting for us to
1498c5fe0b5SSoby Mathew 	 * finish calculating our ticket value that we're done
1508c5fe0b5SSoby Mathew 	 */
1518c5fe0b5SSoby Mathew 	++my_ticket;
1521c9573a1SSoby Mathew 	my_bakery_info->lock_data = make_bakery_data(CHOSEN_TICKET, my_ticket);
1538c5fe0b5SSoby Mathew 
154*f8b30ca8SAntonio Nino Diaz 	write_cache_op((uintptr_t)my_bakery_info, is_cached);
1558c5fe0b5SSoby Mathew 
1568c5fe0b5SSoby Mathew 	return my_ticket;
1578c5fe0b5SSoby Mathew }
1588c5fe0b5SSoby Mathew 
159ee7b35c4SAndrew Thoelke void bakery_lock_get(bakery_lock_t *lock)
1608c5fe0b5SSoby Mathew {
1618c5fe0b5SSoby Mathew 	unsigned int they, me, is_cached;
1628c5fe0b5SSoby Mathew 	unsigned int my_ticket, my_prio, their_ticket;
1638c5fe0b5SSoby Mathew 	bakery_info_t *their_bakery_info;
1641c9573a1SSoby Mathew 	unsigned int their_bakery_data;
1658c5fe0b5SSoby Mathew 
16685a181ceSSoby Mathew 	me = plat_my_core_pos();
16761531a27SSoby Mathew #ifdef AARCH32
16861531a27SSoby Mathew 	is_cached = read_sctlr() & SCTLR_C_BIT;
16961531a27SSoby Mathew #else
1708c5fe0b5SSoby Mathew 	is_cached = read_sctlr_el3() & SCTLR_C_BIT;
17161531a27SSoby Mathew #endif
1728c5fe0b5SSoby Mathew 
1738c5fe0b5SSoby Mathew 	/* Get a ticket */
174ee7b35c4SAndrew Thoelke 	my_ticket = bakery_get_ticket(lock, me, is_cached);
1758c5fe0b5SSoby Mathew 
1768c5fe0b5SSoby Mathew 	/*
1778c5fe0b5SSoby Mathew 	 * Now that we got our ticket, compute our priority value, then compare
1788c5fe0b5SSoby Mathew 	 * with that of others, and proceed to acquire the lock
1798c5fe0b5SSoby Mathew 	 */
180*f8b30ca8SAntonio Nino Diaz 	my_prio = bakery_get_priority(my_ticket, me);
181*f8b30ca8SAntonio Nino Diaz 	for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
1828c5fe0b5SSoby Mathew 		if (me == they)
1838c5fe0b5SSoby Mathew 			continue;
1848c5fe0b5SSoby Mathew 
1858c5fe0b5SSoby Mathew 		/*
1868c5fe0b5SSoby Mathew 		 * Get a reference to the other contender's bakery info and
1878c5fe0b5SSoby Mathew 		 * ensure that a stale copy is not read.
1888c5fe0b5SSoby Mathew 		 */
189ee7b35c4SAndrew Thoelke 		their_bakery_info = get_bakery_info(they, lock);
190*f8b30ca8SAntonio Nino Diaz 		assert(their_bakery_info != NULL);
1918c5fe0b5SSoby Mathew 
1928c5fe0b5SSoby Mathew 		/* Wait for the contender to get their ticket */
1931c9573a1SSoby Mathew 		do {
194*f8b30ca8SAntonio Nino Diaz 			read_cache_op((uintptr_t)their_bakery_info, is_cached);
1958c5fe0b5SSoby Mathew 			their_bakery_data = their_bakery_info->lock_data;
1961c9573a1SSoby Mathew 		} while (bakery_is_choosing(their_bakery_data));
1978c5fe0b5SSoby Mathew 
1988c5fe0b5SSoby Mathew 		/*
1998c5fe0b5SSoby Mathew 		 * If the other party is a contender, they'll have non-zero
2008c5fe0b5SSoby Mathew 		 * (valid) ticket value. If they do, compare priorities
2018c5fe0b5SSoby Mathew 		 */
2028c5fe0b5SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_data);
203*f8b30ca8SAntonio Nino Diaz 		if (their_ticket && (bakery_get_priority(their_ticket, they) < my_prio)) {
2048c5fe0b5SSoby Mathew 			/*
2058c5fe0b5SSoby Mathew 			 * They have higher priority (lower value). Wait for
2068c5fe0b5SSoby Mathew 			 * their ticket value to change (either release the lock
2078c5fe0b5SSoby Mathew 			 * to have it dropped to 0; or drop and probably content
2088c5fe0b5SSoby Mathew 			 * again for the same lock to have an even higher value)
2098c5fe0b5SSoby Mathew 			 */
2108c5fe0b5SSoby Mathew 			do {
2118c5fe0b5SSoby Mathew 				wfe();
212*f8b30ca8SAntonio Nino Diaz 				read_cache_op((uintptr_t)their_bakery_info, is_cached);
2138c5fe0b5SSoby Mathew 			} while (their_ticket
2148c5fe0b5SSoby Mathew 				== bakery_ticket_number(their_bakery_info->lock_data));
2158c5fe0b5SSoby Mathew 		}
2168c5fe0b5SSoby Mathew 	}
21724dc9709SJeenu Viswambharan 
21824dc9709SJeenu Viswambharan 	/*
21924dc9709SJeenu Viswambharan 	 * Lock acquired. Ensure that any reads from a shared resource in the
22024dc9709SJeenu Viswambharan 	 * critical section read values after the lock is acquired.
22124dc9709SJeenu Viswambharan 	 */
22224dc9709SJeenu Viswambharan 	dmbld();
2238c5fe0b5SSoby Mathew }
2248c5fe0b5SSoby Mathew 
225ee7b35c4SAndrew Thoelke void bakery_lock_release(bakery_lock_t *lock)
2268c5fe0b5SSoby Mathew {
2278c5fe0b5SSoby Mathew 	bakery_info_t *my_bakery_info;
22861531a27SSoby Mathew #ifdef AARCH32
22961531a27SSoby Mathew 	unsigned int is_cached = read_sctlr() & SCTLR_C_BIT;
23061531a27SSoby Mathew #else
2318c5fe0b5SSoby Mathew 	unsigned int is_cached = read_sctlr_el3() & SCTLR_C_BIT;
23261531a27SSoby Mathew #endif
2338c5fe0b5SSoby Mathew 
234ee7b35c4SAndrew Thoelke 	my_bakery_info = get_bakery_info(plat_my_core_pos(), lock);
23595c12559SSoby Mathew 
23695c12559SSoby Mathew 	assert(is_lock_acquired(my_bakery_info, is_cached));
237548579f5SSoby Mathew 
23824dc9709SJeenu Viswambharan 	/*
23924dc9709SJeenu Viswambharan 	 * Ensure that other observers see any stores in the critical section
24024dc9709SJeenu Viswambharan 	 * before releasing the lock. Release the lock by resetting ticket.
24124dc9709SJeenu Viswambharan 	 * Then signal other waiting contenders.
24224dc9709SJeenu Viswambharan 	 */
24324dc9709SJeenu Viswambharan 	dmbst();
244*f8b30ca8SAntonio Nino Diaz 	my_bakery_info->lock_data = 0U;
245*f8b30ca8SAntonio Nino Diaz 	write_cache_op((uintptr_t)my_bakery_info, is_cached);
2468c5fe0b5SSoby Mathew 	sev();
2478c5fe0b5SSoby Mathew }
248