Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +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 | |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 7 | #ifndef BAKERY_LOCK_H |
| 8 | #define BAKERY_LOCK_H |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 9 | |
Dan Handley | ed6ff95 | 2014-05-14 17:44:19 +0100 | [diff] [blame] | 10 | #include <platform_def.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 11 | |
| 12 | #define BAKERY_LOCK_MAX_CPUS PLATFORM_CORE_COUNT |
| 13 | |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 14 | #ifndef __ASSEMBLY__ |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 15 | #include <cdefs.h> |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 16 | #include <stdbool.h> |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 17 | #include <stdint.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 18 | |
| 19 | #include <lib/utils_def.h> |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 20 | |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 21 | /***************************************************************************** |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 22 | * Internal helpers used by the bakery lock implementation. |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 23 | ****************************************************************************/ |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 24 | |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 25 | /* Convert a ticket to priority */ |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 26 | static inline unsigned int bakery_get_priority(unsigned int t, unsigned int pos) |
| 27 | { |
| 28 | return (t << 8) | pos; |
| 29 | } |
| 30 | |
| 31 | #define CHOOSING_TICKET U(0x1) |
| 32 | #define CHOSEN_TICKET U(0x0) |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 33 | |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 34 | static inline bool bakery_is_choosing(unsigned int info) |
| 35 | { |
| 36 | return (info & 1U) == CHOOSING_TICKET; |
| 37 | } |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 38 | |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 39 | static inline unsigned int bakery_ticket_number(unsigned int info) |
| 40 | { |
| 41 | return (info >> 1) & 0x7FFFU; |
| 42 | } |
| 43 | |
| 44 | static inline uint16_t make_bakery_data(unsigned int choosing, unsigned int num) |
| 45 | { |
| 46 | unsigned int val = (choosing & 0x1U) | (num << 1); |
| 47 | |
| 48 | return (uint16_t) val; |
| 49 | } |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 50 | |
| 51 | /***************************************************************************** |
| 52 | * External bakery lock interface. |
| 53 | ****************************************************************************/ |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 54 | #if USE_COHERENT_MEM |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 55 | /* |
| 56 | * Bakery locks are stored in coherent memory |
| 57 | * |
| 58 | * Each lock's data is contiguous and fully allocated by the compiler |
| 59 | */ |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 60 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 61 | typedef struct bakery_lock { |
Soby Mathew | a0a897d | 2015-02-19 16:23:51 +0000 | [diff] [blame] | 62 | /* |
| 63 | * The lock_data is a bit-field of 2 members: |
| 64 | * Bit[0] : choosing. This field is set when the CPU is |
| 65 | * choosing its bakery number. |
| 66 | * Bits[1 - 15] : number. This is the bakery number allocated. |
| 67 | */ |
| 68 | volatile uint16_t lock_data[BAKERY_LOCK_MAX_CPUS]; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 69 | } bakery_lock_t; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 70 | |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 71 | #else |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 72 | /* |
| 73 | * Bakery locks are stored in normal .bss memory |
| 74 | * |
| 75 | * Each lock's data is spread across multiple cache lines, one per CPU, |
| 76 | * but multiple locks can share the same cache line. |
| 77 | * The compiler will allocate enough memory for one CPU's bakery locks, |
| 78 | * the remaining cache lines are allocated by the linker script |
| 79 | */ |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 80 | |
| 81 | typedef struct bakery_info { |
| 82 | /* |
| 83 | * The lock_data is a bit-field of 2 members: |
| 84 | * Bit[0] : choosing. This field is set when the CPU is |
| 85 | * choosing its bakery number. |
| 86 | * Bits[1 - 15] : number. This is the bakery number allocated. |
| 87 | */ |
| 88 | volatile uint16_t lock_data; |
| 89 | } bakery_info_t; |
| 90 | |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 91 | typedef bakery_info_t bakery_lock_t; |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 92 | |
| 93 | #endif /* __USE_COHERENT_MEM__ */ |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 94 | |
Sandrine Bailleux | 37a12df | 2016-04-11 13:17:50 +0100 | [diff] [blame] | 95 | static inline void bakery_lock_init(bakery_lock_t *bakery) {} |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 96 | void bakery_lock_get(bakery_lock_t *bakery); |
| 97 | void bakery_lock_release(bakery_lock_t *bakery); |
| 98 | |
Soren Brinkmann | 46dd170 | 2016-01-14 10:11:05 -0800 | [diff] [blame] | 99 | #define DEFINE_BAKERY_LOCK(_name) bakery_lock_t _name __section("bakery_lock") |
Andrew Thoelke | e466c9f | 2015-09-10 11:39:36 +0100 | [diff] [blame] | 100 | |
| 101 | #define DECLARE_BAKERY_LOCK(_name) extern bakery_lock_t _name |
| 102 | |
| 103 | |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 104 | #endif /* __ASSEMBLY__ */ |
Antonio Nino Diaz | 519248b | 2018-10-31 15:55:57 +0000 | [diff] [blame] | 105 | #endif /* BAKERY_LOCK_H */ |