/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to apps/lorenzo_ipedma_test.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-05-05 13:27:22 UTC
  • Revision ID: csa@suren.me-20150505132722-rz47ppxkx39x2koj
Split bar manipulation and fifo operations in stand-alone source and publish kmem and bar headers

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#include <sched.h>
14
14
#include <errno.h>
15
15
 
16
 
#include "pcilib.h"
17
 
#include "irq.h"
18
 
#include "kmem.h"
19
 
#include "pci.h"
 
16
#include <pcilib.h>
 
17
#include <pcilib/irq.h>
 
18
#include <pcilib/kmem.h>
20
19
 
21
20
//#include <sys/ipc.h>
22
21
//#include <sys/shm.h>
35
34
                                    // if set to 0, the update only happens when INT is received
36
35
 
37
36
#define HUGE_PAGE       1           // number of pages per huge page
38
 
#define TLP_SIZE        64          // TLP SIZE = 64 for 256B payload, 32 for 128B payload
 
37
#define TLP_SIZE        32          // TLP SIZE = 64 for 256B payload, 32 for 128B payload
39
38
#define PAGE_SIZE       4096        // other values are not supported in the kernel
40
39
 
41
 
#define USE_64                    // Lorenzo: use 64bit addressing
 
40
#define USE_64                      // Lorenzo: use 64bit addressing
42
41
 
43
42
//#define DUAL_CORE                 // Lorenzo: DUAL Core
44
43
 
45
44
//#define SHARED_MEMORY               // Lorenzo: Test for fast GUI
46
45
 
47
 
#define CHECK_READY                 // Lorenzo: Check if PCI-Express is ready by reading 0x0
48
 
#define CHECK_RESULTS               // Lorenzo: Check if data received is ok (only for counter!)
49
 
//#define PRINT_RESULTS               // Lorenzo: Save the received data in "data.out"
 
46
#define CHECK_READY                   // Lorenzo: Check if PCI-Express is ready by reading 0x0
 
47
#define MEM_COPY                      // Lorenzo: CPY data
 
48
//#define CHECK_RESULTS                 // Lorenzo: Check if data received is ok (only for counter!)   
 
49
//#define CHECK_RESULTS_LOG           // Lorenzo: Check if data received is ok (only for counter!)   
 
50
#define PRINT_RESULTS                 // Lorenzo: Save the received data in "data.out"
50
51
//#define EXIT_ON_EMPTY               // Lorenzo: Exit if an "empty_detected" signal is received
51
52
 
52
 
//#define HEB                       // Lorenzo: Testing HEB
53
 
//#define SWITCH_GENERATOR          // Lorenzo: Testing HEB -> Turn data gen on/off
54
 
 
55
 
//#define TEST_DDR                    // Lorenzo: Testing DDR
56
 
 
57
53
#define TIMEOUT         1000000
58
54
 
59
55
 
69
65
 
70
66
#define FPGA_CLOCK 250 // Lorenzo: in MHz !
71
67
 
72
 
 
73
 
 
74
68
//#define WR(addr, value) { val = value; pcilib_write(pci, BAR, addr, sizeof(val), &val); }
75
69
//#define RD(addr, value) { pcilib_read(pci, BAR, addr, sizeof(val), &val); value = val; }
76
70
#define WR(addr, value) { *(uint32_t*)(bar + addr + offset) = value; }
140
134
 
141
135
 
142
136
    int err;
143
 
    int i, j, k;
144
 
    int mem_diff;
 
137
    long i, j;
145
138
    pcilib_t *pci;
146
139
    pcilib_kmem_handle_t *kdesc;
147
140
    pcilib_kmem_handle_t *kbuf;
148
141
    struct timeval start, end;
149
142
    size_t run_time;
150
 
    size_t size_mb;
 
143
    long long int size_mb;
151
144
    void* volatile bar;
152
145
    uintptr_t bus_addr[BUFFERS];
153
146
    uintptr_t kdesc_bus;
