/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 cli.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-10-26 05:12:31 UTC
  • Revision ID: csa@dside.dyndns.org-20111026051231-5ntkozz31hvjzvb5
Improvements of DMA engine

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
} ACCESS_MODE;
75
75
 
76
76
typedef enum {
 
77
    FLAG_MULTIPACKET = 1,
 
78
    FLAG_WAIT = 2
 
79
} FLAGS;
 
80
 
 
81
typedef enum {
77
82
    OPT_DEVICE = 'd',
78
83
    OPT_MODEL = 'm',
79
84
    OPT_BAR = 'b',
101
106
    OPT_LIST_KMEM,
102
107
    OPT_FREE_KMEM,
103
108
    OPT_READ_KMEM,
104
 
    OPT_FORCE
 
109
    OPT_FORCE,
 
110
    OPT_WAIT,
 
111
    OPT_MULTIPACKET
105
112
} OPTIONS;
106
113
 
107
114
static struct option long_options[] = {
132
139
    {"free-kernel-memory",      required_argument, 0, OPT_FREE_KMEM },
133
140
    {"quiete",                  no_argument, 0, OPT_QUIETE },
134
141
    {"force",                   no_argument, 0, OPT_FORCE },
 
142
    {"multipacket",             no_argument, 0, OPT_MULTIPACKET },
 
143
    {"wait",                    no_argument, 0, OPT_WAIT },
135
144
    {"help",                    no_argument, 0, OPT_HELP },
136
145
    { 0, 0, 0, 0 }
137
146
};
194
203
"   -o <file>                   - Append output to file (default: stdout)\n"
195
204
"   -t <timeout>                - Timeout in microseconds\n"
196
205
"\n"
 
206
"  DMA Options:\n"
 
207
"   --multipacket               - Read multiple packets\n"
 
208
"   --wait                      - Wait until data arrives\n"
 
209
"\n"
197
210
"  Information:\n"
198
211
"   -q                          - Quiete mode (suppress warnings)\n"
199
212
"\n"
393
406
        }
394
407
        
395
408
        for (size = min_size; size <= max_size; size *= 4) {
396
 
            mbs_in = pcilib_benchmark_dma(handle, dma, addr, size, BENCHMARK_ITERATIONS, PCILIB_DMA_FROM_DEVICE);
397
 
            mbs_out = pcilib_benchmark_dma(handle, dma, addr, size, BENCHMARK_ITERATIONS, PCILIB_DMA_TO_DEVICE);
 
409
            mbs_in = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_FROM_DEVICE);
 
410
            mbs_out = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_TO_DEVICE);
398
411
            mbs = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_BIDIRECTIONAL);
399
412
            err = pcilib_wait_irq(handle, 0, 0, &irqs);
400
413
            if (err) irqs = 0;
571
584
 
572
585
#define pci2host16(endianess, value) endianess?
573
586
 
574
 
 
575
 
