[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[add04f7] | 29 | /** @addtogroup ia32
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_ia32_ATOMIC_H_
|
---|
| 36 | #define KERN_ia32_ATOMIC_H_
|
---|
[f761f1eb] | 37 |
|
---|
| 38 | #include <arch/types.h>
|
---|
[53f9821] | 39 | #include <arch/barrier.h>
|
---|
| 40 | #include <preemption.h>
|
---|
[59e07c91] | 41 |
|
---|
| 42 | static inline void atomic_inc(atomic_t *val) {
|
---|
[5f85c91] | 43 | #ifdef CONFIG_SMP
|
---|
[add04f7] | 44 | asm volatile (
|
---|
| 45 | "lock incl %[count]\n"
|
---|
| 46 | : [count] "+m" (val->count)
|
---|
| 47 | );
|
---|
[18e0a6c] | 48 | #else
|
---|
[add04f7] | 49 | asm volatile (
|
---|
| 50 | "incl %[count]\n"
|
---|
| 51 | : [count] "+m" (val->count)
|
---|
| 52 | );
|
---|
[5f85c91] | 53 | #endif /* CONFIG_SMP */
|
---|
[18e0a6c] | 54 | }
|
---|
| 55 |
|
---|
[59e07c91] | 56 | static inline void atomic_dec(atomic_t *val) {
|
---|
[5f85c91] | 57 | #ifdef CONFIG_SMP
|
---|
[add04f7] | 58 | asm volatile (
|
---|
| 59 | "lock decl %[count]\n"
|
---|
| 60 | : [count] "+m" (val->count)
|
---|
| 61 | );
|
---|
[18e0a6c] | 62 | #else
|
---|
[add04f7] | 63 | asm volatile (
|
---|
| 64 | "decl %[count]\n"
|
---|
[f36c061] | 65 | : [count] "+m" (val->count)
|
---|
[add04f7] | 66 | );
|
---|
[5f85c91] | 67 | #endif /* CONFIG_SMP */
|
---|
[18e0a6c] | 68 | }
|
---|
| 69 |
|
---|
[23684b7] | 70 | static inline long atomic_postinc(atomic_t *val)
|
---|
[73a4bab] | 71 | {
|
---|
[e5dc7b8] | 72 | long r = 1;
|
---|
[add04f7] | 73 |
|
---|
[e7b7be3f] | 74 | asm volatile (
|
---|
[add04f7] | 75 | "lock xaddl %[r], %[count]\n"
|
---|
| 76 | : [count] "+m" (val->count), [r] "+r" (r)
|
---|
[73a4bab] | 77 | );
|
---|
[add04f7] | 78 |
|
---|
[73a4bab] | 79 | return r;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[23684b7] | 82 | static inline long atomic_postdec(atomic_t *val)
|
---|
[73a4bab] | 83 | {
|
---|
[e5dc7b8] | 84 | long r = -1;
|
---|
[10c071e] | 85 |
|
---|
[e7b7be3f] | 86 | asm volatile (
|
---|
[add04f7] | 87 | "lock xaddl %[r], %[count]\n"
|
---|
| 88 | : [count] "+m" (val->count), [r] "+r"(r)
|
---|
[73a4bab] | 89 | );
|
---|
[10c071e] | 90 |
|
---|
[73a4bab] | 91 | return r;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[add04f7] | 94 | #define atomic_preinc(val) (atomic_postinc(val) + 1)
|
---|
| 95 | #define atomic_predec(val) (atomic_postdec(val) - 1)
|
---|
[73a4bab] | 96 |
|
---|
[7f1c620] | 97 | static inline uint32_t test_and_set(atomic_t *val) {
|
---|
| 98 | uint32_t v;
|
---|
[18e0a6c] | 99 |
|
---|
[e7b7be3f] | 100 | asm volatile (
|
---|
[add04f7] | 101 | "movl $1, %[v]\n"
|
---|
| 102 | "xchgl %[v], %[count]\n"
|
---|
| 103 | : [v] "=r" (v), [count] "+m" (val->count)
|
---|
[18e0a6c] | 104 | );
|
---|
| 105 |
|
---|
| 106 | return v;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[23684b7] | 109 | /** ia32 specific fast spinlock */
|
---|
[53f9821] | 110 | static inline void atomic_lock_arch(atomic_t *val)
|
---|
| 111 | {
|
---|
[7f1c620] | 112 | uint32_t tmp;
|
---|
[add04f7] | 113 |
|
---|
[53f9821] | 114 | preemption_disable();
|
---|
[e7b7be3f] | 115 | asm volatile (
|
---|
[9f491d7] | 116 | "0:\n"
|
---|
[add04f7] | 117 | "pause\n" /* Pentium 4's HT love this instruction */
|
---|
| 118 | "mov %[count], %[tmp]\n"
|
---|
| 119 | "testl %[tmp], %[tmp]\n"
|
---|
[9f491d7] | 120 | "jnz 0b\n" /* lightweight looping on locked spinlock */
|
---|
[53f9821] | 121 |
|
---|
[add04f7] | 122 | "incl %[tmp]\n" /* now use the atomic operation */
|
---|
| 123 | "xchgl %[count], %[tmp]\n"
|
---|
| 124 | "testl %[tmp], %[tmp]\n"
|
---|
[9f491d7] | 125 | "jnz 0b\n"
|
---|
[add04f7] | 126 | : [count] "+m" (val->count), [tmp] "=&r" (tmp)
|
---|
[9f491d7] | 127 | );
|
---|
[53f9821] | 128 | /*
|
---|
| 129 | * Prevent critical section code from bleeding out this way up.
|
---|
| 130 | */
|
---|
| 131 | CS_ENTER_BARRIER();
|
---|
| 132 | }
|
---|
[f761f1eb] | 133 |
|
---|
| 134 | #endif
|
---|
[b45c443] | 135 |
|
---|
[06e1e95] | 136 | /** @}
|
---|
[b45c443] | 137 | */
|
---|