158
151
#ifdef SWITCH_GENERATOR
159
152
    int switch_generator = 0;
160
153
#endif /* SWITCH_GENERATOR */
161
 
 
 
154
#if defined(CHECK_RESULTS)||defined(CHECK_RESULTS_LOG)
 
155
    long k;
 
156
    int mem_diff;
 
157
#endif /* CHECK_RESULTS */
 
158
 
 
159
   
162
160
    float performance, perf_counter; 
163
161
    pcilib_bar_t bar_tmp = BAR; 
164
162
    uintptr_t offset = 0;
165
163
 
166
164
    unsigned int temp;
167
 
    unsigned iterations_completed, buffers_filled;
 
165
    int iterations_completed, buffers_filled;
168
166
 
169
167
 
170
168
//    int shmid;
172
170
 
173
171
    printf("\n\n**** **** **** KIT-DMA TEST **** **** ****\n\n");
174
172
 
 
173
    //size = ITERATIONS * BUFFERS * HUGE_PAGE * PAGE_SIZE;
175
174
    size_mb = ITERATIONS * BUFFERS * HUGE_PAGE * 4 / 1024;
176
175
    printf("Total size of memory buffer: \t %.3lf GBytes\n", (float)size_mb/1024 );
177
 
    printf("Using %u Buffers with %u iterations\n\n", BUFFERS, ITERATIONS );
 
176
    printf("Using %d Buffers with %d iterations\n\n", BUFFERS, ITERATIONS );
178
177
 
179
178
#ifdef ADD_DELAYS
180
179
    long rpt = 0, rpt2 = 0;
187
186
    pcilib_kmem_flags_t free_flags = PCILIB_KMEM_FLAG_HARDWARE/*|PCILIB_KMEM_FLAG_EXCLUSIVE|PCILIB_KMEM_FLAG_REUSE*/;
188
187
    pcilib_kmem_flags_t clean_flags = PCILIB_KMEM_FLAG_HARDWARE|PCILIB_KMEM_FLAG_PERSISTENT|PCILIB_KMEM_FLAG_EXCLUSIVE;
189
188
 
190
 
    pci = pcilib_open(DEVICE, PCILIB_MODEL_DETECT);
 
189
    pci = pcilib_open(DEVICE, "pci");
191
190
    if (!pci) fail("pcilib_open");
192
191
 
193
192
    bar = pcilib_map_bar(pci, BAR);
239
238
        // Pointers for Virtualized Mem
240
239
        for (j = 0; j < BUFFERS; j++) {
241
240
            ptr[j] = (volatile uint32_t*)pcilib_kmem_get_block_ua(pci, kbuf, j);
242
 
            memset((void*)ptr[j], 0, HUGE_PAGE * PAGE_SIZE);
 
241
            memset((void*)(ptr[j]), 0, HUGE_PAGE * PAGE_SIZE);
243
242
        }
244
243
 
245
244
        err = 0;
271
270
    FILE * Output;
272
271
    FILE * error_log;
273
272
 
274
 
#ifdef CHECK_RESULTS
 
273
#ifdef MEM_COPY
 
274
 
275
275
    uint32_t *temp_data[ITERATIONS][BUFFERS];
276
276
 
