blob: 44524cc8c32a6ca179d67b8df91394c516ee6046 [file] [log] [blame]
Luca Barbieri86a89382010-02-24 10:54:24 +01001/*
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 */
11#include <linux/init.h>
Peter Huewe0dbdd1b2010-05-24 12:13:20 -070012#include <linux/kernel.h>
Luca Barbieri86a89382010-02-24 10:54:24 +010013#include <asm/atomic.h>
14
15#define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
16static __init int test_atomic64(void)
17{
18 long long v0 = 0xaaa31337c001d00dLL;
19 long long v1 = 0xdeadbeefdeafcafeLL;
20 long long v2 = 0xfaceabadf00df001LL;
21 long long onestwos = 0x1111111122222222LL;
22 long long one = 1LL;
23
24 atomic64_t v = ATOMIC64_INIT(v0);
25 long long r = v0;
26 BUG_ON(v.counter != r);
27
28 atomic64_set(&v, v1);
29 r = v1;
30 BUG_ON(v.counter != r);
31 BUG_ON(atomic64_read(&v) != r);
32
33 INIT(v0);
34 atomic64_add(onestwos, &v);
35 r += onestwos;
36 BUG_ON(v.counter != r);
37
38 INIT(v0);
39 atomic64_add(-one, &v);
40 r += -one;
41 BUG_ON(v.counter != r);
42
43 INIT(v0);
44 r += onestwos;
45 BUG_ON(atomic64_add_return(onestwos, &v) != r);
46 BUG_ON(v.counter != r);
47
48 INIT(v0);
49 r += -one;
50 BUG_ON(atomic64_add_return(-one, &v) != r);
51 BUG_ON(v.counter != r);
52
53 INIT(v0);
54 atomic64_sub(onestwos, &v);
55 r -= onestwos;
56 BUG_ON(v.counter != r);
57
58 INIT(v0);
59 atomic64_sub(-one, &v);
60 r -= -one;
61 BUG_ON(v.counter != r);
62
63 INIT(v0);
64 r -= onestwos;
65 BUG_ON(atomic64_sub_return(onestwos, &v) != r);
66 BUG_ON(v.counter != r);
67
68 INIT(v0);
69 r -= -one;
70 BUG_ON(atomic64_sub_return(-one, &v) != r);
71 BUG_ON(v.counter != r);
72
73 INIT(v0);
74 atomic64_inc(&v);
75 r += one;
76 BUG_ON(v.counter != r);
77
78 INIT(v0);
79 r += one;
80 BUG_ON(atomic64_inc_return(&v) != r);
81 BUG_ON(v.counter != r);
82
83 INIT(v0);
84 atomic64_dec(&v);
85 r -= one;
86 BUG_ON(v.counter != r);
87
88 INIT(v0);
89 r -= one;
90 BUG_ON(atomic64_dec_return(&v) != r);
91 BUG_ON(v.counter != r);
92
93 INIT(v0);
94 BUG_ON(atomic64_xchg(&v, v1) != v0);
95 r = v1;
96 BUG_ON(v.counter != r);
97
98 INIT(v0);
99 BUG_ON(atomic64_cmpxchg(&v, v0, v1) != v0);
100 r = v1;
101 BUG_ON(v.counter != r);
102
103 INIT(v0);
104 BUG_ON(atomic64_cmpxchg(&v, v2, v1) != v0);
105 BUG_ON(v.counter != r);
106
107 INIT(v0);
Luca Barbieri9efbcd52010-03-01 19:55:45 +0100108 BUG_ON(atomic64_add_unless(&v, one, v0));
Luca Barbieri86a89382010-02-24 10:54:24 +0100109 BUG_ON(v.counter != r);
110
111 INIT(v0);
Luca Barbieri9efbcd52010-03-01 19:55:45 +0100112 BUG_ON(!atomic64_add_unless(&v, one, v1));
Luca Barbieri86a89382010-02-24 10:54:24 +0100113 r += one;
114 BUG_ON(v.counter != r);
115
Heiko Carstens007d0862010-06-04 14:15:02 -0700116#if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
Will Deaconc58bbd32010-07-08 10:59:59 +0100117 defined(CONFIG_S390) || defined(_ASM_GENERIC_ATOMIC64_H) || defined(CONFIG_ARM)
Luca Barbieri86a89382010-02-24 10:54:24 +0100118 INIT(onestwos);
119 BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
120 r -= one;
121 BUG_ON(v.counter != r);
122
123 INIT(0);
124 BUG_ON(atomic64_dec_if_positive(&v) != -one);
125 BUG_ON(v.counter != r);
126
127 INIT(-one);
128 BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
129 BUG_ON(v.counter != r);
Luca Barbieri8f4f2022010-02-26 12:22:40 +0100130#else
131#warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above
132#endif
Luca Barbieri86a89382010-02-24 10:54:24 +0100133
134 INIT(onestwos);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100135 BUG_ON(!atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100136 r += one;
137 BUG_ON(v.counter != r);
138
139 INIT(0);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100140 BUG_ON(atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100141 BUG_ON(v.counter != r);
142
143 INIT(-one);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100144 BUG_ON(!atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100145 r += one;
146 BUG_ON(v.counter != r);
147
148#ifdef CONFIG_X86
H. Peter Anvina5c91612010-03-01 11:49:23 -0800149 printk(KERN_INFO "atomic64 test passed for %s platform %s CX8 and %s SSE\n",
150#ifdef CONFIG_X86_64
151 "x86-64",
152#elif defined(CONFIG_X86_CMPXCHG64)
153 "i586+",
Luca Barbieri86a89382010-02-24 10:54:24 +0100154#else
H. Peter Anvina5c91612010-03-01 11:49:23 -0800155 "i386+",
Luca Barbieri86a89382010-02-24 10:54:24 +0100156#endif
H. Peter Anvina5c91612010-03-01 11:49:23 -0800157 boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
158 boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
Luca Barbieri86a89382010-02-24 10:54:24 +0100159#else
160 printk(KERN_INFO "atomic64 test passed\n");
161#endif
162
163 return 0;
164}
165
166core_initcall(test_atomic64);