/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-12-08 02:47:23 UTC
  • Revision ID: csa@dside.dyndns.org-20111208024723-ym9uf3uoll6ym2a9
new event architecture, first trial

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#include <arpa/inet.h>
17
17
#include <sys/types.h>
18
18
#include <dirent.h>
 
19
#include <pthread.h>
19
20
 
20
21
#include <getopt.h>
21
22
 
 
23
#include "pcitool/sysinfo.h"
 
24
 
22
25
//#include "pci.h"
23
26
#include "tools.h"
24
27
#include "kernel.h"
46
49
typedef uint8_t access_t;
47
50
 
48
51
typedef enum {
 
52
    GRAB_MODE_GRAB = 1,
 
53
    GRAB_MODE_TRIGGER = 2
 
54
} GRAB_MODE;
 
55
 
 
56
typedef enum {
49
57
    MODE_INVALID,
50
58
    MODE_INFO,
51
59
    MODE_LIST,
78
86
    FLAG_WAIT = 2
79
87
} FLAGS;
80
88
 
 
89
 
 
90
typedef enum {
 
91
    FORMAT_RAW,
 
92
    FORMAT_HEADER,
 
93
    FORMAT_RINGFS
 
94
} FORMAT;
 
95
 
 
96
typedef enum {
 
97
    PARTITION_UNKNOWN,
 
98
    PARTITION_RAW,
 
99
    PARTITION_EXT4
 
100
} PARTITION;
 
101
 
81
102
typedef enum {
82
103
    OPT_DEVICE = 'd',
83
104
    OPT_MODEL = 'm',
96
117
    OPT_HELP = 'h',
97
118
    OPT_RESET = 128,
98
119
    OPT_BENCHMARK,
 
120
    OPT_TRIGGER,
 
121
    OPT_DATA_TYPE,
 
122
    OPT_EVENT,
 
123
    OPT_TRIGGER_RATE,
 
124
    OPT_TRIGGER_TIME,
 
125
    OPT_RUN_TIME,
 
126
    OPT_FORMAT,
 
127
    OPT_BUFFER,
99
128
    OPT_LIST_DMA,
100
129
    OPT_LIST_DMA_BUFFERS,
101
130
    OPT_READ_DMA_BUFFER,
128
157
    {"read",                    optional_argument, 0, OPT_READ },
129
158
    {"write",                   optional_argument, 0, OPT_WRITE },
130
159
    {"grab",                    optional_argument, 0, OPT_GRAB },
 
160
    {"trigger",                 optional_argument, 0, OPT_TRIGGER },
 
161
    {"data",                    required_argument, 0, OPT_DATA_TYPE },
 
162
    {"event",                   required_argument, 0, OPT_EVENT },
 
163
    {"run-time",                required_argument, 0, OPT_RUN_TIME },
 
164
    {"trigger-rate",            required_argument, 0, OPT_TRIGGER_RATE },
 
165
    {"trigger-time",            required_argument, 0, OPT_TRIGGER_TIME },
 
166
    {"format",                  required_argument, 0, OPT_FORMAT },
 
167
    {"buffer",                  optional_argument, 0, OPT_BUFFER },
131
168
    {"start-dma",               required_argument, 0, OPT_START_DMA },
132
169
    {"stop-dma",                optional_argument, 0, OPT_STOP_DMA },
133
170
    {"list-dma-engines",        no_argument, 0, OPT_LIST_DMA },
168
205
"   -l[l]                       - List (detailed) Data Banks & Registers\n"
169
206
"   -r <addr|reg|dmaX>          - Read Data/Register\n"
170
207
"   -w <addr|reg|dmaX>          - Write Data/Register\n"
171
 
"   -g [event]                  - Grab Event\n"
172
208
"   --benchmark <barX|dmaX>     - Performance Evaluation\n"
173
209
"   --reset                     - Reset board\n"
174
210
"   --help                      - Help message\n"
175
211
"\n"
 
212
"  Event Modes:\n"
 
213
"   --trigger [event]           - Trigger Events\n"
 
214
"   -g [event]                  - Grab Events\n"
 
215
"\n"
176
216
"  DMA Modes:\n"
177
217
"   --start-dma <num>[r|w]      - Start specified DMA engine\n"
178
218
"   --stop-dma [num[r|w]]       - Stop specified engine or DMA subsystem\n"
183
223
"\n"
184
224
"  Kernel Modes:\n"
185
225
"   --list-kernel-memory        - List kernel buffers\n"
186
 
"   --read-kernel-memory <blk>  - Read the specified block of the kernel memory,\n"
 
226
"   --read-kernel-memory <blk>  - Read the specified block of the kernel memory\n"
187
227
"                                 block is specified as: use:block_number\n"
188
228
"   --free-kernel-memory <use>  - Cleans lost kernel space buffers (DANGEROUS)\n"
189
229
"       dma                     - Remove all buffers allocated by DMA subsystem\n"
203
243
"   -o <file>                   - Append output to file (default: stdout)\n"
204
244
"   -t <timeout>                - Timeout in microseconds\n"
205
245
"\n"
 
246
"  Event Options:\n"
 
247
"   --event <evt>               - Specifies event for trigger and grab modes\n"
 
248
"   --data <type>               - Data type to request for the events\n"
 
249
"   --run-time <us>             - Grab/trigger events during the specified time\n"
 
250
"   --trigger-rate <tps>        - Generate tps triggers per second\n"
 
251
"   --trigger-time <us>         - Specifies delay between triggers in microseconds\n"
 
252
"   -s <num|unlimited>          - Number of events to grab and trigger\n"
 
253
"   --format [type]             - Specifies how event data should be stored\n"
 
254
"       raw                     - Just write all events sequentially\n"
 
255
"       add_header              - Prefix events with 256 bit header\n"
 
256
"       ringfs                  - Write to RingFS\n"
 
257
"   --buffer [size]             - Request data buffering, size in MB\n"
 
258
"\n"
206
259
"  DMA Options:\n"
207
260
"   --multipacket               - Read multiple packets\n"
208
261
"   --wait                      - Wait until data arrives\n"
237
290
}
238
291
 