277
277
    for (j=0; j < ITERATIONS; j++) {
314
314
    error_log = fopen ("error_log.txt", "w");
315
315
    fclose(error_log);
316
316
   
317
 
   // *************************************
318
 
    Output = fopen("data.txt", "w");
319
 
    fclose(Output);
320
 
 
321
317
    // ******************************************************************
322
318
    // ****      PCIe TEST                                          ***** 
323
319
    // ******************************************************************
345
341
    // ****      DMA CONFIGURATION                                  ***** 
346
342
    // ******************************************************************
347
343
 
348
 
    
349
 
    printf("* DMA: Start Data Generator...\n");
350
 
    WR(0x04, 0x10) // Start data generator
351
 
 
352
 
    printf("* DMA: Send Data Fill Pattern 55aa55aa\n");
353
 
    WR(0x14, 0xbeef);
354
 
 
355
344
    printf("* DMA: Send Data Amount\n");
356
345
#ifdef DUAL_CORE
357
346
    WR(0x10, (HUGE_PAGE * (PAGE_SIZE / (4 * TLP_SIZE)))/2);
360
349
#endif   
361
350
 
362
351
    printf("* DMA: Running mode: ");
 
352
 
363
353
#ifdef USE_64   
364
354
    if (TLP_SIZE == 64) 
365
355
    {
383
373
        printf ("32bit - 128B Payload\n");
384
374
    }
385
375
#endif
386
 
    
 
376
 
387
377
    printf("* DMA: Reset Desc Memory...\n");
388
378
    WR(0x5C, 0x00); // RST Desc Memory
389
379
 
390
 
    //printf("Writing SW Read Descriptor\n");
 
380
    printf("Writing SW Read Descriptor\n");
391
381
    WR(0x58, BUFFERS-1);
392
382
    //WR(0x58, 0x01);
393
383
 
394
 
    //printf("Writing the Descriptor Threshold\n");
 
384
    printf("Writing the Descriptor Threshold\n");
395
385
    WR(0x60, DESC_THRESHOLD);
396
386
 
397
 
    //printf("Writing HW write Descriptor Address: %lx\n", kdesc_bus);
 
387
    printf("Writing HW write Descriptor Address: %lx\n", kdesc_bus);
398
388
    WR(0x54, kdesc_bus);
399
389
    usleep(100000);
400
390
 
403
393
        bus_addr[j] = pcilib_kmem_get_block_ba(pci, kbuf, j);
404
394
        // LEAVE THIS DELAY???!?!?!?!
405
395
        usleep(1000);
406
 
        printf("Writing descriptor num. %d: \t %08lx \r", j, bus_addr[j]);
 
396
        //printf("Writing descriptor num. %ld: \t %08lx \n", j, bus_addr[j]);
407
397
        WR(0x50, bus_addr[j]);
408
398
    }
409
399
 
410
400
    // ******************************************************************
411
 
    // ****      HEB CONFIGURATION                                  ***** 
412
 
    // ******************************************************************
413
 
#ifdef HEB
414
 
 
415
 
 
416
 
    printf("* DDR REGISTERS: AXI_BUF_SIZE \n");
417
 
    WR(0x9130, 0x1000);
418
 
 
419
 
    usleep(100000);
420
 
 
421
 
    printf("* HEB: Control \n");
422
 
    WR(0x9040, 0x00000001);
423
 
 
424
 
    usleep(100000);
425
 
 
426
 
    printf("* HEB: Control \n");
427
 
    WR(0x9040, 0x00000004);
428
 
 
429
 
    usleep(100000);
430
 
 
431
 
    printf("* HEB: Control \n");
432
 
    WR(0x9040, 0x00000000);
433
 
 
434
 
    usleep(100000);
435
 
 
436
 
    printf("* HEB: Writing Total Orbit Num\n");
437
 
    WR(0x9020, 0x2000);
438
 
 
439
 
    printf("* HEB: Orbit Skip Num h9028\n");
440
 
    WR(0x9028, 0x4);
441
 
 
442
 
    //printf("* HEB: LVDS_DELAY h9080\n");
443
 
    //WR(0x9080, 0x10101010);
444
 
 
445
 
    //printf("* HEB: Delay ADCs \n");
446
 
    //WR(0x9088, 0x001);
447
 
    //WR(0x9090, 0x001);
448
 
    //WR(0x9094, 0x001);
449
 
    //WR(0x9098, 0x001);
450
 
 
451
 
    //printf("* HEB: Delay TH \n");
452
 
    //WR(0x90a0, 0x005);
453
 
 
454
 
    //printf("* HEB: Delay_FPGA_reg \n");
455
 
    //WR(0x90a8, 0x006);
456
 
 
457
 
    //printf("* HEB: Control \n");
458
 
    //WR(0x9040, 0x40000000);
