Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Testsuite for atomic64_t functions |
| 3 | * |
| 4 | * Copyright © 2010 Luca Barbieri |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame^] | 11 | |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 14 | #include <linux/init.h> |
Paul Gortmaker | 50af5ea | 2012-01-20 18:35:53 -0500 | [diff] [blame] | 15 | #include <linux/bug.h> |
Peter Huewe | 0dbdd1b | 2010-05-24 12:13:20 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 17 | #include <linux/atomic.h> |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 18 | |
| 19 | #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0) |
| 20 | static __init int test_atomic64(void) |
| 21 | { |
| 22 | long long v0 = 0xaaa31337c001d00dLL; |
| 23 | long long v1 = 0xdeadbeefdeafcafeLL; |
| 24 | long long v2 = 0xfaceabadf00df001LL; |
| 25 | long long onestwos = 0x1111111122222222LL; |
| 26 | long long one = 1LL; |
| 27 | |
| 28 | atomic64_t v = ATOMIC64_INIT(v0); |
| 29 | long long r = v0; |
| 30 | BUG_ON(v.counter != r); |
| 31 | |
| 32 | atomic64_set(&v, v1); |
| 33 | r = v1; |
| 34 | BUG_ON(v.counter != r); |
| 35 | BUG_ON(atomic64_read(&v) != r); |
| 36 | |
| 37 | INIT(v0); |
| 38 | atomic64_add(onestwos, &v); |
| 39 | r += onestwos; |
| 40 | BUG_ON(v.counter != r); |
| 41 | |
| 42 | INIT(v0); |
| 43 | atomic64_add(-one, &v); |
| 44 | r += -one; |
| 45 | BUG_ON(v.counter != r); |
| 46 | |
| 47 | INIT(v0); |
| 48 | r += onestwos; |
| 49 | BUG_ON(atomic64_add_return(onestwos, &v) != r); |
| 50 | BUG_ON(v.counter != r); |
| 51 | |
| 52 | INIT(v0); |
| 53 | r += -one; |
| 54 | BUG_ON(atomic64_add_return(-one, &v) != r); |
| 55 | BUG_ON(v.counter != r); |
| 56 | |
| 57 | INIT(v0); |
| 58 | atomic64_sub(onestwos, &v); |
| 59 | r -= onestwos; |
| 60 | BUG_ON(v.counter != r); |
| 61 | |
| 62 | INIT(v0); |
| 63 | atomic64_sub(-one, &v); |
| 64 | r -= -one; |
| 65 | BUG_ON(v.counter != r); |
| 66 | |
| 67 | INIT(v0); |
| 68 | r -= onestwos; |
| 69 | BUG_ON(atomic64_sub_return(onestwos, &v) != r); |
| 70 | BUG_ON(v.counter != r); |
| 71 | |
| 72 | INIT(v0); |
| 73 | r -= -one; |
| 74 | BUG_ON(atomic64_sub_return(-one, &v) != r); |
| 75 | BUG_ON(v.counter != r); |
| 76 | |
| 77 | INIT(v0); |
| 78 | atomic64_inc(&v); |
| 79 | r += one; |
| 80 | BUG_ON(v.counter != r); |
| 81 | |
| 82 | INIT(v0); |
| 83 | r += one; |
| 84 | BUG_ON(atomic64_inc_return(&v) != r); |
| 85 | BUG_ON(v.counter != r); |
| 86 | |
| 87 | INIT(v0); |
| 88 | atomic64_dec(&v); |
| 89 | r -= one; |
| 90 | BUG_ON(v.counter != r); |
| 91 | |
| 92 | INIT(v0); |
| 93 | r -= one; |
| 94 | BUG_ON(atomic64_dec_return(&v) != r); |
| 95 | BUG_ON(v.counter != r); |
| 96 | |
| 97 | INIT(v0); |
| 98 | BUG_ON(atomic64_xchg(&v, v1) != v0); |
| 99 | r = v1; |
| 100 | BUG_ON(v.counter != r); |
| 101 | |
| 102 | INIT(v0); |
| 103 | BUG_ON(atomic64_cmpxchg(&v, v0, v1) != v0); |
| 104 | r = v1; |
| 105 | BUG_ON(v.counter != r); |
| 106 | |
| 107 | INIT(v0); |
| 108 | BUG_ON(atomic64_cmpxchg(&v, v2, v1) != v0); |
| 109 | BUG_ON(v.counter != r); |
| 110 | |
| 111 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 112 | BUG_ON(atomic64_add_unless(&v, one, v0)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 113 | BUG_ON(v.counter != r); |
| 114 | |
| 115 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 116 | BUG_ON(!atomic64_add_unless(&v, one, v1)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 117 | r += one; |
| 118 | BUG_ON(v.counter != r); |
| 119 | |
Catalin Marinas | 7463449 | 2012-07-30 14:41:09 -0700 | [diff] [blame] | 120 | #ifdef CONFIG_ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 121 | INIT(onestwos); |
| 122 | BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1)); |
| 123 | r -= one; |
| 124 | BUG_ON(v.counter != r); |
| 125 | |
| 126 | INIT(0); |
| 127 | BUG_ON(atomic64_dec_if_positive(&v) != -one); |
| 128 | BUG_ON(v.counter != r); |
| 129 | |
| 130 | INIT(-one); |
| 131 | BUG_ON(atomic64_dec_if_positive(&v) != (-one - one)); |
| 132 | BUG_ON(v.counter != r); |
Luca Barbieri | 8f4f202 | 2010-02-26 12:22:40 +0100 | [diff] [blame] | 133 | #else |
Catalin Marinas | 7463449 | 2012-07-30 14:41:09 -0700 | [diff] [blame] | 134 | #warning Please implement atomic64_dec_if_positive for your architecture and select the above Kconfig symbol |
Luca Barbieri | 8f4f202 | 2010-02-26 12:22:40 +0100 | [diff] [blame] | 135 | #endif |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 136 | |
| 137 | INIT(onestwos); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 138 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 139 | r += one; |
| 140 | BUG_ON(v.counter != r); |
| 141 | |
| 142 | INIT(0); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 143 | BUG_ON(atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 144 | BUG_ON(v.counter != r); |
| 145 | |
| 146 | INIT(-one); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 147 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 148 | r += one; |
| 149 | BUG_ON(v.counter != r); |
| 150 | |
| 151 | #ifdef CONFIG_X86 |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame^] | 152 | pr_info("passed for %s platform %s CX8 and %s SSE\n", |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 153 | #ifdef CONFIG_X86_64 |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame^] | 154 | "x86-64", |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 155 | #elif defined(CONFIG_X86_CMPXCHG64) |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame^] | 156 | "i586+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 157 | #else |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame^] | 158 | "i386+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 159 | #endif |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 160 | boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without", |
| 161 | boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 162 | #else |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame^] | 163 | pr_info("passed\n"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 164 | #endif |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | core_initcall(test_atomic64); |