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