459
 
 
460
 
    //usleep(1000000);
461
 
 
462
 
    printf("* HEB: Control \n");
463
 
    WR(0x9040, 0x40000bf0);
464
 
 
465
 
    usleep(100000);
466
 
 
467
 
    printf("* HEB: Control \n");
468
 
    WR(0x9040, 0x400003f0);
469
 
 
470
 
    usleep(100000);
471
 
 
472
 
    printf("* HEB: Control \n");
473
 
    WR(0x9040, 0x480007F0);
474
 
 
475
 
    usleep(100000);
476
 
 
477
 
    printf("* HEB: Control \n");
478
 
    WR(0x9040, 0x48000FF0);
479
 
 
480
 
 
481
 
#endif
482
 
 
483
 
    // ******************************************************************
484
 
    // ****      TEST DDR conf                                      ***** 
485
 
    // ******************************************************************
486
 
#ifdef TEST_DDR
487
 
 
488
 
 
489
 
    printf("* DDR: AXI_BUF_SIZE_ADDR: 4k\n");
490
 
    WR(0x9010, 0x04000);
491
 
 
492
 
    printf("* DDR: Control \n");
493
 
    WR(0x9000, 0x000000F);
494
 
 
495
 
    usleep(100000);
496
 
    WR(0x9000, 0x00000008);
497
 
    usleep(100000);
498
 
    WR(0x9000, 0x08000008);
499
 
 
500
 
    usleep(50000);
501
 
 
502
 
    printf("* DDR: Control \n");
503
 
    WR(0x9000, 0x08000208);
504
 
 
505
 
 
506
 
#endif
507
 
 
508
 
    // ******************************************************************
509
401
    // ****     START DMA                                           *****
510
402
    // ******************************************************************
511
403
 
513
405
    //getchar();
514
406
 
515
407
    printf("* DMA: Start \n");
516
 
    WR(0x04, 0x1f);
 
408
    WR(0x04, 0x1);
517
409
    gettimeofday(&start, NULL);
518
410
 
519
411
    // ******************************************************************
528
420
 
