1 /* 2 * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef BAKERY_LOCK_H 8 #define BAKERY_LOCK_H 9 10 #include <platform_def.h> 11 12 #define BAKERY_LOCK_MAX_CPUS PLATFORM_CORE_COUNT 13 14 #ifndef __ASSEMBLY__ 15 #include <cdefs.h> 16 #include <stdbool.h> 17 #include <stdint.h> 18 #include <utils_def.h> 19 20 /***************************************************************************** 21 * Internal helpers used by the bakery lock implementation. 22 ****************************************************************************/ 23 24 /* Convert a ticket to priority */ 25 static inline unsigned int bakery_get_priority(unsigned int t, unsigned int pos) 26 { 27 return (t << 8) | pos; 28 } 29 30 #define CHOOSING_TICKET U(0x1) 31 #define CHOSEN_TICKET U(0x0) 32 33 static inline bool bakery_is_choosing(unsigned int info) 34 { 35 return (info & 1U) == CHOOSING_TICKET; 36 } 37 38 static inline unsigned int bakery_ticket_number(unsigned int info) 39 { 40 return (info >> 1) & 0x7FFFU; 41 } 42 43 static inline uint16_t make_bakery_data(unsigned int choosing, unsigned int num) 44 { 45 unsigned int val = (choosing & 0x1U) | (num << 1); 46 47 return (uint16_t) val; 48 } 49 50 /***************************************************************************** 51 * External bakery lock interface. 52 ****************************************************************************/ 53 #if USE_COHERENT_MEM 54 /* 55 * Bakery locks are stored in coherent memory 56 * 57 * Each lock's data is contiguous and fully allocated by the compiler 58 */ 59 60 typedef struct bakery_lock { 61 /* 62 * The lock_data is a bit-field of 2 members: 63 * Bit[0] : choosing. This field is set when the CPU is 64 * choosing its bakery number. 65 * Bits[1 - 15] : number. This is the bakery number allocated. 66 */ 67 volatile uint16_t lock_data[BAKERY_LOCK_MAX_CPUS]; 68 } bakery_lock_t; 69 70 #else 71 /* 72 * Bakery locks are stored in normal .bss memory 73 * 74 * Each lock's data is spread across multiple cache lines, one per CPU, 75 * but multiple locks can share the same cache line. 76 * The compiler will allocate enough memory for one CPU's bakery locks, 77 * the remaining cache lines are allocated by the linker script 78 */ 79 80 typedef struct bakery_info { 81 /* 82 * The lock_data is a bit-field of 2 members: 83 * Bit[0] : choosing. This field is set when the CPU is 84 * choosing its bakery number. 85 * Bits[1 - 15] : number. This is the bakery number allocated. 86 */ 87 volatile uint16_t lock_data; 88 } bakery_info_t; 89 90 typedef bakery_info_t bakery_lock_t; 91 92 #endif /* __USE_COHERENT_MEM__ */ 93 94 static inline void bakery_lock_init(bakery_lock_t *bakery) {} 95 void bakery_lock_get(bakery_lock_t *bakery); 96 void bakery_lock_release(bakery_lock_t *bakery); 97 98 #define DEFINE_BAKERY_LOCK(_name) bakery_lock_t _name __section("bakery_lock") 99 100 #define DECLARE_BAKERY_LOCK(_name) extern bakery_lock_t _name 101 102 103 #endif /* __ASSEMBLY__ */ 104 #endif /* BAKERY_LOCK_H */ 105