cli.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837
  1. #define _XOPEN_SOURCE 700
  2. #define _POSIX_C_SOURCE 200112L
  3. #define _BSD_SOURCE
  4. #define _GNU_SOURCE
  5. #define _DEFAULT_SOURCE
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <strings.h>
  10. #include <stdint.h>
  11. #include <stdarg.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <sys/time.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/mman.h>
  17. #include <errno.h>
  18. #include <alloca.h>
  19. #include <arpa/inet.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <dirent.h>
  23. #include <pthread.h>
  24. #include <signal.h>
  25. #include <dlfcn.h>
  26. #include <getopt.h>
  27. #include <fastwriter.h>
  28. #include "pcitool/sysinfo.h"
  29. #include "pcitool/formaters.h"
  30. #include "views/transform.h"
  31. #include "views/enum.h"
  32. #include "pci.h"
  33. #include "plugin.h"
  34. #include "config.h"
  35. #include "tools.h"
  36. #include "kmem.h"
  37. #include "error.h"
  38. #include "debug.h"
  39. #include "model.h"
  40. #include "locking.h"
  41. /* defines */
  42. #define MAX_KBUF 14
  43. //#define BIGBUFSIZE (512*1024*1024)
  44. #define BIGBUFSIZE (1024*1024)
  45. #define DEFAULT_FPGA_DEVICE "/dev/fpga0"
  46. #define LINE_WIDTH 80
  47. #define SEPARATOR_WIDTH 2
  48. #define BLOCK_SEPARATOR_WIDTH 2
  49. #define BLOCK_SIZE 8
  50. #define BENCHMARK_ITERATIONS 128
  51. #define STATUS_MESSAGE_INTERVAL 5 /* seconds */
  52. #define isnumber pcilib_isnumber
  53. #define isxnumber pcilib_isxnumber
  54. #define isnumber_n pcilib_isnumber_n
  55. #define isxnumber_n pcilib_isxnumber_n
  56. typedef uint8_t access_t;
  57. typedef enum {
  58. GRAB_MODE_GRAB = 1,
  59. GRAB_MODE_TRIGGER = 2
  60. } GRAB_MODE;
  61. typedef enum {
  62. MODE_INVALID,
  63. MODE_INFO,
  64. MODE_LIST,
  65. MODE_BENCHMARK,
  66. MODE_READ,
  67. MODE_READ_REGISTER,
  68. MODE_READ_PROPERTY,
  69. MODE_READ_ATTR,
  70. MODE_WRITE,
  71. MODE_WRITE_REGISTER,
  72. MODE_WRITE_PROPERTY,
  73. MODE_RESET,
  74. MODE_GRAB,
  75. MODE_START_DMA,
  76. MODE_STOP_DMA,
  77. MODE_LIST_DMA,
  78. MODE_LIST_DMA_BUFFERS,
  79. MODE_READ_DMA_BUFFER,
  80. MODE_ENABLE_IRQ,
  81. MODE_DISABLE_IRQ,
  82. MODE_ACK_IRQ,
  83. MODE_WAIT_IRQ,
  84. MODE_ALLOC_KMEM,
  85. MODE_LIST_KMEM,
  86. MODE_READ_KMEM,
  87. MODE_FREE_KMEM,
  88. MODE_LIST_LOCKS,
  89. MODE_FREE_LOCKS,
  90. MODE_LOCK,
  91. MODE_UNLOCK
  92. } MODE;
  93. typedef enum {
  94. ACCESS_BAR,
  95. ACCESS_DMA,
  96. ACCESS_FIFO,
  97. ACCESS_CONFIG
  98. } ACCESS_MODE;
  99. typedef enum {
  100. FLAG_MULTIPACKET = 1,
  101. FLAG_WAIT = 2
  102. } FLAGS;
  103. typedef enum {
  104. FORMAT_DEFAULT = 0,
  105. FORMAT_RAW,
  106. FORMAT_HEADER,
  107. FORMAT_RINGFS
  108. } FORMAT;
  109. typedef enum {
  110. PARTITION_UNKNOWN,
  111. PARTITION_RAW,
  112. PARTITION_EXT4,
  113. PARTITION_NULL
  114. } PARTITION;
  115. typedef enum {
  116. OPT_DEVICE = 'd',
  117. OPT_MODEL = 'm',
  118. OPT_BAR = 'b',
  119. OPT_ACCESS = 'a',
  120. OPT_ENDIANESS = 'e',
  121. OPT_SIZE = 's',
  122. OPT_OUTPUT = 'o',
  123. OPT_TIMEOUT = 't',
  124. OPT_INFO = 'i',
  125. OPT_LIST = 'l',
  126. OPT_READ = 'r',
  127. OPT_WRITE = 'w',
  128. OPT_GRAB = 'g',
  129. OPT_QUIETE = 'q',
  130. OPT_HELP = 'h',
  131. OPT_RESET = 128,
  132. OPT_BENCHMARK,
  133. OPT_TRIGGER,
  134. OPT_DATA_TYPE,
  135. OPT_EVENT,
  136. OPT_TRIGGER_RATE,
  137. OPT_TRIGGER_TIME,
  138. OPT_RUN_TIME,
  139. OPT_FORMAT,
  140. OPT_BUFFER,
  141. OPT_THREADS,
  142. OPT_LIST_DMA,
  143. OPT_LIST_DMA_BUFFERS,
  144. OPT_READ_DMA_BUFFER,
  145. OPT_START_DMA,
  146. OPT_STOP_DMA,
  147. OPT_ENABLE_IRQ,
  148. OPT_DISABLE_IRQ,
  149. OPT_ACK_IRQ,
  150. OPT_WAIT_IRQ,
  151. OPT_ITERATIONS,
  152. OPT_ALLOC_KMEM,
  153. OPT_LIST_KMEM,
  154. OPT_FREE_KMEM,
  155. OPT_READ_KMEM,
  156. OPT_LIST_LOCKS,
  157. OPT_FREE_LOCKS,
  158. OPT_LOCK,
  159. OPT_UNLOCK,
  160. OPT_BLOCK_SIZE,
  161. OPT_ALIGNMENT,
  162. OPT_TYPE,
  163. OPT_FORCE,
  164. OPT_VERIFY,
  165. OPT_WAIT,
  166. OPT_MULTIPACKET,
  167. OPT_VERBOSE
  168. } OPTIONS;
  169. static struct option long_options[] = {
  170. {"device", required_argument, 0, OPT_DEVICE },
  171. {"model", required_argument, 0, OPT_MODEL },
  172. {"bar", required_argument, 0, OPT_BAR },
  173. {"access", required_argument, 0, OPT_ACCESS },
  174. {"endianess", required_argument, 0, OPT_ENDIANESS },
  175. {"size", required_argument, 0, OPT_SIZE },
  176. {"output", required_argument, 0, OPT_OUTPUT },
  177. {"timeout", required_argument, 0, OPT_TIMEOUT },
  178. {"iterations", required_argument, 0, OPT_ITERATIONS },
  179. {"info", optional_argument, 0, OPT_INFO },
  180. {"list", optional_argument, 0, OPT_LIST },
  181. {"reset", no_argument, 0, OPT_RESET },
  182. {"benchmark", optional_argument, 0, OPT_BENCHMARK },
  183. {"read", optional_argument, 0, OPT_READ },
  184. {"write", optional_argument, 0, OPT_WRITE },
  185. {"grab", optional_argument, 0, OPT_GRAB },
  186. {"trigger", optional_argument, 0, OPT_TRIGGER },
  187. {"data", required_argument, 0, OPT_DATA_TYPE },
  188. {"event", required_argument, 0, OPT_EVENT },
  189. {"run-time", required_argument, 0, OPT_RUN_TIME },
  190. {"trigger-rate", required_argument, 0, OPT_TRIGGER_RATE },
  191. {"trigger-time", required_argument, 0, OPT_TRIGGER_TIME },
  192. {"format", required_argument, 0, OPT_FORMAT },
  193. {"buffer", optional_argument, 0, OPT_BUFFER },
  194. {"threads", optional_argument, 0, OPT_THREADS },
  195. {"start-dma", required_argument, 0, OPT_START_DMA },
  196. {"stop-dma", optional_argument, 0, OPT_STOP_DMA },
  197. {"list-dma-engines", no_argument, 0, OPT_LIST_DMA },
  198. {"list-dma-buffers", required_argument, 0, OPT_LIST_DMA_BUFFERS },
  199. {"read-dma-buffer", required_argument, 0, OPT_READ_DMA_BUFFER },
  200. {"enable-irq", optional_argument, 0, OPT_ENABLE_IRQ },
  201. {"disable-irq", optional_argument, 0, OPT_DISABLE_IRQ },
  202. {"acknowledge-irq", optional_argument, 0, OPT_ACK_IRQ },
  203. {"wait-irq", optional_argument, 0, OPT_WAIT_IRQ },
  204. {"list-kernel-memory", optional_argument, 0, OPT_LIST_KMEM },
  205. {"read-kernel-memory", required_argument, 0, OPT_READ_KMEM },
  206. {"alloc-kernel-memory", required_argument, 0, OPT_ALLOC_KMEM },
  207. {"free-kernel-memory", required_argument, 0, OPT_FREE_KMEM },
  208. {"list-locks", no_argument, 0, OPT_LIST_LOCKS },
  209. {"free-locks", no_argument, 0, OPT_FREE_LOCKS },
  210. {"lock", required_argument, 0, OPT_LOCK },
  211. {"unlock", required_argument, 0, OPT_UNLOCK },
  212. {"type", required_argument, 0, OPT_TYPE },
  213. {"block-size", required_argument, 0, OPT_BLOCK_SIZE },
  214. {"alignment", required_argument, 0, OPT_ALIGNMENT },
  215. {"quiete", no_argument, 0, OPT_QUIETE },
  216. {"verbose", optional_argument, 0, OPT_VERBOSE },
  217. {"force", no_argument, 0, OPT_FORCE },
  218. {"verify", no_argument, 0, OPT_VERIFY },
  219. {"multipacket", no_argument, 0, OPT_MULTIPACKET },
  220. {"wait", no_argument, 0, OPT_WAIT },
  221. {"help", no_argument, 0, OPT_HELP },
  222. { 0, 0, 0, 0 }
  223. };
  224. void Usage(int argc, char *argv[], const char *format, ...) {
  225. if (format) {
  226. va_list ap;
  227. va_start(ap, format);
  228. printf("Error %i: ", errno);
  229. vprintf(format, ap);
  230. printf("\n");
  231. va_end(ap);
  232. printf("\n");
  233. }
  234. printf(
  235. "Usage:\n"
  236. " %s <mode> [options] [hex data]\n"
  237. " Modes:\n"
  238. " -i [target] - Device or Register (target) Info\n"
  239. " -l[l] [bank|/branch] - List (detailed) Data Banks & Registers\n"
  240. " -r <addr|dmaX|reg|prop> - Read Data/Register/Property\n"
  241. " -w <addr|dmaX|reg|prop> - Write Data/Register/Property\n"
  242. " --benchmark <barX|dmaX> - Performance Evaluation\n"
  243. " --reset - Reset board\n"
  244. " --help - Help message\n"
  245. "\n"
  246. " Property/Register Modes:\n"
  247. " -r <reg>/view[:unit] - Read register view\n"
  248. " -w <reg>/view[:unit] - Write register view\n"
  249. " -r <reg>/unit - Read register, detect view based on unit\n"
  250. " -w <reg>/unit - Write register, detect view based on unt\n"
  251. " -r <prop>[:unit] - Read property\n"
  252. " -w <prop>[:unit] - Write property\n"
  253. " -r <prop|reg>@attr - Read register/property attribute\n"
  254. "\n"
  255. " Event Modes:\n"
  256. " --trigger [event] - Trigger Events\n"
  257. " -g [event] - Grab Events\n"
  258. "\n"
  259. " IRQ Modes:\n"
  260. " --enable-irq [type] - Enable IRQs\n"
  261. " --disable-irq [type] - Disable IRQs\n"
  262. " --acknowledge-irq <source> - Clean IRQ queue\n"
  263. " --wait-irq <source> - Wait for IRQ\n"
  264. " DMA Modes:\n"
  265. " --start-dma <num>[r|w] - Start specified DMA engine\n"
  266. " --stop-dma [num[r|w]] - Stop specified engine or DMA subsystem\n"
  267. " --list-dma-engines - List active DMA engines\n"
  268. " --list-dma-buffers <dma> - List buffers for specified DMA engine\n"
  269. " --read-dma-buffer <dma:buf> - Read the specified buffer\n"
  270. "\n"
  271. " Kernel Modes:\n"
  272. " --list-kernel-memory [use] - List kernel buffers\n"
  273. " --read-kernel-memory <blk> - Read the specified block of the kernel memory\n"
  274. " block is specified as: use:block_number\n"
  275. " --alloc-kernel-memory <use> - Allocate kernel buffers (DANGEROUS)\n"
  276. " --free-kernel-memory <use> - Cleans lost kernel space buffers (DANGEROUS)\n"
  277. " dma - Remove all buffers allocated by DMA subsystem\n"
  278. " #number - Remove all buffers with the specified use id\n"
  279. "\n"
  280. " --list-locks - List all registered locks\n"
  281. " --free-locks - Destroy all locks (DANGEROUS)\n"
  282. " --lock <lock name> - Obtain persistent lock\n"
  283. " --unlock <lock name> - Release persistent lock\n"
  284. "\n"
  285. " Addressing:\n"
  286. " -d <device> - FPGA device (/dev/fpga0)\n"
  287. " -m <model> - Memory model (autodetected)\n"
  288. " pci - Plain\n"
  289. " ipecamera - IPE Camera\n"
  290. " -b <bank> - PCI bar, Register bank, or DMA channel\n"
  291. "\n"
  292. " Options:\n"
  293. " -s <size> - Number of words (default: 1)\n"
  294. " -a [fifo|dma|config]<bits> - Access type and bits per word (default: 32)\n"
  295. " -e <l|b> - Endianess Little/Big (default: host)\n"
  296. " -o <file> - Append output to file (default: stdout)\n"
  297. " -t <timeout|unlimited> - Timeout in microseconds\n"
  298. " --check - Verify write operations\n"
  299. "\n"
  300. " Event Options:\n"
  301. " --event <evt> - Specifies event for trigger and grab modes\n"
  302. " --data <type> - Data type to request for the events\n"
  303. " --run-time <us> - Limit time to grab/trigger events\n"
  304. " -t <timeout|unlimited> - Timeout to stop if no events triggered\n"
  305. " --trigger-rate <tps> - Generate tps triggers per second\n"
  306. " --trigger-time <us> - Specifies delay between triggers (us)\n"
  307. " -s <num|unlimited> - Number of events to grab and trigger\n"
  308. " --format [type] - Specifies how event data should be stored\n"
  309. " raw - Just write all events sequentially\n"
  310. " add_header - Prefix events with 512 bit header:\n"
  311. " event(64), data(64), nope(64), size(64)\n"
  312. " seqnum(64), offset(64), timestamp(128)\n"
  313. //" ringfs - Write to RingFS\n"
  314. " --buffer [size] - Request data buffering, size in MB\n"
  315. " --threads [num] - Allow multithreaded processing\n"
  316. "\n"
  317. " DMA Options:\n"
  318. " --multipacket - Read multiple packets\n"
  319. " --wait - Wait until data arrives\n"
  320. "\n"
  321. " Kernel Options:\n"
  322. " --type <type> - Type of kernel memory to allocate\n"
  323. " consistent - Consistent memory\n"
  324. " s2c - DMA S2C (write) memory\n"
  325. " c2s - DMA C2S (read) memory\n"
  326. " --page-size <size> - Size of kernel buffer in bytes (default: page)\n"
  327. " -s <size> - Number of buffers to allocate (default: 1)\n"
  328. " --allignment <alignment> - Buffer alignment (default: page)\n"
  329. "\n"
  330. " Information:\n"
  331. " --verbose [level] - Announce details of ongoing operations\n"
  332. " -q - Quiete mode (suppress warnings)\n"
  333. "\n"
  334. " Data:\n"
  335. " Data can be specified as sequence of hexdecimal number or\n"
  336. " a single value prefixed with '*'. In this case it will be\n"
  337. " replicated the specified amount of times\n"
  338. "\n\n",
  339. argv[0]);
  340. exit(0);
  341. }
  342. static int StopFlag = 0;
  343. static void signal_exit_handler(int signo) {
  344. if (++StopFlag > 2)
  345. exit(-1);
  346. }
  347. void LogError(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *format, va_list ap) {
  348. vprintf(format, ap);
  349. if (prio == PCILIB_LOG_ERROR) {
  350. if (errno) printf("\nerrno: %i (%s)", errno, strerror(errno));
  351. }
  352. printf("\n");
  353. if (prio == PCILIB_LOG_ERROR) {
  354. printf("Exiting at [%s:%u]\n\n", file, line);
  355. exit(-1);
  356. }
  357. }
  358. void ErrorInternal(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *format, ...) {
  359. va_list ap;
  360. va_start(ap, format);
  361. LogError(arg, file, line, prio, format, ap);
  362. va_end(ap);
  363. }
  364. #define Error(...) ErrorInternal(NULL, __FILE__, __LINE__, PCILIB_LOG_ERROR, __VA_ARGS__)
  365. int RegisterCompare(const void *aptr, const void *bptr, void *registers) {
  366. pcilib_register_description_t *a = &((pcilib_register_description_t*)registers)[*(const pcilib_register_t*)aptr];
  367. pcilib_register_description_t *b = &((pcilib_register_description_t*)registers)[*(const pcilib_register_t*)bptr];
  368. if (a->bank < b->bank) return -1;
  369. if (a->bank > b->bank) return 1;
  370. if (a->addr < b->addr) return -1;
  371. if (a->addr > b->addr) return 1;
  372. if ((a->type != PCILIB_REGISTER_BITS)&&(b->type == PCILIB_REGISTER_BITS)) return -1;
  373. if ((a->type == PCILIB_REGISTER_BITS)&&(b->type != PCILIB_REGISTER_BITS)) return 1;
  374. if (a->offset < b->offset) return -1;
  375. if (a->offset > b->offset) return 0;
  376. return 0;
  377. }
  378. void ListProperties(pcilib_t *handle, const char *branch, int details) {
  379. int i;
  380. pcilib_property_info_t *props;
  381. props = pcilib_get_property_list(handle, branch, 0);
  382. if (!props) Error("Error getting properties");
  383. if (props[0].path) {
  384. printf("Properties: \n");
  385. for (i = 0; props[i].path; i++) {
  386. const char *mode;
  387. const char *type;
  388. switch (props[i].type) {
  389. case PCILIB_TYPE_LONG:
  390. type = "int ";
  391. break;
  392. case PCILIB_TYPE_DOUBLE:
  393. type = "float ";
  394. break;
  395. case PCILIB_TYPE_STRING:
  396. type = "string ";
  397. break;
  398. case PCILIB_TYPE_INVALID:
  399. type = NULL;
  400. break;
  401. default:
  402. type = "unknown";
  403. }
  404. switch (props[i].mode) {
  405. case PCILIB_ACCESS_RW:
  406. mode = "RW";
  407. break;
  408. case PCILIB_ACCESS_R:
  409. mode = "R ";
  410. break;
  411. case PCILIB_ACCESS_W:
  412. mode = "W ";
  413. break;
  414. default:
  415. mode = " ";
  416. }
  417. if (type)
  418. printf(" (%s %s) ", type, mode);
  419. else
  420. printf(" %12s", "");
  421. if (props[i].flags&PCILIB_LIST_FLAG_CHILDS)
  422. printf(" + ");
  423. else
  424. printf(" ");
  425. if (details > 0) {
  426. printf("%s", props[i].name);
  427. if ((props[i].description)&&(props[i].description[0])) {
  428. printf(": %s", props[i].description);
  429. }
  430. } else {
  431. printf("%s", props[i].path);
  432. }
  433. printf("\n");
  434. }
  435. printf("\n");
  436. pcilib_free_property_info(handle, props);
  437. }
  438. }
  439. void List(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *bank, int details) {
  440. int i, j, k;
  441. const pcilib_register_bank_description_t *banks;
  442. const pcilib_register_description_t *registers;
  443. const pcilib_event_description_t *events;
  444. const pcilib_event_data_type_description_t *types;
  445. const pcilib_board_info_t *board_info = pcilib_get_board_info(handle);
  446. const pcilib_dma_description_t *dma_info = pcilib_get_dma_description(handle);
  447. for (i = 0; i < PCILIB_MAX_BARS; i++) {
  448. if (board_info->bar_length[i] > 0) {
  449. printf(" BAR %d - ", i);
  450. switch ( board_info->bar_flags[i]&IORESOURCE_TYPE_BITS) {
  451. case IORESOURCE_IO: printf(" IO"); break;
  452. case IORESOURCE_MEM: printf("MEM"); break;
  453. case IORESOURCE_IRQ: printf("IRQ"); break;
  454. case IORESOURCE_DMA: printf("DMA"); break;
  455. }
  456. if (board_info->bar_flags[i]&IORESOURCE_MEM_64) printf("64");
  457. else printf("32");
  458. printf(", Start: 0x%08lx, Length: 0x%8lx, Flags: 0x%08lx\n", board_info->bar_start[i], board_info->bar_length[i], board_info->bar_flags[i] );
  459. }
  460. }
  461. printf("\n");
  462. if ((dma_info)&&(dma_info->engines)) {
  463. printf("DMA Engines: \n");
  464. for (i = 0; dma_info->engines[i].addr_bits; i++) {
  465. const pcilib_dma_engine_description_t *engine = &dma_info->engines[i];
  466. printf(" DMA %2d ", engine->addr);
  467. switch (engine->direction) {
  468. case PCILIB_DMA_FROM_DEVICE:
  469. printf("C2S");
  470. break;
  471. case PCILIB_DMA_TO_DEVICE:
  472. printf("S2C");
  473. break;
  474. case PCILIB_DMA_BIDIRECTIONAL:
  475. printf("BI ");
  476. break;
  477. }
  478. printf(" - Type: ");
  479. switch (engine->type) {
  480. case PCILIB_DMA_TYPE_BLOCK:
  481. printf("Block");
  482. break;
  483. case PCILIB_DMA_TYPE_PACKET:
  484. printf("Packet");
  485. break;
  486. default:
  487. printf("Unknown");
  488. }
  489. printf(", Address Width: %02lu bits\n", engine->addr_bits);
  490. }
  491. printf("\n");
  492. }
  493. if ((bank)&&(bank != (char*)-1)) banks = NULL;
  494. else banks = model_info->banks;
  495. if (banks) {
  496. printf("Banks: \n");
  497. for (i = 0; banks[i].access; i++) {
  498. printf(" 0x%02x %s", banks[i].addr, banks[i].name);
  499. if ((banks[i].description)&&(banks[i].description[0])) {
  500. printf(": %s", banks[i].description);
  501. }
  502. printf("\n");
  503. }
  504. printf("\n");
  505. }
  506. if (bank == (char*)-1) registers = NULL;
  507. else registers = model_info->registers;
  508. if (registers) {
  509. pcilib_register_t regsort[handle->num_reg];
  510. pcilib_register_bank_addr_t bank_addr = 0;
  511. if (bank) {
  512. pcilib_register_bank_t bank_id = pcilib_find_register_bank(handle, bank);
  513. const pcilib_register_bank_description_t *b = model_info->banks + bank_id;
  514. bank_addr = b->addr;
  515. if (b->description) printf("%s:\n", b->description);
  516. else if (b->name) printf("Registers of bank %s:\n", b->name);
  517. else printf("Registers of bank 0x%x:\n", b->addr);
  518. } else {
  519. printf("Registers: \n");
  520. }
  521. // sorting
  522. for (i = 0, k = 0; registers[i].bits; i++) {
  523. if ((bank)&&(registers[i].bank != bank_addr)) continue;
  524. if ((registers[i].type == PCILIB_REGISTER_BITS)&&(!details)) continue;
  525. regsort[k++] = i;
  526. }
  527. qsort_r(regsort, k, sizeof(pcilib_register_t), &RegisterCompare, (void*)registers);
  528. for (j = 0; j < k; j++) {
  529. const char *mode;
  530. i = regsort[j];
  531. if (registers[i].type == PCILIB_REGISTER_BITS) {
  532. if (!details) continue;
  533. if (registers[i].bits > 1) {
  534. printf(" [%2u:%2u] - %s\n", registers[i].offset, registers[i].offset + registers[i].bits, registers[i].name);
  535. } else {
  536. printf(" [ %2u] - %s\n", registers[i].offset, registers[i].name);
  537. }
  538. continue;
  539. }
  540. if (registers[i].mode == PCILIB_REGISTER_RW) mode = "RW";
  541. else if (registers[i].mode == PCILIB_REGISTER_R) mode = "R ";
  542. else if (registers[i].mode == PCILIB_REGISTER_W) mode = " W";
  543. else mode = " ";
  544. printf(" 0x%02x (%2i %s) %s", registers[i].addr, registers[i].bits, mode, registers[i].name);
  545. if ((details > 0)&&(registers[i].description)&&(registers[i].description[0])) {
  546. printf(": %s", registers[i].description);
  547. }
  548. printf("\n");
  549. }
  550. printf("\n");
  551. }
  552. ListProperties(handle, "/", details);
  553. if (bank == (char*)-1) events = NULL;
  554. else {
  555. events = model_info->events;
  556. types = model_info->data_types;
  557. }
  558. if (events) {
  559. printf("Events: \n");
  560. for (i = 0; events[i].name; i++) {
  561. printf(" %s", events[i].name);
  562. if ((events[i].description)&&(events[i].description[0])) {
  563. printf(": %s", events[i].description);
  564. }
  565. if (types) {
  566. for (j = 0; types[j].name; j++) {
  567. if (types[j].evid & events[i].evid) {
  568. printf("\n %s", types[j].name);
  569. if ((types[j].description)&&(types[j].description[0])) {
  570. printf(": %s", types[j].description);
  571. }
  572. }
  573. }
  574. }
  575. }
  576. printf("\n");
  577. }
  578. }
  579. void ViewInfo(pcilib_t *handle, pcilib_register_t reg, size_t id) {
  580. int err;
  581. int i;
  582. pcilib_value_t val = {0};
  583. pcilib_register_value_name_t *vnames;
  584. pcilib_view_t view;
  585. const pcilib_model_description_t *model_info = pcilib_get_model_description(handle);
  586. const pcilib_register_description_t *r;
  587. const pcilib_view_description_t *v;
  588. if (reg == PCILIB_REGISTER_INVALID) {
  589. r = NULL;
  590. view = id;
  591. } else {
  592. r = &model_info->registers[reg];
  593. view = pcilib_find_view_by_name(handle, r->views[id].view);
  594. }
  595. if (view == PCILIB_VIEW_INVALID) return;
  596. v = model_info->views[view];
  597. if (r) {
  598. printf(" View %s (", r->views[id].name);
  599. } else {
  600. printf("%s\n", v->name);
  601. printf(" Data type : ");
  602. }
  603. switch (v->type) {
  604. case PCILIB_TYPE_STRING:
  605. printf("char*");
  606. break;
  607. case PCILIB_TYPE_DOUBLE:
  608. printf("double");
  609. break;
  610. case PCILIB_TYPE_LONG:
  611. printf("long");
  612. break;
  613. default:
  614. printf("unknown");
  615. }
  616. if (r) printf(")");
  617. printf("\n");
  618. if (v->mode&PCILIB_ACCESS_R) {
  619. if (r) {
  620. err = pcilib_read_register_view_by_id(handle, reg, r->views[id].name, &val);
  621. } else {
  622. err = pcilib_get_property(handle, v->name, &val);
  623. }
  624. if (!err) err = pcilib_convert_value_type(handle, &val, PCILIB_TYPE_STRING);
  625. } else {
  626. err = PCILIB_ERROR_NOTPERMITED;
  627. }
  628. if (err) {
  629. if (err == PCILIB_ERROR_NOTPERMITED)
  630. printf(" Current value : no read access\n");
  631. else
  632. printf(" Current value : error %i\n", err);
  633. } else {
  634. printf(" Current value : %s", val.sval);
  635. if (v->unit) printf(" (units: %s)", v->unit);
  636. printf("\n");
  637. }
  638. if (v->unit) {
  639. pcilib_unit_t unit = pcilib_find_unit_by_name(handle, v->unit);
  640. printf(" Supported units: %s", v->unit);
  641. if (unit != PCILIB_UNIT_INVALID) {
  642. const pcilib_unit_description_t *u = &model_info->units[unit];
  643. for (i = 0; u->transforms[i].unit; i++)
  644. printf(", %s", u->transforms[i].unit);
  645. }
  646. printf("\n");
  647. }
  648. printf(" Access : ");
  649. if ((v->mode&PCILIB_REGISTER_RW) == 0) printf("-");
  650. if (v->mode&PCILIB_REGISTER_R) printf("R");
  651. if (v->mode&PCILIB_REGISTER_W) printf("W");
  652. printf("\n");
  653. if ((v->api == &pcilib_enum_view_static_api)||(v->api == &pcilib_enum_view_xml_api)) {
  654. vnames = ((pcilib_enum_view_description_t*)v)->names;
  655. printf(" Value aliases :");
  656. for (i = 0; vnames[i].name; i++) {
  657. if (i) printf(",");
  658. printf(" %s = %u", vnames[i].name, vnames[i].value);
  659. if (vnames[i].min != vnames[i].max)
  660. printf(" (%u - %u)", vnames[i].min, vnames[i].max);
  661. }
  662. printf("\n");
  663. } else if (v->api == &pcilib_transform_view_api) {
  664. const pcilib_transform_view_description_t *tv = (const pcilib_transform_view_description_t*)v;
  665. if (tv->read_from_reg)
  666. printf(" Read function : %s\n", tv->read_from_reg);
  667. if (tv->write_to_reg)
  668. printf(" Write function : %s\n", tv->write_to_reg);
  669. }
  670. if (v->description)
  671. printf(" Description : %s\n", v->description);
  672. }
  673. void RegisterInfo(pcilib_t *handle, pcilib_register_t reg) {
  674. int err;
  675. int i;
  676. pcilib_register_value_t regval;
  677. pcilib_register_info_t *info;
  678. const pcilib_model_description_t *model_info = pcilib_get_model_description(handle);
  679. const pcilib_register_description_t *r = &model_info->registers[reg];
  680. pcilib_register_bank_t bank = pcilib_find_register_bank_by_addr(handle, r->bank);
  681. const pcilib_register_bank_description_t *b = &model_info->banks[bank];
  682. if (r->mode&PCILIB_ACCESS_R) {
  683. err = pcilib_read_register_by_id(handle, reg, &regval);
  684. } else {
  685. err = PCILIB_ERROR_NOTPERMITED;
  686. }
  687. info = pcilib_get_register_info(handle, b->name, r->name, 0);
  688. if (!info) Error("Can't obtain register info for %s", r->name);
  689. printf("%s/%s\n", b->name, r->name);
  690. printf(" Current value: ");
  691. if (err) {
  692. if (err == PCILIB_ERROR_NOTPERMITED) printf("no read access");
  693. else printf("error %i", err);
  694. } else printf(b->format, regval);
  695. if (r->mode&PCILIB_REGISTER_W) {
  696. printf(" (default: ");
  697. printf(b->format, r->defvalue);
  698. if (info->range) {
  699. printf(", range: ");
  700. printf(b->format, info->range->min);
  701. printf (" - ");
  702. printf(b->format, info->range->max);
  703. }
  704. printf(")");
  705. }
  706. printf("\n");
  707. printf(" Address : 0x%x [%u:%u]\n", r->addr, r->offset, r->offset + r->bits);
  708. if ((info->values)&&(info->values[0].name)) {
  709. printf(" Value aliases:");
  710. for (i = 0; info->values[i].name; i++)
  711. printf(" %s", info->values[i].name);
  712. printf("\n");
  713. }
  714. printf(" Access : ");
  715. if ((r->mode&PCILIB_REGISTER_RW) == 0) printf("-");
  716. if (r->mode&PCILIB_REGISTER_R) printf("R");
  717. if (r->mode&PCILIB_REGISTER_W) printf("W");
  718. if (r->mode&PCILIB_REGISTER_W1C) printf("/reset");
  719. if (r->mode&PCILIB_REGISTER_W1I) printf("/invert");
  720. printf("\n");
  721. if (r->description)
  722. printf(" Description : %s\n", r->description);
  723. if (r->views) {
  724. printf("\nSupported Views:\n");
  725. for (i = 0; r->views[i].name; i++) {
  726. ViewInfo(handle, reg, i);
  727. }
  728. }
  729. pcilib_free_register_info(handle, info);
  730. }
  731. void Info(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *target) {
  732. int i, j;
  733. DIR *dir;
  734. void *plugin;
  735. const char *path;
  736. struct dirent *entry;
  737. const pcilib_model_description_t *info = NULL;
  738. const pcilib_board_info_t *board_info = pcilib_get_board_info(handle);
  739. const pcilib_pcie_link_info_t *link_info = pcilib_get_pcie_link_info(handle);
  740. path = getenv("PCILIB_PLUGIN_DIR");
  741. if (!path) path = PCILIB_PLUGIN_DIR;
  742. if (board_info)
  743. printf("Vendor: %x, Device: %x, Bus: %x, Slot: %x, Function: %x, Model: %s\n", board_info->vendor_id, board_info->device_id, board_info->bus, board_info->slot, board_info->func, handle->model);
  744. if (link_info) {
  745. printf(" PCIe x%u (gen%u), DMA Payload: %u (of %u)\n", link_info->link_width, link_info->link_speed, 1<<link_info->payload, 1<<link_info->max_payload);
  746. }
  747. if (board_info)
  748. printf(" Interrupt - Pin: %i, Line: %i\n", board_info->interrupt_pin, board_info->interrupt_line);
  749. printf("\n");
  750. if (target) {
  751. if (*target == '/') {
  752. pcilib_view_t view;
  753. view = pcilib_find_view_by_name(handle, target);
  754. if (view != PCILIB_VIEW_INVALID)
  755. return ViewInfo(handle, PCILIB_REGISTER_INVALID, view);
  756. Error(" No property %s is found", target);
  757. } else {
  758. pcilib_register_t reg;
  759. reg = pcilib_find_register(handle, NULL, target);
  760. if (reg != PCILIB_REGISTER_INVALID)
  761. return RegisterInfo(handle, reg);
  762. Error(" No register %s is found", target);
  763. }
  764. }
  765. List(handle, model_info, (char*)-1, 0);
  766. printf("Available models:\n");
  767. dir = opendir(path);
  768. if (dir) {
  769. while ((entry = readdir(dir))) {
  770. const char *suffix = strstr(entry->d_name, ".so");
  771. if ((!suffix)||(strlen(suffix) != 3)) continue;
  772. plugin = pcilib_plugin_load(entry->d_name);
  773. if (plugin) {
  774. info = pcilib_get_plugin_model(handle, plugin, 0, 0, NULL);
  775. if (info) {
  776. printf(" %s\n", entry->d_name);
  777. for (j = 0; info[j].name; j++) {
  778. pcilib_version_t version = info[j].api->version;
  779. printf(" %-12s %u.%u.%u - %s\n", info[j].name,
  780. PCILIB_VERSION_GET_MAJOR(version),
  781. PCILIB_VERSION_GET_MINOR(version),
  782. PCILIB_VERSION_GET_MICRO(version),
  783. info[j].description?info[j].description:"");
  784. }
  785. }
  786. pcilib_plugin_close(plugin);
  787. } else {
  788. const char *msg = dlerror();
  789. if (msg)
  790. printf(" %s: %s\n", entry->d_name, msg);
  791. }
  792. }
  793. closedir(dir);
  794. }
  795. // printf(" XML\n");
  796. printf(" Internal Models\n");
  797. for (i = 0; pcilib_dma[i].api; i++)
  798. printf(" %-12s - %s\n", pcilib_dma[i].name, pcilib_dma[i].description?pcilib_dma[i].description:"");
  799. printf(" %-12s - Plain PCI-access model\n\n", "pci");
  800. }
  801. #define BENCH_MAX_DMA_SIZE 4 * 1024 * 1024
  802. #define BENCH_MAX_FIFO_SIZE 1024 * 1024
  803. int Benchmark(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, size_t iterations) {
  804. int err;
  805. int i, j, errors;
  806. void *data, *buf, *check;
  807. void *fifo = NULL;
  808. struct timeval start, end;
  809. unsigned long time;
  810. size_t size, min_size, max_size;
  811. double mbs_in, mbs_out, mbs;
  812. size_t irqs;
  813. const pcilib_board_info_t *board_info = pcilib_get_board_info(handle);
  814. if (mode == ACCESS_CONFIG)
  815. Error("No benchmarking of configuration space acess is allowed");
  816. if (mode == ACCESS_DMA) {
  817. if (n) {
  818. min_size = n * access;
  819. max_size = n * access;
  820. } else {
  821. min_size = 1024;
  822. max_size = BENCH_MAX_DMA_SIZE;
  823. }
  824. for (size = min_size; size <= max_size; size *= 4) {
  825. mbs_in = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_FROM_DEVICE);
  826. mbs_out = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_TO_DEVICE);
  827. mbs = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_BIDIRECTIONAL);
  828. err = pcilib_wait_irq(handle, 0, 0, &irqs);
  829. if (err) irqs = 0;
  830. printf("%8zu KB - ", size / 1024);
  831. printf("RW: ");
  832. if (mbs < 0) printf("failed ... ");
  833. else printf("%8.2lf MB/s", mbs);
  834. printf(", R: ");
  835. if (mbs_in < 0) printf("failed ... ");
  836. else printf("%8.2lf MB/s", mbs_in);
  837. printf(", W: ");
  838. if (mbs_out < 0) printf("failed ... ");
  839. else printf("%8.2lf MB/s", mbs_out);
  840. if (irqs) {
  841. printf(", IRQs: %lu", irqs);
  842. }
  843. printf("\n");
  844. }
  845. return 0;
  846. }
  847. if (bar == PCILIB_BAR_INVALID) {
  848. unsigned long maxlength = 0;
  849. for (i = 0; i < PCILIB_MAX_REGISTER_BANKS; i++) {
  850. if ((addr >= board_info->bar_start[i])&&((board_info->bar_start[i] + board_info->bar_length[i]) >= (addr + access))) {
  851. bar = i;
  852. break;
  853. }
  854. if (board_info->bar_length[i] > maxlength) {
  855. maxlength = board_info->bar_length[i];
  856. bar = i;
  857. }
  858. }
  859. if (bar < 0) Error("Data banks are not available");
  860. }
  861. if (n) {
  862. if ((mode == ACCESS_BAR)&&(n * access > board_info->bar_length[bar])) Error("The specified size (%i) exceeds the size of bar (%i)", n * access, board_info->bar_length[bar]);
  863. min_size = n * access;
  864. max_size = n * access;
  865. } else {
  866. min_size = access;
  867. if (mode == ACCESS_BAR) max_size = board_info->bar_length[bar];
  868. else max_size = BENCH_MAX_FIFO_SIZE;
  869. }
  870. err = posix_memalign( (void**)&buf, 256, max_size );
  871. if (!err) err = posix_memalign( (void**)&check, 256, max_size );
  872. if ((err)||(!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", max_size);
  873. data = pcilib_map_bar(handle, bar);
  874. if (!data) Error("Can't map bar %i", bar);
  875. if (mode == ACCESS_FIFO) {
  876. fifo = data + (addr - board_info->bar_start[bar]) + (board_info->bar_start[bar] & pcilib_get_page_mask());
  877. // pcilib_resolve_register_address(handle, bar, addr);
  878. if (!fifo) Error("Can't resolve address (%lx) in bar (%u)", addr, bar);
  879. }
  880. if (mode == ACCESS_FIFO)
  881. printf("Transfer time (Bank: %i, Fifo: %lx):\n", bar, addr);
  882. else
  883. printf("Transfer time (Bank: %i):\n", bar);
  884. for (size = min_size ; size < max_size; size *= 8) {
  885. gettimeofday(&start,NULL);
  886. if (mode == ACCESS_BAR) {
  887. for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
  888. pcilib_memcpy(buf, data, size);
  889. }
  890. } else {
  891. for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
  892. for (j = 0; j < (size/access); j++) {
  893. pcilib_memcpy(buf + j * access, fifo, access);
  894. }
  895. }
  896. }
  897. gettimeofday(&end,NULL);
  898. time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
  899. printf("%8zu bytes - read: %8.2lf MB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
  900. fflush(0);
  901. gettimeofday(&start,NULL);
  902. if (mode == ACCESS_BAR) {
  903. for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
  904. pcilib_memcpy(data, buf, size);
  905. }
  906. } else {
  907. for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
  908. for (j = 0; j < (size/access); j++) {
  909. pcilib_memcpy(fifo, buf + j * access, access);
  910. }
  911. }
  912. }
  913. gettimeofday(&end,NULL);
  914. time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
  915. printf(", write: %8.2lf MB/s\n", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
  916. }
  917. pcilib_unmap_bar(handle, bar, data);
  918. printf("\n\nOpen-Transfer-Close time: \n");
  919. for (size = 4 ; size < max_size; size *= 8) {
  920. gettimeofday(&start,NULL);
  921. if (mode == ACCESS_BAR) {
  922. for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
  923. pcilib_read(handle, bar, 0, size, buf);
  924. }
  925. } else {
  926. for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
  927. pcilib_read_fifo(handle, bar, addr, access, size / access, buf);
  928. }
  929. }
  930. gettimeofday(&end,NULL);
  931. time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
  932. printf("%8zu bytes - read: %8.2lf MB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
  933. fflush(0);
  934. gettimeofday(&start,NULL);
  935. if (mode == ACCESS_BAR) {
  936. for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
  937. pcilib_write(handle, bar, 0, size, buf);
  938. }
  939. } else {
  940. for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
  941. pcilib_write_fifo(handle, bar, addr, access, size / access, buf);
  942. }
  943. }
  944. gettimeofday(&end,NULL);
  945. time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
  946. printf(", write: %8.2lf MB/s", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
  947. if (mode == ACCESS_BAR) {
  948. gettimeofday(&start,NULL);
  949. for (i = 0, errors = 0; i < BENCHMARK_ITERATIONS; i++) {
  950. pcilib_write(handle, bar, 0, size, buf);
  951. pcilib_read(handle, bar, 0, size, check);
  952. if (memcmp(buf, check, size)) ++errors;
  953. }
  954. gettimeofday(&end,NULL);
  955. time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
  956. printf(", write-verify: %8.2lf MB/s", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
  957. if (errors) printf(", errors: %u of %u", errors, BENCHMARK_ITERATIONS);
  958. }
  959. printf("\n");
  960. }
  961. printf("\n\n");
  962. free(check);
  963. free(buf);
  964. return 0;
  965. }
  966. #define pci2host16(endianess, value) endianess?
  967. /*
  968. typedef struct {
  969. size_t size;
  970. void *data;
  971. size_t pos;
  972. int multi_mode;
  973. } DMACallbackContext;
  974. static int DMACallback(void *arg, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
  975. DMACallbackContext *ctx = (DMACallbackContext*)arg;
  976. if ((ctx->pos + bufsize > ctx->size)||(!ctx->data)) {
  977. ctx->size *= 2;
  978. ctx->data = realloc(ctx->data, ctx->size);
  979. if (!ctx->data) {
  980. Error("Allocation of %i bytes of memory have failed", ctx->size);
  981. return 0;
  982. }
  983. }
  984. memcpy(ctx->data + ctx->pos, buf, bufsize);
  985. ctx->pos += bufsize;
  986. if (flags & PCILIB_DMA_FLAG_EOP) return 0;
  987. return 1;
  988. }
  989. */
  990. 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) {
  991. void *buf;
  992. int i, err;
  993. size_t ret, bytes;
  994. size_t size = n * abs(access);
  995. int block_width, blocks_per_line;
  996. int numbers_per_block, numbers_per_line;
  997. pcilib_dma_engine_t dmaid;
  998. pcilib_dma_flags_t dma_flags = 0;
  999. int fd;
  1000. char stmp[256];
  1001. struct stat st;
  1002. const pcilib_board_info_t *board_info;
  1003. numbers_per_block = BLOCK_SIZE / access;
  1004. block_width = numbers_per_block * ((access * 2) + SEPARATOR_WIDTH);
  1005. blocks_per_line = (LINE_WIDTH - 10) / (block_width + BLOCK_SEPARATOR_WIDTH);
  1006. if ((blocks_per_line > 1)&&(blocks_per_line % 2)) --blocks_per_line;
  1007. numbers_per_line = blocks_per_line * numbers_per_block;
  1008. if (size) {
  1009. buf = malloc(size);
  1010. if (!buf) Error("Allocation of %zu bytes of memory has failed", size);
  1011. } else {
  1012. buf = NULL;
  1013. }
  1014. switch (mode) {
  1015. case ACCESS_DMA:
  1016. if (timeout == (size_t)-1) timeout = PCILIB_DMA_TIMEOUT;
  1017. dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_FROM_DEVICE, dma);
  1018. if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (%lu) is specified", dma);
  1019. if (flags&FLAG_MULTIPACKET) dma_flags |= PCILIB_DMA_FLAG_MULTIPACKET;
  1020. if (flags&FLAG_WAIT) dma_flags |= PCILIB_DMA_FLAG_WAIT;
  1021. if (size) {
  1022. err = pcilib_read_dma_custom(handle, dmaid, addr, size, dma_flags, timeout, buf, &bytes);
  1023. if (err) Error("Error (%i) is reported by DMA engine", err);
  1024. } else {
  1025. dma_flags |= PCILIB_DMA_FLAG_IGNORE_ERRORS;
  1026. size = 2048; bytes = 0;
  1027. do {
  1028. size *= 2;
  1029. buf = realloc(buf, size);
  1030. if (!buf) Error("Allocation of %zu bytes of memory has failed", size);
  1031. err = pcilib_read_dma_custom(handle, dmaid, addr, size - bytes, dma_flags, timeout, buf + bytes, &ret);
  1032. bytes += ret;
  1033. if ((!err)&&(flags&FLAG_MULTIPACKET)) {
  1034. err = PCILIB_ERROR_TOOBIG;
  1035. if ((flags&FLAG_WAIT)==0) timeout = 0;
  1036. }
  1037. } while (err == PCILIB_ERROR_TOOBIG);
  1038. }
  1039. if ((err)&&(err != PCILIB_ERROR_TIMEOUT)) {
  1040. Error("Error (%i) during DMA read", err);
  1041. }
  1042. if (bytes <= 0) {
  1043. pcilib_warning("No data is returned by DMA engine");
  1044. return -1;
  1045. }
  1046. size = bytes;
  1047. n = bytes / abs(access);
  1048. addr = 0;
  1049. break;
  1050. case ACCESS_FIFO:
  1051. pcilib_read_fifo(handle, bar, addr, access, n, buf);
  1052. addr = 0;
  1053. break;
  1054. case ACCESS_CONFIG:
  1055. board_info = pcilib_get_board_info(handle);
  1056. sprintf(stmp, "/sys/bus/pci/devices/0000:%02x:%02x.%1x/config", board_info->bus, board_info->slot, board_info->func);
  1057. fd = open(stmp, O_RDONLY);
  1058. if ((!fd)||(fstat(fd, &st))) Error("Can't open %s", stmp);
  1059. if (st.st_size < addr)
  1060. Error("Access beyond the end of PCI configuration space");
  1061. if (st.st_size < (addr + size)) {
  1062. n = (st.st_size - addr) / abs(access);
  1063. size = n * abs(access);
  1064. if (!n) Error("Access beyond the end of PCI configuration space");
  1065. }
  1066. lseek(fd, addr, SEEK_SET);
  1067. ret = read(fd, buf, size);
  1068. if (ret == (size_t)-1) Error("Error reading %s", stmp);
  1069. if (ret < size) {
  1070. size = ret;
  1071. n = ret / abs(access);
  1072. }
  1073. close(fd);
  1074. break;
  1075. default:
  1076. pcilib_read(handle, bar, addr, size, buf);
  1077. }
  1078. if (endianess) pcilib_swap(buf, buf, abs(access), n);
  1079. if (o) {
  1080. printf("Writting output (%zu bytes) to file (append to the end)...\n", n * abs(access));
  1081. fwrite(buf, abs(access), n, o);
  1082. } else {
  1083. for (i = 0; i < n; i++) {
  1084. if (i) {
  1085. if (i%numbers_per_line == 0) printf("\n");
  1086. else {
  1087. printf("%*s", SEPARATOR_WIDTH, "");
  1088. if (i%numbers_per_block == 0) printf("%*s", BLOCK_SEPARATOR_WIDTH, "");
  1089. }
  1090. }
  1091. if (i%numbers_per_line == 0) printf("%8lx: ", addr + i * abs(access));
  1092. switch (access) {
  1093. case 1: printf("%0*hhx", access * 2, ((uint8_t*)buf)[i]); break;
  1094. case 2: printf("%0*hx", access * 2, ((uint16_t*)buf)[i]); break;
  1095. case 4: printf("%0*x", access * 2, ((uint32_t*)buf)[i]); break;
  1096. case 8: printf("%0*lx", access * 2, ((uint64_t*)buf)[i]); break;
  1097. }
  1098. }
  1099. printf("\n\n");
  1100. }
  1101. free(buf);
  1102. return 0;
  1103. }
  1104. int ReadRegister(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *bank, const char *reg, const char *view, const char *unit, const char *attr) {
  1105. int i;
  1106. int err;
  1107. const char *format;
  1108. pcilib_register_bank_t bank_id;
  1109. pcilib_register_bank_addr_t bank_addr = 0;
  1110. pcilib_register_value_t value;
  1111. // Adding DMA registers
  1112. pcilib_get_dma_description(handle);
  1113. if (reg||view||attr) {
  1114. pcilib_value_t val = {0};
  1115. if (attr) {
  1116. if (reg) err = pcilib_get_register_attr(handle, bank, reg, attr, &val);
  1117. else if (view) err = pcilib_get_property_attr(handle, view, attr, &val);
  1118. else if (bank) err = pcilib_get_register_bank_attr(handle, bank, attr, &val);
  1119. else err = PCILIB_ERROR_INVALID_ARGUMENT;
  1120. if (err) {
  1121. if (err == PCILIB_ERROR_NOTFOUND)
  1122. Error("Attribute %s is not found", attr);
  1123. else
  1124. Error("Error (%i) reading attribute %s", err, attr);
  1125. }
  1126. err = pcilib_convert_value_type(handle, &val, PCILIB_TYPE_STRING);
  1127. if (err) Error("Error converting attribute %s to string", attr);
  1128. printf("%s = %s", attr, val.sval);
  1129. if ((val.unit)&&(strcasecmp(val.unit, "name")))
  1130. printf(" %s", val.unit);
  1131. printf(" (for %s)\n", (reg?reg:(view?view:bank)));
  1132. } else if (view) {
  1133. if (reg) {
  1134. err = pcilib_read_register_view(handle, bank, reg, view, &val);
  1135. if (err) Error("Error reading view %s of register %s", view, reg);
  1136. } else {
  1137. err = pcilib_get_property(handle, view, &val);
  1138. if (err) Error("Error reading property %s", view);
  1139. }
  1140. if (unit) {
  1141. err = pcilib_convert_value_unit(handle, &val, unit);
  1142. if (err) {
  1143. if (reg) Error("Error converting view %s of register %s to unit %s", view, reg, unit);
  1144. else Error("Error converting property %s to unit %s", view, unit);
  1145. }
  1146. }
  1147. err = pcilib_convert_value_type(handle, &val, PCILIB_TYPE_STRING);
  1148. if (err) {
  1149. if (reg) Error("Error converting view %s of register %s to string", view);
  1150. else Error("Error converting property %s to string", view);
  1151. }
  1152. printf("%s = %s", (reg?reg:view), val.sval);
  1153. if ((val.unit)&&(strcasecmp(val.unit, "name")))
  1154. printf(" %s", val.unit);
  1155. printf("\n");
  1156. } else {
  1157. pcilib_register_t regid = pcilib_find_register(handle, bank, reg);
  1158. bank_id = pcilib_find_register_bank_by_addr(handle, model_info->registers[regid].bank);
  1159. format = model_info->banks[bank_id].format;
  1160. if (!format) format = "%lu";
  1161. err = pcilib_read_register_by_id(handle, regid, &value);
  1162. if (err) Error("Error reading register %s", reg);
  1163. printf("%s = ", reg);
  1164. printf(format, value);
  1165. printf("\n");
  1166. }
  1167. } else {
  1168. if (model_info->registers) {
  1169. if (bank) {
  1170. bank_id = pcilib_find_register_bank(handle, bank);
  1171. bank_addr = model_info->banks[bank_id].addr;
  1172. }
  1173. printf("Registers:\n");
  1174. for (i = 0; model_info->registers[i].bits; i++) {
  1175. if ((model_info->registers[i].mode & PCILIB_REGISTER_R)&&((!bank)||(model_info->registers[i].bank == bank_addr))&&(model_info->registers[i].type != PCILIB_REGISTER_BITS)) {
  1176. bank_id = pcilib_find_register_bank_by_addr(handle, model_info->registers[i].bank);
  1177. format = model_info->banks[bank_id].format;
  1178. if (!format) format = "%lu";
  1179. err = pcilib_read_register_by_id(handle, i, &value);
  1180. if (err) printf(" %s = error reading value", model_info->registers[i].name);
  1181. else {
  1182. printf(" %s = ", model_info->registers[i].name);
  1183. printf(format, value);
  1184. }
  1185. printf(" [");
  1186. printf(format, model_info->registers[i].defvalue);
  1187. printf("]");
  1188. printf("\n");
  1189. }
  1190. }
  1191. } else {
  1192. printf("No registers");
  1193. }
  1194. printf("\n");
  1195. }
  1196. return 0;
  1197. }
  1198. #define WRITE_REGVAL(buf, n, access, o) {\
  1199. uint##access##_t tbuf[n]; \
  1200. for (i = 0; i < n; i++) { \
  1201. tbuf[i] = (uint##access##_t)buf[i]; \
  1202. } \
  1203. fwrite(tbuf, access/8, n, o); \
  1204. }
  1205. int ReadRegisterRange(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *bank, uintptr_t addr, long addr_shift, size_t n, FILE *o) {
  1206. int err;
  1207. int i;
  1208. const pcilib_register_bank_description_t *banks = model_info->banks;
  1209. pcilib_register_bank_t bank_id = pcilib_find_register_bank(handle, bank);
  1210. if (bank_id == PCILIB_REGISTER_BANK_INVALID) {
  1211. if (bank) Error("Invalid register bank is specified (%s)", bank);
  1212. else Error("Register bank should be specified");
  1213. }
  1214. int access = banks[bank_id].access / 8;
  1215. // int size = n * abs(access);
  1216. int block_width, blocks_per_line;
  1217. int numbers_per_block, numbers_per_line;
  1218. numbers_per_block = BLOCK_SIZE / access;
  1219. block_width = numbers_per_block * ((access * 2) + SEPARATOR_WIDTH);
  1220. blocks_per_line = (LINE_WIDTH - 6) / (block_width + BLOCK_SEPARATOR_WIDTH);
  1221. if ((blocks_per_line > 1)&&(blocks_per_line % 2)) --blocks_per_line;
  1222. numbers_per_line = blocks_per_line * numbers_per_block;
  1223. pcilib_register_value_t buf[n];
  1224. err = pcilib_read_register_space(handle, bank, addr, n, buf);
  1225. if (err) Error("Error reading register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n);
  1226. if (o) {
  1227. printf("Writting output (%zu bytes) to file (append to the end)...\n", n * abs(access));
  1228. switch (access) {
  1229. case 1: WRITE_REGVAL(buf, n, 8, o) break;
  1230. case 2: WRITE_REGVAL(buf, n, 16, o) break;
  1231. case 4: WRITE_REGVAL(buf, n, 32, o) break;
  1232. case 8: WRITE_REGVAL(buf, n, 64, o) break;
  1233. }
  1234. } else {
  1235. for (i = 0; i < n; i++) {
  1236. if (i) {
  1237. if (i%numbers_per_line == 0) printf("\n");
  1238. else {
  1239. printf("%*s", SEPARATOR_WIDTH, "");
  1240. if (i%numbers_per_block == 0) printf("%*s", BLOCK_SEPARATOR_WIDTH, "");
  1241. }
  1242. }
  1243. if (i%numbers_per_line == 0) printf("%4lx: ", addr + 4 * i - addr_shift);
  1244. printf("%0*lx", access * 2, (unsigned long)buf[i]);
  1245. }
  1246. printf("\n\n");
  1247. }
  1248. return 0;
  1249. }
  1250. int WriteData(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, char ** data, int verify) {
  1251. int read_back = 0;
  1252. void *buf, *check;
  1253. int res = 0, i, err;
  1254. int size = n * abs(access);
  1255. size_t ret;
  1256. pcilib_dma_engine_t dmaid;
  1257. if (mode == ACCESS_CONFIG)
  1258. Error("Writting to PCI configuration space is not supported");
  1259. err = posix_memalign( (void**)&buf, 256, size );
  1260. if (!err) err = posix_memalign( (void**)&check, 256, size );
  1261. if ((err)||(!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", size);
  1262. for (i = 0; i < n; i++) {
  1263. switch (access) {
  1264. case 1: res = sscanf(data[i], "%hhx", ((uint8_t*)buf)+i); break;
  1265. case 2: res = sscanf(data[i], "%hx", ((uint16_t*)buf)+i); break;
  1266. case 4: res = sscanf(data[i], "%x", ((uint32_t*)buf)+i); break;
  1267. case 8: res = sscanf(data[i], "%lx", ((uint64_t*)buf)+i); break;
  1268. default: Error("Unexpected data size (%lu)", access);
  1269. }
  1270. if ((res != 1)||(!isxnumber(data[i]))) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]);
  1271. }
  1272. if (endianess) pcilib_swap(buf, buf, abs(access), n);
  1273. switch (mode) {
  1274. case ACCESS_DMA:
  1275. dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_TO_DEVICE, dma);
  1276. if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (%lu) is specified", dma);
  1277. err = pcilib_write_dma(handle, dmaid, addr, size, buf, &ret);
  1278. if ((err)||(ret != size)) {
  1279. if (err == PCILIB_ERROR_TIMEOUT) Error("Timeout writting the data to DMA");
  1280. else if (err) Error("DMA engine returned a error while writing the data");
  1281. else if (!ret) Error("No data is written by DMA engine");
  1282. else Error("Only %lu bytes of %lu is written by DMA engine", ret, size);
  1283. }
  1284. break;
  1285. case ACCESS_FIFO:
  1286. pcilib_write_fifo(handle, bar, addr, access, n, buf);
  1287. break;
  1288. default:
  1289. pcilib_write(handle, bar, addr, size, buf);
  1290. if (verify) {
  1291. pcilib_read(handle, bar, addr, size, check);
  1292. read_back = 1;
  1293. }
  1294. }
  1295. if ((read_back)&&(memcmp(buf, check, size))) {
  1296. printf("Write failed: the data written and read differ, the foolowing is read back:\n");
  1297. if (endianess) pcilib_swap(check, check, abs(access), n);
  1298. ReadData(handle, mode, 0, dma, bar, addr, n, access, endianess, (size_t)-1, NULL);
  1299. exit(-1);
  1300. }
  1301. free(check);
  1302. free(buf);
  1303. return 0;
  1304. }
  1305. int WriteRegisterRange(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *bank, uintptr_t addr, long addr_shift, size_t n, char ** data) {
  1306. pcilib_register_value_t *buf, *check;
  1307. int res, i, err;
  1308. unsigned long value;
  1309. int size = n * sizeof(pcilib_register_value_t);
  1310. err = posix_memalign( (void**)&buf, 256, size );
  1311. if (!err) err = posix_memalign( (void**)&check, 256, size );
  1312. if ((err)||(!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", size);
  1313. for (i = 0; i < n; i++) {
  1314. res = sscanf(data[i], "%lx", &value);
  1315. if ((res != 1)||(!isxnumber(data[i]))) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]);
  1316. buf[i] = value;
  1317. }
  1318. err = pcilib_write_register_space(handle, bank, addr, n, buf);
  1319. if (err) Error("Error writting register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n);
  1320. err = pcilib_read_register_space(handle, bank, addr, n, check);
  1321. if (err) Error("Error reading register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n);
  1322. if (memcmp(buf, check, size)) {
  1323. printf("Write failed: the data written and read differ, the foolowing is read back:\n");
  1324. ReadRegisterRange(handle, model_info, bank, addr, addr_shift, n, NULL);
  1325. exit(-1);
  1326. }
  1327. free(check);
  1328. free(buf);
  1329. return 0;
  1330. }
  1331. int WriteRegister(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *bank, const char *reg, const char *view, const char *unit, char **data) {
  1332. int err = 0;
  1333. pcilib_value_t val = {0};
  1334. pcilib_register_value_t value, verify;
  1335. /*
  1336. pcilib_register_bank_t bank_id;
  1337. pcilib_register_bank_addr_t bank_addr;
  1338. bank_id = pcilib_find_bank_by_addr(handle, model_info->registers[regid].bank);
  1339. if (bank_id == PCILIB_REGISTER_BANK_INVALID) Error("Can't find bank of the register (%s)", reg);
  1340. format = model_info->banks[bank_id].format;
  1341. if (!format) format = "%lu";
  1342. */
  1343. err = pcilib_set_value_from_static_string(handle, &val, *data);
  1344. if (err) Error("Error (%i) setting value", err);
  1345. if (view) {
  1346. if (unit)
  1347. val.unit = unit;
  1348. if (reg) {
  1349. err = pcilib_write_register_view(handle, bank, reg, view, &val);
  1350. if (err) Error("Error writting view %s of register %s", view, reg);
  1351. printf("%s is written\n ", reg);
  1352. } else {
  1353. err = pcilib_set_property(handle, view, &val);
  1354. if (err) Error("Error setting property %s", view);
  1355. printf("%s is written\n ", view);
  1356. }
  1357. } else {
  1358. pcilib_register_t regid = pcilib_find_register(handle, bank, reg);
  1359. if (regid == PCILIB_REGISTER_INVALID) Error("Can't find register (%s) from bank (%s)", reg, bank?bank:"autodetected");
  1360. value = pcilib_get_value_as_register_value(handle, &val, &err);
  1361. if (err) Error("Error (%i) parsing data value (%s)", *data);
  1362. err = pcilib_write_register(handle, bank, reg, value);
  1363. if (err) Error("Error writting register %s\n", reg);
  1364. if ((model_info->registers[regid].mode&PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW) {
  1365. const char *format = (val.format?val.format:"%u");
  1366. err = pcilib_read_register(handle, bank, reg, &verify);
  1367. if (err) Error("Error reading back register %s for verification\n", reg);
  1368. if (verify != value) {
  1369. Error("Failed to write register %s: %lu is written and %lu is read back", reg, value, verify);
  1370. } else {
  1371. printf("%s = ", reg);
  1372. printf(format, verify);
  1373. printf("\n");
  1374. }
  1375. } else {
  1376. printf("%s is written\n ", reg);
  1377. }
  1378. }
  1379. return 0;
  1380. }
  1381. typedef struct {
  1382. pcilib_t *handle;
  1383. pcilib_event_t event;
  1384. pcilib_event_data_type_t data;
  1385. fastwriter_t *writer;
  1386. int verbose;
  1387. pcilib_timeout_t timeout;
  1388. size_t run_time;
  1389. size_t trigger_time;
  1390. size_t max_triggers;
  1391. pcilib_event_flags_t flags;
  1392. FORMAT format;
  1393. volatile int event_pending; /**< Used to detect that we have read previously triggered event */
  1394. volatile int trigger_thread_started; /**< Indicates that trigger thread is ready and we can't procced to start event recording */
  1395. volatile int started; /**< Indicates that recording is started */
  1396. volatile int run_flag;
  1397. volatile int writing_flag;
  1398. struct timeval first_frame;
  1399. struct timeval last_frame;
  1400. size_t last_num, last_id;
  1401. size_t trigger_failed;
  1402. size_t trigger_count;
  1403. size_t event_count; /**< Total number of events (including bad ones, but excluding events expected, but not reported by hardware) */
  1404. size_t incomplete_count; /**< Broken events, we even can't extract appropriate block of raw data */
  1405. size_t broken_count; /**< Broken events, error while decoding in the requested format */
  1406. size_t empty_count; /**< Broken events, no associated data or unknown */
  1407. size_t missing_count; /**< Missing events, not received from the hardware */
  1408. size_t dropped_count; /**< Missing events, dropped due slow decoding/copying performance */
  1409. size_t storage_count; /**< Missing events, dropped due to slowness of the storage subsystem */
  1410. struct timeval start_time;
  1411. struct timeval stop_time;
  1412. } GRABContext;
  1413. int GrabCallback(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user) {
  1414. int err = 0;
  1415. void *data;
  1416. size_t size;
  1417. GRABContext *ctx = (GRABContext*)user;
  1418. pcilib_t *handle = ctx->handle;
  1419. gettimeofday(&ctx->last_frame, NULL);
  1420. if (!ctx->event_count) {
  1421. memcpy(&ctx->first_frame, &ctx->last_frame, sizeof(struct timeval));
  1422. }
  1423. ctx->event_pending = 0;
  1424. ctx->event_count++;
  1425. if (ctx->last_num) {
  1426. size_t missing_count = (info->seqnum - ctx->last_num) - 1;
  1427. ctx->missing_count += missing_count;
  1428. #ifdef PCILIB_DEBUG_MISSING_EVENTS
  1429. if (missing_count)
  1430. pcilib_debug(MISSING_EVENTS, "%zu missing events between %zu (hwid: %zu) and %zu (hwid: %zu)", missing_count, ctx->last_id, ctx->last_num, event_id, info->seqnum);
  1431. #endif /* PCILIB_DEBUG_MISSING_EVENTS */
  1432. }
  1433. ctx->last_num = info->seqnum;
  1434. ctx->last_id = event_id;
  1435. if (info->flags&PCILIB_EVENT_INFO_FLAG_BROKEN) {
  1436. ctx->incomplete_count++;
  1437. return PCILIB_STREAMING_CONTINUE;
  1438. }
  1439. switch (ctx->format) {
  1440. case FORMAT_DEFAULT:
  1441. data = pcilib_get_data(handle, event_id, PCILIB_EVENT_DATA, &size);
  1442. break;
  1443. default:
  1444. data = pcilib_get_data(handle, event_id, PCILIB_EVENT_RAW_DATA, &size);
  1445. }
  1446. if (!data) {
  1447. int err = (int)size;
  1448. switch (err) {
  1449. case PCILIB_ERROR_OVERWRITTEN:
  1450. ctx->dropped_count++;
  1451. break;
  1452. case PCILIB_ERROR_INVALID_DATA:
  1453. ctx->broken_count++;
  1454. break;
  1455. default:
  1456. ctx->empty_count++;
  1457. }
  1458. return PCILIB_STREAMING_CONTINUE;
  1459. }
  1460. if (ctx->format == FORMAT_HEADER) {
  1461. uint64_t header[8];
  1462. header[0] = info->type;
  1463. header[1] = ctx->data;
  1464. header[2] = 0;
  1465. header[3] = size;
  1466. header[4] = info->seqnum;
  1467. header[5] = info->offset;
  1468. memcpy(header + 6, &info->timestamp, 16);
  1469. err = fastwriter_push(ctx->writer, 64, header);
  1470. }
  1471. if (!err)
  1472. err = fastwriter_push(ctx->writer, size, data);
  1473. if (err) {
  1474. fastwriter_cancel(ctx->writer);
  1475. if (err != EWOULDBLOCK)
  1476. Error("Storage error %i", err);
  1477. ctx->storage_count++;
  1478. pcilib_return_data(handle, event_id, ctx->data, data);
  1479. return PCILIB_STREAMING_CONTINUE;
  1480. }
  1481. err = pcilib_return_data(handle, event_id, ctx->data, data);
  1482. if (err) {
  1483. ctx->dropped_count++;
  1484. fastwriter_cancel(ctx->writer);
  1485. return PCILIB_STREAMING_CONTINUE;
  1486. }
  1487. err = fastwriter_commit(ctx->writer);
  1488. if (err) Error("Error commiting data to storage, Error: %i", err);
  1489. return PCILIB_STREAMING_CONTINUE;
  1490. }
  1491. 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) {
  1492. int err;
  1493. GRABContext *ctx = (GRABContext*)user;
  1494. // pcilib_t *handle = ctx->handle;
  1495. if ((info)&&(info->seqnum != ctx->last_num)) {
  1496. gettimeofday(&ctx->last_frame, NULL);
  1497. if (!ctx->event_count) {
  1498. memcpy(&ctx->first_frame, &ctx->last_frame, sizeof(struct timeval));
  1499. }
  1500. ctx->event_count++;
  1501. if (ctx->last_num) {
  1502. size_t missing_count = (info->seqnum - ctx->last_num) - 1;
  1503. ctx->missing_count += missing_count;
  1504. #ifdef PCILIB_DEBUG_MISSING_EVENTS
  1505. if (missing_count)
  1506. pcilib_debug(MISSING_EVENTS, "%zu missing events between %zu and %zu", missing_count, ctx->last_num, info->seqnum);
  1507. #endif /* PCILIB_DEBUG_MISSING_EVENTS */
  1508. }
  1509. ctx->last_num = info->seqnum;
  1510. }
  1511. err = fastwriter_push_data(ctx->writer, size, data);
  1512. if (err) {
  1513. if (err == EWOULDBLOCK) Error("Storage is not able to handle the data stream, buffer overrun");
  1514. Error("Storage error %i", err);
  1515. }
  1516. return PCILIB_STREAMING_CONTINUE;
  1517. }
  1518. void *Trigger(void *user) {
  1519. int err;
  1520. struct timeval start;
  1521. GRABContext *ctx = (GRABContext*)user;
  1522. size_t trigger_time = ctx->trigger_time;
  1523. size_t max_triggers = ctx->max_triggers;
  1524. ctx->trigger_thread_started = 1;
  1525. ctx->event_pending = 1;
  1526. while (!ctx->started) ;
  1527. gettimeofday(&start, NULL);
  1528. do {
  1529. err = pcilib_trigger(ctx->handle, ctx->event, 0, NULL);
  1530. if (err) ctx->trigger_failed++;
  1531. if ((++ctx->trigger_count == max_triggers)&&(max_triggers)) break;
  1532. if (trigger_time) {
  1533. pcilib_add_timeout(&start, trigger_time);
  1534. if ((ctx->stop_time.tv_sec)&&(pcilib_timecmp(&start, &ctx->stop_time)>0)) break;
  1535. pcilib_sleep_until_deadline(&start);
  1536. } else {
  1537. while ((ctx->event_pending)&&(ctx->run_flag)) usleep(10);
  1538. ctx->event_pending = 1;
  1539. }
  1540. } while (ctx->run_flag);
  1541. ctx->trigger_thread_started = 0;
  1542. return NULL;
  1543. }
  1544. void GrabStats(GRABContext *ctx, struct timeval *end_time) {
  1545. int verbose;
  1546. pcilib_timeout_t duration, fps_duration;
  1547. struct timeval cur;
  1548. double fps = 0, good_fps = 0;
  1549. size_t total, good, pending = 0;
  1550. verbose = ctx->verbose;
  1551. if (end_time) {
  1552. if (verbose++) {
  1553. printf("-------------------------------------------------------------------------------\n");
  1554. }
  1555. } else {
  1556. gettimeofday(&cur, NULL);
  1557. end_time = &cur;
  1558. }
  1559. // if ((ctx->event_count + ctx->missing_count) == 0)
  1560. // return;
  1561. duration = pcilib_timediff(&ctx->start_time, end_time);
  1562. fps_duration = pcilib_timediff(&ctx->first_frame, &ctx->last_frame);
  1563. if (ctx->trigger_count) {
  1564. total = ctx->trigger_count;
  1565. pending = ctx->trigger_count - ctx->event_count - ctx->missing_count - ctx->trigger_failed;
  1566. } else {
  1567. total = ctx->event_count + ctx->missing_count;
  1568. }
  1569. good = ctx->event_count - ctx->broken_count - ctx->incomplete_count - ctx->storage_count - ctx->empty_count - ctx->dropped_count;
  1570. if (ctx->event_count > 1) {
  1571. fps = (ctx->event_count - 1) / (1.*fps_duration/1000000);
  1572. }
  1573. if (good > 1) {
  1574. good_fps = (good - 1) / (1.*fps_duration/1000000);
  1575. }
  1576. printf("Run: ");
  1577. PrintTime(duration);
  1578. if (ctx->trigger_count) {
  1579. printf(", Triggers: ");
  1580. PrintNumber(ctx->trigger_count);
  1581. }
  1582. printf(", Captured: ");
  1583. PrintNumber(ctx->event_count);
  1584. printf(" FPS %5.0lf", fps);
  1585. if ((ctx->flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY) == 0) {
  1586. printf(", Stored: ");
  1587. PrintNumber(good);
  1588. printf(" FPS %5.0lf", good_fps);
  1589. }
  1590. printf("\n");
  1591. if (verbose > 2) {
  1592. if (ctx->trigger_count) {
  1593. printf("Trig: ");
  1594. PrintNumber(ctx->trigger_count);
  1595. printf(" Issued: ");
  1596. PrintNumber(ctx->trigger_count - ctx->trigger_failed);
  1597. printf(" (");
  1598. PrintPercent(ctx->trigger_count - ctx->trigger_failed, ctx->trigger_count);
  1599. printf("%%) Failed: ");
  1600. PrintNumber(ctx->trigger_failed);
  1601. printf( " (");
  1602. PrintPercent(ctx->trigger_failed, ctx->trigger_count);
  1603. printf( "%%); Pending: ");
  1604. PrintNumber(pending);
  1605. printf( " (");
  1606. PrintPercent(pending, ctx->trigger_count);
  1607. printf( "%%)\n");
  1608. }
  1609. if (ctx->flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY) {
  1610. printf("Captured: ");
  1611. PrintNumber(good);
  1612. } else {
  1613. printf("Good: ");
  1614. PrintNumber(good);
  1615. printf(", Dropped: ");
  1616. PrintNumber(ctx->dropped_count + ctx->storage_count);
  1617. printf(", Bad: ");
  1618. PrintNumber(ctx->incomplete_count + ctx->broken_count);
  1619. printf(", Empty: ");
  1620. PrintNumber(ctx->empty_count);
  1621. }
  1622. printf(", Lost: ");
  1623. PrintNumber(ctx->missing_count);
  1624. printf("\n");
  1625. }
  1626. if (verbose > 1) {
  1627. if (ctx->flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY) {
  1628. printf("Captured: ");
  1629. PrintPercent(good, total);
  1630. } else {
  1631. printf("Good: ");
  1632. PrintPercent(good, total);
  1633. printf("%% Dropped: ");
  1634. PrintPercent(ctx->dropped_count + ctx->storage_count, total);
  1635. printf("%% Bad: ");
  1636. PrintPercent(ctx->incomplete_count + ctx->broken_count, total);
  1637. printf("%% Empty: ");
  1638. PrintPercent(ctx->empty_count, total);
  1639. }
  1640. printf("%% Lost: ");
  1641. PrintPercent(ctx->missing_count, total);
  1642. printf("%%");
  1643. printf("\n");
  1644. }
  1645. }
  1646. void StorageStats(GRABContext *ctx) {
  1647. int err;
  1648. fastwriter_stats_t st;
  1649. pcilib_timeout_t duration;
  1650. struct timeval cur;
  1651. gettimeofday(&cur, NULL);
  1652. duration = pcilib_timediff(&ctx->start_time, &cur);
  1653. err = fastwriter_get_stats(ctx->writer, &st);
  1654. if (err) return;
  1655. printf("Wrote ");
  1656. PrintSize(st.written);
  1657. printf(" of ");
  1658. PrintSize(st.commited);
  1659. printf(" at ");
  1660. PrintSize(1000000.*st.written / duration);
  1661. printf("/s, %6.2lf%% ", 100.*st.buffer_used / st.buffer_size);
  1662. printf(" of ");
  1663. PrintSize(st.buffer_size);
  1664. printf(" buffer (%6.2lf%% max)\n", 100.*st.buffer_max / st.buffer_size);
  1665. }
  1666. void *Monitor(void *user) {
  1667. struct timeval deadline;
  1668. struct timeval nextinfo;
  1669. GRABContext *ctx = (GRABContext*)user;
  1670. int verbose = ctx->verbose;
  1671. pcilib_timeout_t timeout = ctx->timeout;
  1672. if (timeout == PCILIB_TIMEOUT_INFINITE) timeout = 0;
  1673. // while (!ctx->started);
  1674. if (timeout) {
  1675. memcpy(&deadline, (struct timeval*)&ctx->last_frame, sizeof(struct timeval));
  1676. pcilib_add_timeout(&deadline, timeout);
  1677. }
  1678. if (verbose > 0) {
  1679. pcilib_calc_deadline(&nextinfo, STATUS_MESSAGE_INTERVAL*1000000);
  1680. }
  1681. while (ctx->run_flag) {
  1682. if (StopFlag) {
  1683. pcilib_stop(ctx->handle, PCILIB_EVENT_FLAG_STOP_ONLY);
  1684. break;
  1685. }
  1686. if (timeout) {
  1687. if (pcilib_calc_time_to_deadline(&deadline) == 0) {
  1688. memcpy(&deadline, (struct timeval*)&ctx->last_frame, sizeof(struct timeval));
  1689. pcilib_add_timeout(&deadline, timeout);
  1690. if (pcilib_calc_time_to_deadline(&deadline) == 0) {
  1691. pcilib_stop(ctx->handle, PCILIB_EVENT_FLAG_STOP_ONLY);
  1692. break;
  1693. }
  1694. }
  1695. }
  1696. if (verbose > 0) {
  1697. if (pcilib_calc_time_to_deadline(&nextinfo) == 0) {
  1698. GrabStats(ctx, NULL);
  1699. StorageStats(ctx);
  1700. pcilib_calc_deadline(&nextinfo, STATUS_MESSAGE_INTERVAL*1000000);
  1701. }
  1702. }
  1703. usleep(100000);
  1704. }
  1705. pcilib_calc_deadline(&nextinfo, STATUS_MESSAGE_INTERVAL*1000000);
  1706. while (ctx->writing_flag) {
  1707. if (pcilib_calc_time_to_deadline(&nextinfo) == 0) {
  1708. if (verbose >= 0) StorageStats(ctx);
  1709. pcilib_calc_deadline(&nextinfo, STATUS_MESSAGE_INTERVAL*1000000);
  1710. }
  1711. usleep(100000);
  1712. }
  1713. return NULL;
  1714. }
  1715. int TriggerAndGrab(pcilib_t *handle, GRAB_MODE grab_mode, const char *evname, const char *data_type, size_t num, size_t run_time, size_t trigger_time, pcilib_timeout_t timeout, PARTITION partition, FORMAT format, size_t buffer_size, size_t threads, int verbose, const char *output) {
  1716. int err;
  1717. GRABContext ctx;
  1718. // void *data = NULL;
  1719. // size_t size, written;
  1720. pcilib_event_t event;
  1721. pcilib_event_t listen_events;
  1722. pcilib_event_data_type_t data;
  1723. pthread_t monitor_thread;
  1724. pthread_t trigger_thread;
  1725. pthread_attr_t attr;
  1726. struct sched_param sched;
  1727. struct timeval end_time;
  1728. pcilib_event_flags_t flags;
  1729. if (evname) {
  1730. event = pcilib_find_event(handle, evname);
  1731. if (event == PCILIB_EVENT_INVALID)
  1732. Error("Can't find event (%s)", evname);
  1733. listen_events = event;
  1734. } else {
  1735. listen_events = PCILIB_EVENTS_ALL;
  1736. event = PCILIB_EVENT0;
  1737. }
  1738. if (data_type) {
  1739. data = pcilib_find_event_data_type(handle, event, data_type);
  1740. if (data == PCILIB_EVENT_DATA_TYPE_INVALID)
  1741. Error("Can't find data type (%s)", data_type);
  1742. } else {
  1743. data = PCILIB_EVENT_DATA;
  1744. }
  1745. memset(&ctx, 0, sizeof(GRABContext));
  1746. ctx.handle = handle;
  1747. ctx.event = event;
  1748. ctx.data = data;
  1749. ctx.run_time = run_time;
  1750. ctx.timeout = timeout;
  1751. ctx.format = format;
  1752. if (grab_mode&GRAB_MODE_GRAB) ctx.verbose = verbose;
  1753. else ctx.verbose = 0;
  1754. if (grab_mode&GRAB_MODE_GRAB) {
  1755. ctx.writer = fastwriter_init(output, 0);
  1756. if (!ctx.writer)
  1757. Error("Can't initialize fastwritter library");
  1758. fastwriter_set_buffer_size(ctx.writer, buffer_size);
  1759. err = fastwriter_open(ctx.writer, output, 0);
  1760. if (err)
  1761. Error("Error opening file (%s), Error: %i\n", output, err);
  1762. ctx.writing_flag = 1;
  1763. }
  1764. ctx.run_flag = 1;
  1765. flags = PCILIB_EVENT_FLAGS_DEFAULT;
  1766. if (data == PCILIB_EVENT_RAW_DATA) {
  1767. if (format == FORMAT_RAW) {
  1768. flags |= PCILIB_EVENT_FLAG_RAW_DATA_ONLY;
  1769. }
  1770. } else {
  1771. flags |= PCILIB_EVENT_FLAG_PREPROCESS;
  1772. }
  1773. ctx.flags = flags;
  1774. // printf("Limits: %lu %lu %lu\n", num, run_time, timeout);
  1775. pcilib_configure_autostop(handle, num, run_time);
  1776. if (flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY) {
  1777. pcilib_configure_rawdata_callback(handle, &raw_data, &ctx);
  1778. }
  1779. if (flags&PCILIB_EVENT_FLAG_PREPROCESS) {
  1780. pcilib_configure_preprocessing_threads(handle, threads);
  1781. }
  1782. if (grab_mode&GRAB_MODE_TRIGGER) {
  1783. if (trigger_time) {
  1784. if ((timeout)&&(trigger_time * 2 > timeout)) {
  1785. timeout = 2 * trigger_time;
  1786. ctx.timeout = timeout;
  1787. }
  1788. } else {
  1789. // Otherwise, we will trigger next event after previous one is read
  1790. if (((grab_mode&GRAB_MODE_GRAB) == 0)||(flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY)) trigger_time = PCILIB_TRIGGER_TIMEOUT;
  1791. }
  1792. ctx.max_triggers = num;
  1793. ctx.trigger_count = 0;
  1794. ctx.trigger_time = trigger_time;
  1795. // We don't really care if RT priority is imposible
  1796. pthread_attr_init(&attr);
  1797. if (!pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) {
  1798. sched.sched_priority = sched_get_priority_min(SCHED_FIFO);
  1799. pthread_attr_setschedparam(&attr, &sched);
  1800. }
  1801. // Start triggering thread and wait until it is schedulled
  1802. if (pthread_create(&trigger_thread, &attr, Trigger, (void*)&ctx))
  1803. Error("Error spawning trigger thread");
  1804. while (!ctx.trigger_thread_started) usleep(10);
  1805. }
  1806. gettimeofday(&ctx.start_time, NULL);
  1807. if (grab_mode&GRAB_MODE_GRAB) {
  1808. err = pcilib_start(handle, listen_events, flags);
  1809. if (err) Error("Failed to start event engine, error %i", err);
  1810. }
  1811. ctx.started = 1;
  1812. if (run_time) {
  1813. ctx.stop_time.tv_usec = ctx.start_time.tv_usec + run_time%1000000;
  1814. if (ctx.stop_time.tv_usec > 999999) {
  1815. ctx.stop_time.tv_usec -= 1000000;
  1816. __sync_synchronize();
  1817. ctx.stop_time.tv_sec = ctx.start_time.tv_sec + 1 + run_time / 1000000;
  1818. } else {
  1819. __sync_synchronize();
  1820. ctx.stop_time.tv_sec = ctx.start_time.tv_sec + run_time / 1000000;
  1821. }
  1822. }
  1823. memcpy(&ctx.last_frame, &ctx.start_time, sizeof(struct timeval));
  1824. if (pthread_create(&monitor_thread, NULL, Monitor, (void*)&ctx))
  1825. Error("Error spawning monitoring thread");
  1826. if (grab_mode&GRAB_MODE_GRAB) {
  1827. err = pcilib_stream(handle, &GrabCallback, &ctx);
  1828. if (err) Error("Error streaming events, error %i", err);
  1829. }
  1830. ctx.run_flag = 0;
  1831. if (grab_mode&GRAB_MODE_TRIGGER) {
  1832. while (ctx.trigger_thread_started) usleep(10);
  1833. }
  1834. if (grab_mode&GRAB_MODE_GRAB) {
  1835. pcilib_stop(handle, PCILIB_EVENT_FLAGS_DEFAULT);
  1836. }
  1837. gettimeofday(&end_time, NULL);
  1838. if (grab_mode&GRAB_MODE_TRIGGER) {
  1839. pthread_join(trigger_thread, NULL);
  1840. }
  1841. if (grab_mode&GRAB_MODE_GRAB) {
  1842. if (verbose >= 0)
  1843. printf("Grabbing is finished, flushing results....\n");
  1844. err = fastwriter_close(ctx.writer);
  1845. if (err) Error("Storage problems, error %i", err);
  1846. }
  1847. ctx.writing_flag = 0;
  1848. pthread_join(monitor_thread, NULL);
  1849. if ((grab_mode&GRAB_MODE_GRAB)&&(verbose>=0)) {
  1850. GrabStats(&ctx, &end_time);
  1851. StorageStats(&ctx);
  1852. }
  1853. fastwriter_destroy(ctx.writer);
  1854. return 0;
  1855. }
  1856. int StartStopDMA(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_dma_engine_addr_t dma, pcilib_dma_direction_t dma_direction, int start) {
  1857. int err;
  1858. pcilib_dma_engine_t dmaid;
  1859. if (dma == PCILIB_DMA_ENGINE_ADDR_INVALID) {
  1860. const pcilib_dma_description_t *dma_info = pcilib_get_dma_description(handle);
  1861. if (start) Error("DMA engine should be specified");
  1862. for (dmaid = 0; dma_info->engines[dmaid].addr_bits; dmaid++) {
  1863. err = pcilib_start_dma(handle, dmaid, 0);
  1864. if (err) Error("Error starting DMA Engine (%s %i)", ((dma_info->engines[dmaid].direction == PCILIB_DMA_FROM_DEVICE)?"C2S":"S2C"), dma_info->engines[dmaid].addr);
  1865. err = pcilib_stop_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
  1866. if (err) Error("Error stopping DMA Engine (%s %i)", ((dma_info->engines[dmaid].direction == PCILIB_DMA_FROM_DEVICE)?"C2S":"S2C"), dma_info->engines[dmaid].addr);
  1867. }
  1868. return 0;
  1869. }
  1870. if (dma_direction&PCILIB_DMA_FROM_DEVICE) {
  1871. dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_FROM_DEVICE, dma);
  1872. if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (C2S %lu) is specified", dma);
  1873. if (start) {
  1874. err = pcilib_start_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
  1875. if (err) Error("Error starting DMA engine (C2S %lu)", dma);
  1876. } else {
  1877. err = pcilib_start_dma(handle, dmaid, 0);
  1878. if (err) Error("Error starting DMA engine (C2S %lu)", dma);
  1879. err = pcilib_stop_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
  1880. if (err) Error("Error stopping DMA engine (C2S %lu)", dma);
  1881. }
  1882. }
  1883. if (dma_direction&PCILIB_DMA_TO_DEVICE) {
  1884. dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_TO_DEVICE, dma);
  1885. if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (S2C %lu) is specified", dma);
  1886. if (start) {
  1887. err = pcilib_start_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
  1888. if (err) Error("Error starting DMA engine (S2C %lu)", dma);
  1889. } else {
  1890. err = pcilib_start_dma(handle, dmaid, 0);
  1891. if (err) Error("Error starting DMA engine (S2C %lu)", dma);
  1892. err = pcilib_stop_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
  1893. if (err) Error("Error stopping DMA engine (S2C %lu)", dma);
  1894. }
  1895. }
  1896. return 0;
  1897. }
  1898. typedef struct {
  1899. pcilib_kmem_use_t use;
  1900. int referenced;
  1901. int hw_lock;
  1902. int reusable;
  1903. int persistent;
  1904. int open;
  1905. size_t count;
  1906. size_t size;
  1907. } kmem_use_info_t;
  1908. #define MAX_USES 64
  1909. pcilib_kmem_use_t ParseUse(const char *use) {
  1910. unsigned long utmp;
  1911. if (use) {
  1912. if ((!isxnumber(use))||(sscanf(use, "%lx", &utmp) != 1)) Error("Invalid use (%s) is specified", use);
  1913. if (strlen(use) < 5)
  1914. return PCILIB_KMEM_USE(PCILIB_KMEM_USE_USER,utmp);
  1915. else
  1916. return utmp;
  1917. }
  1918. Error("Kernel memory use is not specified");
  1919. return 0;
  1920. }
  1921. size_t FindUse(size_t *n_uses, kmem_use_info_t *uses, pcilib_kmem_use_t use) {
  1922. size_t i, n = *n_uses;
  1923. if (uses[n - 1].use == use) return n - 1;
  1924. for (i = 1; i < (n - 1); i++) {
  1925. if (uses[i].use == use) return i;
  1926. }
  1927. if (n == MAX_USES) return 0;
  1928. memset(&uses[n], 0, sizeof(kmem_use_info_t));
  1929. uses[n].use = use;
  1930. return (*n_uses)++;
  1931. }
  1932. kmem_use_info_t *GetUse(size_t n_uses, kmem_use_info_t *uses, pcilib_kmem_use_t use) {
  1933. size_t i;
  1934. for (i = 0; i < n_uses; i++) {
  1935. if (uses[i].use == use) {
  1936. if (uses[i].count) return uses + i;
  1937. else return NULL;
  1938. }
  1939. }
  1940. return NULL;
  1941. }
  1942. int ParseKMEM(pcilib_t *handle, const char *device, size_t *uses_number, kmem_use_info_t *uses) {
  1943. DIR *dir;
  1944. struct dirent *entry;
  1945. const char *pos;
  1946. char sysdir[256];
  1947. char fname[256];
  1948. char info[256];
  1949. size_t useid, n_uses = 1; // Use 0 is for others
  1950. memset(uses, 0, sizeof(kmem_use_info_t));
  1951. pos = strrchr(device, '/');
  1952. if (pos) ++pos;
  1953. else pos = device;
  1954. snprintf(sysdir, 255, "/sys/class/fpga/%s", pos);
  1955. dir = opendir(sysdir);
  1956. if (!dir) Error("Can't open directory (%s)", sysdir);
  1957. while ((entry = readdir(dir)) != NULL) {
  1958. FILE *f;
  1959. unsigned long use = 0;
  1960. unsigned long size = 0;
  1961. unsigned long refs = 0;
  1962. unsigned long mode = 0;
  1963. unsigned long hwref = 0;
  1964. if (strncmp(entry->d_name, "kbuf", 4)) continue;
  1965. if (!isnumber(entry->d_name+4)) continue;
  1966. snprintf(fname, 255, "%s/%s", sysdir, entry->d_name);
  1967. f = fopen(fname, "r");
  1968. if (!f) Error("Can't access file (%s)", fname);
  1969. while(!feof(f)) {
  1970. if (!fgets(info, 256, f))
  1971. break;
  1972. if (!strncmp(info, "use:", 4)) use = strtoul(info+4, NULL, 16);
  1973. if (!strncmp(info, "size:", 5)) size = strtoul(info+5, NULL, 10);
  1974. if (!strncmp(info, "refs:", 5)) refs = strtoul(info+5, NULL, 10);
  1975. if (!strncmp(info, "mode:", 5)) mode = strtoul(info+5, NULL, 16);
  1976. if (!strncmp(info, "hw ref:", 7)) hwref = strtoul(info+7, NULL, 10);
  1977. }
  1978. fclose(f);
  1979. useid = FindUse(&n_uses, uses, use);
  1980. uses[useid].count++;
  1981. uses[useid].size += size;
  1982. if (refs) uses[useid].referenced = 1;
  1983. if (hwref) uses[useid].hw_lock = 1;
  1984. if (mode&KMEM_MODE_REUSABLE) uses[useid].reusable = 1;
  1985. if (mode&KMEM_MODE_PERSISTENT) uses[useid].persistent = 1;
  1986. if (mode&KMEM_MODE_COUNT) uses[useid].open = 1;
  1987. }
  1988. closedir(dir);
  1989. *uses_number = n_uses;
  1990. return 0;
  1991. }
  1992. int ListKMEM(pcilib_t *handle, const char *device) {
  1993. int err;
  1994. char stmp[256];
  1995. size_t i, useid, n_uses;
  1996. kmem_use_info_t uses[MAX_USES];
  1997. const pcilib_model_description_t *model_info = pcilib_get_model_description(handle);
  1998. err = ParseKMEM(handle, device, &n_uses, uses);
  1999. if (err) Error("Failed to parse kernel memory information provided through sysfs");
  2000. if ((n_uses == 1)&&(uses[0].count == 0)) {
  2001. printf("No kernel memory is allocated\n");
  2002. return 0;
  2003. }
  2004. printf("Use Type Count Total Size REF Mode \n");
  2005. printf("--------------------------------------------------------------------------------\n");
  2006. for (useid = 0; useid < n_uses; useid++) {
  2007. if (useid + 1 == n_uses) {
  2008. if (!uses[0].count) continue;
  2009. i = 0;
  2010. } else i = useid + 1;
  2011. printf("%08x ", uses[i].use);
  2012. if (i) {
  2013. switch(PCILIB_KMEM_USE_TYPE(uses[i].use)) {
  2014. case PCILIB_KMEM_USE_DMA_RING:
  2015. printf("DMA%u %s Ring ", uses[i].use&0x7F, ((uses[i].use&0x80)?"S2C":"C2S"));
  2016. break;
  2017. case PCILIB_KMEM_USE_DMA_PAGES:
  2018. printf("DMA%u %s Pages ", uses[i].use&0x7F, ((uses[i].use&0x80)?"S2C":"C2S"));
  2019. break;
  2020. case PCILIB_KMEM_USE_SOFTWARE_REGISTERS: {
  2021. pcilib_register_bank_t bank = pcilib_find_register_bank_by_addr(handle, PCILIB_KMEM_USE_SUBTYPE(uses[i].use));
  2022. if (bank == PCILIB_REGISTER_BANK_INVALID)
  2023. printf("SoftRegs (%8u)", PCILIB_KMEM_USE_SUBTYPE(uses[i].use));
  2024. else
  2025. printf("SoftRegs (%8s)", model_info->banks[bank].name);
  2026. break;
  2027. }
  2028. case PCILIB_KMEM_USE_LOCKS:
  2029. printf("Locks ");
  2030. break;
  2031. case PCILIB_KMEM_USE_USER:
  2032. printf("User %04x ", uses[i].use&0xFFFF);
  2033. break;
  2034. default:
  2035. printf (" ");
  2036. }
  2037. } else printf("All Others ");
  2038. printf(" ");
  2039. printf("%6zu", uses[i].count);
  2040. printf(" ");
  2041. printf("%10s", GetPrintSize(stmp, uses[i].size));
  2042. printf(" ");
  2043. if ((uses[i].referenced)&&(uses[i].hw_lock)) printf("HW+SW");
  2044. else if (uses[i].referenced) printf(" SW");
  2045. else if (uses[i].hw_lock) printf("HW ");
  2046. else printf(" - ");
  2047. printf(" ");
  2048. if (uses[i].persistent) printf("Persistent");
  2049. else if (uses[i].open) printf("Open ");
  2050. else if (uses[i].reusable) printf("Reusable ");
  2051. else printf("Closed ");
  2052. printf("\n");
  2053. }
  2054. printf("--------------------------------------------------------------------------------\n");
  2055. printf("REF - Software/Hardware Reference, MODE - Reusable/Persistent/Open\n");
  2056. return 0;
  2057. }
  2058. int DetailKMEM(pcilib_t *handle, const char *device, const char *use, size_t block) {
  2059. int err;
  2060. size_t i, n;
  2061. pcilib_kmem_handle_t *kbuf;
  2062. pcilib_kmem_use_t useid = ParseUse(use);
  2063. size_t n_uses;
  2064. kmem_use_info_t uses[MAX_USES];
  2065. kmem_use_info_t *use_info;
  2066. if (block == (size_t)-1) {
  2067. err = ParseKMEM(handle, device, &n_uses, uses);
  2068. if (err) Error("Failed to parse kernel memory information provided through sysfs");
  2069. use_info = GetUse(n_uses, uses, useid);
  2070. if (!use_info) Error("No kernel buffers is allocated for the specified use (%lx)", useid);
  2071. i = 0;
  2072. n = use_info->count;
  2073. } else {
  2074. i = block;
  2075. n = block + 1;
  2076. }
  2077. kbuf = pcilib_alloc_kernel_memory(handle, 0, n, 0, 0, useid, PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_TRY);
  2078. if (!kbuf) {
  2079. Error("Allocation of kernel buffer (use %lx, count %lu) is failed\n", useid, n);
  2080. return 0;
  2081. }
  2082. printf("Buffer Address Hardware Address Bus Address\n");
  2083. printf("--------------------------------------------------------------------------------\n");
  2084. for (; i < n; i++) {
  2085. void *data = pcilib_kmem_get_block_ua(handle, kbuf, i);
  2086. uintptr_t pa = pcilib_kmem_get_block_pa(handle, kbuf, i);
  2087. uintptr_t ba = pcilib_kmem_get_block_ba(handle, kbuf, i);
  2088. printf("%6lu %16p %16lx %16lx\n", i, data, pa, ba);
  2089. }
  2090. printf("\n");
  2091. pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
  2092. return 0;
  2093. }
  2094. int ReadKMEM(pcilib_t *handle, const char *device, pcilib_kmem_use_t useid, size_t block, size_t max_size, FILE *o) {
  2095. int err;
  2096. void *data;
  2097. size_t size;
  2098. pcilib_kmem_handle_t *kbuf;
  2099. if (block == (size_t)-1) block = 0;
  2100. kbuf = pcilib_alloc_kernel_memory(handle, 0, block + 1, 0, 0, useid, PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_TRY);
  2101. if (!kbuf) {
  2102. Error("The specified kernel buffer is not allocated\n");
  2103. return 0;
  2104. }
  2105. err = pcilib_kmem_sync_block(handle, kbuf, PCILIB_KMEM_SYNC_FROMDEVICE, block);
  2106. if (err) {
  2107. pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
  2108. Error("The synchronization of kernel buffer has failed\n");
  2109. return 0;
  2110. }
  2111. data = pcilib_kmem_get_block_ua(handle, kbuf, block);
  2112. if (data) {
  2113. size = pcilib_kmem_get_block_size(handle, kbuf, block);
  2114. if ((max_size)&&(size > max_size)) size = max_size;
  2115. fwrite(data, 1, size, o?o:stdout);
  2116. } else {
  2117. pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
  2118. Error("The specified block is not existing\n");
  2119. return 0;
  2120. }
  2121. pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
  2122. return 0;
  2123. }
  2124. int AllocKMEM(pcilib_t *handle, const char *device, const char *use, const char *type, size_t size, size_t block_size, size_t alignment) {
  2125. pcilib_kmem_type_t ktype = PCILIB_KMEM_TYPE_PAGE;
  2126. pcilib_kmem_flags_t flags = KMEM_FLAG_REUSE;
  2127. pcilib_kmem_handle_t *kbuf;
  2128. pcilib_kmem_use_t useid = ParseUse(use);
  2129. long page_size = sysconf(_SC_PAGESIZE);
  2130. if (type) {
  2131. if (!strcmp(type, "consistent")) ktype = PCILIB_KMEM_TYPE_CONSISTENT;
  2132. else if (!strcmp(type, "c2s")) ktype = PCILIB_KMEM_TYPE_DMA_C2S_PAGE;
  2133. else if (!strcmp(type, "s2c")) ktype = PCILIB_KMEM_TYPE_DMA_S2C_PAGE;
  2134. else Error("Invalid memory type (%s) is specified", type);
  2135. }
  2136. if ((block_size)&&(ktype != PCILIB_KMEM_TYPE_CONSISTENT))
  2137. Error("Selected memory type does not allow custom size");
  2138. kbuf = pcilib_alloc_kernel_memory(handle, ktype, size, (block_size?block_size:page_size), (alignment?alignment:page_size), useid, flags|KMEM_FLAG_PERSISTENT);
  2139. if (!kbuf) Error("Allocation of kernel memory has failed");
  2140. pcilib_free_kernel_memory(handle, kbuf, flags);
  2141. return 0;
  2142. }
  2143. int FreeKMEM(pcilib_t *handle, const char *device, const char *use, int force) {
  2144. int err;
  2145. int i;
  2146. pcilib_kmem_use_t useid;
  2147. pcilib_kmem_flags_t flags = PCILIB_KMEM_FLAG_HARDWARE|PCILIB_KMEM_FLAG_PERSISTENT|PCILIB_KMEM_FLAG_EXCLUSIVE;
  2148. if (force) flags |= PCILIB_KMEM_FLAG_FORCE; // this will ignore mmap locks as well.
  2149. if (!strcasecmp(use, "dma")) {
  2150. for (i = 0; i < PCILIB_MAX_DMA_ENGINES; i++) {
  2151. err = pcilib_clean_kernel_memory(handle, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_RING, i), flags);
  2152. if (err) Error("Error cleaning DMA%i C2S Ring buffer", i);
  2153. err = pcilib_clean_kernel_memory(handle, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_RING, 0x80|i), flags);
  2154. if (err) Error("Error cleaning DMA%i S2C Ring buffer", i);
  2155. err = pcilib_clean_kernel_memory(handle, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_PAGES, i), flags);
  2156. if (err) Error("Error cleaning DMA%i C2S Page buffers", i);
  2157. err = pcilib_clean_kernel_memory(handle, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_PAGES, 0x80|i), flags);
  2158. if (err) Error("Error cleaning DMA%i S2C Page buffers", i);
  2159. }
  2160. return 0;
  2161. }
  2162. useid = ParseUse(use);
  2163. err = pcilib_clean_kernel_memory(handle, useid, flags);
  2164. if (err) Error("Error cleaning kernel buffers for use (0x%lx)", useid);
  2165. return 0;
  2166. }
  2167. int ListDMA(pcilib_t *handle, const char *device, const pcilib_model_description_t *model_info) {
  2168. int err;
  2169. DIR *dir;
  2170. struct dirent *entry;
  2171. const char *pos;
  2172. char sysdir[256];
  2173. char fname[256];
  2174. char info[256];
  2175. char stmp[256];
  2176. pcilib_dma_engine_t dmaid;
  2177. pcilib_dma_engine_status_t status;
  2178. pos = strrchr(device, '/');
  2179. if (pos) ++pos;
  2180. else pos = device;
  2181. snprintf(sysdir, 255, "/sys/class/fpga/%s", pos);
  2182. dir = opendir(sysdir);
  2183. if (!dir) Error("Can't open directory (%s)", sysdir);
  2184. printf("DMA Engine Status Total Size Buffer Ring (1st used - 1st free)\n");
  2185. printf("--------------------------------------------------------------------------------\n");
  2186. while ((entry = readdir(dir)) != NULL) {
  2187. FILE *f;
  2188. unsigned long use = 0;
  2189. // unsigned long size = 0;
  2190. // unsigned long refs = 0;
  2191. unsigned long mode = 0;
  2192. // unsigned long hwref = 0;
  2193. if (strncmp(entry->d_name, "kbuf", 4)) continue;
  2194. if (!isnumber(entry->d_name+4)) continue;
  2195. snprintf(fname, 255, "%s/%s", sysdir, entry->d_name);
  2196. f = fopen(fname, "r");
  2197. if (!f) Error("Can't access file (%s)", fname);
  2198. while(!feof(f)) {
  2199. if (!fgets(info, 256, f))
  2200. break;
  2201. if (!strncmp(info, "use:", 4)) use = strtoul(info+4, NULL, 16);
  2202. // if (!strncmp(info, "size:", 5)) size = strtoul(info+5, NULL, 10);
  2203. // if (!strncmp(info, "refs:", 5)) refs = strtoul(info+5, NULL, 10);
  2204. if (!strncmp(info, "mode:", 5)) mode = strtoul(info+5, NULL, 16);
  2205. // if (!strncmp(info, "hw ref:", 7)) hwref = strtoul(info+7, NULL, 10);
  2206. }
  2207. fclose(f);
  2208. if ((mode&(KMEM_MODE_REUSABLE|KMEM_MODE_PERSISTENT|KMEM_MODE_COUNT)) == 0) continue; // closed
  2209. if ((use >> 16) != PCILIB_KMEM_USE_DMA_RING) continue;
  2210. if (use&0x80) {
  2211. dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_TO_DEVICE, use&0x7F);
  2212. } else {
  2213. dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_FROM_DEVICE, use&0x7F);
  2214. }
  2215. if (dmaid == PCILIB_DMA_ENGINE_INVALID) continue;
  2216. printf("DMA%lu %s ", use&0x7F, (use&0x80)?"S2C":"C2S");
  2217. err = pcilib_start_dma(handle, dmaid, 0);
  2218. if (err) {
  2219. printf("-- Wrong state, start is failed\n");
  2220. continue;
  2221. }
  2222. err = pcilib_get_dma_status(handle, dmaid, &status, 0, NULL);
  2223. if (err) {
  2224. printf("-- Wrong state, failed to obtain status\n");
  2225. pcilib_stop_dma(handle, dmaid, 0);
  2226. continue;
  2227. }
  2228. pcilib_stop_dma(handle, dmaid, 0);
  2229. if (status.started) printf("S");
  2230. else printf(" ");
  2231. if (status.ring_head == status.ring_tail) printf(" ");
  2232. else printf("D");
  2233. printf(" ");
  2234. printf("%10s", GetPrintSize(stmp, status.ring_size * status.buffer_size));
  2235. printf(" ");
  2236. printf("%zu - %zu (of %zu)", status.ring_tail, status.ring_head, status.ring_size);
  2237. printf("\n");
  2238. }
  2239. closedir(dir);
  2240. printf("--------------------------------------------------------------------------------\n");
  2241. printf("S - Started, D - Data in buffers\n");
  2242. return 0;
  2243. }
  2244. int ListBuffers(pcilib_t *handle, const char *device, const pcilib_model_description_t *model_info, pcilib_dma_engine_addr_t dma, pcilib_dma_direction_t dma_direction) {
  2245. int err;
  2246. size_t i;
  2247. pcilib_dma_engine_t dmaid;
  2248. pcilib_dma_engine_status_t status;
  2249. pcilib_dma_buffer_status_t *buffer;
  2250. char stmp[256];
  2251. dmaid = pcilib_find_dma_by_addr(handle, dma_direction, dma);
  2252. if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("The specified DMA engine is not found");
  2253. err = pcilib_start_dma(handle, dmaid, 0);
  2254. if (err) Error("Error starting the specified DMA engine");
  2255. err = pcilib_get_dma_status(handle, dmaid, &status, 0, NULL);
  2256. if (err) Error("Failed to obtain status of the specified DMA engine");
  2257. buffer = (pcilib_dma_buffer_status_t*)malloc(status.ring_size*sizeof(pcilib_dma_buffer_status_t));
  2258. if (!buffer) Error("Failed to allocate memory for status buffer");
  2259. err = pcilib_get_dma_status(handle, dmaid, &status, status.ring_size, buffer);
  2260. if (err) Error("Failed to obtain extended status of the specified DMA engine");
  2261. printf("Buffer Status Total Size \n");
  2262. printf("--------------------------------------------------------------------------------\n");
  2263. for (i = 0; i < status.ring_size; i++) {
  2264. printf("%8zu ", i);
  2265. printf("%c%c %c%c ", buffer[i].used?'U':' ', buffer[i].error?'E':' ', buffer[i].first?'F':' ', buffer[i].last?'L':' ');
  2266. printf("%10s", GetPrintSize(stmp, buffer[i].size));
  2267. printf("\n");
  2268. }
  2269. printf("--------------------------------------------------------------------------------\n");
  2270. printf("U - Used, E - Error, F - First block, L - Last Block\n");
  2271. free(buffer);
  2272. pcilib_stop_dma(handle, dmaid, 0);
  2273. return 0;
  2274. }
  2275. int ReadBuffer(pcilib_t *handle, const char *device, const pcilib_model_description_t *model_info, pcilib_dma_engine_addr_t dma, pcilib_dma_direction_t dma_direction, size_t block, FILE *o) {
  2276. int err;
  2277. pcilib_dma_engine_t dmaid;
  2278. pcilib_dma_engine_status_t status;
  2279. pcilib_dma_buffer_status_t *buffer;
  2280. size_t size;
  2281. dmaid = pcilib_find_dma_by_addr(handle, dma_direction, dma);
  2282. if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("The specified DMA engine is not found");
  2283. err = pcilib_start_dma(handle, dmaid, 0);
  2284. if (err) Error("Error starting the specified DMA engine");
  2285. err = pcilib_get_dma_status(handle, dmaid, &status, 0, NULL);
  2286. if (err) Error("Failed to obtain status of the specified DMA engine");
  2287. buffer = (pcilib_dma_buffer_status_t*)malloc(status.ring_size*sizeof(pcilib_dma_buffer_status_t));
  2288. if (!buffer) Error("Failed to allocate memory for status buffer");
  2289. err = pcilib_get_dma_status(handle, dmaid, &status, status.ring_size, buffer);
  2290. if (err) Error("Failed to obtain extended status of the specified DMA engine");
  2291. if (block == (size_t)-1) {
  2292. // get current
  2293. }
  2294. size = buffer[block].size;
  2295. free(buffer);
  2296. pcilib_stop_dma(handle, dmaid, 0);
  2297. return ReadKMEM(handle, device, ((dma&0x7F)|((dma_direction == PCILIB_DMA_TO_DEVICE)?0x80:0x00))|(PCILIB_KMEM_USE_DMA_PAGES<<16), block, size, o);
  2298. }
  2299. int ListLocks(pcilib_t *ctx, int verbose) {
  2300. int err;
  2301. pcilib_lock_id_t i;
  2302. if (verbose)
  2303. printf("ID Refs Flags Locked Name\n");
  2304. else
  2305. printf("ID Refs Flags Name\n");
  2306. printf("--------------------------------------------------------------------------------\n");
  2307. for (i = 0; i < PCILIB_MAX_LOCKS; i++) {
  2308. pcilib_lock_t *lock = pcilib_get_lock_by_id(ctx, i);
  2309. const char *name = pcilib_lock_get_name(lock);
  2310. if (!name) break;
  2311. pcilib_lock_flags_t flags = pcilib_lock_get_flags(lock);
  2312. size_t refs = pcilib_lock_get_refs(lock);
  2313. printf("%4u %4zu ", i, refs);
  2314. if (flags&PCILIB_LOCK_FLAG_PERSISTENT) printf("P");
  2315. else printf(" ");
  2316. printf(" ");
  2317. if (verbose) {
  2318. err = pcilib_lock_custom(lock, PCILIB_LOCK_FLAGS_DEFAULT, PCILIB_TIMEOUT_IMMEDIATE);
  2319. switch (err) {
  2320. case 0:
  2321. pcilib_unlock(lock);
  2322. printf("No ");
  2323. break;
  2324. case PCILIB_ERROR_TIMEOUT:
  2325. printf("Yes ");
  2326. break;
  2327. default:
  2328. printf("Err: %3i ", err);
  2329. }
  2330. }
  2331. printf("%s\n", name);
  2332. }
  2333. printf("--------------------------------------------------------------------------------\n");
  2334. printf("P - Persistent\n");
  2335. return 0;
  2336. }
  2337. int FreeLocks(pcilib_t *handle, int force) {
  2338. return pcilib_destroy_all_locks(handle, force);
  2339. }
  2340. int LockUnlock(pcilib_t *handle, const char *name, int do_lock, pcilib_timeout_t timeout) {
  2341. int err = 0;
  2342. pcilib_lock_t *lock = pcilib_get_lock(handle, PCILIB_LOCK_FLAG_PERSISTENT, name);
  2343. if (!lock) Error("Error getting persistent lock %s", name);
  2344. if (do_lock)
  2345. err = pcilib_lock_custom(lock, PCILIB_LOCK_FLAGS_DEFAULT, timeout);
  2346. else
  2347. pcilib_unlock(lock);
  2348. if (err) {
  2349. pcilib_return_lock(handle, PCILIB_LOCK_FLAGS_DEFAULT, lock);
  2350. switch (err) {
  2351. case PCILIB_ERROR_TIMEOUT:
  2352. printf("Timeout locking %s\n", name);
  2353. break;
  2354. default:
  2355. Error("Error (%i) locking %s", err, name);
  2356. }
  2357. } else if (do_lock) {
  2358. pcilib_lock_ref(lock);
  2359. pcilib_return_lock(handle, PCILIB_LOCK_FLAGS_DEFAULT, lock);
  2360. printf("%s is locked\n", name);
  2361. } else {
  2362. pcilib_lock_unref(lock);
  2363. pcilib_return_lock(handle, PCILIB_LOCK_FLAGS_DEFAULT, lock);
  2364. }
  2365. return err;
  2366. }
  2367. int EnableIRQ(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_irq_type_t irq_type) {
  2368. int err;
  2369. err = pcilib_enable_irq(handle, irq_type, 0);
  2370. if (err) {
  2371. if ((err != PCILIB_ERROR_NOTSUPPORTED)&&(err != PCILIB_ERROR_NOTAVAILABLE))
  2372. Error("Error enabling IRQs");
  2373. }
  2374. return err;
  2375. }
  2376. int DisableIRQ(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_irq_type_t irq_type) {
  2377. int err;
  2378. err = pcilib_disable_irq(handle, 0);
  2379. if (err) {
  2380. if ((err != PCILIB_ERROR_NOTSUPPORTED)&&(err != PCILIB_ERROR_NOTAVAILABLE))
  2381. Error("Error disabling IRQs");
  2382. }
  2383. return err;
  2384. }
  2385. int AckIRQ(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_irq_hw_source_t irq_source) {
  2386. pcilib_clear_irq(handle, irq_source);
  2387. return 0;
  2388. }
  2389. int WaitIRQ(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_irq_hw_source_t irq_source, pcilib_timeout_t timeout) {
  2390. int err;
  2391. size_t count;
  2392. err = pcilib_wait_irq(handle, irq_source, timeout, &count);
  2393. if (err) {
  2394. if (err == PCILIB_ERROR_TIMEOUT) Error("Timeout waiting for IRQ");
  2395. else Error("Error waiting for IRQ");
  2396. }
  2397. return 0;
  2398. }
  2399. int main(int argc, char **argv) {
  2400. int err = 0;
  2401. int i;
  2402. long itmp;
  2403. size_t ztmp;
  2404. unsigned char c;
  2405. const char *stmp;
  2406. const char *num_offset;
  2407. int details = 0;
  2408. int verbose = 0;
  2409. int quiete = 0;
  2410. int force = 0;
  2411. int verify = 0;
  2412. pcilib_log_priority_t log_priority;
  2413. const char *model = NULL;
  2414. const pcilib_model_description_t *model_info;
  2415. const pcilib_dma_description_t *dma_info;
  2416. MODE mode = MODE_INVALID;
  2417. GRAB_MODE grab_mode = 0;
  2418. size_t trigger_time = 0;
  2419. size_t run_time = 0;
  2420. size_t buffer = 0;
  2421. size_t threads = 1;
  2422. FORMAT format = FORMAT_DEFAULT;
  2423. PARTITION partition = PARTITION_UNKNOWN;
  2424. FLAGS flags = 0;
  2425. const char *atype = NULL;
  2426. const char *type = NULL;
  2427. ACCESS_MODE amode = ACCESS_BAR;
  2428. const char *fpga_device = DEFAULT_FPGA_DEVICE;
  2429. pcilib_bar_t bar = PCILIB_BAR_DETECT;
  2430. const char *addr = NULL;
  2431. const char *reg = NULL;
  2432. const char *view = NULL;
  2433. const char *unit = NULL;
  2434. const char *attr = NULL;
  2435. const char *bank = NULL;
  2436. char **data = NULL;
  2437. const char *event = NULL;
  2438. const char *data_type = NULL;
  2439. const char *dma_channel = NULL;
  2440. const char *use = NULL;
  2441. const char *lock = NULL;
  2442. const char *info_target = NULL;
  2443. const char *list_target = NULL;
  2444. size_t block = (size_t)-1;
  2445. pcilib_irq_type_t irq_type = PCILIB_IRQ_TYPE_ALL;
  2446. pcilib_irq_hw_source_t irq_source = PCILIB_IRQ_SOURCE_DEFAULT;
  2447. pcilib_dma_direction_t dma_direction = PCILIB_DMA_BIDIRECTIONAL;
  2448. pcilib_kmem_use_t useid = 0;
  2449. pcilib_dma_engine_addr_t dma = PCILIB_DMA_ENGINE_ADDR_INVALID;
  2450. long addr_shift = 0;
  2451. uintptr_t start = -1;
  2452. size_t block_size = 0;
  2453. size_t size = 1;
  2454. access_t access = 4;
  2455. // int skip = 0;
  2456. int endianess = 0;
  2457. size_t timeout = 0;
  2458. size_t alignment = 0;
  2459. const char *output = NULL;
  2460. FILE *ofile = NULL;
  2461. size_t iterations = BENCHMARK_ITERATIONS;
  2462. pcilib_t *handle;
  2463. int size_set = 0;
  2464. int timeout_set = 0;
  2465. // int run_time_set = 0;
  2466. struct sched_param sched_param = {0};
  2467. while ((c = getopt_long(argc, argv, "hqilr::w::g::d:m:t:b:a:s:e:o:", long_options, NULL)) != (unsigned char)-1) {
  2468. extern int optind;
  2469. switch (c) {
  2470. case OPT_HELP:
  2471. Usage(argc, argv, NULL);
  2472. break;
  2473. case OPT_INFO:
  2474. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2475. if (optarg) info_target = optarg;
  2476. else if ((optind < argc)&&(argv[optind][0] != '-')) info_target = argv[optind++];
  2477. mode = MODE_INFO;
  2478. break;
  2479. case OPT_LIST:
  2480. if (mode == MODE_LIST) details++;
  2481. else if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2482. if (optarg) list_target = optarg;
  2483. else if ((optind < argc)&&(argv[optind][0] != '-')) list_target = argv[optind++];
  2484. mode = MODE_LIST;
  2485. break;
  2486. case OPT_RESET:
  2487. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2488. mode = MODE_RESET;
  2489. break;
  2490. case OPT_BENCHMARK:
  2491. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2492. mode = MODE_BENCHMARK;
  2493. if (optarg) addr = optarg;
  2494. else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
  2495. break;
  2496. case OPT_READ:
  2497. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2498. mode = MODE_READ;
  2499. if (optarg) addr = optarg;
  2500. else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
  2501. break;
  2502. case OPT_WRITE:
  2503. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2504. mode = MODE_WRITE;
  2505. if (optarg) addr = optarg;
  2506. else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
  2507. break;
  2508. case OPT_GRAB:
  2509. if ((mode != MODE_INVALID)&&((mode != MODE_GRAB)||(grab_mode&GRAB_MODE_GRAB))) Usage(argc, argv, "Multiple operations are not supported");
  2510. mode = MODE_GRAB;
  2511. grab_mode |= GRAB_MODE_GRAB;
  2512. stmp = NULL;
  2513. if (optarg) stmp = optarg;
  2514. else if ((optind < argc)&&(argv[optind][0] != '-')) stmp = argv[optind++];
  2515. if (stmp) {
  2516. if ((event)&&(strcasecmp(stmp,event))) Usage(argc, argv, "Redefinition of considered event");
  2517. event = stmp;
  2518. }
  2519. break;
  2520. case OPT_TRIGGER:
  2521. if ((mode != MODE_INVALID)&&((mode != MODE_GRAB)||(grab_mode&GRAB_MODE_TRIGGER))) Usage(argc, argv, "Multiple operations are not supported");
  2522. mode = MODE_GRAB;
  2523. grab_mode |= GRAB_MODE_TRIGGER;
  2524. stmp = NULL;
  2525. if (optarg) stmp = optarg;
  2526. else if ((optind < argc)&&(argv[optind][0] != '-')) stmp = argv[optind++];
  2527. if (stmp) {
  2528. if ((event)&&(strcasecmp(stmp,event))) Usage(argc, argv, "Redefinition of considered event");
  2529. event = stmp;
  2530. }
  2531. break;
  2532. case OPT_LIST_DMA:
  2533. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2534. mode = MODE_LIST_DMA;
  2535. break;
  2536. case OPT_LIST_DMA_BUFFERS:
  2537. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2538. mode = MODE_LIST_DMA_BUFFERS;
  2539. dma_channel = optarg;
  2540. break;
  2541. case OPT_READ_DMA_BUFFER:
  2542. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2543. mode = MODE_READ_DMA_BUFFER;
  2544. num_offset = strchr(optarg, ':');
  2545. if (num_offset) {
  2546. if (sscanf(num_offset + 1, "%zu", &block) != 1)
  2547. Usage(argc, argv, "Invalid buffer is specified (%s)", num_offset + 1);
  2548. *(char*)num_offset = 0;
  2549. } else block = (size_t)-1;
  2550. dma_channel = optarg;
  2551. break;
  2552. case OPT_START_DMA:
  2553. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2554. mode = MODE_START_DMA;
  2555. if (optarg) dma_channel = optarg;
  2556. else if ((optind < argc)&&(argv[optind][0] != '-')) dma_channel = argv[optind++];
  2557. break;
  2558. case OPT_STOP_DMA:
  2559. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2560. mode = MODE_STOP_DMA;
  2561. if (optarg) dma_channel = optarg;
  2562. else if ((optind < argc)&&(argv[optind][0] != '-')) dma_channel = argv[optind++];
  2563. break;
  2564. case OPT_ENABLE_IRQ:
  2565. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2566. mode = MODE_ENABLE_IRQ;
  2567. if (optarg) num_offset = optarg;
  2568. else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
  2569. else num_offset = NULL;
  2570. if (num_offset) {
  2571. if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
  2572. Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
  2573. irq_type = itmp;
  2574. }
  2575. break;
  2576. case OPT_DISABLE_IRQ:
  2577. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2578. mode = MODE_DISABLE_IRQ;
  2579. if (optarg) num_offset = optarg;
  2580. else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
  2581. else num_offset = NULL;
  2582. if (num_offset) {
  2583. if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
  2584. Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
  2585. irq_type = itmp;
  2586. }
  2587. break;
  2588. case OPT_ACK_IRQ:
  2589. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2590. mode = MODE_ACK_IRQ;
  2591. if (optarg) num_offset = optarg;
  2592. else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
  2593. else num_offset = NULL;
  2594. if (num_offset) {
  2595. if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
  2596. Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
  2597. irq_source = itmp;
  2598. }
  2599. break;
  2600. case OPT_WAIT_IRQ:
  2601. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2602. mode = MODE_WAIT_IRQ;
  2603. if (optarg) num_offset = optarg;
  2604. else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
  2605. else num_offset = NULL;
  2606. if (num_offset) {
  2607. if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
  2608. Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
  2609. irq_source = itmp;
  2610. }
  2611. break;
  2612. case OPT_LIST_KMEM:
  2613. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2614. mode = MODE_LIST_KMEM;
  2615. if (optarg) use = optarg;
  2616. else if ((optind < argc)&&(argv[optind][0] != '-')) use = argv[optind++];
  2617. else use = NULL;
  2618. if (use) {
  2619. num_offset = strchr(use, ':');
  2620. if (num_offset) {
  2621. if (sscanf(num_offset + 1, "%zu", &block) != 1)
  2622. Usage(argc, argv, "Invalid block number is specified (%s)", num_offset + 1);
  2623. *(char*)num_offset = 0;
  2624. }
  2625. }
  2626. break;
  2627. case OPT_READ_KMEM:
  2628. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2629. mode = MODE_READ_KMEM;
  2630. if (!model) model = "pci";
  2631. num_offset = strchr(optarg, ':');
  2632. if (num_offset) {
  2633. if (sscanf(num_offset + 1, "%zu", &block) != 1)
  2634. Usage(argc, argv, "Invalid block number is specified (%s)", num_offset + 1);
  2635. *(char*)num_offset = 0;
  2636. }
  2637. use = optarg;
  2638. useid = ParseUse(use);
  2639. break;
  2640. case OPT_ALLOC_KMEM:
  2641. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2642. mode = MODE_ALLOC_KMEM;
  2643. model = "pci";
  2644. if (optarg) use = optarg;
  2645. else if ((optind < argc)&&(argv[optind][0] != '-')) use = argv[optind++];
  2646. break;
  2647. case OPT_FREE_KMEM:
  2648. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2649. mode = MODE_FREE_KMEM;
  2650. if (!model) model = "pci";
  2651. if (optarg) use = optarg;
  2652. else if ((optind < argc)&&(argv[optind][0] != '-')) use = argv[optind++];
  2653. break;
  2654. case OPT_LIST_LOCKS:
  2655. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2656. mode = MODE_LIST_LOCKS;
  2657. break;
  2658. case OPT_FREE_LOCKS:
  2659. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2660. mode = MODE_FREE_LOCKS;
  2661. model = "maintenance";
  2662. break;
  2663. case OPT_LOCK:
  2664. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2665. mode = MODE_LOCK;
  2666. lock = optarg;
  2667. break;
  2668. case OPT_UNLOCK:
  2669. if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
  2670. mode = MODE_UNLOCK;
  2671. lock = optarg;
  2672. break;
  2673. case OPT_DEVICE:
  2674. fpga_device = optarg;
  2675. break;
  2676. case OPT_MODEL:
  2677. model = optarg;
  2678. /* if (!strcasecmp(optarg, "pci")) model = PCILIB_MODEL_PCI;
  2679. else if (!strcasecmp(optarg, "ipecamera")) model = PCILIB_MODEL_IPECAMERA;
  2680. else if (!strcasecmp(optarg, "kapture")) model = PCILIB_MODEL_KAPTURE;
  2681. else Usage(argc, argv, "Invalid memory model (%s) is specified", optarg);*/
  2682. break;
  2683. case OPT_BAR:
  2684. bank = optarg;
  2685. // if ((sscanf(optarg,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_BANKS)) Usage(argc, argv, "Invalid data bank (%s) is specified", optarg);
  2686. // else bar = itmp;
  2687. break;
  2688. case OPT_ALIGNMENT:
  2689. if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &alignment) != 1)) {
  2690. Usage(argc, argv, "Invalid alignment is specified (%s)", optarg);
  2691. }
  2692. break;
  2693. case OPT_ACCESS:
  2694. if (!strncasecmp(optarg, "fifo", 4)) {
  2695. atype = "fifo";
  2696. num_offset = optarg + 4;
  2697. amode = ACCESS_FIFO;
  2698. } else if (!strncasecmp(optarg, "dma", 3)) {
  2699. atype = "dma";
  2700. num_offset = optarg + 3;
  2701. amode = ACCESS_DMA;
  2702. } else if (!strncasecmp(optarg, "bar", 3)) {
  2703. atype = "plain";
  2704. num_offset = optarg + 3;
  2705. amode = ACCESS_BAR;
  2706. } else if (!strncasecmp(optarg, "config", 6)) {
  2707. atype = "config";
  2708. num_offset = optarg + 6;
  2709. amode = ACCESS_CONFIG;
  2710. } else if (!strncasecmp(optarg, "plain", 5)) {
  2711. atype = "plain";
  2712. num_offset = optarg + 5;
  2713. amode = ACCESS_BAR;
  2714. } else {
  2715. num_offset = optarg;
  2716. }
  2717. if (*num_offset) {
  2718. if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
  2719. Usage(argc, argv, "Invalid access type (%s) is specified", optarg);
  2720. switch (itmp) {
  2721. case 8: access = 1; break;
  2722. case 16: access = 2; break;
  2723. case 32: access = 4; break;
  2724. case 64: access = 8; break;
  2725. default: Usage(argc, argv, "Invalid data width (%s) is specified", num_offset);
  2726. }
  2727. }
  2728. break;
  2729. case OPT_SIZE:
  2730. if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &size) != 1)) {
  2731. if (strcasecmp(optarg, "unlimited"))
  2732. Usage(argc, argv, "Invalid size is specified (%s)", optarg);
  2733. else
  2734. size = 0;//(size_t)-1;
  2735. }
  2736. size_set = 1;
  2737. break;
  2738. case OPT_BLOCK_SIZE:
  2739. if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &block_size) != 1)) {
  2740. Usage(argc, argv, "Invalid size is specified (%s)", optarg);
  2741. }
  2742. break;
  2743. case OPT_ENDIANESS:
  2744. if ((*optarg == 'b')||(*optarg == 'B')) {
  2745. if (ntohs(1) == 1) endianess = 0;
  2746. else endianess = 1;
  2747. } else if ((*optarg == 'l')||(*optarg == 'L')) {
  2748. if (ntohs(1) == 1) endianess = 1;
  2749. else endianess = 0;
  2750. } else Usage(argc, argv, "Invalid endianess is specified (%s)", optarg);
  2751. break;
  2752. case OPT_TIMEOUT:
  2753. if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &timeout) != 1)) {
  2754. if (strcasecmp(optarg, "unlimited"))
  2755. Usage(argc, argv, "Invalid timeout is specified (%s)", optarg);
  2756. else
  2757. timeout = PCILIB_TIMEOUT_INFINITE;
  2758. }
  2759. timeout_set = 1;
  2760. break;
  2761. case OPT_OUTPUT:
  2762. output = optarg;
  2763. break;
  2764. case OPT_ITERATIONS:
  2765. if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &iterations) != 1))
  2766. Usage(argc, argv, "Invalid number of iterations is specified (%s)", optarg);
  2767. break;
  2768. case OPT_EVENT:
  2769. event = optarg;
  2770. break;
  2771. case OPT_TYPE:
  2772. type = optarg;
  2773. break;
  2774. case OPT_DATA_TYPE:
  2775. data_type = optarg;
  2776. break;
  2777. case OPT_RUN_TIME:
  2778. if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &run_time) != 1)) {
  2779. if (strcasecmp(optarg, "unlimited"))
  2780. Usage(argc, argv, "Invalid run-time is specified (%s)", optarg);
  2781. else
  2782. run_time = 0;
  2783. }
  2784. // run_time_set = 1;
  2785. break;
  2786. case OPT_TRIGGER_TIME:
  2787. if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &trigger_time) != 1))
  2788. Usage(argc, argv, "Invalid trigger-time is specified (%s)", optarg);
  2789. break;
  2790. case OPT_TRIGGER_RATE:
  2791. if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &ztmp) != 1))
  2792. Usage(argc, argv, "Invalid trigger-rate is specified (%s)", optarg);
  2793. trigger_time = (1000000 / ztmp) + ((1000000 % ztmp)?1:0);
  2794. break;
  2795. case OPT_BUFFER:
  2796. if (optarg) num_offset = optarg;
  2797. else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
  2798. else num_offset = NULL;
  2799. if (num_offset) {
  2800. if ((!isnumber(num_offset))||(sscanf(num_offset, "%zu", &buffer) != 1))
  2801. Usage(argc, argv, "Invalid buffer size is specified (%s)", num_offset);
  2802. buffer *= 1024 * 1024;
  2803. } else {
  2804. buffer = get_free_memory();
  2805. if (buffer < 256) Error("Not enough free memory (%lz MB) for buffering", buffer / 1024 / 1024);
  2806. buffer -= 128 + buffer/16;
  2807. }
  2808. break;
  2809. case OPT_THREADS:
  2810. if (optarg) num_offset = optarg;
  2811. else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
  2812. else num_offset = NULL;
  2813. if (num_offset) {
  2814. if ((!isnumber(num_offset))||(sscanf(num_offset, "%zu", &threads) != 1))
  2815. Usage(argc, argv, "Invalid threads number is specified (%s)", num_offset);
  2816. } else {
  2817. threads = 0;
  2818. }
  2819. break;
  2820. case OPT_FORMAT:
  2821. if (!strcasecmp(optarg, "raw")) format = FORMAT_RAW;
  2822. else if (!strcasecmp(optarg, "add_header")) format = FORMAT_HEADER;
  2823. // else if (!strcasecmp(optarg, "ringfs")) format = FORMAT_RINGFS;
  2824. else if (strcasecmp(optarg, "default")) Error("Invalid format (%s) is specified", optarg);
  2825. break;
  2826. case OPT_QUIETE:
  2827. quiete = 1;
  2828. verbose = -1;
  2829. break;
  2830. case OPT_VERBOSE:
  2831. if (optarg) num_offset = optarg;
  2832. else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
  2833. else num_offset = NULL;
  2834. if (num_offset) {
  2835. if ((!isnumber(num_offset))||(sscanf(num_offset, "%i", &verbose) != 1))
  2836. Usage(argc, argv, "Invalid verbosity level is specified (%s)", num_offset);
  2837. } else {
  2838. verbose = 1;
  2839. }
  2840. break;
  2841. case OPT_FORCE:
  2842. force = 1;
  2843. break;
  2844. case OPT_VERIFY:
  2845. verify = 1;
  2846. break;
  2847. case OPT_MULTIPACKET:
  2848. flags |= FLAG_MULTIPACKET;
  2849. break;
  2850. case OPT_WAIT:
  2851. flags |= FLAG_WAIT;
  2852. break;
  2853. default:
  2854. Usage(argc, argv, "Unknown option (%s) with argument (%s)", optarg?argv[optind-2]:argv[optind-1], optarg?optarg:"(null)");
  2855. }
  2856. }
  2857. if (mode == MODE_INVALID) {
  2858. if (argc > 1) Usage(argc, argv, "Operation is not specified");
  2859. else Usage(argc, argv, NULL);
  2860. }
  2861. if (verbose) log_priority = PCILIB_LOG_INFO;
  2862. else if (quiete) log_priority = PCILIB_LOG_ERROR;
  2863. else log_priority = PCILIB_LOG_WARNING;
  2864. pcilib_set_logger(log_priority, &LogError, NULL);
  2865. handle = pcilib_open(fpga_device, model);
  2866. if (handle < 0) Error("Failed to open FPGA device: %s", fpga_device);
  2867. model_info = pcilib_get_model_description(handle);
  2868. dma_info = pcilib_get_dma_description(handle);
  2869. switch (mode) {
  2870. case MODE_WRITE:
  2871. if (((argc - optind) == 1)&&(*argv[optind] == '*')) {
  2872. int vallen = strlen(argv[optind]);
  2873. if (vallen > 1) {
  2874. data = (char**)malloc(size * (vallen + sizeof(char*)));
  2875. if (!data) Error("Error allocating memory for data array");
  2876. for (i = 0; i < size; i++) {
  2877. data[i] = ((char*)data) + size * sizeof(char*) + i * vallen;
  2878. strcpy(data[i], argv[optind] + 1);
  2879. }
  2880. } else {
  2881. data = (char**)malloc(size * (9 + sizeof(char*)));
  2882. if (!data) Error("Error allocating memory for data array");
  2883. for (i = 0; i < size; i++) {
  2884. data[i] = ((char*)data) + size * sizeof(char*) + i * 9;
  2885. sprintf(data[i], "%x", i);
  2886. }
  2887. }
  2888. } else if ((argc - optind) == size) data = argv + optind;
  2889. else Usage(argc, argv, "The %i data values is specified, but %i required", argc - optind, size);
  2890. case MODE_READ:
  2891. if (!addr) {
  2892. if (((!dma_info)||(!dma_info->api))&&(!model_info->api)&&(!handle->num_reg)) {
  2893. // if (model == PCILIB_MODEL_PCI) {
  2894. if ((amode != ACCESS_DMA)&&(amode != ACCESS_CONFIG))
  2895. Usage(argc, argv, "The address is not specified");
  2896. } else ++mode;
  2897. }
  2898. break;
  2899. case MODE_START_DMA:
  2900. case MODE_STOP_DMA:
  2901. case MODE_LIST_DMA_BUFFERS:
  2902. case MODE_READ_DMA_BUFFER:
  2903. if ((dma_channel)&&(*dma_channel)) {
  2904. itmp = strlen(dma_channel) - 1;
  2905. if (dma_channel[itmp] == 'r') dma_direction = PCILIB_DMA_FROM_DEVICE;
  2906. else if (dma_channel[itmp] == 'w') dma_direction = PCILIB_DMA_TO_DEVICE;
  2907. if (dma_direction != PCILIB_DMA_BIDIRECTIONAL) itmp--;
  2908. if (strncmp(dma_channel, "dma", 3)) num_offset = dma_channel;
  2909. else {
  2910. num_offset = dma_channel + 3;
  2911. itmp -= 3;
  2912. }
  2913. if (bank) {
  2914. if (strncmp(num_offset, bank, itmp)) Usage(argc, argv, "Conflicting DMA channels are specified in mode parameter (%s) and bank parameter (%s)", dma_channel, bank);
  2915. }
  2916. if (!isnumber_n(num_offset, itmp))
  2917. Usage(argc, argv, "Invalid DMA channel (%s) is specified", dma_channel);
  2918. dma = atoi(num_offset);
  2919. }
  2920. break;
  2921. case MODE_LIST:
  2922. if (bank&&list_target) {
  2923. if (strcmp(list_target, bank))
  2924. Usage(argc, argv, "Conflicting banks are specified in list parameter (%s) and bank parameter (%s)", list_target, bank);
  2925. } else if (bank) {
  2926. list_target = bank;
  2927. }
  2928. break;
  2929. default:
  2930. if (argc > optind) Usage(argc, argv, "Invalid non-option parameters are supplied");
  2931. }
  2932. if (addr) {
  2933. if ((!strncmp(addr, "dma", 3))&&((addr[3]==0)||isnumber(addr+3))) {
  2934. if ((atype)&&(amode != ACCESS_DMA)) Usage(argc, argv, "Conflicting access modes, the DMA read is requested, but access type is (%s)", type);
  2935. if (bank) {
  2936. if ((addr[3] != 0)&&(strcmp(addr + 3, bank))) Usage(argc, argv, "Conflicting DMA channels are specified in read parameter (%s) and bank parameter (%s)", addr + 3, bank);
  2937. } else {
  2938. if (addr[3] == 0) Usage(argc, argv, "The DMA channel is not specified");
  2939. }
  2940. dma = atoi(addr + 3);
  2941. amode = ACCESS_DMA;
  2942. addr = NULL;
  2943. } else if ((!strncmp(addr, "bar", 3))&&((addr[3]==0)||isnumber(addr+3))) {
  2944. if ((atype)&&(amode != ACCESS_BAR)) Usage(argc, argv, "Conflicting access modes, the plain PCI read is requested, but access type is (%s)", type);
  2945. if ((addr[3] != 0)&&(strcmp(addr + 3, bank))) Usage(argc, argv, "Conflicting PCI bars are specified in read parameter (%s) and bank parameter (%s)", addr + 3, bank);
  2946. bar = atoi(addr + 3);
  2947. amode = ACCESS_BAR;
  2948. addr = NULL;
  2949. } else if (!strcmp(addr, "config")) {
  2950. if ((atype)&&(amode != ACCESS_CONFIG)) Usage(argc, argv, "Conflicting access modes, the read of PCI configurataion space is requested, but access type is (%s)", type);
  2951. amode = ACCESS_CONFIG;
  2952. addr = NULL;
  2953. } else if ((isxnumber(addr))&&(sscanf(addr, "%lx", &start) == 1)) {
  2954. // check if the address in the register range
  2955. const pcilib_register_range_t *ranges = model_info->ranges;
  2956. if (ranges) {
  2957. for (i = 0; ranges[i].start != ranges[i].end; i++)
  2958. if ((start >= ranges[i].start)&&(start <= ranges[i].end)) break;
  2959. // register access in plain mode
  2960. if (ranges[i].start != ranges[i].end) {
  2961. pcilib_register_bank_t regbank = pcilib_find_register_bank_by_addr(handle, ranges[i].bank);
  2962. if (regbank == PCILIB_REGISTER_BANK_INVALID) Error("Configuration error: register bank specified in the address range is not found");
  2963. bank = model_info->banks[regbank].name;
  2964. start += ranges[i].addr_shift;
  2965. addr_shift = ranges[i].addr_shift;
  2966. ++mode;
  2967. }
  2968. }
  2969. } else {
  2970. const char *spec;
  2971. attr = strchr(addr, '@');
  2972. if (attr) {
  2973. size_t spec_size = strlen(addr) - strlen(attr);
  2974. spec = strndupa(addr, spec_size);
  2975. attr++;
  2976. } else {
  2977. spec = addr;
  2978. }
  2979. view = strchr(spec, '/');
  2980. unit = strchr((view?view:spec), ':');
  2981. if (view||unit) {
  2982. size_t reg_size = strlen(spec) - strlen(view?view:unit);
  2983. if (reg_size) reg = strndupa(spec, reg_size);
  2984. else reg = NULL;
  2985. if ((reg)&&(view)) view++;
  2986. if (unit) unit++;
  2987. if (view&&unit) {
  2988. view = strndupa(view, strlen(view) - strlen(unit) - 1);
  2989. } else if ((reg)&&(unit)) {
  2990. view = unit;
  2991. unit = NULL;
  2992. }
  2993. } else {
  2994. if (*spec) reg = spec;
  2995. else reg = NULL;
  2996. }
  2997. if (reg) {
  2998. if (pcilib_find_register(handle, bank, reg) == PCILIB_REGISTER_INVALID) {
  2999. Usage(argc, argv, "Invalid address (%s) is specified", addr);
  3000. }
  3001. }
  3002. if (attr) {
  3003. if (mode == MODE_WRITE)
  3004. Error("Writting of attributes is not supported");
  3005. mode += 3;
  3006. } else if (reg) {
  3007. mode += 1;
  3008. } else {
  3009. mode += 2;
  3010. }
  3011. }
  3012. }
  3013. if (mode == MODE_GRAB) {
  3014. if (output) {
  3015. char fsname[128];
  3016. if (!get_file_fs(output, 127, fsname)) {
  3017. if (!strcmp(fsname, "ext4")) partition = PARTITION_EXT4;
  3018. else if (!strcmp(fsname, "raw")) partition = PARTITION_RAW;
  3019. }
  3020. } else {
  3021. output = "/dev/null";
  3022. partition = PARTITION_NULL;
  3023. }
  3024. if (!timeout_set) {
  3025. if (run_time) timeout = PCILIB_TIMEOUT_INFINITE;
  3026. else timeout = PCILIB_EVENT_TIMEOUT;
  3027. }
  3028. if (!size_set) {
  3029. if (run_time) size = 0;
  3030. }
  3031. }
  3032. if (mode != MODE_GRAB) {
  3033. if (size == (size_t)-1)
  3034. Usage(argc, argv, "Unlimited size is not supported in selected operation mode");
  3035. }
  3036. if ((bank)&&(amode == ACCESS_DMA)) {
  3037. if ((!isnumber(bank))||(sscanf(bank,"%li", &itmp) != 1)||(itmp < 0))
  3038. Usage(argc, argv, "Invalid DMA channel (%s) is specified", bank);
  3039. else dma = itmp;
  3040. } else if (bank) {
  3041. switch (mode) {
  3042. case MODE_BENCHMARK:
  3043. case MODE_READ:
  3044. case MODE_WRITE:
  3045. if ((!isnumber(bank))||(sscanf(bank,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_REGISTER_BANKS))
  3046. Usage(argc, argv, "Invalid data bank (%s) is specified", bank);
  3047. else bar = itmp;
  3048. break;
  3049. default:
  3050. if (pcilib_find_register_bank(handle, bank) == PCILIB_REGISTER_BANK_INVALID)
  3051. Usage(argc, argv, "Invalid data bank (%s) is specified", bank);
  3052. }
  3053. }
  3054. signal(SIGINT, signal_exit_handler);
  3055. if ((mode != MODE_GRAB)&&(output)) {
  3056. ofile = fopen(output, "a+");
  3057. if (!ofile) {
  3058. Error("Failed to open file \"%s\"", output);
  3059. }
  3060. }
  3061. // Requesting real-time priority when needed
  3062. switch (mode) {
  3063. case MODE_READ:
  3064. case MODE_WRITE:
  3065. if (amode != ACCESS_DMA)
  3066. break;
  3067. case MODE_BENCHMARK:
  3068. case MODE_GRAB:
  3069. sched_param.sched_priority = sched_get_priority_min(SCHED_FIFO);
  3070. err = sched_setscheduler(0, SCHED_FIFO, &sched_param);
  3071. if (err) pcilib_info("Failed to acquire real-time priority (errno: %i)", errno);
  3072. break;
  3073. default:
  3074. ;
  3075. }
  3076. switch (mode) {
  3077. case MODE_INFO:
  3078. Info(handle, model_info, info_target);
  3079. break;
  3080. case MODE_LIST:
  3081. if ((list_target)&&(*list_target == '/'))
  3082. ListProperties(handle, list_target, details);
  3083. else
  3084. List(handle, model_info, list_target, details);
  3085. break;
  3086. case MODE_BENCHMARK:
  3087. Benchmark(handle, amode, dma, bar, start, size_set?size:0, access, iterations);
  3088. break;
  3089. case MODE_READ:
  3090. if (amode == ACCESS_DMA) {
  3091. err = ReadData(handle, amode, flags, dma, bar, start, size_set?size:0, access, endianess, timeout_set?timeout:(size_t)-1, ofile);
  3092. } else if (amode == ACCESS_CONFIG) {
  3093. err = ReadData(handle, amode, flags, dma, bar, addr?start:0, (addr||size_set)?size:(256/abs(access)), access, endianess, (size_t)-1, ofile);
  3094. } else if (addr) {
  3095. err = ReadData(handle, amode, flags, dma, bar, start, size, access, endianess, (size_t)-1, ofile);
  3096. } else {
  3097. Error("Address to read is not specified");
  3098. }
  3099. break;
  3100. case MODE_READ_REGISTER:
  3101. case MODE_READ_PROPERTY:
  3102. case MODE_READ_ATTR:
  3103. if ((reg)||(view)||(attr)||(!addr)) ReadRegister(handle, model_info, bank, reg, view, unit, attr);
  3104. else ReadRegisterRange(handle, model_info, bank, start, addr_shift, size, ofile);
  3105. break;
  3106. case MODE_WRITE:
  3107. WriteData(handle, amode, dma, bar, start, size, access, endianess, data, verify);
  3108. break;
  3109. case MODE_WRITE_REGISTER:
  3110. case MODE_WRITE_PROPERTY:
  3111. if (reg||view) WriteRegister(handle, model_info, bank, reg, view, unit, data);
  3112. else WriteRegisterRange(handle, model_info, bank, start, addr_shift, size, data);
  3113. break;
  3114. case MODE_RESET:
  3115. pcilib_reset(handle);
  3116. break;
  3117. case MODE_GRAB:
  3118. TriggerAndGrab(handle, grab_mode, event, data_type, size, run_time, trigger_time, timeout, partition, format, buffer, threads, verbose, output);
  3119. break;
  3120. case MODE_LIST_DMA:
  3121. ListDMA(handle, fpga_device, model_info);
  3122. break;
  3123. case MODE_LIST_DMA_BUFFERS:
  3124. ListBuffers(handle, fpga_device, model_info, dma, dma_direction);
  3125. break;
  3126. case MODE_READ_DMA_BUFFER:
  3127. ReadBuffer(handle, fpga_device, model_info, dma, dma_direction, block, ofile);
  3128. break;
  3129. case MODE_START_DMA:
  3130. StartStopDMA(handle, model_info, dma, dma_direction, 1);
  3131. break;
  3132. case MODE_STOP_DMA:
  3133. StartStopDMA(handle, model_info, dma, dma_direction, 0);
  3134. break;
  3135. case MODE_ENABLE_IRQ:
  3136. EnableIRQ(handle, model_info, irq_type);
  3137. break;
  3138. case MODE_DISABLE_IRQ:
  3139. DisableIRQ(handle, model_info, irq_type);
  3140. break;
  3141. case MODE_ACK_IRQ:
  3142. AckIRQ(handle, model_info, irq_source);
  3143. break;
  3144. case MODE_WAIT_IRQ:
  3145. WaitIRQ(handle, model_info, irq_source, timeout);
  3146. break;
  3147. case MODE_LIST_KMEM:
  3148. if (use) DetailKMEM(handle, fpga_device, use, block);
  3149. else ListKMEM(handle, fpga_device);
  3150. break;
  3151. case MODE_READ_KMEM:
  3152. ReadKMEM(handle, fpga_device, useid, block, 0, ofile);
  3153. break;
  3154. case MODE_ALLOC_KMEM:
  3155. AllocKMEM(handle, fpga_device, use, type, size, block_size, alignment);
  3156. break;
  3157. case MODE_FREE_KMEM:
  3158. FreeKMEM(handle, fpga_device, use, force);
  3159. break;
  3160. case MODE_LIST_LOCKS:
  3161. ListLocks(handle, verbose);
  3162. break;
  3163. case MODE_FREE_LOCKS:
  3164. FreeLocks(handle, force);
  3165. break;
  3166. case MODE_LOCK:
  3167. LockUnlock(handle, lock, 1, timeout_set?timeout:PCILIB_TIMEOUT_INFINITE);
  3168. break;
  3169. case MODE_UNLOCK:
  3170. LockUnlock(handle, lock, 0, timeout_set?timeout:PCILIB_TIMEOUT_INFINITE);
  3171. break;
  3172. case MODE_INVALID:
  3173. break;
  3174. }
  3175. if (ofile) fclose(ofile);
  3176. pcilib_close(handle);
  3177. if (data != argv + optind) free(data);
  3178. return err;
  3179. }