529
421
    while (i < ITERATIONS) {
530
422
        j = 0;
531
 
        // printf("\ndesc0: %lx", htonl(desc[0])); 
532
 
        // printf("\ndesc1: %lx", htonl(desc[1])); 
533
 
        // printf("\ndesc2: %lx", htonl(desc[2])); 
534
 
        // printf("\ndesc3: %lx", htonl(desc[3])); 
535
 
        // printf("\ndesc4: %lx", htonl(desc[4]));
 
423
        //printf("\ndesc0: %lx", desc[0]); 
 
424
        //printf("\ndesc1: %lx", desc[1]); 
 
425
        //printf("\ndesc2: %lx", desc[2]); 
 
426
        //printf("\ndesc3: %lx", desc[3]); 
 
427
        //printf("\ndesc4: %lx", desc[4]);
536
428
        // printf("\ndesc5: %lx", htonl(desc[5]));
537
429
        //printf("Iteration: %li of %li \r", i+1, ITERATIONS); 
 
430
        //getchar();
538
431
        //loadBar(i+1, ITERATIONS, ITERATIONS, 30);
539
432
        // printf("\nhwptr: %zu", hwptr);  
540
433
        // printf("\ncurptr: %zu", curptr); 
551
444
 
552
445
        do {    
553
446
            pcilib_kmem_sync_block(pci, kbuf, PCILIB_KMEM_SYNC_FROMDEVICE, curbuf);
554
 
#ifdef CHECK_RESULTS   
 
447
#ifdef MEM_COPY   
555
448
            memcpy(temp_data[i][curbuf], (void*)ptr[curbuf], 4096);
556
449
#endif
 
450
#ifdef CHECK_RESULTS
 
451
for (k = 0; k < 1024 ; k++) 
 
452
            {
 
453
                mem_diff = (ptr[curbuf][k] - ptr[curbuf][k]);
 
454
                //if ((mem_diff == 1) || (mem_diff == (-7)) || (k == 1023) ) 
 
455
                if (mem_diff == -1)
 
456
                    {;}
 
457
                else {
 
458
                    //fprintf(error_log, "Error in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j][k], temp_data[i][j][k+1], mem_diff);
 
459
                    err++;
 
460
                }
 
461
            }
 
462
#endif
557
463
#ifdef SHARED_MEMORY
558
 
            memcpy(shared_memory, (void*)ptr[curbuf], 4096); 
 
464
            memcpy(shared_memory, ptr[curbuf], 4096); 
559
465
#endif            
560
466
            //printf("\ncurbuf: %08x", curbuf); 
561
467
            //printf("\nbus_addr[curbuf]\n: %08x",bus_addr[curbuf]);
600
506
        //printf("WR %d\n", curbuf + 1); 
601
507
        //printf("%u (%lu)\n", curbuf, j);
602
508
        curptr = hwptr;
603
 
 
604
509
    }
605
 
    
606
510
 
607
511
 
608
512
    // ******************************************************************
611
515
 
612
516
    gettimeofday(&end, NULL);
613
517
    WR(0x04, 0x00);
614
 
    WR(0x01, 0x00);
 
518
    usleep(100);
615
519
    RD(0x28, perf_counter);
 
520
    usleep(100);
 
521
    WR(0x00, 0x01);
 
522
 
616
523
 
617
524
 
618
525
 
619
526
    iterations_completed   = i;
620
527
    buffers_filled      = curbuf;
621
 
    if (empty) printf("* DMA: Empty FIFO! Last iteration: %u of %u\n", i+1, ITERATIONS);
 
528
    if (empty) printf("* DMA: Empty FIFO! Last iteration: %li of %i\n", i+1, ITERATIONS);
622
529
    printf ("* DMA: Stop\n\n");
623
530
 
624
 
#ifdef CHECK_RESULTS
 
531
#ifdef MEM_COPY
625
532
    printf ("First value:\t %08x\n", temp_data[0][0][0]);
626
533
    printf ("Last value:\t %08x\n\n", temp_data[ITERATIONS-1][BUFFERS-1][(PAGE_SIZE/4)-4]);
627
534
#endif
634
541
 
635
542
 
636
543
    run_time = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
 
544
    //size = (long long int) (( BUFFERS * (iterations_completed)  + buffers_filled) * HUGE_PAGE * PAGE_SIZE);
637
545
    size_mb = (long long int) (( BUFFERS * (iterations_completed)  + buffers_filled) * HUGE_PAGE * 4 / 1024);
638
 
    printf("Performance: transfered %zu Mbytes in %zu us using %u buffers\n", (size_mb), run_time, BUFFERS);
 
546
    printf("Performance: transfered %llu Mbytes in %zu us using %d buffers\n", (size_mb), run_time, BUFFERS);
639
547
    //printf("Buffers: \t %d \n", BUFFERS);
640
548
    //printf("Buf_Size: \t %d \n", PAGE_SIZE);
641
549
    //printf("Perf_counter: \t %f \n", perf_counter);
649
557
    // ******************************************************************
650
558
 
651
559
 
652
 
#ifdef PRINT_RESULTS
 
560
    #ifdef PRINT_RESULTS
653
561
    printf("Writing Data to HDD... \n");
654
562
    for (i=0; i < iterations_completed; i++) {
655
563
        for (j=0; j < BUFFERS; j++)
668
576
        fclose(Output);
669
577
    }   
670
578
    printf("Data saved in data.out. \n");
671
 
#endif
 
579
    #endif
672
580
 
673
 
#ifdef CHECK_RESULTS
 
581
   #ifdef CHECK_RESULTS_LOG
674
582
    err = 0;
675
583
    error_log = fopen ("error_log.txt", "a");
676
584
    printf("\nChecking data ...\n");
683
591
                if ((mem_diff == -1) || (k == 1023) ) 
684
592
                    {;}
685
593
                else {
686
 
                    fprintf(error_log, "Error in: \t IT %u \t BUF : %u \t OFFSET: %u \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j][k], temp_data[i][j][k+1], mem_diff);
 
594
                    fprintf(error_log, "Error in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j][k], temp_data[i][j][k+1], mem_diff);
687
595
                    err++;
688
596
                }
689
597
            }
693
601
                if (mem_diff == (1)) 
694
602
                    {;}
695
603
                else {
696
 
                    fprintf(error_log, "Error_2 in: \t IT %u \t BUF : %u \t OFFSET: %u \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j+1][0], temp_data[i][j][1023], mem_diff);
 
604
                    fprintf(error_log, "Error_2 in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j+1][0], temp_data[i][j][1023], mem_diff);
697
605
                    err++;
698
606
                }
699
607
            }
702
610
        loadBar(i+1, ITERATIONS, ITERATIONS, 30);
703
611
    }
704
612
    for (j = 0; j < buffers_filled; j++) {
705
 
        for (k = 0; k < 1024 ; k++) {
 
613
        for (k = 0; k < 1024 ; k++) 
 
614
        {
706
615
            mem_diff = ((uint32_t)temp_data[iterations_completed][j][k] - (uint32_t)temp_data[iterations_completed][j][k+1]);
707
 
            if ((mem_diff == -1) || (k == 1023) ) {
708
 
                ;
709
 
            } else {
710
 
                fprintf(error_log, "Error in: \t IT %u \t BUF : %u \t OFFSET: %u \t | %08x --> %08x - DIFF: %d \n", iterations_completed, j, k, temp_data[iterations_completed][j][k], temp_data[iterations_completed][j][k+1], mem_diff);
 
616
                if ((mem_diff == -1) || (k == 1023) ) 
 
617
                {;}
 
618
            else {
 
619
                fprintf(error_log, "Error in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", iterations_completed, j, k, temp_data[iterations_completed][j][k], temp_data[iterations_completed][j][k+1], mem_diff);
711
620
                err++;
712
621
            }
713
622
        }
714
 
        if (j < (buffers_filled-1)) {
 
623
        if (j != buffers_filled-1) {
715
624
        // Check first and Last
716
625
            mem_diff = (uint32_t)(temp_data[i][j+1][0] - temp_data[i][j][1023]);
717
 
            if (mem_diff == (1)) {
718
 
                ;
719
 
            } else {
720
 
                fprintf(error_log, "Error_2 in: \t IT %u \t BUF : %u \t OFFSET: %u \t | %08x --> %08x - DIFF: %d \n", iterations_completed, j, k, temp_data[iterations_completed][j+1][0], temp_data[iterations_completed][j][1023], mem_diff);
 
626
            if (mem_diff == (1)) 
 
627
                {;}
 
628
            else {
 
629
                fprintf(error_log, "Error_2 in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", iterations_completed, j, k, temp_data[iterations_completed][j+1][0], temp_data[iterations_completed][j][1023], mem_diff);
721
630
                err++;
722
631
            }
723
632
        }
725
634
    if (err != 0) printf("\rChecking data: \xE2\x9C\x98 %d errors found  \n See \"error_log.txt\" for details \n\n", err);
726
635
    else printf("\rChecking data: \xE2\x9C\x93 no errors found  \n\n");
727
636
    fclose(error_log);
728
 
#endif
729
 
 
 
637
    #endif
730
638
 
731
639
    // *********** Free Memory
732
 
#ifdef CHECK_RESULTS
 
640
#ifdef MEM_COPY
733
641
    for (i=0; i < ITERATIONS; i++) {
734
642
        for (j=0; j < BUFFERS; j++)
735
643
        {
736
644
            free(temp_data[i][j]);
737
645
        }
738
646
    }
739
 
#endif /* CHECK_RESULTS */
 
647
#endif
740
648
 
741
649
    pcilib_free_kernel_memory(pci, kbuf,  free_flags);
742
650
    pcilib_free_kernel_memory(pci, kdesc,  free_flags);