int ReadData(pcilib_t *handle, ACCESS_MODE mode, pcilib_dma_engine_addr_t dma, pcilib_bar_t bar, uintptr_t addr, size_t n, access_t access, int endianess, FILE *o) {
 
587
/*
 
588
typedef struct {
 
589
    size_t size;
 
590
    void *data;
 
591
    size_t pos;
 
592
 
 
593
    int multi_mode;
 
594
} DMACallbackContext;
 
595
 
 
596
static int DMACallback(void *arg, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
 
597
    DMACallbackContext *ctx = (DMACallbackContext*)arg;
 
598
    
 
599
    if ((ctx->pos + bufsize > ctx->size)||(!ctx->data)) {
 
600
        ctx->size *= 2;
 
601
        ctx->data = realloc(ctx->data, ctx->size);
 
602
        if (!ctx->data) {
 
603
            Error("Allocation of %i bytes of memory have failed", ctx->size);
 
604
            return 0;
 
605
        }
 
606
    }
 
607
    
 
608
    memcpy(ctx->data + ctx->pos, buf, bufsize);
 
609
    ctx->pos += bufsize;
 
610
 
 
611
    if (flags & PCILIB_DMA_FLAG_EOP) return 0;
 
612
    return 1;
 
613
}
 
614
*/
 
615
 
 
616
 
 
617
int ReadData(pcilib_t *handle, ACCESS_MODE mode, FLAGS flags, pcilib_dma_engine_addr_t dma, pcilib_bar_t bar, uintptr_t addr, size_t n, access_t access, int endianess, size_t timeout, FILE *o) {
576
618
    void *buf;
577
619
    int i, err;
578
 
    size_t ret;
 
620
    size_t ret, bytes;
579
621
    int size = n * abs(access);
580
622
    int block_width, blocks_per_line;
581
623
    int numbers_per_block, numbers_per_line; 
582
624
    pcilib_dma_engine_t dmaid;
 
625
    pcilib_dma_flags_t dma_flags = 0;
583
626
    
584
627
    numbers_per_block = BLOCK_SIZE / access;
585
628
 
588
631
    if ((blocks_per_line > 1)&&(blocks_per_line % 2)) --blocks_per_line;
589
632
    numbers_per_line = blocks_per_line * numbers_per_block;
590
633
 
591
 
//    buf = alloca(size);
592
 
    err = posix_memalign( (void**)&buf, 256, size );
593
 
    if ((err)||(!buf)) Error("Allocation of %i bytes of memory have failed", size);
 
634
    if (size) {
 
635
        buf = malloc(size);
 
636
        if (!buf) Error("Allocation of %i bytes of memory have failed", size);
 
637
    } else {
 
638
        buf = NULL;
 
639
    }
594
640
    
595
641
    switch (mode) {
596
642
      case ACCESS_DMA:
 
643
        if (timeout == (size_t)-1) timeout = PCILIB_DMA_TIMEOUT;
 
644
    
597
645
        dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_FROM_DEVICE, dma);
598
646
        if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (%lu) is specified", dma);
599
 
        err = pcilib_read_dma(handle, dmaid, addr, size, buf, &ret);
600
 
        if ((err)||(ret <= 0)) Error("No data is returned by DMA engine");
601
 
        size = ret;
602
 
        n = ret / abs(access);
 
647
        
 
648
        if (flags&FLAG_MULTIPACKET) dma_flags |= PCILIB_DMA_FLAG_MULTIPACKET;
 
649
        if (flags&FLAG_WAIT) dma_flags |= PCILIB_DMA_FLAG_WAIT;
 
650
        
 
651
        if (size) {
 
652
            err = pcilib_read_dma_custom(handle, dmaid, addr, size, dma_flags, timeout, buf, &bytes);
 
653
            if (err) Error("Error (%i) is reported by DMA engine", err);
 
654
        } else {
 
655
            dma_flags |= PCILIB_DMA_FLAG_IGNORE_ERRORS;
 
656
            
 
657
            size = 2048; bytes = 0;
 
658
            do {
 
659
                size *= 2;
 
660
                buf = realloc(buf, size);
 
661
                err = pcilib_read_dma_custom(handle, dmaid, addr, size - bytes, dma_flags, timeout, buf + bytes, &ret);
 
662
                bytes += ret;
 
663
                
 
664
                if ((!err)&&(flags&FLAG_MULTIPACKET)) {
 
665
                    err = PCILIB_ERROR_TOOBIG;
 
666
                    if ((flags&FLAG_WAIT)==0) timeout = 0;
 
667
                }
 
668
            } while (err == PCILIB_ERROR_TOOBIG);
 
669
        }
 
670
        if (bytes <= 0) Error("No data is returned by DMA engine");
 
671
        size = bytes;
 
672
        n = bytes / abs(access);
603
673
        addr = 0;
604
674
      break;
605
675
      case ACCESS_FIFO:
639
709
 
640
710
    
641
711
    free(buf);
 
712
    return 0;
642
713
}
643
714
 
644
715
 
703
774
        }
704
775
        printf("\n");
705
776
    }
 
777
    
 
778
    return 0;
