Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 1 | /* |
Joel Hutton | 9f85f9e | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <bakery_lock.h> |
| 10 | #include <cpu_data.h> |
| 11 | #include <platform.h> |
| 12 | #include <string.h> |
Joel Hutton | 9f85f9e | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 13 | #include <utils_def.h> |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * Functions in this file implement Bakery Algorithm for mutual exclusion with the |
| 17 | * bakery lock data structures in cacheable and Normal memory. |
| 18 | * |
| 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, 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 cacheable and Normal. |
| 31 | * |
| 32 | * Note that the ARM architecture guarantees single-copy atomicity for aligned |
| 33 | * accesses regardless of status of address translation. |
| 34 | */ |
| 35 | |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 36 | #ifdef PLAT_PERCPU_BAKERY_LOCK_SIZE |
| 37 | /* |
| 38 | * Verify that the platform defined value for the per-cpu space for bakery locks is |
| 39 | * a multiple of the cache line size, to prevent multiple CPUs writing to the same |
| 40 | * bakery lock cache line |
| 41 | * |
| 42 | * Using this value, if provided, rather than the linker generated value results in |
| 43 | * more efficient code |
| 44 | */ |
| 45 | CASSERT((PLAT_PERCPU_BAKERY_LOCK_SIZE & (CACHE_WRITEBACK_GRANULE - 1)) == 0, \ |
| 46 | PLAT_PERCPU_BAKERY_LOCK_SIZE_not_cacheline_multiple); |
| 47 | #define PERCPU_BAKERY_LOCK_SIZE (PLAT_PERCPU_BAKERY_LOCK_SIZE) |
| 48 | #else |
| 49 | /* |
| 50 | * Use the linker defined symbol which has evaluated the size reqiurement. |
| 51 | * This is not as efficient as using a platform defined constant |
| 52 | */ |
Joel Hutton | 9f85f9e | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 53 | IMPORT_SYM(uintptr_t, __PERCPU_BAKERY_LOCK_SIZE__, PERCPU_BAKERY_LOCK_SIZE); |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 54 | #endif |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 55 | |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 56 | static inline bakery_lock_t *get_bakery_info(unsigned int cpu_ix, |
| 57 | bakery_lock_t *lock) |
| 58 | { |
| 59 | return (bakery_info_t *)((uintptr_t)lock + |
| 60 | cpu_ix * PERCPU_BAKERY_LOCK_SIZE); |
| 61 | } |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 62 | |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 63 | static inline void write_cache_op(uintptr_t addr, bool cached) |
| 64 | { |
| 65 | if (cached) |
| 66 | dccvac(addr); |
| 67 | else |
| 68 | dcivac(addr); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 69 | |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 70 | dsbish(); |
| 71 | } |
| 72 | |
| 73 | static inline void read_cache_op(uintptr_t addr, bool cached) |
| 74 | { |
| 75 | if (cached) |
| 76 | dccivac(addr); |
| 77 | } |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 78 | |
Soby Mathew | 95c1255 | 2016-11-14 17:19:35 +0000 | [diff] [blame] | 79 | /* Helper function to check if the lock is acquired */ |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 80 | static inline bool is_lock_acquired(const bakery_info_t *my_bakery_info, |
Soby Mathew | 95c1255 | 2016-11-14 17:19:35 +0000 | [diff] [blame] | 81 | int is_cached) |
| 82 | { |
| 83 | /* |
| 84 | * Even though lock data is updated only by the owning cpu and |
| 85 | * appropriate cache maintenance operations are performed, |
| 86 | * if the previous update was done when the cpu was not participating |
| 87 | * in coherency, then there is a chance that cache maintenance |
| 88 | * operations were not propagated to all the caches in the system. |
| 89 | * Hence do a `read_cache_op()` prior to read. |
| 90 | */ |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 91 | read_cache_op((uintptr_t)my_bakery_info, is_cached); |
| 92 | return bakery_ticket_number(my_bakery_info->lock_data) != 0U; |
Soby Mathew | 95c1255 | 2016-11-14 17:19:35 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 95 | static unsigned int bakery_get_ticket(bakery_lock_t *lock, |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 96 | unsigned int me, int is_cached) |
| 97 | { |
| 98 | unsigned int my_ticket, their_ticket; |
| 99 | unsigned int they; |
| 100 | bakery_info_t *my_bakery_info, *their_bakery_info; |
| 101 | |
| 102 | /* |
| 103 | * Obtain a reference to the bakery information for this cpu and ensure |
| 104 | * it is not NULL. |
| 105 | */ |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 106 | my_bakery_info = get_bakery_info(me, lock); |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 107 | assert(my_bakery_info != NULL); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 108 | |
Soby Mathew | 95c1255 | 2016-11-14 17:19:35 +0000 | [diff] [blame] | 109 | /* Prevent recursive acquisition.*/ |
| 110 | assert(!is_lock_acquired(my_bakery_info, is_cached)); |
Soby Mathew | 548579f | 2015-02-20 16:04:17 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 113 | * Tell other contenders that we are through the bakery doorway i.e. |
| 114 | * going to allocate a ticket for this cpu. |
| 115 | */ |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 116 | my_ticket = 0U; |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 117 | my_bakery_info->lock_data = make_bakery_data(CHOOSING_TICKET, my_ticket); |
| 118 | |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 119 | write_cache_op((uintptr_t)my_bakery_info, is_cached); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Iterate through the bakery information of each contender to allocate |
| 123 | * the highest ticket number for this cpu. |
| 124 | */ |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 125 | for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) { |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 126 | if (me == they) |
| 127 | continue; |
| 128 | |
| 129 | /* |
| 130 | * Get a reference to the other contender's bakery info and |
| 131 | * ensure that a stale copy is not read. |
| 132 | */ |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 133 | their_bakery_info = get_bakery_info(they, lock); |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 134 | assert(their_bakery_info != NULL); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 135 | |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 136 | read_cache_op((uintptr_t)their_bakery_info, is_cached); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Update this cpu's ticket number if a higher ticket number is |
| 140 | * seen |
| 141 | */ |
| 142 | their_ticket = bakery_ticket_number(their_bakery_info->lock_data); |
| 143 | if (their_ticket > my_ticket) |
| 144 | my_ticket = their_ticket; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Compute ticket; then signal to other contenders waiting for us to |
| 149 | * finish calculating our ticket value that we're done |
| 150 | */ |
| 151 | ++my_ticket; |
Soby Mathew | 1c9573a | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 152 | my_bakery_info->lock_data = make_bakery_data(CHOSEN_TICKET, my_ticket); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 153 | |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 154 | write_cache_op((uintptr_t)my_bakery_info, is_cached); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 155 | |
| 156 | return my_ticket; |
| 157 | } |
| 158 | |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 159 | void bakery_lock_get(bakery_lock_t *lock) |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 160 | { |
| 161 | unsigned int they, me, is_cached; |
| 162 | unsigned int my_ticket, my_prio, their_ticket; |
| 163 | bakery_info_t *their_bakery_info; |
Soby Mathew | 1c9573a | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 164 | unsigned int their_bakery_data; |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 165 | |
Soby Mathew | 85a181c | 2015-07-13 11:21:11 +0100 | [diff] [blame] | 166 | me = plat_my_core_pos(); |
Soby Mathew | 61531a2 | 2017-02-14 10:16:18 +0000 | [diff] [blame] | 167 | #ifdef AARCH32 |
| 168 | is_cached = read_sctlr() & SCTLR_C_BIT; |
| 169 | #else |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 170 | is_cached = read_sctlr_el3() & SCTLR_C_BIT; |
Soby Mathew | 61531a2 | 2017-02-14 10:16:18 +0000 | [diff] [blame] | 171 | #endif |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 172 | |
| 173 | /* Get a ticket */ |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 174 | my_ticket = bakery_get_ticket(lock, me, is_cached); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * Now that we got our ticket, compute our priority value, then compare |
| 178 | * with that of others, and proceed to acquire the lock |
| 179 | */ |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 180 | my_prio = bakery_get_priority(my_ticket, me); |
| 181 | for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) { |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 182 | if (me == they) |
| 183 | continue; |
| 184 | |
| 185 | /* |
| 186 | * Get a reference to the other contender's bakery info and |
| 187 | * ensure that a stale copy is not read. |
| 188 | */ |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 189 | their_bakery_info = get_bakery_info(they, lock); |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 190 | assert(their_bakery_info != NULL); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 191 | |
| 192 | /* Wait for the contender to get their ticket */ |
Soby Mathew | 1c9573a | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 193 | do { |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 194 | read_cache_op((uintptr_t)their_bakery_info, is_cached); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 195 | their_bakery_data = their_bakery_info->lock_data; |
Soby Mathew | 1c9573a | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 196 | } while (bakery_is_choosing(their_bakery_data)); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * If the other party is a contender, they'll have non-zero |
| 200 | * (valid) ticket value. If they do, compare priorities |
| 201 | */ |
| 202 | their_ticket = bakery_ticket_number(their_bakery_data); |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 203 | if (their_ticket && (bakery_get_priority(their_ticket, they) < my_prio)) { |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 204 | /* |
| 205 | * They have higher priority (lower value). Wait for |
| 206 | * their ticket value to change (either release the lock |
| 207 | * to have it dropped to 0; or drop and probably content |
| 208 | * again for the same lock to have an even higher value) |
| 209 | */ |
| 210 | do { |
| 211 | wfe(); |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 212 | read_cache_op((uintptr_t)their_bakery_info, is_cached); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 213 | } while (their_ticket |
| 214 | == bakery_ticket_number(their_bakery_info->lock_data)); |
| 215 | } |
| 216 | } |
Jeenu Viswambharan | 24dc970 | 2018-08-08 14:10:55 +0100 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * Lock acquired. Ensure that any reads from a shared resource in the |
| 220 | * critical section read values after the lock is acquired. |
| 221 | */ |
| 222 | dmbld(); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 225 | void bakery_lock_release(bakery_lock_t *lock) |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 226 | { |
| 227 | bakery_info_t *my_bakery_info; |
Soby Mathew | 61531a2 | 2017-02-14 10:16:18 +0000 | [diff] [blame] | 228 | #ifdef AARCH32 |
| 229 | unsigned int is_cached = read_sctlr() & SCTLR_C_BIT; |
| 230 | #else |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 231 | unsigned int is_cached = read_sctlr_el3() & SCTLR_C_BIT; |
Soby Mathew | 61531a2 | 2017-02-14 10:16:18 +0000 | [diff] [blame] | 232 | #endif |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 233 | |
Andrew Thoelke | ee7b35c | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 234 | my_bakery_info = get_bakery_info(plat_my_core_pos(), lock); |
Soby Mathew | 95c1255 | 2016-11-14 17:19:35 +0000 | [diff] [blame] | 235 | |
| 236 | assert(is_lock_acquired(my_bakery_info, is_cached)); |
Soby Mathew | 548579f | 2015-02-20 16:04:17 +0000 | [diff] [blame] | 237 | |
Jeenu Viswambharan | 24dc970 | 2018-08-08 14:10:55 +0100 | [diff] [blame] | 238 | /* |
| 239 | * Ensure that other observers see any stores in the critical section |
| 240 | * before releasing the lock. Release the lock by resetting ticket. |
| 241 | * Then signal other waiting contenders. |
| 242 | */ |
| 243 | dmbst(); |
Antonio Nino Diaz | f8b30ca | 2018-10-31 15:55:57 +0000 | [diff] [blame^] | 244 | my_bakery_info->lock_data = 0U; |
| 245 | write_cache_op((uintptr_t)my_bakery_info, is_cached); |
Soby Mathew | 8c5fe0b | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 246 | sev(); |
| 247 | } |