1
/*******************************************************************
2
* This is a test program for the IOctl interface of the
6
* $Date: 2006-11-17 18:49:01 $
8
*******************************************************************/
10
/*******************************************************************
13
* $Log: not supported by cvs2svn $
14
* Revision 1.2 2006/10/16 16:56:09 marcus
15
* Added nice comment at the start.
17
*******************************************************************/
26
#include <sys/ioctl.h>
33
#include "driver/pciDriver.h"
39
//#define BIGBUFSIZE (512*1024*1024)
40
#define BIGBUFSIZE (1024*1024)
43
#define DEFAULT_FPGA_DEVICE "/dev/fpga0"
47
#define SEPARATOR_WIDTH 2
48
#define BLOCK_SEPARATOR_WIDTH 2
50
#define BENCHMARK_ITERATIONS 128
76
static struct option long_options[] = {
77
{"device", required_argument, 0, OPT_DEVICE },
78
{"bar", required_argument, 0, OPT_BAR },
79
{"access", required_argument, 0, OPT_ACCESS },
80
{"size", required_argument, 0, OPT_SIZE },
81
{"info", no_argument, 0, OPT_INFO },
82
{"list", no_argument, 0, OPT_LIST },
83
{"benchmark", no_argument, 0, OPT_BENCHMARK },
84
{"read", optional_argument, 0, OPT_READ },
85
{"write", optional_argument, 0, OPT_WRITE },
86
{"help", no_argument, 0, OPT_HELP },
91
void Usage(int argc, char *argv[], const char *format, ...) {
96
printf("Error %i: ", errno);
107
" %s <mode> [options] [hex data]\n"
109
" -i - Device Info\n"
110
" -l - List Data Banks\n"
111
" -p - Performance Evaluation\n"
112
" -r <addr> - Read Data\n"
113
" -w <addr> - Write Data\n"
114
" --help - Help message\n"
117
" -d <device> - FPGA device (/dev/fpga0)\n"
118
" -b <bank> - Data bank (autodetected)\n"
121
" -s <size> - Number of words (default: 1)\n"
122
" -a <bitness> - Bits per word (default: 32)\n"
129
void Error(const char *format, ...) {
132
va_start(ap, format);
133
printf("Error %i: ", errno);
135
if (errno) printf("\n errno: %s", strerror(errno));
142
static pci_board_info board_info;
143
static page_mask = -1;
145
void GetBoardInfo(int handle) {
149
ret = ioctl( handle, PCIDRIVER_IOC_PCI_INFO, &board_info );
150
if (ret) Error("PCIDRIVER_IOC_PCI_INFO ioctl have failed");
152
page_mask = get_page_mask();
156
void List(int handle) {
159
GetBoardInfo(handle);
161
for (i = 0; i < MAX_BANKS; i++) {
162
if (board_info.bar_length[i] > 0) {
163
printf(" BAR %d - Start: 0x%x, Length: 0x%x\n", i, board_info.bar_start[i], board_info.bar_length[i] );
168
void Info(int handle) {
169
GetBoardInfo(handle);
171
printf("Vendor: %lx, Device: %lx, Interrupt Pin: %i, Interrupt Line: %i\n", board_info.vendor_id, board_info.device_id, board_info.interrupt_pin, board_info.interrupt_line);
176
int DetectBar(int handle, unsigned long addr, int size) {
179
GetBoardInfo(handle);
181
for (i = 0; i < MAX_BANKS; i++) {
182
if ((addr >= board_info.bar_start[i])&&((board_info.bar_start[i] + board_info.bar_length[i]) >= (addr + size))) return i;
188
void *DetectAddress(int handle, int *bar, unsigned long *addr, int size) {
190
*bar = DetectBar(handle, *addr, size);
191
if (*bar < 0) Error("The requested data block at address 0x%x with size 0x%x does not belongs to any available memory bank", *addr, size);
193
GetBoardInfo(handle);
195
if ((*addr < board_info.bar_start[*bar])||((board_info.bar_start[*bar] + board_info.bar_length[*bar]) < (((uintptr_t)*addr) + size))) {
196
if ((board_info.bar_length[*bar]) >= (((uintptr_t)*addr) + size))
197
*addr += board_info.bar_start[*bar];
199
Error("The requested data block at address 0x%x with size 0x%x does not belong the specified memory bank (Bar %i: starting at 0x%x with size 0x%x)", *addr, size, *bar, board_info.bar_start[*bar], board_info.bar_length[*bar]);
203
*addr -= board_info.bar_start[*bar];
204
*addr += board_info.bar_start[*bar] & page_mask;
212
void *MapBar(int handle, int bar) {
216
GetBoardInfo(handle);
218
ret = ioctl( handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_PCI );
219
if (ret) Error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed", bar);
221
ret = ioctl( handle, PCIDRIVER_IOC_MMAP_AREA, PCIDRIVER_BAR0 + bar );
222
if (ret) Error("PCIDRIVER_IOC_MMAP_AREA ioctl have failed for bank %i", bar);
225
file_io_handle = open("/root/drivers/pciDriver/data", O_RDWR);
226
res = mmap( 0, board_info.bar_length[bar], PROT_WRITE | PROT_READ, MAP_SHARED, file_io_handle, 0 );
228
res = mmap( 0, board_info.bar_length[bar], PROT_WRITE | PROT_READ, MAP_SHARED, handle, 0 );
230
if ((!res)||(res == MAP_FAILED)) Error("Failed to mmap data bank %i", bar);
236
void UnmapBar(int handle, int bar, void *data) {
237
munmap(data, board_info.bar_length[bar]);
239
close(file_io_handle);
245
int Read(void *buf, int handle, int bar, unsigned long addr, int size) {
249
char local_buf[size];
253
DetectAddress(handle, &bar, &addr, size);
254
data = MapBar(handle, bar);
257
for (i = 0; i < size/4; i++) {
258
((uint32_t*)((char*)data+addr))[i] = 0x100 * i + 1;
262
memcpy0(buf, data + addr, size);
264
UnmapBar(handle, bar, data);
267
int Write(void *buf, int handle, int bar, unsigned long addr, int size) {
271
char local_buf[size];
274
DetectAddress(handle, &bar, &addr, size);
275
data = MapBar(handle, bar);
277
memcpy0(data + addr, buf, size);
279
UnmapBar(handle, bar, data);
283
int Benchmark(int handle, int bar) {
285
void *data, *buf, *check;
286
struct timeval start, end;
288
unsigned int size, max_size;
290
GetBoardInfo(handle);
293
for (i = 0; i < MAX_BANKS; i++) {
294
if (board_info.bar_length[i] > 0) {
300
if (bar < 0) Error("Data banks are not available");
304
max_size = board_info.bar_length[bar];
306
posix_memalign( (void**)&buf, 256, max_size );
307
posix_memalign( (void**)&check, 256, max_size );
308
if ((!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", max_size);
310
printf("Transfer time:\n");
311
data = MapBar(handle, bar);
313
for (size = 4 ; size < max_size; size *= 8) {
314
gettimeofday(&start,NULL);
315
for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
316
memcpy0(buf, data, size);
318
gettimeofday(&end,NULL);
320
time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
321
printf("%8i bytes - read: %8.2lf MB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024 * 1024));
325
gettimeofday(&start,NULL);
326
for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
327
memcpy0(data, buf, size);
329
gettimeofday(&end,NULL);
331
time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
332
printf(", write: %8.2lf MB/s\n", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024 * 1024));
335
UnmapBar(handle, bar, data);
337
printf("\n\nOpen-Transfer-Close time: \n");
339
for (size = 4 ; size < max_size; size *= 8) {
340
gettimeofday(&start,NULL);
341
for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
342
Read(buf, handle, bar, 0, size);
344
gettimeofday(&end,NULL);
346
time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
347
printf("%8i bytes - read: %8.2lf MB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024 * 1024));
351
gettimeofday(&start,NULL);
352
for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
353
Write(buf, handle, bar, 0, size);
355
gettimeofday(&end,NULL);
357
time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
358
printf(", write: %8.2lf MB/s", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024 * 1024));
360
gettimeofday(&start,NULL);
361
for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
362
Write(buf, handle, bar, 0, size);
363
Read(check, handle, bar, 0, size);
364
memcmp(buf, check, size);
366
gettimeofday(&end,NULL);
368
time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
369
printf(", write-verify: %8.2lf MB/s\n", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024 * 1024));
378
int ReadData(int handle, int bar, unsigned long addr, int n, int access) {
381
int size = n * abs(access);
382
int block_width, blocks_per_line;
383
int numbers_per_block, numbers_per_line;
385
numbers_per_block = BLOCK_SIZE / access;
387
block_width = numbers_per_block * ((access * 2) + SEPARATOR_WIDTH);
388
blocks_per_line = (LINE_WIDTH - 10) / (block_width + BLOCK_SEPARATOR_WIDTH);
389
if ((blocks_per_line > 1)&&(blocks_per_line % 2)) --blocks_per_line;
390
numbers_per_line = blocks_per_line * numbers_per_block;
392
// buf = alloca(size);
393
posix_memalign( (void**)&buf, 256, size );
395
if (!buf) Error("Allocation of %i bytes of memory have failed", size);
397
Read(buf, handle, bar, addr, size);
399
for (i = 0; i < n; i++) {
400
if ((i)&&(i%numbers_per_line == 0)) printf("\n");
401
else if ((i)&&(i%numbers_per_block == 0)) printf("%*s", BLOCK_SEPARATOR_WIDTH, "");
403
if (i%numbers_per_line == 0) printf("%8lx: ", addr + i * abs(access));
406
case 1: printf("% *hhx", access * 2 + SEPARATOR_WIDTH, ((uint8_t*)buf)[i]); break;
407
case 2: printf("% *hx", access * 2 + SEPARATOR_WIDTH, ((uint16_t*)buf)[i]); break;
408
case 4: printf("% *x", access * 2 + SEPARATOR_WIDTH, ((uint32_t*)buf)[i]); break;
409
case 8: printf("% *lx", access * 2 + SEPARATOR_WIDTH, ((uint64_t*)buf)[i]); break;
418
int WriteData(int handle, int bar, unsigned long addr, int n, int access, char ** data) {
421
int size = n * abs(access);
423
posix_memalign( (void**)&buf, 256, size );
424
posix_memalign( (void**)&check, 256, size );
425
if ((!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", size);
427
for (i = 0; i < n; i++) {
429
case 1: res = sscanf(data[i], "%hhx", ((uint8_t*)buf)+i); break;
430
case 2: res = sscanf(data[i], "%hx", ((uint16_t*)buf)+i); break;
431
case 4: res = sscanf(data[i], "%x", ((uint32_t*)buf)+i); break;
432
case 8: res = sscanf(data[i], "%lx", ((uint64_t*)buf)+i); break;
435
if (res != 1) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]);
438
Write(buf, handle, bar, addr, size);
439
// ReadData(handle, bar, addr, n, access);
440
Read(check, handle, bar, addr, size);
442
if (memcmp(buf, check, size)) Error("Write failed, the data written and read differ");
450
int main(int argc, char **argv) {
453
MODE mode = MODE_INVALID;
454
const char *fpga_device = DEFAULT_FPGA_DEVICE;
456
const char *addr = NULL;
457
unsigned long start = -1;
464
while ((c = getopt_long(argc, argv, "hilpr::w::d:b:a:s:", long_options, NULL)) != (unsigned char)-1) {
468
Usage(argc, argv, NULL);
471
if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
476
if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
481
if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
483
mode = MODE_BENCHMARK;
486
if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
489
if (optarg) addr = optarg;
490
else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
493
if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
496
if (optarg) addr = optarg;
497
else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
500
fpga_device = optarg;
503
if ((sscanf(optarg,"%u", &bar) != 1)||(bar < 0)||(bar >= MAX_BANKS)) Usage(argc, argv, "Invalid data bank (%s) is specified", optarg);
506
if (sscanf(optarg, "%i", &access) != 1) access = 0;
508
case 8: access = 1; break;
509
case 16: access = 2; break;
510
case 32: access = 4; break;
511
case 64: access = 8; break;
512
default: Usage(argc, argv, "Invalid data width (%s) is specified", optarg);
516
if (sscanf(optarg, "%u", &size) != 1)
517
Usage(argc, argv, "Invalid size is specified (%s)", optarg);
520
Usage(argc, argv, "Unknown option (%s)", argv[optind]);
524
if (mode == MODE_INVALID) {
525
if (argc > 1) Usage(argc, argv, "Operation is not specified");
526
else Usage(argc, argv, NULL);
530
if (sscanf(addr, "%lx", &start) != 1) Usage(argc, argv, "Invalid address (%s) is specified", addr);
535
if ((argc - optind) != size) Usage(argc, argv, "The %i data values is specified, but %i required", argc - optind, size);
537
if (!addr) Usage(argc, argv, "The address is not specified");
540
if (argc > optind) Usage(argc, argv, "Invalid non-option parameters are supplied");
543
handle = open(fpga_device, O_RDWR);
544
if (handle < 0) Error("Failed to open FPGA device: %s", fpga_device);
554
Benchmark(handle, bar);
558
ReadData(handle, bar, start, size, access);
560
Error("Address to read is not specified");
564
WriteData(handle, bar, start, size, access, argv + optind);