blob: 748eeddf4d7e9f212b00ffc347169a52c096c999 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Jeenu Viswambharan0ffd9e22018-08-08 14:10:55 +01002 * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
7#include <assert.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +01008#include <string.h>
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +00009
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <arch_helpers.h>
11#include <lib/bakery_lock.h>
12#include <lib/el3_runtime/cpu_data.h>
13#include <plat/common/platform.h>
14
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000015/*
Soby Mathew523d6332015-01-08 18:02:19 +000016 * Functions in this file implement Bakery Algorithm for mutual exclusion with the
17 * bakery lock data structures in coherent memory.
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000018 *
19 * ARM architecture offers a family of exclusive access instructions to
20 * efficiently implement mutual exclusion with hardware support. However, as
21 * well as depending on external hardware, the these instructions have defined
22 * behavior only on certain memory types (cacheable and Normal memory in
23 * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
24 * in trusted firmware are such that mutual exclusion implementation cannot
25 * expect that accesses to the lock have the specific type required by the
26 * architecture for these primitives to function (for example, not all
27 * contenders may have address translation enabled).
28 *
29 * This implementation does not use mutual exclusion primitives. It expects
30 * memory regions where the locks reside to be fully ordered and coherent
31 * (either by disabling address translation, or by assigning proper attributes
32 * when translation is enabled).
33 *
34 * Note that the ARM architecture guarantees single-copy atomicity for aligned
35 * accesses regardless of status of address translation.
36 */
Achin Gupta4f6ad662013-10-25 09:08:21 +010037
Daniel Boulbyfef5d2d2018-05-04 14:04:07 +010038#define assert_bakery_entry_valid(_entry, _bakery) do { \
Antonio Nino Diaz519248b2018-10-31 15:55:57 +000039 assert((_bakery) != NULL); \
40 assert((_entry) < BAKERY_LOCK_MAX_CPUS); \
41} while (false)
Achin Gupta4f6ad662013-10-25 09:08:21 +010042
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000043/* Obtain a ticket for a given CPU */
Dan Handleye2712bc2014-04-10 15:37:22 +010044static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me)
Achin Gupta4f6ad662013-10-25 09:08:21 +010045{
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000046 unsigned int my_ticket, their_ticket;
47 unsigned int they;
Achin Gupta4f6ad662013-10-25 09:08:21 +010048
Soby Mathew156280c2015-02-20 16:04:17 +000049 /* Prevent recursive acquisition */
Antonio Nino Diaz519248b2018-10-31 15:55:57 +000050 assert(bakery_ticket_number(bakery->lock_data[me]) == 0U);
Soby Mathew156280c2015-02-20 16:04:17 +000051
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000052 /*
53 * Flag that we're busy getting our ticket. All CPUs are iterated in the
54 * order of their ordinal position to decide the maximum ticket value
55 * observed so far. Our priority is set to be greater than the maximum
56 * observed priority
57 *
58 * Note that it's possible that more than one contender gets the same
59 * ticket value. That's OK as the lock is acquired based on the priority
60 * value, not the ticket value alone.
61 */
Antonio Nino Diaz519248b2018-10-31 15:55:57 +000062 my_ticket = 0U;
Soby Mathewa0a897d2015-02-19 16:23:51 +000063 bakery->lock_data[me] = make_bakery_data(CHOOSING_TICKET, my_ticket);
Antonio Nino Diaz519248b2018-10-31 15:55:57 +000064 for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
Soby Mathewa0a897d2015-02-19 16:23:51 +000065 their_ticket = bakery_ticket_number(bakery->lock_data[they]);
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000066 if (their_ticket > my_ticket)
67 my_ticket = their_ticket;
68 }
Achin Gupta4f6ad662013-10-25 09:08:21 +010069
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000070 /*
71 * Compute ticket; then signal to other contenders waiting for us to
72 * finish calculating our ticket value that we're done
73 */
74 ++my_ticket;
Soby Mathewa0a897d2015-02-19 16:23:51 +000075 bakery->lock_data[me] = make_bakery_data(CHOSEN_TICKET, my_ticket);
Achin Gupta4f6ad662013-10-25 09:08:21 +010076
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000077 return my_ticket;
78}
Achin Gupta4f6ad662013-10-25 09:08:21 +010079
Achin Gupta4f6ad662013-10-25 09:08:21 +010080
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000081/*
82 * Acquire bakery lock
83 *
84 * Contending CPUs need first obtain a non-zero ticket and then calculate
85 * priority value. A contending CPU iterate over all other CPUs in the platform,
86 * which may be contending for the same lock, in the order of their ordinal
87 * position (CPU0, CPU1 and so on). A non-contending CPU will have its ticket
88 * (and priority) value as 0. The contending CPU compares its priority with that
89 * of others'. The CPU with the highest priority (lowest numerical value)
90 * acquires the lock
91 */
Andrew Thoelke958cc022014-06-09 12:54:15 +010092void bakery_lock_get(bakery_lock_t *bakery)
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000093{
94 unsigned int they, me;
95 unsigned int my_ticket, my_prio, their_ticket;
Soby Mathewa0a897d2015-02-19 16:23:51 +000096 unsigned int their_bakery_data;
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000097
Soby Mathew3700a922015-07-13 11:21:11 +010098 me = plat_my_core_pos();
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +000099
100 assert_bakery_entry_valid(me, bakery);
101
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000102 /* Get a ticket */
103 my_ticket = bakery_get_ticket(bakery, me);
104
105 /*
106 * Now that we got our ticket, compute our priority value, then compare
107 * with that of others, and proceed to acquire the lock
108 */
Antonio Nino Diaz519248b2018-10-31 15:55:57 +0000109 my_prio = bakery_get_priority(my_ticket, me);
110 for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000111 if (me == they)
112 continue;
113
114 /* Wait for the contender to get their ticket */
Soby Mathewa0a897d2015-02-19 16:23:51 +0000115 do {
116 their_bakery_data = bakery->lock_data[they];
117 } while (bakery_is_choosing(their_bakery_data));
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000118
119 /*
120 * If the other party is a contender, they'll have non-zero
121 * (valid) ticket value. If they do, compare priorities
122 */
Soby Mathewa0a897d2015-02-19 16:23:51 +0000123 their_ticket = bakery_ticket_number(their_bakery_data);
Antonio Nino Diaz519248b2018-10-31 15:55:57 +0000124 if ((their_ticket != 0U) &&
125 (bakery_get_priority(their_ticket, they) < my_prio)) {
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000126 /*
127 * They have higher priority (lower value). Wait for
128 * their ticket value to change (either release the lock
129 * to have it dropped to 0; or drop and probably content
130 * again for the same lock to have an even higher value)
131 */
132 do {
133 wfe();
Soby Mathewa0a897d2015-02-19 16:23:51 +0000134 } while (their_ticket ==
135 bakery_ticket_number(bakery->lock_data[they]));
Achin Gupta4f6ad662013-10-25 09:08:21 +0100136 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100137 }
Jeenu Viswambharan0ffd9e22018-08-08 14:10:55 +0100138
139 /*
Raghu Krishnamurthyc3c44002020-01-25 19:20:45 -0800140 * Lock acquired. Ensure that any reads and writes from a shared
141 * resource in the critical section read/write values after the lock is
142 * acquired.
Jeenu Viswambharan0ffd9e22018-08-08 14:10:55 +0100143 */
Raghu Krishnamurthyc3c44002020-01-25 19:20:45 -0800144 dmbish();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100145}
146
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000147
148/* Release the lock and signal contenders */
Andrew Thoelke958cc022014-06-09 12:54:15 +0100149void bakery_lock_release(bakery_lock_t *bakery)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100150{
Soby Mathew3700a922015-07-13 11:21:11 +0100151 unsigned int me = plat_my_core_pos();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100152
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000153 assert_bakery_entry_valid(me, bakery);
Antonio Nino Diaz519248b2018-10-31 15:55:57 +0000154 assert(bakery_ticket_number(bakery->lock_data[me]) != 0U);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100155
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000156 /*
Jeenu Viswambharan0ffd9e22018-08-08 14:10:55 +0100157 * Ensure that other observers see any stores in the critical section
Raghu Krishnamurthyc3c44002020-01-25 19:20:45 -0800158 * before releasing the lock. Also ensure all loads in the critical
159 * section are complete before releasing the lock. Release the lock by
160 * resetting ticket. Then signal other waiting contenders.
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000161 */
Raghu Krishnamurthyc3c44002020-01-25 19:20:45 -0800162 dmbish();
Antonio Nino Diaz519248b2018-10-31 15:55:57 +0000163 bakery->lock_data[me] = 0U;
Raghu Krishnamurthyc3c44002020-01-25 19:20:45 -0800164
165 /* Required to ensure ordering of the following sev */
Achin Guptaed04c4a2014-11-10 11:50:30 +0000166 dsb();
Jeenu Viswambharanbe87cfc2014-03-13 11:16:25 +0000167 sev();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100168}