blob: 0211d30d8c390f06299c27a43285ee83748c6944 [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 */
Fabian Frederickb3b16d22014-06-04 16:12:00 -070011
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Luca Barbieri86a89382010-02-24 10:54:24 +010014#include <linux/init.h>
Paul Gortmaker50af5ea2012-01-20 18:35:53 -050015#include <linux/bug.h>
Peter Huewe0dbdd1b2010-05-24 12:13:20 -070016#include <linux/kernel.h>
Arun Sharma600634972011-07-26 16:09:06 -070017#include <linux/atomic.h>
Luca Barbieri86a89382010-02-24 10:54:24 +010018
19#define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
20static __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 Barbieri9efbcd52010-03-01 19:55:45 +0100112 BUG_ON(atomic64_add_unless(&v, one, v0));
Luca Barbieri86a89382010-02-24 10:54:24 +0100113 BUG_ON(v.counter != r);
114
115 INIT(v0);
Luca Barbieri9efbcd52010-03-01 19:55:45 +0100116 BUG_ON(!atomic64_add_unless(&v, one, v1));
Luca Barbieri86a89382010-02-24 10:54:24 +0100117 r += one;
118 BUG_ON(v.counter != r);
119
Catalin Marinas74634492012-07-30 14:41:09 -0700120#ifdef CONFIG_ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
Luca Barbieri86a89382010-02-24 10:54:24 +0100121 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 Barbieri8f4f2022010-02-26 12:22:40 +0100133#else
Catalin Marinas74634492012-07-30 14:41:09 -0700134#warning Please implement atomic64_dec_if_positive for your architecture and select the above Kconfig symbol
Luca Barbieri8f4f2022010-02-26 12:22:40 +0100135#endif
Luca Barbieri86a89382010-02-24 10:54:24 +0100136
137 INIT(onestwos);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100138 BUG_ON(!atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100139 r += one;
140 BUG_ON(v.counter != r);
141
142 INIT(0);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100143 BUG_ON(atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100144 BUG_ON(v.counter != r);
145
146 INIT(-one);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100147 BUG_ON(!atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100148 r += one;
149 BUG_ON(v.counter != r);
150
151#ifdef CONFIG_X86
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700152 pr_info("passed for %s platform %s CX8 and %s SSE\n",
H. Peter Anvina5c91612010-03-01 11:49:23 -0800153#ifdef CONFIG_X86_64
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700154 "x86-64",
H. Peter Anvina5c91612010-03-01 11:49:23 -0800155#elif defined(CONFIG_X86_CMPXCHG64)
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700156 "i586+",
Luca Barbieri86a89382010-02-24 10:54:24 +0100157#else
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700158 "i386+",
Luca Barbieri86a89382010-02-24 10:54:24 +0100159#endif
H. Peter Anvina5c91612010-03-01 11:49:23 -0800160 boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
161 boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
Luca Barbieri86a89382010-02-24 10:54:24 +0100162#else
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700163 pr_info("passed\n");
Luca Barbieri86a89382010-02-24 10:54:24 +0100164#endif
165
166 return 0;
167}
168
169core_initcall(test_atomic64);