xref: /rk3399_ARM-atf/lib/locks/bakery/bakery_lock_coherent.c (revision d77a1ec5213a6618bf1a52ea591506e995b29392)
18c5fe0b5SSoby Mathew /*
24c700c15SGovindraj Raja  * Copyright (c) 2013-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>
98c5fe0b5SSoby Mathew 
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 <plat/common/platform.h>
1409d40e0eSAntonio Nino Diaz 
158c5fe0b5SSoby Mathew /*
168c5fe0b5SSoby Mathew  * Functions in this file implement Bakery Algorithm for mutual exclusion with the
178c5fe0b5SSoby Mathew  * bakery lock data structures in coherent 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, the 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 fully ordered and coherent
318c5fe0b5SSoby Mathew  * (either by disabling address translation, or by assigning proper attributes
328c5fe0b5SSoby Mathew  * when translation is enabled).
338c5fe0b5SSoby Mathew  *
348c5fe0b5SSoby Mathew  * Note that the ARM architecture guarantees single-copy atomicity for aligned
358c5fe0b5SSoby Mathew  * accesses regardless of status of address translation.
368c5fe0b5SSoby Mathew  */
378c5fe0b5SSoby Mathew 
38896a5902SDaniel Boulby #define assert_bakery_entry_valid(_entry, _bakery) do {	\
39f8b30ca8SAntonio Nino Diaz 	assert((_bakery) != NULL);			\
40f8b30ca8SAntonio Nino Diaz 	assert((_entry) < BAKERY_LOCK_MAX_CPUS);	\
41f8b30ca8SAntonio Nino Diaz } while (false)
428c5fe0b5SSoby Mathew 
438c5fe0b5SSoby Mathew /* Obtain a ticket for a given CPU */
bakery_get_ticket(bakery_lock_t * bakery,unsigned int me)448c5fe0b5SSoby Mathew static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me)
458c5fe0b5SSoby Mathew {
468c5fe0b5SSoby Mathew 	unsigned int my_ticket, their_ticket;
478c5fe0b5SSoby Mathew 	unsigned int they;
488c5fe0b5SSoby Mathew 
49548579f5SSoby Mathew 	/* Prevent recursive acquisition */
50f8b30ca8SAntonio Nino Diaz 	assert(bakery_ticket_number(bakery->lock_data[me]) == 0U);
51548579f5SSoby Mathew 
528c5fe0b5SSoby Mathew 	/*
538c5fe0b5SSoby Mathew 	 * Flag that we're busy getting our ticket. All CPUs are iterated in the
548c5fe0b5SSoby Mathew 	 * order of their ordinal position to decide the maximum ticket value
558c5fe0b5SSoby Mathew 	 * observed so far. Our priority is set to be greater than the maximum
568c5fe0b5SSoby Mathew 	 * observed priority
578c5fe0b5SSoby Mathew 	 *
588c5fe0b5SSoby Mathew 	 * Note that it's possible that more than one contender gets the same
598c5fe0b5SSoby Mathew 	 * ticket value. That's OK as the lock is acquired based on the priority
608c5fe0b5SSoby Mathew 	 * value, not the ticket value alone.
618c5fe0b5SSoby Mathew 	 */
62f8b30ca8SAntonio Nino Diaz 	my_ticket = 0U;
631c9573a1SSoby Mathew 	bakery->lock_data[me] = make_bakery_data(CHOOSING_TICKET, my_ticket);
64f8b30ca8SAntonio Nino Diaz 	for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
651c9573a1SSoby Mathew 		their_ticket = bakery_ticket_number(bakery->lock_data[they]);
66*bd7ad5e6SMaheedhar Bollapalli 		if (their_ticket > my_ticket) {
678c5fe0b5SSoby Mathew 			my_ticket = their_ticket;
688c5fe0b5SSoby Mathew 		}
69*bd7ad5e6SMaheedhar Bollapalli 	}
708c5fe0b5SSoby Mathew 
718c5fe0b5SSoby Mathew 	/*
728c5fe0b5SSoby Mathew 	 * Compute ticket; then signal to other contenders waiting for us to
738c5fe0b5SSoby Mathew 	 * finish calculating our ticket value that we're done
748c5fe0b5SSoby Mathew 	 */
758c5fe0b5SSoby Mathew 	++my_ticket;
761c9573a1SSoby Mathew 	bakery->lock_data[me] = make_bakery_data(CHOSEN_TICKET, my_ticket);
778c5fe0b5SSoby Mathew 
788c5fe0b5SSoby Mathew 	return my_ticket;
798c5fe0b5SSoby Mathew }
808c5fe0b5SSoby Mathew 
818c5fe0b5SSoby Mathew 
828c5fe0b5SSoby Mathew /*
838c5fe0b5SSoby Mathew  * Acquire bakery lock
848c5fe0b5SSoby Mathew  *
858c5fe0b5SSoby Mathew  * Contending CPUs need first obtain a non-zero ticket and then calculate
868c5fe0b5SSoby Mathew  * priority value. A contending CPU iterate over all other CPUs in the platform,
878c5fe0b5SSoby Mathew  * which may be contending for the same lock, in the order of their ordinal
888c5fe0b5SSoby Mathew  * position (CPU0, CPU1 and so on). A non-contending CPU will have its ticket
898c5fe0b5SSoby Mathew  * (and priority) value as 0. The contending CPU compares its priority with that
908c5fe0b5SSoby Mathew  * of others'. The CPU with the highest priority (lowest numerical value)
918c5fe0b5SSoby Mathew  * acquires the lock
928c5fe0b5SSoby Mathew  */
bakery_lock_get(bakery_lock_t * bakery)938c5fe0b5SSoby Mathew void bakery_lock_get(bakery_lock_t *bakery)
948c5fe0b5SSoby Mathew {
958c5fe0b5SSoby Mathew 	unsigned int they, me;
968c5fe0b5SSoby Mathew 	unsigned int my_ticket, my_prio, their_ticket;
971c9573a1SSoby Mathew 	unsigned int their_bakery_data;
988c5fe0b5SSoby Mathew 
9985a181ceSSoby Mathew 	me = plat_my_core_pos();
1008c5fe0b5SSoby Mathew 
1018c5fe0b5SSoby Mathew 	assert_bakery_entry_valid(me, bakery);
1028c5fe0b5SSoby Mathew 
1038c5fe0b5SSoby Mathew 	/* Get a ticket */
1048c5fe0b5SSoby Mathew 	my_ticket = bakery_get_ticket(bakery, me);
1058c5fe0b5SSoby Mathew 
1068c5fe0b5SSoby Mathew 	/*
1078c5fe0b5SSoby Mathew 	 * Now that we got our ticket, compute our priority value, then compare
1088c5fe0b5SSoby Mathew 	 * with that of others, and proceed to acquire the lock
1098c5fe0b5SSoby Mathew 	 */
110f8b30ca8SAntonio Nino Diaz 	my_prio = bakery_get_priority(my_ticket, me);
111f8b30ca8SAntonio Nino Diaz 	for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
112*bd7ad5e6SMaheedhar Bollapalli 		if (me == they) {
1138c5fe0b5SSoby Mathew 			continue;
114*bd7ad5e6SMaheedhar Bollapalli 		}
1158c5fe0b5SSoby Mathew 
1168c5fe0b5SSoby Mathew 		/* Wait for the contender to get their ticket */
1171c9573a1SSoby Mathew 		do {
1181c9573a1SSoby Mathew 			their_bakery_data = bakery->lock_data[they];
1191c9573a1SSoby Mathew 		} while (bakery_is_choosing(their_bakery_data));
1208c5fe0b5SSoby Mathew 
1218c5fe0b5SSoby Mathew 		/*
1228c5fe0b5SSoby Mathew 		 * If the other party is a contender, they'll have non-zero
1238c5fe0b5SSoby Mathew 		 * (valid) ticket value. If they do, compare priorities
1248c5fe0b5SSoby Mathew 		 */
1251c9573a1SSoby Mathew 		their_ticket = bakery_ticket_number(their_bakery_data);
126f8b30ca8SAntonio Nino Diaz 		if ((their_ticket != 0U) &&
127f8b30ca8SAntonio Nino Diaz 		    (bakery_get_priority(their_ticket, they) < my_prio)) {
1288c5fe0b5SSoby Mathew 			/*
1298c5fe0b5SSoby Mathew 			 * They have higher priority (lower value). Wait for
1308c5fe0b5SSoby Mathew 			 * their ticket value to change (either release the lock
1318c5fe0b5SSoby Mathew 			 * to have it dropped to 0; or drop and probably content
1328c5fe0b5SSoby Mathew 			 * again for the same lock to have an even higher value)
1338c5fe0b5SSoby Mathew 			 */
1348c5fe0b5SSoby Mathew 			do {
1358c5fe0b5SSoby Mathew 				wfe();
1361c9573a1SSoby Mathew 			} while (their_ticket ==
1371c9573a1SSoby Mathew 				bakery_ticket_number(bakery->lock_data[they]));
1388c5fe0b5SSoby Mathew 		}
1398c5fe0b5SSoby Mathew 	}
14024dc9709SJeenu Viswambharan 
14124dc9709SJeenu Viswambharan 	/*
142c0018913SRaghu Krishnamurthy 	 * Lock acquired. Ensure that any reads and writes from a shared
143c0018913SRaghu Krishnamurthy 	 * resource in the critical section read/write values after the lock is
144c0018913SRaghu Krishnamurthy 	 * acquired.
14524dc9709SJeenu Viswambharan 	 */
146c0018913SRaghu Krishnamurthy 	dmbish();
1478c5fe0b5SSoby Mathew }
1488c5fe0b5SSoby Mathew 
1498c5fe0b5SSoby Mathew 
1508c5fe0b5SSoby Mathew /* Release the lock and signal contenders */
bakery_lock_release(bakery_lock_t * bakery)1518c5fe0b5SSoby Mathew void bakery_lock_release(bakery_lock_t *bakery)
1528c5fe0b5SSoby Mathew {
15385a181ceSSoby Mathew 	unsigned int me = plat_my_core_pos();
1548c5fe0b5SSoby Mathew 
1558c5fe0b5SSoby Mathew 	assert_bakery_entry_valid(me, bakery);
156f8b30ca8SAntonio Nino Diaz 	assert(bakery_ticket_number(bakery->lock_data[me]) != 0U);
1578c5fe0b5SSoby Mathew 
1588c5fe0b5SSoby Mathew 	/*
15924dc9709SJeenu Viswambharan 	 * Ensure that other observers see any stores in the critical section
160c0018913SRaghu Krishnamurthy 	 * before releasing the lock. Also ensure all loads in the critical
161c0018913SRaghu Krishnamurthy 	 * section are complete before releasing the lock. Release the lock by
162c0018913SRaghu Krishnamurthy 	 * resetting ticket. Then signal other waiting contenders.
1638c5fe0b5SSoby Mathew 	 */
164c0018913SRaghu Krishnamurthy 	dmbish();
165f8b30ca8SAntonio Nino Diaz 	bakery->lock_data[me] = 0U;
166c0018913SRaghu Krishnamurthy 
167c0018913SRaghu Krishnamurthy 	/* Required to ensure ordering of the following sev */
1688c5fe0b5SSoby Mathew 	dsb();
1698c5fe0b5SSoby Mathew 	sev();
1708c5fe0b5SSoby Mathew }
171