1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949 |
- #define _XOPEN_SOURCE 700
- #define _POSIX_C_SOURCE 200112L
- #define _BSD_SOURCE
- #define _GNU_SOURCE
- #define _DEFAULT_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <stdint.h>
- #include <stdarg.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <sys/ioctl.h>
- #include <sys/mman.h>
- #include <errno.h>
- #include <alloca.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <pthread.h>
- #include <signal.h>
- #include <dlfcn.h>
- #include <getopt.h>
- #include <fastwriter.h>
- #include "pcitool/sysinfo.h"
- #include "pcitool/formaters.h"
- #include "pcitool/buildinfo.h"
- #include "views/transform.h"
- #include "views/enum.h"
- #include "pcilib/pci.h"
- #include "pcilib/plugin.h"
- #include "pcilib/config.h"
- #include "pcilib/tools.h"
- #include "pcilib/kmem.h"
- #include "pcilib/error.h"
- #include "pcilib/debug.h"
- #include "pcilib/model.h"
- #include "pcilib/locking.h"
- /* defines */
- #define MAX_KBUF 14
- //#define BIGBUFSIZE (512*1024*1024)
- #define BIGBUFSIZE (1024*1024)
- #define DEFAULT_FPGA_DEVICE "/dev/fpga0"
- #define LINE_WIDTH 80
- #define SEPARATOR_WIDTH 2
- #define BLOCK_SEPARATOR_WIDTH 2
- #define BLOCK_SIZE 8
- #define BENCHMARK_ITERATIONS 128
- #define STATUS_MESSAGE_INTERVAL 5 /* seconds */
- #define isnumber pcilib_isnumber
- #define isxnumber pcilib_isxnumber
- #define isnumber_n pcilib_isnumber_n
- #define isxnumber_n pcilib_isxnumber_n
- typedef uint8_t access_t;
- typedef enum {
- GRAB_MODE_GRAB = 1,
- GRAB_MODE_TRIGGER = 2
- } GRAB_MODE;
- typedef enum {
- MODE_INVALID,
- MODE_VERSION,
- MODE_INFO,
- MODE_LIST,
- MODE_BENCHMARK,
- MODE_READ,
- MODE_READ_REGISTER,
- MODE_READ_PROPERTY,
- MODE_READ_ATTR,
- MODE_WRITE,
- MODE_WRITE_REGISTER,
- MODE_WRITE_PROPERTY,
- MODE_RESET,
- MODE_GRAB,
- MODE_START_DMA,
- MODE_STOP_DMA,
- MODE_LIST_DMA,
- MODE_LIST_DMA_BUFFERS,
- MODE_READ_DMA_BUFFER,
- MODE_ENABLE_IRQ,
- MODE_DISABLE_IRQ,
- MODE_ACK_IRQ,
- MODE_WAIT_IRQ,
- MODE_SET_DMASK,
- MODE_SET_MPS,
- MODE_ALLOC_KMEM,
- MODE_LIST_KMEM,
- MODE_READ_KMEM,
- MODE_FREE_KMEM,
- MODE_LIST_LOCKS,
- MODE_FREE_LOCKS,
- MODE_LOCK,
- MODE_UNLOCK
- } MODE;
- typedef enum {
- ACCESS_BAR,
- ACCESS_DMA,
- ACCESS_FIFO,
- ACCESS_CONFIG
- } ACCESS_MODE;
- typedef enum {
- FLAG_MULTIPACKET = 1,
- FLAG_WAIT = 2
- } FLAGS;
- typedef enum {
- FORMAT_DEFAULT = 0,
- FORMAT_RAW,
- FORMAT_HEADER,
- FORMAT_RINGFS
- } FORMAT;
- typedef enum {
- PARTITION_UNKNOWN,
- PARTITION_RAW,
- PARTITION_EXT4,
- PARTITION_NULL
- } PARTITION;
- typedef enum {
- OPT_DEVICE = 'd',
- OPT_MODEL = 'm',
- OPT_BAR = 'b',
- OPT_ACCESS = 'a',
- OPT_ENDIANESS = 'e',
- OPT_SIZE = 's',
- OPT_OUTPUT = 'o',
- OPT_TIMEOUT = 't',
- OPT_INFO = 'i',
- OPT_LIST = 'l',
- OPT_READ = 'r',
- OPT_WRITE = 'w',
- OPT_GRAB = 'g',
- OPT_QUIETE = 'q',
- OPT_HELP = 'h',
- OPT_VERSION = 128,
- OPT_RESET,
- OPT_BENCHMARK,
- OPT_TRIGGER,
- OPT_DATA_TYPE,
- OPT_EVENT,
- OPT_TRIGGER_RATE,
- OPT_TRIGGER_TIME,
- OPT_RUN_TIME,
- OPT_FORMAT,
- OPT_BUFFER,
- OPT_THREADS,
- OPT_LIST_DMA,
- OPT_LIST_DMA_BUFFERS,
- OPT_READ_DMA_BUFFER,
- OPT_START_DMA,
- OPT_STOP_DMA,
- OPT_ENABLE_IRQ,
- OPT_DISABLE_IRQ,
- OPT_ACK_IRQ,
- OPT_WAIT_IRQ,
- OPT_ITERATIONS,
- OPT_SET_DMASK,
- OPT_SET_MPS,
- OPT_ALLOC_KMEM,
- OPT_LIST_KMEM,
- OPT_FREE_KMEM,
- OPT_READ_KMEM,
- OPT_LIST_LOCKS,
- OPT_FREE_LOCKS,
- OPT_LOCK,
- OPT_UNLOCK,
- OPT_BLOCK_SIZE,
- OPT_ALIGNMENT,
- OPT_TYPE,
- OPT_FORCE,
- OPT_VERIFY,
- OPT_WAIT,
- OPT_MULTIPACKET,
- OPT_VERBOSE
- } OPTIONS;
- static struct option long_options[] = {
- {"device", required_argument, 0, OPT_DEVICE },
- {"model", required_argument, 0, OPT_MODEL },
- {"bar", required_argument, 0, OPT_BAR },
- {"access", required_argument, 0, OPT_ACCESS },
- {"endianess", required_argument, 0, OPT_ENDIANESS },
- {"size", required_argument, 0, OPT_SIZE },
- {"output", required_argument, 0, OPT_OUTPUT },
- {"timeout", required_argument, 0, OPT_TIMEOUT },
- {"iterations", required_argument, 0, OPT_ITERATIONS },
- {"info", optional_argument, 0, OPT_INFO },
- {"list", optional_argument, 0, OPT_LIST },
- {"reset", no_argument, 0, OPT_RESET },
- {"benchmark", optional_argument, 0, OPT_BENCHMARK },
- {"read", optional_argument, 0, OPT_READ },
- {"write", optional_argument, 0, OPT_WRITE },
- {"grab", optional_argument, 0, OPT_GRAB },
- {"trigger", optional_argument, 0, OPT_TRIGGER },
- {"data", required_argument, 0, OPT_DATA_TYPE },
- {"event", required_argument, 0, OPT_EVENT },
- {"run-time", required_argument, 0, OPT_RUN_TIME },
- {"trigger-rate", required_argument, 0, OPT_TRIGGER_RATE },
- {"trigger-time", required_argument, 0, OPT_TRIGGER_TIME },
- {"format", required_argument, 0, OPT_FORMAT },
- {"buffer", optional_argument, 0, OPT_BUFFER },
- {"threads", optional_argument, 0, OPT_THREADS },
- {"start-dma", required_argument, 0, OPT_START_DMA },
- {"stop-dma", optional_argument, 0, OPT_STOP_DMA },
- {"list-dma-engines", no_argument, 0, OPT_LIST_DMA },
- {"list-dma-buffers", required_argument, 0, OPT_LIST_DMA_BUFFERS },
- {"read-dma-buffer", required_argument, 0, OPT_READ_DMA_BUFFER },
- {"enable-irq", optional_argument, 0, OPT_ENABLE_IRQ },
- {"disable-irq", optional_argument, 0, OPT_DISABLE_IRQ },
- {"acknowledge-irq", optional_argument, 0, OPT_ACK_IRQ },
- {"wait-irq", optional_argument, 0, OPT_WAIT_IRQ },
- {"set-dma-mask", required_argument, 0, OPT_SET_DMASK },
- {"set-mps", required_argument, 0, OPT_SET_MPS },
- {"list-kernel-memory", optional_argument, 0, OPT_LIST_KMEM },
- {"read-kernel-memory", required_argument, 0, OPT_READ_KMEM },
- {"alloc-kernel-memory", required_argument, 0, OPT_ALLOC_KMEM },
- {"free-kernel-memory", required_argument, 0, OPT_FREE_KMEM },
- {"list-locks", no_argument, 0, OPT_LIST_LOCKS },
- {"free-locks", no_argument, 0, OPT_FREE_LOCKS },
- {"lock", required_argument, 0, OPT_LOCK },
- {"unlock", required_argument, 0, OPT_UNLOCK },
- {"type", required_argument, 0, OPT_TYPE },
- {"block-size", required_argument, 0, OPT_BLOCK_SIZE },
- {"alignment", required_argument, 0, OPT_ALIGNMENT },
- {"quiete", no_argument, 0, OPT_QUIETE },
- {"verbose", optional_argument, 0, OPT_VERBOSE },
- {"force", no_argument, 0, OPT_FORCE },
- {"verify", no_argument, 0, OPT_VERIFY },
- {"multipacket", no_argument, 0, OPT_MULTIPACKET },
- {"wait", no_argument, 0, OPT_WAIT },
- {"version", no_argument, 0, OPT_VERSION },
- {"help", no_argument, 0, OPT_HELP },
- { 0, 0, 0, 0 }
- };
- void Usage(int argc, char *argv[], const char *format, ...) {
- if (format) {
- va_list ap;
-
- va_start(ap, format);
- printf("Error %i: ", errno);
- vprintf(format, ap);
- printf("\n");
- va_end(ap);
-
- printf("\n");
- }
- printf(
- "Usage:\n"
- " %s <mode> [options] [hex data]\n"
- " Modes:\n"
- " -i [target] - Device or Register (target) Info\n"
- " -l[l] [bank|/branch] - List (detailed) Data Banks & Registers\n"
- " -r <addr|dmaX|reg|prop> - Read Data/Register/Property\n"
- " -w <addr|dmaX|reg|prop> - Write Data/Register/Property\n"
- " --benchmark <barX|dmaX> - Performance Evaluation\n"
- " --reset - Reset board\n"
- " --version - Version information\n"
- " --help - Help message\n"
- "\n"
- " Property/Register Modes:\n"
- " -r <reg>/view[:unit] - Read register view\n"
- " -w <reg>/view[:unit] - Write register view\n"
- " -r <reg>/unit - Read register, detect view based on unit\n"
- " -w <reg>/unit - Write register, detect view based on unt\n"
- " -r <prop>[:unit] - Read property\n"
- " -w <prop>[:unit] - Write property\n"
- " -r <prop|reg>@attr - Read register/property attribute\n"
- "\n"
- " Event Modes:\n"
- " --trigger [event] - Trigger Events\n"
- " -g [event] - Grab Events\n"
- "\n"
- " IRQ Modes:\n"
- " --enable-irq [type] - Enable IRQs\n"
- " --disable-irq [type] - Disable IRQs\n"
- " --acknowledge-irq <source> - Clean IRQ queue\n"
- " --wait-irq <source> - Wait for IRQ\n"
- "\n"
- " DMA Modes:\n"
- " --start-dma <num>[r|w] - Start specified DMA engine\n"
- " --stop-dma [num[r|w]] - Stop specified engine or DMA subsystem\n"
- " --list-dma-engines - List active DMA engines\n"
- " --list-dma-buffers <dma> - List buffers for specified DMA engine\n"
- " --read-dma-buffer <dma:buf> - Read the specified buffer\n"
- "\n"
- " PCI Configuration:\n"
- " --set-dma-mask [bits] - Set DMA address width (DANGEROUS)\n"
- " --set-mps [bits] - Set PCIe Payload Size (DANGEROUS)\n"
- "\n"
- " Kernel Memory Modes:\n"
- " --list-kernel-memory [use] - List kernel buffers\n"
- " --read-kernel-memory <blk> - Read the specified block of the kernel memory\n"
- " block is specified as: use:block_number\n"
- " --alloc-kernel-memory <use> - Allocate kernel buffers (DANGEROUS)\n"
- " --free-kernel-memory <use> - Cleans lost kernel space buffers (DANGEROUS)\n"
- " dma - Remove all buffers allocated by DMA subsystem\n"
- " #number - Remove all buffers with the specified use id\n"
- "\n"
- " --list-locks - List all registered locks\n"
- " --free-locks - Destroy all locks (DANGEROUS)\n"
- " --lock <lock name> - Obtain persistent lock\n"
- " --unlock <lock name> - Release persistent lock\n"
- "\n"
- " Addressing:\n"
- " -d <device> - FPGA device (/dev/fpga0)\n"
- " -m <model> - Memory model (autodetected)\n"
- " pci - Plain\n"
- " ipecamera - IPE Camera\n"
- " -b <bank> - PCI bar, Register bank, or DMA channel\n"
- "\n"
- " Options:\n"
- " -s <size> - Number of words (default: 1)\n"
- " -a [fifo|dma|config]<bits> - Access type and bits per word (default: 32)\n"
- " -e <l|b> - Endianess Little/Big (default: host)\n"
- " -o <file> - Append output to file (default: stdout)\n"
- " -t <timeout|unlimited> - Timeout in microseconds\n"
- " --check - Verify write operations\n"
- "\n"
- " Event Options:\n"
- " --event <evt> - Specifies event for trigger and grab modes\n"
- " --data <type> - Data type to request for the events\n"
- " --run-time <us> - Limit time to grab/trigger events\n"
- " -t <timeout|unlimited> - Timeout to stop if no events triggered\n"
- " --trigger-rate <tps> - Generate tps triggers per second\n"
- " --trigger-time <us> - Specifies delay between triggers (us)\n"
- " -s <num|unlimited> - Number of events to grab and trigger\n"
- " --format [type] - Specifies how event data should be stored\n"
- " raw - Just write all events sequentially\n"
- " add_header - Prefix events with 512 bit header:\n"
- " event(64), data(64), nope(64), size(64)\n"
- " seqnum(64), offset(64), timestamp(128)\n"
- //" ringfs - Write to RingFS\n"
- " --buffer [size] - Request data buffering, size in MB\n"
- " --threads [num] - Allow multithreaded processing\n"
- "\n"
- " DMA Options:\n"
- " --multipacket - Read multiple packets\n"
- " --wait - Wait until data arrives\n"
- "\n"
- " Kernel Options:\n"
- " --type <type> - Type of kernel memory to allocate\n"
- " consistent - Consistent memory\n"
- " s2c - DMA S2C (write) memory\n"
- " c2s - DMA C2S (read) memory\n"
- " --page-size <size> - Size of kernel buffer in bytes (default: page)\n"
- " -s <size> - Number of buffers to allocate (default: 1)\n"
- " --allignment <alignment> - Buffer alignment (default: page)\n"
- "\n"
- " Information:\n"
- " --verbose [level] - Announce details of ongoing operations\n"
- " -q - Quiete mode (suppress warnings)\n"
- "\n"
- " Data:\n"
- " Data can be specified as sequence of hexdecimal number or\n"
- " a single value prefixed with '*'. In this case it will be\n"
- " replicated the specified amount of times\n"
- "\n\n",
- argv[0]);
- exit(0);
- }
- static int StopFlag = 0;
- static void signal_exit_handler(int signo) {
- if (++StopFlag > 2)
- exit(-1);
- }
- void LogError(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *format, va_list ap) {
- vprintf(format, ap);
- if (prio == PCILIB_LOG_ERROR) {
- if (errno) printf("\nerrno: %i (%s)", errno, strerror(errno));
- }
- printf("\n");
- if (prio == PCILIB_LOG_ERROR) {
- printf("Exiting at [%s:%u]\n\n", file, line);
- exit(-1);
- }
- }
- void ErrorInternal(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *format, ...) {
- va_list ap;
- va_start(ap, format);
- LogError(arg, file, line, prio, format, ap);
- va_end(ap);
- }
- #define Error(...) ErrorInternal(NULL, __FILE__, __LINE__, PCILIB_LOG_ERROR, __VA_ARGS__)
- int RegisterCompare(const void *aptr, const void *bptr, void *registers) {
- pcilib_register_description_t *a = &((pcilib_register_description_t*)registers)[*(const pcilib_register_t*)aptr];
- pcilib_register_description_t *b = &((pcilib_register_description_t*)registers)[*(const pcilib_register_t*)bptr];
- if (a->bank < b->bank) return -1;
- if (a->bank > b->bank) return 1;
- if (a->addr < b->addr) return -1;
- if (a->addr > b->addr) return 1;
- if ((a->type != PCILIB_REGISTER_BITS)&&(b->type == PCILIB_REGISTER_BITS)) return -1;
- if ((a->type == PCILIB_REGISTER_BITS)&&(b->type != PCILIB_REGISTER_BITS)) return 1;
- if (a->offset < b->offset) return -1;
- if (a->offset > b->offset) return 0;
- return 0;
- }
- void ListProperties(pcilib_t *handle, const char *branch, int details) {
- int i;
- pcilib_property_info_t *props;
- props = pcilib_get_property_list(handle, branch, 0);
- if (!props) Error("Error getting properties");
- if (props[0].path) {
- printf("Properties: \n");
- for (i = 0; props[i].path; i++) {
- const char *mode;
- const char *type;
- switch (props[i].type) {
- case PCILIB_TYPE_LONG:
- type = "int ";
- break;
- case PCILIB_TYPE_DOUBLE:
- type = "float ";
- break;
- case PCILIB_TYPE_STRING:
- type = "string ";
- break;
- case PCILIB_TYPE_INVALID:
- type = NULL;
- break;
- default:
- type = "unknown";
- }
-
- switch (props[i].mode) {
- case PCILIB_ACCESS_RW:
- mode = "RW";
- break;
- case PCILIB_ACCESS_R:
- mode = "R ";
- break;
- case PCILIB_ACCESS_W:
- mode = "W ";
- break;
- default:
- mode = " ";
- }
- if (type)
- printf(" (%s %s) ", type, mode);
- else
- printf(" %12s", "");
- if (props[i].flags&PCILIB_LIST_FLAG_CHILDS)
- printf(" + ");
- else
- printf(" ");
- if (details > 0) {
- printf("%s", props[i].name);
- if ((props[i].description)&&(props[i].description[0])) {
- printf(": %s", props[i].description);
- }
- } else {
- printf("%s", props[i].path);
- }
- printf("\n");
- }
- printf("\n");
- pcilib_free_property_info(handle, props);
- }
- }
- void List(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *bank, int details) {
- int i, j, k;
- const pcilib_register_bank_description_t *banks;
- const pcilib_register_description_t *registers;
- const pcilib_event_description_t *events;
- const pcilib_event_data_type_description_t *types;
- const pcilib_board_info_t *board_info = pcilib_get_board_info(handle);
- const pcilib_dma_description_t *dma_info = pcilib_get_dma_description(handle);
- for (i = 0; i < PCILIB_MAX_BARS; i++) {
- if (board_info->bar_length[i] > 0) {
- printf(" BAR %d - ", i);
- switch ( board_info->bar_flags[i]&IORESOURCE_TYPE_BITS) {
- case IORESOURCE_IO: printf(" IO"); break;
- case IORESOURCE_MEM: printf("MEM"); break;
- case IORESOURCE_IRQ: printf("IRQ"); break;
- case IORESOURCE_DMA: printf("DMA"); break;
- }
-
- if (board_info->bar_flags[i]&IORESOURCE_MEM_64) printf("64");
- else printf("32");
-
- 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] );
- }
- }
- printf("\n");
-
- if ((dma_info)&&(dma_info->engines)) {
- printf("DMA Engines: \n");
- for (i = 0; dma_info->engines[i].addr_bits; i++) {
- const pcilib_dma_engine_description_t *engine = &dma_info->engines[i];
- printf(" DMA %2d ", engine->addr);
- switch (engine->direction) {
- case PCILIB_DMA_FROM_DEVICE:
- printf("C2S");
- break;
- case PCILIB_DMA_TO_DEVICE:
- printf("S2C");
- break;
- case PCILIB_DMA_BIDIRECTIONAL:
- printf("BI ");
- break;
- }
- printf(" - Type: ");
- switch (engine->type) {
- case PCILIB_DMA_TYPE_BLOCK:
- printf("Block");
- break;
- case PCILIB_DMA_TYPE_PACKET:
- printf("Packet");
- break;
- default:
- printf("Unknown");
- }
-
- printf(", Address Width: %02lu bits\n", engine->addr_bits);
- }
- printf("\n");
- }
- if ((bank)&&(bank != (char*)-1)) banks = NULL;
- else banks = model_info->banks;
- if (banks) {
- printf("Banks: \n");
- for (i = 0; banks[i].access; i++) {
- printf(" 0x%02x %s", banks[i].addr, banks[i].name);
- if ((banks[i].description)&&(banks[i].description[0])) {
- printf(": %s", banks[i].description);
- }
- printf("\n");
- }
- printf("\n");
- }
-
- if (bank == (char*)-1) registers = NULL;
- else registers = model_info->registers;
-
- if (registers) {
- pcilib_register_t regsort[handle->num_reg];
-
- pcilib_register_bank_addr_t bank_addr = 0;
- if (bank) {
- pcilib_register_bank_t bank_id = pcilib_find_register_bank(handle, bank);
- const pcilib_register_bank_description_t *b = model_info->banks + bank_id;
- bank_addr = b->addr;
- if (b->description) printf("%s:\n", b->description);
- else if (b->name) printf("Registers of bank %s:\n", b->name);
- else printf("Registers of bank 0x%x:\n", b->addr);
- } else {
- printf("Registers: \n");
- }
- // sorting
- for (i = 0, k = 0; registers[i].bits; i++) {
- if ((bank)&&(registers[i].bank != bank_addr)) continue;
- if ((registers[i].type == PCILIB_REGISTER_BITS)&&(!details)) continue;
- regsort[k++] = i;
- }
- qsort_r(regsort, k, sizeof(pcilib_register_t), &RegisterCompare, (void*)registers);
-
-
- for (j = 0; j < k; j++) {
- const char *mode;
- i = regsort[j];
- if (registers[i].type == PCILIB_REGISTER_BITS) {
- if (!details) continue;
-
- if (registers[i].bits > 1) {
- printf(" [%2u:%2u] - %s\n", registers[i].offset, registers[i].offset + registers[i].bits, registers[i].name);
- } else {
- printf(" [ %2u] - %s\n", registers[i].offset, registers[i].name);
- }
-
- continue;
- }
- if (registers[i].mode == PCILIB_REGISTER_RW) mode = "RW";
- else if (registers[i].mode == PCILIB_REGISTER_R) mode = "R ";
- else if (registers[i].mode == PCILIB_REGISTER_W) mode = " W";
- else mode = " ";
-
- printf(" 0x%02x (%2i %s) %s", registers[i].addr, registers[i].bits, mode, registers[i].name);
- if ((details > 0)&&(registers[i].description)&&(registers[i].description[0])) {
- printf(": %s", registers[i].description);
- }
- printf("\n");
- }
- printf("\n");
- }
- ListProperties(handle, "/", details);
- if (bank == (char*)-1) events = NULL;
- else {
- events = model_info->events;
- types = model_info->data_types;
- }
- if (events) {
- printf("Events: \n");
- for (i = 0; events[i].name; i++) {
- printf(" %s", events[i].name);
- if ((events[i].description)&&(events[i].description[0])) {
- printf(": %s", events[i].description);
- }
-
- if (types) {
- for (j = 0; types[j].name; j++) {
- if (types[j].evid & events[i].evid) {
- printf("\n %s", types[j].name);
- if ((types[j].description)&&(types[j].description[0])) {
- printf(": %s", types[j].description);
- }
- }
- }
- }
- }
- printf("\n");
- }
- }
- void ViewInfo(pcilib_t *handle, pcilib_register_t reg, size_t id) {
- int err;
- int i;
- pcilib_value_t val = {0};
- pcilib_register_value_name_t *vnames;
- pcilib_view_t view;
- const pcilib_model_description_t *model_info = pcilib_get_model_description(handle);
- const pcilib_register_description_t *r;
- const pcilib_view_description_t *v;
- if (reg == PCILIB_REGISTER_INVALID) {
- r = NULL;
- view = id;
- } else {
- r = &model_info->registers[reg];
- view = pcilib_find_view_by_name(handle, r->views[id].view);
- }
- if (view == PCILIB_VIEW_INVALID) return;
- v = model_info->views[view];
- if (r) {
- printf(" View %s (", r->views[id].name);
- } else {
- printf("%s\n", v->name);
- printf(" Data type : ");
- }
- switch (v->type) {
- case PCILIB_TYPE_STRING:
- printf("char*");
- break;
- case PCILIB_TYPE_DOUBLE:
- printf("double");
- break;
- case PCILIB_TYPE_LONG:
- printf("long");
- break;
- default:
- printf("unknown");
- }
- if (r) printf(")");
- printf("\n");
- if (v->mode&PCILIB_ACCESS_R) {
- if (r) {
- err = pcilib_read_register_view_by_id(handle, reg, r->views[id].name, &val);
- } else {
- err = pcilib_get_property(handle, v->name, &val);
- }
- if (!err) err = pcilib_convert_value_type(handle, &val, PCILIB_TYPE_STRING);
- } else {
- err = PCILIB_ERROR_NOTPERMITED;
- }
- if (err) {
- if (err == PCILIB_ERROR_NOTPERMITED)
- printf(" Current value : no read access\n");
- else
- printf(" Current value : error %i\n", err);
- } else {
- printf(" Current value : %s", val.sval);
- if (v->unit) printf(" (units: %s)", v->unit);
- printf("\n");
- }
- if (v->unit) {
- pcilib_unit_t unit = pcilib_find_unit_by_name(handle, v->unit);
- printf(" Supported units: %s", v->unit);
- if (unit != PCILIB_UNIT_INVALID) {
- const pcilib_unit_description_t *u = &model_info->units[unit];
- for (i = 0; u->transforms[i].unit; i++)
- printf(", %s", u->transforms[i].unit);
- }
- printf("\n");
- }
- printf(" Access : ");
- if ((v->mode&PCILIB_REGISTER_RW) == 0) printf("-");
- if (v->mode&PCILIB_REGISTER_R) printf("R");
- if (v->mode&PCILIB_REGISTER_W) printf("W");
- printf("\n");
- if ((v->api == &pcilib_enum_view_static_api)||(v->api == &pcilib_enum_view_xml_api)) {
- vnames = ((pcilib_enum_view_description_t*)v)->names;
- printf(" Value aliases :");
- for (i = 0; vnames[i].name; i++) {
- if (i) printf(",");
- printf(" %s = %lu", vnames[i].name, vnames[i].value);
- if (vnames[i].min != vnames[i].max)
- printf(" (%lu - %lu)", vnames[i].min, vnames[i].max);
- }
- printf("\n");
- } else if (v->api == &pcilib_transform_view_api) {
- const pcilib_transform_view_description_t *tv = (const pcilib_transform_view_description_t*)v;
- if (tv->read_from_reg)
- printf(" Read function : %s\n", tv->read_from_reg);
- if (tv->write_to_reg)
- printf(" Write function : %s\n", tv->write_to_reg);
- }
- if (v->description)
- printf(" Description : %s\n", v->description);
- }
- void RegisterInfo(pcilib_t *handle, pcilib_register_t reg) {
- int err;
- int i;
- pcilib_register_value_t regval;
- pcilib_register_info_t *info;
- const pcilib_model_description_t *model_info = pcilib_get_model_description(handle);
- const pcilib_register_description_t *r = &model_info->registers[reg];
- pcilib_register_bank_t bank = pcilib_find_register_bank_by_addr(handle, r->bank);
- const pcilib_register_bank_description_t *b = &model_info->banks[bank];
- if (r->mode&PCILIB_ACCESS_R) {
- err = pcilib_read_register_by_id(handle, reg, ®val);
- } else {
- err = PCILIB_ERROR_NOTPERMITED;
- }
- info = pcilib_get_register_info(handle, b->name, r->name, 0);
- if (!info) Error("Can't obtain register info for %s", r->name);
- printf("%s/%s\n", b->name, r->name);
- printf(" Current value: ");
- if (err) {
- if (err == PCILIB_ERROR_NOTPERMITED) printf("no read access");
- else printf("error %i", err);
- } else printf(b->format, regval);
- if (r->mode&PCILIB_REGISTER_W) {
- printf(" (default: ");
- printf(b->format, r->defvalue);
- if (info->range) {
- printf(", range: ");
- printf(b->format, info->range->min);
- printf (" - ");
- printf(b->format, info->range->max);
- }
- printf(")");
- }
- printf("\n");
- printf(" Address : 0x%x [%u:%u]\n", r->addr, r->offset, r->offset + r->bits);
- if ((info->values)&&(info->values[0].name)) {
- printf(" Value aliases:");
- for (i = 0; info->values[i].name; i++)
- printf(" %s", info->values[i].name);
- printf("\n");
- }
- printf(" Access : ");
- if ((r->mode&PCILIB_REGISTER_RW) == 0) printf("-");
- if (r->mode&PCILIB_REGISTER_R) printf("R");
- if (r->mode&PCILIB_REGISTER_W) printf("W");
- if (r->mode&PCILIB_REGISTER_W1C) printf("/reset");
- if (r->mode&PCILIB_REGISTER_W1I) printf("/invert");
- printf("\n");
- if (r->description)
- printf(" Description : %s\n", r->description);
- if (r->views) {
- printf("\nSupported Views:\n");
- for (i = 0; r->views[i].name; i++) {
- ViewInfo(handle, reg, i);
- }
- }
- pcilib_free_register_info(handle, info);
- }
- void Version(pcilib_t *handle, const pcilib_model_description_t *model_info) {
- const pcilib_driver_version_t *driver_version;
- driver_version = pcilib_get_driver_version(handle);
- printf("pcilib version: %u.%u.%u\n",
- PCILIB_VERSION_GET_MAJOR(PCILIB_VERSION),
- PCILIB_VERSION_GET_MINOR(PCILIB_VERSION),
- PCILIB_VERSION_GET_MICRO(PCILIB_VERSION)
- );
- printf("driver version: %lu.%lu.%lu, interface: 0x%lx, registered ioctls: %lu\n",
- PCILIB_VERSION_GET_MAJOR(driver_version->version),
- PCILIB_VERSION_GET_MINOR(driver_version->version),
- PCILIB_VERSION_GET_MICRO(driver_version->version),
- driver_version->interface,
- driver_version->ioctls
- );
- if (model_info) {
- pcilib_version_t version = model_info->interface_version;
- printf("Model: %s", handle->model);
- if (version) {
- printf(", version: %u.%u.%u\n",
- PCILIB_VERSION_GET_MAJOR(version),
- PCILIB_VERSION_GET_MINOR(version),
- PCILIB_VERSION_GET_MICRO(version)
- );
- } else {
- printf(" (embedded)\n");
- }
- }
- if (model_info->dma) {
- pcilib_version_t version = model_info->dma->api->version;
- printf("DMA Engine: %s, version: %u.%u.%u\n", model_info->dma->name,
- PCILIB_VERSION_GET_MAJOR(version),
- PCILIB_VERSION_GET_MINOR(version),
- PCILIB_VERSION_GET_MICRO(version)
- );
- }
- BuildInfo();
- }
- void Info(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *target) {
- int i, j;
- DIR *dir;
- void *plugin;
- const char *path;
- struct dirent *entry;
- const pcilib_model_description_t *info = NULL;
- const pcilib_board_info_t *board_info = pcilib_get_board_info(handle);
- const pcilib_pcie_link_info_t *link_info = pcilib_get_pcie_link_info(handle);
- int have_state;
- pcilib_device_state_t state;
- path = getenv("PCILIB_PLUGIN_DIR");
- if (!path) path = PCILIB_PLUGIN_DIR;
- have_state = !pcilib_get_device_state(handle, &state);
- if (board_info)
- 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);
- if (link_info) {
- printf(" PCIe x%u (gen%u), DMA Payload: %u (of %u)", link_info->link_width, link_info->link_speed, 1<<link_info->payload, 1<<link_info->max_payload);
- if (have_state) {
- int bits = 0;
- unsigned long mask;
- for (mask = state.dma_mask; mask&1; mask>>=1) bits++;
- printf(", DMA Mask: ");
- if (mask) printf("0x%lx", state.dma_mask);
- else printf("%u bits", bits);
- printf(", IOMMU: %s", state.iommu?"on":"off");
- }
- printf("\n");
- }
- if (board_info)
- printf(" Interrupt - Pin: %i, Line: %i\n", board_info->interrupt_pin, board_info->interrupt_line);
- printf("\n");
- if (target) {
- if (*target == '/') {
- pcilib_view_t view;
- view = pcilib_find_view_by_name(handle, target);
- if (view != PCILIB_VIEW_INVALID)
- return ViewInfo(handle, PCILIB_REGISTER_INVALID, view);
- Error(" No property %s is found", target);
- } else {
- pcilib_register_t reg;
-
- reg = pcilib_find_register(handle, NULL, target);
- if (reg != PCILIB_REGISTER_INVALID)
- return RegisterInfo(handle, reg);
- Error(" No register %s is found", target);
- }
-
- }
- List(handle, model_info, (char*)-1, 0);
- printf("Available models:\n");
- dir = opendir(path);
- if (dir) {
- while ((entry = readdir(dir))) {
- const char *suffix = strstr(entry->d_name, ".so");
- if ((!suffix)||(strlen(suffix) != 3)) continue;
- plugin = pcilib_plugin_load(entry->d_name);
- if (plugin) {
- info = pcilib_get_plugin_model(handle, plugin, 0, 0, NULL);
- if (info) {
- printf(" %s\n", entry->d_name);
- for (j = 0; info[j].name; j++) {
- pcilib_version_t version = info[j].api?info[j].api->version:0;
- printf(" %-12s %u.%u.%u - %s\n", info[j].name,
- PCILIB_VERSION_GET_MAJOR(version),
- PCILIB_VERSION_GET_MINOR(version),
- PCILIB_VERSION_GET_MICRO(version),
- info[j].description?info[j].description:"");
- }
- }
- pcilib_plugin_close(plugin);
- } else {
- const char *msg = dlerror();
- if (msg)
- printf(" %s: %s\n", entry->d_name, msg);
- }
- }
- closedir(dir);
- }
- // printf(" XML\n");
- printf(" Internal Models\n");
- for (i = 0; pcilib_dma[i].api; i++)
- printf(" %-12s - %s\n", pcilib_dma[i].name, pcilib_dma[i].description?pcilib_dma[i].description:"");
- printf(" %-12s - Plain PCI-access model\n\n", "pci");
- }
- #define BENCH_MAX_DMA_SIZE 4 * 1024 * 1024
- #define BENCH_MAX_FIFO_SIZE 1024 * 1024
- 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) {
- int err;
- int i, j, errors;
- void *data, *buf, *check;
- void *fifo = NULL;
- struct timeval start, end;
- unsigned long time;
- size_t size, min_size, max_size;
- double mbs_in, mbs_out, mbs;
- size_t irqs;
-
- const pcilib_board_info_t *board_info = pcilib_get_board_info(handle);
-
- if (mode == ACCESS_CONFIG)
- Error("No benchmarking of configuration space acess is allowed");
- if (mode == ACCESS_DMA) {
- if (n) {
- min_size = n * access;
- max_size = n * access;
- } else {
- min_size = 1024;
- max_size = BENCH_MAX_DMA_SIZE;
- }
-
- for (size = min_size; size <= max_size; size *= 4) {
- mbs_in = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_FROM_DEVICE);
- mbs_out = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_TO_DEVICE);
- mbs = pcilib_benchmark_dma(handle, dma, addr, size, iterations, PCILIB_DMA_BIDIRECTIONAL);
- err = pcilib_wait_irq(handle, 0, 0, &irqs);
- if (err) irqs = 0;
-
- printf("%8zu KiB - ", size / 1024);
-
- printf("RW: ");
- if (mbs < 0) printf("failed ... ");
- else printf("%8.2lf MiB/s", mbs);
- printf(", R: ");
- if (mbs_in < 0) printf("failed ... ");
- else printf("%8.2lf MiB/s", mbs_in);
- printf(", W: ");
- if (mbs_out < 0) printf("failed ... ");
- else printf("%8.2lf MiB/s", mbs_out);
-
- if (irqs) {
- printf(", IRQs: %lu", irqs);
- }
- printf("\n");
- }
-
- return 0;
- }
- if (bar == PCILIB_BAR_INVALID) {
- unsigned long maxlength = 0;
-
- for (i = 0; i < PCILIB_MAX_REGISTER_BANKS; i++) {
- if ((addr >= board_info->bar_start[i])&&((board_info->bar_start[i] + board_info->bar_length[i]) >= (addr + access))) {
- bar = i;
- break;
- }
- if (board_info->bar_length[i] > maxlength) {
- maxlength = board_info->bar_length[i];
- bar = i;
- }
- }
-
- if (bar < 0) Error("Data banks are not available");
- }
-
- if (n) {
- 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]);
- min_size = n * access;
- max_size = n * access;
- } else {
- min_size = access;
- if (mode == ACCESS_BAR) max_size = board_info->bar_length[bar];
- else max_size = BENCH_MAX_FIFO_SIZE;
- }
-
- err = posix_memalign( (void**)&buf, 256, max_size );
- if (!err) err = posix_memalign( (void**)&check, 256, max_size );
- if ((err)||(!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", max_size);
- if (mode == ACCESS_FIFO) {
- fifo = pcilib_resolve_bar_address(handle, bar, addr);
- if (!fifo) Error("Can't resolve address (%lx) in bar (%u)", addr, bar);
- } else {
- data = pcilib_resolve_bar_address(handle, bar, 0);
- if (!data) Error("Can't resolve start of bar (%u)", bar);
- }
- if (mode == ACCESS_FIFO)
- printf("Transfer time (Bank: %i, Fifo: %lx):\n", bar, addr);
- else
- printf("Transfer time (Bank: %i):\n", bar);
-
- for (size = min_size ; size < max_size; size *= 8) {
- gettimeofday(&start,NULL);
- if (mode == ACCESS_BAR) {
- for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
- pcilib_memcpy(buf, data, access, size / access);
- }
- } else {
- for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
- for (j = 0; j < (size/access); j++) {
- pcilib_memcpy(buf + j * access, fifo, access, 1);
- }
- }
- }
- gettimeofday(&end,NULL);
- time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
- printf("%8zu bytes - read: %8.2lf MiB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
-
- fflush(0);
- gettimeofday(&start,NULL);
- if (mode == ACCESS_BAR) {
- for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
- pcilib_memcpy(data, buf, access, size / access);
- }
- } else {
- for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
- for (j = 0; j < (size/access); j++) {
- pcilib_memcpy(fifo, buf + j * access, access, 1);
- }
- }
- }
- gettimeofday(&end,NULL);
- time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
- printf(", write: %8.2lf MiB/s\n", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
- }
-
- printf("\n\nOpen-Transfer-Close time: \n");
-
- for (size = 4 ; size < max_size; size *= 8) {
- gettimeofday(&start,NULL);
- if (mode == ACCESS_BAR) {
- for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
- pcilib_read(handle, bar, 0, access, size / access, buf);
- }
- } else {
- for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
- pcilib_read_fifo(handle, bar, addr, access, size / access, buf);
- }
- }
- gettimeofday(&end,NULL);
- time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
- printf("%8zu bytes - read: %8.2lf MiB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
-
- fflush(0);
- gettimeofday(&start,NULL);
- if (mode == ACCESS_BAR) {
- for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
- pcilib_write(handle, bar, 0, access, size / access, buf);
- }
- } else {
- for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
- pcilib_write_fifo(handle, bar, addr, access, size / access, buf);
- }
- }
-
- gettimeofday(&end,NULL);
- time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
- printf(", write: %8.2lf MiB/s", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
- if (mode == ACCESS_BAR) {
- gettimeofday(&start,NULL);
- for (i = 0, errors = 0; i < BENCHMARK_ITERATIONS; i++) {
- pcilib_write(handle, bar, 0, access, size / access, buf);
- pcilib_read(handle, bar, 0, access, size / access, check);
- if (memcmp(buf, check, size)) ++errors;
- }
- gettimeofday(&end,NULL);
- time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
- printf(", write-verify: %8.2lf MiB/s", 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
- if (errors) printf(", errors: %u of %u", errors, BENCHMARK_ITERATIONS);
- }
- printf("\n");
- }
-
- printf("\n\n");
- free(check);
- free(buf);
-
- return 0;
- }
- #define pci2host16(endianess, value) endianess?
- /*
- typedef struct {
- size_t size;
- void *data;
- size_t pos;
- int multi_mode;
- } DMACallbackContext;
- static int DMACallback(void *arg, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
- DMACallbackContext *ctx = (DMACallbackContext*)arg;
-
- if ((ctx->pos + bufsize > ctx->size)||(!ctx->data)) {
- ctx->size *= 2;
- ctx->data = realloc(ctx->data, ctx->size);
- if (!ctx->data) {
- Error("Allocation of %i bytes of memory have failed", ctx->size);
- return 0;
- }
- }
-
- memcpy(ctx->data + ctx->pos, buf, bufsize);
- ctx->pos += bufsize;
- if (flags & PCILIB_DMA_FLAG_EOP) return 0;
- return 1;
- }
- */
- 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) {
- void *buf;
- int i, err;
- size_t ret, bytes;
- size_t size = n * abs(access);
- int block_width, blocks_per_line;
- int numbers_per_block, numbers_per_line;
- pcilib_dma_engine_t dmaid;
- pcilib_dma_flags_t dma_flags = 0;
- int fd;
- char stmp[256];
- struct stat st;
- const pcilib_board_info_t *board_info;
- numbers_per_block = BLOCK_SIZE / access;
- block_width = numbers_per_block * ((access * 2) + SEPARATOR_WIDTH);
- blocks_per_line = (LINE_WIDTH - 10) / (block_width + BLOCK_SEPARATOR_WIDTH);
- if ((blocks_per_line > 1)&&(blocks_per_line % 2)) --blocks_per_line;
- numbers_per_line = blocks_per_line * numbers_per_block;
- if (size) {
- buf = malloc(size);
- if (!buf) Error("Allocation of %zu bytes of memory has failed", size);
- } else {
- buf = NULL;
- }
-
- switch (mode) {
- case ACCESS_DMA:
- if (timeout == (size_t)-1) timeout = PCILIB_DMA_TIMEOUT;
-
- dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_FROM_DEVICE, dma);
- if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (%lu) is specified", dma);
-
- if (flags&FLAG_MULTIPACKET) dma_flags |= PCILIB_DMA_FLAG_MULTIPACKET;
- if (flags&FLAG_WAIT) dma_flags |= PCILIB_DMA_FLAG_WAIT;
-
- if (size) {
- err = pcilib_read_dma_custom(handle, dmaid, addr, size, dma_flags, timeout, buf, &bytes);
- if (err) Error("Error (%i) is reported by DMA engine", err);
- } else {
- dma_flags |= PCILIB_DMA_FLAG_IGNORE_ERRORS;
-
- size = 2048; bytes = 0;
- do {
- if ((size - bytes) < 4096) {
- size *= 2;
- buf = realloc(buf, size);
- if (!buf) Error("Allocation of %zu bytes of memory has failed", size);
- }
- err = pcilib_read_dma_custom(handle, dmaid, addr, size - bytes, dma_flags, timeout, buf + bytes, &ret);
- bytes += ret;
-
- if ((!err)&&(flags&FLAG_MULTIPACKET)) {
- err = PCILIB_ERROR_TOOBIG;
- if ((flags&FLAG_WAIT)==0) timeout = 0;
- }
- } while (err == PCILIB_ERROR_TOOBIG);
- }
- if ((err)&&(err != PCILIB_ERROR_TIMEOUT)) {
- Error("Error (%i) during DMA read", err);
- }
- if (bytes <= 0) {
- pcilib_warning("No data is returned by DMA engine");
- return -1;
- }
- size = bytes;
- n = bytes / abs(access);
- addr = 0;
- break;
- case ACCESS_FIFO:
- pcilib_read_fifo(handle, bar, addr, access, n, buf);
- addr = 0;
- break;
- case ACCESS_CONFIG:
- board_info = pcilib_get_board_info(handle);
- sprintf(stmp, "/sys/bus/pci/devices/0000:%02x:%02x.%1x/config", board_info->bus, board_info->slot, board_info->func);
- fd = open(stmp, O_RDONLY);
- if ((!fd)||(fstat(fd, &st))) Error("Can't open %s", stmp);
-
- if (st.st_size < addr)
- Error("Access beyond the end of PCI configuration space");
-
- if (st.st_size < (addr + size)) {
- n = (st.st_size - addr) / abs(access);
- size = n * abs(access);
- if (!n) Error("Access beyond the end of PCI configuration space");
- }
-
- lseek(fd, addr, SEEK_SET);
- ret = read(fd, buf, size);
- if (ret == (size_t)-1) Error("Error reading %s", stmp);
-
- if (ret < size) {
- size = ret;
- n = ret / abs(access);
- }
-
- close(fd);
- break;
- default:
- pcilib_read(handle, bar, addr, access, size / access, buf);
- }
-
- if (endianess) pcilib_swap(buf, buf, abs(access), n);
- if (o) {
- printf("Writting output (%zu bytes) to file (append to the end)...\n", n * abs(access));
- fwrite(buf, abs(access), n, o);
- } else {
- for (i = 0; i < n; i++) {
- if (i) {
- if (i%numbers_per_line == 0) printf("\n");
- else {
- printf("%*s", SEPARATOR_WIDTH, "");
- if (i%numbers_per_block == 0) printf("%*s", BLOCK_SEPARATOR_WIDTH, "");
- }
- }
-
- if (i%numbers_per_line == 0) printf("%8lx: ", addr + i * abs(access));
- switch (access) {
- case 1: printf("%0*hhx", access * 2, ((uint8_t*)buf)[i]); break;
- case 2: printf("%0*hx", access * 2, ((uint16_t*)buf)[i]); break;
- case 4: printf("%0*x", access * 2, ((uint32_t*)buf)[i]); break;
- case 8: printf("%0*lx", access * 2, ((uint64_t*)buf)[i]); break;
- }
- }
- printf("\n\n");
- }
-
- free(buf);
- return 0;
- }
- 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) {
- int i;
- int err;
- const char *format;
- pcilib_register_bank_t bank_id;
- pcilib_register_bank_addr_t bank_addr = 0;
- pcilib_register_value_t value;
- // Adding DMA registers
- pcilib_get_dma_description(handle);
- if (reg||view||attr) {
- pcilib_value_t val = {0};
- if (attr) {
- if (reg) err = pcilib_get_register_attr(handle, bank, reg, attr, &val);
- else if (view) err = pcilib_get_property_attr(handle, view, attr, &val);
- else if (bank) err = pcilib_get_register_bank_attr(handle, bank, attr, &val);
- else err = PCILIB_ERROR_INVALID_ARGUMENT;
- if (err) {
- if (err == PCILIB_ERROR_NOTFOUND)
- Error("Attribute %s is not found", attr);
- else
- Error("Error (%i) reading attribute %s", err, attr);
- }
- err = pcilib_convert_value_type(handle, &val, PCILIB_TYPE_STRING);
- if (err) Error("Error converting attribute %s to string", attr);
- printf("%s = %s", attr, val.sval);
- if ((val.unit)&&(strcasecmp(val.unit, "name")))
- printf(" %s", val.unit);
- printf(" (for %s)\n", (reg?reg:(view?view:bank)));
- } else if (view) {
- if (reg) {
- err = pcilib_read_register_view(handle, bank, reg, view, &val);
- if (err) Error("Error reading view %s of register %s", view, reg);
- } else {
- err = pcilib_get_property(handle, view, &val);
- if (err) Error("Error reading property %s", view);
- }
- if (unit) {
- err = pcilib_convert_value_unit(handle, &val, unit);
- if (err) {
- if (reg) Error("Error converting view %s of register %s to unit %s", view, reg, unit);
- else Error("Error converting property %s to unit %s", view, unit);
- }
- }
-
- err = pcilib_convert_value_type(handle, &val, PCILIB_TYPE_STRING);
- if (err) {
- if (reg) Error("Error converting view %s of register %s to string", view);
- else Error("Error converting property %s to string", view);
- }
- printf("%s = %s", (reg?reg:view), val.sval);
- if ((val.unit)&&(strcasecmp(val.unit, "name")))
- printf(" %s", val.unit);
- printf("\n");
- } else {
- pcilib_register_t regid = pcilib_find_register(handle, bank, reg);
- bank_id = pcilib_find_register_bank_by_addr(handle, model_info->registers[regid].bank);
- format = model_info->banks[bank_id].format;
- if (!format) format = "%lu";
- err = pcilib_read_register_by_id(handle, regid, &value);
- if (err) Error("Error reading register %s", reg);
- printf("%s = ", reg);
- printf(format, value);
- printf("\n");
- }
- } else {
- if (model_info->registers) {
- if (bank) {
- bank_id = pcilib_find_register_bank(handle, bank);
- bank_addr = model_info->banks[bank_id].addr;
- }
-
- printf("Registers:\n");
- for (i = 0; model_info->registers[i].bits; i++) {
- 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)) {
- bank_id = pcilib_find_register_bank_by_addr(handle, model_info->registers[i].bank);
- format = model_info->banks[bank_id].format;
- if (!format) format = "%lu";
- err = pcilib_read_register_by_id(handle, i, &value);
- if (err) printf(" %s = error reading value", model_info->registers[i].name);
- else {
- printf(" %s = ", model_info->registers[i].name);
- printf(format, value);
- }
- printf(" [");
- printf(format, model_info->registers[i].defvalue);
- printf("]");
- printf("\n");
- }
- }
- } else {
- printf("No registers");
- }
- printf("\n");
- }
- return 0;
- }
- #define WRITE_REGVAL(buf, n, access, o) {\
- uint##access##_t tbuf[n]; \
- for (i = 0; i < n; i++) { \
- tbuf[i] = (uint##access##_t)buf[i]; \
- } \
- fwrite(tbuf, access/8, n, o); \
- }
- 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) {
- int err;
- int i;
- const pcilib_register_bank_description_t *banks = model_info->banks;
- pcilib_register_bank_t bank_id = pcilib_find_register_bank(handle, bank);
- if (bank_id == PCILIB_REGISTER_BANK_INVALID) {
- if (bank) Error("Invalid register bank is specified (%s)", bank);
- else Error("Register bank should be specified");
- }
- int access = banks[bank_id].access / 8;
- // int size = n * abs(access);
- int block_width, blocks_per_line;
- int numbers_per_block, numbers_per_line;
-
- numbers_per_block = BLOCK_SIZE / access;
- block_width = numbers_per_block * ((access * 2) + SEPARATOR_WIDTH);
- blocks_per_line = (LINE_WIDTH - 6) / (block_width + BLOCK_SEPARATOR_WIDTH);
- if ((blocks_per_line > 1)&&(blocks_per_line % 2)) --blocks_per_line;
- numbers_per_line = blocks_per_line * numbers_per_block;
- pcilib_register_value_t buf[n];
- err = pcilib_read_register_space(handle, bank, addr, n, buf);
- if (err) Error("Error reading register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n);
- if (o) {
- printf("Writting output (%zu bytes) to file (append to the end)...\n", n * abs(access));
- switch (access) {
- case 1: WRITE_REGVAL(buf, n, 8, o) break;
- case 2: WRITE_REGVAL(buf, n, 16, o) break;
- case 4: WRITE_REGVAL(buf, n, 32, o) break;
- case 8: WRITE_REGVAL(buf, n, 64, o) break;
- }
- } else {
- for (i = 0; i < n; i++) {
- if (i) {
- if (i%numbers_per_line == 0) printf("\n");
- else {
- printf("%*s", SEPARATOR_WIDTH, "");
- if (i%numbers_per_block == 0) printf("%*s", BLOCK_SEPARATOR_WIDTH, "");
- }
- }
-
- if (i%numbers_per_line == 0) printf("%4lx: ", addr + 4 * i - addr_shift);
- printf("%0*lx", access * 2, (unsigned long)buf[i]);
- }
- printf("\n\n");
- }
-
- return 0;
- }
- 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) {
- int read_back = 0;
- void *buf, *check;
- int res = 0, i, err;
- int size = n * abs(access);
- size_t ret;
- pcilib_dma_engine_t dmaid;
- if (mode == ACCESS_CONFIG)
- Error("Writting to PCI configuration space is not supported");
- err = posix_memalign( (void**)&buf, 256, size );
- if (!err) err = posix_memalign( (void**)&check, 256, size );
- if ((err)||(!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", size);
- for (i = 0; i < n; i++) {
- switch (access) {
- case 1: res = sscanf(data[i], "%hhx", ((uint8_t*)buf)+i); break;
- case 2: res = sscanf(data[i], "%hx", ((uint16_t*)buf)+i); break;
- case 4: res = sscanf(data[i], "%x", ((uint32_t*)buf)+i); break;
- case 8: res = sscanf(data[i], "%lx", ((uint64_t*)buf)+i); break;
- default: Error("Unexpected data size (%lu)", access);
- }
- if ((res != 1)||(!isxnumber(data[i]))) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]);
- }
- if (endianess) pcilib_swap(buf, buf, abs(access), n);
- switch (mode) {
- case ACCESS_DMA:
- dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_TO_DEVICE, dma);
- if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (%lu) is specified", dma);
- err = pcilib_write_dma(handle, dmaid, addr, size, buf, &ret);
- if ((err)||(ret != size)) {
- if (err == PCILIB_ERROR_TIMEOUT) Error("Timeout writting the data to DMA");
- else if (err) Error("DMA engine returned a error while writing the data");
- else if (!ret) Error("No data is written by DMA engine");
- else Error("Only %lu bytes of %lu is written by DMA engine", ret, size);
- }
- break;
- case ACCESS_FIFO:
- pcilib_write_fifo(handle, bar, addr, access, n, buf);
- break;
- default:
- pcilib_write(handle, bar, addr, access, size / access, buf);
- if (verify) {
- pcilib_read(handle, bar, addr, access, size / access, check);
- read_back = 1;
- }
- }
- if ((read_back)&&(memcmp(buf, check, size))) {
- printf("Write failed: the data written and read differ, the foolowing is read back:\n");
- if (endianess) pcilib_swap(check, check, abs(access), n);
- ReadData(handle, mode, 0, dma, bar, addr, n, access, endianess, (size_t)-1, NULL);
- exit(-1);
- }
- free(check);
- free(buf);
-
- return 0;
- }
- 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) {
- pcilib_register_value_t *buf, *check;
- int res, i, err;
- unsigned long value;
- int size = n * sizeof(pcilib_register_value_t);
- err = posix_memalign( (void**)&buf, 256, size );
- if (!err) err = posix_memalign( (void**)&check, 256, size );
- if ((err)||(!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", size);
- for (i = 0; i < n; i++) {
- res = sscanf(data[i], "%lx", &value);
- if ((res != 1)||(!isxnumber(data[i]))) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]);
- buf[i] = value;
- }
- err = pcilib_write_register_space(handle, bank, addr, n, buf);
- if (err) Error("Error writting register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n);
-
- err = pcilib_read_register_space(handle, bank, addr, n, check);
- if (err) Error("Error reading register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n);
- if (memcmp(buf, check, size)) {
- printf("Write failed: the data written and read differ, the foolowing is read back:\n");
- ReadRegisterRange(handle, model_info, bank, addr, addr_shift, n, NULL);
- exit(-1);
- }
- free(check);
- free(buf);
-
- return 0;
- }
- 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) {
- int err = 0;
- pcilib_value_t val = {0};
- pcilib_register_value_t value, verify;
- /*
- pcilib_register_bank_t bank_id;
- pcilib_register_bank_addr_t bank_addr;
- bank_id = pcilib_find_bank_by_addr(handle, model_info->registers[regid].bank);
- if (bank_id == PCILIB_REGISTER_BANK_INVALID) Error("Can't find bank of the register (%s)", reg);
- format = model_info->banks[bank_id].format;
- if (!format) format = "%lu";
- */
- err = pcilib_set_value_from_static_string(handle, &val, *data);
- if (err) Error("Error (%i) setting value", err);
- if (view) {
- if (unit)
- val.unit = unit;
- if (reg) {
- err = pcilib_write_register_view(handle, bank, reg, view, &val);
- if (err) Error("Error writting view %s of register %s", view, reg);
- printf("%s is written\n ", reg);
- } else {
- err = pcilib_set_property(handle, view, &val);
- if (err) Error("Error setting property %s", view);
- printf("%s is written\n ", view);
- }
- } else {
- pcilib_register_t regid = pcilib_find_register(handle, bank, reg);
- if (regid == PCILIB_REGISTER_INVALID) Error("Can't find register (%s) from bank (%s)", reg, bank?bank:"autodetected");
- value = pcilib_get_value_as_register_value(handle, &val, &err);
- if (err) Error("Error (%i) parsing data value (%s)", *data);
- err = pcilib_write_register(handle, bank, reg, value);
- if (err) Error("Error writting register %s\n", reg);
- if ((model_info->registers[regid].mode&(PCILIB_REGISTER_RW|PCILIB_REGISTER_INCONSISTENT)) == PCILIB_REGISTER_RW) {
- const char *format = (val.format?val.format:"%u");
-
- err = pcilib_read_register(handle, bank, reg, &verify);
- if (err) Error("Error reading back register %s for verification\n", reg);
-
- if (verify != value) {
- Error("Failed to write register %s: %lu is written and %lu is read back", reg, value, verify);
- } else {
- printf("%s = ", reg);
- printf(format, verify);
- printf("\n");
- }
- } else {
- printf("%s is written\n ", reg);
- }
- }
- return 0;
- }
- typedef struct {
- pcilib_t *handle;
- pcilib_event_t event;
- pcilib_event_data_type_t data;
- fastwriter_t *writer;
- int verbose;
- pcilib_timeout_t timeout;
- size_t run_time;
- size_t trigger_time;
- size_t max_triggers;
- pcilib_event_flags_t flags;
- FORMAT format;
-
- volatile int event_pending; /**< Used to detect that we have read previously triggered event */
- volatile int trigger_thread_started; /**< Indicates that trigger thread is ready and we can't procced to start event recording */
- volatile int started; /**< Indicates that recording is started */
-
- volatile int run_flag;
- volatile int writing_flag;
- struct timeval first_frame;
- struct timeval last_frame;
- size_t last_num, last_id;
- size_t trigger_failed;
- size_t trigger_count;
- size_t event_count; /**< Total number of events (including bad ones, but excluding events expected, but not reported by hardware) */
- size_t incomplete_count; /**< Broken events, we even can't extract appropriate block of raw data */
- size_t broken_count; /**< Broken events, error while decoding in the requested format */
- size_t empty_count; /**< Broken events, no associated data or unknown */
- size_t missing_count; /**< Missing events, not received from the hardware */
- size_t dropped_count; /**< Missing events, dropped due slow decoding/copying performance */
- size_t storage_count; /**< Missing events, dropped due to slowness of the storage subsystem */
-
- struct timeval start_time;
- struct timeval stop_time;
- } GRABContext;
- int GrabCallback(pcilib_event_id_t event_id, const pcilib_event_info_t *info, void *user) {
- int err = 0;
- void *data;
- size_t size;
-
- GRABContext *ctx = (GRABContext*)user;
- pcilib_t *handle = ctx->handle;
- gettimeofday(&ctx->last_frame, NULL);
- if (!ctx->event_count) {
- memcpy(&ctx->first_frame, &ctx->last_frame, sizeof(struct timeval));
- }
- ctx->event_pending = 0;
- ctx->event_count++;
- if (ctx->last_num) {
- size_t missing_count = (info->seqnum - ctx->last_num) - 1;
- ctx->missing_count += missing_count;
- #ifdef PCILIB_DEBUG_MISSING_EVENTS
- if (missing_count)
- 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);
- #endif /* PCILIB_DEBUG_MISSING_EVENTS */
- }
- ctx->last_num = info->seqnum;
- ctx->last_id = event_id;
- if (info->flags&PCILIB_EVENT_INFO_FLAG_BROKEN) {
- ctx->incomplete_count++;
- return PCILIB_STREAMING_CONTINUE;
- }
- data = pcilib_get_data(handle, event_id, ctx->data, &size);
- if (!data) {
- int err = (int)size;
- switch (err) {
- case PCILIB_ERROR_OVERWRITTEN:
- ctx->dropped_count++;
- break;
- case PCILIB_ERROR_INVALID_DATA:
- ctx->broken_count++;
- break;
- default:
- ctx->empty_count++;
- }
- return PCILIB_STREAMING_CONTINUE;
- }
- if (ctx->format == FORMAT_HEADER) {
- uint64_t header[8];
- header[0] = info->type;
- header[1] = ctx->data;
- header[2] = 0;
- header[3] = size;
- header[4] = info->seqnum;
- header[5] = info->offset;
- memcpy(header + 6, &info->timestamp, 16);
-
- err = fastwriter_push(ctx->writer, 64, header);
- }
- if (!err)
- err = fastwriter_push(ctx->writer, size, data);
- if (err) {
- fastwriter_cancel(ctx->writer);
- if (err != EWOULDBLOCK)
- Error("Storage error %i", err);
- ctx->storage_count++;
- pcilib_return_data(handle, event_id, ctx->data, data);
- return PCILIB_STREAMING_CONTINUE;
- }
- err = pcilib_return_data(handle, event_id, ctx->data, data);
- if (err) {
- ctx->dropped_count++;
- fastwriter_cancel(ctx->writer);
- return PCILIB_STREAMING_CONTINUE;
- }
- err = fastwriter_commit(ctx->writer);
- if (err) Error("Error commiting data to storage, Error: %i", err);
-
- return PCILIB_STREAMING_CONTINUE;
- }
- int raw_data(pcilib_event_id_t event_id, const pcilib_event_info_t *info, pcilib_event_flags_t flags, size_t size, void *data, void *user) {
- int err;
- GRABContext *ctx = (GRABContext*)user;
- // pcilib_t *handle = ctx->handle;
- if ((info)&&(info->seqnum != ctx->last_num)) {
- gettimeofday(&ctx->last_frame, NULL);
- if (!ctx->event_count) {
- memcpy(&ctx->first_frame, &ctx->last_frame, sizeof(struct timeval));
- }
- ctx->event_count++;
- if (ctx->last_num) {
- size_t missing_count = (info->seqnum - ctx->last_num) - 1;
- ctx->missing_count += missing_count;
- #ifdef PCILIB_DEBUG_MISSING_EVENTS
- if (missing_count)
- pcilib_debug(MISSING_EVENTS, "%zu missing events between %zu and %zu", missing_count, ctx->last_num, info->seqnum);
- #endif /* PCILIB_DEBUG_MISSING_EVENTS */
- }
- ctx->last_num = info->seqnum;
- }
- err = fastwriter_push_data(ctx->writer, size, data);
- if (err) {
- if (err == EWOULDBLOCK) Error("Storage is not able to handle the data stream, buffer overrun");
- Error("Storage error %i", err);
- }
-
- return PCILIB_STREAMING_CONTINUE;
- }
- void *Trigger(void *user) {
- int err;
- struct timeval start;
- GRABContext *ctx = (GRABContext*)user;
- size_t trigger_time = ctx->trigger_time;
- size_t max_triggers = ctx->max_triggers;
-
- ctx->trigger_thread_started = 1;
- ctx->event_pending = 1;
- while (!ctx->started) ;
- gettimeofday(&start, NULL);
- do {
- err = pcilib_trigger(ctx->handle, ctx->event, 0, NULL);
- if (err) ctx->trigger_failed++;
- if ((++ctx->trigger_count == max_triggers)&&(max_triggers)) break;
-
- if (trigger_time) {
- pcilib_add_timeout(&start, trigger_time);
- if ((ctx->stop_time.tv_sec)&&(pcilib_timecmp(&start, &ctx->stop_time)>0)) break;
- pcilib_sleep_until_deadline(&start);
- } else {
- while ((ctx->event_pending)&&(ctx->run_flag)) usleep(10);
- ctx->event_pending = 1;
- }
- } while (ctx->run_flag);
- ctx->trigger_thread_started = 0;
- return NULL;
- }
- void GrabStats(GRABContext *ctx, struct timeval *end_time) {
- int verbose;
- pcilib_timeout_t duration, fps_duration;
- struct timeval cur;
- double fps = 0, good_fps = 0;
- size_t total, good, pending = 0;
- verbose = ctx->verbose;
-
- if (end_time) {
- if (verbose++) {
- printf("-------------------------------------------------------------------------------\n");
- }
- } else {
- gettimeofday(&cur, NULL);
- end_time = &cur;
- }
- // if ((ctx->event_count + ctx->missing_count) == 0)
- // return;
-
- duration = pcilib_timediff(&ctx->start_time, end_time);
- fps_duration = pcilib_timediff(&ctx->first_frame, &ctx->last_frame);
-
- if (ctx->trigger_count) {
- total = ctx->trigger_count;
- pending = ctx->trigger_count - ctx->event_count - ctx->missing_count - ctx->trigger_failed;
- } else {
- total = ctx->event_count + ctx->missing_count;
- }
-
- good = ctx->event_count - ctx->broken_count - ctx->incomplete_count - ctx->storage_count - ctx->empty_count - ctx->dropped_count;
- if (ctx->event_count > 1) {
- fps = (ctx->event_count - 1) / (1.*fps_duration/1000000);
- }
-
- if (good > 1) {
- good_fps = (good - 1) / (1.*fps_duration/1000000);
- }
- printf("Run: ");
- PrintTime(duration);
-
- if (ctx->trigger_count) {
- printf(", Triggers: ");
- PrintNumber(ctx->trigger_count);
- }
-
- printf(", Captured: ");
- PrintNumber(ctx->event_count);
- printf(" FPS %5.0lf", fps);
- if ((ctx->flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY) == 0) {
- printf(", Stored: ");
- PrintNumber(good);
- printf(" FPS %5.0lf", good_fps);
- }
- printf("\n");
- if (verbose > 2) {
- if (ctx->trigger_count) {
- printf("Trig: ");
- PrintNumber(ctx->trigger_count);
- printf(" Issued: ");
- PrintNumber(ctx->trigger_count - ctx->trigger_failed);
- printf(" (");
- PrintPercent(ctx->trigger_count - ctx->trigger_failed, ctx->trigger_count);
- printf("%%) Failed: ");
- PrintNumber(ctx->trigger_failed);
- printf( " (");
- PrintPercent(ctx->trigger_failed, ctx->trigger_count);
- printf( "%%); Pending: ");
- PrintNumber(pending);
- printf( " (");
- PrintPercent(pending, ctx->trigger_count);
- printf( "%%)\n");
- }
-
- if (ctx->flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY) {
- printf("Captured: ");
- PrintNumber(good);
- } else {
- printf("Good: ");
- PrintNumber(good);
- printf(", Dropped: ");
- PrintNumber(ctx->dropped_count + ctx->storage_count);
- printf(", Bad: ");
- PrintNumber(ctx->incomplete_count + ctx->broken_count);
- printf(", Empty: ");
- PrintNumber(ctx->empty_count);
- }
- printf(", Lost: ");
- PrintNumber(ctx->missing_count);
- printf("\n");
- }
- if (verbose > 1) {
- if (ctx->flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY) {
- printf("Captured: ");
- PrintPercent(good, total);
- } else {
- printf("Good: ");
- PrintPercent(good, total);
- printf("%% Dropped: ");
- PrintPercent(ctx->dropped_count + ctx->storage_count, total);
- printf("%% Bad: ");
- PrintPercent(ctx->incomplete_count + ctx->broken_count, total);
- printf("%% Empty: ");
- PrintPercent(ctx->empty_count, total);
- }
- printf("%% Lost: ");
- PrintPercent(ctx->missing_count, total);
- printf("%%");
- printf("\n");
- }
- }
- void StorageStats(GRABContext *ctx) {
- int err;
- fastwriter_stats_t st;
- pcilib_timeout_t duration;
- struct timeval cur;
- gettimeofday(&cur, NULL);
- duration = pcilib_timediff(&ctx->start_time, &cur);
-
- err = fastwriter_get_stats(ctx->writer, &st);
- if (err) return;
- printf("Wrote ");
- PrintSize(st.written);
- printf(" of ");
- PrintSize(st.commited);
- printf(" at ");
- PrintSize(1000000.*st.written / duration);
- printf("/s, %6.2lf%% ", 100.*st.buffer_used / st.buffer_size);
- printf(" of ");
- PrintSize(st.buffer_size);
- printf(" buffer (%6.2lf%% max)\n", 100.*st.buffer_max / st.buffer_size);
- }
- void *Monitor(void *user) {
- struct timeval deadline;
- struct timeval nextinfo;
-
- GRABContext *ctx = (GRABContext*)user;
- int verbose = ctx->verbose;
- pcilib_timeout_t timeout = ctx->timeout;
-
-
- if (timeout == PCILIB_TIMEOUT_INFINITE) timeout = 0;
- // while (!ctx->started);
-
- if (timeout) {
- memcpy(&deadline, (struct timeval*)&ctx->last_frame, sizeof(struct timeval));
- pcilib_add_timeout(&deadline, timeout);
- }
-
- if (verbose > 0) {
- pcilib_calc_deadline(&nextinfo, STATUS_MESSAGE_INTERVAL*1000000);
- }
-
- while (ctx->run_flag) {
- if (StopFlag) {
- pcilib_stop(ctx->handle, PCILIB_EVENT_FLAG_STOP_ONLY);
- break;
- }
-
- if (timeout) {
- if (pcilib_calc_time_to_deadline(&deadline) == 0) {
- memcpy(&deadline, (struct timeval*)&ctx->last_frame, sizeof(struct timeval));
- pcilib_add_timeout(&deadline, timeout);
-
- if (pcilib_calc_time_to_deadline(&deadline) == 0) {
- pcilib_stop(ctx->handle, PCILIB_EVENT_FLAG_STOP_ONLY);
- break;
- }
- }
- }
-
- if (verbose > 0) {
- if (pcilib_calc_time_to_deadline(&nextinfo) == 0) {
- GrabStats(ctx, NULL);
- StorageStats(ctx);
- pcilib_calc_deadline(&nextinfo, STATUS_MESSAGE_INTERVAL*1000000);
- }
- }
-
- usleep(100000);
- }
-
- pcilib_calc_deadline(&nextinfo, STATUS_MESSAGE_INTERVAL*1000000);
- while (ctx->writing_flag) {
- if (pcilib_calc_time_to_deadline(&nextinfo) == 0) {
- if (verbose >= 0) StorageStats(ctx);
- pcilib_calc_deadline(&nextinfo, STATUS_MESSAGE_INTERVAL*1000000);
- }
-
- usleep(100000);
- }
-
- return NULL;
- }
- 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) {
- int err;
- GRABContext ctx;
- // void *data = NULL;
- // size_t size, written;
- pcilib_event_t event;
- pcilib_event_t listen_events;
- pcilib_event_data_type_t data;
- pthread_t monitor_thread;
- pthread_t trigger_thread;
- pthread_attr_t attr;
- struct sched_param sched;
- struct timeval end_time;
- pcilib_event_flags_t flags;
- if (evname) {
- event = pcilib_find_event(handle, evname);
- if (event == PCILIB_EVENT_INVALID)
- Error("Can't find event (%s)", evname);
- listen_events = event;
- } else {
- listen_events = PCILIB_EVENTS_ALL;
- event = PCILIB_EVENT0;
- }
-
- if (data_type) {
- data = pcilib_find_event_data_type(handle, event, data_type);
- if (data == PCILIB_EVENT_DATA_TYPE_INVALID)
- Error("Can't find data type (%s)", data_type);
- } else {
- switch (format) {
- case FORMAT_DEFAULT:
- data = PCILIB_EVENT_DATA;
- break;
- default:
- data = PCILIB_EVENT_RAW_DATA;
- }
- }
- memset(&ctx, 0, sizeof(GRABContext));
- ctx.handle = handle;
- ctx.event = event;
- ctx.data = data;
- ctx.run_time = run_time;
- ctx.timeout = timeout;
- ctx.format = format;
- if (grab_mode&GRAB_MODE_GRAB) ctx.verbose = verbose;
- else ctx.verbose = 0;
-
- if (grab_mode&GRAB_MODE_GRAB) {
- ctx.writer = fastwriter_init(output, 0);
- if (!ctx.writer)
- Error("Can't initialize fastwritter library");
- fastwriter_set_buffer_size(ctx.writer, buffer_size);
-
- err = fastwriter_open(ctx.writer, output, 0);
- if (err)
- Error("Error opening file (%s), Error: %i\n", output, err);
- ctx.writing_flag = 1;
- }
- ctx.run_flag = 1;
- flags = PCILIB_EVENT_FLAGS_DEFAULT;
-
- if (data == PCILIB_EVENT_RAW_DATA) {
- if (format == FORMAT_RAW) {
- flags |= PCILIB_EVENT_FLAG_RAW_DATA_ONLY;
- }
- } else {
- flags |= PCILIB_EVENT_FLAG_PREPROCESS;
- }
-
- ctx.flags = flags;
- // printf("Limits: %lu %lu %lu\n", num, run_time, timeout);
- pcilib_configure_autostop(handle, num, run_time);
-
- if (flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY) {
- pcilib_configure_rawdata_callback(handle, &raw_data, &ctx);
- }
-
- if (flags&PCILIB_EVENT_FLAG_PREPROCESS) {
- pcilib_write_register(handle, "conf", "max_threads", threads);
- }
-
- if (grab_mode&GRAB_MODE_TRIGGER) {
- if (trigger_time) {
- if ((timeout)&&(trigger_time * 2 > timeout)) {
- timeout = 2 * trigger_time;
- ctx.timeout = timeout;
- }
- } else {
- // Otherwise, we will trigger next event after previous one is read
- if (((grab_mode&GRAB_MODE_GRAB) == 0)||(flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY)) trigger_time = PCILIB_TRIGGER_TIMEOUT;
- }
-
- ctx.max_triggers = num;
- ctx.trigger_count = 0;
- ctx.trigger_time = trigger_time;
- // We don't really care if RT priority is imposible
- pthread_attr_init(&attr);
- if (!pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) {
- sched.sched_priority = sched_get_priority_min(SCHED_FIFO);
- pthread_attr_setschedparam(&attr, &sched);
- }
-
- // Start triggering thread and wait until it is schedulled
- if (pthread_create(&trigger_thread, &attr, Trigger, (void*)&ctx))
- Error("Error spawning trigger thread");
- while (!ctx.trigger_thread_started) usleep(10);
- }
- gettimeofday(&ctx.start_time, NULL);
- if (grab_mode&GRAB_MODE_GRAB) {
- err = pcilib_start(handle, listen_events, flags);
- if (err) Error("Failed to start event engine, error %i", err);
- }
-
- ctx.started = 1;
-
- if (run_time) {
- ctx.stop_time.tv_usec = ctx.start_time.tv_usec + run_time%1000000;
- if (ctx.stop_time.tv_usec > 999999) {
- ctx.stop_time.tv_usec -= 1000000;
- __sync_synchronize();
- ctx.stop_time.tv_sec = ctx.start_time.tv_sec + 1 + run_time / 1000000;
- } else {
- __sync_synchronize();
- ctx.stop_time.tv_sec = ctx.start_time.tv_sec + run_time / 1000000;
- }
- }
-
- memcpy(&ctx.last_frame, &ctx.start_time, sizeof(struct timeval));
- if (pthread_create(&monitor_thread, NULL, Monitor, (void*)&ctx))
- Error("Error spawning monitoring thread");
- if (grab_mode&GRAB_MODE_GRAB) {
- err = pcilib_stream(handle, &GrabCallback, &ctx);
- if (err) Error("Error streaming events, error %i", err);
- }
-
- ctx.run_flag = 0;
- if (grab_mode&GRAB_MODE_TRIGGER) {
- while (ctx.trigger_thread_started) usleep(10);
- }
-
- if (grab_mode&GRAB_MODE_GRAB) {
- pcilib_stop(handle, PCILIB_EVENT_FLAGS_DEFAULT);
- }
- gettimeofday(&end_time, NULL);
- if (grab_mode&GRAB_MODE_TRIGGER) {
- pthread_join(trigger_thread, NULL);
- }
-
- if (grab_mode&GRAB_MODE_GRAB) {
- if (verbose >= 0)
- printf("Grabbing is finished, flushing results....\n");
-
- err = fastwriter_close(ctx.writer);
- if (err) Error("Storage problems, error %i", err);
- }
- ctx.writing_flag = 0;
- pthread_join(monitor_thread, NULL);
- if ((grab_mode&GRAB_MODE_GRAB)&&(verbose>=0)) {
- GrabStats(&ctx, &end_time);
- StorageStats(&ctx);
- }
- fastwriter_destroy(ctx.writer);
- return 0;
- }
- 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) {
- int err;
- pcilib_dma_engine_t dmaid;
-
- if (dma == PCILIB_DMA_ENGINE_ADDR_INVALID) {
- const pcilib_dma_description_t *dma_info = pcilib_get_dma_description(handle);
- if (start) Error("DMA engine should be specified");
- for (dmaid = 0; dma_info->engines[dmaid].addr_bits; dmaid++) {
- err = pcilib_start_dma(handle, dmaid, PCILIB_DMA_FLAG_STOP);
- 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);
- err = pcilib_stop_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
- 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);
- }
-
- return 0;
- }
-
- if (dma_direction&PCILIB_DMA_FROM_DEVICE) {
- dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_FROM_DEVICE, dma);
- if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (C2S %lu) is specified", dma);
-
- if (start) {
- err = pcilib_start_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
- if (err) Error("Error starting DMA engine (C2S %lu)", dma);
- } else {
- err = pcilib_start_dma(handle, dmaid, PCILIB_DMA_FLAG_STOP);
- if (err) Error("Error starting DMA engine (C2S %lu)", dma);
- err = pcilib_stop_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
- if (err) Error("Error stopping DMA engine (C2S %lu)", dma);
- }
- }
-
- if (dma_direction&PCILIB_DMA_TO_DEVICE) {
- dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_TO_DEVICE, dma);
- if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("Invalid DMA engine (S2C %lu) is specified", dma);
-
- if (start) {
- err = pcilib_start_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
- if (err) Error("Error starting DMA engine (S2C %lu)", dma);
- } else {
- err = pcilib_start_dma(handle, dmaid, PCILIB_DMA_FLAG_STOP);
- if (err) Error("Error starting DMA engine (S2C %lu)", dma);
- err = pcilib_stop_dma(handle, dmaid, PCILIB_DMA_FLAG_PERSISTENT);
- if (err) Error("Error stopping DMA engine (S2C %lu)", dma);
- }
- }
- return 0;
- }
- typedef struct {
- pcilib_kmem_use_t use;
-
- int referenced;
- int hw_lock;
- int reusable;
- int persistent;
- int open;
-
- size_t count;
- size_t size;
- } kmem_use_info_t;
- #define MAX_USES 64
- pcilib_kmem_use_t ParseUse(const char *use) {
- unsigned long utmp;
- if (use) {
- if ((!isxnumber(use))||(sscanf(use, "%lx", &utmp) != 1)) Error("Invalid use (%s) is specified", use);
- if (strlen(use) < 5)
- return PCILIB_KMEM_USE(PCILIB_KMEM_USE_USER,utmp);
- else
- return utmp;
- }
- Error("Kernel memory use is not specified");
- return 0;
- }
- size_t FindUse(size_t *n_uses, kmem_use_info_t *uses, pcilib_kmem_use_t use) {
- size_t i, n = *n_uses;
-
- if (uses[n - 1].use == use) return n - 1;
- for (i = 1; i < (n - 1); i++) {
- if (uses[i].use == use) return i;
- }
-
- if (n == MAX_USES) return 0;
- memset(&uses[n], 0, sizeof(kmem_use_info_t));
- uses[n].use = use;
- return (*n_uses)++;
- }
- kmem_use_info_t *GetUse(size_t n_uses, kmem_use_info_t *uses, pcilib_kmem_use_t use) {
- size_t i;
- for (i = 0; i < n_uses; i++) {
- if (uses[i].use == use) {
- if (uses[i].count) return uses + i;
- else return NULL;
- }
- }
- return NULL;
- }
- int ParseKMEM(pcilib_t *handle, const char *device, size_t *uses_number, kmem_use_info_t *uses) {
- DIR *dir;
- struct dirent *entry;
- const char *pos;
- char sysdir[256];
- char fname[256];
- char info[256];
- size_t useid, n_uses = 1; // Use 0 is for others
- memset(uses, 0, sizeof(kmem_use_info_t));
-
- pos = strrchr(device, '/');
- if (pos) ++pos;
- else pos = device;
-
- snprintf(sysdir, 255, "/sys/class/fpga/%s", pos);
- dir = opendir(sysdir);
- if (!dir) Error("Can't open directory (%s)", sysdir);
-
- while ((entry = readdir(dir)) != NULL) {
- FILE *f;
- unsigned long use = 0;
- unsigned long size = 0;
- unsigned long refs = 0;
- unsigned long mode = 0;
- unsigned long hwref = 0;
-
- if (strncmp(entry->d_name, "kbuf", 4)) continue;
- if (!isnumber(entry->d_name+4)) continue;
-
- snprintf(fname, 255, "%s/%s", sysdir, entry->d_name);
- f = fopen(fname, "r");
- if (!f) Error("Can't access file (%s)", fname);
- while(!feof(f)) {
- if (!fgets(info, 256, f))
- break;
- if (!strncmp(info, "use:", 4)) use = strtoul(info+4, NULL, 16);
- if (!strncmp(info, "size:", 5)) size = strtoul(info+5, NULL, 10);
- if (!strncmp(info, "refs:", 5)) refs = strtoul(info+5, NULL, 10);
- if (!strncmp(info, "mode:", 5)) mode = strtoul(info+5, NULL, 16);
- if (!strncmp(info, "hw ref:", 7)) hwref = strtoul(info+7, NULL, 10);
- }
- fclose(f);
- useid = FindUse(&n_uses, uses, use);
- uses[useid].count++;
- uses[useid].size += size;
- if (refs) uses[useid].referenced = 1;
- if (hwref) uses[useid].hw_lock = 1;
- if (mode&KMEM_MODE_REUSABLE) uses[useid].reusable = 1;
- if (mode&KMEM_MODE_PERSISTENT) uses[useid].persistent = 1;
- if (mode&KMEM_MODE_COUNT) uses[useid].open = 1;
- }
- closedir(dir);
- *uses_number = n_uses;
- return 0;
- }
- int ListKMEM(pcilib_t *handle, const char *device) {
- int err;
- char stmp[256];
- size_t i, useid, n_uses;
- kmem_use_info_t uses[MAX_USES];
- const pcilib_model_description_t *model_info = pcilib_get_model_description(handle);
- err = ParseKMEM(handle, device, &n_uses, uses);
- if (err) Error("Failed to parse kernel memory information provided through sysfs");
- if ((n_uses == 1)&&(uses[0].count == 0)) {
- printf("No kernel memory is allocated\n");
- return 0;
- }
- printf("Use Type Count Total Size REF Mode \n");
- printf("--------------------------------------------------------------------------------\n");
- for (useid = 0; useid < n_uses; useid++) {
- if (useid + 1 == n_uses) {
- if (!uses[0].count) continue;
- i = 0;
- } else i = useid + 1;
-
- printf("%08x ", uses[i].use);
- if (i) {
- switch(PCILIB_KMEM_USE_TYPE(uses[i].use)) {
- case PCILIB_KMEM_USE_DMA_RING:
- printf("DMA%u %s Ring ", uses[i].use&0x7F, ((uses[i].use&0x80)?"S2C":"C2S"));
- break;
- case PCILIB_KMEM_USE_DMA_PAGES:
- printf("DMA%u %s Pages ", uses[i].use&0x7F, ((uses[i].use&0x80)?"S2C":"C2S"));
- break;
- case PCILIB_KMEM_USE_SOFTWARE_REGISTERS: {
- pcilib_register_bank_t bank = pcilib_find_register_bank_by_addr(handle, PCILIB_KMEM_USE_SUBTYPE(uses[i].use));
- if (bank == PCILIB_REGISTER_BANK_INVALID)
- printf("SoftRegs (%8u)", PCILIB_KMEM_USE_SUBTYPE(uses[i].use));
- else
- printf("SoftRegs (%8s)", model_info->banks[bank].name);
- break;
- }
- case PCILIB_KMEM_USE_LOCKS:
- printf("Locks ");
- break;
- case PCILIB_KMEM_USE_USER:
- printf("User %04x ", uses[i].use&0xFFFF);
- break;
- default:
- printf (" ");
- }
- } else printf("All Others ");
-
- printf(" ");
- printf("%6zu", uses[i].count);
- printf(" ");
- printf("%10s", GetPrintSize(stmp, uses[i].size));
- printf(" ");
- if ((uses[i].referenced)&&(uses[i].hw_lock)) printf("HW+SW");
- else if (uses[i].referenced) printf(" SW");
- else if (uses[i].hw_lock) printf("HW ");
- else printf(" - ");
- printf(" ");
- if (uses[i].persistent) printf("Persistent");
- else if (uses[i].open) printf("Open ");
- else if (uses[i].reusable) printf("Reusable ");
- else printf("Closed ");
- printf("\n");
- }
- printf("--------------------------------------------------------------------------------\n");
- printf("REF - Software/Hardware Reference, MODE - Reusable/Persistent/Open\n");
- return 0;
- }
- int DetailKMEM(pcilib_t *handle, const char *device, const char *use, size_t block) {
- int err;
- size_t i, n;
- pcilib_kmem_handle_t *kbuf;
- pcilib_kmem_use_t useid = ParseUse(use);
- size_t n_uses;
- kmem_use_info_t uses[MAX_USES];
- kmem_use_info_t *use_info;
-
- if (block == (size_t)-1) {
- err = ParseKMEM(handle, device, &n_uses, uses);
- if (err) Error("Failed to parse kernel memory information provided through sysfs");
- use_info = GetUse(n_uses, uses, useid);
- if (!use_info) Error("No kernel buffers is allocated for the specified use (%lx)", useid);
- i = 0;
- n = use_info->count;
- } else {
- i = block;
- n = block + 1;
- }
-
- kbuf = pcilib_alloc_kernel_memory(handle, 0, n, 0, 0, useid, PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_TRY);
- if (!kbuf) {
- Error("Allocation of kernel buffer (use %lx, count %lu) is failed\n", useid, n);
- return 0;
- }
- printf("Buffer Address Hardware Address Bus Address\n");
- printf("--------------------------------------------------------------------------------\n");
- for (; i < n; i++) {
- volatile void *data = pcilib_kmem_get_block_ua(handle, kbuf, i);
- uintptr_t pa = pcilib_kmem_get_block_pa(handle, kbuf, i);
- uintptr_t ba = pcilib_kmem_get_block_ba(handle, kbuf, i);
- printf("%6lu %16p %16lx %16lx\n", i, data, pa, ba);
- }
- printf("\n");
- pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
- return 0;
- }
- int ReadKMEM(pcilib_t *handle, const char *device, pcilib_kmem_use_t useid, size_t block, size_t max_size, FILE *o) {
- int err;
- void *data;
- size_t size;
- pcilib_kmem_handle_t *kbuf;
- if (block == (size_t)-1) block = 0;
- kbuf = pcilib_alloc_kernel_memory(handle, 0, block + 1, 0, 0, useid, PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_TRY);
- if (!kbuf) {
- Error("The specified kernel buffer is not allocated\n");
- return 0;
- }
- err = pcilib_kmem_sync_block(handle, kbuf, PCILIB_KMEM_SYNC_FROMDEVICE, block);
- if (err) {
- pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
- Error("The synchronization of kernel buffer has failed\n");
- return 0;
- }
- data = (void*)pcilib_kmem_get_block_ua(handle, kbuf, block);
- if (data) {
- size = pcilib_kmem_get_block_size(handle, kbuf, block);
- if ((max_size)&&(size > max_size)) size = max_size;
-
- fwrite(data, 1, size, o?o:stdout);
- } else {
- pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
- Error("The specified block is not existing\n");
- return 0;
- }
- pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
- return 0;
- }
- int AllocKMEM(pcilib_t *handle, const char *device, const char *use, const char *type, size_t size, size_t block_size, size_t alignment) {
- pcilib_kmem_type_t ktype = PCILIB_KMEM_TYPE_PAGE;
- pcilib_kmem_flags_t flags = KMEM_FLAG_REUSE;
- pcilib_kmem_handle_t *kbuf;
- pcilib_kmem_use_t useid = ParseUse(use);
- long page_size = sysconf(_SC_PAGESIZE);
- if (type) {
- if (!strcmp(type, "consistent")) ktype = PCILIB_KMEM_TYPE_CONSISTENT;
- else if (!strcmp(type, "c2s")) ktype = PCILIB_KMEM_TYPE_DMA_C2S_PAGE;
- else if (!strcmp(type, "s2c")) ktype = PCILIB_KMEM_TYPE_DMA_S2C_PAGE;
- else Error("Invalid memory type (%s) is specified", type);
- }
-
- if ((block_size)&&(ktype != PCILIB_KMEM_TYPE_CONSISTENT))
- Error("Selected memory type does not allow custom size");
-
- kbuf = pcilib_alloc_kernel_memory(handle, ktype, size, (block_size?block_size:page_size), (alignment?alignment:page_size), useid, flags|KMEM_FLAG_PERSISTENT);
- if (!kbuf) Error("Allocation of kernel memory has failed");
- pcilib_free_kernel_memory(handle, kbuf, flags);
- return 0;
- }
- int FreeKMEM(pcilib_t *handle, const char *device, const char *use, int force) {
- int err;
- int i;
- pcilib_kmem_use_t useid;
- pcilib_kmem_flags_t flags = PCILIB_KMEM_FLAG_HARDWARE|PCILIB_KMEM_FLAG_PERSISTENT|PCILIB_KMEM_FLAG_EXCLUSIVE;
- if (force) flags |= PCILIB_KMEM_FLAG_FORCE; // this will ignore mmap locks as well.
- if (!strcasecmp(use, "dma")) {
- for (i = 0; i < PCILIB_MAX_DMA_ENGINES; i++) {
- err = pcilib_clean_kernel_memory(handle, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_RING, i), flags);
- if (err) Error("Error cleaning DMA%i C2S Ring buffer", i);
- err = pcilib_clean_kernel_memory(handle, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_RING, 0x80|i), flags);
- if (err) Error("Error cleaning DMA%i S2C Ring buffer", i);
- err = pcilib_clean_kernel_memory(handle, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_PAGES, i), flags);
- if (err) Error("Error cleaning DMA%i C2S Page buffers", i);
- err = pcilib_clean_kernel_memory(handle, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_PAGES, 0x80|i), flags);
- if (err) Error("Error cleaning DMA%i S2C Page buffers", i);
- }
-
- return 0;
- }
- useid = ParseUse(use);
- err = pcilib_clean_kernel_memory(handle, useid, flags);
- if (err) Error("Error cleaning kernel buffers for use (0x%lx)", useid);
- return 0;
- }
- int ListDMA(pcilib_t *handle, const char *device, const pcilib_model_description_t *model_info) {
- int err;
-
- DIR *dir;
- struct dirent *entry;
- const char *pos;
- char sysdir[256];
- char fname[256];
- char info[256];
- char stmp[256];
- pcilib_dma_engine_t dmaid;
- pcilib_dma_engine_status_t status;
-
- pos = strrchr(device, '/');
- if (pos) ++pos;
- else pos = device;
-
- snprintf(sysdir, 255, "/sys/class/fpga/%s", pos);
- dir = opendir(sysdir);
- if (!dir) Error("Can't open directory (%s)", sysdir);
-
- printf("DMA Engine Status Total Size Buffer Ring (1st used - 1st free)\n");
- printf("--------------------------------------------------------------------------------\n");
- while ((entry = readdir(dir)) != NULL) {
- FILE *f;
- unsigned long use = 0;
- // unsigned long size = 0;
- // unsigned long refs = 0;
- unsigned long mode = 0;
- // unsigned long hwref = 0;
-
- if (strncmp(entry->d_name, "kbuf", 4)) continue;
- if (!isnumber(entry->d_name+4)) continue;
-
- snprintf(fname, 255, "%s/%s", sysdir, entry->d_name);
- f = fopen(fname, "r");
- if (!f) Error("Can't access file (%s)", fname);
- while(!feof(f)) {
- if (!fgets(info, 256, f))
- break;
- if (!strncmp(info, "use:", 4)) use = strtoul(info+4, NULL, 16);
- // if (!strncmp(info, "size:", 5)) size = strtoul(info+5, NULL, 10);
- // if (!strncmp(info, "refs:", 5)) refs = strtoul(info+5, NULL, 10);
- if (!strncmp(info, "mode:", 5)) mode = strtoul(info+5, NULL, 16);
- // if (!strncmp(info, "hw ref:", 7)) hwref = strtoul(info+7, NULL, 10);
- }
- fclose(f);
-
- if ((mode&(KMEM_MODE_REUSABLE|KMEM_MODE_PERSISTENT|KMEM_MODE_COUNT)) == 0) continue; // closed
- if ((use >> 16) != PCILIB_KMEM_USE_DMA_RING) continue;
- if (use&0x80) {
- dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_TO_DEVICE, use&0x7F);
- } else {
- dmaid = pcilib_find_dma_by_addr(handle, PCILIB_DMA_FROM_DEVICE, use&0x7F);
- }
-
- if (dmaid == PCILIB_DMA_ENGINE_INVALID) continue;
-
-
- printf("DMA%lu %s ", use&0x7F, (use&0x80)?"S2C":"C2S");
- err = pcilib_start_dma(handle, dmaid, 0);
- if (err) {
- printf("-- Wrong state, start is failed\n");
- continue;
- }
-
- err = pcilib_get_dma_status(handle, dmaid, &status, 0, NULL);
- if (err) {
- printf("-- Wrong state, failed to obtain status\n");
- pcilib_stop_dma(handle, dmaid, 0);
- continue;
- }
- pcilib_stop_dma(handle, dmaid, 0);
-
- if (status.started) printf("S");
- else printf(" ");
-
- if (status.ring_head == status.ring_tail) printf(" ");
- else printf("D");
- printf(" ");
- printf("%10s", GetPrintSize(stmp, status.ring_size * status.buffer_size));
-
- printf(" ");
- printf("%zu - %zu (of %zu)", status.ring_tail, status.ring_head, status.ring_size);
- printf("\n");
-
- }
- closedir(dir);
- printf("--------------------------------------------------------------------------------\n");
- printf("S - Started, D - Data in buffers\n");
- return 0;
- }
- 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) {
- int err;
- size_t i;
- pcilib_dma_engine_t dmaid;
- pcilib_dma_engine_status_t status;
- pcilib_dma_buffer_status_t *buffer;
- char stmp[256];
- dmaid = pcilib_find_dma_by_addr(handle, dma_direction, dma);
- if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("The specified DMA engine is not found");
- err = pcilib_start_dma(handle, dmaid, 0);
- if (err) Error("Error starting the specified DMA engine");
- err = pcilib_get_dma_status(handle, dmaid, &status, 0, NULL);
- if (err) Error("Failed to obtain status of the specified DMA engine");
- buffer = (pcilib_dma_buffer_status_t*)malloc(status.ring_size*sizeof(pcilib_dma_buffer_status_t));
- if (!buffer) Error("Failed to allocate memory for status buffer");
- err = pcilib_get_dma_status(handle, dmaid, &status, status.ring_size, buffer);
- if (err) Error("Failed to obtain extended status of the specified DMA engine");
- printf("Buffer Status Total Size \n");
- printf("--------------------------------------------------------------------------------\n");
- for (i = 0; i < status.ring_size; i++) {
- printf("%8zu ", i);
- printf("%c%c %c%c ", buffer[i].used?'U':' ', buffer[i].error?'E':' ', buffer[i].first?'F':' ', buffer[i].last?'L':' ');
- printf("%10s", GetPrintSize(stmp, buffer[i].size));
- printf("\n");
- }
- printf("--------------------------------------------------------------------------------\n");
- printf("U - Used, E - Error, F - First block, L - Last Block\n");
- free(buffer);
- pcilib_stop_dma(handle, dmaid, 0);
- return 0;
- }
- 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) {
- int err;
- pcilib_dma_engine_t dmaid;
- pcilib_dma_engine_status_t status;
- pcilib_dma_buffer_status_t *buffer;
- size_t size;
- dmaid = pcilib_find_dma_by_addr(handle, dma_direction, dma);
- if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("The specified DMA engine is not found");
- err = pcilib_start_dma(handle, dmaid, 0);
- if (err) Error("Error starting the specified DMA engine");
-
- err = pcilib_get_dma_status(handle, dmaid, &status, 0, NULL);
- if (err) Error("Failed to obtain status of the specified DMA engine");
-
- buffer = (pcilib_dma_buffer_status_t*)malloc(status.ring_size*sizeof(pcilib_dma_buffer_status_t));
- if (!buffer) Error("Failed to allocate memory for status buffer");
- err = pcilib_get_dma_status(handle, dmaid, &status, status.ring_size, buffer);
- if (err) Error("Failed to obtain extended status of the specified DMA engine");
- if (block == (size_t)-1) {
- // get current
- }
- size = buffer[block].size;
- free(buffer);
- pcilib_stop_dma(handle, dmaid, 0);
- return ReadKMEM(handle, device, ((dma&0x7F)|((dma_direction == PCILIB_DMA_TO_DEVICE)?0x80:0x00))|(PCILIB_KMEM_USE_DMA_PAGES<<16), block, size, o);
- }
- int ListLocks(pcilib_t *ctx, int verbose) {
- int err;
- pcilib_lock_id_t i;
- if (verbose)
- printf("ID Refs Flags Locked Name\n");
- else
- printf("ID Refs Flags Name\n");
- printf("--------------------------------------------------------------------------------\n");
- for (i = 0; i < PCILIB_MAX_LOCKS; i++) {
- pcilib_lock_t *lock = pcilib_get_lock_by_id(ctx, i);
- const char *name = pcilib_lock_get_name(lock);
- if (!name) break;
-
- pcilib_lock_flags_t flags = pcilib_lock_get_flags(lock);
- size_t refs = pcilib_lock_get_refs(lock);
-
- printf("%4u %4zu ", i, refs);
- if (flags&PCILIB_LOCK_FLAG_PERSISTENT) printf("P");
- else printf(" ");
- printf(" ");
-
- if (verbose) {
- err = pcilib_lock_custom(lock, PCILIB_LOCK_FLAGS_DEFAULT, PCILIB_TIMEOUT_IMMEDIATE);
- switch (err) {
- case 0:
- pcilib_unlock(lock);
- printf("No ");
- break;
- case PCILIB_ERROR_TIMEOUT:
- printf("Yes ");
- break;
- default:
- printf("Err: %3i ", err);
- }
- }
- printf("%s\n", name);
- }
- printf("--------------------------------------------------------------------------------\n");
- printf("P - Persistent\n");
- return 0;
- }
- int FreeLocks(pcilib_t *handle, int force) {
- return pcilib_destroy_all_locks(handle, force);
- }
- int LockUnlock(pcilib_t *handle, const char *name, int do_lock, pcilib_timeout_t timeout) {
- int err = 0;
- pcilib_lock_t *lock = pcilib_get_lock(handle, PCILIB_LOCK_FLAG_PERSISTENT, name);
- if (!lock) Error("Error getting persistent lock %s", name);
- if (do_lock)
- err = pcilib_lock_custom(lock, PCILIB_LOCK_FLAGS_DEFAULT, timeout);
- else
- pcilib_unlock(lock);
-
- if (err) {
- pcilib_return_lock(handle, PCILIB_LOCK_FLAGS_DEFAULT, lock);
- switch (err) {
- case PCILIB_ERROR_TIMEOUT:
- printf("Timeout locking %s\n", name);
- break;
- default:
- Error("Error (%i) locking %s", err, name);
- }
- } else if (do_lock) {
- pcilib_lock_ref(lock);
- pcilib_return_lock(handle, PCILIB_LOCK_FLAGS_DEFAULT, lock);
- printf("%s is locked\n", name);
- } else {
- pcilib_lock_unref(lock);
- pcilib_return_lock(handle, PCILIB_LOCK_FLAGS_DEFAULT, lock);
- }
- return err;
- }
- int EnableIRQ(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_irq_type_t irq_type) {
- int err;
- err = pcilib_enable_irq(handle, irq_type, 0);
- if (err) {
- if ((err != PCILIB_ERROR_NOTSUPPORTED)&&(err != PCILIB_ERROR_NOTAVAILABLE))
- Error("Error enabling IRQs");
- }
-
- return err;
- }
- int DisableIRQ(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_irq_type_t irq_type) {
- int err;
-
- err = pcilib_disable_irq(handle, 0);
- if (err) {
- if ((err != PCILIB_ERROR_NOTSUPPORTED)&&(err != PCILIB_ERROR_NOTAVAILABLE))
- Error("Error disabling IRQs");
- }
-
- return err;
- }
- int AckIRQ(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_irq_hw_source_t irq_source) {
- pcilib_clear_irq(handle, irq_source);
- return 0;
- }
- int WaitIRQ(pcilib_t *handle, const pcilib_model_description_t *model_info, pcilib_irq_hw_source_t irq_source, pcilib_timeout_t timeout) {
- int err;
- size_t count;
- err = pcilib_wait_irq(handle, irq_source, timeout, &count);
- if (err) {
- if (err == PCILIB_ERROR_TIMEOUT) Error("Timeout waiting for IRQ");
- else Error("Error waiting for IRQ");
- }
- return 0;
- }
- int main(int argc, char **argv) {
- int err = 0;
- int i;
- long itmp;
- size_t ztmp;
- unsigned char c;
- const char *stmp;
- const char *num_offset;
- int details = 0;
- int verbose = 0;
- int quiete = 0;
- int force = 0;
- int verify = 0;
-
- pcilib_log_priority_t log_priority;
-
- const char *model = NULL;
- const pcilib_model_description_t *model_info;
- const pcilib_dma_description_t *dma_info;
- MODE mode = MODE_INVALID;
- GRAB_MODE grab_mode = 0;
- size_t trigger_time = 0;
- size_t run_time = 0;
- size_t buffer = 0;
- size_t threads = 1;
- FORMAT format = FORMAT_DEFAULT;
- PARTITION partition = PARTITION_UNKNOWN;
- FLAGS flags = 0;
- const char *atype = NULL;
- const char *type = NULL;
- ACCESS_MODE amode = ACCESS_BAR;
- const char *fpga_device = DEFAULT_FPGA_DEVICE;
- pcilib_bar_t bar = PCILIB_BAR_DETECT;
- const char *addr = NULL;
- const char *reg = NULL;
- const char *view = NULL;
- const char *unit = NULL;
- const char *attr = NULL;
- const char *bank = NULL;
- char **data = NULL;
- const char *event = NULL;
- const char *data_type = NULL;
- const char *dma_channel = NULL;
- const char *use = NULL;
- const char *lock = NULL;
- const char *info_target = NULL;
- const char *list_target = NULL;
- size_t block = (size_t)-1;
- pcilib_irq_type_t irq_type = PCILIB_IRQ_TYPE_ALL;
- pcilib_irq_hw_source_t irq_source = PCILIB_IRQ_SOURCE_DEFAULT;
- pcilib_dma_direction_t dma_direction = PCILIB_DMA_BIDIRECTIONAL;
- pcilib_kmem_use_t useid = 0;
-
- pcilib_dma_engine_addr_t dma = PCILIB_DMA_ENGINE_ADDR_INVALID;
- long addr_shift = 0;
- uintptr_t start = -1;
- size_t block_size = 0;
- size_t size = 1;
- access_t access = 4;
- // int skip = 0;
- int endianess = 0;
- size_t timeout = 0;
- size_t alignment = 0;
- const char *output = NULL;
- FILE *ofile = NULL;
- size_t iterations = BENCHMARK_ITERATIONS;
- unsigned long dma_mask = 0;
- unsigned long pcie_mps = 0;
- pcilib_t *handle;
- int size_set = 0;
- int timeout_set = 0;
- // int run_time_set = 0;
- struct sched_param sched_param = {0};
- while ((c = getopt_long(argc, argv, "hqilr::w::g::d:m:t:b:a:s:e:o:", long_options, NULL)) != (unsigned char)-1) {
- extern int optind;
- switch (c) {
- case OPT_HELP:
- Usage(argc, argv, NULL);
- break;
- case OPT_VERSION:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_VERSION;
- break;
- case OPT_INFO:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- if (optarg) info_target = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) info_target = argv[optind++];
- mode = MODE_INFO;
- break;
- case OPT_LIST:
- if (mode == MODE_LIST) details++;
- else if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- if (optarg) list_target = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) list_target = argv[optind++];
- mode = MODE_LIST;
- break;
- case OPT_RESET:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_RESET;
- break;
- case OPT_BENCHMARK:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_BENCHMARK;
- if (optarg) addr = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
- break;
- case OPT_READ:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_READ;
- if (optarg) addr = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
- break;
- case OPT_WRITE:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_WRITE;
- if (optarg) addr = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++];
- break;
- case OPT_GRAB:
- if ((mode != MODE_INVALID)&&((mode != MODE_GRAB)||(grab_mode&GRAB_MODE_GRAB))) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_GRAB;
- grab_mode |= GRAB_MODE_GRAB;
-
- stmp = NULL;
- if (optarg) stmp = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) stmp = argv[optind++];
- if (stmp) {
- if ((event)&&(strcasecmp(stmp,event))) Usage(argc, argv, "Redefinition of considered event");
- event = stmp;
- }
- break;
- case OPT_TRIGGER:
- if ((mode != MODE_INVALID)&&((mode != MODE_GRAB)||(grab_mode&GRAB_MODE_TRIGGER))) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_GRAB;
- grab_mode |= GRAB_MODE_TRIGGER;
-
- stmp = NULL;
- if (optarg) stmp = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) stmp = argv[optind++];
- if (stmp) {
- if ((event)&&(strcasecmp(stmp,event))) Usage(argc, argv, "Redefinition of considered event");
- event = stmp;
- }
- break;
- case OPT_LIST_DMA:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_LIST_DMA;
- break;
- case OPT_LIST_DMA_BUFFERS:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_LIST_DMA_BUFFERS;
- dma_channel = optarg;
- break;
- case OPT_READ_DMA_BUFFER:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_READ_DMA_BUFFER;
- num_offset = strchr(optarg, ':');
- if (num_offset) {
- if (sscanf(num_offset + 1, "%zu", &block) != 1)
- Usage(argc, argv, "Invalid buffer is specified (%s)", num_offset + 1);
- *(char*)num_offset = 0;
- } else block = (size_t)-1;
-
- dma_channel = optarg;
- break;
- case OPT_START_DMA:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_START_DMA;
- if (optarg) dma_channel = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) dma_channel = argv[optind++];
- break;
- case OPT_STOP_DMA:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_STOP_DMA;
- if (optarg) dma_channel = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) dma_channel = argv[optind++];
- break;
- case OPT_ENABLE_IRQ:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_ENABLE_IRQ;
- if (optarg) num_offset = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
- else num_offset = NULL;
-
- if (num_offset) {
- if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
- Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
- irq_type = itmp;
- }
- break;
- case OPT_DISABLE_IRQ:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_DISABLE_IRQ;
- if (optarg) num_offset = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
- else num_offset = NULL;
-
- if (num_offset) {
- if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
- Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
- irq_type = itmp;
- }
- break;
- case OPT_ACK_IRQ:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_ACK_IRQ;
- if (optarg) num_offset = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
- else num_offset = NULL;
-
- if (num_offset) {
- if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
- Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
- irq_source = itmp;
- }
- break;
- case OPT_WAIT_IRQ:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
-
- mode = MODE_WAIT_IRQ;
- if (optarg) num_offset = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
- else num_offset = NULL;
-
- if (num_offset) {
- if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
- Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
- irq_source = itmp;
- }
- break;
- case OPT_SET_DMASK:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_SET_DMASK;
- if ((!isnumber(optarg))||(sscanf(optarg, "%lu", &dma_mask) != 1)||(dma_mask<24)||(dma_mask>64))
- Usage(argc, argv, "Invalid DMA mask is specified (%s)", optarg);
- break;
- case OPT_SET_MPS:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_SET_MPS;
- if ((!isnumber(optarg))||(sscanf(optarg, "%lu", &pcie_mps) != 1)||(pcie_mps<128)||(pcie_mps>2048))
- Usage(argc, argv, "Invalid payload size is specified (%s)", optarg);
- break;
- case OPT_LIST_KMEM:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_LIST_KMEM;
-
- if (optarg) use = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) use = argv[optind++];
- else use = NULL;
-
- if (use) {
- num_offset = strchr(use, ':');
- if (num_offset) {
- if (sscanf(num_offset + 1, "%zu", &block) != 1)
- Usage(argc, argv, "Invalid block number is specified (%s)", num_offset + 1);
- *(char*)num_offset = 0;
- }
- }
- break;
- case OPT_READ_KMEM:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_READ_KMEM;
- if (!model) model = "pci";
- num_offset = strchr(optarg, ':');
- if (num_offset) {
- if (sscanf(num_offset + 1, "%zu", &block) != 1)
- Usage(argc, argv, "Invalid block number is specified (%s)", num_offset + 1);
- *(char*)num_offset = 0;
- }
-
- use = optarg;
- useid = ParseUse(use);
- break;
- case OPT_ALLOC_KMEM:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_ALLOC_KMEM;
- model = "pci";
- if (optarg) use = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) use = argv[optind++];
- break;
- case OPT_FREE_KMEM:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_FREE_KMEM;
- if (!model) model = "pci";
- if (optarg) use = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) use = argv[optind++];
- break;
- case OPT_LIST_LOCKS:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_LIST_LOCKS;
- break;
- case OPT_FREE_LOCKS:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_FREE_LOCKS;
- model = "maintenance";
- break;
- case OPT_LOCK:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_LOCK;
- lock = optarg;
- break;
- case OPT_UNLOCK:
- if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
- mode = MODE_UNLOCK;
- lock = optarg;
- break;
- case OPT_DEVICE:
- fpga_device = optarg;
- break;
- case OPT_MODEL:
- model = optarg;
- /* if (!strcasecmp(optarg, "pci")) model = PCILIB_MODEL_PCI;
- else if (!strcasecmp(optarg, "ipecamera")) model = PCILIB_MODEL_IPECAMERA;
- else if (!strcasecmp(optarg, "kapture")) model = PCILIB_MODEL_KAPTURE;
- else Usage(argc, argv, "Invalid memory model (%s) is specified", optarg);*/
- break;
- case OPT_BAR:
- bank = optarg;
- // if ((sscanf(optarg,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_BANKS)) Usage(argc, argv, "Invalid data bank (%s) is specified", optarg);
- // else bar = itmp;
- break;
- case OPT_ALIGNMENT:
- if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &alignment) != 1)) {
- Usage(argc, argv, "Invalid alignment is specified (%s)", optarg);
- }
- break;
- case OPT_ACCESS:
- if (!strncasecmp(optarg, "fifo", 4)) {
- atype = "fifo";
- num_offset = optarg + 4;
- amode = ACCESS_FIFO;
- } else if (!strncasecmp(optarg, "dma", 3)) {
- atype = "dma";
- num_offset = optarg + 3;
- amode = ACCESS_DMA;
- } else if (!strncasecmp(optarg, "bar", 3)) {
- atype = "plain";
- num_offset = optarg + 3;
- amode = ACCESS_BAR;
- } else if (!strncasecmp(optarg, "config", 6)) {
- atype = "config";
- num_offset = optarg + 6;
- amode = ACCESS_CONFIG;
- } else if (!strncasecmp(optarg, "plain", 5)) {
- atype = "plain";
- num_offset = optarg + 5;
- amode = ACCESS_BAR;
- } else {
- num_offset = optarg;
- }
- if (*num_offset) {
- if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
- Usage(argc, argv, "Invalid access type (%s) is specified", optarg);
- switch (itmp) {
- case 8: access = 1; break;
- case 16: access = 2; break;
- case 32: access = 4; break;
- case 64: access = 8; break;
- default: Usage(argc, argv, "Invalid data width (%s) is specified", num_offset);
- }
- }
- break;
- case OPT_SIZE:
- if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &size) != 1)) {
- if (strcasecmp(optarg, "unlimited"))
- Usage(argc, argv, "Invalid size is specified (%s)", optarg);
- else
- size = 0;//(size_t)-1;
- }
-
- size_set = 1;
- break;
- case OPT_BLOCK_SIZE:
- if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &block_size) != 1)) {
- Usage(argc, argv, "Invalid size is specified (%s)", optarg);
- }
- break;
- case OPT_ENDIANESS:
- if ((*optarg == 'b')||(*optarg == 'B')) {
- if (ntohs(1) == 1) endianess = 0;
- else endianess = 1;
- } else if ((*optarg == 'l')||(*optarg == 'L')) {
- if (ntohs(1) == 1) endianess = 1;
- else endianess = 0;
- } else Usage(argc, argv, "Invalid endianess is specified (%s)", optarg);
-
- break;
- case OPT_TIMEOUT:
- if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &timeout) != 1)) {
- if (strcasecmp(optarg, "unlimited"))
- Usage(argc, argv, "Invalid timeout is specified (%s)", optarg);
- else
- timeout = PCILIB_TIMEOUT_INFINITE;
- }
- timeout_set = 1;
- break;
- case OPT_OUTPUT:
- output = optarg;
- break;
- case OPT_ITERATIONS:
- if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &iterations) != 1))
- Usage(argc, argv, "Invalid number of iterations is specified (%s)", optarg);
- break;
- case OPT_EVENT:
- event = optarg;
- break;
- case OPT_TYPE:
- type = optarg;
- break;
- case OPT_DATA_TYPE:
- data_type = optarg;
- break;
- case OPT_RUN_TIME:
- if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &run_time) != 1)) {
- if (strcasecmp(optarg, "unlimited"))
- Usage(argc, argv, "Invalid run-time is specified (%s)", optarg);
- else
- run_time = 0;
- }
- // run_time_set = 1;
- break;
- case OPT_TRIGGER_TIME:
- if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &trigger_time) != 1))
- Usage(argc, argv, "Invalid trigger-time is specified (%s)", optarg);
- break;
- case OPT_TRIGGER_RATE:
- if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &ztmp) != 1))
- Usage(argc, argv, "Invalid trigger-rate is specified (%s)", optarg);
-
- trigger_time = (1000000 / ztmp) + ((1000000 % ztmp)?1:0);
- break;
- case OPT_BUFFER:
- if (optarg) num_offset = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
- else num_offset = NULL;
-
- if (num_offset) {
- if ((!isnumber(num_offset))||(sscanf(num_offset, "%zu", &buffer) != 1))
- Usage(argc, argv, "Invalid buffer size is specified (%s)", num_offset);
- buffer *= 1024 * 1024;
- } else {
- buffer = get_free_memory();
- if (buffer < 256) Error("Not enough free memory (%lz MiB) for buffering", buffer / 1024 / 1024);
-
- buffer -= 128 + buffer/16;
- }
- break;
- case OPT_THREADS:
- if (optarg) num_offset = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
- else num_offset = NULL;
-
- if (num_offset) {
- if ((!isnumber(num_offset))||(sscanf(num_offset, "%zu", &threads) != 1))
- Usage(argc, argv, "Invalid threads number is specified (%s)", num_offset);
- } else {
- threads = 0;
- }
- break;
- case OPT_FORMAT:
- if (!strcasecmp(optarg, "raw")) format = FORMAT_RAW;
- else if (!strcasecmp(optarg, "add_header")) format = FORMAT_HEADER;
- // else if (!strcasecmp(optarg, "ringfs")) format = FORMAT_RINGFS;
- else if (strcasecmp(optarg, "default")) Error("Invalid format (%s) is specified", optarg);
- break;
- case OPT_QUIETE:
- quiete = 1;
- verbose = -1;
- break;
- case OPT_VERBOSE:
- if (optarg) num_offset = optarg;
- else if ((optind < argc)&&(argv[optind][0] != '-')) num_offset = argv[optind++];
- else num_offset = NULL;
-
- if (num_offset) {
- if ((!isnumber(num_offset))||(sscanf(num_offset, "%i", &verbose) != 1))
- Usage(argc, argv, "Invalid verbosity level is specified (%s)", num_offset);
- } else {
- verbose = 1;
- }
- break;
- case OPT_FORCE:
- force = 1;
- break;
- case OPT_VERIFY:
- verify = 1;
- break;
- case OPT_MULTIPACKET:
- flags |= FLAG_MULTIPACKET;
- break;
- case OPT_WAIT:
- flags |= FLAG_WAIT;
- break;
- default:
- Usage(argc, argv, "Unknown option (%s) with argument (%s)", optarg?argv[optind-2]:argv[optind-1], optarg?optarg:"(null)");
- }
- }
- if (mode == MODE_INVALID) {
- if (argc > 1) Usage(argc, argv, "Operation is not specified");
- else Usage(argc, argv, NULL);
- }
- if (verbose) log_priority = PCILIB_LOG_INFO;
- else if (quiete) log_priority = PCILIB_LOG_ERROR;
- else log_priority = PCILIB_LOG_WARNING;
- pcilib_set_logger(log_priority, &LogError, NULL);
- handle = pcilib_open(fpga_device, model);
- if (handle < 0) Error("Failed to open FPGA device: %s", fpga_device);
- model_info = pcilib_get_model_description(handle);
- dma_info = pcilib_get_dma_description(handle);
- switch (mode) {
- case MODE_WRITE:
- if (((argc - optind) == 1)&&(*argv[optind] == '*')) {
- int vallen = strlen(argv[optind]);
- if (vallen > 1) {
- data = (char**)malloc(size * (vallen + sizeof(char*)));
- if (!data) Error("Error allocating memory for data array");
- for (i = 0; i < size; i++) {
- data[i] = ((char*)data) + size * sizeof(char*) + i * vallen;
- strcpy(data[i], argv[optind] + 1);
- }
- } else {
- data = (char**)malloc(size * (9 + sizeof(char*)));
- if (!data) Error("Error allocating memory for data array");
-
- for (i = 0; i < size; i++) {
- data[i] = ((char*)data) + size * sizeof(char*) + i * 9;
- sprintf(data[i], "%x", i);
- }
- }
- } else if ((argc - optind) == size) data = argv + optind;
- else Usage(argc, argv, "The %i data values is specified, but %i required", argc - optind, size);
- case MODE_READ:
- if (!addr) {
- if (((!dma_info)||(!dma_info->api))&&(!model_info->api)&&(!handle->num_reg)) {
- // if (model == PCILIB_MODEL_PCI) {
- if ((amode != ACCESS_DMA)&&(amode != ACCESS_CONFIG))
- Usage(argc, argv, "The address is not specified");
- } else ++mode;
- }
- break;
- case MODE_START_DMA:
- case MODE_STOP_DMA:
- case MODE_LIST_DMA_BUFFERS:
- case MODE_READ_DMA_BUFFER:
- if ((dma_channel)&&(*dma_channel)) {
- itmp = strlen(dma_channel) - 1;
- if (dma_channel[itmp] == 'r') dma_direction = PCILIB_DMA_FROM_DEVICE;
- else if (dma_channel[itmp] == 'w') dma_direction = PCILIB_DMA_TO_DEVICE;
- if (dma_direction != PCILIB_DMA_BIDIRECTIONAL) itmp--;
-
- if (strncmp(dma_channel, "dma", 3)) num_offset = dma_channel;
- else {
- num_offset = dma_channel + 3;
- itmp -= 3;
- }
- if (bank) {
- 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);
- }
-
- if (!isnumber_n(num_offset, itmp))
- Usage(argc, argv, "Invalid DMA channel (%s) is specified", dma_channel);
- dma = atoi(num_offset);
- }
- break;
- case MODE_LIST:
- if (bank&&list_target) {
- if (strcmp(list_target, bank))
- Usage(argc, argv, "Conflicting banks are specified in list parameter (%s) and bank parameter (%s)", list_target, bank);
- } else if (bank) {
- list_target = bank;
- }
- break;
- default:
- if (argc > optind) Usage(argc, argv, "Invalid non-option parameters are supplied");
- }
- if (addr) {
- if ((!strncmp(addr, "dma", 3))&&((addr[3]==0)||isnumber(addr+3))) {
- if ((atype)&&(amode != ACCESS_DMA)) Usage(argc, argv, "Conflicting access modes, the DMA read is requested, but access type is (%s)", type);
- if (bank) {
- 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);
- } else {
- if (addr[3] == 0) Usage(argc, argv, "The DMA channel is not specified");
- }
- dma = atoi(addr + 3);
- amode = ACCESS_DMA;
- addr = NULL;
- } else if ((!strncmp(addr, "bar", 3))&&((addr[3]==0)||isnumber(addr+3))) {
- if ((atype)&&(amode != ACCESS_BAR)) Usage(argc, argv, "Conflicting access modes, the plain PCI read is requested, but access type is (%s)", type);
- 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);
- bar = atoi(addr + 3);
- amode = ACCESS_BAR;
- addr = NULL;
- } else if (!strcmp(addr, "config")) {
- 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);
- amode = ACCESS_CONFIG;
- addr = NULL;
- } else if ((isxnumber(addr))&&(sscanf(addr, "%lx", &start) == 1)) {
- // check if the address in the register range
- const pcilib_register_range_t *ranges = model_info->ranges;
-
- if (ranges) {
- for (i = 0; ranges[i].start != ranges[i].end; i++)
- if ((start >= ranges[i].start)&&(start <= ranges[i].end)) break;
-
- // register access in plain mode
- if (ranges[i].start != ranges[i].end) {
- pcilib_register_bank_t regbank = pcilib_find_register_bank_by_addr(handle, ranges[i].bank);
- if (regbank == PCILIB_REGISTER_BANK_INVALID) Error("Configuration error: register bank specified in the address range is not found");
-
- bank = model_info->banks[regbank].name;
- start += ranges[i].addr_shift;
- addr_shift = ranges[i].addr_shift;
- ++mode;
- }
- }
- } else {
- const char *spec;
- attr = strchr(addr, '@');
- if (attr) {
- size_t spec_size = strlen(addr) - strlen(attr);
- spec = strndupa(addr, spec_size);
- attr++;
- } else {
- spec = addr;
- }
- view = strchr(spec, '/');
- unit = strchr((view?view:spec), ':');
- if (view||unit) {
- size_t reg_size = strlen(spec) - strlen(view?view:unit);
- if (reg_size) reg = strndupa(spec, reg_size);
- else reg = NULL;
- if ((reg)&&(view)) view++;
- if (unit) unit++;
- if (view&&unit) {
- view = strndupa(view, strlen(view) - strlen(unit) - 1);
- } else if ((reg)&&(unit)) {
- view = unit;
- unit = NULL;
- }
- } else {
- if (*spec) reg = spec;
- else reg = NULL;
- }
- if (reg) {
- if (pcilib_find_register(handle, bank, reg) == PCILIB_REGISTER_INVALID) {
- Usage(argc, argv, "Invalid address (%s) is specified", addr);
- }
- }
- if (attr) {
- if (mode == MODE_WRITE)
- Error("Writting of attributes is not supported");
- mode += 3;
- } else if (reg) {
- mode += 1;
- } else {
- mode += 2;
- }
- }
- }
-
- if (mode == MODE_GRAB) {
- if (output) {
- char fsname[128];
- if (!get_file_fs(output, 127, fsname)) {
- if (!strcmp(fsname, "ext4")) partition = PARTITION_EXT4;
- else if (!strcmp(fsname, "raw")) partition = PARTITION_RAW;
- }
- } else {
- output = "/dev/null";
- partition = PARTITION_NULL;
- }
- if (!timeout_set) {
- if (run_time) timeout = PCILIB_TIMEOUT_INFINITE;
- else timeout = PCILIB_EVENT_TIMEOUT;
- }
-
- if (!size_set) {
- if (run_time) size = 0;
- }
- }
-
- if (mode != MODE_GRAB) {
- if (size == (size_t)-1)
- Usage(argc, argv, "Unlimited size is not supported in selected operation mode");
- }
-
- if ((bank)&&(amode == ACCESS_DMA)) {
- if ((!isnumber(bank))||(sscanf(bank,"%li", &itmp) != 1)||(itmp < 0))
- Usage(argc, argv, "Invalid DMA channel (%s) is specified", bank);
- else dma = itmp;
- } else if (bank) {
- switch (mode) {
- case MODE_BENCHMARK:
- case MODE_READ:
- case MODE_WRITE:
- if ((!isnumber(bank))||(sscanf(bank,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_REGISTER_BANKS))
- Usage(argc, argv, "Invalid data bank (%s) is specified", bank);
- else bar = itmp;
- break;
- default:
- if (pcilib_find_register_bank(handle, bank) == PCILIB_REGISTER_BANK_INVALID)
- Usage(argc, argv, "Invalid data bank (%s) is specified", bank);
- }
- }
- signal(SIGINT, signal_exit_handler);
- if ((mode != MODE_GRAB)&&(output)) {
- ofile = fopen(output, "a+");
- if (!ofile) {
- Error("Failed to open file \"%s\"", output);
- }
- }
- // Requesting real-time priority when needed
- switch (mode) {
- case MODE_READ:
- case MODE_WRITE:
- if (amode != ACCESS_DMA)
- break;
- case MODE_BENCHMARK:
- sched_param.sched_priority = sched_get_priority_max(SCHED_FIFO);
- err = sched_setscheduler(0, SCHED_FIFO, &sched_param);
- if (err) pcilib_info("Failed to acquire real-time priority (errno: %i)", errno);
- break;
- case MODE_GRAB:
- sched_param.sched_priority = sched_get_priority_min(SCHED_FIFO);
- err = sched_setscheduler(0, SCHED_FIFO, &sched_param);
- if (err) pcilib_info("Failed to acquire real-time priority (errno: %i)", errno);
- break;
- default:
- ;
- }
- switch (mode) {
- case MODE_VERSION:
- Version(handle, model_info);
- break;
- case MODE_INFO:
- Info(handle, model_info, info_target);
- break;
- case MODE_LIST:
- if ((list_target)&&(*list_target == '/'))
- ListProperties(handle, list_target, details);
- else
- List(handle, model_info, list_target, details);
- break;
- case MODE_BENCHMARK:
- Benchmark(handle, amode, dma, bar, start, size_set?size:0, access, iterations);
- break;
- case MODE_READ:
- if (amode == ACCESS_DMA) {
- err = ReadData(handle, amode, flags, dma, bar, start, size_set?size:0, access, endianess, timeout_set?timeout:(size_t)-1, ofile);
- } else if (amode == ACCESS_CONFIG) {
- err = ReadData(handle, amode, flags, dma, bar, addr?start:0, (addr||size_set)?size:(256/abs(access)), access, endianess, (size_t)-1, ofile);
- } else if (addr) {
- err = ReadData(handle, amode, flags, dma, bar, start, size, access, endianess, (size_t)-1, ofile);
- } else {
- Error("Address to read is not specified");
- }
- break;
- case MODE_READ_REGISTER:
- case MODE_READ_PROPERTY:
- case MODE_READ_ATTR:
- if ((reg)||(view)||(attr)||(!addr)) ReadRegister(handle, model_info, bank, reg, view, unit, attr);
- else ReadRegisterRange(handle, model_info, bank, start, addr_shift, size, ofile);
- break;
- case MODE_WRITE:
- WriteData(handle, amode, dma, bar, start, size, access, endianess, data, verify);
- break;
- case MODE_WRITE_REGISTER:
- case MODE_WRITE_PROPERTY:
- if (reg||view) WriteRegister(handle, model_info, bank, reg, view, unit, data);
- else WriteRegisterRange(handle, model_info, bank, start, addr_shift, size, data);
- break;
- case MODE_RESET:
- pcilib_reset(handle);
- break;
- case MODE_GRAB:
- TriggerAndGrab(handle, grab_mode, event, data_type, size, run_time, trigger_time, timeout, partition, format, buffer, threads, verbose, output);
- break;
- case MODE_LIST_DMA:
- ListDMA(handle, fpga_device, model_info);
- break;
- case MODE_LIST_DMA_BUFFERS:
- ListBuffers(handle, fpga_device, model_info, dma, dma_direction);
- break;
- case MODE_READ_DMA_BUFFER:
- ReadBuffer(handle, fpga_device, model_info, dma, dma_direction, block, ofile);
- break;
- case MODE_START_DMA:
- StartStopDMA(handle, model_info, dma, dma_direction, 1);
- break;
- case MODE_STOP_DMA:
- StartStopDMA(handle, model_info, dma, dma_direction, 0);
- break;
- case MODE_ENABLE_IRQ:
- EnableIRQ(handle, model_info, irq_type);
- break;
- case MODE_DISABLE_IRQ:
- DisableIRQ(handle, model_info, irq_type);
- break;
- case MODE_ACK_IRQ:
- AckIRQ(handle, model_info, irq_source);
- break;
- case MODE_WAIT_IRQ:
- WaitIRQ(handle, model_info, irq_source, timeout);
- break;
- case MODE_SET_DMASK:
- pcilib_set_dma_mask(handle, dma_mask);
- break;
- case MODE_SET_MPS:
- pcilib_set_mps(handle, pcie_mps);
- break;
- case MODE_LIST_KMEM:
- if (use) DetailKMEM(handle, fpga_device, use, block);
- else ListKMEM(handle, fpga_device);
- break;
- case MODE_READ_KMEM:
- ReadKMEM(handle, fpga_device, useid, block, 0, ofile);
- break;
- case MODE_ALLOC_KMEM:
- AllocKMEM(handle, fpga_device, use, type, size, block_size, alignment);
- break;
- case MODE_FREE_KMEM:
- FreeKMEM(handle, fpga_device, use, force);
- break;
- case MODE_LIST_LOCKS:
- ListLocks(handle, verbose);
- break;
- case MODE_FREE_LOCKS:
- FreeLocks(handle, force);
- break;
- case MODE_LOCK:
- LockUnlock(handle, lock, 1, timeout_set?timeout:PCILIB_TIMEOUT_INFINITE);
- break;
- case MODE_UNLOCK:
- LockUnlock(handle, lock, 0, timeout_set?timeout:PCILIB_TIMEOUT_INFINITE);
- break;
- case MODE_INVALID:
- break;
- }
- if (ofile) fclose(ofile);
- pcilib_close(handle);
-
- if (data != argv + optind) free(data);
- return err;
- }
|