forked from open-power/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap_memcopy.c
359 lines (322 loc) · 9.36 KB
/
snap_memcopy.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
/*
* Copyright 2016, 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 <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
#include <malloc.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <assert.h>
#include <snap_tools.h>
#include <action_memcopy.h>
#include <libsnap.h>
#include <snap_hls_if.h>
int verbose_flag = 0;
static const char *version = GIT_VERSION;
static const char *mem_tab[] = { "HOST_DRAM", "CARD_DRAM", "TYPE_NVME" };
/**
* @brief prints valid command line options
*
* @param prog current program's name
*/
static void usage(const char *prog)
{
printf("Usage: %s [-h] [-v, --verbose] [-V, --version]\n"
" -C, --card <cardno> can be (0...3)\n"
" -i, --input <file.bin> input file.\n"
" -o, --output <file.bin> output file.\n"
" -A, --type-in <CARD_DRAM, HOST_DRAM, ...>.\n"
" -a, --addr-in <addr> address e.g. in CARD_RAM.\n"
" -D, --type-out <CARD_DRAM, HOST_DRAM, ...>.\n"
" -d, --addr-out <addr> address e.g. in CARD_RAM.\n"
" -s, --size <size> size of data.\n"
" -m, --mode <mode> mode flags.\n"
" -t, --timeout Timeout in sec to wait for done. (10 sec default)\n"
" -X, --verify verify result if possible\n"
" -I, --irq Enable Interrupts\n"
"\n"
"Example:\n"
" snap_memcopy ...\n"
"\n",
prog);
}
static void snap_prepare_memcopy(struct snap_job *cjob,
struct memcopy_job *mjob,
void *addr_in,
uint32_t size_in,
uint8_t type_in,
void *addr_out,
uint32_t size_out,
uint8_t type_out)
{
fprintf(stderr, " prepare memcopy job of %ld bytes size\n", sizeof(*mjob));
assert(sizeof(*mjob) <= SNAP_JOBSIZE);
memset(mjob, 0, sizeof(*mjob));
snap_addr_set(&mjob->in, addr_in, size_in, type_in,
SNAP_ADDRFLAG_ADDR | SNAP_ADDRFLAG_SRC);
snap_addr_set(&mjob->out, addr_out, size_out, type_out,
SNAP_ADDRFLAG_ADDR | SNAP_ADDRFLAG_DST |
SNAP_ADDRFLAG_END);
snap_job_set(cjob, mjob, sizeof(*mjob), NULL, 0);
}
/**
* Read accelerator specific registers. Must be called as root!
*/
int main(int argc, char *argv[])
{
int ch, rc = 0;
int card_no = 0;
struct snap_card *card = NULL;
struct snap_action *action = NULL;
char device[128];
struct snap_job cjob;
struct memcopy_job mjob;
const char *input = NULL;
const char *output = NULL;
unsigned long timeout = 10;
unsigned int mode = 0x0;
const char *space = "CARD_RAM";
struct timeval etime, stime;
ssize_t size = 1024 * 1024;
uint8_t *ibuff = NULL, *obuff = NULL;
unsigned int page_size = sysconf(_SC_PAGESIZE);
uint8_t type_in = SNAP_ADDRTYPE_HOST_DRAM;
uint64_t addr_in = 0x0ull;
uint8_t type_out = SNAP_ADDRTYPE_HOST_DRAM;
uint64_t addr_out = 0x0ull;
int verify = 0;
int exit_code = EXIT_SUCCESS;
uint8_t trailing_zeros[1024] = { 0, };
snap_action_flag_t action_irq = 0;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{ "card", required_argument, NULL, 'C' },
{ "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o' },
{ "src-type", required_argument, NULL, 'A' },
{ "src-addr", required_argument, NULL, 'a' },
{ "dst-type", required_argument, NULL, 'D' },
{ "dst-addr", required_argument, NULL, 'd' },
{ "size", required_argument, NULL, 's' },
{ "mode", required_argument, NULL, 'm' },
{ "timeout", required_argument, NULL, 't' },
{ "verify", no_argument, NULL, 'X' },
{ "version", no_argument, NULL, 'V' },
{ "verbose", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ "irq", no_argument, NULL, 'I' },
{ 0, no_argument, NULL, 0 },
};
ch = getopt_long(argc, argv,
"A:C:i:o:a:S:D:d:x:s:t:XVqvhI",
long_options, &option_index);
if (ch == -1)
break;
switch (ch) {
case 'C':
card_no = strtol(optarg, (char **)NULL, 0);
break;
case 'i':
input = optarg;
break;
case 'o':
output = optarg;
break;
case 's':
size = __str_to_num(optarg);
break;
case 't':
timeout = strtol(optarg, (char **)NULL, 0);
break;
case 'm':
mode = strtol(optarg, (char **)NULL, 0);
break;
/* input data */
case 'A':
space = optarg;
if (strcmp(space, "CARD_DRAM") == 0)
type_in = SNAP_ADDRTYPE_CARD_DRAM;
else if (strcmp(space, "HOST_DRAM") == 0)
type_in = SNAP_ADDRTYPE_HOST_DRAM;
else {
usage(argv[0]);
exit(EXIT_FAILURE);
}
break;
case 'a':
addr_in = strtol(optarg, (char **)NULL, 0);
break;
/* output data */
case 'D':
space = optarg;
if (strcmp(space, "CARD_DRAM") == 0)
type_out = SNAP_ADDRTYPE_CARD_DRAM;
else if (strcmp(space, "HOST_DRAM") == 0)
type_out = SNAP_ADDRTYPE_HOST_DRAM;
else {
usage(argv[0]);
exit(EXIT_FAILURE);
}
break;
case 'd':
addr_out = strtol(optarg, (char **)NULL, 0);
break;
case 'X':
verify++;
break;
/* service */
case 'V':
printf("%s\n", version);
exit(EXIT_SUCCESS);
case 'v':
verbose_flag = 1;
break;
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
break;
case 'I':
action_irq = (SNAP_ACTION_DONE_IRQ | SNAP_ATTACH_IRQ);
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind != argc) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
/* if input file is defined, use that as input */
if (input != NULL) {
/* source buffer */
ibuff = memalign(page_size, size);
if (ibuff == NULL)
goto out_error;
memset(ibuff, 0, size);
size = __file_size(input);
if (size < 0)
goto out_error;
fprintf(stdout, "reading input data %d bytes from %s\n",
(int)size, input);
rc = __file_read(input, ibuff, size);
if (rc < 0)
goto out_error;
type_in = SNAP_ADDRTYPE_HOST_DRAM;
addr_in = (unsigned long)ibuff;
}
/* if output file is defined, use that as output */
if (output != NULL) {
size_t set_size = size + (verify ? sizeof(trailing_zeros) : 0);
obuff = memalign(page_size, set_size);
if (obuff == NULL)
goto out_error;
memset(obuff, 0x0, set_size);
type_out = SNAP_ADDRTYPE_HOST_DRAM;
addr_out = (unsigned long)obuff;
}
printf("PARAMETERS:\n"
" input: %s\n"
" output: %s\n"
" type_in: %x %s\n"
" addr_in: %016llx\n"
" type_out: %x %s\n"
" addr_out: %016llx\n"
" size_in/out: %08lx/\n"
" mode: %08x\n",
input ? input : "unknown",
output ? output : "unknown",
type_in, mem_tab[type_in], (long long)addr_in,
type_out, mem_tab[type_out], (long long)addr_out,
size, mode);
snprintf(device, sizeof(device)-1, "/dev/cxl/afu%d.0s", card_no);
card = snap_card_alloc_dev(device, SNAP_VENDOR_ID_IBM,
SNAP_DEVICE_ID_SNAP);
if (card == NULL) {
fprintf(stderr, "err: failed to open card %u: %s\n",
card_no, strerror(errno));
goto out_error;
}
action = snap_attach_action(card, MEMCOPY_ACTION_TYPE, action_irq, 60);
if (action == NULL) {
fprintf(stderr, "err: failed to attach action %u: %s\n",
card_no, strerror(errno));
goto out_error1;
}
snap_prepare_memcopy(&cjob, &mjob,
(void *)addr_in, size, type_in,
(void *)addr_out, size, type_out);
__hexdump(stderr, &mjob, sizeof(mjob));
gettimeofday(&stime, NULL);
rc = snap_action_sync_execute_job(action, &cjob, timeout);
gettimeofday(&etime, NULL);
if (rc != 0) {
fprintf(stderr, "err: job execution %d: %s!\n", rc,
strerror(errno));
goto out_error2;
}
/* If the output buffer is in host DRAM we can write it to a file */
if (output != NULL) {
fprintf(stdout, "writing output data %p %d bytes to %s\n",
obuff, (int)size, output);
rc = __file_write(output, obuff, size);
if (rc < 0)
goto out_error2;
}
/* obuff[size] = 0xff; */
fprintf(stdout, "RETC=%x\n", cjob.retc);
if (cjob.retc != SNAP_RETC_SUCCESS) {
fprintf(stderr, "err: Unexpected RETC=%x!\n", cjob.retc);
goto out_error2;
}
if (verify) {
if ((type_in == SNAP_ADDRTYPE_HOST_DRAM) &&
(type_out == SNAP_ADDRTYPE_HOST_DRAM)) {
rc = memcmp(ibuff, obuff, size);
if (rc != 0)
exit_code = EX_ERR_VERIFY;
rc = memcmp(obuff + size, trailing_zeros, 1024);
if (rc != 0) {
fprintf(stderr, "err: trailing zero "
"verification failed!\n");
__hexdump(stderr, obuff + size, 1024);
exit_code = EX_ERR_VERIFY;
}
} else
fprintf(stderr, "warn: Verification works currently "
"only with HOST_DRAM\n");
}
fprintf(stdout, "memcopy took %lld usec\n",
(long long)timediff_usec(&etime, &stime));
snap_detach_action(action);
snap_card_free(card);
__free(obuff);
__free(ibuff);
exit(exit_code);
out_error2:
snap_detach_action(action);
out_error1:
snap_card_free(card);
out_error:
__free(obuff);
__free(ibuff);
exit(EXIT_FAILURE);
}