239
292
void List(pcilib_t *handle, pcilib_model_description_t *model_info, const char *bank, int details) {
240
 
    int i;
 
293
    int i,j;
241
294
    pcilib_register_bank_description_t *banks;
242
295
    pcilib_register_description_t *registers;
243
296
    pcilib_event_description_t *events;
 
297
    pcilib_event_data_type_description_t *types;
244
298
 
245
299
    const pcilib_board_info_t *board_info = pcilib_get_board_info(handle);
246
300
    const pcilib_dma_info_t *dma_info = pcilib_get_dma_info(handle);
358
412
    }
359
413
 
360
414
    if (bank == (char*)-1) events = NULL;
361
 
    else events = model_info->events;
 
415
    else {
 
416
        events = model_info->events;
 
417
        types = model_info->data_types;
 
418
    }
362
419
 
363
420
    if (events) {
364
421
        printf("Events: \n");
367
424
            if ((events[i].description)&&(events[i].description[0])) {
368
425
                printf(": %s", events[i].description);
369
426
            }
 
427
            
 
428
            if (types) {
 
429
                for (j = 0; types[j].name; j++) {
 
430
                    if (types[j].evid & events[i].evid) {
 
431
                        printf("\n    %s", types[j].name);
 
432
                        if ((types[j].description)&&(types[j].description[0])) {
 
433
                            printf(": %s", types[j].description);
 
434
                        }
 
435
                    }
 
436
                }
 
437
            }
370
438
        }
371
439
        printf("\n");
372
440
    }
998
1066
    return 0;
999
1067
}
1000
1068
 
1001
 
