functionality test added
[warm.git] / test.c
CommitLineData
38e809e1 1/*
2 * wARM functionality test
3 *
4 * written by GraÅžvydas "notaz" Ignotas
5 *
6 * see warm.c for license info
7 */
198a1649 8#include <stdio.h>
38e809e1 9#include <stdlib.h>
10#include <string.h>
11#include <sys/time.h>
12#include <time.h>
13#include <unistd.h>
14
15#if 1
198a1649 16#include "warm.h"
38e809e1 17#else
18#define warm_init(...) 0
19#define warm_finish(...)
20#define warm_cache_op_range(...)
21#define warm_change_cb_range(...)
22#endif
23
24typedef unsigned long long u64;
25static u64 start_time, end_time;
26
27static unsigned char buff[8 * 1024 * 1024] __attribute__((aligned(32)));
28static unsigned char *buff_mid = &buff[8 * 1024 * 1024 / 2];
29
30static u64 xtime(void)
31{
32 struct timeval tv;
33 gettimeofday(&tv, NULL);
34 return (u64)tv.tv_sec * 1000000 + tv.tv_usec;
35}
36
37static void test_start(void)
38{
39 start_time = xtime();
40}
41
42static void test_end(void)
43{
44 end_time = xtime();
45}
46
47static void show_result(const char *name, int bytes)
48{
49 double secs = (end_time - start_time) / 1000000.0;
50 printf("%-16s: %4.1fs, %5.1f MB/s\n", name, secs, bytes / secs / 1048576);
51}
52
53static void do_memset(void)
54{
55 memset(buff, 0, sizeof(buff));
56}
57
58static void byte_fill(void)
59{
60 char *p = (void *)buff;
61 int i;
62 for (i = sizeof(buff); i > 0; i--)
63 *p++ = 0;
64}
65
66static void word_fill(void)
67{
68 long *p = (void *)buff;
69 int i;
70 for (i = sizeof(buff) / sizeof(*p); i > 0; i--)
71 *p++ = 0;
72}
73
74static void do_memcpy(void)
75{
76 memcpy(buff, buff_mid, sizeof(buff) / 2);
77}
78
79static void byte_cpy(void)
80{
81 char *d = (void *)buff;
82 char *s = (void *)buff_mid;
83 int i;
84 for (i = sizeof(buff) / sizeof(*d) / 2; i > 0; i--)
85 *d++ = *s++;
86}
87
88static void word_cpy(void)
89{
90 long *d = (void *)buff;
91 long *s = (void *)buff_mid;
92 int i;
93 for (i = sizeof(buff) / sizeof(*d) / 2; i > 0; i--)
94 *d++ = *s++;
95}
96
97static void word_inc(void)
98{
99 long *p = (void *)buff;
100 int i;
101
102 for (i = sizeof(buff) / sizeof(*p); i > 0; i--) {
103 (*p)++;
104 p++;
105 }
106}
107
108#define ONE_TEST(count, func) \
109 test_start(); \
110 for (i = count; i > 0; i--) \
111 func(); \
112 test_end()
113
114static void tests(void)
115{
116 int i;
117
118 ONE_TEST(64, do_memset);
119 show_result("memset", sizeof(buff) * 64);
120
121 ONE_TEST(64, byte_fill);
122 show_result("byte fill", sizeof(buff) * 64);
123
124 ONE_TEST(64, word_fill);
125 show_result("word fill", sizeof(buff) * 64);
126
127 ONE_TEST(128, do_memcpy);
128 show_result("memcpy", sizeof(buff) * 128 / 2);
129
130 ONE_TEST(128, byte_cpy);
131 show_result("byte copy", sizeof(buff) * 128 / 2);
132
133 ONE_TEST(128, word_cpy);
134 show_result("word copy", sizeof(buff) * 128 / 2);
135
136 ONE_TEST(64, word_inc);
137 show_result("word inc", sizeof(buff) * 64);
138
139 usleep(200000);
140}
141
142#if 1
143#include <sys/types.h>
144#include <sys/stat.h>
145#include <fcntl.h>
146#include <sys/mman.h>
147
148void coherency_test(void)
149{
150 volatile unsigned char *buff_mapped;
151 volatile unsigned char *buff_vol;
152 unsigned long buff_phys, align;
153 unsigned char buff_mapped_vals[5];
154 unsigned char buff_vals[5];
155 int random_offs;
156 int memdev;
157
158 srand(time(NULL));
159
160 buff_phys = warm_virt2phys(buff);
161 align = buff_phys & 0xfff;
162
163 memdev = open("/dev/mem", O_RDONLY | O_SYNC);
164 if (memdev < 0) {
165 perror("open /dev/mem");
166 return;
167 }
168
169 /* the mapping is valid for 1 page only. */
170 buff_mapped = mmap(NULL, 0x1000, PROT_READ,
171 MAP_SHARED, memdev, buff_phys & ~0xfff);
172 if (buff_mapped == MAP_FAILED) {
173 perror("mmap buff");
174 return;
175 }
176 buff_mapped += align;
177
178 random_offs = rand() % (0x1000 - align);
179
180 buff_vol = (volatile void *)buff;
181 buff_vals[0] = buff_vol[random_offs]; buff_mapped_vals[0] = buff_mapped[random_offs];
182
183 /* incremented: */
184 buff_vol[random_offs]++;
185 buff_vals[1] = buff_vol[random_offs]; buff_mapped_vals[1] = buff_mapped[random_offs];
186
187 /* cleaned: */
188 warm_cache_op_range(WOP_D_CLEAN, (char *)buff_vol + random_offs, 32);
189 buff_vals[2] = buff_vol[random_offs]; buff_mapped_vals[2] = buff_mapped[random_offs];
190
191 /* incremented: */
192 buff_vol[random_offs]++;
193 buff_vals[3] = buff_vol[random_offs]; buff_mapped_vals[3] = buff_mapped[random_offs];
194
195 /* invalidated: */
196 warm_cache_op_range(WOP_D_INVALIDATE, (char *)buff_vol + random_offs, 32);
197 buff_vals[4] = buff_vol[random_offs]; buff_mapped_vals[4] = buff_mapped[random_offs];
198
199 printf("buff is @ %p -> %lx, mapped %p, random offset %x\n", buff_vol, buff_phys,
200 buff_mapped, random_offs);
201 printf("val: %02x, mmaped: %02x\n", buff_vals[0], buff_mapped_vals[0]);
202
203 printf("incremented:\n");
204 printf("val: %02x, mmaped: %02x\n", buff_vals[1], buff_mapped_vals[1]);
205
206 printf("cleaned:\n");
207 printf("val: %02x, mmaped: %02x\n", buff_vals[2], buff_mapped_vals[2]);
208
209 printf("incremented:\n");
210 printf("val: %02x, mmaped: %02x\n", buff_vals[3], buff_mapped_vals[3]);
211
212 printf("invalidated:\n");
213 printf("val: %02x, mmaped: %02x\n", buff_vals[4], buff_mapped_vals[4]);
214}
215#else
216#define coherency_test()
217#endif
198a1649 218
219int main()
220{
38e809e1 221 int ret;
222
223 ret = warm_init();
224 if (ret != 0)
225 {
226 printf("init failed.\n");
227 return 1;
228 }
229
230 printf("buff: %p - %p\n", buff, buff + sizeof(buff) - 1);
231
232 printf("-- default --\n");
233 tests();
234
235 printf("-- ncnb --\n");
236 warm_change_cb_range(WCB_C_BIT|WCB_B_BIT, 0, buff, sizeof(buff));
237 tests();
238
239 printf("-- nc b --\n");
240 warm_change_cb_range(WCB_B_BIT, 1, buff, sizeof(buff));
241 tests();
242
243 printf("-- cnb --\n");
244 warm_change_cb_range(WCB_C_BIT, 1, buff, sizeof(buff));
245 warm_change_cb_range(WCB_B_BIT, 0, buff, sizeof(buff));
246 tests();
247
248 warm_change_cb_range(WCB_C_BIT|WCB_B_BIT, 1, buff, sizeof(buff));
249 coherency_test();
250
251 warm_finish();
198a1649 252
253 return 0;
254}
255