[5753fbb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[5753fbb] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[f24d300] | 29 | /** @addtogroup amd64
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_amd64_ATOMIC_H_
|
---|
| 36 | #define KERN_amd64_ATOMIC_H_
|
---|
[5753fbb] | 37 |
|
---|
[d99c1d2] | 38 | #include <typedefs.h>
|
---|
[53f9821] | 39 | #include <arch/barrier.h>
|
---|
| 40 | #include <preemption.h>
|
---|
[7a0359b] | 41 | #include <trace.h>
|
---|
[5753fbb] | 42 |
|
---|
[7a0359b] | 43 | NO_TRACE static inline void atomic_inc(atomic_t *val)
|
---|
[228666c] | 44 | {
|
---|
[5753fbb] | 45 | #ifdef CONFIG_SMP
|
---|
[f24d300] | 46 | asm volatile (
|
---|
| 47 | "lock incq %[count]\n"
|
---|
| 48 | : [count] "+m" (val->count)
|
---|
| 49 | );
|
---|
[5753fbb] | 50 | #else
|
---|
[f24d300] | 51 | asm volatile (
|
---|
| 52 | "incq %[count]\n"
|
---|
| 53 | : [count] "+m" (val->count)
|
---|
| 54 | );
|
---|
[5753fbb] | 55 | #endif /* CONFIG_SMP */
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[7a0359b] | 58 | NO_TRACE static inline void atomic_dec(atomic_t *val)
|
---|
[228666c] | 59 | {
|
---|
[5753fbb] | 60 | #ifdef CONFIG_SMP
|
---|
[f24d300] | 61 | asm volatile (
|
---|
| 62 | "lock decq %[count]\n"
|
---|
| 63 | : [count] "+m" (val->count)
|
---|
| 64 | );
|
---|
[5753fbb] | 65 | #else
|
---|
[f24d300] | 66 | asm volatile (
|
---|
| 67 | "decq %[count]\n"
|
---|
| 68 | : [count] "+m" (val->count)
|
---|
| 69 | );
|
---|
[5753fbb] | 70 | #endif /* CONFIG_SMP */
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[7a0359b] | 73 | NO_TRACE static inline atomic_count_t atomic_postinc(atomic_t *val)
|
---|
[5753fbb] | 74 | {
|
---|
[228666c] | 75 | atomic_count_t r = 1;
|
---|
[f24d300] | 76 |
|
---|
[e7b7be3f] | 77 | asm volatile (
|
---|
[f24d300] | 78 | "lock xaddq %[r], %[count]\n"
|
---|
[228666c] | 79 | : [count] "+m" (val->count),
|
---|
| 80 | [r] "+r" (r)
|
---|
[5753fbb] | 81 | );
|
---|
[f24d300] | 82 |
|
---|
[5753fbb] | 83 | return r;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[7a0359b] | 86 | NO_TRACE static inline atomic_count_t atomic_postdec(atomic_t *val)
|
---|
[5753fbb] | 87 | {
|
---|
[228666c] | 88 | atomic_count_t r = -1;
|
---|
[5753fbb] | 89 |
|
---|
[e7b7be3f] | 90 | asm volatile (
|
---|
[f24d300] | 91 | "lock xaddq %[r], %[count]\n"
|
---|
[228666c] | 92 | : [count] "+m" (val->count),
|
---|
| 93 | [r] "+r" (r)
|
---|
[5753fbb] | 94 | );
|
---|
| 95 |
|
---|
| 96 | return r;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[f24d300] | 99 | #define atomic_preinc(val) (atomic_postinc(val) + 1)
|
---|
| 100 | #define atomic_predec(val) (atomic_postdec(val) - 1)
|
---|
[5753fbb] | 101 |
|
---|
[7a0359b] | 102 | NO_TRACE static inline atomic_count_t test_and_set(atomic_t *val)
|
---|
[228666c] | 103 | {
|
---|
[ba371e1] | 104 | atomic_count_t v = 1;
|
---|
[5753fbb] | 105 |
|
---|
[e7b7be3f] | 106 | asm volatile (
|
---|
[f24d300] | 107 | "xchgq %[v], %[count]\n"
|
---|
[ba371e1] | 108 | : [v] "+r" (v),
|
---|
[228666c] | 109 | [count] "+m" (val->count)
|
---|
[5753fbb] | 110 | );
|
---|
| 111 |
|
---|
| 112 | return v;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[23684b7] | 115 | /** amd64 specific fast spinlock */
|
---|
[7a0359b] | 116 | NO_TRACE static inline void atomic_lock_arch(atomic_t *val)
|
---|
[53f9821] | 117 | {
|
---|
[228666c] | 118 | atomic_count_t tmp;
|
---|
[f24d300] | 119 |
|
---|
[53f9821] | 120 | preemption_disable();
|
---|
[e7b7be3f] | 121 | asm volatile (
|
---|
[ad2e39b] | 122 | "0:\n"
|
---|
[7a0359b] | 123 | " pause\n"
|
---|
| 124 | " mov %[count], %[tmp]\n"
|
---|
| 125 | " testq %[tmp], %[tmp]\n"
|
---|
| 126 | " jnz 0b\n" /* lightweight looping on locked spinlock */
|
---|
[53f9821] | 127 |
|
---|
[7a0359b] | 128 | " incq %[tmp]\n" /* now use the atomic operation */
|
---|
| 129 | " xchgq %[count], %[tmp]\n"
|
---|
| 130 | " testq %[tmp], %[tmp]\n"
|
---|
| 131 | " jnz 0b\n"
|
---|
[228666c] | 132 | : [count] "+m" (val->count),
|
---|
| 133 | [tmp] "=&r" (tmp)
|
---|
[ad2e39b] | 134 | );
|
---|
[228666c] | 135 |
|
---|
[53f9821] | 136 | /*
|
---|
| 137 | * Prevent critical section code from bleeding out this way up.
|
---|
| 138 | */
|
---|
| 139 | CS_ENTER_BARRIER();
|
---|
| 140 | }
|
---|
[5753fbb] | 141 |
|
---|
| 142 | #endif
|
---|
[b45c443] | 143 |
|
---|
[06e1e95] | 144 | /** @}
|
---|
[b45c443] | 145 | */
|
---|