int Grab(pcilib_t *handle, const char *event, FILE *o) {
1002
 
    int err;
 
1069
typedef struct {
 
1070
    pcilib_t *handle;
 
1071
    pcilib_event_t event;
 
1072
    pcilib_event_data_type_t data;
 
1073
    FILE *output;
 
1074
 
 
1075
    size_t run_time;
 
1076
    size_t trigger_time;    
1003
1077
    
1004
 
    void *data = NULL;
 
1078
    int run_flag;
 
1079
} GRABContext;
 
1080
 
 
1081
int GrabCallback(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user) {
 
1082
/*    int err;
 
1083
    void *data;
1005
1084
    size_t size, written;
1006
1085
    
1007
 
    // ignoring event for now
1008
 
        
1009
 
    err = pcilib_grab(handle, PCILIB_EVENTS_ALL, &size, &data, PCILIB_TIMEOUT_TRIGGER);
1010
 
    if (err) {
1011
 
        Error("Grabbing event is failed");
1012
 
    }
 
1086
    GRABContext *ctx = (GRABContext*)user;
 
1087
    pcilib_t *handle = ctx->handle;
 
1088
    FILE *o = ctx->output;
 
1089
    
 
1090
    data = pcilib_get_data(handle, ctx->event, ctx->data, &size);
 
1091
    if (!data) Error("Internal Error: No data is provided to event callback");
1013
1092
 
1014
1093
    if (o) printf("Writting %zu bytes into file...\n", size);
1015
1094
    else o = stdout;
1020
1099
        else Error("Write failed");
1021
1100
    }
1022
1101
    
 
1102
    pcilib_return_data(handle, ctx->event, data);
 
1103
*/
 
1104
 
 
1105
    printf("data callback: %lu\n", event_id);    
 
1106
}
 
1107
 
 
1108
int raw_data(pcilib_event_id_t event_id, pcilib_event_info_t *info, pcilib_event_flags_t flags, size_t size, void *data, void *user) {
 
1109
//    printf("%i\n", event_id);
 
1110
}
 
1111
 
 
1112
void *Trigger(void *user) {
 
1113
    GRABContext *ctx = (GRABContext*)user;
 
1114
    
 
1115
    pcilib_trigger(ctx->handle, PCILIB_EVENT0, 0, NULL);
 
1116
    usleep(3000);
 
1117
    pcilib_trigger(ctx->handle, PCILIB_EVENT0, 0, NULL);
 
1118
    
 
1119
    return NULL;
 
1120
}
 
1121
 
 
1122
int TriggerAndGrab(pcilib_t *handle, GRAB_MODE grab_mode, const char *event, const char *data_type, size_t num, size_t run_time, size_t trigger_time, PARTITION partition, FORMAT format, size_t buffer_size, FILE *ofile) {
 
1123
    int err;
 
1124
    GRABContext ctx;    
 
1125
    void *data = NULL;
 
1126
    size_t size, written;
 
1127
 
 
1128
    pthread_t trigger_thread;
 
1129
 
 
1130
    ctx.handle = handle;
 
1131
    ctx.output = ofile;
 
1132
    ctx.event = PCILIB_EVENT0;
 
1133
    ctx.run_time = run_time;
 
1134
    ctx.trigger_time = trigger_time;
 
1135
    
 
1136
    ctx.run_flag = 1;
 
1137
    
 
1138
    // ignoring event for now
 
1139
    pcilib_configure_autostop(handle, 2, 1000000);//PCILIB_TIMEOUT_TRIGGER);
 
1140
    pcilib_configure_rawdata_callback(handle, &raw_data, NULL);
 
1141
 
 
1142
    err = pcilib_start(handle, PCILIB_EVENTS_ALL, PCILIB_EVENT_FLAGS_DEFAULT);
 
1143
    if (err) Error("Failed to start event engine, error %i", err);
 
1144
    
 
1145
    if (pthread_create(&trigger_thread, NULL, Trigger, (void*)&ctx))
 
1146
        Error("Error starting trigger thread");
 
1147
 
 
1148
//    sleep(1);
 
1149
    err = pcilib_stream(handle, &GrabCallback, &ctx);
 
1150
    if (err) Error("Error streaming events, error %i", err);
 
1151
    
 
1152
    pcilib_stop(handle, PCILIB_EVENT_FLAGS_DEFAULT);
 
1153
 
 
1154
/*      
 
1155
    err = pcilib_grab(handle, PCILIB_EVENTS_ALL, &size, &data, PCILIB_TIMEOUT_TRIGGER);
 
1156
    if (err) {
 
1157
        Error("Grabbing event is failed");
 
1158
    }
 
1159
*/
 
1160
    ctx.run_flag = 0;
 
1161
    pthread_join(trigger_thread, NULL);
 
1162
    
1023
1163
    return 0;
1024
1164
}
1025
1165
 
1026
 
 
 
1166
/*
 
1167
int Trigger(pcilib_t *handle, const char *event, size_t triggers, size_t run_time, size_t trigger_time) {
 
1168
    //
 
1169
}
 
1170
*/
1027
1171
int StartStopDMA(pcilib_t *handle,  pcilib_model_description_t *model_info, pcilib_dma_engine_addr_t dma, pcilib_dma_direction_t dma_direction, int start) {
1028
1172
    int err;
1029
1173
    pcilib_dma_engine_t dmaid;
1479
1623
    int i;
1480
1624
    long itmp;
1481
1625
    unsigned long utmp;
 
1626
    size_t ztmp;
1482
1627
    unsigned char c;
1483
1628
 
 
1629
    const char *stmp;
1484
1630
    const char *num_offset;
1485
1631
 
1486
1632
    int details = 0;
1490
1636
    pcilib_model_t model = PCILIB_MODEL_DETECT;
1491
1637
    pcilib_model_description_t *model_info;
1492
1638
    MODE mode = MODE_INVALID;
 
1639
    GRAB_MODE grab_mode = 0;
 
1640
    size_t trigger_time = 0;
 
1641
    size_t run_time = 0;
 
1642
    size_t buffer = 0;
 
1643
    FORMAT format = FORMAT_RAW;
 
1644
    PARTITION partition = PARTITION_UNKNOWN;
1493
1645
    FLAGS flags = 0;
1494
1646
    const char *type = NULL;
1495
1647
    ACCESS_MODE amode = ACCESS_BAR;
1500
1652
    const char *bank = NULL;
1501
1653
    char **data = NULL;
1502
1654
    const char *event = NULL;
 
1655
    const char *data_type = NULL;
1503
1656
    const char *dma_channel = NULL;
1504
1657
    const char *use = NULL;
1505
1658
    pcilib_kmem_use_t use_id;
1568
1721
                else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
1569
1722
            break;
1570
1723
            case OPT_GRAB:
1571
 
                if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
1572
 
 
1573
 
                mode = MODE_GRAB;
1574
 
                if (optarg) event = optarg;
1575
 
                else if ((optind < argc)&&(argv[optind][0] != '-')) event = argv[optind++];
 
1724
                if ((mode != MODE_INVALID)&&((mode != MODE_GRAB)||(grab_mode&GRAB_MODE_GRAB))) Usage(argc, argv, "Multiple operations are not supported");
 
1725
 
 
1726
                mode = MODE_GRAB;
 
1727
                grab_mode |= GRAB_MODE_GRAB;
 
1728
                
 
1729
                stmp = NULL;
 
1730
                if (optarg) stmp = optarg;
 
1731
                else if ((optind < argc)&&(argv[optind][0] != '-')) stmp = argv[optind++];
 
1732
 
 
1733
                if (stmp) {
 
1734
                    if ((event)&&(strcasecmp(stmp,event))) Usage(argc, argv, "Redefinition of considered event");
 
1735
                    event = stmp;
 
1736
                }
 
1737
            break;
 
1738
            case OPT_TRIGGER:
 
1739
                if ((mode != MODE_INVALID)&&((mode != MODE_GRAB)||(grab_mode&GRAB_MODE_TRIGGER))) Usage(argc, argv, "Multiple operations are not supported");
 
1740
 
 
1741
                mode = MODE_GRAB;
 
1742
                grab_mode |= GRAB_MODE_TRIGGER;
 
1743
                
 
1744
                stmp = NULL;
 
1745
                if (optarg) stmp = optarg;
 
1746
                else if ((optind < argc)&&(argv[optind][0] != '-')) stmp = argv[optind++];
 
1747
 
 
1748
                if (stmp) {
 
1749
                    if ((event)&&(strcasecmp(stmp,event))) Usage(argc, argv, "Redefinition of considered event");
 
1750
                    event = stmp;
 
1751
                }
1576
1752
            break;
1577
1753
            case OPT_LIST_DMA:
1578
1754
                if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
1708
1884
            break;
1709
1885
            case OPT_SIZE:
1710
1886
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &size) != 1))
1711
 
                    Usage(argc, argv, "Invalid size is specified (%s)", optarg);
 
