| 1 | #ifndef __CB_BDK_SPINLOCK_H__ |
| 2 | #define __CB_BDK_SPINLOCK_H__ |
| 3 | /***********************license start*********************************** |
| 4 | * Copyright (c) 2003-2017 Cavium Inc. ([email protected]). All rights |
| 5 | * reserved. |
| 6 | * |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are |
| 10 | * met: |
| 11 | * |
| 12 | * * Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * |
| 15 | * * Redistributions in binary form must reproduce the above |
| 16 | * copyright notice, this list of conditions and the following |
| 17 | * disclaimer in the documentation and/or other materials provided |
| 18 | * with the distribution. |
| 19 | * |
| 20 | * * Neither the name of Cavium Inc. nor the names of |
| 21 | * its contributors may be used to endorse or promote products |
| 22 | * derived from this software without specific prior written |
| 23 | * permission. |
| 24 | * |
| 25 | * This Software, including technical data, may be subject to U.S. export |
| 26 | * control laws, including the U.S. Export Administration Act and its |
| 27 | * associated regulations, and may be subject to export or import |
| 28 | * regulations in other countries. |
| 29 | * |
| 30 | * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" |
| 31 | * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR |
| 32 | * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT |
| 33 | * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY |
| 34 | * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT |
| 35 | * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES |
| 36 | * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR |
| 37 | * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, |
| 38 | * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK |
| 39 | * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU. |
| 40 | ***********************license end**************************************/ |
| 41 | |
| 42 | /** |
| 43 | * @file |
| 44 | * |
| 45 | * Implementation of spinlocks. |
| 46 | * |
| 47 | * <hr>$Revision: 49448 $<hr> |
| 48 | * |
| 49 | * @addtogroup hal |
| 50 | * @{ |
| 51 | */ |
| 52 | |
| 53 | /** |
| 54 | * Spinlocks |
| 55 | */ |
| 56 | typedef union |
| 57 | { |
| 58 | uint64_t combined; |
| 59 | struct |
| 60 | { |
| 61 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 62 | uint32_t ticket; |
| 63 | uint32_t serving; |
| 64 | #else |
| 65 | uint32_t serving; |
| 66 | uint32_t ticket; |
| 67 | #endif |
| 68 | } s; |
| 69 | } bdk_spinlock_t; |
| 70 | |
| 71 | /** |
| 72 | * Initialize a spinlock |
| 73 | * |
| 74 | * @param lock Lock to initialize |
| 75 | */ |
| 76 | static inline void bdk_spinlock_init(bdk_spinlock_t *lock) |
| 77 | { |
| 78 | asm volatile ("str xzr, [%[b]]" |
| 79 | : "+m" (lock->combined) |
| 80 | : [b] "r" (&lock->combined) |
| 81 | : "memory"); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Releases lock |
| 86 | * |
| 87 | * @param lock pointer to lock structure |
| 88 | */ |
| 89 | static inline void bdk_spinlock_unlock(bdk_spinlock_t *lock) __attribute__ ((always_inline)); |
| 90 | static inline void bdk_spinlock_unlock(bdk_spinlock_t *lock) |
| 91 | { |
| 92 | /* Implies a release */ |
| 93 | asm volatile ("stlr %w[v], [%[b]]" |
| 94 | : "+m" (lock->s.serving) |
| 95 | : [v] "r" (lock->s.serving + 1), [b] "r" (&lock->s.serving) |
| 96 | : "memory"); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets lock, spins until lock is taken |
| 101 | * |
| 102 | * @param lock pointer to lock structure |
| 103 | */ |
| 104 | static inline void bdk_spinlock_lock(bdk_spinlock_t *lock) __attribute__ ((always_inline)); |
| 105 | static inline void bdk_spinlock_lock(bdk_spinlock_t *lock) |
| 106 | { |
| 107 | uint64_t combined; |
| 108 | uint32_t ticket; |
| 109 | uint32_t serving; |
| 110 | |
| 111 | asm volatile ( |
| 112 | "mov %x[serving], 1<<32 \n" |
| 113 | "ldadda %x[serving], %x[combined], [%[ptr]] \n" |
| 114 | "and %x[serving], %x[combined], 0xffffffff \n" |
| 115 | "lsr %x[ticket], %x[combined], 32 \n" |
| 116 | "cmp %x[ticket], %x[serving] \n" |
| 117 | "b.eq 1f \n" |
| 118 | "sevl \n" |
| 119 | "2: wfe \n" |
| 120 | "ldxr %w[serving], [%[ptr2]] \n" |
| 121 | "cmp %x[ticket], %x[serving] \n" |
| 122 | "b.ne 2b \n" |
| 123 | "1: \n" |
| 124 | : [serving] "=&r" (serving), [ticket] "=&r" (ticket), [combined] "=&r" (combined), "+m" (lock->combined) |
| 125 | : [ptr] "r" (&lock->combined), [ptr2] "r" (&lock->s.serving) |
| 126 | : "memory" |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Trys to get the lock, failing if we can't get it immediately |
| 132 | * |
| 133 | * @param lock pointer to lock structure |
| 134 | */ |
| 135 | static inline int bdk_spinlock_trylock(bdk_spinlock_t *lock) __attribute__ ((always_inline)); |
| 136 | static inline int bdk_spinlock_trylock(bdk_spinlock_t *lock) |
| 137 | { |
| 138 | uint64_t combined = *(volatile uint64_t *)&lock->combined; |
| 139 | uint32_t ticket = combined >> 32; |
| 140 | uint32_t serving = (uint32_t)combined; |
| 141 | if (ticket != serving) |
| 142 | return -1; |
| 143 | uint64_t new_combined = combined + (1ull << 32); |
| 144 | bool success = bdk_atomic_compare_and_store64(&lock->combined, combined, new_combined); |
| 145 | return success ? 0 : -1; |
| 146 | } |
| 147 | |
| 148 | /** @} */ |
| 149 | #endif /* !__CB_BDK_SPINLOCK_H__ */ |