forked from open-power/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap_example_set.c
397 lines (356 loc) · 10 KB
/
snap_example_set.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright 2017, International Business Machines
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/time.h>
#include <getopt.h>
#include <ctype.h>
#include <stdbool.h>
#include <linux/random.h>
#include <libsnap.h>
#include <snap_tools.h>
#include <snap_s_regs.h>
#include "snap_example.h"
/* defaults */
#define ACTION_WAIT_TIME 1 /* Default in sec */
#define KILO_BYTE (1024)
#define MEGA_BYTE (1024*1024)
#define DEFAULT_MEM 0xff
static const char *version = GIT_VERSION;
static int verbose_level = 0;
#define PRINTF0(fmt, ...) do { \
printf(fmt, ## __VA_ARGS__); \
} while (0)
#define PRINTF1(fmt, ...) do { \
if (verbose_level > 0) \
printf(fmt, ## __VA_ARGS__); \
} while (0)
#define PRINTF2(fmt, ...) do { \
if (verbose_level > 1) \
printf(fmt, ## __VA_ARGS__); \
} while (0)
#define PRINTF3(fmt, ...) do { \
if (verbose_level > 2) \
printf(fmt, ## __VA_ARGS__); \
} while (0)
#define PRINTF4(fmt, ...) do { \
if (verbose_level > 3) \
printf(fmt, ## __VA_ARGS__); \
} while (0)
static uint64_t get_usec(void)
{
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec * 1000000 + t.tv_usec;
}
static void print_time(uint64_t elapsed)
{
if (elapsed > 10000)
PRINTF1(" T = %d msec" ,(int)elapsed/1000);
else PRINTF1(" T = %d usec", (int)elapsed);
}
/*
* Check Pattern in buffer
*/
static int check_buffer(uint8_t *hb, /* Host Buffer */
int begin, /* Offset in Buffer */
int size,
uint8_t pattern) /* Patthern to check */
{
int i;
int rc, bad;
uint8_t data;
PRINTF2("\nCheck %d bytes in Buffer Start at: %d for Pattern: 0x%02x",
size, begin, pattern);
rc = bad = 0;
for (i = 0; i < begin; i++) {
data = *hb; /* Get data */
if (data != DEFAULT_MEM) {
PRINTF0("\nError1@: %d Expect: 0x%02x Read: 0x%02x",
i, /* Address */
DEFAULT_MEM, /* What i expect */
data); /* What i got */
bad++;
if (bad > 10) rc = 1; /* Exit */
}
if (rc) return 1;
hb++;
}
for (i = 0; i < size; i++) {
data = *hb; /* Get data */
if (data != pattern) {
PRINTF0("\nError@: %d Expect: 0x%02x Read: 0x%02x",
i, /* Address */
pattern, /* What i expect */
data); /* What i got */
bad++;
if (bad > 9) rc = 1; /* Exit */
}
if (rc) return 2;
hb++;
}
return 0;
}
/* Action or Kernel Write and Read are 32 bit MMIO */
static void action_write(struct snap_card* h, uint32_t addr, uint32_t data)
{
int rc;
rc = snap_mmio_write32(h, (uint64_t)addr, data);
if (0 != rc)
PRINTF0("Write MMIO 32 Err\n");
return;
}
/*
* Start Action and wait for Idle.
*/
static int action_wait_idle(struct snap_card* h, int timeout)
{
uint64_t t_start; /* time in usec */
uint64_t td = 0;
int rc = 1;
/* FIXME Use act and not h */
snap_action_start((void*)h);
t_start = get_usec();
/* Wait for Action to go back to Idle */
if (snap_action_completed((void*)h, NULL, timeout))
rc = 0; /* Good */
td = get_usec() - t_start;
print_time(td);
return rc;
}
static void action_start(struct snap_card* h,
int action,
uint64_t dest, /* End address for MEMSET */
uint64_t src) /* Start address for MEMSET */
{
uint64_t addr;
uint8_t pattern;
pattern = (action & 0xff00) >> 8;
switch (action & 0xff) {
case ACTION_CONFIG_MEMSET_H:
PRINTF2("\n[Host]");
break;
case ACTION_CONFIG_MEMSET_F:
PRINTF2("\n[FPGA]");
break;
default:
PRINTF0("\nInvalid Action\n");
return;
break;
}
PRINTF2(" Set: %lld Bytes with Pattern: 0x%02x ",
(long long)(dest-src), pattern);
action_write(h, ACTION_CONFIG, action);
addr = src;
action_write(h, ACTION_SRC_LOW, (uint32_t)(addr & 0xffffffff));
action_write(h, ACTION_SRC_HIGH, (uint32_t)(addr >> 32));
addr = dest;
action_write(h, ACTION_DEST_LOW, (uint32_t)(addr & 0xffffffff));
action_write(h, ACTION_DEST_HIGH, (uint32_t)(addr >> 32));
return;
}
static void free_mem(void *buffer)
{
PRINTF3("Free Host Buffer: %p\n", buffer);
if (buffer)
free(buffer);
}
static void usage(const char *prog)
{
PRINTF0("Usage: %s\n"
" -h, --help print usage information\n"
" -v, --verbose verbose mode\n"
" -C, --card <cardno> use this card for operation\n"
" -V, --version\n"
" -q, --quiet quiece output\n"
" -t, --timeout timeout in sec (default=1 sec)\n"
" -i, --iter Number of Iterations (default 1)\n"
" -H, --host Set Host Memory (defualt)\n"
" -F, --fpga Set FPGA Memory\n"
" -s, --size Size to fill (default %d)\n"
" -b, --begin Byte offset to begin set (default 0)\n"
" -p, --pattern Byte Pattern to use (default 0x0)\n"
" -I, --irq Use Interrupts\n"
"\tSNAP Test tool to Set Memory in Host (Option -H) or FPGA (Option -F) Card\n"
, prog,
(4 * KILO_BYTE));
}
int main(int argc, char *argv[])
{
char device[64];
struct snap_card *dn; /* lib snap handle */
int card_no = 0;
int cmd;
int rc = 1;
int i, iter = 1;
int timeout = ACTION_WAIT_TIME;
long size = (4 * KILO_BYTE);
uint64_t begin = 0;
uint8_t pattern = 0x0;
int func = ACTION_CONFIG_MEMSET_H;
void *hb = NULL;
int h_mem_size = 0;
int h_begin = 0;
uint64_t start, stop;
snap_action_flag_t attach_flags = 0;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{ "card", required_argument, NULL, 'C' },
{ "verbose", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "quiet", no_argument, NULL, 'q' },
{ "iter", required_argument, NULL, 'i' },
{ "timeout", required_argument, NULL, 't' },
{ "host", required_argument, NULL, 'H' },
{ "fpga", required_argument, NULL, 'F' },
{ "size", required_argument, NULL, 's' },
{ "begin", required_argument, NULL, 'b' },
{ "pattern", required_argument, NULL, 'p' },
{ "irq", required_argument, NULL, 'I' },
{ 0, no_argument, NULL, 0 },
};
cmd = getopt_long(argc, argv, "C:i:t:s:b:p:IFHqvVh",
long_options, &option_index);
if (cmd == -1) /* all params processed ? */
break;
switch (cmd) {
case 'v': /* verbose */
verbose_level++;
break;
case 'V': /* version */
PRINTF0("%s\n", version);
exit(EXIT_SUCCESS);;
case 'h': /* help */
usage(argv[0]);
exit(EXIT_SUCCESS);;
case 'C': /* card */
card_no = strtol(optarg, (char **)NULL, 0);
break;
case 'i': /* iter */
iter = strtol(optarg, (char **)NULL, 0);
break;
case 't': /* timeout */
timeout = strtol(optarg, (char **)NULL, 0);
break;
case 'H': /* host */
func = ACTION_CONFIG_MEMSET_H;
break;
case 'F': /* fpga */
func = ACTION_CONFIG_MEMSET_F;
break;
case 's': /* size */
size = strtol(optarg, (char **)NULL, 0);
if ((errno == ERANGE && (size == LONG_MAX || size == LONG_MIN))
|| (errno != 0 && size == 0)) {
PRINTF0("Error errno %d\n", errno);
exit(1);
}
if ((size > 0x100000000) || (size == 0)) {
PRINTF0("Error: Size %ld -s or --size Out of range\n", size);
exit(1);
}
break;
case 'b': /* begin */
begin = strtoll(optarg, (char **)NULL, 0);
break;
case 'p': /* pattern */
pattern = strtol(optarg, (char **)NULL, 0);
break;
case 'I': /* irq */
attach_flags |= SNAP_ACTION_DONE_IRQ | SNAP_ATTACH_IRQ;
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (card_no > 3) {
usage(argv[0]);
exit(1);
}
PRINTF2("Start Memset Test. Timeout: %d sec Device: ",
timeout);
sprintf(device, "/dev/cxl/afu%d.0s", card_no);
dn = snap_card_alloc_dev(device, SNAP_VENDOR_ID_IBM, SNAP_DEVICE_ID_SNAP);
PRINTF2("%s\n", device);
if (NULL == dn) {
errno = ENODEV;
PRINTF0("ERROR: snap_card_alloc_dev(%s)\n", device);
return -1;
}
if (ACTION_CONFIG_MEMSET_H == func) {
if (size > (32 * MEGA_BYTE)) {
PRINTF0("Size %ld must be less than %d\n", size, (32*MEGA_BYTE));
goto __exit1;
}
h_begin = (int)(begin & 0xF) + 16;
h_mem_size = (h_begin + size + 32);
/* Allocate Host Buffer */
if (posix_memalign((void **)&hb, 4096, h_mem_size) != 0) {
perror("FAILED: posix_memalign source");
goto __exit1;
}
PRINTF1("Allocate Host Buffer at %p Size: %d Bytes\n", hb, h_mem_size);
}
PRINTF1("Fill %ld Bytes from %lld to %lld with Pattern 0x%02x\n",
size, (long long)begin, (long long)begin+size-1, pattern);
for (i = 0; i < iter; i++) {
struct snap_action *act;
PRINTF1("[%d/%d] Start Memset ", i+1, iter);
PRINTF1(" Attach %x", ACTION_TYPE_EXAMPLE);
act = snap_attach_action(dn, ACTION_TYPE_EXAMPLE, attach_flags, 5*timeout);
if (NULL == act) {
PRINTF0(" ERROR from Attach Action:%x\n",
ACTION_TYPE_EXAMPLE);
goto __exit1;
}
if (ACTION_CONFIG_MEMSET_F == func) {
start = begin;
stop = begin + size -1;
action_start(dn, func | (pattern << 8), stop, start);
if (0 != action_wait_idle(dn, timeout))
goto __exit1;
}
if (ACTION_CONFIG_MEMSET_H == func) {
/* Set Host Buffer */
memset(hb, DEFAULT_MEM, h_mem_size);
start = (uint64_t)hb + h_begin; /* Host Start Address */
stop = start + size - 1; /* Host End Address */
action_start(dn, func | (pattern << 8), stop, start);
if (0 != action_wait_idle(dn, timeout))
goto __exit1;
if (0 != check_buffer(hb, h_begin, size, pattern))
goto __exit1;
}
snap_detach_action(act);
PRINTF1(" done\n");
}
rc = 0;
__exit1:
PRINTF1("\n");
PRINTF3("Close Card Handle: %p\n", dn);
snap_card_free(dn);
free_mem(hb);
PRINTF2("Exit rc: %d\n", rc);
return rc;
}