Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Jeenu Viswambharan | 0ffd9e2 | 2018-08-08 14:10:55 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 8 | #include <string.h> |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #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 Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 15 | /* |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 16 | * Functions in this file implement Bakery Algorithm for mutual exclusion with the |
| 17 | * bakery lock data structures in coherent memory. |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 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, 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 Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 37 | |
Daniel Boulby | fef5d2d | 2018-05-04 14:04:07 +0100 | [diff] [blame] | 38 | #define assert_bakery_entry_valid(_entry, _bakery) do { \ |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 39 | assert((_bakery) != NULL); \ |
| 40 | assert((_entry) < BAKERY_LOCK_MAX_CPUS); \ |
| 41 | } while (false) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 42 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 43 | /* Obtain a ticket for a given CPU */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 44 | static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 45 | { |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 46 | unsigned int my_ticket, their_ticket; |
| 47 | unsigned int they; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 48 | |
Soby Mathew | 156280c | 2015-02-20 16:04:17 +0000 | [diff] [blame] | 49 | /* Prevent recursive acquisition */ |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 50 | assert(bakery_ticket_number(bakery->lock_data[me]) == 0U); |
Soby Mathew | 156280c | 2015-02-20 16:04:17 +0000 | [diff] [blame] | 51 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 52 | /* |
| 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 Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 62 | my_ticket = 0U; |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 63 | bakery->lock_data[me] = make_bakery_data(CHOOSING_TICKET, my_ticket); |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 64 | for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) { |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 65 | their_ticket = bakery_ticket_number(bakery->lock_data[they]); |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 66 | if (their_ticket > my_ticket) |
| 67 | my_ticket = their_ticket; |
| 68 | } |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 69 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 70 | /* |
| 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 Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 75 | bakery->lock_data[me] = make_bakery_data(CHOSEN_TICKET, my_ticket); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 76 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 77 | return my_ticket; |
| 78 | } |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 79 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 80 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 81 | /* |
| 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 Thoelke | 958cc02 | 2014-06-09 12:54:15 +0100 | [diff] [blame] | 92 | void bakery_lock_get(bakery_lock_t *bakery) |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 93 | { |
| 94 | unsigned int they, me; |
| 95 | unsigned int my_ticket, my_prio, their_ticket; |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 96 | unsigned int their_bakery_data; |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 97 | |
Soby Mathew | 3700a92 | 2015-07-13 11:21:11 +0100 | [diff] [blame] | 98 | me = plat_my_core_pos(); |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 99 | |
| 100 | assert_bakery_entry_valid(me, bakery); |
| 101 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 102 | /* 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 Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 109 | my_prio = bakery_get_priority(my_ticket, me); |
| 110 | for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) { |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 111 | if (me == they) |
| 112 | continue; |
| 113 | |
| 114 | /* Wait for the contender to get their ticket */ |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 115 | do { |
| 116 | their_bakery_data = bakery->lock_data[they]; |
| 117 | } while (bakery_is_choosing(their_bakery_data)); |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 118 | |
| 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 Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 123 | their_ticket = bakery_ticket_number(their_bakery_data); |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 124 | if ((their_ticket != 0U) && |
| 125 | (bakery_get_priority(their_ticket, they) < my_prio)) { |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 126 | /* |
| 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 Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 134 | } while (their_ticket == |
| 135 | bakery_ticket_number(bakery->lock_data[they])); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 136 | } |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 137 | } |
Jeenu Viswambharan | 0ffd9e2 | 2018-08-08 14:10:55 +0100 | [diff] [blame] | 138 | |
| 139 | /* |
Raghu Krishnamurthy | c3c4400 | 2020-01-25 19:20:45 -0800 | [diff] [blame] | 140 | * 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 Viswambharan | 0ffd9e2 | 2018-08-08 14:10:55 +0100 | [diff] [blame] | 143 | */ |
Raghu Krishnamurthy | c3c4400 | 2020-01-25 19:20:45 -0800 | [diff] [blame] | 144 | dmbish(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 147 | |
| 148 | /* Release the lock and signal contenders */ |
Andrew Thoelke | 958cc02 | 2014-06-09 12:54:15 +0100 | [diff] [blame] | 149 | void bakery_lock_release(bakery_lock_t *bakery) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 150 | { |
Soby Mathew | 3700a92 | 2015-07-13 11:21:11 +0100 | [diff] [blame] | 151 | unsigned int me = plat_my_core_pos(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 152 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 153 | assert_bakery_entry_valid(me, bakery); |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 154 | assert(bakery_ticket_number(bakery->lock_data[me]) != 0U); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 155 | |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 156 | /* |
Jeenu Viswambharan | 0ffd9e2 | 2018-08-08 14:10:55 +0100 | [diff] [blame] | 157 | * Ensure that other observers see any stores in the critical section |
Raghu Krishnamurthy | c3c4400 | 2020-01-25 19:20:45 -0800 | [diff] [blame] | 158 | * 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 Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 161 | */ |
Raghu Krishnamurthy | c3c4400 | 2020-01-25 19:20:45 -0800 | [diff] [blame] | 162 | dmbish(); |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 163 | bakery->lock_data[me] = 0U; |
Raghu Krishnamurthy | c3c4400 | 2020-01-25 19:20:45 -0800 | [diff] [blame] | 164 | |
| 165 | /* Required to ensure ordering of the following sev */ |
Achin Gupta | ed04c4a | 2014-11-10 11:50:30 +0000 | [diff] [blame] | 166 | dsb(); |
Jeenu Viswambharan | be87cfc | 2014-03-13 11:16:25 +0000 | [diff] [blame] | 167 | sev(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 168 | } |