1887
                    if (strcasecmp(optarg, "unlimited"))
 
1888
                        Usage(argc, argv, "Invalid size is specified (%s)", optarg);
 
1889
                    else
 
1890
                        size = (size_t)-1;
 
1891
                        
1712
1892
                size_set = 1;
1713
1893
            break;
1714
1894
            case OPT_ENDIANESS:
1733
1913
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &iterations) != 1))
1734
1914
                    Usage(argc, argv, "Invalid number of iterations is specified (%s)", optarg);
1735
1915
            break;
 
1916
            case OPT_EVENT:
 
1917
                event = optarg;
 
1918
            break;
 
1919
            case OPT_DATA_TYPE:
 
1920
                data_type = optarg;
 
1921
            break;
 
1922
            case OPT_RUN_TIME:
 
1923
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &run_time) != 1))
 
1924
                    Usage(argc, argv, "Invalid timeout is specified (%s)", optarg);
 
1925
            break;
 
1926
            case OPT_TRIGGER_TIME:
 
1927
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &trigger_time) != 1))
 
1928
                    Usage(argc, argv, "Invalid trigger-time is specified (%s)", optarg);
 
1929
            break;          
 
1930
            case OPT_TRIGGER_RATE:
 
1931
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &ztmp) != 1))
 
1932
                    Usage(argc, argv, "Invalid trigger-rate is specified (%s)", optarg);
 
