Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 1 | /* |
Alexei Fedorov | 3a377b2 | 2019-05-10 16:55:16 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <asm_macros.S> |
| 8 | |
| 9 | .globl spin_lock |
| 10 | .globl spin_unlock |
| 11 | |
Jeenu Viswambharan | 0bc79d9 | 2017-08-16 11:44:25 +0100 | [diff] [blame] | 12 | #if ARM_ARCH_AT_LEAST(8, 1) |
Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 13 | |
Jeenu Viswambharan | fca7680 | 2017-01-16 16:52:35 +0000 | [diff] [blame] | 14 | /* |
| 15 | * When compiled for ARMv8.1 or later, choose spin locks based on Compare and |
| 16 | * Swap instruction. |
| 17 | */ |
| 18 | # define USE_CAS 1 |
| 19 | |
| 20 | /* |
| 21 | * Lock contenders using CAS, upon failing to acquire the lock, wait with the |
| 22 | * monitor in open state. Therefore, a normal store upon unlocking won't |
| 23 | * generate an SEV. Use explicit SEV instruction with CAS unlock. |
| 24 | */ |
| 25 | # define COND_SEV() sev |
| 26 | |
| 27 | #else |
| 28 | |
| 29 | # define USE_CAS 0 |
| 30 | |
| 31 | /* |
| 32 | * Lock contenders using exclusive pairs, upon failing to acquire the lock, wait |
| 33 | * with the monitor in exclusive state. A normal store upon unlocking will |
| 34 | * implicitly generate an envent; so, no explicit SEV with unlock is required. |
| 35 | */ |
| 36 | # define COND_SEV() |
| 37 | |
| 38 | #endif |
| 39 | |
| 40 | #if USE_CAS |
| 41 | |
Jeenu Viswambharan | fca7680 | 2017-01-16 16:52:35 +0000 | [diff] [blame] | 42 | /* |
| 43 | * Acquire lock using Compare and Swap instruction. |
| 44 | * |
| 45 | * Compare for 0 with acquire semantics, and swap 1. Wait until CAS returns |
| 46 | * 0. |
| 47 | * |
| 48 | * void spin_lock(spinlock_t *lock); |
| 49 | */ |
Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 50 | func spin_lock |
| 51 | mov w2, #1 |
| 52 | sevl |
Jeenu Viswambharan | fca7680 | 2017-01-16 16:52:35 +0000 | [diff] [blame] | 53 | 1: |
| 54 | wfe |
| 55 | mov w1, wzr |
| 56 | casa w1, w2, [x0] |
| 57 | cbnz w1, 1b |
| 58 | ret |
| 59 | endfunc spin_lock |
| 60 | |
Jeenu Viswambharan | fca7680 | 2017-01-16 16:52:35 +0000 | [diff] [blame] | 61 | #else /* !USE_CAS */ |
| 62 | |
| 63 | /* |
| 64 | * Acquire lock using load-/store-exclusive instruction pair. |
| 65 | * |
| 66 | * void spin_lock(spinlock_t *lock); |
| 67 | */ |
| 68 | func spin_lock |
| 69 | mov w2, #1 |
| 70 | sevl |
Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 71 | l1: wfe |
| 72 | l2: ldaxr w1, [x0] |
| 73 | cbnz w1, l1 |
| 74 | stxr w1, w2, [x0] |
| 75 | cbnz w1, l2 |
| 76 | ret |
| 77 | endfunc spin_lock |
| 78 | |
Jeenu Viswambharan | fca7680 | 2017-01-16 16:52:35 +0000 | [diff] [blame] | 79 | #endif /* USE_CAS */ |
Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 80 | |
Jeenu Viswambharan | fca7680 | 2017-01-16 16:52:35 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Release lock previously acquired by spin_lock. |
| 83 | * |
| 84 | * Unconditionally write 0, and conditionally generate an event. |
| 85 | * |
| 86 | * void spin_unlock(spinlock_t *lock); |
| 87 | */ |
Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 88 | func spin_unlock |
| 89 | stlr wzr, [x0] |
Jeenu Viswambharan | fca7680 | 2017-01-16 16:52:35 +0000 | [diff] [blame] | 90 | COND_SEV() |
Soby Mathew | 7b5c9b3 | 2016-08-08 12:42:53 +0100 | [diff] [blame] | 91 | ret |
| 92 | endfunc spin_unlock |