706
779
}
707
780
 
708
781
#define WRITE_REGVAL(buf, n, access, o) {\
818
891
    if ((read_back)&&(memcmp(buf, check, size))) {
819
892
        printf("Write failed: the data written and read differ, the foolowing is read back:\n");
820
893
        if (endianess) pcilib_swap(check, check, abs(access), n);
821
 
        ReadData(handle, mode, dma, bar, addr, n, access, endianess, NULL);
 
894
        ReadData(handle, mode, 0, dma, bar, addr, n, access, endianess, (size_t)-1, NULL);
822
895
        exit(-1);
823
896
    }
824
897
 
825
898
    free(check);
826
899
    free(buf);
 
900
    
 
901
    return 0;
827
902
}
828
903
 
829
904
int WriteRegisterRange(pcilib_t *handle, pcilib_model_description_t *model_info, const char *bank, uintptr_t addr, size_t n, char ** data) {
856
931
 
857
932
    free(check);
858
933
    free(buf);
 
934
    
 
935
    return 0;
859
936
 
860
937
}
861
938
 
1412
1489
    pcilib_model_t model = PCILIB_MODEL_DETECT;
1413
1490
    pcilib_model_description_t *model_info;
1414
1491
    MODE mode = MODE_INVALID;
 
1492
    FLAGS flags = 0;
1415
1493
    const char *type = NULL;
1416
1494
    ACCESS_MODE amode = ACCESS_BAR;
1417
1495
    const char *fpga_device = DEFAULT_FPGA_DEVICE;
1442
1520
    pcilib_t *handle;
1443
1521
    
1444
1522
    int size_set = 0;
1445
 
    
 
1523
    int timeout_set = 0;
1446
1524
    
1447
1525
    while ((c = getopt_long(argc, argv, "hqilr::w::g::d:m:t:b:a:s:e:o:", long_options, NULL)) != (unsigned char)-1) {
1448
1526
        extern int optind;
1645
1723
            case OPT_TIMEOUT:
1646
1724
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &timeout) != 1))
1647
1725
                    Usage(argc, argv, "Invalid timeout is specified (%s)", optarg);
 
1726
                timeout_set = 1;
1648
1727
            break;
1649
1728
            case OPT_OUTPUT:
1650
1729
                output = optarg;
1652
1731
            case OPT_ITERATIONS:
1653
1732
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &iterations) != 1))
1654
1733
                    Usage(argc, argv, "Invalid number of iterations is specified (%s)", optarg);
1655
 
                size_set = 1;
1656
1734
            break;
1657
1735
            case OPT_QUIETE:
1658
1736
                quiete = 1;
1660
1738
            case OPT_FORCE:
1661
1739
                force = 1;
1662
1740
            break;
 
1741
            case OPT_MULTIPACKET:
 
1742
                flags |= FLAG_MULTIPACKET;
 
1743
            break;
 
1744
            case OPT_WAIT:
 
1745
                flags |= FLAG_WAIT;
 
1746
            break;
1663
1747
            default:
1664
1748
                Usage(argc, argv, "Unknown option (%s) with argument (%s)", optarg?argv[optind-2]:argv[optind-1], optarg?optarg:"(null)");
1665
1749
        }
1823
1907
        Benchmark(handle, amode, dma, bar, start, size_set?size:0, access, iterations);
1824
1908
     break;
1825
1909
     case MODE_READ:
1826
 
        if ((addr)||(amode == ACCESS_DMA)) {
1827
 
            ReadData(handle, amode, dma, bar, start, size, access, endianess, ofile);
 
1910
        if (amode == ACCESS_DMA) {
 
1911
            ReadData(handle, amode, flags, dma, bar, start, size_set?size:0, access, endianess, timeout_set?timeout:(size_t)-1, ofile);
 
1912
        } else if (addr) {
 
1913
            ReadData(handle, amode, flags, dma, bar, start, size, access, endianess, (size_t)-1, ofile);
1828
1914
        } else {
1829
1915
            Error("Address to read is not specified");
1830
1916
        }