1933
                    
 
1934
                    trigger_time = 1000000 / ztmp + (1000000 % ztmp)?1:0;
 
1935
            break;
 
1936
            case OPT_BUFFER:
 
1937
                if (optarg) num_offset = optarg;
 
1938
                else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
 
1939
                else num_offset = NULL;
 
1940
                
 
1941
                if (num_offset) {
 
1942
                    if ((!isnumber(num_offset))||(sscanf(num_offset, "%zu", &buffer) != 1))
 
1943
                        Usage(argc, argv, "Invalid buffer size is specified (%s)", num_offset);
 
1944
                    buffer *= 1024 * 1024;
 
1945
                } else {
 
1946
                    buffer = get_free_memory();
 
1947
                    if (buffer < 256) Error("Not enough free memory (%lz MB) for buffering", buffer / 1024 / 1024);
 
1948
                    
 
1949
                    buffer -= 128 + buffer/16;
 
1950
                }
 
1951
            break;         
 
1952
            case OPT_FORMAT:
 
1953
                if (!strcasecmp(optarg, "add_header")) format =  FORMAT_HEADER;
 
1954
                else if (!strcasecmp(optarg, "ringfs")) format =  FORMAT_RINGFS;
 
1955
                else if (strcasecmp(optarg, "raw")) Error("Invalid format (%s) is specified", optarg);
 
1956
            break; 
1736
1957
            case OPT_QUIETE:
1737
1958
                quiete = 1;
1738
1959
            break;
1869
2090
            }
1870
2091
        } 
1871
2092
    }
 
2093
        
 
2094
    if (mode == MODE_GRAB) {
 
2095
        if (output) {
 
2096
            char fsname[128];
 
2097
            if (!get_file_fs(output, 127, fsname)) {
 
2098
                if (!strcmp(fsname, "ext4")) partition = PARTITION_EXT4;
 
2099
                else if (!strcmp(fsname, "raw")) partition = PARTITION_RAW;
 
2100
            }
 
2101
        }
 
2102
    }
 
2103
    
 
2104
    if (mode != MODE_GRAB) {
 
2105
        if (size == (size_t)-1)
 
2106
            Usage(argc, argv, "Unlimited size is not supported in selected operation mode");
 
2107
    }
1872
2108
    
1873
2109
 
1874
2110
    if ((bank)&&(amode == ACCESS_DMA)) {
1931
2167
        pcilib_reset(handle);
1932
2168
     break;
1933
2169
     case MODE_GRAB:
1934
 
        Grab(handle, event, ofile);
 
2170
        TriggerAndGrab(handle, grab_mode, event, data_type, size, run_time, trigger_time, partition, format, buffer, ofile);
1935
2171
     break;
1936
2172
     case MODE_LIST_DMA:
1937
2173
        ListDMA(handle, fpga_device, model_info);