Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 1 | /* |
Joel Hutton | 5cc3bc8 | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 523d633 | 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 | 5cc3bc8 | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 13 | #include <utils_def.h> |
Soby Mathew | 523d633 | 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 | e466c9f | 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 | 5cc3bc8 | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 53 | IMPORT_SYM(uintptr_t, __PERCPU_BAKERY_LOCK_SIZE__, PERCPU_BAKERY_LOCK_SIZE); |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 54 | #endif |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 55 | |
Daniel Boulby | 7a4e8f5 | 2018-05-09 11:29:07 +0100 | [diff] [blame] | 56 | #define get_bakery_info(_cpu_ix, _lock) \ |
| 57 | (bakery_info_t *)((uintptr_t)_lock + _cpu_ix * PERCPU_BAKERY_LOCK_SIZE) |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 58 | |
Daniel Boulby | 7a4e8f5 | 2018-05-09 11:29:07 +0100 | [diff] [blame] | 59 | #define write_cache_op(_addr, _cached) \ |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 60 | do { \ |
Daniel Boulby | 7a4e8f5 | 2018-05-09 11:29:07 +0100 | [diff] [blame] | 61 | (_cached ? dccvac((uintptr_t)_addr) :\ |
| 62 | dcivac((uintptr_t)_addr));\ |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 63 | dsbish();\ |
| 64 | } while (0) |
| 65 | |
Daniel Boulby | 7a4e8f5 | 2018-05-09 11:29:07 +0100 | [diff] [blame] | 66 | #define read_cache_op(_addr, _cached) if (_cached) \ |
| 67 | dccivac((uintptr_t)_addr) |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 68 | |
Soby Mathew | f9baaa5 | 2016-11-14 17:19:35 +0000 | [diff] [blame] | 69 | /* Helper function to check if the lock is acquired */ |
| 70 | static inline int is_lock_acquired(const bakery_info_t *my_bakery_info, |
| 71 | int is_cached) |
| 72 | { |
| 73 | /* |
| 74 | * Even though lock data is updated only by the owning cpu and |
| 75 | * appropriate cache maintenance operations are performed, |
| 76 | * if the previous update was done when the cpu was not participating |
| 77 | * in coherency, then there is a chance that cache maintenance |
| 78 | * operations were not propagated to all the caches in the system. |
| 79 | * Hence do a `read_cache_op()` prior to read. |
| 80 | */ |
| 81 | read_cache_op(my_bakery_info, is_cached); |
| 82 | return !!(bakery_ticket_number(my_bakery_info->lock_data)); |
| 83 | } |
| 84 | |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 85 | static unsigned int bakery_get_ticket(bakery_lock_t *lock, |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 86 | unsigned int me, int is_cached) |
| 87 | { |
| 88 | unsigned int my_ticket, their_ticket; |
| 89 | unsigned int they; |
| 90 | bakery_info_t *my_bakery_info, *their_bakery_info; |
| 91 | |
| 92 | /* |
| 93 | * Obtain a reference to the bakery information for this cpu and ensure |
| 94 | * it is not NULL. |
| 95 | */ |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 96 | my_bakery_info = get_bakery_info(me, lock); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 97 | assert(my_bakery_info); |
| 98 | |
Soby Mathew | f9baaa5 | 2016-11-14 17:19:35 +0000 | [diff] [blame] | 99 | /* Prevent recursive acquisition.*/ |
| 100 | assert(!is_lock_acquired(my_bakery_info, is_cached)); |
Soby Mathew | 156280c | 2015-02-20 16:04:17 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 103 | * Tell other contenders that we are through the bakery doorway i.e. |
| 104 | * going to allocate a ticket for this cpu. |
| 105 | */ |
| 106 | my_ticket = 0; |
| 107 | my_bakery_info->lock_data = make_bakery_data(CHOOSING_TICKET, my_ticket); |
| 108 | |
| 109 | write_cache_op(my_bakery_info, is_cached); |
| 110 | |
| 111 | /* |
| 112 | * Iterate through the bakery information of each contender to allocate |
| 113 | * the highest ticket number for this cpu. |
| 114 | */ |
| 115 | for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) { |
| 116 | if (me == they) |
| 117 | continue; |
| 118 | |
| 119 | /* |
| 120 | * Get a reference to the other contender's bakery info and |
| 121 | * ensure that a stale copy is not read. |
| 122 | */ |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 123 | their_bakery_info = get_bakery_info(they, lock); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 124 | assert(their_bakery_info); |
| 125 | |
| 126 | read_cache_op(their_bakery_info, is_cached); |
| 127 | |
| 128 | /* |
| 129 | * Update this cpu's ticket number if a higher ticket number is |
| 130 | * seen |
| 131 | */ |
| 132 | their_ticket = bakery_ticket_number(their_bakery_info->lock_data); |
| 133 | if (their_ticket > my_ticket) |
| 134 | my_ticket = their_ticket; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Compute ticket; then signal to other contenders waiting for us to |
| 139 | * finish calculating our ticket value that we're done |
| 140 | */ |
| 141 | ++my_ticket; |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 142 | my_bakery_info->lock_data = make_bakery_data(CHOSEN_TICKET, my_ticket); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 143 | |
| 144 | write_cache_op(my_bakery_info, is_cached); |
| 145 | |
| 146 | return my_ticket; |
| 147 | } |
| 148 | |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 149 | void bakery_lock_get(bakery_lock_t *lock) |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 150 | { |
| 151 | unsigned int they, me, is_cached; |
| 152 | unsigned int my_ticket, my_prio, their_ticket; |
| 153 | bakery_info_t *their_bakery_info; |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 154 | unsigned int their_bakery_data; |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 155 | |
Soby Mathew | 3700a92 | 2015-07-13 11:21:11 +0100 | [diff] [blame] | 156 | me = plat_my_core_pos(); |
Soby Mathew | 077ab54 | 2017-02-14 10:16:18 +0000 | [diff] [blame] | 157 | #ifdef AARCH32 |
| 158 | is_cached = read_sctlr() & SCTLR_C_BIT; |
| 159 | #else |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 160 | is_cached = read_sctlr_el3() & SCTLR_C_BIT; |
Soby Mathew | 077ab54 | 2017-02-14 10:16:18 +0000 | [diff] [blame] | 161 | #endif |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 162 | |
| 163 | /* Get a ticket */ |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 164 | my_ticket = bakery_get_ticket(lock, me, is_cached); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Now that we got our ticket, compute our priority value, then compare |
| 168 | * with that of others, and proceed to acquire the lock |
| 169 | */ |
| 170 | my_prio = PRIORITY(my_ticket, me); |
| 171 | for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) { |
| 172 | if (me == they) |
| 173 | continue; |
| 174 | |
| 175 | /* |
| 176 | * Get a reference to the other contender's bakery info and |
| 177 | * ensure that a stale copy is not read. |
| 178 | */ |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 179 | their_bakery_info = get_bakery_info(they, lock); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 180 | assert(their_bakery_info); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 181 | |
| 182 | /* Wait for the contender to get their ticket */ |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 183 | do { |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 184 | read_cache_op(their_bakery_info, is_cached); |
| 185 | their_bakery_data = their_bakery_info->lock_data; |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 186 | } while (bakery_is_choosing(their_bakery_data)); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * If the other party is a contender, they'll have non-zero |
| 190 | * (valid) ticket value. If they do, compare priorities |
| 191 | */ |
| 192 | their_ticket = bakery_ticket_number(their_bakery_data); |
| 193 | if (their_ticket && (PRIORITY(their_ticket, they) < my_prio)) { |
| 194 | /* |
| 195 | * They have higher priority (lower value). Wait for |
| 196 | * their ticket value to change (either release the lock |
| 197 | * to have it dropped to 0; or drop and probably content |
| 198 | * again for the same lock to have an even higher value) |
| 199 | */ |
| 200 | do { |
| 201 | wfe(); |
| 202 | read_cache_op(their_bakery_info, is_cached); |
| 203 | } while (their_ticket |
| 204 | == bakery_ticket_number(their_bakery_info->lock_data)); |
| 205 | } |
| 206 | } |
Jeenu Viswambharan | 0ffd9e2 | 2018-08-08 14:10:55 +0100 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * Lock acquired. Ensure that any reads from a shared resource in the |
| 210 | * critical section read values after the lock is acquired. |
| 211 | */ |
| 212 | dmbld(); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 215 | void bakery_lock_release(bakery_lock_t *lock) |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 216 | { |
| 217 | bakery_info_t *my_bakery_info; |
Soby Mathew | 077ab54 | 2017-02-14 10:16:18 +0000 | [diff] [blame] | 218 | #ifdef AARCH32 |
| 219 | unsigned int is_cached = read_sctlr() & SCTLR_C_BIT; |
| 220 | #else |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 221 | unsigned int is_cached = read_sctlr_el3() & SCTLR_C_BIT; |
Soby Mathew | 077ab54 | 2017-02-14 10:16:18 +0000 | [diff] [blame] | 222 | #endif |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 223 | |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 224 | my_bakery_info = get_bakery_info(plat_my_core_pos(), lock); |
Soby Mathew | f9baaa5 | 2016-11-14 17:19:35 +0000 | [diff] [blame] | 225 | |
| 226 | assert(is_lock_acquired(my_bakery_info, is_cached)); |
Soby Mathew | 156280c | 2015-02-20 16:04:17 +0000 | [diff] [blame] | 227 | |
Jeenu Viswambharan | 0ffd9e2 | 2018-08-08 14:10:55 +0100 | [diff] [blame] | 228 | /* |
| 229 | * Ensure that other observers see any stores in the critical section |
| 230 | * before releasing the lock. Release the lock by resetting ticket. |
| 231 | * Then signal other waiting contenders. |
| 232 | */ |
| 233 | dmbst(); |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 234 | my_bakery_info->lock_data = 0; |
| 235 | write_cache_op(my_bakery_info, is_cached); |
| 236 | sev(); |
| 237 | } |