18c5fe0b5SSoby Mathew /* 2*1c9573a1SSoby Mathew * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. 38c5fe0b5SSoby Mathew * 48c5fe0b5SSoby Mathew * Redistribution and use in source and binary forms, with or without 58c5fe0b5SSoby Mathew * modification, are permitted provided that the following conditions are met: 68c5fe0b5SSoby Mathew * 78c5fe0b5SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 88c5fe0b5SSoby Mathew * list of conditions and the following disclaimer. 98c5fe0b5SSoby Mathew * 108c5fe0b5SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 118c5fe0b5SSoby Mathew * this list of conditions and the following disclaimer in the documentation 128c5fe0b5SSoby Mathew * and/or other materials provided with the distribution. 138c5fe0b5SSoby Mathew * 148c5fe0b5SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 158c5fe0b5SSoby Mathew * to endorse or promote products derived from this software without specific 168c5fe0b5SSoby Mathew * prior written permission. 178c5fe0b5SSoby Mathew * 188c5fe0b5SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 198c5fe0b5SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 208c5fe0b5SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 218c5fe0b5SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 228c5fe0b5SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 238c5fe0b5SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 248c5fe0b5SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 258c5fe0b5SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 268c5fe0b5SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 278c5fe0b5SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 288c5fe0b5SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 298c5fe0b5SSoby Mathew */ 308c5fe0b5SSoby Mathew 318c5fe0b5SSoby Mathew #include <arch_helpers.h> 328c5fe0b5SSoby Mathew #include <assert.h> 338c5fe0b5SSoby Mathew #include <bakery_lock.h> 348c5fe0b5SSoby Mathew #include <cpu_data.h> 358c5fe0b5SSoby Mathew #include <platform.h> 368c5fe0b5SSoby Mathew #include <string.h> 378c5fe0b5SSoby Mathew 388c5fe0b5SSoby Mathew /* 398c5fe0b5SSoby Mathew * Functions in this file implement Bakery Algorithm for mutual exclusion with the 408c5fe0b5SSoby Mathew * bakery lock data structures in coherent memory. 418c5fe0b5SSoby Mathew * 428c5fe0b5SSoby Mathew * ARM architecture offers a family of exclusive access instructions to 438c5fe0b5SSoby Mathew * efficiently implement mutual exclusion with hardware support. However, as 448c5fe0b5SSoby Mathew * well as depending on external hardware, the these instructions have defined 458c5fe0b5SSoby Mathew * behavior only on certain memory types (cacheable and Normal memory in 468c5fe0b5SSoby Mathew * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases 478c5fe0b5SSoby Mathew * in trusted firmware are such that mutual exclusion implementation cannot 488c5fe0b5SSoby Mathew * expect that accesses to the lock have the specific type required by the 498c5fe0b5SSoby Mathew * architecture for these primitives to function (for example, not all 508c5fe0b5SSoby Mathew * contenders may have address translation enabled). 518c5fe0b5SSoby Mathew * 528c5fe0b5SSoby Mathew * This implementation does not use mutual exclusion primitives. It expects 538c5fe0b5SSoby Mathew * memory regions where the locks reside to be fully ordered and coherent 548c5fe0b5SSoby Mathew * (either by disabling address translation, or by assigning proper attributes 558c5fe0b5SSoby Mathew * when translation is enabled). 568c5fe0b5SSoby Mathew * 578c5fe0b5SSoby Mathew * Note that the ARM architecture guarantees single-copy atomicity for aligned 588c5fe0b5SSoby Mathew * accesses regardless of status of address translation. 598c5fe0b5SSoby Mathew */ 608c5fe0b5SSoby Mathew 618c5fe0b5SSoby Mathew #define assert_bakery_entry_valid(entry, bakery) do { \ 628c5fe0b5SSoby Mathew assert(bakery); \ 638c5fe0b5SSoby Mathew assert(entry < BAKERY_LOCK_MAX_CPUS); \ 648c5fe0b5SSoby Mathew } while (0) 658c5fe0b5SSoby Mathew 668c5fe0b5SSoby Mathew /* Initialize Bakery Lock to reset ownership and all ticket values */ 678c5fe0b5SSoby Mathew void bakery_lock_init(bakery_lock_t *bakery) 688c5fe0b5SSoby Mathew { 698c5fe0b5SSoby Mathew assert(bakery); 708c5fe0b5SSoby Mathew 718c5fe0b5SSoby Mathew /* All ticket values need to be 0 */ 728c5fe0b5SSoby Mathew memset(bakery, 0, sizeof(*bakery)); 738c5fe0b5SSoby Mathew bakery->owner = NO_OWNER; 748c5fe0b5SSoby Mathew } 758c5fe0b5SSoby Mathew 768c5fe0b5SSoby Mathew 778c5fe0b5SSoby Mathew /* Obtain a ticket for a given CPU */ 788c5fe0b5SSoby Mathew static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me) 798c5fe0b5SSoby Mathew { 808c5fe0b5SSoby Mathew unsigned int my_ticket, their_ticket; 818c5fe0b5SSoby Mathew unsigned int they; 828c5fe0b5SSoby Mathew 838c5fe0b5SSoby Mathew /* 848c5fe0b5SSoby Mathew * Flag that we're busy getting our ticket. All CPUs are iterated in the 858c5fe0b5SSoby Mathew * order of their ordinal position to decide the maximum ticket value 868c5fe0b5SSoby Mathew * observed so far. Our priority is set to be greater than the maximum 878c5fe0b5SSoby Mathew * observed priority 888c5fe0b5SSoby Mathew * 898c5fe0b5SSoby Mathew * Note that it's possible that more than one contender gets the same 908c5fe0b5SSoby Mathew * ticket value. That's OK as the lock is acquired based on the priority 918c5fe0b5SSoby Mathew * value, not the ticket value alone. 928c5fe0b5SSoby Mathew */ 938c5fe0b5SSoby Mathew my_ticket = 0; 94*1c9573a1SSoby Mathew bakery->lock_data[me] = make_bakery_data(CHOOSING_TICKET, my_ticket); 958c5fe0b5SSoby Mathew for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) { 96*1c9573a1SSoby Mathew their_ticket = bakery_ticket_number(bakery->lock_data[they]); 978c5fe0b5SSoby Mathew if (their_ticket > my_ticket) 988c5fe0b5SSoby Mathew my_ticket = their_ticket; 998c5fe0b5SSoby Mathew } 1008c5fe0b5SSoby Mathew 1018c5fe0b5SSoby Mathew /* 1028c5fe0b5SSoby Mathew * Compute ticket; then signal to other contenders waiting for us to 1038c5fe0b5SSoby Mathew * finish calculating our ticket value that we're done 1048c5fe0b5SSoby Mathew */ 1058c5fe0b5SSoby Mathew ++my_ticket; 106*1c9573a1SSoby Mathew bakery->lock_data[me] = make_bakery_data(CHOSEN_TICKET, my_ticket); 1078c5fe0b5SSoby Mathew 1088c5fe0b5SSoby Mathew return my_ticket; 1098c5fe0b5SSoby Mathew } 1108c5fe0b5SSoby Mathew 1118c5fe0b5SSoby Mathew 1128c5fe0b5SSoby Mathew /* 1138c5fe0b5SSoby Mathew * Acquire bakery lock 1148c5fe0b5SSoby Mathew * 1158c5fe0b5SSoby Mathew * Contending CPUs need first obtain a non-zero ticket and then calculate 1168c5fe0b5SSoby Mathew * priority value. A contending CPU iterate over all other CPUs in the platform, 1178c5fe0b5SSoby Mathew * which may be contending for the same lock, in the order of their ordinal 1188c5fe0b5SSoby Mathew * position (CPU0, CPU1 and so on). A non-contending CPU will have its ticket 1198c5fe0b5SSoby Mathew * (and priority) value as 0. The contending CPU compares its priority with that 1208c5fe0b5SSoby Mathew * of others'. The CPU with the highest priority (lowest numerical value) 1218c5fe0b5SSoby Mathew * acquires the lock 1228c5fe0b5SSoby Mathew */ 1238c5fe0b5SSoby Mathew void bakery_lock_get(bakery_lock_t *bakery) 1248c5fe0b5SSoby Mathew { 1258c5fe0b5SSoby Mathew unsigned int they, me; 1268c5fe0b5SSoby Mathew unsigned int my_ticket, my_prio, their_ticket; 127*1c9573a1SSoby Mathew unsigned int their_bakery_data; 1288c5fe0b5SSoby Mathew 1298c5fe0b5SSoby Mathew me = platform_get_core_pos(read_mpidr_el1()); 1308c5fe0b5SSoby Mathew 1318c5fe0b5SSoby Mathew assert_bakery_entry_valid(me, bakery); 1328c5fe0b5SSoby Mathew 1338c5fe0b5SSoby Mathew /* Prevent recursive acquisition */ 1348c5fe0b5SSoby Mathew assert(bakery->owner != me); 1358c5fe0b5SSoby Mathew 1368c5fe0b5SSoby Mathew /* Get a ticket */ 1378c5fe0b5SSoby Mathew my_ticket = bakery_get_ticket(bakery, me); 1388c5fe0b5SSoby Mathew 1398c5fe0b5SSoby Mathew /* 1408c5fe0b5SSoby Mathew * Now that we got our ticket, compute our priority value, then compare 1418c5fe0b5SSoby Mathew * with that of others, and proceed to acquire the lock 1428c5fe0b5SSoby Mathew */ 1438c5fe0b5SSoby Mathew my_prio = PRIORITY(my_ticket, me); 1448c5fe0b5SSoby Mathew for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) { 1458c5fe0b5SSoby Mathew if (me == they) 1468c5fe0b5SSoby Mathew continue; 1478c5fe0b5SSoby Mathew 1488c5fe0b5SSoby Mathew /* Wait for the contender to get their ticket */ 149*1c9573a1SSoby Mathew do { 150*1c9573a1SSoby Mathew their_bakery_data = bakery->lock_data[they]; 151*1c9573a1SSoby Mathew } while (bakery_is_choosing(their_bakery_data)); 1528c5fe0b5SSoby Mathew 1538c5fe0b5SSoby Mathew /* 1548c5fe0b5SSoby Mathew * If the other party is a contender, they'll have non-zero 1558c5fe0b5SSoby Mathew * (valid) ticket value. If they do, compare priorities 1568c5fe0b5SSoby Mathew */ 157*1c9573a1SSoby Mathew their_ticket = bakery_ticket_number(their_bakery_data); 1588c5fe0b5SSoby Mathew if (their_ticket && (PRIORITY(their_ticket, they) < my_prio)) { 1598c5fe0b5SSoby Mathew /* 1608c5fe0b5SSoby Mathew * They have higher priority (lower value). Wait for 1618c5fe0b5SSoby Mathew * their ticket value to change (either release the lock 1628c5fe0b5SSoby Mathew * to have it dropped to 0; or drop and probably content 1638c5fe0b5SSoby Mathew * again for the same lock to have an even higher value) 1648c5fe0b5SSoby Mathew */ 1658c5fe0b5SSoby Mathew do { 1668c5fe0b5SSoby Mathew wfe(); 167*1c9573a1SSoby Mathew } while (their_ticket == 168*1c9573a1SSoby Mathew bakery_ticket_number(bakery->lock_data[they])); 1698c5fe0b5SSoby Mathew } 1708c5fe0b5SSoby Mathew } 1718c5fe0b5SSoby Mathew 1728c5fe0b5SSoby Mathew /* Lock acquired */ 1738c5fe0b5SSoby Mathew bakery->owner = me; 1748c5fe0b5SSoby Mathew } 1758c5fe0b5SSoby Mathew 1768c5fe0b5SSoby Mathew 1778c5fe0b5SSoby Mathew /* Release the lock and signal contenders */ 1788c5fe0b5SSoby Mathew void bakery_lock_release(bakery_lock_t *bakery) 1798c5fe0b5SSoby Mathew { 1808c5fe0b5SSoby Mathew unsigned int me = platform_get_core_pos(read_mpidr_el1()); 1818c5fe0b5SSoby Mathew 1828c5fe0b5SSoby Mathew assert_bakery_entry_valid(me, bakery); 1838c5fe0b5SSoby Mathew assert(bakery->owner == me); 1848c5fe0b5SSoby Mathew 1858c5fe0b5SSoby Mathew /* 1868c5fe0b5SSoby Mathew * Release lock by resetting ownership and ticket. Then signal other 1878c5fe0b5SSoby Mathew * waiting contenders 1888c5fe0b5SSoby Mathew */ 1898c5fe0b5SSoby Mathew bakery->owner = NO_OWNER; 190*1c9573a1SSoby Mathew bakery->lock_data[me] = 0; 1918c5fe0b5SSoby Mathew dsb(); 1928c5fe0b5SSoby Mathew sev(); 1938c5fe0b5SSoby Mathew } 194