0036-bcm2708-vchiq-driver.patch 408 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200
  1. From 6862270cc99bacc345403c730c38b17615834aef Mon Sep 17 00:00:00 2001
  2. From: popcornmix <popcornmix@gmail.com>
  3. Date: Tue, 2 Jul 2013 23:42:01 +0100
  4. Subject: [PATCH 036/381] bcm2708 vchiq driver
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Signed-off-by: popcornmix <popcornmix@gmail.com>
  9. vchiq: create_pagelist copes with vmalloc memory
  10. Signed-off-by: Daniel Stone <daniels@collabora.com>
  11. vchiq: fix the shim message release
  12. Signed-off-by: Daniel Stone <daniels@collabora.com>
  13. vchiq: export additional symbols
  14. Signed-off-by: Daniel Stone <daniels@collabora.com>
  15. VCHIQ: Make service closure fully synchronous (drv)
  16. This is one half of a two-part patch, the other half of which is to
  17. the vchiq_lib user library. With these patches, calls to
  18. vchiq_close_service and vchiq_remove_service won't return until any
  19. associated callbacks have been delivered to the callback thread.
  20. VCHIQ: Add per-service tracing
  21. The new service option VCHIQ_SERVICE_OPTION_TRACE is a boolean that
  22. toggles tracing for the specified service.
  23. This commit also introduces vchi_service_set_option and the associated
  24. option VCHI_SERVICE_OPTION_TRACE.
  25. vchiq: Make the synchronous-CLOSE logic more tolerant
  26. vchiq: Move logging control into debugfs
  27. vchiq: Take care of a corner case tickled by VCSM
  28. Closing a connection that isn't fully open requires care, since one
  29. side does not know the other side's port number. Code was present to
  30. handle the case where a CLOSE is sent immediately after an OPEN, i.e.
  31. before the OPENACK has been received, but this was incorrectly being
  32. used when an OPEN from a client using port 0 was rejected.
  33. (In the observed failure, the host was attempting to use the VCSM
  34. service, which isn't present in the 'cutdown' firmware. The failure
  35. was intermittent because sometimes the keepalive service would
  36. grab port 0.)
  37. This case can be distinguished because the client's remoteport will
  38. still be VCHIQ_PORT_FREE, and the srvstate will be OPENING. Either
  39. condition is sufficient to differentiate it from the special case
  40. described above.
  41. vchiq: Avoid high load when blocked and unkillable
  42. vchiq: Include SIGSTOP and SIGCONT in list of signals not-masked by vchiq to allow gdb to work
  43. vchiq_arm: Complete support for SYNCHRONOUS mode
  44. vchiq: Remove inline from suspend/resume
  45. vchiq: Allocation does not need to be atomic
  46. vchiq: Fix wrong condition check
  47. The log level is checked from within the log call. Remove the check in the call.
  48. Signed-off-by: Pranith Kumar <bobby.prani@gmail.com>
  49. BCM270x: Add vchiq device to platform file and Device Tree
  50. Prepare to turn the vchiq module into a driver.
  51. Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
  52. bcm2708: vchiq: Add Device Tree support
  53. Turn vchiq into a driver and stop hardcoding resources.
  54. Use devm_* functions in probe path to simplify cleanup.
  55. A global variable is used to hold the register address. This is done
  56. to keep this patch as small as possible.
  57. Also make available on ARCH_BCM2835.
  58. Based on work by Lubomir Rintel.
  59. Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
  60. vchiq: Change logging level for inbound data
  61. vchiq_arm: Two cacheing fixes
  62. 1) Make fragment size vary with cache line size
  63. Without this patch, non-cache-line-aligned transfers may corrupt
  64. (or be corrupted by) adjacent data structures.
  65. Both ARM and VC need to be updated to enable this feature. This is
  66. ensured by having the loader apply a new DT parameter -
  67. cache-line-size. The existence of this parameter guarantees that the
  68. kernel is capable, and the parameter will only be modified from the
  69. safe default if the loader is capable.
  70. 2) Flush/invalidate vmalloc'd memory, and invalidate after reads
  71. vchiq: fix NULL pointer dereference when closing driver
  72. The following code run as root will cause a null pointer dereference oops:
  73. int fd = open("/dev/vc-cma", O_RDONLY);
  74. if (fd < 0)
  75. err(1, "open failed");
  76. (void)close(fd);
  77. [ 1704.877721] Unable to handle kernel NULL pointer dereference at virtual address 00000000
  78. [ 1704.877725] pgd = b899c000
  79. [ 1704.877736] [00000000] *pgd=37fab831, *pte=00000000, *ppte=00000000
  80. [ 1704.877748] Internal error: Oops: 817 [#1] PREEMPT SMP ARM
  81. [ 1704.877765] Modules linked in: evdev i2c_bcm2708 uio_pdrv_genirq uio
  82. [ 1704.877774] CPU: 2 PID: 3656 Comm: stress-ng-fstat Not tainted 3.19.1-12-generic-bcm2709 #12-Ubuntu
  83. [ 1704.877777] Hardware name: BCM2709
  84. [ 1704.877783] task: b8ab9b00 ti: b7e68000 task.ti: b7e68000
  85. [ 1704.877798] PC is at __down_interruptible+0x50/0xec
  86. [ 1704.877806] LR is at down_interruptible+0x5c/0x68
  87. [ 1704.877813] pc : [<80630ee8>] lr : [<800704b0>] psr: 60080093
  88. sp : b7e69e50 ip : b7e69e88 fp : b7e69e84
  89. [ 1704.877817] r10: b88123c8 r9 : 00000010 r8 : 00000001
  90. [ 1704.877822] r7 : b8ab9b00 r6 : 7fffffff r5 : 80a1cc34 r4 : 80a1cc34
  91. [ 1704.877826] r3 : b7e69e50 r2 : 00000000 r1 : 00000000 r0 : 80a1cc34
  92. [ 1704.877833] Flags: nZCv IRQs off FIQs on Mode SVC_32 ISA ARM Segment user
  93. [ 1704.877838] Control: 10c5387d Table: 3899c06a DAC: 00000015
  94. [ 1704.877843] Process do-oops (pid: 3656, stack limit = 0xb7e68238)
  95. [ 1704.877848] Stack: (0xb7e69e50 to 0xb7e6a000)
  96. [ 1704.877856] 9e40: 80a1cc3c 00000000 00000010 b88123c8
  97. [ 1704.877865] 9e60: b7e69e84 80a1cc34 fff9fee9 ffffffff b7e68000 00000009 b7e69ea4 b7e69e88
  98. [ 1704.877874] 9e80: 800704b0 80630ea4 fff9fee9 60080013 80a1cc28 fff9fee9 b7e69edc b7e69ea8
  99. [ 1704.877884] 9ea0: 8040f558 80070460 fff9fee9 ffffffff 00000000 00000000 00000009 80a1cb7c
  100. [ 1704.877893] 9ec0: 00000000 80a1cb7c 00000000 00000010 b7e69ef4 b7e69ee0 803e1ba4 8040f514
  101. [ 1704.877902] 9ee0: 00000e48 80a1cb7c b7e69f14 b7e69ef8 803e1c9c 803e1b74 b88123c0 b92acb18
  102. [ 1704.877911] 9f00: b8812790 b8d815d8 b7e69f24 b7e69f18 803e2250 803e1bc8 b7e69f5c b7e69f28
  103. [ 1704.877921] 9f20: 80167bac 803e222c 00000000 00000000 b7e69f54 b8ab9ffc 00000000 8098c794
  104. [ 1704.877930] 9f40: b8ab9b00 8000efc4 b7e68000 00000000 b7e69f6c b7e69f60 80167d6c 80167b28
  105. [ 1704.877939] 9f60: b7e69f8c b7e69f70 80047d38 80167d60 b7e68000 b7e68010 8000efc4 b7e69fb0
  106. [ 1704.877949] 9f80: b7e69fac b7e69f90 80012820 80047c84 01155490 011549a8 00000001 00000006
  107. [ 1704.877957] 9fa0: 00000000 b7e69fb0 8000ee5c 80012790 00000000 353d8c0f 7efc4308 00000000
  108. [ 1704.877966] 9fc0: 01155490 011549a8 00000001 00000006 00000000 00000000 76cf3ba0 00000003
  109. [ 1704.877975] 9fe0: 00000000 7efc42e4 0002272f 76e2ed66 60080030 00000003 00000000 00000000
  110. [ 1704.877998] [<80630ee8>] (__down_interruptible) from [<800704b0>] (down_interruptible+0x5c/0x68)
  111. [ 1704.878015] [<800704b0>] (down_interruptible) from [<8040f558>] (vchiu_queue_push+0x50/0xd8)
  112. [ 1704.878032] [<8040f558>] (vchiu_queue_push) from [<803e1ba4>] (send_worker_msg+0x3c/0x54)
  113. [ 1704.878045] [<803e1ba4>] (send_worker_msg) from [<803e1c9c>] (vc_cma_set_reserve+0xe0/0x1c4)
  114. [ 1704.878057] [<803e1c9c>] (vc_cma_set_reserve) from [<803e2250>] (vc_cma_release+0x30/0x38)
  115. [ 1704.878069] [<803e2250>] (vc_cma_release) from [<80167bac>] (__fput+0x90/0x1e0)
  116. [ 1704.878082] [<80167bac>] (__fput) from [<80167d6c>] (____fput+0x18/0x1c)
  117. [ 1704.878094] [<80167d6c>] (____fput) from [<80047d38>] (task_work_run+0xc0/0xf8)
  118. [ 1704.878109] [<80047d38>] (task_work_run) from [<80012820>] (do_work_pending+0x9c/0xc4)
  119. [ 1704.878123] [<80012820>] (do_work_pending) from [<8000ee5c>] (work_pending+0xc/0x20)
  120. [ 1704.878133] Code: e50b1034 e3a01000 e50b2030 e580300c (e5823000)
  121. ..the fix is to ensure that we have actually initialized the queue before we attempt
  122. to push any items onto it. This occurs if we do an open() followed by a close() without
  123. any activity in between.
  124. Signed-off-by: Colin Ian King <colin.king@canonical.com>
  125. vchiq_arm: Sort out the vmalloc case
  126. See: https://github.com/raspberrypi/linux/issues/1055
  127. vchiq: hack: Add include depecated dma include file
  128. ---
  129. arch/arm/mach-bcm2708/include/mach/platform.h | 2 +
  130. arch/arm/mach-bcm2709/include/mach/platform.h | 2 +
  131. drivers/misc/Kconfig | 1 +
  132. drivers/misc/Makefile | 1 +
  133. drivers/misc/vc04_services/Kconfig | 9 +
  134. drivers/misc/vc04_services/Makefile | 14 +
  135. .../interface/vchi/connections/connection.h | 328 ++
  136. .../interface/vchi/message_drivers/message.h | 204 +
  137. drivers/misc/vc04_services/interface/vchi/vchi.h | 378 ++
  138. .../misc/vc04_services/interface/vchi/vchi_cfg.h | 224 ++
  139. .../interface/vchi/vchi_cfg_internal.h | 71 +
  140. .../vc04_services/interface/vchi/vchi_common.h | 175 +
  141. .../misc/vc04_services/interface/vchi/vchi_mh.h | 42 +
  142. .../misc/vc04_services/interface/vchiq_arm/vchiq.h | 40 +
  143. .../vc04_services/interface/vchiq_arm/vchiq_2835.h | 42 +
  144. .../interface/vchiq_arm/vchiq_2835_arm.c | 586 +++
  145. .../vc04_services/interface/vchiq_arm/vchiq_arm.c | 2903 +++++++++++++++
  146. .../vc04_services/interface/vchiq_arm/vchiq_arm.h | 220 ++
  147. .../interface/vchiq_arm/vchiq_build_info.h | 37 +
  148. .../vc04_services/interface/vchiq_arm/vchiq_cfg.h | 69 +
  149. .../interface/vchiq_arm/vchiq_connected.c | 120 +
  150. .../interface/vchiq_arm/vchiq_connected.h | 50 +
  151. .../vc04_services/interface/vchiq_arm/vchiq_core.c | 3934 ++++++++++++++++++++
  152. .../vc04_services/interface/vchiq_arm/vchiq_core.h | 712 ++++
  153. .../interface/vchiq_arm/vchiq_debugfs.c | 383 ++
  154. .../interface/vchiq_arm/vchiq_debugfs.h | 52 +
  155. .../interface/vchiq_arm/vchiq_genversion | 87 +
  156. .../vc04_services/interface/vchiq_arm/vchiq_if.h | 189 +
  157. .../interface/vchiq_arm/vchiq_ioctl.h | 131 +
  158. .../interface/vchiq_arm/vchiq_kern_lib.c | 458 +++
  159. .../interface/vchiq_arm/vchiq_killable.h | 69 +
  160. .../interface/vchiq_arm/vchiq_memdrv.h | 71 +
  161. .../interface/vchiq_arm/vchiq_pagelist.h | 58 +
  162. .../vc04_services/interface/vchiq_arm/vchiq_shim.c | 860 +++++
  163. .../vc04_services/interface/vchiq_arm/vchiq_util.c | 156 +
  164. .../vc04_services/interface/vchiq_arm/vchiq_util.h | 82 +
  165. .../interface/vchiq_arm/vchiq_version.c | 59 +
  166. 37 files changed, 12819 insertions(+)
  167. create mode 100644 drivers/misc/vc04_services/Kconfig
  168. create mode 100644 drivers/misc/vc04_services/Makefile
  169. create mode 100644 drivers/misc/vc04_services/interface/vchi/connections/connection.h
  170. create mode 100644 drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  171. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi.h
  172. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  173. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  174. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_common.h
  175. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  176. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  177. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  178. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  179. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  180. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  181. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  182. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  183. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  184. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  185. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  186. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  187. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
  188. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
  189. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  190. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  191. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  192. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  193. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h
  194. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  195. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  196. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  197. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  198. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  199. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  200. --- a/arch/arm/mach-bcm2708/include/mach/platform.h
  201. +++ b/arch/arm/mach-bcm2708/include/mach/platform.h
  202. @@ -78,6 +78,8 @@
  203. #define ARMCTRL_IC_BASE (ARM_BASE + 0x200) /* ARM interrupt controller */
  204. #define ARMCTRL_TIMER0_1_BASE (ARM_BASE + 0x400) /* Timer 0 and 1 */
  205. #define ARMCTRL_0_SBM_BASE (ARM_BASE + 0x800) /* User 0 (ARM)'s Semaphores Doorbells and Mailboxes */
  206. +#define ARMCTRL_0_BELL_BASE (ARMCTRL_0_SBM_BASE + 0x40) /* User 0 (ARM)'s Doorbell */
  207. +#define ARMCTRL_0_MAIL0_BASE (ARMCTRL_0_SBM_BASE + 0x80) /* User 0 (ARM)'s Mailbox 0 */
  208. /*
  209. * Watchdog
  210. --- a/arch/arm/mach-bcm2709/include/mach/platform.h
  211. +++ b/arch/arm/mach-bcm2709/include/mach/platform.h
  212. @@ -78,6 +78,8 @@
  213. #define ARMCTRL_IC_BASE (ARM_BASE + 0x200) /* ARM interrupt controller */
  214. #define ARMCTRL_TIMER0_1_BASE (ARM_BASE + 0x400) /* Timer 0 and 1 */
  215. #define ARMCTRL_0_SBM_BASE (ARM_BASE + 0x800) /* User 0 (ARM)'s Semaphores Doorbells and Mailboxes */
  216. +#define ARMCTRL_0_BELL_BASE (ARMCTRL_0_SBM_BASE + 0x40) /* User 0 (ARM)'s Doorbell */
  217. +#define ARMCTRL_0_MAIL0_BASE (ARMCTRL_0_SBM_BASE + 0x80) /* User 0 (ARM)'s Mailbox 0 */
  218. /*
  219. * Watchdog
  220. --- a/drivers/misc/Kconfig
  221. +++ b/drivers/misc/Kconfig
  222. @@ -533,6 +533,7 @@ source "drivers/misc/lis3lv02d/Kconfig"
  223. source "drivers/misc/altera-stapl/Kconfig"
  224. source "drivers/misc/mei/Kconfig"
  225. source "drivers/misc/vmw_vmci/Kconfig"
  226. +source "drivers/misc/vc04_services/Kconfig"
  227. source "drivers/misc/mic/Kconfig"
  228. source "drivers/misc/genwqe/Kconfig"
  229. source "drivers/misc/echo/Kconfig"
  230. --- a/drivers/misc/Makefile
  231. +++ b/drivers/misc/Makefile
  232. @@ -51,6 +51,7 @@ obj-$(CONFIG_INTEL_MEI) += mei/
  233. obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
  234. obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
  235. obj-$(CONFIG_SRAM) += sram.o
  236. +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
  237. obj-y += mic/
  238. obj-$(CONFIG_GENWQE) += genwqe/
  239. obj-$(CONFIG_ECHO) += echo/
  240. --- /dev/null
  241. +++ b/drivers/misc/vc04_services/Kconfig
  242. @@ -0,0 +1,9 @@
  243. +config BCM2708_VCHIQ
  244. + tristate "Videocore VCHIQ"
  245. + depends on RASPBERRYPI_FIRMWARE
  246. + default y
  247. + help
  248. + Kernel to VideoCore communication interface for the
  249. + BCM2708 family of products.
  250. + Defaults to Y when the Broadcom Videocore services
  251. + are included in the build, N otherwise.
  252. --- /dev/null
  253. +++ b/drivers/misc/vc04_services/Makefile
  254. @@ -0,0 +1,14 @@
  255. +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
  256. +
  257. +vchiq-objs := \
  258. + interface/vchiq_arm/vchiq_core.o \
  259. + interface/vchiq_arm/vchiq_arm.o \
  260. + interface/vchiq_arm/vchiq_kern_lib.o \
  261. + interface/vchiq_arm/vchiq_2835_arm.o \
  262. + interface/vchiq_arm/vchiq_debugfs.o \
  263. + interface/vchiq_arm/vchiq_shim.o \
  264. + interface/vchiq_arm/vchiq_util.o \
  265. + interface/vchiq_arm/vchiq_connected.o \
  266. +
  267. +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
  268. +
  269. --- /dev/null
  270. +++ b/drivers/misc/vc04_services/interface/vchi/connections/connection.h
  271. @@ -0,0 +1,328 @@
  272. +/**
  273. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  274. + *
  275. + * Redistribution and use in source and binary forms, with or without
  276. + * modification, are permitted provided that the following conditions
  277. + * are met:
  278. + * 1. Redistributions of source code must retain the above copyright
  279. + * notice, this list of conditions, and the following disclaimer,
  280. + * without modification.
  281. + * 2. Redistributions in binary form must reproduce the above copyright
  282. + * notice, this list of conditions and the following disclaimer in the
  283. + * documentation and/or other materials provided with the distribution.
  284. + * 3. The names of the above-listed copyright holders may not be used
  285. + * to endorse or promote products derived from this software without
  286. + * specific prior written permission.
  287. + *
  288. + * ALTERNATIVELY, this software may be distributed under the terms of the
  289. + * GNU General Public License ("GPL") version 2, as published by the Free
  290. + * Software Foundation.
  291. + *
  292. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  293. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  294. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  295. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  296. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  297. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  298. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  299. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  300. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  301. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  302. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  303. + */
  304. +
  305. +#ifndef CONNECTION_H_
  306. +#define CONNECTION_H_
  307. +
  308. +#include <linux/kernel.h>
  309. +#include <linux/types.h>
  310. +#include <linux/semaphore.h>
  311. +
  312. +#include "interface/vchi/vchi_cfg_internal.h"
  313. +#include "interface/vchi/vchi_common.h"
  314. +#include "interface/vchi/message_drivers/message.h"
  315. +
  316. +/******************************************************************************
  317. + Global defs
  318. + *****************************************************************************/
  319. +
  320. +// Opaque handle for a connection / service pair
  321. +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
  322. +
  323. +// opaque handle to the connection state information
  324. +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
  325. +
  326. +typedef struct vchi_connection_t VCHI_CONNECTION_T;
  327. +
  328. +
  329. +/******************************************************************************
  330. + API
  331. + *****************************************************************************/
  332. +
  333. +// Routine to init a connection with a particular low level driver
  334. +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
  335. + const VCHI_MESSAGE_DRIVER_T * driver );
  336. +
  337. +// Routine to control CRC enabling at a connection level
  338. +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
  339. + VCHI_CRC_CONTROL_T control );
  340. +
  341. +// Routine to create a service
  342. +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
  343. + int32_t service_id,
  344. + uint32_t rx_fifo_size,
  345. + uint32_t tx_fifo_size,
  346. + int server,
  347. + VCHI_CALLBACK_T callback,
  348. + void *callback_param,
  349. + int32_t want_crc,
  350. + int32_t want_unaligned_bulk_rx,
  351. + int32_t want_unaligned_bulk_tx,
  352. + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
  353. +
  354. +// Routine to close a service
  355. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
  356. +
  357. +// Routine to queue a message
  358. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  359. + const void *data,
  360. + uint32_t data_size,
  361. + VCHI_FLAGS_T flags,
  362. + void *msg_handle );
  363. +
  364. +// scatter-gather (vector) message queueing
  365. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  366. + VCHI_MSG_VECTOR_T *vector,
  367. + uint32_t count,
  368. + VCHI_FLAGS_T flags,
  369. + void *msg_handle );
  370. +
  371. +// Routine to dequeue a message
  372. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  373. + void *data,
  374. + uint32_t max_data_size_to_read,
  375. + uint32_t *actual_msg_size,
  376. + VCHI_FLAGS_T flags );
  377. +
  378. +// Routine to peek at a message
  379. +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  380. + void **data,
  381. + uint32_t *msg_size,
  382. + VCHI_FLAGS_T flags );
  383. +
  384. +// Routine to hold a message
  385. +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  386. + void **data,
  387. + uint32_t *msg_size,
  388. + VCHI_FLAGS_T flags,
  389. + void **message_handle );
  390. +
  391. +// Routine to initialise a received message iterator
  392. +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  393. + VCHI_MSG_ITER_T *iter,
  394. + VCHI_FLAGS_T flags );
  395. +
  396. +// Routine to release a held message
  397. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  398. + void *message_handle );
  399. +
  400. +// Routine to get info on a held message
  401. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  402. + void *message_handle,
  403. + void **data,
  404. + int32_t *msg_size,
  405. + uint32_t *tx_timestamp,
  406. + uint32_t *rx_timestamp );
  407. +
  408. +// Routine to check whether the iterator has a next message
  409. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  410. + const VCHI_MSG_ITER_T *iter );
  411. +
  412. +// Routine to advance the iterator
  413. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  414. + VCHI_MSG_ITER_T *iter,
  415. + void **data,
  416. + uint32_t *msg_size );
  417. +
  418. +// Routine to remove the last message returned by the iterator
  419. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  420. + VCHI_MSG_ITER_T *iter );
  421. +
  422. +// Routine to hold the last message returned by the iterator
  423. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  424. + VCHI_MSG_ITER_T *iter,
  425. + void **msg_handle );
  426. +
  427. +// Routine to transmit bulk data
  428. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  429. + const void *data_src,
  430. + uint32_t data_size,
  431. + VCHI_FLAGS_T flags,
  432. + void *bulk_handle );
  433. +
  434. +// Routine to receive data
  435. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  436. + void *data_dst,
  437. + uint32_t data_size,
  438. + VCHI_FLAGS_T flags,
  439. + void *bulk_handle );
  440. +
  441. +// Routine to report if a server is available
  442. +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
  443. +
  444. +// Routine to report the number of RX slots available
  445. +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
  446. +
  447. +// Routine to report the RX slot size
  448. +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
  449. +
  450. +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  451. +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
  452. + int32_t service,
  453. + uint32_t length,
  454. + MESSAGE_TX_CHANNEL_T channel,
  455. + uint32_t channel_params,
  456. + uint32_t data_length,
  457. + uint32_t data_offset);
  458. +
  459. +// Callback to inform a service that a Xon or Xoff message has been received
  460. +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
  461. +
  462. +// Callback to inform a service that a server available reply message has been received
  463. +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
  464. +
  465. +// Callback to indicate that bulk auxiliary messages have arrived
  466. +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
  467. +
  468. +// Callback to indicate that bulk auxiliary messages have arrived
  469. +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
  470. +
  471. +// Callback with all the connection info you require
  472. +typedef void (*VCHI_CONNECTION_INFO)(VCHI_CONNECTION_STATE_T *state, uint32_t protocol_version, uint32_t slot_size, uint32_t num_slots, uint32_t min_bulk_size);
  473. +
  474. +// Callback to inform of a disconnect
  475. +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
  476. +
  477. +// Callback to inform of a power control request
  478. +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
  479. +
  480. +// allocate memory suitably aligned for this connection
  481. +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
  482. +
  483. +// free memory allocated by buffer_allocate
  484. +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
  485. +
  486. +
  487. +/******************************************************************************
  488. + System driver struct
  489. + *****************************************************************************/
  490. +
  491. +struct opaque_vchi_connection_api_t
  492. +{
  493. + // Routine to init the connection
  494. + VCHI_CONNECTION_INIT_T init;
  495. +
  496. + // Connection-level CRC control
  497. + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
  498. +
  499. + // Routine to connect to or create service
  500. + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
  501. +
  502. + // Routine to disconnect from a service
  503. + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
  504. +
  505. + // Routine to queue a message
  506. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
  507. +
  508. + // scatter-gather (vector) message queue
  509. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
  510. +
  511. + // Routine to dequeue a message
  512. + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
  513. +
  514. + // Routine to peek at a message
  515. + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
  516. +
  517. + // Routine to hold a message
  518. + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
  519. +
  520. + // Routine to initialise a received message iterator
  521. + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
  522. +
  523. + // Routine to release a message
  524. + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
  525. +
  526. + // Routine to get information on a held message
  527. + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
  528. +
  529. + // Routine to check for next message on iterator
  530. + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
  531. +
  532. + // Routine to get next message on iterator
  533. + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
  534. +
  535. + // Routine to remove the last message returned by iterator
  536. + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
  537. +
  538. + // Routine to hold the last message returned by iterator
  539. + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
  540. +
  541. + // Routine to transmit bulk data
  542. + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
  543. +
  544. + // Routine to receive data
  545. + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
  546. +
  547. + // Routine to report the available servers
  548. + VCHI_CONNECTION_SERVER_PRESENT server_present;
  549. +
  550. + // Routine to report the number of RX slots available
  551. + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
  552. +
  553. + // Routine to report the RX slot size
  554. + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
  555. +
  556. + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  557. + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
  558. +
  559. + // Callback to inform a service that a Xon or Xoff message has been received
  560. + VCHI_CONNECTION_FLOW_CONTROL flow_control;
  561. +
  562. + // Callback to inform a service that a server available reply message has been received
  563. + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
  564. +
  565. + // Callback to indicate that bulk auxiliary messages have arrived
  566. + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
  567. +
  568. + // Callback to indicate that a bulk auxiliary message has been transmitted
  569. + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
  570. +
  571. + // Callback to provide information about the connection
  572. + VCHI_CONNECTION_INFO connection_info;
  573. +
  574. + // Callback to notify that peer has requested disconnect
  575. + VCHI_CONNECTION_DISCONNECT disconnect;
  576. +
  577. + // Callback to notify that peer has requested power change
  578. + VCHI_CONNECTION_POWER_CONTROL power_control;
  579. +
  580. + // allocate memory suitably aligned for this connection
  581. + VCHI_BUFFER_ALLOCATE buffer_allocate;
  582. +
  583. + // free memory allocated by buffer_allocate
  584. + VCHI_BUFFER_FREE buffer_free;
  585. +
  586. +};
  587. +
  588. +struct vchi_connection_t {
  589. + const VCHI_CONNECTION_API_T *api;
  590. + VCHI_CONNECTION_STATE_T *state;
  591. +#ifdef VCHI_COARSE_LOCKING
  592. + struct semaphore sem;
  593. +#endif
  594. +};
  595. +
  596. +
  597. +#endif /* CONNECTION_H_ */
  598. +
  599. +/****************************** End of file **********************************/
  600. --- /dev/null
  601. +++ b/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  602. @@ -0,0 +1,204 @@
  603. +/**
  604. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  605. + *
  606. + * Redistribution and use in source and binary forms, with or without
  607. + * modification, are permitted provided that the following conditions
  608. + * are met:
  609. + * 1. Redistributions of source code must retain the above copyright
  610. + * notice, this list of conditions, and the following disclaimer,
  611. + * without modification.
  612. + * 2. Redistributions in binary form must reproduce the above copyright
  613. + * notice, this list of conditions and the following disclaimer in the
  614. + * documentation and/or other materials provided with the distribution.
  615. + * 3. The names of the above-listed copyright holders may not be used
  616. + * to endorse or promote products derived from this software without
  617. + * specific prior written permission.
  618. + *
  619. + * ALTERNATIVELY, this software may be distributed under the terms of the
  620. + * GNU General Public License ("GPL") version 2, as published by the Free
  621. + * Software Foundation.
  622. + *
  623. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  624. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  625. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  626. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  627. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  628. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  629. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  630. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  631. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  632. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  633. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  634. + */
  635. +
  636. +#ifndef _VCHI_MESSAGE_H_
  637. +#define _VCHI_MESSAGE_H_
  638. +
  639. +#include <linux/kernel.h>
  640. +#include <linux/types.h>
  641. +#include <linux/semaphore.h>
  642. +
  643. +#include "interface/vchi/vchi_cfg_internal.h"
  644. +#include "interface/vchi/vchi_common.h"
  645. +
  646. +
  647. +typedef enum message_event_type {
  648. + MESSAGE_EVENT_NONE,
  649. + MESSAGE_EVENT_NOP,
  650. + MESSAGE_EVENT_MESSAGE,
  651. + MESSAGE_EVENT_SLOT_COMPLETE,
  652. + MESSAGE_EVENT_RX_BULK_PAUSED,
  653. + MESSAGE_EVENT_RX_BULK_COMPLETE,
  654. + MESSAGE_EVENT_TX_COMPLETE,
  655. + MESSAGE_EVENT_MSG_DISCARDED
  656. +} MESSAGE_EVENT_TYPE_T;
  657. +
  658. +typedef enum vchi_msg_flags
  659. +{
  660. + VCHI_MSG_FLAGS_NONE = 0x0,
  661. + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
  662. +} VCHI_MSG_FLAGS_T;
  663. +
  664. +typedef enum message_tx_channel
  665. +{
  666. + MESSAGE_TX_CHANNEL_MESSAGE = 0,
  667. + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  668. +} MESSAGE_TX_CHANNEL_T;
  669. +
  670. +// Macros used for cycling through bulk channels
  671. +#define MESSAGE_TX_CHANNEL_BULK_PREV(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION-1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  672. +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  673. +
  674. +typedef enum message_rx_channel
  675. +{
  676. + MESSAGE_RX_CHANNEL_MESSAGE = 0,
  677. + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  678. +} MESSAGE_RX_CHANNEL_T;
  679. +
  680. +// Message receive slot information
  681. +typedef struct rx_msg_slot_info {
  682. +
  683. + struct rx_msg_slot_info *next;
  684. + //struct slot_info *prev;
  685. +#if !defined VCHI_COARSE_LOCKING
  686. + struct semaphore sem;
  687. +#endif
  688. +
  689. + uint8_t *addr; // base address of slot
  690. + uint32_t len; // length of slot in bytes
  691. +
  692. + uint32_t write_ptr; // hardware causes this to advance
  693. + uint32_t read_ptr; // this module does the reading
  694. + int active; // is this slot in the hardware dma fifo?
  695. + uint32_t msgs_parsed; // count how many messages are in this slot
  696. + uint32_t msgs_released; // how many messages have been released
  697. + void *state; // connection state information
  698. + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
  699. +} RX_MSG_SLOTINFO_T;
  700. +
  701. +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
  702. +// In particular, it mustn't use addr and len - they're the client buffer, but the message
  703. +// driver will be tasked with sending the aligned core section.
  704. +typedef struct rx_bulk_slotinfo_t {
  705. + struct rx_bulk_slotinfo_t *next;
  706. +
  707. + struct semaphore *blocking;
  708. +
  709. + // needed by DMA
  710. + void *addr;
  711. + uint32_t len;
  712. +
  713. + // needed for the callback
  714. + void *service;
  715. + void *handle;
  716. + VCHI_FLAGS_T flags;
  717. +} RX_BULK_SLOTINFO_T;
  718. +
  719. +
  720. +/* ----------------------------------------------------------------------
  721. + * each connection driver will have a pool of the following struct.
  722. + *
  723. + * the pool will be managed by vchi_qman_*
  724. + * this means there will be multiple queues (single linked lists)
  725. + * a given struct message_info will be on exactly one of these queues
  726. + * at any one time
  727. + * -------------------------------------------------------------------- */
  728. +typedef struct rx_message_info {
  729. +
  730. + struct message_info *next;
  731. + //struct message_info *prev;
  732. +
  733. + uint8_t *addr;
  734. + uint32_t len;
  735. + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
  736. + uint32_t tx_timestamp;
  737. + uint32_t rx_timestamp;
  738. +
  739. +} RX_MESSAGE_INFO_T;
  740. +
  741. +typedef struct {
  742. + MESSAGE_EVENT_TYPE_T type;
  743. +
  744. + struct {
  745. + // for messages
  746. + void *addr; // address of message
  747. + uint16_t slot_delta; // whether this message indicated slot delta
  748. + uint32_t len; // length of message
  749. + RX_MSG_SLOTINFO_T *slot; // slot this message is in
  750. + int32_t service; // service id this message is destined for
  751. + uint32_t tx_timestamp; // timestamp from the header
  752. + uint32_t rx_timestamp; // timestamp when we parsed it
  753. + } message;
  754. +
  755. + // FIXME: cleanup slot reporting...
  756. + RX_MSG_SLOTINFO_T *rx_msg;
  757. + RX_BULK_SLOTINFO_T *rx_bulk;
  758. + void *tx_handle;
  759. + MESSAGE_TX_CHANNEL_T tx_channel;
  760. +
  761. +} MESSAGE_EVENT_T;
  762. +
  763. +
  764. +// callbacks
  765. +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
  766. +
  767. +typedef struct {
  768. + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
  769. +} VCHI_MESSAGE_DRIVER_OPEN_T;
  770. +
  771. +
  772. +// handle to this instance of message driver (as returned by ->open)
  773. +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
  774. +
  775. +struct opaque_vchi_message_driver_t {
  776. + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
  777. + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
  778. + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
  779. + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
  780. + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
  781. + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
  782. + int32_t (*send)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, VCHI_MSG_FLAGS_T flags, void *send_handle ); // tx (message & bulk)
  783. + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
  784. + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
  785. + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
  786. + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
  787. +
  788. + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
  789. + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
  790. + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
  791. + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
  792. + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  793. + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  794. +
  795. + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  796. + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  797. + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  798. + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
  799. + void (*form_bulk_aux)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, uint32_t chunk_size, const void **aux_data, int32_t *aux_len );
  800. + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
  801. +};
  802. +
  803. +
  804. +#endif // _VCHI_MESSAGE_H_
  805. +
  806. +/****************************** End of file ***********************************/
  807. --- /dev/null
  808. +++ b/drivers/misc/vc04_services/interface/vchi/vchi.h
  809. @@ -0,0 +1,378 @@
  810. +/**
  811. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  812. + *
  813. + * Redistribution and use in source and binary forms, with or without
  814. + * modification, are permitted provided that the following conditions
  815. + * are met:
  816. + * 1. Redistributions of source code must retain the above copyright
  817. + * notice, this list of conditions, and the following disclaimer,
  818. + * without modification.
  819. + * 2. Redistributions in binary form must reproduce the above copyright
  820. + * notice, this list of conditions and the following disclaimer in the
  821. + * documentation and/or other materials provided with the distribution.
  822. + * 3. The names of the above-listed copyright holders may not be used
  823. + * to endorse or promote products derived from this software without
  824. + * specific prior written permission.
  825. + *
  826. + * ALTERNATIVELY, this software may be distributed under the terms of the
  827. + * GNU General Public License ("GPL") version 2, as published by the Free
  828. + * Software Foundation.
  829. + *
  830. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  831. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  832. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  833. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  834. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  835. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  836. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  837. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  838. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  839. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  840. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  841. + */
  842. +
  843. +#ifndef VCHI_H_
  844. +#define VCHI_H_
  845. +
  846. +#include "interface/vchi/vchi_cfg.h"
  847. +#include "interface/vchi/vchi_common.h"
  848. +#include "interface/vchi/connections/connection.h"
  849. +#include "vchi_mh.h"
  850. +
  851. +
  852. +/******************************************************************************
  853. + Global defs
  854. + *****************************************************************************/
  855. +
  856. +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
  857. +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
  858. +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
  859. +
  860. +#ifdef USE_VCHIQ_ARM
  861. +#define VCHI_BULK_ALIGNED(x) 1
  862. +#else
  863. +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
  864. +#endif
  865. +
  866. +struct vchi_version {
  867. + uint32_t version;
  868. + uint32_t version_min;
  869. +};
  870. +#define VCHI_VERSION(v_) { v_, v_ }
  871. +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
  872. +
  873. +typedef enum
  874. +{
  875. + VCHI_VEC_POINTER,
  876. + VCHI_VEC_HANDLE,
  877. + VCHI_VEC_LIST
  878. +} VCHI_MSG_VECTOR_TYPE_T;
  879. +
  880. +typedef struct vchi_msg_vector_ex {
  881. +
  882. + VCHI_MSG_VECTOR_TYPE_T type;
  883. + union
  884. + {
  885. + // a memory handle
  886. + struct
  887. + {
  888. + VCHI_MEM_HANDLE_T handle;
  889. + uint32_t offset;
  890. + int32_t vec_len;
  891. + } handle;
  892. +
  893. + // an ordinary data pointer
  894. + struct
  895. + {
  896. + const void *vec_base;
  897. + int32_t vec_len;
  898. + } ptr;
  899. +
  900. + // a nested vector list
  901. + struct
  902. + {
  903. + struct vchi_msg_vector_ex *vec;
  904. + uint32_t vec_len;
  905. + } list;
  906. + } u;
  907. +} VCHI_MSG_VECTOR_EX_T;
  908. +
  909. +
  910. +// Construct an entry in a msg vector for a pointer (p) of length (l)
  911. +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
  912. +
  913. +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
  914. +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
  915. +
  916. +// Macros to manipulate 'FOURCC' values
  917. +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
  918. +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
  919. +
  920. +
  921. +// Opaque service information
  922. +struct opaque_vchi_service_t;
  923. +
  924. +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
  925. +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
  926. +typedef struct
  927. +{
  928. + struct opaque_vchi_service_t *service;
  929. + void *message;
  930. +} VCHI_HELD_MSG_T;
  931. +
  932. +
  933. +
  934. +// structure used to provide the information needed to open a server or a client
  935. +typedef struct {
  936. + struct vchi_version version;
  937. + int32_t service_id;
  938. + VCHI_CONNECTION_T *connection;
  939. + uint32_t rx_fifo_size;
  940. + uint32_t tx_fifo_size;
  941. + VCHI_CALLBACK_T callback;
  942. + void *callback_param;
  943. + /* client intends to receive bulk transfers of
  944. + odd lengths or into unaligned buffers */
  945. + int32_t want_unaligned_bulk_rx;
  946. + /* client intends to transmit bulk transfers of
  947. + odd lengths or out of unaligned buffers */
  948. + int32_t want_unaligned_bulk_tx;
  949. + /* client wants to check CRCs on (bulk) xfers.
  950. + Only needs to be set at 1 end - will do both directions. */
  951. + int32_t want_crc;
  952. +} SERVICE_CREATION_T;
  953. +
  954. +// Opaque handle for a VCHI instance
  955. +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
  956. +
  957. +// Opaque handle for a server or client
  958. +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
  959. +
  960. +// Service registration & startup
  961. +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
  962. +
  963. +typedef struct service_info_tag {
  964. + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
  965. + VCHI_SERVICE_INIT init; /* Service initialisation function */
  966. + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
  967. +} SERVICE_INFO_T;
  968. +
  969. +/******************************************************************************
  970. + Global funcs - implementation is specific to which side you are on (local / remote)
  971. + *****************************************************************************/
  972. +
  973. +#ifdef __cplusplus
  974. +extern "C" {
  975. +#endif
  976. +
  977. +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
  978. + const VCHI_MESSAGE_DRIVER_T * low_level);
  979. +
  980. +
  981. +// Routine used to initialise the vchi on both local + remote connections
  982. +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
  983. +
  984. +extern int32_t vchi_exit( void );
  985. +
  986. +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
  987. + const uint32_t num_connections,
  988. + VCHI_INSTANCE_T instance_handle );
  989. +
  990. +//When this is called, ensure that all services have no data pending.
  991. +//Bulk transfers can remain 'queued'
  992. +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
  993. +
  994. +// Global control over bulk CRC checking
  995. +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
  996. + VCHI_CRC_CONTROL_T control );
  997. +
  998. +// helper functions
  999. +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
  1000. +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
  1001. +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
  1002. +
  1003. +
  1004. +/******************************************************************************
  1005. + Global service API
  1006. + *****************************************************************************/
  1007. +// Routine to create a named service
  1008. +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
  1009. + SERVICE_CREATION_T *setup,
  1010. + VCHI_SERVICE_HANDLE_T *handle );
  1011. +
  1012. +// Routine to destory a service
  1013. +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
  1014. +
  1015. +// Routine to open a named service
  1016. +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
  1017. + SERVICE_CREATION_T *setup,
  1018. + VCHI_SERVICE_HANDLE_T *handle);
  1019. +
  1020. +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
  1021. + short *peer_version );
  1022. +
  1023. +// Routine to close a named service
  1024. +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
  1025. +
  1026. +// Routine to increment ref count on a named service
  1027. +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
  1028. +
  1029. +// Routine to decrement ref count on a named service
  1030. +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
  1031. +
  1032. +// Routine to set a control option for a named service
  1033. +extern int32_t vchi_service_set_option( const VCHI_SERVICE_HANDLE_T handle,
  1034. + VCHI_SERVICE_OPTION_T option,
  1035. + int value);
  1036. +
  1037. +// Routine to send a message across a service
  1038. +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
  1039. + const void *data,
  1040. + uint32_t data_size,
  1041. + VCHI_FLAGS_T flags,
  1042. + void *msg_handle );
  1043. +
  1044. +// scatter-gather (vector) and send message
  1045. +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
  1046. + VCHI_MSG_VECTOR_EX_T *vector,
  1047. + uint32_t count,
  1048. + VCHI_FLAGS_T flags,
  1049. + void *msg_handle );
  1050. +
  1051. +// legacy scatter-gather (vector) and send message, only handles pointers
  1052. +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
  1053. + VCHI_MSG_VECTOR_T *vector,
  1054. + uint32_t count,
  1055. + VCHI_FLAGS_T flags,
  1056. + void *msg_handle );
  1057. +
  1058. +// Routine to receive a msg from a service
  1059. +// Dequeue is equivalent to hold, copy into client buffer, release
  1060. +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
  1061. + void *data,
  1062. + uint32_t max_data_size_to_read,
  1063. + uint32_t *actual_msg_size,
  1064. + VCHI_FLAGS_T flags );
  1065. +
  1066. +// Routine to look at a message in place.
  1067. +// The message is not dequeued, so a subsequent call to peek or dequeue
  1068. +// will return the same message.
  1069. +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
  1070. + void **data,
  1071. + uint32_t *msg_size,
  1072. + VCHI_FLAGS_T flags );
  1073. +
  1074. +// Routine to remove a message after it has been read in place with peek
  1075. +// The first message on the queue is dequeued.
  1076. +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
  1077. +
  1078. +// Routine to look at a message in place.
  1079. +// The message is dequeued, so the caller is left holding it; the descriptor is
  1080. +// filled in and must be released when the user has finished with the message.
  1081. +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
  1082. + void **data, // } may be NULL, as info can be
  1083. + uint32_t *msg_size, // } obtained from HELD_MSG_T
  1084. + VCHI_FLAGS_T flags,
  1085. + VCHI_HELD_MSG_T *message_descriptor );
  1086. +
  1087. +// Initialise an iterator to look through messages in place
  1088. +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
  1089. + VCHI_MSG_ITER_T *iter,
  1090. + VCHI_FLAGS_T flags );
  1091. +
  1092. +/******************************************************************************
  1093. + Global service support API - operations on held messages and message iterators
  1094. + *****************************************************************************/
  1095. +
  1096. +// Routine to get the address of a held message
  1097. +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
  1098. +
  1099. +// Routine to get the size of a held message
  1100. +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
  1101. +
  1102. +// Routine to get the transmit timestamp as written into the header by the peer
  1103. +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
  1104. +
  1105. +// Routine to get the reception timestamp, written as we parsed the header
  1106. +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
  1107. +
  1108. +// Routine to release a held message after it has been processed
  1109. +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
  1110. +
  1111. +// Indicates whether the iterator has a next message.
  1112. +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
  1113. +
  1114. +// Return the pointer and length for the next message and advance the iterator.
  1115. +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
  1116. + void **data,
  1117. + uint32_t *msg_size );
  1118. +
  1119. +// Remove the last message returned by vchi_msg_iter_next.
  1120. +// Can only be called once after each call to vchi_msg_iter_next.
  1121. +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
  1122. +
  1123. +// Hold the last message returned by vchi_msg_iter_next.
  1124. +// Can only be called once after each call to vchi_msg_iter_next.
  1125. +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
  1126. + VCHI_HELD_MSG_T *message );
  1127. +
  1128. +// Return information for the next message, and hold it, advancing the iterator.
  1129. +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
  1130. + void **data, // } may be NULL
  1131. + uint32_t *msg_size, // }
  1132. + VCHI_HELD_MSG_T *message );
  1133. +
  1134. +
  1135. +/******************************************************************************
  1136. + Global bulk API
  1137. + *****************************************************************************/
  1138. +
  1139. +// Routine to prepare interface for a transfer from the other side
  1140. +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
  1141. + void *data_dst,
  1142. + uint32_t data_size,
  1143. + VCHI_FLAGS_T flags,
  1144. + void *transfer_handle );
  1145. +
  1146. +
  1147. +// Prepare interface for a transfer from the other side into relocatable memory.
  1148. +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
  1149. + VCHI_MEM_HANDLE_T h_dst,
  1150. + uint32_t offset,
  1151. + uint32_t data_size,
  1152. + const VCHI_FLAGS_T flags,
  1153. + void * const bulk_handle );
  1154. +
  1155. +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
  1156. +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
  1157. + const void *data_src,
  1158. + uint32_t data_size,
  1159. + VCHI_FLAGS_T flags,
  1160. + void *transfer_handle );
  1161. +
  1162. +
  1163. +/******************************************************************************
  1164. + Configuration plumbing
  1165. + *****************************************************************************/
  1166. +
  1167. +// function prototypes for the different mid layers (the state info gives the different physical connections)
  1168. +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
  1169. +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
  1170. +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
  1171. +
  1172. +// declare all message drivers here
  1173. +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
  1174. +
  1175. +#ifdef __cplusplus
  1176. +}
  1177. +#endif
  1178. +
  1179. +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
  1180. + VCHI_MEM_HANDLE_T h_src,
  1181. + uint32_t offset,
  1182. + uint32_t data_size,
  1183. + VCHI_FLAGS_T flags,
  1184. + void *transfer_handle );
  1185. +#endif /* VCHI_H_ */
  1186. +
  1187. +/****************************** End of file **********************************/
  1188. --- /dev/null
  1189. +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  1190. @@ -0,0 +1,224 @@
  1191. +/**
  1192. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1193. + *
  1194. + * Redistribution and use in source and binary forms, with or without
  1195. + * modification, are permitted provided that the following conditions
  1196. + * are met:
  1197. + * 1. Redistributions of source code must retain the above copyright
  1198. + * notice, this list of conditions, and the following disclaimer,
  1199. + * without modification.
  1200. + * 2. Redistributions in binary form must reproduce the above copyright
  1201. + * notice, this list of conditions and the following disclaimer in the
  1202. + * documentation and/or other materials provided with the distribution.
  1203. + * 3. The names of the above-listed copyright holders may not be used
  1204. + * to endorse or promote products derived from this software without
  1205. + * specific prior written permission.
  1206. + *
  1207. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1208. + * GNU General Public License ("GPL") version 2, as published by the Free
  1209. + * Software Foundation.
  1210. + *
  1211. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1212. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1213. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1214. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1215. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1216. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1217. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1218. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1219. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1220. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1221. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1222. + */
  1223. +
  1224. +#ifndef VCHI_CFG_H_
  1225. +#define VCHI_CFG_H_
  1226. +
  1227. +/****************************************************************************************
  1228. + * Defines in this first section are part of the VCHI API and may be examined by VCHI
  1229. + * services.
  1230. + ***************************************************************************************/
  1231. +
  1232. +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
  1233. +/* Really determined by the message driver, and should be available from a run-time call. */
  1234. +#ifndef VCHI_BULK_ALIGN
  1235. +# if __VCCOREVER__ >= 0x04000000
  1236. +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
  1237. +# else
  1238. +# define VCHI_BULK_ALIGN 16
  1239. +# endif
  1240. +#endif
  1241. +
  1242. +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
  1243. +/* May be less than or greater than VCHI_BULK_ALIGN */
  1244. +/* Really determined by the message driver, and should be available from a run-time call. */
  1245. +#ifndef VCHI_BULK_GRANULARITY
  1246. +# if __VCCOREVER__ >= 0x04000000
  1247. +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
  1248. +# else
  1249. +# define VCHI_BULK_GRANULARITY 16
  1250. +# endif
  1251. +#endif
  1252. +
  1253. +/* The largest possible message to be queued with vchi_msg_queue. */
  1254. +#ifndef VCHI_MAX_MSG_SIZE
  1255. +# if defined VCHI_LOCAL_HOST_PORT
  1256. +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
  1257. +# else
  1258. +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
  1259. +# endif
  1260. +#endif
  1261. +
  1262. +/******************************************************************************************
  1263. + * Defines below are system configuration options, and should not be used by VCHI services.
  1264. + *****************************************************************************************/
  1265. +
  1266. +/* How many connections can we support? A localhost implementation uses 2 connections,
  1267. + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
  1268. + * driver. */
  1269. +#ifndef VCHI_MAX_NUM_CONNECTIONS
  1270. +# define VCHI_MAX_NUM_CONNECTIONS 3
  1271. +#endif
  1272. +
  1273. +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
  1274. + * amount of static memory. */
  1275. +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
  1276. +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
  1277. +#endif
  1278. +
  1279. +/* Adjust if using a message driver that supports more logical TX channels */
  1280. +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
  1281. +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
  1282. +#endif
  1283. +
  1284. +/* Adjust if using a message driver that supports more logical RX channels */
  1285. +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
  1286. +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
  1287. +#endif
  1288. +
  1289. +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
  1290. + * receive queue space, less message headers. */
  1291. +#ifndef VCHI_NUM_READ_SLOTS
  1292. +# if defined(VCHI_LOCAL_HOST_PORT)
  1293. +# define VCHI_NUM_READ_SLOTS 4
  1294. +# else
  1295. +# define VCHI_NUM_READ_SLOTS 48
  1296. +# endif
  1297. +#endif
  1298. +
  1299. +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
  1300. + * performance. Only define on VideoCore end, talking to host.
  1301. + */
  1302. +//#define VCHI_MSG_RX_OVERRUN
  1303. +
  1304. +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
  1305. + * underneath VCHI will usually have its own buffering. */
  1306. +#ifndef VCHI_NUM_WRITE_SLOTS
  1307. +# define VCHI_NUM_WRITE_SLOTS 4
  1308. +#endif
  1309. +
  1310. +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
  1311. + * then it's taking up too much buffer space, and the peer service will be told to stop
  1312. + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
  1313. + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
  1314. + * is too high. */
  1315. +#ifndef VCHI_XOFF_THRESHOLD
  1316. +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
  1317. +#endif
  1318. +
  1319. +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
  1320. + * service has dequeued/released enough messages that it's now occupying
  1321. + * VCHI_XON_THRESHOLD slots or fewer. */
  1322. +#ifndef VCHI_XON_THRESHOLD
  1323. +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
  1324. +#endif
  1325. +
  1326. +/* A size below which a bulk transfer omits the handshake completely and always goes
  1327. + * via the message channel, if bulk auxiliary is being sent on that service. (The user
  1328. + * can guarantee this by enabling unaligned transmits).
  1329. + * Not API. */
  1330. +#ifndef VCHI_MIN_BULK_SIZE
  1331. +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
  1332. +#endif
  1333. +
  1334. +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
  1335. + * speed and latency; the smaller the chunk size the better change of messages and other
  1336. + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
  1337. + * break transmissions into chunks.
  1338. + */
  1339. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
  1340. +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
  1341. +#endif
  1342. +
  1343. +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
  1344. + * with multiple-line frames. Only use if the receiver can cope. */
  1345. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
  1346. +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
  1347. +#endif
  1348. +
  1349. +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
  1350. + * vchi_msg_queue will be blocked. */
  1351. +#ifndef VCHI_TX_MSG_QUEUE_SIZE
  1352. +# define VCHI_TX_MSG_QUEUE_SIZE 256
  1353. +#endif
  1354. +
  1355. +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
  1356. + * will be suspended until older messages are dequeued/released. */
  1357. +#ifndef VCHI_RX_MSG_QUEUE_SIZE
  1358. +# define VCHI_RX_MSG_QUEUE_SIZE 256
  1359. +#endif
  1360. +
  1361. +/* Really should be able to cope if we run out of received message descriptors, by
  1362. + * suspending parsing as the comment above says, but we don't. This sweeps the issue
  1363. + * under the carpet. */
  1364. +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  1365. +# undef VCHI_RX_MSG_QUEUE_SIZE
  1366. +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  1367. +#endif
  1368. +
  1369. +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
  1370. + * will be blocked. */
  1371. +#ifndef VCHI_TX_BULK_QUEUE_SIZE
  1372. +# define VCHI_TX_BULK_QUEUE_SIZE 64
  1373. +#endif
  1374. +
  1375. +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
  1376. + * will be blocked. */
  1377. +#ifndef VCHI_RX_BULK_QUEUE_SIZE
  1378. +# define VCHI_RX_BULK_QUEUE_SIZE 64
  1379. +#endif
  1380. +
  1381. +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
  1382. + * the peer asks for more than this, VCHI will fail and assert. The number is determined
  1383. + * by the peer's hardware - it's the number of outstanding requests that can be queued
  1384. + * on all bulk channels. VC3's MPHI peripheral allows 16. */
  1385. +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
  1386. +# define VCHI_MAX_PEER_BULK_REQUESTS 32
  1387. +#endif
  1388. +
  1389. +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
  1390. + * transmitter on and off.
  1391. + */
  1392. +/*#define VCHI_CCP2TX_MANUAL_POWER*/
  1393. +
  1394. +#ifndef VCHI_CCP2TX_MANUAL_POWER
  1395. +
  1396. +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
  1397. + * negative for no IDLE.
  1398. + */
  1399. +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
  1400. +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
  1401. +# endif
  1402. +
  1403. +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
  1404. + * negative for no OFF.
  1405. + */
  1406. +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
  1407. +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
  1408. +# endif
  1409. +
  1410. +#endif /* VCHI_CCP2TX_MANUAL_POWER */
  1411. +
  1412. +#endif /* VCHI_CFG_H_ */
  1413. +
  1414. +/****************************** End of file **********************************/
  1415. --- /dev/null
  1416. +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  1417. @@ -0,0 +1,71 @@
  1418. +/**
  1419. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1420. + *
  1421. + * Redistribution and use in source and binary forms, with or without
  1422. + * modification, are permitted provided that the following conditions
  1423. + * are met:
  1424. + * 1. Redistributions of source code must retain the above copyright
  1425. + * notice, this list of conditions, and the following disclaimer,
  1426. + * without modification.
  1427. + * 2. Redistributions in binary form must reproduce the above copyright
  1428. + * notice, this list of conditions and the following disclaimer in the
  1429. + * documentation and/or other materials provided with the distribution.
  1430. + * 3. The names of the above-listed copyright holders may not be used
  1431. + * to endorse or promote products derived from this software without
  1432. + * specific prior written permission.
  1433. + *
  1434. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1435. + * GNU General Public License ("GPL") version 2, as published by the Free
  1436. + * Software Foundation.
  1437. + *
  1438. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1439. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1440. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1441. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1442. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1443. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1444. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1445. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1446. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1447. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1448. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1449. + */
  1450. +
  1451. +#ifndef VCHI_CFG_INTERNAL_H_
  1452. +#define VCHI_CFG_INTERNAL_H_
  1453. +
  1454. +/****************************************************************************************
  1455. + * Control optimisation attempts.
  1456. + ***************************************************************************************/
  1457. +
  1458. +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
  1459. +#define VCHI_COARSE_LOCKING
  1460. +
  1461. +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
  1462. +// (only relevant if VCHI_COARSE_LOCKING)
  1463. +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
  1464. +
  1465. +// Avoid lock on non-blocking peek
  1466. +// (only relevant if VCHI_COARSE_LOCKING)
  1467. +#define VCHI_AVOID_PEEK_LOCK
  1468. +
  1469. +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
  1470. +#define VCHI_MULTIPLE_HANDLER_THREADS
  1471. +
  1472. +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
  1473. +// our way through the pool of descriptors.
  1474. +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
  1475. +
  1476. +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
  1477. +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
  1478. +
  1479. +// Don't use message descriptors for TX messages that don't need them
  1480. +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
  1481. +
  1482. +// Nano-locks for multiqueue
  1483. +//#define VCHI_MQUEUE_NANOLOCKS
  1484. +
  1485. +// Lock-free(er) dequeuing
  1486. +//#define VCHI_RX_NANOLOCKS
  1487. +
  1488. +#endif /*VCHI_CFG_INTERNAL_H_*/
  1489. --- /dev/null
  1490. +++ b/drivers/misc/vc04_services/interface/vchi/vchi_common.h
  1491. @@ -0,0 +1,175 @@
  1492. +/**
  1493. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1494. + *
  1495. + * Redistribution and use in source and binary forms, with or without
  1496. + * modification, are permitted provided that the following conditions
  1497. + * are met:
  1498. + * 1. Redistributions of source code must retain the above copyright
  1499. + * notice, this list of conditions, and the following disclaimer,
  1500. + * without modification.
  1501. + * 2. Redistributions in binary form must reproduce the above copyright
  1502. + * notice, this list of conditions and the following disclaimer in the
  1503. + * documentation and/or other materials provided with the distribution.
  1504. + * 3. The names of the above-listed copyright holders may not be used
  1505. + * to endorse or promote products derived from this software without
  1506. + * specific prior written permission.
  1507. + *
  1508. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1509. + * GNU General Public License ("GPL") version 2, as published by the Free
  1510. + * Software Foundation.
  1511. + *
  1512. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1513. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1514. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1515. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1516. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1517. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1518. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1519. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1520. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1521. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1522. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1523. + */
  1524. +
  1525. +#ifndef VCHI_COMMON_H_
  1526. +#define VCHI_COMMON_H_
  1527. +
  1528. +
  1529. +//flags used when sending messages (must be bitmapped)
  1530. +typedef enum
  1531. +{
  1532. + VCHI_FLAGS_NONE = 0x0,
  1533. + VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE = 0x1, // waits for message to be received, or sent (NB. not the same as being seen on other side)
  1534. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
  1535. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
  1536. + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
  1537. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
  1538. + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
  1539. +
  1540. + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
  1541. + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
  1542. + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
  1543. + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
  1544. + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
  1545. + VCHI_FLAGS_INTERNAL = 0xFF0000
  1546. +} VCHI_FLAGS_T;
  1547. +
  1548. +// constants for vchi_crc_control()
  1549. +typedef enum {
  1550. + VCHI_CRC_NOTHING = -1,
  1551. + VCHI_CRC_PER_SERVICE = 0,
  1552. + VCHI_CRC_EVERYTHING = 1,
  1553. +} VCHI_CRC_CONTROL_T;
  1554. +
  1555. +//callback reasons when an event occurs on a service
  1556. +typedef enum
  1557. +{
  1558. + VCHI_CALLBACK_REASON_MIN,
  1559. +
  1560. + //This indicates that there is data available
  1561. + //handle is the msg id that was transmitted with the data
  1562. + // When a message is received and there was no FULL message available previously, send callback
  1563. + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
  1564. + VCHI_CALLBACK_MSG_AVAILABLE,
  1565. + VCHI_CALLBACK_MSG_SENT,
  1566. + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
  1567. +
  1568. + // This indicates that a transfer from the other side has completed
  1569. + VCHI_CALLBACK_BULK_RECEIVED,
  1570. + //This indicates that data queued up to be sent has now gone
  1571. + //handle is the msg id that was used when sending the data
  1572. + VCHI_CALLBACK_BULK_SENT,
  1573. + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
  1574. + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
  1575. +
  1576. + VCHI_CALLBACK_SERVICE_CLOSED,
  1577. +
  1578. + // this side has sent XOFF to peer due to lack of data consumption by service
  1579. + // (suggests the service may need to take some recovery action if it has
  1580. + // been deliberately holding off consuming data)
  1581. + VCHI_CALLBACK_SENT_XOFF,
  1582. + VCHI_CALLBACK_SENT_XON,
  1583. +
  1584. + // indicates that a bulk transfer has finished reading the source buffer
  1585. + VCHI_CALLBACK_BULK_DATA_READ,
  1586. +
  1587. + // power notification events (currently host side only)
  1588. + VCHI_CALLBACK_PEER_OFF,
  1589. + VCHI_CALLBACK_PEER_SUSPENDED,
  1590. + VCHI_CALLBACK_PEER_ON,
  1591. + VCHI_CALLBACK_PEER_RESUMED,
  1592. + VCHI_CALLBACK_FORCED_POWER_OFF,
  1593. +
  1594. +#ifdef USE_VCHIQ_ARM
  1595. + // some extra notifications provided by vchiq_arm
  1596. + VCHI_CALLBACK_SERVICE_OPENED,
  1597. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  1598. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  1599. +#endif
  1600. +
  1601. + VCHI_CALLBACK_REASON_MAX
  1602. +} VCHI_CALLBACK_REASON_T;
  1603. +
  1604. +// service control options
  1605. +typedef enum
  1606. +{
  1607. + VCHI_SERVICE_OPTION_MIN,
  1608. +
  1609. + VCHI_SERVICE_OPTION_TRACE,
  1610. + VCHI_SERVICE_OPTION_SYNCHRONOUS,
  1611. +
  1612. + VCHI_SERVICE_OPTION_MAX
  1613. +} VCHI_SERVICE_OPTION_T;
  1614. +
  1615. +
  1616. +//Callback used by all services / bulk transfers
  1617. +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
  1618. + VCHI_CALLBACK_REASON_T reason,
  1619. + void *handle ); //for transmitting msg's only
  1620. +
  1621. +
  1622. +
  1623. +/*
  1624. + * Define vector struct for scatter-gather (vector) operations
  1625. + * Vectors can be nested - if a vector element has negative length, then
  1626. + * the data pointer is treated as pointing to another vector array, with
  1627. + * '-vec_len' elements. Thus to append a header onto an existing vector,
  1628. + * you can do this:
  1629. + *
  1630. + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
  1631. + * {
  1632. + * VCHI_MSG_VECTOR_T nv[2];
  1633. + * nv[0].vec_base = my_header;
  1634. + * nv[0].vec_len = sizeof my_header;
  1635. + * nv[1].vec_base = v;
  1636. + * nv[1].vec_len = -n;
  1637. + * ...
  1638. + *
  1639. + */
  1640. +typedef struct vchi_msg_vector {
  1641. + const void *vec_base;
  1642. + int32_t vec_len;
  1643. +} VCHI_MSG_VECTOR_T;
  1644. +
  1645. +// Opaque type for a connection API
  1646. +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
  1647. +
  1648. +// Opaque type for a message driver
  1649. +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
  1650. +
  1651. +
  1652. +// Iterator structure for reading ahead through received message queue. Allocated by client,
  1653. +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
  1654. +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
  1655. +// will not proceed to messages received since. Behaviour is undefined if an iterator
  1656. +// is used again after messages for that service are removed/dequeued by any
  1657. +// means other than vchi_msg_iter_... calls on the iterator itself.
  1658. +typedef struct {
  1659. + struct opaque_vchi_service_t *service;
  1660. + void *last;
  1661. + void *next;
  1662. + void *remove;
  1663. +} VCHI_MSG_ITER_T;
  1664. +
  1665. +
  1666. +#endif // VCHI_COMMON_H_
  1667. --- /dev/null
  1668. +++ b/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  1669. @@ -0,0 +1,42 @@
  1670. +/**
  1671. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1672. + *
  1673. + * Redistribution and use in source and binary forms, with or without
  1674. + * modification, are permitted provided that the following conditions
  1675. + * are met:
  1676. + * 1. Redistributions of source code must retain the above copyright
  1677. + * notice, this list of conditions, and the following disclaimer,
  1678. + * without modification.
  1679. + * 2. Redistributions in binary form must reproduce the above copyright
  1680. + * notice, this list of conditions and the following disclaimer in the
  1681. + * documentation and/or other materials provided with the distribution.
  1682. + * 3. The names of the above-listed copyright holders may not be used
  1683. + * to endorse or promote products derived from this software without
  1684. + * specific prior written permission.
  1685. + *
  1686. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1687. + * GNU General Public License ("GPL") version 2, as published by the Free
  1688. + * Software Foundation.
  1689. + *
  1690. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1691. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1692. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1693. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1694. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1695. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1696. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1697. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1698. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1699. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1700. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1701. + */
  1702. +
  1703. +#ifndef VCHI_MH_H_
  1704. +#define VCHI_MH_H_
  1705. +
  1706. +#include <linux/types.h>
  1707. +
  1708. +typedef int32_t VCHI_MEM_HANDLE_T;
  1709. +#define VCHI_MEM_HANDLE_INVALID 0
  1710. +
  1711. +#endif
  1712. --- /dev/null
  1713. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  1714. @@ -0,0 +1,40 @@
  1715. +/**
  1716. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1717. + *
  1718. + * Redistribution and use in source and binary forms, with or without
  1719. + * modification, are permitted provided that the following conditions
  1720. + * are met:
  1721. + * 1. Redistributions of source code must retain the above copyright
  1722. + * notice, this list of conditions, and the following disclaimer,
  1723. + * without modification.
  1724. + * 2. Redistributions in binary form must reproduce the above copyright
  1725. + * notice, this list of conditions and the following disclaimer in the
  1726. + * documentation and/or other materials provided with the distribution.
  1727. + * 3. The names of the above-listed copyright holders may not be used
  1728. + * to endorse or promote products derived from this software without
  1729. + * specific prior written permission.
  1730. + *
  1731. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1732. + * GNU General Public License ("GPL") version 2, as published by the Free
  1733. + * Software Foundation.
  1734. + *
  1735. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1736. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1737. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1738. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1739. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1740. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1741. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1742. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1743. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1744. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1745. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1746. + */
  1747. +
  1748. +#ifndef VCHIQ_VCHIQ_H
  1749. +#define VCHIQ_VCHIQ_H
  1750. +
  1751. +#include "vchiq_if.h"
  1752. +#include "vchiq_util.h"
  1753. +
  1754. +#endif
  1755. --- /dev/null
  1756. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  1757. @@ -0,0 +1,42 @@
  1758. +/**
  1759. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1760. + *
  1761. + * Redistribution and use in source and binary forms, with or without
  1762. + * modification, are permitted provided that the following conditions
  1763. + * are met:
  1764. + * 1. Redistributions of source code must retain the above copyright
  1765. + * notice, this list of conditions, and the following disclaimer,
  1766. + * without modification.
  1767. + * 2. Redistributions in binary form must reproduce the above copyright
  1768. + * notice, this list of conditions and the following disclaimer in the
  1769. + * documentation and/or other materials provided with the distribution.
  1770. + * 3. The names of the above-listed copyright holders may not be used
  1771. + * to endorse or promote products derived from this software without
  1772. + * specific prior written permission.
  1773. + *
  1774. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1775. + * GNU General Public License ("GPL") version 2, as published by the Free
  1776. + * Software Foundation.
  1777. + *
  1778. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1779. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1780. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1781. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1782. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1783. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1784. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1785. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1786. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1787. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1788. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1789. + */
  1790. +
  1791. +#ifndef VCHIQ_2835_H
  1792. +#define VCHIQ_2835_H
  1793. +
  1794. +#include "vchiq_pagelist.h"
  1795. +
  1796. +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
  1797. +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
  1798. +
  1799. +#endif /* VCHIQ_2835_H */
  1800. --- /dev/null
  1801. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  1802. @@ -0,0 +1,586 @@
  1803. +/**
  1804. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1805. + *
  1806. + * Redistribution and use in source and binary forms, with or without
  1807. + * modification, are permitted provided that the following conditions
  1808. + * are met:
  1809. + * 1. Redistributions of source code must retain the above copyright
  1810. + * notice, this list of conditions, and the following disclaimer,
  1811. + * without modification.
  1812. + * 2. Redistributions in binary form must reproduce the above copyright
  1813. + * notice, this list of conditions and the following disclaimer in the
  1814. + * documentation and/or other materials provided with the distribution.
  1815. + * 3. The names of the above-listed copyright holders may not be used
  1816. + * to endorse or promote products derived from this software without
  1817. + * specific prior written permission.
  1818. + *
  1819. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1820. + * GNU General Public License ("GPL") version 2, as published by the Free
  1821. + * Software Foundation.
  1822. + *
  1823. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1824. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1825. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1826. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1827. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1828. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1829. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1830. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1831. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1832. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1833. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1834. + */
  1835. +
  1836. +#include <linux/kernel.h>
  1837. +#include <linux/types.h>
  1838. +#include <linux/errno.h>
  1839. +#include <linux/interrupt.h>
  1840. +#include <linux/pagemap.h>
  1841. +#include <linux/dma-mapping.h>
  1842. +#include <linux/version.h>
  1843. +#include <linux/io.h>
  1844. +#include <linux/platform_device.h>
  1845. +#include <linux/uaccess.h>
  1846. +#include <linux/of.h>
  1847. +#include <asm/pgtable.h>
  1848. +#include <soc/bcm2835/raspberrypi-firmware.h>
  1849. +
  1850. +#define dmac_map_area __glue(_CACHE,_dma_map_area)
  1851. +#define dmac_unmap_area __glue(_CACHE,_dma_unmap_area)
  1852. +
  1853. +extern void dmac_map_area(const void *, size_t, int);
  1854. +extern void dmac_unmap_area(const void *, size_t, int);
  1855. +
  1856. +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
  1857. +
  1858. +#define VCHIQ_ARM_ADDRESS(x) ((void *)((char *)x + g_virt_to_bus_offset))
  1859. +
  1860. +#include "vchiq_arm.h"
  1861. +#include "vchiq_2835.h"
  1862. +#include "vchiq_connected.h"
  1863. +#include "vchiq_killable.h"
  1864. +
  1865. +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
  1866. +
  1867. +#define BELL0 0x00
  1868. +#define BELL2 0x08
  1869. +
  1870. +typedef struct vchiq_2835_state_struct {
  1871. + int inited;
  1872. + VCHIQ_ARM_STATE_T arm_state;
  1873. +} VCHIQ_2835_ARM_STATE_T;
  1874. +
  1875. +static void __iomem *g_regs;
  1876. +static unsigned int g_cache_line_size = sizeof(CACHE_LINE_SIZE);
  1877. +static unsigned int g_fragments_size;
  1878. +static char *g_fragments_base;
  1879. +static char *g_free_fragments;
  1880. +static struct semaphore g_free_fragments_sema;
  1881. +static unsigned long g_virt_to_bus_offset;
  1882. +
  1883. +extern int vchiq_arm_log_level;
  1884. +
  1885. +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
  1886. +
  1887. +static irqreturn_t
  1888. +vchiq_doorbell_irq(int irq, void *dev_id);
  1889. +
  1890. +static int
  1891. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  1892. + struct task_struct *task, PAGELIST_T ** ppagelist);
  1893. +
  1894. +static void
  1895. +free_pagelist(PAGELIST_T *pagelist, int actual);
  1896. +
  1897. +int vchiq_platform_init(struct platform_device *pdev, VCHIQ_STATE_T *state)
  1898. +{
  1899. + struct device *dev = &pdev->dev;
  1900. + struct rpi_firmware *fw = platform_get_drvdata(pdev);
  1901. + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
  1902. + struct resource *res;
  1903. + void *slot_mem;
  1904. + dma_addr_t slot_phys;
  1905. + u32 channelbase;
  1906. + int slot_mem_size, frag_mem_size;
  1907. + int err, irq, i;
  1908. +
  1909. + g_virt_to_bus_offset = virt_to_dma(dev, (void *)0);
  1910. +
  1911. + (void)of_property_read_u32(dev->of_node, "cache-line-size",
  1912. + &g_cache_line_size);
  1913. + g_fragments_size = 2 * g_cache_line_size;
  1914. +
  1915. + /* Allocate space for the channels in coherent memory */
  1916. + slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
  1917. + frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
  1918. +
  1919. + slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
  1920. + &slot_phys, GFP_KERNEL);
  1921. + if (!slot_mem) {
  1922. + dev_err(dev, "could not allocate DMA memory\n");
  1923. + return -ENOMEM;
  1924. + }
  1925. +
  1926. + WARN_ON(((int)slot_mem & (PAGE_SIZE - 1)) != 0);
  1927. +
  1928. + vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
  1929. + if (!vchiq_slot_zero)
  1930. + return -EINVAL;
  1931. +
  1932. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
  1933. + (int)slot_phys + slot_mem_size;
  1934. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
  1935. + MAX_FRAGMENTS;
  1936. +
  1937. + g_fragments_base = (char *)slot_mem + slot_mem_size;
  1938. + slot_mem_size += frag_mem_size;
  1939. +
  1940. + g_free_fragments = g_fragments_base;
  1941. + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
  1942. + *(char **)&g_fragments_base[i*g_fragments_size] =
  1943. + &g_fragments_base[(i + 1)*g_fragments_size];
  1944. + }
  1945. + *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
  1946. + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
  1947. +
  1948. + if (vchiq_init_state(state, vchiq_slot_zero, 0) != VCHIQ_SUCCESS)
  1949. + return -EINVAL;
  1950. +
  1951. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1952. + g_regs = devm_ioremap_resource(&pdev->dev, res);
  1953. + if (IS_ERR(g_regs))
  1954. + return PTR_ERR(g_regs);
  1955. +
  1956. + irq = platform_get_irq(pdev, 0);
  1957. + if (irq <= 0) {
  1958. + dev_err(dev, "failed to get IRQ\n");
  1959. + return irq;
  1960. + }
  1961. +
  1962. + err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
  1963. + "VCHIQ doorbell", state);
  1964. + if (err) {
  1965. + dev_err(dev, "failed to register irq=%d\n", irq);
  1966. + return err;
  1967. + }
  1968. +
  1969. + /* Send the base address of the slots to VideoCore */
  1970. + channelbase = slot_phys;
  1971. + err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
  1972. + &channelbase, sizeof(channelbase));
  1973. + if (err || channelbase) {
  1974. + dev_err(dev, "failed to set channelbase\n");
  1975. + return err ? : -ENXIO;
  1976. + }
  1977. +
  1978. + vchiq_log_info(vchiq_arm_log_level,
  1979. + "vchiq_init - done (slots %x, phys %pad)",
  1980. + (unsigned int)vchiq_slot_zero, &slot_phys);
  1981. +
  1982. + vchiq_call_connected_callbacks();
  1983. +
  1984. + return 0;
  1985. +}
  1986. +
  1987. +VCHIQ_STATUS_T
  1988. +vchiq_platform_init_state(VCHIQ_STATE_T *state)
  1989. +{
  1990. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  1991. + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
  1992. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
  1993. + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
  1994. + if(status != VCHIQ_SUCCESS)
  1995. + {
  1996. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
  1997. + }
  1998. + return status;
  1999. +}
  2000. +
  2001. +VCHIQ_ARM_STATE_T*
  2002. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
  2003. +{
  2004. + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
  2005. + {
  2006. + BUG();
  2007. + }
  2008. + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
  2009. +}
  2010. +
  2011. +void
  2012. +remote_event_signal(REMOTE_EVENT_T *event)
  2013. +{
  2014. + wmb();
  2015. +
  2016. + event->fired = 1;
  2017. +
  2018. + dsb(); /* data barrier operation */
  2019. +
  2020. + if (event->armed)
  2021. + writel(0, g_regs + BELL2); /* trigger vc interrupt */
  2022. +}
  2023. +
  2024. +int
  2025. +vchiq_copy_from_user(void *dst, const void *src, int size)
  2026. +{
  2027. + if ((uint32_t)src < TASK_SIZE) {
  2028. + return copy_from_user(dst, src, size);
  2029. + } else {
  2030. + memcpy(dst, src, size);
  2031. + return 0;
  2032. + }
  2033. +}
  2034. +
  2035. +VCHIQ_STATUS_T
  2036. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
  2037. + void *offset, int size, int dir)
  2038. +{
  2039. + PAGELIST_T *pagelist;
  2040. + int ret;
  2041. +
  2042. + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
  2043. +
  2044. + ret = create_pagelist((char __user *)offset, size,
  2045. + (dir == VCHIQ_BULK_RECEIVE)
  2046. + ? PAGELIST_READ
  2047. + : PAGELIST_WRITE,
  2048. + current,
  2049. + &pagelist);
  2050. + if (ret != 0)
  2051. + return VCHIQ_ERROR;
  2052. +
  2053. + bulk->handle = memhandle;
  2054. + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
  2055. +
  2056. + /* Store the pagelist address in remote_data, which isn't used by the
  2057. + slave. */
  2058. + bulk->remote_data = pagelist;
  2059. +
  2060. + return VCHIQ_SUCCESS;
  2061. +}
  2062. +
  2063. +void
  2064. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
  2065. +{
  2066. + if (bulk && bulk->remote_data && bulk->actual)
  2067. + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
  2068. +}
  2069. +
  2070. +void
  2071. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
  2072. +{
  2073. + /*
  2074. + * This should only be called on the master (VideoCore) side, but
  2075. + * provide an implementation to avoid the need for ifdefery.
  2076. + */
  2077. + BUG();
  2078. +}
  2079. +
  2080. +void
  2081. +vchiq_dump_platform_state(void *dump_context)
  2082. +{
  2083. + char buf[80];
  2084. + int len;
  2085. + len = snprintf(buf, sizeof(buf),
  2086. + " Platform: 2835 (VC master)");
  2087. + vchiq_dump(dump_context, buf, len + 1);
  2088. +}
  2089. +
  2090. +VCHIQ_STATUS_T
  2091. +vchiq_platform_suspend(VCHIQ_STATE_T *state)
  2092. +{
  2093. + return VCHIQ_ERROR;
  2094. +}
  2095. +
  2096. +VCHIQ_STATUS_T
  2097. +vchiq_platform_resume(VCHIQ_STATE_T *state)
  2098. +{
  2099. + return VCHIQ_SUCCESS;
  2100. +}
  2101. +
  2102. +void
  2103. +vchiq_platform_paused(VCHIQ_STATE_T *state)
  2104. +{
  2105. +}
  2106. +
  2107. +void
  2108. +vchiq_platform_resumed(VCHIQ_STATE_T *state)
  2109. +{
  2110. +}
  2111. +
  2112. +int
  2113. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
  2114. +{
  2115. + return 1; // autosuspend not supported - videocore always wanted
  2116. +}
  2117. +
  2118. +int
  2119. +vchiq_platform_use_suspend_timer(void)
  2120. +{
  2121. + return 0;
  2122. +}
  2123. +void
  2124. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
  2125. +{
  2126. + vchiq_log_info(vchiq_arm_log_level, "Suspend timer not in use");
  2127. +}
  2128. +void
  2129. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
  2130. +{
  2131. + (void)state;
  2132. +}
  2133. +/*
  2134. + * Local functions
  2135. + */
  2136. +
  2137. +static irqreturn_t
  2138. +vchiq_doorbell_irq(int irq, void *dev_id)
  2139. +{
  2140. + VCHIQ_STATE_T *state = dev_id;
  2141. + irqreturn_t ret = IRQ_NONE;
  2142. + unsigned int status;
  2143. +
  2144. + /* Read (and clear) the doorbell */
  2145. + status = readl(g_regs + BELL0);
  2146. +
  2147. + if (status & 0x4) { /* Was the doorbell rung? */
  2148. + remote_event_pollall(state);
  2149. + ret = IRQ_HANDLED;
  2150. + }
  2151. +
  2152. + return ret;
  2153. +}
  2154. +
  2155. +/* There is a potential problem with partial cache lines (pages?)
  2156. +** at the ends of the block when reading. If the CPU accessed anything in
  2157. +** the same line (page?) then it may have pulled old data into the cache,
  2158. +** obscuring the new data underneath. We can solve this by transferring the
  2159. +** partial cache lines separately, and allowing the ARM to copy into the
  2160. +** cached area.
  2161. +
  2162. +** N.B. This implementation plays slightly fast and loose with the Linux
  2163. +** driver programming rules, e.g. its use of dmac_map_area instead of
  2164. +** dma_map_single, but it isn't a multi-platform driver and it benefits
  2165. +** from increased speed as a result.
  2166. +*/
  2167. +
  2168. +static int
  2169. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  2170. + struct task_struct *task, PAGELIST_T ** ppagelist)
  2171. +{
  2172. + PAGELIST_T *pagelist;
  2173. + struct page **pages;
  2174. + unsigned long *addrs;
  2175. + unsigned int num_pages, offset, i;
  2176. + char *addr, *base_addr, *next_addr;
  2177. + int run, addridx, actual_pages;
  2178. + unsigned long *need_release;
  2179. +
  2180. + offset = (unsigned int)buf & (PAGE_SIZE - 1);
  2181. + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
  2182. +
  2183. + *ppagelist = NULL;
  2184. +
  2185. + /* Allocate enough storage to hold the page pointers and the page
  2186. + ** list
  2187. + */
  2188. + pagelist = kmalloc(sizeof(PAGELIST_T) +
  2189. + (num_pages * sizeof(unsigned long)) +
  2190. + sizeof(unsigned long) +
  2191. + (num_pages * sizeof(pages[0])),
  2192. + GFP_KERNEL);
  2193. +
  2194. + vchiq_log_trace(vchiq_arm_log_level,
  2195. + "create_pagelist - %x", (unsigned int)pagelist);
  2196. + if (!pagelist)
  2197. + return -ENOMEM;
  2198. +
  2199. + addrs = pagelist->addrs;
  2200. + need_release = (unsigned long *)(addrs + num_pages);
  2201. + pages = (struct page **)(addrs + num_pages + 1);
  2202. +
  2203. + if (is_vmalloc_addr(buf)) {
  2204. + int dir = (type == PAGELIST_WRITE) ?
  2205. + DMA_TO_DEVICE : DMA_FROM_DEVICE;
  2206. + unsigned long length = count;
  2207. + unsigned int off = offset;
  2208. +
  2209. + for (actual_pages = 0; actual_pages < num_pages;
  2210. + actual_pages++) {
  2211. + struct page *pg = vmalloc_to_page(buf + (actual_pages *
  2212. + PAGE_SIZE));
  2213. + size_t bytes = PAGE_SIZE - off;
  2214. +
  2215. + if (bytes > length)
  2216. + bytes = length;
  2217. + pages[actual_pages] = pg;
  2218. + dmac_map_area(page_address(pg) + off, bytes, dir);
  2219. + length -= bytes;
  2220. + off = 0;
  2221. + }
  2222. + *need_release = 0; /* do not try and release vmalloc pages */
  2223. + } else {
  2224. + down_read(&task->mm->mmap_sem);
  2225. + actual_pages = get_user_pages(task, task->mm,
  2226. + (unsigned long)buf & ~(PAGE_SIZE - 1),
  2227. + num_pages,
  2228. + (type == PAGELIST_READ) /*Write */ ,
  2229. + 0 /*Force */ ,
  2230. + pages,
  2231. + NULL /*vmas */);
  2232. + up_read(&task->mm->mmap_sem);
  2233. +
  2234. + if (actual_pages != num_pages) {
  2235. + vchiq_log_info(vchiq_arm_log_level,
  2236. + "create_pagelist - only %d/%d pages locked",
  2237. + actual_pages,
  2238. + num_pages);
  2239. +
  2240. + /* This is probably due to the process being killed */
  2241. + while (actual_pages > 0)
  2242. + {
  2243. + actual_pages--;
  2244. + page_cache_release(pages[actual_pages]);
  2245. + }
  2246. + kfree(pagelist);
  2247. + if (actual_pages == 0)
  2248. + actual_pages = -ENOMEM;
  2249. + return actual_pages;
  2250. + }
  2251. + *need_release = 1; /* release user pages */
  2252. + }
  2253. +
  2254. + pagelist->length = count;
  2255. + pagelist->type = type;
  2256. + pagelist->offset = offset;
  2257. +
  2258. + /* Group the pages into runs of contiguous pages */
  2259. +
  2260. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  2261. + next_addr = base_addr + PAGE_SIZE;
  2262. + addridx = 0;
  2263. + run = 0;
  2264. +
  2265. + for (i = 1; i < num_pages; i++) {
  2266. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  2267. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  2268. + next_addr += PAGE_SIZE;
  2269. + run++;
  2270. + } else {
  2271. + addrs[addridx] = (unsigned long)base_addr + run;
  2272. + addridx++;
  2273. + base_addr = addr;
  2274. + next_addr = addr + PAGE_SIZE;
  2275. + run = 0;
  2276. + }
  2277. + }
  2278. +
  2279. + addrs[addridx] = (unsigned long)base_addr + run;
  2280. + addridx++;
  2281. +
  2282. + /* Partial cache lines (fragments) require special measures */
  2283. + if ((type == PAGELIST_READ) &&
  2284. + ((pagelist->offset & (g_cache_line_size - 1)) ||
  2285. + ((pagelist->offset + pagelist->length) &
  2286. + (g_cache_line_size - 1)))) {
  2287. + char *fragments;
  2288. +
  2289. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  2290. + kfree(pagelist);
  2291. + return -EINTR;
  2292. + }
  2293. +
  2294. + WARN_ON(g_free_fragments == NULL);
  2295. +
  2296. + down(&g_free_fragments_mutex);
  2297. + fragments = g_free_fragments;
  2298. + WARN_ON(fragments == NULL);
  2299. + g_free_fragments = *(char **) g_free_fragments;
  2300. + up(&g_free_fragments_mutex);
  2301. + pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
  2302. + (fragments - g_fragments_base) / g_fragments_size;
  2303. + }
  2304. +
  2305. + dmac_flush_range(pagelist, addrs + num_pages);
  2306. +
  2307. + *ppagelist = pagelist;
  2308. +
  2309. + return 0;
  2310. +}
  2311. +
  2312. +static void
  2313. +free_pagelist(PAGELIST_T *pagelist, int actual)
  2314. +{
  2315. + unsigned long *need_release;
  2316. + struct page **pages;
  2317. + unsigned int num_pages, i;
  2318. +
  2319. + vchiq_log_trace(vchiq_arm_log_level,
  2320. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  2321. +
  2322. + num_pages =
  2323. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  2324. + PAGE_SIZE;
  2325. +
  2326. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  2327. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  2328. +
  2329. + /* Deal with any partial cache lines (fragments) */
  2330. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  2331. + char *fragments = g_fragments_base +
  2332. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
  2333. + g_fragments_size;
  2334. + int head_bytes, tail_bytes;
  2335. + head_bytes = (g_cache_line_size - pagelist->offset) &
  2336. + (g_cache_line_size - 1);
  2337. + tail_bytes = (pagelist->offset + actual) &
  2338. + (g_cache_line_size - 1);
  2339. +
  2340. + if ((actual >= 0) && (head_bytes != 0)) {
  2341. + if (head_bytes > actual)
  2342. + head_bytes = actual;
  2343. +
  2344. + memcpy((char *)page_address(pages[0]) +
  2345. + pagelist->offset,
  2346. + fragments,
  2347. + head_bytes);
  2348. + }
  2349. + if ((actual >= 0) && (head_bytes < actual) &&
  2350. + (tail_bytes != 0)) {
  2351. + memcpy((char *)page_address(pages[num_pages - 1]) +
  2352. + ((pagelist->offset + actual) &
  2353. + (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
  2354. + fragments + g_cache_line_size,
  2355. + tail_bytes);
  2356. + }
  2357. +
  2358. + down(&g_free_fragments_mutex);
  2359. + *(char **)fragments = g_free_fragments;
  2360. + g_free_fragments = fragments;
  2361. + up(&g_free_fragments_mutex);
  2362. + up(&g_free_fragments_sema);
  2363. + }
  2364. +
  2365. + if (*need_release) {
  2366. + unsigned int length = pagelist->length;
  2367. + unsigned int offset = pagelist->offset;
  2368. +
  2369. + for (i = 0; i < num_pages; i++) {
  2370. + struct page *pg = pages[i];
  2371. +
  2372. + if (pagelist->type != PAGELIST_WRITE) {
  2373. + unsigned int bytes = PAGE_SIZE - offset;
  2374. +
  2375. + if (bytes > length)
  2376. + bytes = length;
  2377. + dmac_unmap_area(page_address(pg) + offset,
  2378. + bytes, DMA_FROM_DEVICE);
  2379. + length -= bytes;
  2380. + offset = 0;
  2381. + set_page_dirty(pg);
  2382. + }
  2383. + page_cache_release(pg);
  2384. + }
  2385. + }
  2386. +
  2387. + kfree(pagelist);
  2388. +}
  2389. --- /dev/null
  2390. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  2391. @@ -0,0 +1,2903 @@
  2392. +/**
  2393. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  2394. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  2395. + *
  2396. + * Redistribution and use in source and binary forms, with or without
  2397. + * modification, are permitted provided that the following conditions
  2398. + * are met:
  2399. + * 1. Redistributions of source code must retain the above copyright
  2400. + * notice, this list of conditions, and the following disclaimer,
  2401. + * without modification.
  2402. + * 2. Redistributions in binary form must reproduce the above copyright
  2403. + * notice, this list of conditions and the following disclaimer in the
  2404. + * documentation and/or other materials provided with the distribution.
  2405. + * 3. The names of the above-listed copyright holders may not be used
  2406. + * to endorse or promote products derived from this software without
  2407. + * specific prior written permission.
  2408. + *
  2409. + * ALTERNATIVELY, this software may be distributed under the terms of the
  2410. + * GNU General Public License ("GPL") version 2, as published by the Free
  2411. + * Software Foundation.
  2412. + *
  2413. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  2414. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  2415. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  2416. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  2417. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  2418. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  2419. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  2420. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  2421. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2422. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2423. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2424. + */
  2425. +
  2426. +#include <linux/kernel.h>
  2427. +#include <linux/module.h>
  2428. +#include <linux/types.h>
  2429. +#include <linux/errno.h>
  2430. +#include <linux/cdev.h>
  2431. +#include <linux/fs.h>
  2432. +#include <linux/device.h>
  2433. +#include <linux/mm.h>
  2434. +#include <linux/highmem.h>
  2435. +#include <linux/pagemap.h>
  2436. +#include <linux/bug.h>
  2437. +#include <linux/semaphore.h>
  2438. +#include <linux/list.h>
  2439. +#include <linux/of.h>
  2440. +#include <linux/platform_device.h>
  2441. +#include <soc/bcm2835/raspberrypi-firmware.h>
  2442. +
  2443. +#include "vchiq_core.h"
  2444. +#include "vchiq_ioctl.h"
  2445. +#include "vchiq_arm.h"
  2446. +#include "vchiq_debugfs.h"
  2447. +#include "vchiq_killable.h"
  2448. +
  2449. +#define DEVICE_NAME "vchiq"
  2450. +
  2451. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  2452. +#undef MODULE_PARAM_PREFIX
  2453. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  2454. +
  2455. +#define VCHIQ_MINOR 0
  2456. +
  2457. +/* Some per-instance constants */
  2458. +#define MAX_COMPLETIONS 16
  2459. +#define MAX_SERVICES 64
  2460. +#define MAX_ELEMENTS 8
  2461. +#define MSG_QUEUE_SIZE 64
  2462. +
  2463. +#define KEEPALIVE_VER 1
  2464. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  2465. +
  2466. +/* Run time control of log level, based on KERN_XXX level. */
  2467. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  2468. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  2469. +
  2470. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  2471. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  2472. +
  2473. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  2474. +static const char *const suspend_state_names[] = {
  2475. + "VC_SUSPEND_FORCE_CANCELED",
  2476. + "VC_SUSPEND_REJECTED",
  2477. + "VC_SUSPEND_FAILED",
  2478. + "VC_SUSPEND_IDLE",
  2479. + "VC_SUSPEND_REQUESTED",
  2480. + "VC_SUSPEND_IN_PROGRESS",
  2481. + "VC_SUSPEND_SUSPENDED"
  2482. +};
  2483. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  2484. +static const char *const resume_state_names[] = {
  2485. + "VC_RESUME_FAILED",
  2486. + "VC_RESUME_IDLE",
  2487. + "VC_RESUME_REQUESTED",
  2488. + "VC_RESUME_IN_PROGRESS",
  2489. + "VC_RESUME_RESUMED"
  2490. +};
  2491. +/* The number of times we allow force suspend to timeout before actually
  2492. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  2493. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  2494. +*/
  2495. +#define FORCE_SUSPEND_FAIL_MAX 8
  2496. +
  2497. +/* The time in ms allowed for videocore to go idle when force suspend has been
  2498. + * requested */
  2499. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  2500. +
  2501. +
  2502. +static void suspend_timer_callback(unsigned long context);
  2503. +
  2504. +
  2505. +typedef struct user_service_struct {
  2506. + VCHIQ_SERVICE_T *service;
  2507. + void *userdata;
  2508. + VCHIQ_INSTANCE_T instance;
  2509. + char is_vchi;
  2510. + char dequeue_pending;
  2511. + char close_pending;
  2512. + int message_available_pos;
  2513. + int msg_insert;
  2514. + int msg_remove;
  2515. + struct semaphore insert_event;
  2516. + struct semaphore remove_event;
  2517. + struct semaphore close_event;
  2518. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  2519. +} USER_SERVICE_T;
  2520. +
  2521. +struct bulk_waiter_node {
  2522. + struct bulk_waiter bulk_waiter;
  2523. + int pid;
  2524. + struct list_head list;
  2525. +};
  2526. +
  2527. +struct vchiq_instance_struct {
  2528. + VCHIQ_STATE_T *state;
  2529. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  2530. + int completion_insert;
  2531. + int completion_remove;
  2532. + struct semaphore insert_event;
  2533. + struct semaphore remove_event;
  2534. + struct mutex completion_mutex;
  2535. +
  2536. + int connected;
  2537. + int closing;
  2538. + int pid;
  2539. + int mark;
  2540. + int use_close_delivered;
  2541. + int trace;
  2542. +
  2543. + struct list_head bulk_waiter_list;
  2544. + struct mutex bulk_waiter_list_mutex;
  2545. +
  2546. + VCHIQ_DEBUGFS_NODE_T debugfs_node;
  2547. +};
  2548. +
  2549. +typedef struct dump_context_struct {
  2550. + char __user *buf;
  2551. + size_t actual;
  2552. + size_t space;
  2553. + loff_t offset;
  2554. +} DUMP_CONTEXT_T;
  2555. +
  2556. +static struct cdev vchiq_cdev;
  2557. +static dev_t vchiq_devid;
  2558. +static VCHIQ_STATE_T g_state;
  2559. +static struct class *vchiq_class;
  2560. +static struct device *vchiq_dev;
  2561. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  2562. +
  2563. +static const char *const ioctl_names[] = {
  2564. + "CONNECT",
  2565. + "SHUTDOWN",
  2566. + "CREATE_SERVICE",
  2567. + "REMOVE_SERVICE",
  2568. + "QUEUE_MESSAGE",
  2569. + "QUEUE_BULK_TRANSMIT",
  2570. + "QUEUE_BULK_RECEIVE",
  2571. + "AWAIT_COMPLETION",
  2572. + "DEQUEUE_MESSAGE",
  2573. + "GET_CLIENT_ID",
  2574. + "GET_CONFIG",
  2575. + "CLOSE_SERVICE",
  2576. + "USE_SERVICE",
  2577. + "RELEASE_SERVICE",
  2578. + "SET_SERVICE_OPTION",
  2579. + "DUMP_PHYS_MEM",
  2580. + "LIB_VERSION",
  2581. + "CLOSE_DELIVERED"
  2582. +};
  2583. +
  2584. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  2585. + (VCHIQ_IOC_MAX + 1));
  2586. +
  2587. +static void
  2588. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  2589. +
  2590. +/****************************************************************************
  2591. +*
  2592. +* add_completion
  2593. +*
  2594. +***************************************************************************/
  2595. +
  2596. +static VCHIQ_STATUS_T
  2597. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  2598. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  2599. + void *bulk_userdata)
  2600. +{
  2601. + VCHIQ_COMPLETION_DATA_T *completion;
  2602. + DEBUG_INITIALISE(g_state.local)
  2603. +
  2604. + while (instance->completion_insert ==
  2605. + (instance->completion_remove + MAX_COMPLETIONS)) {
  2606. + /* Out of space - wait for the client */
  2607. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2608. + vchiq_log_trace(vchiq_arm_log_level,
  2609. + "add_completion - completion queue full");
  2610. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  2611. + if (down_interruptible(&instance->remove_event) != 0) {
  2612. + vchiq_log_info(vchiq_arm_log_level,
  2613. + "service_callback interrupted");
  2614. + return VCHIQ_RETRY;
  2615. + } else if (instance->closing) {
  2616. + vchiq_log_info(vchiq_arm_log_level,
  2617. + "service_callback closing");
  2618. + return VCHIQ_ERROR;
  2619. + }
  2620. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2621. + }
  2622. +
  2623. + completion =
  2624. + &instance->completions[instance->completion_insert &
  2625. + (MAX_COMPLETIONS - 1)];
  2626. +
  2627. + completion->header = header;
  2628. + completion->reason = reason;
  2629. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  2630. + completion->service_userdata = user_service->service;
  2631. + completion->bulk_userdata = bulk_userdata;
  2632. +
  2633. + if (reason == VCHIQ_SERVICE_CLOSED) {
  2634. + /* Take an extra reference, to be held until
  2635. + this CLOSED notification is delivered. */
  2636. + lock_service(user_service->service);
  2637. + if (instance->use_close_delivered)
  2638. + user_service->close_pending = 1;
  2639. + }
  2640. +
  2641. + /* A write barrier is needed here to ensure that the entire completion
  2642. + record is written out before the insert point. */
  2643. + wmb();
  2644. +
  2645. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  2646. + user_service->message_available_pos =
  2647. + instance->completion_insert;
  2648. + instance->completion_insert++;
  2649. +
  2650. + up(&instance->insert_event);
  2651. +
  2652. + return VCHIQ_SUCCESS;
  2653. +}
  2654. +
  2655. +/****************************************************************************
  2656. +*
  2657. +* service_callback
  2658. +*
  2659. +***************************************************************************/
  2660. +
  2661. +static VCHIQ_STATUS_T
  2662. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  2663. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  2664. +{
  2665. + /* How do we ensure the callback goes to the right client?
  2666. + ** The service_user data points to a USER_SERVICE_T record containing
  2667. + ** the original callback and the user state structure, which contains a
  2668. + ** circular buffer for completion records.
  2669. + */
  2670. + USER_SERVICE_T *user_service;
  2671. + VCHIQ_SERVICE_T *service;
  2672. + VCHIQ_INSTANCE_T instance;
  2673. + DEBUG_INITIALISE(g_state.local)
  2674. +
  2675. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2676. +
  2677. + service = handle_to_service(handle);
  2678. + BUG_ON(!service);
  2679. + user_service = (USER_SERVICE_T *)service->base.userdata;
  2680. + instance = user_service->instance;
  2681. +
  2682. + if (!instance || instance->closing)
  2683. + return VCHIQ_SUCCESS;
  2684. +
  2685. + vchiq_log_trace(vchiq_arm_log_level,
  2686. + "service_callback - service %lx(%d,%p), reason %d, header %lx, "
  2687. + "instance %lx, bulk_userdata %lx",
  2688. + (unsigned long)user_service,
  2689. + service->localport, user_service->userdata,
  2690. + reason, (unsigned long)header,
  2691. + (unsigned long)instance, (unsigned long)bulk_userdata);
  2692. +
  2693. + if (header && user_service->is_vchi) {
  2694. + spin_lock(&msg_queue_spinlock);
  2695. + while (user_service->msg_insert ==
  2696. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  2697. + spin_unlock(&msg_queue_spinlock);
  2698. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2699. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  2700. + vchiq_log_trace(vchiq_arm_log_level,
  2701. + "service_callback - msg queue full");
  2702. + /* If there is no MESSAGE_AVAILABLE in the completion
  2703. + ** queue, add one
  2704. + */
  2705. + if ((user_service->message_available_pos -
  2706. + instance->completion_remove) < 0) {
  2707. + VCHIQ_STATUS_T status;
  2708. + vchiq_log_info(vchiq_arm_log_level,
  2709. + "Inserting extra MESSAGE_AVAILABLE");
  2710. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2711. + status = add_completion(instance, reason,
  2712. + NULL, user_service, bulk_userdata);
  2713. + if (status != VCHIQ_SUCCESS) {
  2714. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2715. + return status;
  2716. + }
  2717. + }
  2718. +
  2719. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2720. + if (down_interruptible(&user_service->remove_event)
  2721. + != 0) {
  2722. + vchiq_log_info(vchiq_arm_log_level,
  2723. + "service_callback interrupted");
  2724. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2725. + return VCHIQ_RETRY;
  2726. + } else if (instance->closing) {
  2727. + vchiq_log_info(vchiq_arm_log_level,
  2728. + "service_callback closing");
  2729. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2730. + return VCHIQ_ERROR;
  2731. + }
  2732. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2733. + spin_lock(&msg_queue_spinlock);
  2734. + }
  2735. +
  2736. + user_service->msg_queue[user_service->msg_insert &
  2737. + (MSG_QUEUE_SIZE - 1)] = header;
  2738. + user_service->msg_insert++;
  2739. + spin_unlock(&msg_queue_spinlock);
  2740. +
  2741. + up(&user_service->insert_event);
  2742. +
  2743. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  2744. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  2745. + ** bypass the completion queue.
  2746. + */
  2747. + if (((user_service->message_available_pos -
  2748. + instance->completion_remove) >= 0) ||
  2749. + user_service->dequeue_pending) {
  2750. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2751. + user_service->dequeue_pending = 0;
  2752. + return VCHIQ_SUCCESS;
  2753. + }
  2754. +
  2755. + header = NULL;
  2756. + }
  2757. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2758. +
  2759. + return add_completion(instance, reason, header, user_service,
  2760. + bulk_userdata);
  2761. +}
  2762. +
  2763. +/****************************************************************************
  2764. +*
  2765. +* user_service_free
  2766. +*
  2767. +***************************************************************************/
  2768. +static void
  2769. +user_service_free(void *userdata)
  2770. +{
  2771. + kfree(userdata);
  2772. +}
  2773. +
  2774. +/****************************************************************************
  2775. +*
  2776. +* close_delivered
  2777. +*
  2778. +***************************************************************************/
  2779. +static void close_delivered(USER_SERVICE_T *user_service)
  2780. +{
  2781. + vchiq_log_info(vchiq_arm_log_level,
  2782. + "close_delivered(handle=%x)",
  2783. + user_service->service->handle);
  2784. +
  2785. + if (user_service->close_pending) {
  2786. + /* Allow the underlying service to be culled */
  2787. + unlock_service(user_service->service);
  2788. +
  2789. + /* Wake the user-thread blocked in close_ or remove_service */
  2790. + up(&user_service->close_event);
  2791. +
  2792. + user_service->close_pending = 0;
  2793. + }
  2794. +}
  2795. +
  2796. +/****************************************************************************
  2797. +*
  2798. +* vchiq_ioctl
  2799. +*
  2800. +***************************************************************************/
  2801. +static long
  2802. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2803. +{
  2804. + VCHIQ_INSTANCE_T instance = file->private_data;
  2805. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  2806. + VCHIQ_SERVICE_T *service = NULL;
  2807. + long ret = 0;
  2808. + int i, rc;
  2809. + DEBUG_INITIALISE(g_state.local)
  2810. +
  2811. + vchiq_log_trace(vchiq_arm_log_level,
  2812. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  2813. + (unsigned int)instance,
  2814. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  2815. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  2816. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  2817. +
  2818. + switch (cmd) {
  2819. + case VCHIQ_IOC_SHUTDOWN:
  2820. + if (!instance->connected)
  2821. + break;
  2822. +
  2823. + /* Remove all services */
  2824. + i = 0;
  2825. + while ((service = next_service_by_instance(instance->state,
  2826. + instance, &i)) != NULL) {
  2827. + status = vchiq_remove_service(service->handle);
  2828. + unlock_service(service);
  2829. + if (status != VCHIQ_SUCCESS)
  2830. + break;
  2831. + }
  2832. + service = NULL;
  2833. +
  2834. + if (status == VCHIQ_SUCCESS) {
  2835. + /* Wake the completion thread and ask it to exit */
  2836. + instance->closing = 1;
  2837. + up(&instance->insert_event);
  2838. + }
  2839. +
  2840. + break;
  2841. +
  2842. + case VCHIQ_IOC_CONNECT:
  2843. + if (instance->connected) {
  2844. + ret = -EINVAL;
  2845. + break;
  2846. + }
  2847. + rc = mutex_lock_interruptible(&instance->state->mutex);
  2848. + if (rc != 0) {
  2849. + vchiq_log_error(vchiq_arm_log_level,
  2850. + "vchiq: connect: could not lock mutex for "
  2851. + "state %d: %d",
  2852. + instance->state->id, rc);
  2853. + ret = -EINTR;
  2854. + break;
  2855. + }
  2856. + status = vchiq_connect_internal(instance->state, instance);
  2857. + mutex_unlock(&instance->state->mutex);
  2858. +
  2859. + if (status == VCHIQ_SUCCESS)
  2860. + instance->connected = 1;
  2861. + else
  2862. + vchiq_log_error(vchiq_arm_log_level,
  2863. + "vchiq: could not connect: %d", status);
  2864. + break;
  2865. +
  2866. + case VCHIQ_IOC_CREATE_SERVICE: {
  2867. + VCHIQ_CREATE_SERVICE_T args;
  2868. + USER_SERVICE_T *user_service = NULL;
  2869. + void *userdata;
  2870. + int srvstate;
  2871. +
  2872. + if (copy_from_user
  2873. + (&args, (const void __user *)arg,
  2874. + sizeof(args)) != 0) {
  2875. + ret = -EFAULT;
  2876. + break;
  2877. + }
  2878. +
  2879. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  2880. + if (!user_service) {
  2881. + ret = -ENOMEM;
  2882. + break;
  2883. + }
  2884. +
  2885. + if (args.is_open) {
  2886. + if (!instance->connected) {
  2887. + ret = -ENOTCONN;
  2888. + kfree(user_service);
  2889. + break;
  2890. + }
  2891. + srvstate = VCHIQ_SRVSTATE_OPENING;
  2892. + } else {
  2893. + srvstate =
  2894. + instance->connected ?
  2895. + VCHIQ_SRVSTATE_LISTENING :
  2896. + VCHIQ_SRVSTATE_HIDDEN;
  2897. + }
  2898. +
  2899. + userdata = args.params.userdata;
  2900. + args.params.callback = service_callback;
  2901. + args.params.userdata = user_service;
  2902. + service = vchiq_add_service_internal(
  2903. + instance->state,
  2904. + &args.params, srvstate,
  2905. + instance, user_service_free);
  2906. +
  2907. + if (service != NULL) {
  2908. + user_service->service = service;
  2909. + user_service->userdata = userdata;
  2910. + user_service->instance = instance;
  2911. + user_service->is_vchi = (args.is_vchi != 0);
  2912. + user_service->dequeue_pending = 0;
  2913. + user_service->close_pending = 0;
  2914. + user_service->message_available_pos =
  2915. + instance->completion_remove - 1;
  2916. + user_service->msg_insert = 0;
  2917. + user_service->msg_remove = 0;
  2918. + sema_init(&user_service->insert_event, 0);
  2919. + sema_init(&user_service->remove_event, 0);
  2920. + sema_init(&user_service->close_event, 0);
  2921. +
  2922. + if (args.is_open) {
  2923. + status = vchiq_open_service_internal
  2924. + (service, instance->pid);
  2925. + if (status != VCHIQ_SUCCESS) {
  2926. + vchiq_remove_service(service->handle);
  2927. + service = NULL;
  2928. + ret = (status == VCHIQ_RETRY) ?
  2929. + -EINTR : -EIO;
  2930. + break;
  2931. + }
  2932. + }
  2933. +
  2934. + if (copy_to_user((void __user *)
  2935. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  2936. + arg)->handle),
  2937. + (const void *)&service->handle,
  2938. + sizeof(service->handle)) != 0) {
  2939. + ret = -EFAULT;
  2940. + vchiq_remove_service(service->handle);
  2941. + }
  2942. +
  2943. + service = NULL;
  2944. + } else {
  2945. + ret = -EEXIST;
  2946. + kfree(user_service);
  2947. + }
  2948. + } break;
  2949. +
  2950. + case VCHIQ_IOC_CLOSE_SERVICE: {
  2951. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  2952. +
  2953. + service = find_service_for_instance(instance, handle);
  2954. + if (service != NULL) {
  2955. + USER_SERVICE_T *user_service =
  2956. + (USER_SERVICE_T *)service->base.userdata;
  2957. + /* close_pending is false on first entry, and when the
  2958. + wait in vchiq_close_service has been interrupted. */
  2959. + if (!user_service->close_pending) {
  2960. + status = vchiq_close_service(service->handle);
  2961. + if (status != VCHIQ_SUCCESS)
  2962. + break;
  2963. + }
  2964. +
  2965. + /* close_pending is true once the underlying service
  2966. + has been closed until the client library calls the
  2967. + CLOSE_DELIVERED ioctl, signalling close_event. */
  2968. + if (user_service->close_pending &&
  2969. + down_interruptible(&user_service->close_event))
  2970. + status = VCHIQ_RETRY;
  2971. + }
  2972. + else
  2973. + ret = -EINVAL;
  2974. + } break;
  2975. +
  2976. + case VCHIQ_IOC_REMOVE_SERVICE: {
  2977. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  2978. +
  2979. + service = find_service_for_instance(instance, handle);
  2980. + if (service != NULL) {
  2981. + USER_SERVICE_T *user_service =
  2982. + (USER_SERVICE_T *)service->base.userdata;
  2983. + /* close_pending is false on first entry, and when the
  2984. + wait in vchiq_close_service has been interrupted. */
  2985. + if (!user_service->close_pending) {
  2986. + status = vchiq_remove_service(service->handle);
  2987. + if (status != VCHIQ_SUCCESS)
  2988. + break;
  2989. + }
  2990. +
  2991. + /* close_pending is true once the underlying service
  2992. + has been closed until the client library calls the
  2993. + CLOSE_DELIVERED ioctl, signalling close_event. */
  2994. + if (user_service->close_pending &&
  2995. + down_interruptible(&user_service->close_event))
  2996. + status = VCHIQ_RETRY;
  2997. + }
  2998. + else
  2999. + ret = -EINVAL;
  3000. + } break;
  3001. +
  3002. + case VCHIQ_IOC_USE_SERVICE:
  3003. + case VCHIQ_IOC_RELEASE_SERVICE: {
  3004. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  3005. +
  3006. + service = find_service_for_instance(instance, handle);
  3007. + if (service != NULL) {
  3008. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  3009. + vchiq_use_service_internal(service) :
  3010. + vchiq_release_service_internal(service);
  3011. + if (status != VCHIQ_SUCCESS) {
  3012. + vchiq_log_error(vchiq_susp_log_level,
  3013. + "%s: cmd %s returned error %d for "
  3014. + "service %c%c%c%c:%03d",
  3015. + __func__,
  3016. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  3017. + "VCHIQ_IOC_USE_SERVICE" :
  3018. + "VCHIQ_IOC_RELEASE_SERVICE",
  3019. + status,
  3020. + VCHIQ_FOURCC_AS_4CHARS(
  3021. + service->base.fourcc),
  3022. + service->client_id);
  3023. + ret = -EINVAL;
  3024. + }
  3025. + } else
  3026. + ret = -EINVAL;
  3027. + } break;
  3028. +
  3029. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  3030. + VCHIQ_QUEUE_MESSAGE_T args;
  3031. + if (copy_from_user
  3032. + (&args, (const void __user *)arg,
  3033. + sizeof(args)) != 0) {
  3034. + ret = -EFAULT;
  3035. + break;
  3036. + }
  3037. +
  3038. + service = find_service_for_instance(instance, args.handle);
  3039. +
  3040. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  3041. + /* Copy elements into kernel space */
  3042. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  3043. + if (copy_from_user(elements, args.elements,
  3044. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  3045. + status = vchiq_queue_message
  3046. + (args.handle,
  3047. + elements, args.count);
  3048. + else
  3049. + ret = -EFAULT;
  3050. + } else {
  3051. + ret = -EINVAL;
  3052. + }
  3053. + } break;
  3054. +
  3055. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  3056. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  3057. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  3058. + struct bulk_waiter_node *waiter = NULL;
  3059. + VCHIQ_BULK_DIR_T dir =
  3060. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  3061. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  3062. +
  3063. + if (copy_from_user
  3064. + (&args, (const void __user *)arg,
  3065. + sizeof(args)) != 0) {
  3066. + ret = -EFAULT;
  3067. + break;
  3068. + }
  3069. +
  3070. + service = find_service_for_instance(instance, args.handle);
  3071. + if (!service) {
  3072. + ret = -EINVAL;
  3073. + break;
  3074. + }
  3075. +
  3076. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  3077. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  3078. + GFP_KERNEL);
  3079. + if (!waiter) {
  3080. + ret = -ENOMEM;
  3081. + break;
  3082. + }
  3083. + args.userdata = &waiter->bulk_waiter;
  3084. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  3085. + struct list_head *pos;
  3086. + mutex_lock(&instance->bulk_waiter_list_mutex);
  3087. + list_for_each(pos, &instance->bulk_waiter_list) {
  3088. + if (list_entry(pos, struct bulk_waiter_node,
  3089. + list)->pid == current->pid) {
  3090. + waiter = list_entry(pos,
  3091. + struct bulk_waiter_node,
  3092. + list);
  3093. + list_del(pos);
  3094. + break;
  3095. + }
  3096. +
  3097. + }
  3098. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  3099. + if (!waiter) {
  3100. + vchiq_log_error(vchiq_arm_log_level,
  3101. + "no bulk_waiter found for pid %d",
  3102. + current->pid);
  3103. + ret = -ESRCH;
  3104. + break;
  3105. + }
  3106. + vchiq_log_info(vchiq_arm_log_level,
  3107. + "found bulk_waiter %x for pid %d",
  3108. + (unsigned int)waiter, current->pid);
  3109. + args.userdata = &waiter->bulk_waiter;
  3110. + }
  3111. + status = vchiq_bulk_transfer
  3112. + (args.handle,
  3113. + VCHI_MEM_HANDLE_INVALID,
  3114. + args.data, args.size,
  3115. + args.userdata, args.mode,
  3116. + dir);
  3117. + if (!waiter)
  3118. + break;
  3119. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  3120. + !waiter->bulk_waiter.bulk) {
  3121. + if (waiter->bulk_waiter.bulk) {
  3122. + /* Cancel the signal when the transfer
  3123. + ** completes. */
  3124. + spin_lock(&bulk_waiter_spinlock);
  3125. + waiter->bulk_waiter.bulk->userdata = NULL;
  3126. + spin_unlock(&bulk_waiter_spinlock);
  3127. + }
  3128. + kfree(waiter);
  3129. + } else {
  3130. + const VCHIQ_BULK_MODE_T mode_waiting =
  3131. + VCHIQ_BULK_MODE_WAITING;
  3132. + waiter->pid = current->pid;
  3133. + mutex_lock(&instance->bulk_waiter_list_mutex);
  3134. + list_add(&waiter->list, &instance->bulk_waiter_list);
  3135. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  3136. + vchiq_log_info(vchiq_arm_log_level,
  3137. + "saved bulk_waiter %x for pid %d",
  3138. + (unsigned int)waiter, current->pid);
  3139. +
  3140. + if (copy_to_user((void __user *)
  3141. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  3142. + arg)->mode),
  3143. + (const void *)&mode_waiting,
  3144. + sizeof(mode_waiting)) != 0)
  3145. + ret = -EFAULT;
  3146. + }
  3147. + } break;
  3148. +
  3149. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  3150. + VCHIQ_AWAIT_COMPLETION_T args;
  3151. +
  3152. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3153. + if (!instance->connected) {
  3154. + ret = -ENOTCONN;
  3155. + break;
  3156. + }
  3157. +
  3158. + if (copy_from_user(&args, (const void __user *)arg,
  3159. + sizeof(args)) != 0) {
  3160. + ret = -EFAULT;
  3161. + break;
  3162. + }
  3163. +
  3164. + mutex_lock(&instance->completion_mutex);
  3165. +
  3166. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3167. + while ((instance->completion_remove ==
  3168. + instance->completion_insert)
  3169. + && !instance->closing) {
  3170. + int rc;
  3171. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3172. + mutex_unlock(&instance->completion_mutex);
  3173. + rc = down_interruptible(&instance->insert_event);
  3174. + mutex_lock(&instance->completion_mutex);
  3175. + if (rc != 0) {
  3176. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3177. + vchiq_log_info(vchiq_arm_log_level,
  3178. + "AWAIT_COMPLETION interrupted");
  3179. + ret = -EINTR;
  3180. + break;
  3181. + }
  3182. + }
  3183. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3184. +
  3185. + /* A read memory barrier is needed to stop prefetch of a stale
  3186. + ** completion record
  3187. + */
  3188. + rmb();
  3189. +
  3190. + if (ret == 0) {
  3191. + int msgbufcount = args.msgbufcount;
  3192. + for (ret = 0; ret < args.count; ret++) {
  3193. + VCHIQ_COMPLETION_DATA_T *completion;
  3194. + VCHIQ_SERVICE_T *service;
  3195. + USER_SERVICE_T *user_service;
  3196. + VCHIQ_HEADER_T *header;
  3197. + if (instance->completion_remove ==
  3198. + instance->completion_insert)
  3199. + break;
  3200. + completion = &instance->completions[
  3201. + instance->completion_remove &
  3202. + (MAX_COMPLETIONS - 1)];
  3203. +
  3204. + service = completion->service_userdata;
  3205. + user_service = service->base.userdata;
  3206. + completion->service_userdata =
  3207. + user_service->userdata;
  3208. +
  3209. + header = completion->header;
  3210. + if (header) {
  3211. + void __user *msgbuf;
  3212. + int msglen;
  3213. +
  3214. + msglen = header->size +
  3215. + sizeof(VCHIQ_HEADER_T);
  3216. + /* This must be a VCHIQ-style service */
  3217. + if (args.msgbufsize < msglen) {
  3218. + vchiq_log_error(
  3219. + vchiq_arm_log_level,
  3220. + "header %x: msgbufsize"
  3221. + " %x < msglen %x",
  3222. + (unsigned int)header,
  3223. + args.msgbufsize,
  3224. + msglen);
  3225. + WARN(1, "invalid message "
  3226. + "size\n");
  3227. + if (ret == 0)
  3228. + ret = -EMSGSIZE;
  3229. + break;
  3230. + }
  3231. + if (msgbufcount <= 0)
  3232. + /* Stall here for lack of a
  3233. + ** buffer for the message. */
  3234. + break;
  3235. + /* Get the pointer from user space */
  3236. + msgbufcount--;
  3237. + if (copy_from_user(&msgbuf,
  3238. + (const void __user *)
  3239. + &args.msgbufs[msgbufcount],
  3240. + sizeof(msgbuf)) != 0) {
  3241. + if (ret == 0)
  3242. + ret = -EFAULT;
  3243. + break;
  3244. + }
  3245. +
  3246. + /* Copy the message to user space */
  3247. + if (copy_to_user(msgbuf, header,
  3248. + msglen) != 0) {
  3249. + if (ret == 0)
  3250. + ret = -EFAULT;
  3251. + break;
  3252. + }
  3253. +
  3254. + /* Now it has been copied, the message
  3255. + ** can be released. */
  3256. + vchiq_release_message(service->handle,
  3257. + header);
  3258. +
  3259. + /* The completion must point to the
  3260. + ** msgbuf. */
  3261. + completion->header = msgbuf;
  3262. + }
  3263. +
  3264. + if ((completion->reason ==
  3265. + VCHIQ_SERVICE_CLOSED) &&
  3266. + !instance->use_close_delivered)
  3267. + unlock_service(service);
  3268. +
  3269. + if (copy_to_user((void __user *)(
  3270. + (size_t)args.buf +
  3271. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  3272. + completion,
  3273. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  3274. + if (ret == 0)
  3275. + ret = -EFAULT;
  3276. + break;
  3277. + }
  3278. +
  3279. + instance->completion_remove++;
  3280. + }
  3281. +
  3282. + if (msgbufcount != args.msgbufcount) {
  3283. + if (copy_to_user((void __user *)
  3284. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  3285. + msgbufcount,
  3286. + &msgbufcount,
  3287. + sizeof(msgbufcount)) != 0) {
  3288. + ret = -EFAULT;
  3289. + }
  3290. + }
  3291. + }
  3292. +
  3293. + if (ret != 0)
  3294. + up(&instance->remove_event);
  3295. + mutex_unlock(&instance->completion_mutex);
  3296. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3297. + } break;
  3298. +
  3299. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  3300. + VCHIQ_DEQUEUE_MESSAGE_T args;
  3301. + USER_SERVICE_T *user_service;
  3302. + VCHIQ_HEADER_T *header;
  3303. +
  3304. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3305. + if (copy_from_user
  3306. + (&args, (const void __user *)arg,
  3307. + sizeof(args)) != 0) {
  3308. + ret = -EFAULT;
  3309. + break;
  3310. + }
  3311. + service = find_service_for_instance(instance, args.handle);
  3312. + if (!service) {
  3313. + ret = -EINVAL;
  3314. + break;
  3315. + }
  3316. + user_service = (USER_SERVICE_T *)service->base.userdata;
  3317. + if (user_service->is_vchi == 0) {
  3318. + ret = -EINVAL;
  3319. + break;
  3320. + }
  3321. +
  3322. + spin_lock(&msg_queue_spinlock);
  3323. + if (user_service->msg_remove == user_service->msg_insert) {
  3324. + if (!args.blocking) {
  3325. + spin_unlock(&msg_queue_spinlock);
  3326. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3327. + ret = -EWOULDBLOCK;
  3328. + break;
  3329. + }
  3330. + user_service->dequeue_pending = 1;
  3331. + do {
  3332. + spin_unlock(&msg_queue_spinlock);
  3333. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3334. + if (down_interruptible(
  3335. + &user_service->insert_event) != 0) {
  3336. + vchiq_log_info(vchiq_arm_log_level,
  3337. + "DEQUEUE_MESSAGE interrupted");
  3338. + ret = -EINTR;
  3339. + break;
  3340. + }
  3341. + spin_lock(&msg_queue_spinlock);
  3342. + } while (user_service->msg_remove ==
  3343. + user_service->msg_insert);
  3344. +
  3345. + if (ret)
  3346. + break;
  3347. + }
  3348. +
  3349. + BUG_ON((int)(user_service->msg_insert -
  3350. + user_service->msg_remove) < 0);
  3351. +
  3352. + header = user_service->msg_queue[user_service->msg_remove &
  3353. + (MSG_QUEUE_SIZE - 1)];
  3354. + user_service->msg_remove++;
  3355. + spin_unlock(&msg_queue_spinlock);
  3356. +
  3357. + up(&user_service->remove_event);
  3358. + if (header == NULL)
  3359. + ret = -ENOTCONN;
  3360. + else if (header->size <= args.bufsize) {
  3361. + /* Copy to user space if msgbuf is not NULL */
  3362. + if ((args.buf == NULL) ||
  3363. + (copy_to_user((void __user *)args.buf,
  3364. + header->data,
  3365. + header->size) == 0)) {
  3366. + ret = header->size;
  3367. + vchiq_release_message(
  3368. + service->handle,
  3369. + header);
  3370. + } else
  3371. + ret = -EFAULT;
  3372. + } else {
  3373. + vchiq_log_error(vchiq_arm_log_level,
  3374. + "header %x: bufsize %x < size %x",
  3375. + (unsigned int)header, args.bufsize,
  3376. + header->size);
  3377. + WARN(1, "invalid size\n");
  3378. + ret = -EMSGSIZE;
  3379. + }
  3380. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3381. + } break;
  3382. +
  3383. + case VCHIQ_IOC_GET_CLIENT_ID: {
  3384. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  3385. +
  3386. + ret = vchiq_get_client_id(handle);
  3387. + } break;
  3388. +
  3389. + case VCHIQ_IOC_GET_CONFIG: {
  3390. + VCHIQ_GET_CONFIG_T args;
  3391. + VCHIQ_CONFIG_T config;
  3392. +
  3393. + if (copy_from_user(&args, (const void __user *)arg,
  3394. + sizeof(args)) != 0) {
  3395. + ret = -EFAULT;
  3396. + break;
  3397. + }
  3398. + if (args.config_size > sizeof(config)) {
  3399. + ret = -EINVAL;
  3400. + break;
  3401. + }
  3402. + status = vchiq_get_config(instance, args.config_size, &config);
  3403. + if (status == VCHIQ_SUCCESS) {
  3404. + if (copy_to_user((void __user *)args.pconfig,
  3405. + &config, args.config_size) != 0) {
  3406. + ret = -EFAULT;
  3407. + break;
  3408. + }
  3409. + }
  3410. + } break;
  3411. +
  3412. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  3413. + VCHIQ_SET_SERVICE_OPTION_T args;
  3414. +
  3415. + if (copy_from_user(
  3416. + &args, (const void __user *)arg,
  3417. + sizeof(args)) != 0) {
  3418. + ret = -EFAULT;
  3419. + break;
  3420. + }
  3421. +
  3422. + service = find_service_for_instance(instance, args.handle);
  3423. + if (!service) {
  3424. + ret = -EINVAL;
  3425. + break;
  3426. + }
  3427. +
  3428. + status = vchiq_set_service_option(
  3429. + args.handle, args.option, args.value);
  3430. + } break;
  3431. +
  3432. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  3433. + VCHIQ_DUMP_MEM_T args;
  3434. +
  3435. + if (copy_from_user
  3436. + (&args, (const void __user *)arg,
  3437. + sizeof(args)) != 0) {
  3438. + ret = -EFAULT;
  3439. + break;
  3440. + }
  3441. + dump_phys_mem(args.virt_addr, args.num_bytes);
  3442. + } break;
  3443. +
  3444. + case VCHIQ_IOC_LIB_VERSION: {
  3445. + unsigned int lib_version = (unsigned int)arg;
  3446. +
  3447. + if (lib_version < VCHIQ_VERSION_MIN)
  3448. + ret = -EINVAL;
  3449. + else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
  3450. + instance->use_close_delivered = 1;
  3451. + } break;
  3452. +
  3453. + case VCHIQ_IOC_CLOSE_DELIVERED: {
  3454. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  3455. +
  3456. + service = find_closed_service_for_instance(instance, handle);
  3457. + if (service != NULL) {
  3458. + USER_SERVICE_T *user_service =
  3459. + (USER_SERVICE_T *)service->base.userdata;
  3460. + close_delivered(user_service);
  3461. + }
  3462. + else
  3463. + ret = -EINVAL;
  3464. + } break;
  3465. +
  3466. + default:
  3467. + ret = -ENOTTY;
  3468. + break;
  3469. + }
  3470. +
  3471. + if (service)
  3472. + unlock_service(service);
  3473. +
  3474. + if (ret == 0) {
  3475. + if (status == VCHIQ_ERROR)
  3476. + ret = -EIO;
  3477. + else if (status == VCHIQ_RETRY)
  3478. + ret = -EINTR;
  3479. + }
  3480. +
  3481. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  3482. + (ret != -EWOULDBLOCK))
  3483. + vchiq_log_info(vchiq_arm_log_level,
  3484. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  3485. + (unsigned long)instance,
  3486. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  3487. + ioctl_names[_IOC_NR(cmd)] :
  3488. + "<invalid>",
  3489. + status, ret);
  3490. + else
  3491. + vchiq_log_trace(vchiq_arm_log_level,
  3492. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  3493. + (unsigned long)instance,
  3494. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  3495. + ioctl_names[_IOC_NR(cmd)] :
  3496. + "<invalid>",
  3497. + status, ret);
  3498. +
  3499. + return ret;
  3500. +}
  3501. +
  3502. +/****************************************************************************
  3503. +*
  3504. +* vchiq_open
  3505. +*
  3506. +***************************************************************************/
  3507. +
  3508. +static int
  3509. +vchiq_open(struct inode *inode, struct file *file)
  3510. +{
  3511. + int dev = iminor(inode) & 0x0f;
  3512. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  3513. + switch (dev) {
  3514. + case VCHIQ_MINOR: {
  3515. + int ret;
  3516. + VCHIQ_STATE_T *state = vchiq_get_state();
  3517. + VCHIQ_INSTANCE_T instance;
  3518. +
  3519. + if (!state) {
  3520. + vchiq_log_error(vchiq_arm_log_level,
  3521. + "vchiq has no connection to VideoCore");
  3522. + return -ENOTCONN;
  3523. + }
  3524. +
  3525. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  3526. + if (!instance)
  3527. + return -ENOMEM;
  3528. +
  3529. + instance->state = state;
  3530. + instance->pid = current->tgid;
  3531. +
  3532. + ret = vchiq_debugfs_add_instance(instance);
  3533. + if (ret != 0) {
  3534. + kfree(instance);
  3535. + return ret;
  3536. + }
  3537. +
  3538. + sema_init(&instance->insert_event, 0);
  3539. + sema_init(&instance->remove_event, 0);
  3540. + mutex_init(&instance->completion_mutex);
  3541. + mutex_init(&instance->bulk_waiter_list_mutex);
  3542. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  3543. +
  3544. + file->private_data = instance;
  3545. + } break;
  3546. +
  3547. + default:
  3548. + vchiq_log_error(vchiq_arm_log_level,
  3549. + "Unknown minor device: %d", dev);
  3550. + return -ENXIO;
  3551. + }
  3552. +
  3553. + return 0;
  3554. +}
  3555. +
  3556. +/****************************************************************************
  3557. +*
  3558. +* vchiq_release
  3559. +*
  3560. +***************************************************************************/
  3561. +
  3562. +static int
  3563. +vchiq_release(struct inode *inode, struct file *file)
  3564. +{
  3565. + int dev = iminor(inode) & 0x0f;
  3566. + int ret = 0;
  3567. + switch (dev) {
  3568. + case VCHIQ_MINOR: {
  3569. + VCHIQ_INSTANCE_T instance = file->private_data;
  3570. + VCHIQ_STATE_T *state = vchiq_get_state();
  3571. + VCHIQ_SERVICE_T *service;
  3572. + int i;
  3573. +
  3574. + vchiq_log_info(vchiq_arm_log_level,
  3575. + "vchiq_release: instance=%lx",
  3576. + (unsigned long)instance);
  3577. +
  3578. + if (!state) {
  3579. + ret = -EPERM;
  3580. + goto out;
  3581. + }
  3582. +
  3583. + /* Ensure videocore is awake to allow termination. */
  3584. + vchiq_use_internal(instance->state, NULL,
  3585. + USE_TYPE_VCHIQ);
  3586. +
  3587. + mutex_lock(&instance->completion_mutex);
  3588. +
  3589. + /* Wake the completion thread and ask it to exit */
  3590. + instance->closing = 1;
  3591. + up(&instance->insert_event);
  3592. +
  3593. + mutex_unlock(&instance->completion_mutex);
  3594. +
  3595. + /* Wake the slot handler if the completion queue is full. */
  3596. + up(&instance->remove_event);
  3597. +
  3598. + /* Mark all services for termination... */
  3599. + i = 0;
  3600. + while ((service = next_service_by_instance(state, instance,
  3601. + &i)) != NULL) {
  3602. + USER_SERVICE_T *user_service = service->base.userdata;
  3603. +
  3604. + /* Wake the slot handler if the msg queue is full. */
  3605. + up(&user_service->remove_event);
  3606. +
  3607. + vchiq_terminate_service_internal(service);
  3608. + unlock_service(service);
  3609. + }
  3610. +
  3611. + /* ...and wait for them to die */
  3612. + i = 0;
  3613. + while ((service = next_service_by_instance(state, instance, &i))
  3614. + != NULL) {
  3615. + USER_SERVICE_T *user_service = service->base.userdata;
  3616. +
  3617. + down(&service->remove_event);
  3618. +
  3619. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  3620. +
  3621. + spin_lock(&msg_queue_spinlock);
  3622. +
  3623. + while (user_service->msg_remove !=
  3624. + user_service->msg_insert) {
  3625. + VCHIQ_HEADER_T *header = user_service->
  3626. + msg_queue[user_service->msg_remove &
  3627. + (MSG_QUEUE_SIZE - 1)];
  3628. + user_service->msg_remove++;
  3629. + spin_unlock(&msg_queue_spinlock);
  3630. +
  3631. + if (header)
  3632. + vchiq_release_message(
  3633. + service->handle,
  3634. + header);
  3635. + spin_lock(&msg_queue_spinlock);
  3636. + }
  3637. +
  3638. + spin_unlock(&msg_queue_spinlock);
  3639. +
  3640. + unlock_service(service);
  3641. + }
  3642. +
  3643. + /* Release any closed services */
  3644. + while (instance->completion_remove !=
  3645. + instance->completion_insert) {
  3646. + VCHIQ_COMPLETION_DATA_T *completion;
  3647. + VCHIQ_SERVICE_T *service;
  3648. + completion = &instance->completions[
  3649. + instance->completion_remove &
  3650. + (MAX_COMPLETIONS - 1)];
  3651. + service = completion->service_userdata;
  3652. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  3653. + {
  3654. + USER_SERVICE_T *user_service =
  3655. + service->base.userdata;
  3656. +
  3657. + /* Wake any blocked user-thread */
  3658. + if (instance->use_close_delivered)
  3659. + up(&user_service->close_event);
  3660. + unlock_service(service);
  3661. + }
  3662. + instance->completion_remove++;
  3663. + }
  3664. +
  3665. + /* Release the PEER service count. */
  3666. + vchiq_release_internal(instance->state, NULL);
  3667. +
  3668. + {
  3669. + struct list_head *pos, *next;
  3670. + list_for_each_safe(pos, next,
  3671. + &instance->bulk_waiter_list) {
  3672. + struct bulk_waiter_node *waiter;
  3673. + waiter = list_entry(pos,
  3674. + struct bulk_waiter_node,
  3675. + list);
  3676. + list_del(pos);
  3677. + vchiq_log_info(vchiq_arm_log_level,
  3678. + "bulk_waiter - cleaned up %x "
  3679. + "for pid %d",
  3680. + (unsigned int)waiter, waiter->pid);
  3681. + kfree(waiter);
  3682. + }
  3683. + }
  3684. +
  3685. + vchiq_debugfs_remove_instance(instance);
  3686. +
  3687. + kfree(instance);
  3688. + file->private_data = NULL;
  3689. + } break;
  3690. +
  3691. + default:
  3692. + vchiq_log_error(vchiq_arm_log_level,
  3693. + "Unknown minor device: %d", dev);
  3694. + ret = -ENXIO;
  3695. + }
  3696. +
  3697. +out:
  3698. + return ret;
  3699. +}
  3700. +
  3701. +/****************************************************************************
  3702. +*
  3703. +* vchiq_dump
  3704. +*
  3705. +***************************************************************************/
  3706. +
  3707. +void
  3708. +vchiq_dump(void *dump_context, const char *str, int len)
  3709. +{
  3710. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  3711. +
  3712. + if (context->actual < context->space) {
  3713. + int copy_bytes;
  3714. + if (context->offset > 0) {
  3715. + int skip_bytes = min(len, (int)context->offset);
  3716. + str += skip_bytes;
  3717. + len -= skip_bytes;
  3718. + context->offset -= skip_bytes;
  3719. + if (context->offset > 0)
  3720. + return;
  3721. + }
  3722. + copy_bytes = min(len, (int)(context->space - context->actual));
  3723. + if (copy_bytes == 0)
  3724. + return;
  3725. + if (copy_to_user(context->buf + context->actual, str,
  3726. + copy_bytes))
  3727. + context->actual = -EFAULT;
  3728. + context->actual += copy_bytes;
  3729. + len -= copy_bytes;
  3730. +
  3731. + /* If tne terminating NUL is included in the length, then it
  3732. + ** marks the end of a line and should be replaced with a
  3733. + ** carriage return. */
  3734. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  3735. + char cr = '\n';
  3736. + if (copy_to_user(context->buf + context->actual - 1,
  3737. + &cr, 1))
  3738. + context->actual = -EFAULT;
  3739. + }
  3740. + }
  3741. +}
  3742. +
  3743. +/****************************************************************************
  3744. +*
  3745. +* vchiq_dump_platform_instance_state
  3746. +*
  3747. +***************************************************************************/
  3748. +
  3749. +void
  3750. +vchiq_dump_platform_instances(void *dump_context)
  3751. +{
  3752. + VCHIQ_STATE_T *state = vchiq_get_state();
  3753. + char buf[80];
  3754. + int len;
  3755. + int i;
  3756. +
  3757. + /* There is no list of instances, so instead scan all services,
  3758. + marking those that have been dumped. */
  3759. +
  3760. + for (i = 0; i < state->unused_service; i++) {
  3761. + VCHIQ_SERVICE_T *service = state->services[i];
  3762. + VCHIQ_INSTANCE_T instance;
  3763. +
  3764. + if (service && (service->base.callback == service_callback)) {
  3765. + instance = service->instance;
  3766. + if (instance)
  3767. + instance->mark = 0;
  3768. + }
  3769. + }
  3770. +
  3771. + for (i = 0; i < state->unused_service; i++) {
  3772. + VCHIQ_SERVICE_T *service = state->services[i];
  3773. + VCHIQ_INSTANCE_T instance;
  3774. +
  3775. + if (service && (service->base.callback == service_callback)) {
  3776. + instance = service->instance;
  3777. + if (instance && !instance->mark) {
  3778. + len = snprintf(buf, sizeof(buf),
  3779. + "Instance %x: pid %d,%s completions "
  3780. + "%d/%d",
  3781. + (unsigned int)instance, instance->pid,
  3782. + instance->connected ? " connected, " :
  3783. + "",
  3784. + instance->completion_insert -
  3785. + instance->completion_remove,
  3786. + MAX_COMPLETIONS);
  3787. +
  3788. + vchiq_dump(dump_context, buf, len + 1);
  3789. +
  3790. + instance->mark = 1;
  3791. + }
  3792. + }
  3793. + }
  3794. +}
  3795. +
  3796. +/****************************************************************************
  3797. +*
  3798. +* vchiq_dump_platform_service_state
  3799. +*
  3800. +***************************************************************************/
  3801. +
  3802. +void
  3803. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  3804. +{
  3805. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  3806. + char buf[80];
  3807. + int len;
  3808. +
  3809. + len = snprintf(buf, sizeof(buf), " instance %x",
  3810. + (unsigned int)service->instance);
  3811. +
  3812. + if ((service->base.callback == service_callback) &&
  3813. + user_service->is_vchi) {
  3814. + len += snprintf(buf + len, sizeof(buf) - len,
  3815. + ", %d/%d messages",
  3816. + user_service->msg_insert - user_service->msg_remove,
  3817. + MSG_QUEUE_SIZE);
  3818. +
  3819. + if (user_service->dequeue_pending)
  3820. + len += snprintf(buf + len, sizeof(buf) - len,
  3821. + " (dequeue pending)");
  3822. + }
  3823. +
  3824. + vchiq_dump(dump_context, buf, len + 1);
  3825. +}
  3826. +
  3827. +/****************************************************************************
  3828. +*
  3829. +* dump_user_mem
  3830. +*
  3831. +***************************************************************************/
  3832. +
  3833. +static void
  3834. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  3835. +{
  3836. + int rc;
  3837. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  3838. + int num_pages;
  3839. + int offset;
  3840. + int end_offset;
  3841. + int page_idx;
  3842. + int prev_idx;
  3843. + struct page *page;
  3844. + struct page **pages;
  3845. + uint8_t *kmapped_virt_ptr;
  3846. +
  3847. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  3848. +
  3849. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  3850. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  3851. + ~0x0fuL);
  3852. +
  3853. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  3854. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  3855. +
  3856. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  3857. +
  3858. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  3859. + if (pages == NULL) {
  3860. + vchiq_log_error(vchiq_arm_log_level,
  3861. + "Unable to allocation memory for %d pages\n",
  3862. + num_pages);
  3863. + return;
  3864. + }
  3865. +
  3866. + down_read(&current->mm->mmap_sem);
  3867. + rc = get_user_pages(current, /* task */
  3868. + current->mm, /* mm */
  3869. + (unsigned long)virt_addr, /* start */
  3870. + num_pages, /* len */
  3871. + 0, /* write */
  3872. + 0, /* force */
  3873. + pages, /* pages (array of page pointers) */
  3874. + NULL); /* vmas */
  3875. + up_read(&current->mm->mmap_sem);
  3876. +
  3877. + prev_idx = -1;
  3878. + page = NULL;
  3879. +
  3880. + while (offset < end_offset) {
  3881. +
  3882. + int page_offset = offset % PAGE_SIZE;
  3883. + page_idx = offset / PAGE_SIZE;
  3884. +
  3885. + if (page_idx != prev_idx) {
  3886. +
  3887. + if (page != NULL)
  3888. + kunmap(page);
  3889. + page = pages[page_idx];
  3890. + kmapped_virt_ptr = kmap(page);
  3891. +
  3892. + prev_idx = page_idx;
  3893. + }
  3894. +
  3895. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  3896. + vchiq_log_dump_mem("ph",
  3897. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  3898. + page_offset],
  3899. + &kmapped_virt_ptr[page_offset], 16);
  3900. +
  3901. + offset += 16;
  3902. + }
  3903. + if (page != NULL)
  3904. + kunmap(page);
  3905. +
  3906. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  3907. + page_cache_release(pages[page_idx]);
  3908. +
  3909. + kfree(pages);
  3910. +}
  3911. +
  3912. +/****************************************************************************
  3913. +*
  3914. +* vchiq_read
  3915. +*
  3916. +***************************************************************************/
  3917. +
  3918. +static ssize_t
  3919. +vchiq_read(struct file *file, char __user *buf,
  3920. + size_t count, loff_t *ppos)
  3921. +{
  3922. + DUMP_CONTEXT_T context;
  3923. + context.buf = buf;
  3924. + context.actual = 0;
  3925. + context.space = count;
  3926. + context.offset = *ppos;
  3927. +
  3928. + vchiq_dump_state(&context, &g_state);
  3929. +
  3930. + *ppos += context.actual;
  3931. +
  3932. + return context.actual;
  3933. +}
  3934. +
  3935. +VCHIQ_STATE_T *
  3936. +vchiq_get_state(void)
  3937. +{
  3938. +
  3939. + if (g_state.remote == NULL)
  3940. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  3941. + else if (g_state.remote->initialised != 1)
  3942. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  3943. + __func__, g_state.remote->initialised);
  3944. +
  3945. + return ((g_state.remote != NULL) &&
  3946. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  3947. +}
  3948. +
  3949. +static const struct file_operations
  3950. +vchiq_fops = {
  3951. + .owner = THIS_MODULE,
  3952. + .unlocked_ioctl = vchiq_ioctl,
  3953. + .open = vchiq_open,
  3954. + .release = vchiq_release,
  3955. + .read = vchiq_read
  3956. +};
  3957. +
  3958. +/*
  3959. + * Autosuspend related functionality
  3960. + */
  3961. +
  3962. +int
  3963. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  3964. +{
  3965. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  3966. + if (!arm_state)
  3967. + /* autosuspend not supported - always return wanted */
  3968. + return 1;
  3969. + else if (arm_state->blocked_count)
  3970. + return 1;
  3971. + else if (!arm_state->videocore_use_count)
  3972. + /* usage count zero - check for override unless we're forcing */
  3973. + if (arm_state->resume_blocked)
  3974. + return 0;
  3975. + else
  3976. + return vchiq_platform_videocore_wanted(state);
  3977. + else
  3978. + /* non-zero usage count - videocore still required */
  3979. + return 1;
  3980. +}
  3981. +
  3982. +static VCHIQ_STATUS_T
  3983. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  3984. + VCHIQ_HEADER_T *header,
  3985. + VCHIQ_SERVICE_HANDLE_T service_user,
  3986. + void *bulk_user)
  3987. +{
  3988. + vchiq_log_error(vchiq_susp_log_level,
  3989. + "%s callback reason %d", __func__, reason);
  3990. + return 0;
  3991. +}
  3992. +
  3993. +static int
  3994. +vchiq_keepalive_thread_func(void *v)
  3995. +{
  3996. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  3997. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  3998. +
  3999. + VCHIQ_STATUS_T status;
  4000. + VCHIQ_INSTANCE_T instance;
  4001. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  4002. +
  4003. + VCHIQ_SERVICE_PARAMS_T params = {
  4004. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  4005. + .callback = vchiq_keepalive_vchiq_callback,
  4006. + .version = KEEPALIVE_VER,
  4007. + .version_min = KEEPALIVE_VER_MIN
  4008. + };
  4009. +
  4010. + status = vchiq_initialise(&instance);
  4011. + if (status != VCHIQ_SUCCESS) {
  4012. + vchiq_log_error(vchiq_susp_log_level,
  4013. + "%s vchiq_initialise failed %d", __func__, status);
  4014. + goto exit;
  4015. + }
  4016. +
  4017. + status = vchiq_connect(instance);
  4018. + if (status != VCHIQ_SUCCESS) {
  4019. + vchiq_log_error(vchiq_susp_log_level,
  4020. + "%s vchiq_connect failed %d", __func__, status);
  4021. + goto shutdown;
  4022. + }
  4023. +
  4024. + status = vchiq_add_service(instance, &params, &ka_handle);
  4025. + if (status != VCHIQ_SUCCESS) {
  4026. + vchiq_log_error(vchiq_susp_log_level,
  4027. + "%s vchiq_open_service failed %d", __func__, status);
  4028. + goto shutdown;
  4029. + }
  4030. +
  4031. + while (1) {
  4032. + long rc = 0, uc = 0;
  4033. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  4034. + != 0) {
  4035. + vchiq_log_error(vchiq_susp_log_level,
  4036. + "%s interrupted", __func__);
  4037. + flush_signals(current);
  4038. + continue;
  4039. + }
  4040. +
  4041. + /* read and clear counters. Do release_count then use_count to
  4042. + * prevent getting more releases than uses */
  4043. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  4044. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  4045. +
  4046. + /* Call use/release service the requisite number of times.
  4047. + * Process use before release so use counts don't go negative */
  4048. + while (uc--) {
  4049. + atomic_inc(&arm_state->ka_use_ack_count);
  4050. + status = vchiq_use_service(ka_handle);
  4051. + if (status != VCHIQ_SUCCESS) {
  4052. + vchiq_log_error(vchiq_susp_log_level,
  4053. + "%s vchiq_use_service error %d",
  4054. + __func__, status);
  4055. + }
  4056. + }
  4057. + while (rc--) {
  4058. + status = vchiq_release_service(ka_handle);
  4059. + if (status != VCHIQ_SUCCESS) {
  4060. + vchiq_log_error(vchiq_susp_log_level,
  4061. + "%s vchiq_release_service error %d",
  4062. + __func__, status);
  4063. + }
  4064. + }
  4065. + }
  4066. +
  4067. +shutdown:
  4068. + vchiq_shutdown(instance);
  4069. +exit:
  4070. + return 0;
  4071. +}
  4072. +
  4073. +
  4074. +
  4075. +VCHIQ_STATUS_T
  4076. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  4077. +{
  4078. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  4079. +
  4080. + if (arm_state) {
  4081. + rwlock_init(&arm_state->susp_res_lock);
  4082. +
  4083. + init_completion(&arm_state->ka_evt);
  4084. + atomic_set(&arm_state->ka_use_count, 0);
  4085. + atomic_set(&arm_state->ka_use_ack_count, 0);
  4086. + atomic_set(&arm_state->ka_release_count, 0);
  4087. +
  4088. + init_completion(&arm_state->vc_suspend_complete);
  4089. +
  4090. + init_completion(&arm_state->vc_resume_complete);
  4091. + /* Initialise to 'done' state. We only want to block on resume
  4092. + * completion while videocore is suspended. */
  4093. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  4094. +
  4095. + init_completion(&arm_state->resume_blocker);
  4096. + /* Initialise to 'done' state. We only want to block on this
  4097. + * completion while resume is blocked */
  4098. + complete_all(&arm_state->resume_blocker);
  4099. +
  4100. + init_completion(&arm_state->blocked_blocker);
  4101. + /* Initialise to 'done' state. We only want to block on this
  4102. + * completion while things are waiting on the resume blocker */
  4103. + complete_all(&arm_state->blocked_blocker);
  4104. +
  4105. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  4106. + arm_state->suspend_timer_running = 0;
  4107. + init_timer(&arm_state->suspend_timer);
  4108. + arm_state->suspend_timer.data = (unsigned long)(state);
  4109. + arm_state->suspend_timer.function = suspend_timer_callback;
  4110. +
  4111. + arm_state->first_connect = 0;
  4112. +
  4113. + }
  4114. + return status;
  4115. +}
  4116. +
  4117. +/*
  4118. +** Functions to modify the state variables;
  4119. +** set_suspend_state
  4120. +** set_resume_state
  4121. +**
  4122. +** There are more state variables than we might like, so ensure they remain in
  4123. +** step. Suspend and resume state are maintained separately, since most of
  4124. +** these state machines can operate independently. However, there are a few
  4125. +** states where state transitions in one state machine cause a reset to the
  4126. +** other state machine. In addition, there are some completion events which
  4127. +** need to occur on state machine reset and end-state(s), so these are also
  4128. +** dealt with in these functions.
  4129. +**
  4130. +** In all states we set the state variable according to the input, but in some
  4131. +** cases we perform additional steps outlined below;
  4132. +**
  4133. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  4134. +** The suspend completion is completed after any suspend
  4135. +** attempt. When we reset the state machine we also reset
  4136. +** the completion. This reset occurs when videocore is
  4137. +** resumed, and also if we initiate suspend after a suspend
  4138. +** failure.
  4139. +**
  4140. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  4141. +** suspend - ie from this point on we must try to suspend
  4142. +** before resuming can occur. We therefore also reset the
  4143. +** resume state machine to VC_RESUME_IDLE in this state.
  4144. +**
  4145. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  4146. +** complete_all on the suspend completion to notify
  4147. +** anything waiting for suspend to happen.
  4148. +**
  4149. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  4150. +** initiate resume, so no need to alter resume state.
  4151. +** We call complete_all on the suspend completion to notify
  4152. +** of suspend rejection.
  4153. +**
  4154. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  4155. +** suspend completion and reset the resume state machine.
  4156. +**
  4157. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  4158. +** resume completion is in it's 'done' state whenever
  4159. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  4160. +** implies that videocore is suspended.
  4161. +** Hence, any thread which needs to wait until videocore is
  4162. +** running can wait on this completion - it will only block
  4163. +** if videocore is suspended.
  4164. +**
  4165. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  4166. +** Call complete_all on the resume completion to unblock
  4167. +** any threads waiting for resume. Also reset the suspend
  4168. +** state machine to it's idle state.
  4169. +**
  4170. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  4171. +*/
  4172. +
  4173. +void
  4174. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  4175. + enum vc_suspend_status new_state)
  4176. +{
  4177. + /* set the state in all cases */
  4178. + arm_state->vc_suspend_state = new_state;
  4179. +
  4180. + /* state specific additional actions */
  4181. + switch (new_state) {
  4182. + case VC_SUSPEND_FORCE_CANCELED:
  4183. + complete_all(&arm_state->vc_suspend_complete);
  4184. + break;
  4185. + case VC_SUSPEND_REJECTED:
  4186. + complete_all(&arm_state->vc_suspend_complete);
  4187. + break;
  4188. + case VC_SUSPEND_FAILED:
  4189. + complete_all(&arm_state->vc_suspend_complete);
  4190. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  4191. + complete_all(&arm_state->vc_resume_complete);
  4192. + break;
  4193. + case VC_SUSPEND_IDLE:
  4194. + reinit_completion(&arm_state->vc_suspend_complete);
  4195. + break;
  4196. + case VC_SUSPEND_REQUESTED:
  4197. + break;
  4198. + case VC_SUSPEND_IN_PROGRESS:
  4199. + set_resume_state(arm_state, VC_RESUME_IDLE);
  4200. + break;
  4201. + case VC_SUSPEND_SUSPENDED:
  4202. + complete_all(&arm_state->vc_suspend_complete);
  4203. + break;
  4204. + default:
  4205. + BUG();
  4206. + break;
  4207. + }
  4208. +}
  4209. +
  4210. +void
  4211. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  4212. + enum vc_resume_status new_state)
  4213. +{
  4214. + /* set the state in all cases */
  4215. + arm_state->vc_resume_state = new_state;
  4216. +
  4217. + /* state specific additional actions */
  4218. + switch (new_state) {
  4219. + case VC_RESUME_FAILED:
  4220. + break;
  4221. + case VC_RESUME_IDLE:
  4222. + reinit_completion(&arm_state->vc_resume_complete);
  4223. + break;
  4224. + case VC_RESUME_REQUESTED:
  4225. + break;
  4226. + case VC_RESUME_IN_PROGRESS:
  4227. + break;
  4228. + case VC_RESUME_RESUMED:
  4229. + complete_all(&arm_state->vc_resume_complete);
  4230. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4231. + break;
  4232. + default:
  4233. + BUG();
  4234. + break;
  4235. + }
  4236. +}
  4237. +
  4238. +
  4239. +/* should be called with the write lock held */
  4240. +inline void
  4241. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  4242. +{
  4243. + del_timer(&arm_state->suspend_timer);
  4244. + arm_state->suspend_timer.expires = jiffies +
  4245. + msecs_to_jiffies(arm_state->
  4246. + suspend_timer_timeout);
  4247. + add_timer(&arm_state->suspend_timer);
  4248. + arm_state->suspend_timer_running = 1;
  4249. +}
  4250. +
  4251. +/* should be called with the write lock held */
  4252. +static inline void
  4253. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  4254. +{
  4255. + if (arm_state->suspend_timer_running) {
  4256. + del_timer(&arm_state->suspend_timer);
  4257. + arm_state->suspend_timer_running = 0;
  4258. + }
  4259. +}
  4260. +
  4261. +static inline int
  4262. +need_resume(VCHIQ_STATE_T *state)
  4263. +{
  4264. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4265. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  4266. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  4267. + vchiq_videocore_wanted(state);
  4268. +}
  4269. +
  4270. +static int
  4271. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  4272. +{
  4273. + int status = VCHIQ_SUCCESS;
  4274. + const unsigned long timeout_val =
  4275. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  4276. + int resume_count = 0;
  4277. +
  4278. + /* Allow any threads which were blocked by the last force suspend to
  4279. + * complete if they haven't already. Only give this one shot; if
  4280. + * blocked_count is incremented after blocked_blocker is completed
  4281. + * (which only happens when blocked_count hits 0) then those threads
  4282. + * will have to wait until next time around */
  4283. + if (arm_state->blocked_count) {
  4284. + reinit_completion(&arm_state->blocked_blocker);
  4285. + write_unlock_bh(&arm_state->susp_res_lock);
  4286. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  4287. + "blocked clients", __func__);
  4288. + if (wait_for_completion_interruptible_timeout(
  4289. + &arm_state->blocked_blocker, timeout_val)
  4290. + <= 0) {
  4291. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  4292. + "previously blocked clients failed" , __func__);
  4293. + status = VCHIQ_ERROR;
  4294. + write_lock_bh(&arm_state->susp_res_lock);
  4295. + goto out;
  4296. + }
  4297. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  4298. + "clients resumed", __func__);
  4299. + write_lock_bh(&arm_state->susp_res_lock);
  4300. + }
  4301. +
  4302. + /* We need to wait for resume to complete if it's in process */
  4303. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  4304. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  4305. + if (resume_count > 1) {
  4306. + status = VCHIQ_ERROR;
  4307. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  4308. + "many times for resume" , __func__);
  4309. + goto out;
  4310. + }
  4311. + write_unlock_bh(&arm_state->susp_res_lock);
  4312. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  4313. + __func__);
  4314. + if (wait_for_completion_interruptible_timeout(
  4315. + &arm_state->vc_resume_complete, timeout_val)
  4316. + <= 0) {
  4317. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  4318. + "resume failed (%s)", __func__,
  4319. + resume_state_names[arm_state->vc_resume_state +
  4320. + VC_RESUME_NUM_OFFSET]);
  4321. + status = VCHIQ_ERROR;
  4322. + write_lock_bh(&arm_state->susp_res_lock);
  4323. + goto out;
  4324. + }
  4325. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  4326. + write_lock_bh(&arm_state->susp_res_lock);
  4327. + resume_count++;
  4328. + }
  4329. + reinit_completion(&arm_state->resume_blocker);
  4330. + arm_state->resume_blocked = 1;
  4331. +
  4332. +out:
  4333. + return status;
  4334. +}
  4335. +
  4336. +static inline void
  4337. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  4338. +{
  4339. + complete_all(&arm_state->resume_blocker);
  4340. + arm_state->resume_blocked = 0;
  4341. +}
  4342. +
  4343. +/* Initiate suspend via slot handler. Should be called with the write lock
  4344. + * held */
  4345. +VCHIQ_STATUS_T
  4346. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  4347. +{
  4348. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  4349. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4350. +
  4351. + if (!arm_state)
  4352. + goto out;
  4353. +
  4354. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4355. + status = VCHIQ_SUCCESS;
  4356. +
  4357. +
  4358. + switch (arm_state->vc_suspend_state) {
  4359. + case VC_SUSPEND_REQUESTED:
  4360. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  4361. + "requested", __func__);
  4362. + break;
  4363. + case VC_SUSPEND_IN_PROGRESS:
  4364. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  4365. + "progress", __func__);
  4366. + break;
  4367. +
  4368. + default:
  4369. + /* We don't expect to be in other states, so log but continue
  4370. + * anyway */
  4371. + vchiq_log_error(vchiq_susp_log_level,
  4372. + "%s unexpected suspend state %s", __func__,
  4373. + suspend_state_names[arm_state->vc_suspend_state +
  4374. + VC_SUSPEND_NUM_OFFSET]);
  4375. + /* fall through */
  4376. + case VC_SUSPEND_REJECTED:
  4377. + case VC_SUSPEND_FAILED:
  4378. + /* Ensure any idle state actions have been run */
  4379. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4380. + /* fall through */
  4381. + case VC_SUSPEND_IDLE:
  4382. + vchiq_log_info(vchiq_susp_log_level,
  4383. + "%s: suspending", __func__);
  4384. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  4385. + /* kick the slot handler thread to initiate suspend */
  4386. + request_poll(state, NULL, 0);
  4387. + break;
  4388. + }
  4389. +
  4390. +out:
  4391. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  4392. + return status;
  4393. +}
  4394. +
  4395. +void
  4396. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  4397. +{
  4398. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4399. + int susp = 0;
  4400. +
  4401. + if (!arm_state)
  4402. + goto out;
  4403. +
  4404. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4405. +
  4406. + write_lock_bh(&arm_state->susp_res_lock);
  4407. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  4408. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  4409. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  4410. + susp = 1;
  4411. + }
  4412. + write_unlock_bh(&arm_state->susp_res_lock);
  4413. +
  4414. + if (susp)
  4415. + vchiq_platform_suspend(state);
  4416. +
  4417. +out:
  4418. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4419. + return;
  4420. +}
  4421. +
  4422. +
  4423. +static void
  4424. +output_timeout_error(VCHIQ_STATE_T *state)
  4425. +{
  4426. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4427. + char service_err[50] = "";
  4428. + int vc_use_count = arm_state->videocore_use_count;
  4429. + int active_services = state->unused_service;
  4430. + int i;
  4431. +
  4432. + if (!arm_state->videocore_use_count) {
  4433. + snprintf(service_err, 50, " Videocore usecount is 0");
  4434. + goto output_msg;
  4435. + }
  4436. + for (i = 0; i < active_services; i++) {
  4437. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  4438. + if (service_ptr && service_ptr->service_use_count &&
  4439. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  4440. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  4441. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  4442. + service_ptr->base.fourcc),
  4443. + service_ptr->client_id,
  4444. + service_ptr->service_use_count,
  4445. + service_ptr->service_use_count ==
  4446. + vc_use_count ? "" : " (+ more)");
  4447. + break;
  4448. + }
  4449. + }
  4450. +
  4451. +output_msg:
  4452. + vchiq_log_error(vchiq_susp_log_level,
  4453. + "timed out waiting for vc suspend (%d).%s",
  4454. + arm_state->autosuspend_override, service_err);
  4455. +
  4456. +}
  4457. +
  4458. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  4459. +** We don't actually force suspend, since videocore may get into a bad state
  4460. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  4461. +** determine a good point to suspend. If this doesn't happen within 100ms we
  4462. +** report failure.
  4463. +**
  4464. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  4465. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  4466. +*/
  4467. +VCHIQ_STATUS_T
  4468. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  4469. +{
  4470. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4471. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  4472. + long rc = 0;
  4473. + int repeat = -1;
  4474. +
  4475. + if (!arm_state)
  4476. + goto out;
  4477. +
  4478. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4479. +
  4480. + write_lock_bh(&arm_state->susp_res_lock);
  4481. +
  4482. + status = block_resume(arm_state);
  4483. + if (status != VCHIQ_SUCCESS)
  4484. + goto unlock;
  4485. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  4486. + /* Already suspended - just block resume and exit */
  4487. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  4488. + __func__);
  4489. + status = VCHIQ_SUCCESS;
  4490. + goto unlock;
  4491. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  4492. + /* initiate suspend immediately in the case that we're waiting
  4493. + * for the timeout */
  4494. + stop_suspend_timer(arm_state);
  4495. + if (!vchiq_videocore_wanted(state)) {
  4496. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  4497. + "idle, initiating suspend", __func__);
  4498. + status = vchiq_arm_vcsuspend(state);
  4499. + } else if (arm_state->autosuspend_override <
  4500. + FORCE_SUSPEND_FAIL_MAX) {
  4501. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  4502. + "videocore go idle", __func__);
  4503. + status = VCHIQ_SUCCESS;
  4504. + } else {
  4505. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  4506. + "many times - attempting suspend", __func__);
  4507. + status = vchiq_arm_vcsuspend(state);
  4508. + }
  4509. + } else {
  4510. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  4511. + "in progress - wait for completion", __func__);
  4512. + status = VCHIQ_SUCCESS;
  4513. + }
  4514. +
  4515. + /* Wait for suspend to happen due to system idle (not forced..) */
  4516. + if (status != VCHIQ_SUCCESS)
  4517. + goto unblock_resume;
  4518. +
  4519. + do {
  4520. + write_unlock_bh(&arm_state->susp_res_lock);
  4521. +
  4522. + rc = wait_for_completion_interruptible_timeout(
  4523. + &arm_state->vc_suspend_complete,
  4524. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  4525. +
  4526. + write_lock_bh(&arm_state->susp_res_lock);
  4527. + if (rc < 0) {
  4528. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  4529. + "interrupted waiting for suspend", __func__);
  4530. + status = VCHIQ_ERROR;
  4531. + goto unblock_resume;
  4532. + } else if (rc == 0) {
  4533. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  4534. + /* Repeat timeout once if in progress */
  4535. + if (repeat < 0) {
  4536. + repeat = 1;
  4537. + continue;
  4538. + }
  4539. + }
  4540. + arm_state->autosuspend_override++;
  4541. + output_timeout_error(state);
  4542. +
  4543. + status = VCHIQ_RETRY;
  4544. + goto unblock_resume;
  4545. + }
  4546. + } while (0 < (repeat--));
  4547. +
  4548. + /* Check and report state in case we need to abort ARM suspend */
  4549. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  4550. + status = VCHIQ_RETRY;
  4551. + vchiq_log_error(vchiq_susp_log_level,
  4552. + "%s videocore suspend failed (state %s)", __func__,
  4553. + suspend_state_names[arm_state->vc_suspend_state +
  4554. + VC_SUSPEND_NUM_OFFSET]);
  4555. + /* Reset the state only if it's still in an error state.
  4556. + * Something could have already initiated another suspend. */
  4557. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  4558. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4559. +
  4560. + goto unblock_resume;
  4561. + }
  4562. +
  4563. + /* successfully suspended - unlock and exit */
  4564. + goto unlock;
  4565. +
  4566. +unblock_resume:
  4567. + /* all error states need to unblock resume before exit */
  4568. + unblock_resume(arm_state);
  4569. +
  4570. +unlock:
  4571. + write_unlock_bh(&arm_state->susp_res_lock);
  4572. +
  4573. +out:
  4574. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  4575. + return status;
  4576. +}
  4577. +
  4578. +void
  4579. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  4580. +{
  4581. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4582. +
  4583. + if (!arm_state)
  4584. + goto out;
  4585. +
  4586. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4587. +
  4588. + write_lock_bh(&arm_state->susp_res_lock);
  4589. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  4590. + arm_state->first_connect &&
  4591. + !vchiq_videocore_wanted(state)) {
  4592. + vchiq_arm_vcsuspend(state);
  4593. + }
  4594. + write_unlock_bh(&arm_state->susp_res_lock);
  4595. +
  4596. +out:
  4597. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4598. + return;
  4599. +}
  4600. +
  4601. +
  4602. +int
  4603. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  4604. +{
  4605. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4606. + int resume = 0;
  4607. + int ret = -1;
  4608. +
  4609. + if (!arm_state)
  4610. + goto out;
  4611. +
  4612. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4613. +
  4614. + write_lock_bh(&arm_state->susp_res_lock);
  4615. + unblock_resume(arm_state);
  4616. + resume = vchiq_check_resume(state);
  4617. + write_unlock_bh(&arm_state->susp_res_lock);
  4618. +
  4619. + if (resume) {
  4620. + if (wait_for_completion_interruptible(
  4621. + &arm_state->vc_resume_complete) < 0) {
  4622. + vchiq_log_error(vchiq_susp_log_level,
  4623. + "%s interrupted", __func__);
  4624. + /* failed, cannot accurately derive suspend
  4625. + * state, so exit early. */
  4626. + goto out;
  4627. + }
  4628. + }
  4629. +
  4630. + read_lock_bh(&arm_state->susp_res_lock);
  4631. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  4632. + vchiq_log_info(vchiq_susp_log_level,
  4633. + "%s: Videocore remains suspended", __func__);
  4634. + } else {
  4635. + vchiq_log_info(vchiq_susp_log_level,
  4636. + "%s: Videocore resumed", __func__);
  4637. + ret = 0;
  4638. + }
  4639. + read_unlock_bh(&arm_state->susp_res_lock);
  4640. +out:
  4641. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4642. + return ret;
  4643. +}
  4644. +
  4645. +/* This function should be called with the write lock held */
  4646. +int
  4647. +vchiq_check_resume(VCHIQ_STATE_T *state)
  4648. +{
  4649. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4650. + int resume = 0;
  4651. +
  4652. + if (!arm_state)
  4653. + goto out;
  4654. +
  4655. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4656. +
  4657. + if (need_resume(state)) {
  4658. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  4659. + request_poll(state, NULL, 0);
  4660. + resume = 1;
  4661. + }
  4662. +
  4663. +out:
  4664. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4665. + return resume;
  4666. +}
  4667. +
  4668. +void
  4669. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  4670. +{
  4671. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4672. + int res = 0;
  4673. +
  4674. + if (!arm_state)
  4675. + goto out;
  4676. +
  4677. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4678. +
  4679. + write_lock_bh(&arm_state->susp_res_lock);
  4680. + if (arm_state->wake_address == 0) {
  4681. + vchiq_log_info(vchiq_susp_log_level,
  4682. + "%s: already awake", __func__);
  4683. + goto unlock;
  4684. + }
  4685. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  4686. + vchiq_log_info(vchiq_susp_log_level,
  4687. + "%s: already resuming", __func__);
  4688. + goto unlock;
  4689. + }
  4690. +
  4691. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  4692. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  4693. + res = 1;
  4694. + } else
  4695. + vchiq_log_trace(vchiq_susp_log_level,
  4696. + "%s: not resuming (resume state %s)", __func__,
  4697. + resume_state_names[arm_state->vc_resume_state +
  4698. + VC_RESUME_NUM_OFFSET]);
  4699. +
  4700. +unlock:
  4701. + write_unlock_bh(&arm_state->susp_res_lock);
  4702. +
  4703. + if (res)
  4704. + vchiq_platform_resume(state);
  4705. +
  4706. +out:
  4707. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4708. + return;
  4709. +
  4710. +}
  4711. +
  4712. +
  4713. +
  4714. +VCHIQ_STATUS_T
  4715. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  4716. + enum USE_TYPE_E use_type)
  4717. +{
  4718. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4719. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  4720. + char entity[16];
  4721. + int *entity_uc;
  4722. + int local_uc, local_entity_uc;
  4723. +
  4724. + if (!arm_state)
  4725. + goto out;
  4726. +
  4727. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4728. +
  4729. + if (use_type == USE_TYPE_VCHIQ) {
  4730. + sprintf(entity, "VCHIQ: ");
  4731. + entity_uc = &arm_state->peer_use_count;
  4732. + } else if (service) {
  4733. + sprintf(entity, "%c%c%c%c:%03d",
  4734. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  4735. + service->client_id);
  4736. + entity_uc = &service->service_use_count;
  4737. + } else {
  4738. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  4739. + "ptr", __func__);
  4740. + ret = VCHIQ_ERROR;
  4741. + goto out;
  4742. + }
  4743. +
  4744. + write_lock_bh(&arm_state->susp_res_lock);
  4745. + while (arm_state->resume_blocked) {
  4746. + /* If we call 'use' while force suspend is waiting for suspend,
  4747. + * then we're about to block the thread which the force is
  4748. + * waiting to complete, so we're bound to just time out. In this
  4749. + * case, set the suspend state such that the wait will be
  4750. + * canceled, so we can complete as quickly as possible. */
  4751. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  4752. + VC_SUSPEND_IDLE) {
  4753. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  4754. + break;
  4755. + }
  4756. + /* If suspend is already in progress then we need to block */
  4757. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  4758. + /* Indicate that there are threads waiting on the resume
  4759. + * blocker. These need to be allowed to complete before
  4760. + * a _second_ call to force suspend can complete,
  4761. + * otherwise low priority threads might never actually
  4762. + * continue */
  4763. + arm_state->blocked_count++;
  4764. + write_unlock_bh(&arm_state->susp_res_lock);
  4765. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  4766. + "blocked - waiting...", __func__, entity);
  4767. + if (wait_for_completion_killable(
  4768. + &arm_state->resume_blocker) != 0) {
  4769. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  4770. + "wait for resume blocker interrupted",
  4771. + __func__, entity);
  4772. + ret = VCHIQ_ERROR;
  4773. + write_lock_bh(&arm_state->susp_res_lock);
  4774. + arm_state->blocked_count--;
  4775. + write_unlock_bh(&arm_state->susp_res_lock);
  4776. + goto out;
  4777. + }
  4778. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  4779. + "unblocked", __func__, entity);
  4780. + write_lock_bh(&arm_state->susp_res_lock);
  4781. + if (--arm_state->blocked_count == 0)
  4782. + complete_all(&arm_state->blocked_blocker);
  4783. + }
  4784. + }
  4785. +
  4786. + stop_suspend_timer(arm_state);
  4787. +
  4788. + local_uc = ++arm_state->videocore_use_count;
  4789. + local_entity_uc = ++(*entity_uc);
  4790. +
  4791. + /* If there's a pending request which hasn't yet been serviced then
  4792. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  4793. + * vc_resume_complete will block until we either resume or fail to
  4794. + * suspend */
  4795. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  4796. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4797. +
  4798. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  4799. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  4800. + vchiq_log_info(vchiq_susp_log_level,
  4801. + "%s %s count %d, state count %d",
  4802. + __func__, entity, local_entity_uc, local_uc);
  4803. + request_poll(state, NULL, 0);
  4804. + } else
  4805. + vchiq_log_trace(vchiq_susp_log_level,
  4806. + "%s %s count %d, state count %d",
  4807. + __func__, entity, *entity_uc, local_uc);
  4808. +
  4809. +
  4810. + write_unlock_bh(&arm_state->susp_res_lock);
  4811. +
  4812. + /* Completion is in a done state when we're not suspended, so this won't
  4813. + * block for the non-suspended case. */
  4814. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  4815. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  4816. + __func__, entity);
  4817. + if (wait_for_completion_killable(
  4818. + &arm_state->vc_resume_complete) != 0) {
  4819. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  4820. + "resume interrupted", __func__, entity);
  4821. + ret = VCHIQ_ERROR;
  4822. + goto out;
  4823. + }
  4824. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  4825. + entity);
  4826. + }
  4827. +
  4828. + if (ret == VCHIQ_SUCCESS) {
  4829. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  4830. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  4831. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  4832. + /* Send the use notify to videocore */
  4833. + status = vchiq_send_remote_use_active(state);
  4834. + if (status == VCHIQ_SUCCESS)
  4835. + ack_cnt--;
  4836. + else
  4837. + atomic_add(ack_cnt,
  4838. + &arm_state->ka_use_ack_count);
  4839. + }
  4840. + }
  4841. +
  4842. +out:
  4843. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4844. + return ret;
  4845. +}
  4846. +
  4847. +VCHIQ_STATUS_T
  4848. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  4849. +{
  4850. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4851. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  4852. + char entity[16];
  4853. + int *entity_uc;
  4854. + int local_uc, local_entity_uc;
  4855. +
  4856. + if (!arm_state)
  4857. + goto out;
  4858. +
  4859. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4860. +
  4861. + if (service) {
  4862. + sprintf(entity, "%c%c%c%c:%03d",
  4863. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  4864. + service->client_id);
  4865. + entity_uc = &service->service_use_count;
  4866. + } else {
  4867. + sprintf(entity, "PEER: ");
  4868. + entity_uc = &arm_state->peer_use_count;
  4869. + }
  4870. +
  4871. + write_lock_bh(&arm_state->susp_res_lock);
  4872. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  4873. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  4874. + WARN_ON(!arm_state->videocore_use_count);
  4875. + WARN_ON(!(*entity_uc));
  4876. + ret = VCHIQ_ERROR;
  4877. + goto unlock;
  4878. + }
  4879. + local_uc = --arm_state->videocore_use_count;
  4880. + local_entity_uc = --(*entity_uc);
  4881. +
  4882. + if (!vchiq_videocore_wanted(state)) {
  4883. + if (vchiq_platform_use_suspend_timer() &&
  4884. + !arm_state->resume_blocked) {
  4885. + /* Only use the timer if we're not trying to force
  4886. + * suspend (=> resume_blocked) */
  4887. + start_suspend_timer(arm_state);
  4888. + } else {
  4889. + vchiq_log_info(vchiq_susp_log_level,
  4890. + "%s %s count %d, state count %d - suspending",
  4891. + __func__, entity, *entity_uc,
  4892. + arm_state->videocore_use_count);
  4893. + vchiq_arm_vcsuspend(state);
  4894. + }
  4895. + } else
  4896. + vchiq_log_trace(vchiq_susp_log_level,
  4897. + "%s %s count %d, state count %d",
  4898. + __func__, entity, *entity_uc,
  4899. + arm_state->videocore_use_count);
  4900. +
  4901. +unlock:
  4902. + write_unlock_bh(&arm_state->susp_res_lock);
  4903. +
  4904. +out:
  4905. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4906. + return ret;
  4907. +}
  4908. +
  4909. +void
  4910. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  4911. +{
  4912. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4913. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4914. + atomic_inc(&arm_state->ka_use_count);
  4915. + complete(&arm_state->ka_evt);
  4916. +}
  4917. +
  4918. +void
  4919. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  4920. +{
  4921. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4922. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4923. + atomic_inc(&arm_state->ka_release_count);
  4924. + complete(&arm_state->ka_evt);
  4925. +}
  4926. +
  4927. +VCHIQ_STATUS_T
  4928. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  4929. +{
  4930. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  4931. +}
  4932. +
  4933. +VCHIQ_STATUS_T
  4934. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  4935. +{
  4936. + return vchiq_release_internal(service->state, service);
  4937. +}
  4938. +
  4939. +VCHIQ_DEBUGFS_NODE_T *
  4940. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
  4941. +{
  4942. + return &instance->debugfs_node;
  4943. +}
  4944. +
  4945. +int
  4946. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  4947. +{
  4948. + VCHIQ_SERVICE_T *service;
  4949. + int use_count = 0, i;
  4950. + i = 0;
  4951. + while ((service = next_service_by_instance(instance->state,
  4952. + instance, &i)) != NULL) {
  4953. + use_count += service->service_use_count;
  4954. + unlock_service(service);
  4955. + }
  4956. + return use_count;
  4957. +}
  4958. +
  4959. +int
  4960. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
  4961. +{
  4962. + return instance->pid;
  4963. +}
  4964. +
  4965. +int
  4966. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
  4967. +{
  4968. + return instance->trace;
  4969. +}
  4970. +
  4971. +void
  4972. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
  4973. +{
  4974. + VCHIQ_SERVICE_T *service;
  4975. + int i;
  4976. + i = 0;
  4977. + while ((service = next_service_by_instance(instance->state,
  4978. + instance, &i)) != NULL) {
  4979. + service->trace = trace;
  4980. + unlock_service(service);
  4981. + }
  4982. + instance->trace = (trace != 0);
  4983. +}
  4984. +
  4985. +static void suspend_timer_callback(unsigned long context)
  4986. +{
  4987. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  4988. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4989. + if (!arm_state)
  4990. + goto out;
  4991. + vchiq_log_info(vchiq_susp_log_level,
  4992. + "%s - suspend timer expired - check suspend", __func__);
  4993. + vchiq_check_suspend(state);
  4994. +out:
  4995. + return;
  4996. +}
  4997. +
  4998. +VCHIQ_STATUS_T
  4999. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  5000. +{
  5001. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  5002. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  5003. + if (service) {
  5004. + ret = vchiq_use_internal(service->state, service,
  5005. + USE_TYPE_SERVICE_NO_RESUME);
  5006. + unlock_service(service);
  5007. + }
  5008. + return ret;
  5009. +}
  5010. +
  5011. +VCHIQ_STATUS_T
  5012. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  5013. +{
  5014. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  5015. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  5016. + if (service) {
  5017. + ret = vchiq_use_internal(service->state, service,
  5018. + USE_TYPE_SERVICE);
  5019. + unlock_service(service);
  5020. + }
  5021. + return ret;
  5022. +}
  5023. +
  5024. +VCHIQ_STATUS_T
  5025. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  5026. +{
  5027. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  5028. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  5029. + if (service) {
  5030. + ret = vchiq_release_internal(service->state, service);
  5031. + unlock_service(service);
  5032. + }
  5033. + return ret;
  5034. +}
  5035. +
  5036. +void
  5037. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  5038. +{
  5039. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  5040. + int i, j = 0;
  5041. + /* Only dump 64 services */
  5042. + static const int local_max_services = 64;
  5043. + /* If there's more than 64 services, only dump ones with
  5044. + * non-zero counts */
  5045. + int only_nonzero = 0;
  5046. + static const char *nz = "<-- preventing suspend";
  5047. +
  5048. + enum vc_suspend_status vc_suspend_state;
  5049. + enum vc_resume_status vc_resume_state;
  5050. + int peer_count;
  5051. + int vc_use_count;
  5052. + int active_services;
  5053. + struct service_data_struct {
  5054. + int fourcc;
  5055. + int clientid;
  5056. + int use_count;
  5057. + } service_data[local_max_services];
  5058. +
  5059. + if (!arm_state)
  5060. + return;
  5061. +
  5062. + read_lock_bh(&arm_state->susp_res_lock);
  5063. + vc_suspend_state = arm_state->vc_suspend_state;
  5064. + vc_resume_state = arm_state->vc_resume_state;
  5065. + peer_count = arm_state->peer_use_count;
  5066. + vc_use_count = arm_state->videocore_use_count;
  5067. + active_services = state->unused_service;
  5068. + if (active_services > local_max_services)
  5069. + only_nonzero = 1;
  5070. +
  5071. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  5072. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  5073. + if (!service_ptr)
  5074. + continue;
  5075. +
  5076. + if (only_nonzero && !service_ptr->service_use_count)
  5077. + continue;
  5078. +
  5079. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  5080. + service_data[j].fourcc = service_ptr->base.fourcc;
  5081. + service_data[j].clientid = service_ptr->client_id;
  5082. + service_data[j++].use_count = service_ptr->
  5083. + service_use_count;
  5084. + }
  5085. + }
  5086. +
  5087. + read_unlock_bh(&arm_state->susp_res_lock);
  5088. +
  5089. + vchiq_log_warning(vchiq_susp_log_level,
  5090. + "-- Videcore suspend state: %s --",
  5091. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  5092. + vchiq_log_warning(vchiq_susp_log_level,
  5093. + "-- Videcore resume state: %s --",
  5094. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  5095. +
  5096. + if (only_nonzero)
  5097. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  5098. + "services (%d). Only dumping up to first %d services "
  5099. + "with non-zero use-count", active_services,
  5100. + local_max_services);
  5101. +
  5102. + for (i = 0; i < j; i++) {
  5103. + vchiq_log_warning(vchiq_susp_log_level,
  5104. + "----- %c%c%c%c:%d service count %d %s",
  5105. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  5106. + service_data[i].clientid,
  5107. + service_data[i].use_count,
  5108. + service_data[i].use_count ? nz : "");
  5109. + }
  5110. + vchiq_log_warning(vchiq_susp_log_level,
  5111. + "----- VCHIQ use count count %d", peer_count);
  5112. + vchiq_log_warning(vchiq_susp_log_level,
  5113. + "--- Overall vchiq instance use count %d", vc_use_count);
  5114. +
  5115. + vchiq_dump_platform_use_state(state);
  5116. +}
  5117. +
  5118. +VCHIQ_STATUS_T
  5119. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  5120. +{
  5121. + VCHIQ_ARM_STATE_T *arm_state;
  5122. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  5123. +
  5124. + if (!service || !service->state)
  5125. + goto out;
  5126. +
  5127. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  5128. +
  5129. + arm_state = vchiq_platform_get_arm_state(service->state);
  5130. +
  5131. + read_lock_bh(&arm_state->susp_res_lock);
  5132. + if (service->service_use_count)
  5133. + ret = VCHIQ_SUCCESS;
  5134. + read_unlock_bh(&arm_state->susp_res_lock);
  5135. +
  5136. + if (ret == VCHIQ_ERROR) {
  5137. + vchiq_log_error(vchiq_susp_log_level,
  5138. + "%s ERROR - %c%c%c%c:%d service count %d, "
  5139. + "state count %d, videocore suspend state %s", __func__,
  5140. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  5141. + service->client_id, service->service_use_count,
  5142. + arm_state->videocore_use_count,
  5143. + suspend_state_names[arm_state->vc_suspend_state +
  5144. + VC_SUSPEND_NUM_OFFSET]);
  5145. + vchiq_dump_service_use_state(service->state);
  5146. + }
  5147. +out:
  5148. + return ret;
  5149. +}
  5150. +
  5151. +/* stub functions */
  5152. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  5153. +{
  5154. + (void)state;
  5155. +}
  5156. +
  5157. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  5158. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  5159. +{
  5160. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  5161. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  5162. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  5163. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  5164. + write_lock_bh(&arm_state->susp_res_lock);
  5165. + if (!arm_state->first_connect) {
  5166. + char threadname[10];
  5167. + arm_state->first_connect = 1;
  5168. + write_unlock_bh(&arm_state->susp_res_lock);
  5169. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  5170. + state->id);
  5171. + arm_state->ka_thread = kthread_create(
  5172. + &vchiq_keepalive_thread_func,
  5173. + (void *)state,
  5174. + threadname);
  5175. + if (arm_state->ka_thread == NULL) {
  5176. + vchiq_log_error(vchiq_susp_log_level,
  5177. + "vchiq: FATAL: couldn't create thread %s",
  5178. + threadname);
  5179. + } else {
  5180. + wake_up_process(arm_state->ka_thread);
  5181. + }
  5182. + } else
  5183. + write_unlock_bh(&arm_state->susp_res_lock);
  5184. + }
  5185. +}
  5186. +
  5187. +static int vchiq_probe(struct platform_device *pdev)
  5188. +{
  5189. + struct device_node *fw_node;
  5190. + struct rpi_firmware *fw;
  5191. + int err;
  5192. + void *ptr_err;
  5193. +
  5194. + fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
  5195. +/* Remove comment when booting without Device Tree is no longer supported
  5196. + if (!fw_node) {
  5197. + dev_err(&pdev->dev, "Missing firmware node\n");
  5198. + return -ENOENT;
  5199. + }
  5200. +*/
  5201. + fw = rpi_firmware_get(fw_node);
  5202. + if (!fw)
  5203. + return -EPROBE_DEFER;
  5204. +
  5205. + platform_set_drvdata(pdev, fw);
  5206. +
  5207. + /* create debugfs entries */
  5208. + err = vchiq_debugfs_init();
  5209. + if (err != 0)
  5210. + goto failed_debugfs_init;
  5211. +
  5212. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  5213. + if (err != 0) {
  5214. + vchiq_log_error(vchiq_arm_log_level,
  5215. + "Unable to allocate device number");
  5216. + goto failed_alloc_chrdev;
  5217. + }
  5218. + cdev_init(&vchiq_cdev, &vchiq_fops);
  5219. + vchiq_cdev.owner = THIS_MODULE;
  5220. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  5221. + if (err != 0) {
  5222. + vchiq_log_error(vchiq_arm_log_level,
  5223. + "Unable to register device");
  5224. + goto failed_cdev_add;
  5225. + }
  5226. +
  5227. + /* create sysfs entries */
  5228. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  5229. + ptr_err = vchiq_class;
  5230. + if (IS_ERR(ptr_err))
  5231. + goto failed_class_create;
  5232. +
  5233. + vchiq_dev = device_create(vchiq_class, NULL,
  5234. + vchiq_devid, NULL, "vchiq");
  5235. + ptr_err = vchiq_dev;
  5236. + if (IS_ERR(ptr_err))
  5237. + goto failed_device_create;
  5238. +
  5239. + err = vchiq_platform_init(pdev, &g_state);
  5240. + if (err != 0)
  5241. + goto failed_platform_init;
  5242. +
  5243. + vchiq_log_info(vchiq_arm_log_level,
  5244. + "vchiq: initialised - version %d (min %d), device %d.%d",
  5245. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  5246. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  5247. +
  5248. + return 0;
  5249. +
  5250. +failed_platform_init:
  5251. + device_destroy(vchiq_class, vchiq_devid);
  5252. +failed_device_create:
  5253. + class_destroy(vchiq_class);
  5254. +failed_class_create:
  5255. + cdev_del(&vchiq_cdev);
  5256. + err = PTR_ERR(ptr_err);
  5257. +failed_cdev_add:
  5258. + unregister_chrdev_region(vchiq_devid, 1);
  5259. +failed_alloc_chrdev:
  5260. + vchiq_debugfs_deinit();
  5261. +failed_debugfs_init:
  5262. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  5263. + return err;
  5264. +}
  5265. +
  5266. +static int vchiq_remove(struct platform_device *pdev)
  5267. +{
  5268. + device_destroy(vchiq_class, vchiq_devid);
  5269. + class_destroy(vchiq_class);
  5270. + cdev_del(&vchiq_cdev);
  5271. + unregister_chrdev_region(vchiq_devid, 1);
  5272. +
  5273. + return 0;
  5274. +}
  5275. +
  5276. +static const struct of_device_id vchiq_of_match[] = {
  5277. + { .compatible = "brcm,bcm2835-vchiq", },
  5278. + {},
  5279. +};
  5280. +MODULE_DEVICE_TABLE(of, vchiq_of_match);
  5281. +
  5282. +static struct platform_driver vchiq_driver = {
  5283. + .driver = {
  5284. + .name = "bcm2835_vchiq",
  5285. + .owner = THIS_MODULE,
  5286. + .of_match_table = vchiq_of_match,
  5287. + },
  5288. + .probe = vchiq_probe,
  5289. + .remove = vchiq_remove,
  5290. +};
  5291. +module_platform_driver(vchiq_driver);
  5292. +
  5293. +MODULE_LICENSE("GPL");
  5294. +MODULE_AUTHOR("Broadcom Corporation");
  5295. --- /dev/null
  5296. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  5297. @@ -0,0 +1,220 @@
  5298. +/**
  5299. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  5300. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5301. + *
  5302. + * Redistribution and use in source and binary forms, with or without
  5303. + * modification, are permitted provided that the following conditions
  5304. + * are met:
  5305. + * 1. Redistributions of source code must retain the above copyright
  5306. + * notice, this list of conditions, and the following disclaimer,
  5307. + * without modification.
  5308. + * 2. Redistributions in binary form must reproduce the above copyright
  5309. + * notice, this list of conditions and the following disclaimer in the
  5310. + * documentation and/or other materials provided with the distribution.
  5311. + * 3. The names of the above-listed copyright holders may not be used
  5312. + * to endorse or promote products derived from this software without
  5313. + * specific prior written permission.
  5314. + *
  5315. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5316. + * GNU General Public License ("GPL") version 2, as published by the Free
  5317. + * Software Foundation.
  5318. + *
  5319. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5320. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5321. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5322. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5323. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5324. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5325. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5326. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5327. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5328. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5329. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5330. + */
  5331. +
  5332. +#ifndef VCHIQ_ARM_H
  5333. +#define VCHIQ_ARM_H
  5334. +
  5335. +#include <linux/mutex.h>
  5336. +#include <linux/platform_device.h>
  5337. +#include <linux/semaphore.h>
  5338. +#include <linux/atomic.h>
  5339. +#include "vchiq_core.h"
  5340. +#include "vchiq_debugfs.h"
  5341. +
  5342. +
  5343. +enum vc_suspend_status {
  5344. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  5345. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  5346. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  5347. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  5348. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  5349. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  5350. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  5351. +};
  5352. +
  5353. +enum vc_resume_status {
  5354. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  5355. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  5356. + VC_RESUME_REQUESTED, /* User has requested resume */
  5357. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  5358. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  5359. +};
  5360. +
  5361. +
  5362. +enum USE_TYPE_E {
  5363. + USE_TYPE_SERVICE,
  5364. + USE_TYPE_SERVICE_NO_RESUME,
  5365. + USE_TYPE_VCHIQ
  5366. +};
  5367. +
  5368. +
  5369. +
  5370. +typedef struct vchiq_arm_state_struct {
  5371. + /* Keepalive-related data */
  5372. + struct task_struct *ka_thread;
  5373. + struct completion ka_evt;
  5374. + atomic_t ka_use_count;
  5375. + atomic_t ka_use_ack_count;
  5376. + atomic_t ka_release_count;
  5377. +
  5378. + struct completion vc_suspend_complete;
  5379. + struct completion vc_resume_complete;
  5380. +
  5381. + rwlock_t susp_res_lock;
  5382. + enum vc_suspend_status vc_suspend_state;
  5383. + enum vc_resume_status vc_resume_state;
  5384. +
  5385. + unsigned int wake_address;
  5386. +
  5387. + struct timer_list suspend_timer;
  5388. + int suspend_timer_timeout;
  5389. + int suspend_timer_running;
  5390. +
  5391. + /* Global use count for videocore.
  5392. + ** This is equal to the sum of the use counts for all services. When
  5393. + ** this hits zero the videocore suspend procedure will be initiated.
  5394. + */
  5395. + int videocore_use_count;
  5396. +
  5397. + /* Use count to track requests from videocore peer.
  5398. + ** This use count is not associated with a service, so needs to be
  5399. + ** tracked separately with the state.
  5400. + */
  5401. + int peer_use_count;
  5402. +
  5403. + /* Flag to indicate whether resume is blocked. This happens when the
  5404. + ** ARM is suspending
  5405. + */
  5406. + struct completion resume_blocker;
  5407. + int resume_blocked;
  5408. + struct completion blocked_blocker;
  5409. + int blocked_count;
  5410. +
  5411. + int autosuspend_override;
  5412. +
  5413. + /* Flag to indicate that the first vchiq connect has made it through.
  5414. + ** This means that both sides should be fully ready, and we should
  5415. + ** be able to suspend after this point.
  5416. + */
  5417. + int first_connect;
  5418. +
  5419. + unsigned long long suspend_start_time;
  5420. + unsigned long long sleep_start_time;
  5421. + unsigned long long resume_start_time;
  5422. + unsigned long long last_wake_time;
  5423. +
  5424. +} VCHIQ_ARM_STATE_T;
  5425. +
  5426. +extern int vchiq_arm_log_level;
  5427. +extern int vchiq_susp_log_level;
  5428. +
  5429. +int vchiq_platform_init(struct platform_device *pdev, VCHIQ_STATE_T *state);
  5430. +
  5431. +extern VCHIQ_STATE_T *
  5432. +vchiq_get_state(void);
  5433. +
  5434. +extern VCHIQ_STATUS_T
  5435. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  5436. +
  5437. +extern VCHIQ_STATUS_T
  5438. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  5439. +
  5440. +extern int
  5441. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  5442. +
  5443. +extern VCHIQ_STATUS_T
  5444. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  5445. +
  5446. +extern VCHIQ_STATUS_T
  5447. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  5448. +
  5449. +extern int
  5450. +vchiq_check_resume(VCHIQ_STATE_T *state);
  5451. +
  5452. +extern void
  5453. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  5454. + VCHIQ_STATUS_T
  5455. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  5456. +
  5457. +extern VCHIQ_STATUS_T
  5458. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  5459. +
  5460. +extern VCHIQ_STATUS_T
  5461. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  5462. +
  5463. +extern VCHIQ_STATUS_T
  5464. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  5465. +
  5466. +extern int
  5467. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  5468. +
  5469. +extern int
  5470. +vchiq_platform_use_suspend_timer(void);
  5471. +
  5472. +extern void
  5473. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  5474. +
  5475. +extern void
  5476. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  5477. +
  5478. +extern VCHIQ_ARM_STATE_T*
  5479. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  5480. +
  5481. +extern int
  5482. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  5483. +
  5484. +extern VCHIQ_STATUS_T
  5485. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  5486. + enum USE_TYPE_E use_type);
  5487. +extern VCHIQ_STATUS_T
  5488. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  5489. +
  5490. +extern VCHIQ_DEBUGFS_NODE_T *
  5491. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance);
  5492. +
  5493. +extern int
  5494. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance);
  5495. +
  5496. +extern int
  5497. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance);
  5498. +
  5499. +extern int
  5500. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance);
  5501. +
  5502. +extern void
  5503. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace);
  5504. +
  5505. +extern void
  5506. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  5507. + enum vc_suspend_status new_state);
  5508. +
  5509. +extern void
  5510. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  5511. + enum vc_resume_status new_state);
  5512. +
  5513. +extern void
  5514. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  5515. +
  5516. +
  5517. +#endif /* VCHIQ_ARM_H */
  5518. --- /dev/null
  5519. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  5520. @@ -0,0 +1,37 @@
  5521. +/**
  5522. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5523. + *
  5524. + * Redistribution and use in source and binary forms, with or without
  5525. + * modification, are permitted provided that the following conditions
  5526. + * are met:
  5527. + * 1. Redistributions of source code must retain the above copyright
  5528. + * notice, this list of conditions, and the following disclaimer,
  5529. + * without modification.
  5530. + * 2. Redistributions in binary form must reproduce the above copyright
  5531. + * notice, this list of conditions and the following disclaimer in the
  5532. + * documentation and/or other materials provided with the distribution.
  5533. + * 3. The names of the above-listed copyright holders may not be used
  5534. + * to endorse or promote products derived from this software without
  5535. + * specific prior written permission.
  5536. + *
  5537. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5538. + * GNU General Public License ("GPL") version 2, as published by the Free
  5539. + * Software Foundation.
  5540. + *
  5541. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5542. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5543. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5544. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5545. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5546. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5547. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5548. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5549. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5550. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5551. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5552. + */
  5553. +
  5554. +const char *vchiq_get_build_hostname(void);
  5555. +const char *vchiq_get_build_version(void);
  5556. +const char *vchiq_get_build_time(void);
  5557. +const char *vchiq_get_build_date(void);
  5558. --- /dev/null
  5559. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  5560. @@ -0,0 +1,69 @@
  5561. +/**
  5562. + * Copyright (c) 2010-2014 Broadcom. All rights reserved.
  5563. + *
  5564. + * Redistribution and use in source and binary forms, with or without
  5565. + * modification, are permitted provided that the following conditions
  5566. + * are met:
  5567. + * 1. Redistributions of source code must retain the above copyright
  5568. + * notice, this list of conditions, and the following disclaimer,
  5569. + * without modification.
  5570. + * 2. Redistributions in binary form must reproduce the above copyright
  5571. + * notice, this list of conditions and the following disclaimer in the
  5572. + * documentation and/or other materials provided with the distribution.
  5573. + * 3. The names of the above-listed copyright holders may not be used
  5574. + * to endorse or promote products derived from this software without
  5575. + * specific prior written permission.
  5576. + *
  5577. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5578. + * GNU General Public License ("GPL") version 2, as published by the Free
  5579. + * Software Foundation.
  5580. + *
  5581. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5582. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5583. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5584. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5585. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5586. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5587. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5588. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5589. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5590. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5591. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5592. + */
  5593. +
  5594. +#ifndef VCHIQ_CFG_H
  5595. +#define VCHIQ_CFG_H
  5596. +
  5597. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  5598. +/* The version of VCHIQ - change with any non-trivial change */
  5599. +#define VCHIQ_VERSION 8
  5600. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  5601. +** incompatible change */
  5602. +#define VCHIQ_VERSION_MIN 3
  5603. +
  5604. +/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
  5605. +#define VCHIQ_VERSION_LIB_VERSION 7
  5606. +
  5607. +/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
  5608. +#define VCHIQ_VERSION_CLOSE_DELIVERED 7
  5609. +
  5610. +/* The version that made it safe to use SYNCHRONOUS mode */
  5611. +#define VCHIQ_VERSION_SYNCHRONOUS_MODE 8
  5612. +
  5613. +#define VCHIQ_MAX_STATES 1
  5614. +#define VCHIQ_MAX_SERVICES 4096
  5615. +#define VCHIQ_MAX_SLOTS 128
  5616. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  5617. +
  5618. +#define VCHIQ_NUM_CURRENT_BULKS 32
  5619. +#define VCHIQ_NUM_SERVICE_BULKS 4
  5620. +
  5621. +#ifndef VCHIQ_ENABLE_DEBUG
  5622. +#define VCHIQ_ENABLE_DEBUG 1
  5623. +#endif
  5624. +
  5625. +#ifndef VCHIQ_ENABLE_STATS
  5626. +#define VCHIQ_ENABLE_STATS 1
  5627. +#endif
  5628. +
  5629. +#endif /* VCHIQ_CFG_H */
  5630. --- /dev/null
  5631. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  5632. @@ -0,0 +1,120 @@
  5633. +/**
  5634. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5635. + *
  5636. + * Redistribution and use in source and binary forms, with or without
  5637. + * modification, are permitted provided that the following conditions
  5638. + * are met:
  5639. + * 1. Redistributions of source code must retain the above copyright
  5640. + * notice, this list of conditions, and the following disclaimer,
  5641. + * without modification.
  5642. + * 2. Redistributions in binary form must reproduce the above copyright
  5643. + * notice, this list of conditions and the following disclaimer in the
  5644. + * documentation and/or other materials provided with the distribution.
  5645. + * 3. The names of the above-listed copyright holders may not be used
  5646. + * to endorse or promote products derived from this software without
  5647. + * specific prior written permission.
  5648. + *
  5649. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5650. + * GNU General Public License ("GPL") version 2, as published by the Free
  5651. + * Software Foundation.
  5652. + *
  5653. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5654. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5655. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5656. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5657. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5658. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5659. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5660. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5661. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5662. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5663. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5664. + */
  5665. +
  5666. +#include "vchiq_connected.h"
  5667. +#include "vchiq_core.h"
  5668. +#include "vchiq_killable.h"
  5669. +#include <linux/module.h>
  5670. +#include <linux/mutex.h>
  5671. +
  5672. +#define MAX_CALLBACKS 10
  5673. +
  5674. +static int g_connected;
  5675. +static int g_num_deferred_callbacks;
  5676. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  5677. +static int g_once_init;
  5678. +static struct mutex g_connected_mutex;
  5679. +
  5680. +/****************************************************************************
  5681. +*
  5682. +* Function to initialize our lock.
  5683. +*
  5684. +***************************************************************************/
  5685. +
  5686. +static void connected_init(void)
  5687. +{
  5688. + if (!g_once_init) {
  5689. + mutex_init(&g_connected_mutex);
  5690. + g_once_init = 1;
  5691. + }
  5692. +}
  5693. +
  5694. +/****************************************************************************
  5695. +*
  5696. +* This function is used to defer initialization until the vchiq stack is
  5697. +* initialized. If the stack is already initialized, then the callback will
  5698. +* be made immediately, otherwise it will be deferred until
  5699. +* vchiq_call_connected_callbacks is called.
  5700. +*
  5701. +***************************************************************************/
  5702. +
  5703. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  5704. +{
  5705. + connected_init();
  5706. +
  5707. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  5708. + return;
  5709. +
  5710. + if (g_connected)
  5711. + /* We're already connected. Call the callback immediately. */
  5712. +
  5713. + callback();
  5714. + else {
  5715. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  5716. + vchiq_log_error(vchiq_core_log_level,
  5717. + "There already %d callback registered - "
  5718. + "please increase MAX_CALLBACKS",
  5719. + g_num_deferred_callbacks);
  5720. + else {
  5721. + g_deferred_callback[g_num_deferred_callbacks] =
  5722. + callback;
  5723. + g_num_deferred_callbacks++;
  5724. + }
  5725. + }
  5726. + mutex_unlock(&g_connected_mutex);
  5727. +}
  5728. +
  5729. +/****************************************************************************
  5730. +*
  5731. +* This function is called by the vchiq stack once it has been connected to
  5732. +* the videocore and clients can start to use the stack.
  5733. +*
  5734. +***************************************************************************/
  5735. +
  5736. +void vchiq_call_connected_callbacks(void)
  5737. +{
  5738. + int i;
  5739. +
  5740. + connected_init();
  5741. +
  5742. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  5743. + return;
  5744. +
  5745. + for (i = 0; i < g_num_deferred_callbacks; i++)
  5746. + g_deferred_callback[i]();
  5747. +
  5748. + g_num_deferred_callbacks = 0;
  5749. + g_connected = 1;
  5750. + mutex_unlock(&g_connected_mutex);
  5751. +}
  5752. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  5753. --- /dev/null
  5754. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  5755. @@ -0,0 +1,50 @@
  5756. +/**
  5757. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5758. + *
  5759. + * Redistribution and use in source and binary forms, with or without
  5760. + * modification, are permitted provided that the following conditions
  5761. + * are met:
  5762. + * 1. Redistributions of source code must retain the above copyright
  5763. + * notice, this list of conditions, and the following disclaimer,
  5764. + * without modification.
  5765. + * 2. Redistributions in binary form must reproduce the above copyright
  5766. + * notice, this list of conditions and the following disclaimer in the
  5767. + * documentation and/or other materials provided with the distribution.
  5768. + * 3. The names of the above-listed copyright holders may not be used
  5769. + * to endorse or promote products derived from this software without
  5770. + * specific prior written permission.
  5771. + *
  5772. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5773. + * GNU General Public License ("GPL") version 2, as published by the Free
  5774. + * Software Foundation.
  5775. + *
  5776. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5777. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5778. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5779. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5780. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5781. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5782. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5783. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5784. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5785. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5786. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5787. + */
  5788. +
  5789. +#ifndef VCHIQ_CONNECTED_H
  5790. +#define VCHIQ_CONNECTED_H
  5791. +
  5792. +/* ---- Include Files ----------------------------------------------------- */
  5793. +
  5794. +/* ---- Constants and Types ---------------------------------------------- */
  5795. +
  5796. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  5797. +
  5798. +/* ---- Variable Externs ------------------------------------------------- */
  5799. +
  5800. +/* ---- Function Prototypes ---------------------------------------------- */
  5801. +
  5802. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  5803. +void vchiq_call_connected_callbacks(void);
  5804. +
  5805. +#endif /* VCHIQ_CONNECTED_H */
  5806. --- /dev/null
  5807. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  5808. @@ -0,0 +1,3934 @@
  5809. +/**
  5810. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5811. + *
  5812. + * Redistribution and use in source and binary forms, with or without
  5813. + * modification, are permitted provided that the following conditions
  5814. + * are met:
  5815. + * 1. Redistributions of source code must retain the above copyright
  5816. + * notice, this list of conditions, and the following disclaimer,
  5817. + * without modification.
  5818. + * 2. Redistributions in binary form must reproduce the above copyright
  5819. + * notice, this list of conditions and the following disclaimer in the
  5820. + * documentation and/or other materials provided with the distribution.
  5821. + * 3. The names of the above-listed copyright holders may not be used
  5822. + * to endorse or promote products derived from this software without
  5823. + * specific prior written permission.
  5824. + *
  5825. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5826. + * GNU General Public License ("GPL") version 2, as published by the Free
  5827. + * Software Foundation.
  5828. + *
  5829. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5830. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5831. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5832. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5833. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5834. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5835. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5836. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5837. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5838. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5839. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5840. + */
  5841. +
  5842. +#include "vchiq_core.h"
  5843. +#include "vchiq_killable.h"
  5844. +
  5845. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  5846. +
  5847. +#define HANDLE_STATE_SHIFT 12
  5848. +
  5849. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  5850. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  5851. +#define SLOT_INDEX_FROM_DATA(state, data) \
  5852. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  5853. + VCHIQ_SLOT_SIZE)
  5854. +#define SLOT_INDEX_FROM_INFO(state, info) \
  5855. + ((unsigned int)(info - state->slot_info))
  5856. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  5857. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  5858. +
  5859. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  5860. +
  5861. +#define SRVTRACE_LEVEL(srv) \
  5862. + (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
  5863. +#define SRVTRACE_ENABLED(srv, lev) \
  5864. + (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
  5865. +
  5866. +struct vchiq_open_payload {
  5867. + int fourcc;
  5868. + int client_id;
  5869. + short version;
  5870. + short version_min;
  5871. +};
  5872. +
  5873. +struct vchiq_openack_payload {
  5874. + short version;
  5875. +};
  5876. +
  5877. +enum
  5878. +{
  5879. + QMFLAGS_IS_BLOCKING = (1 << 0),
  5880. + QMFLAGS_NO_MUTEX_LOCK = (1 << 1),
  5881. + QMFLAGS_NO_MUTEX_UNLOCK = (1 << 2)
  5882. +};
  5883. +
  5884. +/* we require this for consistency between endpoints */
  5885. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  5886. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  5887. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  5888. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  5889. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  5890. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  5891. +
  5892. +/* Run time control of log level, based on KERN_XXX level. */
  5893. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  5894. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  5895. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  5896. +
  5897. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  5898. +
  5899. +static DEFINE_SPINLOCK(service_spinlock);
  5900. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  5901. +DEFINE_SPINLOCK(quota_spinlock);
  5902. +
  5903. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  5904. +static unsigned int handle_seq;
  5905. +
  5906. +static const char *const srvstate_names[] = {
  5907. + "FREE",
  5908. + "HIDDEN",
  5909. + "LISTENING",
  5910. + "OPENING",
  5911. + "OPEN",
  5912. + "OPENSYNC",
  5913. + "CLOSESENT",
  5914. + "CLOSERECVD",
  5915. + "CLOSEWAIT",
  5916. + "CLOSED"
  5917. +};
  5918. +
  5919. +static const char *const reason_names[] = {
  5920. + "SERVICE_OPENED",
  5921. + "SERVICE_CLOSED",
  5922. + "MESSAGE_AVAILABLE",
  5923. + "BULK_TRANSMIT_DONE",
  5924. + "BULK_RECEIVE_DONE",
  5925. + "BULK_TRANSMIT_ABORTED",
  5926. + "BULK_RECEIVE_ABORTED"
  5927. +};
  5928. +
  5929. +static const char *const conn_state_names[] = {
  5930. + "DISCONNECTED",
  5931. + "CONNECTING",
  5932. + "CONNECTED",
  5933. + "PAUSING",
  5934. + "PAUSE_SENT",
  5935. + "PAUSED",
  5936. + "RESUMING",
  5937. + "PAUSE_TIMEOUT",
  5938. + "RESUME_TIMEOUT"
  5939. +};
  5940. +
  5941. +
  5942. +static void
  5943. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  5944. +
  5945. +static const char *msg_type_str(unsigned int msg_type)
  5946. +{
  5947. + switch (msg_type) {
  5948. + case VCHIQ_MSG_PADDING: return "PADDING";
  5949. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  5950. + case VCHIQ_MSG_OPEN: return "OPEN";
  5951. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  5952. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  5953. + case VCHIQ_MSG_DATA: return "DATA";
  5954. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  5955. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  5956. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  5957. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  5958. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  5959. + case VCHIQ_MSG_RESUME: return "RESUME";
  5960. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  5961. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  5962. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  5963. + }
  5964. + return "???";
  5965. +}
  5966. +
  5967. +static inline void
  5968. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  5969. +{
  5970. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  5971. + service->state->id, service->localport,
  5972. + srvstate_names[service->srvstate],
  5973. + srvstate_names[newstate]);
  5974. + service->srvstate = newstate;
  5975. +}
  5976. +
  5977. +VCHIQ_SERVICE_T *
  5978. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  5979. +{
  5980. + VCHIQ_SERVICE_T *service;
  5981. +
  5982. + spin_lock(&service_spinlock);
  5983. + service = handle_to_service(handle);
  5984. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  5985. + (service->handle == handle)) {
  5986. + BUG_ON(service->ref_count == 0);
  5987. + service->ref_count++;
  5988. + } else
  5989. + service = NULL;
  5990. + spin_unlock(&service_spinlock);
  5991. +
  5992. + if (!service)
  5993. + vchiq_log_info(vchiq_core_log_level,
  5994. + "Invalid service handle 0x%x", handle);
  5995. +
  5996. + return service;
  5997. +}
  5998. +
  5999. +VCHIQ_SERVICE_T *
  6000. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  6001. +{
  6002. + VCHIQ_SERVICE_T *service = NULL;
  6003. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  6004. + spin_lock(&service_spinlock);
  6005. + service = state->services[localport];
  6006. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  6007. + BUG_ON(service->ref_count == 0);
  6008. + service->ref_count++;
  6009. + } else
  6010. + service = NULL;
  6011. + spin_unlock(&service_spinlock);
  6012. + }
  6013. +
  6014. + if (!service)
  6015. + vchiq_log_info(vchiq_core_log_level,
  6016. + "Invalid port %d", localport);
  6017. +
  6018. + return service;
  6019. +}
  6020. +
  6021. +VCHIQ_SERVICE_T *
  6022. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  6023. + VCHIQ_SERVICE_HANDLE_T handle) {
  6024. + VCHIQ_SERVICE_T *service;
  6025. +
  6026. + spin_lock(&service_spinlock);
  6027. + service = handle_to_service(handle);
  6028. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  6029. + (service->handle == handle) &&
  6030. + (service->instance == instance)) {
  6031. + BUG_ON(service->ref_count == 0);
  6032. + service->ref_count++;
  6033. + } else
  6034. + service = NULL;
  6035. + spin_unlock(&service_spinlock);
  6036. +
  6037. + if (!service)
  6038. + vchiq_log_info(vchiq_core_log_level,
  6039. + "Invalid service handle 0x%x", handle);
  6040. +
  6041. + return service;
  6042. +}
  6043. +
  6044. +VCHIQ_SERVICE_T *
  6045. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  6046. + VCHIQ_SERVICE_HANDLE_T handle) {
  6047. + VCHIQ_SERVICE_T *service;
  6048. +
  6049. + spin_lock(&service_spinlock);
  6050. + service = handle_to_service(handle);
  6051. + if (service &&
  6052. + ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  6053. + (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
  6054. + (service->handle == handle) &&
  6055. + (service->instance == instance)) {
  6056. + BUG_ON(service->ref_count == 0);
  6057. + service->ref_count++;
  6058. + } else
  6059. + service = NULL;
  6060. + spin_unlock(&service_spinlock);
  6061. +
  6062. + if (!service)
  6063. + vchiq_log_info(vchiq_core_log_level,
  6064. + "Invalid service handle 0x%x", handle);
  6065. +
  6066. + return service;
  6067. +}
  6068. +
  6069. +VCHIQ_SERVICE_T *
  6070. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  6071. + int *pidx)
  6072. +{
  6073. + VCHIQ_SERVICE_T *service = NULL;
  6074. + int idx = *pidx;
  6075. +
  6076. + spin_lock(&service_spinlock);
  6077. + while (idx < state->unused_service) {
  6078. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  6079. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  6080. + (srv->instance == instance)) {
  6081. + service = srv;
  6082. + BUG_ON(service->ref_count == 0);
  6083. + service->ref_count++;
  6084. + break;
  6085. + }
  6086. + }
  6087. + spin_unlock(&service_spinlock);
  6088. +
  6089. + *pidx = idx;
  6090. +
  6091. + return service;
  6092. +}
  6093. +
  6094. +void
  6095. +lock_service(VCHIQ_SERVICE_T *service)
  6096. +{
  6097. + spin_lock(&service_spinlock);
  6098. + BUG_ON(!service || (service->ref_count == 0));
  6099. + if (service)
  6100. + service->ref_count++;
  6101. + spin_unlock(&service_spinlock);
  6102. +}
  6103. +
  6104. +void
  6105. +unlock_service(VCHIQ_SERVICE_T *service)
  6106. +{
  6107. + VCHIQ_STATE_T *state = service->state;
  6108. + spin_lock(&service_spinlock);
  6109. + BUG_ON(!service || (service->ref_count == 0));
  6110. + if (service && service->ref_count) {
  6111. + service->ref_count--;
  6112. + if (!service->ref_count) {
  6113. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  6114. + state->services[service->localport] = NULL;
  6115. + } else
  6116. + service = NULL;
  6117. + }
  6118. + spin_unlock(&service_spinlock);
  6119. +
  6120. + if (service && service->userdata_term)
  6121. + service->userdata_term(service->base.userdata);
  6122. +
  6123. + kfree(service);
  6124. +}
  6125. +
  6126. +int
  6127. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  6128. +{
  6129. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  6130. + int id;
  6131. +
  6132. + id = service ? service->client_id : 0;
  6133. + if (service)
  6134. + unlock_service(service);
  6135. +
  6136. + return id;
  6137. +}
  6138. +
  6139. +void *
  6140. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  6141. +{
  6142. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  6143. +
  6144. + return service ? service->base.userdata : NULL;
  6145. +}
  6146. +
  6147. +int
  6148. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  6149. +{
  6150. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  6151. +
  6152. + return service ? service->base.fourcc : 0;
  6153. +}
  6154. +
  6155. +static void
  6156. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  6157. +{
  6158. + VCHIQ_STATE_T *state = service->state;
  6159. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  6160. +
  6161. + service->closing = 1;
  6162. +
  6163. + /* Synchronise with other threads. */
  6164. + mutex_lock(&state->recycle_mutex);
  6165. + mutex_unlock(&state->recycle_mutex);
  6166. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  6167. + /* If we're pausing then the slot_mutex is held until resume
  6168. + * by the slot handler. Therefore don't try to acquire this
  6169. + * mutex if we're the slot handler and in the pause sent state.
  6170. + * We don't need to in this case anyway. */
  6171. + mutex_lock(&state->slot_mutex);
  6172. + mutex_unlock(&state->slot_mutex);
  6173. + }
  6174. +
  6175. + /* Unblock any sending thread. */
  6176. + service_quota = &state->service_quotas[service->localport];
  6177. + up(&service_quota->quota_event);
  6178. +}
  6179. +
  6180. +static void
  6181. +mark_service_closing(VCHIQ_SERVICE_T *service)
  6182. +{
  6183. + mark_service_closing_internal(service, 0);
  6184. +}
  6185. +
  6186. +static inline VCHIQ_STATUS_T
  6187. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  6188. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  6189. +{
  6190. + VCHIQ_STATUS_T status;
  6191. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  6192. + service->state->id, service->localport, reason_names[reason],
  6193. + (unsigned int)header, (unsigned int)bulk_userdata);
  6194. + status = service->base.callback(reason, header, service->handle,
  6195. + bulk_userdata);
  6196. + if (status == VCHIQ_ERROR) {
  6197. + vchiq_log_warning(vchiq_core_log_level,
  6198. + "%d: ignoring ERROR from callback to service %x",
  6199. + service->state->id, service->handle);
  6200. + status = VCHIQ_SUCCESS;
  6201. + }
  6202. + return status;
  6203. +}
  6204. +
  6205. +inline void
  6206. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  6207. +{
  6208. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  6209. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  6210. + conn_state_names[oldstate],
  6211. + conn_state_names[newstate]);
  6212. + state->conn_state = newstate;
  6213. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  6214. +}
  6215. +
  6216. +static inline void
  6217. +remote_event_create(REMOTE_EVENT_T *event)
  6218. +{
  6219. + event->armed = 0;
  6220. + /* Don't clear the 'fired' flag because it may already have been set
  6221. + ** by the other side. */
  6222. + sema_init(event->event, 0);
  6223. +}
  6224. +
  6225. +static inline void
  6226. +remote_event_destroy(REMOTE_EVENT_T *event)
  6227. +{
  6228. + (void)event;
  6229. +}
  6230. +
  6231. +static inline int
  6232. +remote_event_wait(REMOTE_EVENT_T *event)
  6233. +{
  6234. + if (!event->fired) {
  6235. + event->armed = 1;
  6236. + dsb();
  6237. + if (!event->fired) {
  6238. + if (down_interruptible(event->event) != 0) {
  6239. + event->armed = 0;
  6240. + return 0;
  6241. + }
  6242. + }
  6243. + event->armed = 0;
  6244. + wmb();
  6245. + }
  6246. +
  6247. + event->fired = 0;
  6248. + return 1;
  6249. +}
  6250. +
  6251. +static inline void
  6252. +remote_event_signal_local(REMOTE_EVENT_T *event)
  6253. +{
  6254. + event->armed = 0;
  6255. + up(event->event);
  6256. +}
  6257. +
  6258. +static inline void
  6259. +remote_event_poll(REMOTE_EVENT_T *event)
  6260. +{
  6261. + if (event->fired && event->armed)
  6262. + remote_event_signal_local(event);
  6263. +}
  6264. +
  6265. +void
  6266. +remote_event_pollall(VCHIQ_STATE_T *state)
  6267. +{
  6268. + remote_event_poll(&state->local->sync_trigger);
  6269. + remote_event_poll(&state->local->sync_release);
  6270. + remote_event_poll(&state->local->trigger);
  6271. + remote_event_poll(&state->local->recycle);
  6272. +}
  6273. +
  6274. +/* Round up message sizes so that any space at the end of a slot is always big
  6275. +** enough for a header. This relies on header size being a power of two, which
  6276. +** has been verified earlier by a static assertion. */
  6277. +
  6278. +static inline unsigned int
  6279. +calc_stride(unsigned int size)
  6280. +{
  6281. + /* Allow room for the header */
  6282. + size += sizeof(VCHIQ_HEADER_T);
  6283. +
  6284. + /* Round up */
  6285. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  6286. + - 1);
  6287. +}
  6288. +
  6289. +/* Called by the slot handler thread */
  6290. +static VCHIQ_SERVICE_T *
  6291. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  6292. +{
  6293. + int i;
  6294. +
  6295. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  6296. +
  6297. + for (i = 0; i < state->unused_service; i++) {
  6298. + VCHIQ_SERVICE_T *service = state->services[i];
  6299. + if (service &&
  6300. + (service->public_fourcc == fourcc) &&
  6301. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  6302. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  6303. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  6304. + lock_service(service);
  6305. + return service;
  6306. + }
  6307. + }
  6308. +
  6309. + return NULL;
  6310. +}
  6311. +
  6312. +/* Called by the slot handler thread */
  6313. +static VCHIQ_SERVICE_T *
  6314. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  6315. +{
  6316. + int i;
  6317. + for (i = 0; i < state->unused_service; i++) {
  6318. + VCHIQ_SERVICE_T *service = state->services[i];
  6319. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  6320. + && (service->remoteport == port)) {
  6321. + lock_service(service);
  6322. + return service;
  6323. + }
  6324. + }
  6325. + return NULL;
  6326. +}
  6327. +
  6328. +inline void
  6329. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  6330. +{
  6331. + uint32_t value;
  6332. +
  6333. + if (service) {
  6334. + do {
  6335. + value = atomic_read(&service->poll_flags);
  6336. + } while (atomic_cmpxchg(&service->poll_flags, value,
  6337. + value | (1 << poll_type)) != value);
  6338. +
  6339. + do {
  6340. + value = atomic_read(&state->poll_services[
  6341. + service->localport>>5]);
  6342. + } while (atomic_cmpxchg(
  6343. + &state->poll_services[service->localport>>5],
  6344. + value, value | (1 << (service->localport & 0x1f)))
  6345. + != value);
  6346. + }
  6347. +
  6348. + state->poll_needed = 1;
  6349. + wmb();
  6350. +
  6351. + /* ... and ensure the slot handler runs. */
  6352. + remote_event_signal_local(&state->local->trigger);
  6353. +}
  6354. +
  6355. +/* Called from queue_message, by the slot handler and application threads,
  6356. +** with slot_mutex held */
  6357. +static VCHIQ_HEADER_T *
  6358. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  6359. +{
  6360. + VCHIQ_SHARED_STATE_T *local = state->local;
  6361. + int tx_pos = state->local_tx_pos;
  6362. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  6363. +
  6364. + if (space > slot_space) {
  6365. + VCHIQ_HEADER_T *header;
  6366. + /* Fill the remaining space with padding */
  6367. + WARN_ON(state->tx_data == NULL);
  6368. + header = (VCHIQ_HEADER_T *)
  6369. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  6370. + header->msgid = VCHIQ_MSGID_PADDING;
  6371. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  6372. +
  6373. + tx_pos += slot_space;
  6374. + }
  6375. +
  6376. + /* If necessary, get the next slot. */
  6377. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  6378. + int slot_index;
  6379. +
  6380. + /* If there is no free slot... */
  6381. +
  6382. + if (down_trylock(&state->slot_available_event) != 0) {
  6383. + /* ...wait for one. */
  6384. +
  6385. + VCHIQ_STATS_INC(state, slot_stalls);
  6386. +
  6387. + /* But first, flush through the last slot. */
  6388. + state->local_tx_pos = tx_pos;
  6389. + local->tx_pos = tx_pos;
  6390. + remote_event_signal(&state->remote->trigger);
  6391. +
  6392. + if (!is_blocking ||
  6393. + (down_interruptible(
  6394. + &state->slot_available_event) != 0))
  6395. + return NULL; /* No space available */
  6396. + }
  6397. +
  6398. + BUG_ON(tx_pos ==
  6399. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  6400. +
  6401. + slot_index = local->slot_queue[
  6402. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  6403. + VCHIQ_SLOT_QUEUE_MASK];
  6404. + state->tx_data =
  6405. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  6406. + }
  6407. +
  6408. + state->local_tx_pos = tx_pos + space;
  6409. +
  6410. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  6411. +}
  6412. +
  6413. +/* Called by the recycle thread. */
  6414. +static void
  6415. +process_free_queue(VCHIQ_STATE_T *state)
  6416. +{
  6417. + VCHIQ_SHARED_STATE_T *local = state->local;
  6418. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  6419. + int slot_queue_available;
  6420. +
  6421. + /* Use a read memory barrier to ensure that any state that may have
  6422. + ** been modified by another thread is not masked by stale prefetched
  6423. + ** values. */
  6424. + rmb();
  6425. +
  6426. + /* Find slots which have been freed by the other side, and return them
  6427. + ** to the available queue. */
  6428. + slot_queue_available = state->slot_queue_available;
  6429. +
  6430. + while (slot_queue_available != local->slot_queue_recycle) {
  6431. + unsigned int pos;
  6432. + int slot_index = local->slot_queue[slot_queue_available++ &
  6433. + VCHIQ_SLOT_QUEUE_MASK];
  6434. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  6435. + int data_found = 0;
  6436. +
  6437. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  6438. + state->id, slot_index, (unsigned int)data,
  6439. + local->slot_queue_recycle, slot_queue_available);
  6440. +
  6441. + /* Initialise the bitmask for services which have used this
  6442. + ** slot */
  6443. + BITSET_ZERO(service_found);
  6444. +
  6445. + pos = 0;
  6446. +
  6447. + while (pos < VCHIQ_SLOT_SIZE) {
  6448. + VCHIQ_HEADER_T *header =
  6449. + (VCHIQ_HEADER_T *)(data + pos);
  6450. + int msgid = header->msgid;
  6451. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  6452. + int port = VCHIQ_MSG_SRCPORT(msgid);
  6453. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  6454. + &state->service_quotas[port];
  6455. + int count;
  6456. + spin_lock(&quota_spinlock);
  6457. + count = service_quota->message_use_count;
  6458. + if (count > 0)
  6459. + service_quota->message_use_count =
  6460. + count - 1;
  6461. + spin_unlock(&quota_spinlock);
  6462. +
  6463. + if (count == service_quota->message_quota)
  6464. + /* Signal the service that it
  6465. + ** has dropped below its quota
  6466. + */
  6467. + up(&service_quota->quota_event);
  6468. + else if (count == 0) {
  6469. + vchiq_log_error(vchiq_core_log_level,
  6470. + "service %d "
  6471. + "message_use_count=%d "
  6472. + "(header %x, msgid %x, "
  6473. + "header->msgid %x, "
  6474. + "header->size %x)",
  6475. + port,
  6476. + service_quota->
  6477. + message_use_count,
  6478. + (unsigned int)header, msgid,
  6479. + header->msgid,
  6480. + header->size);
  6481. + WARN(1, "invalid message use count\n");
  6482. + }
  6483. + if (!BITSET_IS_SET(service_found, port)) {
  6484. + /* Set the found bit for this service */
  6485. + BITSET_SET(service_found, port);
  6486. +
  6487. + spin_lock(&quota_spinlock);
  6488. + count = service_quota->slot_use_count;
  6489. + if (count > 0)
  6490. + service_quota->slot_use_count =
  6491. + count - 1;
  6492. + spin_unlock(&quota_spinlock);
  6493. +
  6494. + if (count > 0) {
  6495. + /* Signal the service in case
  6496. + ** it has dropped below its
  6497. + ** quota */
  6498. + up(&service_quota->quota_event);
  6499. + vchiq_log_trace(
  6500. + vchiq_core_log_level,
  6501. + "%d: pfq:%d %x@%x - "
  6502. + "slot_use->%d",
  6503. + state->id, port,
  6504. + header->size,
  6505. + (unsigned int)header,
  6506. + count - 1);
  6507. + } else {
  6508. + vchiq_log_error(
  6509. + vchiq_core_log_level,
  6510. + "service %d "
  6511. + "slot_use_count"
  6512. + "=%d (header %x"
  6513. + ", msgid %x, "
  6514. + "header->msgid"
  6515. + " %x, header->"
  6516. + "size %x)",
  6517. + port, count,
  6518. + (unsigned int)header,
  6519. + msgid,
  6520. + header->msgid,
  6521. + header->size);
  6522. + WARN(1, "bad slot use count\n");
  6523. + }
  6524. + }
  6525. +
  6526. + data_found = 1;
  6527. + }
  6528. +
  6529. + pos += calc_stride(header->size);
  6530. + if (pos > VCHIQ_SLOT_SIZE) {
  6531. + vchiq_log_error(vchiq_core_log_level,
  6532. + "pfq - pos %x: header %x, msgid %x, "
  6533. + "header->msgid %x, header->size %x",
  6534. + pos, (unsigned int)header, msgid,
  6535. + header->msgid, header->size);
  6536. + WARN(1, "invalid slot position\n");
  6537. + }
  6538. + }
  6539. +
  6540. + if (data_found) {
  6541. + int count;
  6542. + spin_lock(&quota_spinlock);
  6543. + count = state->data_use_count;
  6544. + if (count > 0)
  6545. + state->data_use_count =
  6546. + count - 1;
  6547. + spin_unlock(&quota_spinlock);
  6548. + if (count == state->data_quota)
  6549. + up(&state->data_quota_event);
  6550. + }
  6551. +
  6552. + state->slot_queue_available = slot_queue_available;
  6553. + up(&state->slot_available_event);
  6554. + }
  6555. +}
  6556. +
  6557. +/* Called by the slot handler and application threads */
  6558. +static VCHIQ_STATUS_T
  6559. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  6560. + int msgid, const VCHIQ_ELEMENT_T *elements,
  6561. + int count, int size, int flags)
  6562. +{
  6563. + VCHIQ_SHARED_STATE_T *local;
  6564. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  6565. + VCHIQ_HEADER_T *header;
  6566. + int type = VCHIQ_MSG_TYPE(msgid);
  6567. +
  6568. + unsigned int stride;
  6569. +
  6570. + local = state->local;
  6571. +
  6572. + stride = calc_stride(size);
  6573. +
  6574. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  6575. +
  6576. + if (!(flags & QMFLAGS_NO_MUTEX_LOCK) &&
  6577. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  6578. + return VCHIQ_RETRY;
  6579. +
  6580. + if (type == VCHIQ_MSG_DATA) {
  6581. + int tx_end_index;
  6582. +
  6583. + BUG_ON(!service);
  6584. + BUG_ON((flags & (QMFLAGS_NO_MUTEX_LOCK |
  6585. + QMFLAGS_NO_MUTEX_UNLOCK)) != 0);
  6586. +
  6587. + if (service->closing) {
  6588. + /* The service has been closed */
  6589. + mutex_unlock(&state->slot_mutex);
  6590. + return VCHIQ_ERROR;
  6591. + }
  6592. +
  6593. + service_quota = &state->service_quotas[service->localport];
  6594. +
  6595. + spin_lock(&quota_spinlock);
  6596. +
  6597. + /* Ensure this service doesn't use more than its quota of
  6598. + ** messages or slots */
  6599. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6600. + state->local_tx_pos + stride - 1);
  6601. +
  6602. + /* Ensure data messages don't use more than their quota of
  6603. + ** slots */
  6604. + while ((tx_end_index != state->previous_data_index) &&
  6605. + (state->data_use_count == state->data_quota)) {
  6606. + VCHIQ_STATS_INC(state, data_stalls);
  6607. + spin_unlock(&quota_spinlock);
  6608. + mutex_unlock(&state->slot_mutex);
  6609. +
  6610. + if (down_interruptible(&state->data_quota_event)
  6611. + != 0)
  6612. + return VCHIQ_RETRY;
  6613. +
  6614. + mutex_lock(&state->slot_mutex);
  6615. + spin_lock(&quota_spinlock);
  6616. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6617. + state->local_tx_pos + stride - 1);
  6618. + if ((tx_end_index == state->previous_data_index) ||
  6619. + (state->data_use_count < state->data_quota)) {
  6620. + /* Pass the signal on to other waiters */
  6621. + up(&state->data_quota_event);
  6622. + break;
  6623. + }
  6624. + }
  6625. +
  6626. + while ((service_quota->message_use_count ==
  6627. + service_quota->message_quota) ||
  6628. + ((tx_end_index != service_quota->previous_tx_index) &&
  6629. + (service_quota->slot_use_count ==
  6630. + service_quota->slot_quota))) {
  6631. + spin_unlock(&quota_spinlock);
  6632. + vchiq_log_trace(vchiq_core_log_level,
  6633. + "%d: qm:%d %s,%x - quota stall "
  6634. + "(msg %d, slot %d)",
  6635. + state->id, service->localport,
  6636. + msg_type_str(type), size,
  6637. + service_quota->message_use_count,
  6638. + service_quota->slot_use_count);
  6639. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  6640. + mutex_unlock(&state->slot_mutex);
  6641. + if (down_interruptible(&service_quota->quota_event)
  6642. + != 0)
  6643. + return VCHIQ_RETRY;
  6644. + if (service->closing)
  6645. + return VCHIQ_ERROR;
  6646. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  6647. + return VCHIQ_RETRY;
  6648. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  6649. + /* The service has been closed */
  6650. + mutex_unlock(&state->slot_mutex);
  6651. + return VCHIQ_ERROR;
  6652. + }
  6653. + spin_lock(&quota_spinlock);
  6654. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6655. + state->local_tx_pos + stride - 1);
  6656. + }
  6657. +
  6658. + spin_unlock(&quota_spinlock);
  6659. + }
  6660. +
  6661. + header = reserve_space(state, stride, flags & QMFLAGS_IS_BLOCKING);
  6662. +
  6663. + if (!header) {
  6664. + if (service)
  6665. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  6666. + /* In the event of a failure, return the mutex to the
  6667. + state it was in */
  6668. + if (!(flags & QMFLAGS_NO_MUTEX_LOCK))
  6669. + mutex_unlock(&state->slot_mutex);
  6670. + return VCHIQ_RETRY;
  6671. + }
  6672. +
  6673. + if (type == VCHIQ_MSG_DATA) {
  6674. + int i, pos;
  6675. + int tx_end_index;
  6676. + int slot_use_count;
  6677. +
  6678. + vchiq_log_info(vchiq_core_log_level,
  6679. + "%d: qm %s@%x,%x (%d->%d)",
  6680. + state->id,
  6681. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6682. + (unsigned int)header, size,
  6683. + VCHIQ_MSG_SRCPORT(msgid),
  6684. + VCHIQ_MSG_DSTPORT(msgid));
  6685. +
  6686. + BUG_ON(!service);
  6687. + BUG_ON((flags & (QMFLAGS_NO_MUTEX_LOCK |
  6688. + QMFLAGS_NO_MUTEX_UNLOCK)) != 0);
  6689. +
  6690. + for (i = 0, pos = 0; i < (unsigned int)count;
  6691. + pos += elements[i++].size)
  6692. + if (elements[i].size) {
  6693. + if (vchiq_copy_from_user
  6694. + (header->data + pos, elements[i].data,
  6695. + (size_t) elements[i].size) !=
  6696. + VCHIQ_SUCCESS) {
  6697. + mutex_unlock(&state->slot_mutex);
  6698. + VCHIQ_SERVICE_STATS_INC(service,
  6699. + error_count);
  6700. + return VCHIQ_ERROR;
  6701. + }
  6702. + if (i == 0) {
  6703. + if (SRVTRACE_ENABLED(service,
  6704. + VCHIQ_LOG_INFO))
  6705. + vchiq_log_dump_mem("Sent", 0,
  6706. + header->data + pos,
  6707. + min(64u,
  6708. + elements[0].size));
  6709. + }
  6710. + }
  6711. +
  6712. + spin_lock(&quota_spinlock);
  6713. + service_quota->message_use_count++;
  6714. +
  6715. + tx_end_index =
  6716. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  6717. +
  6718. + /* If this transmission can't fit in the last slot used by any
  6719. + ** service, the data_use_count must be increased. */
  6720. + if (tx_end_index != state->previous_data_index) {
  6721. + state->previous_data_index = tx_end_index;
  6722. + state->data_use_count++;
  6723. + }
  6724. +
  6725. + /* If this isn't the same slot last used by this service,
  6726. + ** the service's slot_use_count must be increased. */
  6727. + if (tx_end_index != service_quota->previous_tx_index) {
  6728. + service_quota->previous_tx_index = tx_end_index;
  6729. + slot_use_count = ++service_quota->slot_use_count;
  6730. + } else {
  6731. + slot_use_count = 0;
  6732. + }
  6733. +
  6734. + spin_unlock(&quota_spinlock);
  6735. +
  6736. + if (slot_use_count)
  6737. + vchiq_log_trace(vchiq_core_log_level,
  6738. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  6739. + state->id, service->localport,
  6740. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  6741. + slot_use_count, header);
  6742. +
  6743. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  6744. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  6745. + } else {
  6746. + vchiq_log_info(vchiq_core_log_level,
  6747. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  6748. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6749. + (unsigned int)header, size,
  6750. + VCHIQ_MSG_SRCPORT(msgid),
  6751. + VCHIQ_MSG_DSTPORT(msgid));
  6752. + if (size != 0) {
  6753. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  6754. + memcpy(header->data, elements[0].data,
  6755. + elements[0].size);
  6756. + }
  6757. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  6758. + }
  6759. +
  6760. + header->msgid = msgid;
  6761. + header->size = size;
  6762. +
  6763. + {
  6764. + int svc_fourcc;
  6765. +
  6766. + svc_fourcc = service
  6767. + ? service->base.fourcc
  6768. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  6769. +
  6770. + vchiq_log_info(SRVTRACE_LEVEL(service),
  6771. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  6772. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6773. + VCHIQ_MSG_TYPE(msgid),
  6774. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  6775. + VCHIQ_MSG_SRCPORT(msgid),
  6776. + VCHIQ_MSG_DSTPORT(msgid),
  6777. + size);
  6778. + }
  6779. +
  6780. + /* Make sure the new header is visible to the peer. */
  6781. + wmb();
  6782. +
  6783. + /* Make the new tx_pos visible to the peer. */
  6784. + local->tx_pos = state->local_tx_pos;
  6785. + wmb();
  6786. +
  6787. + if (service && (type == VCHIQ_MSG_CLOSE))
  6788. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  6789. +
  6790. + if (!(flags & QMFLAGS_NO_MUTEX_UNLOCK))
  6791. + mutex_unlock(&state->slot_mutex);
  6792. +
  6793. + remote_event_signal(&state->remote->trigger);
  6794. +
  6795. + return VCHIQ_SUCCESS;
  6796. +}
  6797. +
  6798. +/* Called by the slot handler and application threads */
  6799. +static VCHIQ_STATUS_T
  6800. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  6801. + int msgid, const VCHIQ_ELEMENT_T *elements,
  6802. + int count, int size, int is_blocking)
  6803. +{
  6804. + VCHIQ_SHARED_STATE_T *local;
  6805. + VCHIQ_HEADER_T *header;
  6806. +
  6807. + local = state->local;
  6808. +
  6809. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  6810. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  6811. + return VCHIQ_RETRY;
  6812. +
  6813. + remote_event_wait(&local->sync_release);
  6814. +
  6815. + rmb();
  6816. +
  6817. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  6818. + local->slot_sync);
  6819. +
  6820. + {
  6821. + int oldmsgid = header->msgid;
  6822. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  6823. + vchiq_log_error(vchiq_core_log_level,
  6824. + "%d: qms - msgid %x, not PADDING",
  6825. + state->id, oldmsgid);
  6826. + }
  6827. +
  6828. + if (service) {
  6829. + int i, pos;
  6830. +
  6831. + vchiq_log_info(vchiq_sync_log_level,
  6832. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  6833. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6834. + (unsigned int)header, size,
  6835. + VCHIQ_MSG_SRCPORT(msgid),
  6836. + VCHIQ_MSG_DSTPORT(msgid));
  6837. +
  6838. + for (i = 0, pos = 0; i < (unsigned int)count;
  6839. + pos += elements[i++].size)
  6840. + if (elements[i].size) {
  6841. + if (vchiq_copy_from_user
  6842. + (header->data + pos, elements[i].data,
  6843. + (size_t) elements[i].size) !=
  6844. + VCHIQ_SUCCESS) {
  6845. + mutex_unlock(&state->sync_mutex);
  6846. + VCHIQ_SERVICE_STATS_INC(service,
  6847. + error_count);
  6848. + return VCHIQ_ERROR;
  6849. + }
  6850. + if (i == 0) {
  6851. + if (vchiq_sync_log_level >=
  6852. + VCHIQ_LOG_TRACE)
  6853. + vchiq_log_dump_mem("Sent Sync",
  6854. + 0, header->data + pos,
  6855. + min(64u,
  6856. + elements[0].size));
  6857. + }
  6858. + }
  6859. +
  6860. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  6861. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  6862. + } else {
  6863. + vchiq_log_info(vchiq_sync_log_level,
  6864. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  6865. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6866. + (unsigned int)header, size,
  6867. + VCHIQ_MSG_SRCPORT(msgid),
  6868. + VCHIQ_MSG_DSTPORT(msgid));
  6869. + if (size != 0) {
  6870. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  6871. + memcpy(header->data, elements[0].data,
  6872. + elements[0].size);
  6873. + }
  6874. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  6875. + }
  6876. +
  6877. + header->size = size;
  6878. + header->msgid = msgid;
  6879. +
  6880. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  6881. + int svc_fourcc;
  6882. +
  6883. + svc_fourcc = service
  6884. + ? service->base.fourcc
  6885. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  6886. +
  6887. + vchiq_log_trace(vchiq_sync_log_level,
  6888. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  6889. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6890. + VCHIQ_MSG_TYPE(msgid),
  6891. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  6892. + VCHIQ_MSG_SRCPORT(msgid),
  6893. + VCHIQ_MSG_DSTPORT(msgid),
  6894. + size);
  6895. + }
  6896. +
  6897. + /* Make sure the new header is visible to the peer. */
  6898. + wmb();
  6899. +
  6900. + remote_event_signal(&state->remote->sync_trigger);
  6901. +
  6902. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  6903. + mutex_unlock(&state->sync_mutex);
  6904. +
  6905. + return VCHIQ_SUCCESS;
  6906. +}
  6907. +
  6908. +static inline void
  6909. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  6910. +{
  6911. + slot->use_count++;
  6912. +}
  6913. +
  6914. +static void
  6915. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  6916. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  6917. +{
  6918. + int release_count;
  6919. +
  6920. + mutex_lock(&state->recycle_mutex);
  6921. +
  6922. + if (header) {
  6923. + int msgid = header->msgid;
  6924. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  6925. + (service && service->closing)) {
  6926. + mutex_unlock(&state->recycle_mutex);
  6927. + return;
  6928. + }
  6929. +
  6930. + /* Rewrite the message header to prevent a double
  6931. + ** release */
  6932. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  6933. + }
  6934. +
  6935. + release_count = slot_info->release_count;
  6936. + slot_info->release_count = ++release_count;
  6937. +
  6938. + if (release_count == slot_info->use_count) {
  6939. + int slot_queue_recycle;
  6940. + /* Add to the freed queue */
  6941. +
  6942. + /* A read barrier is necessary here to prevent speculative
  6943. + ** fetches of remote->slot_queue_recycle from overtaking the
  6944. + ** mutex. */
  6945. + rmb();
  6946. +
  6947. + slot_queue_recycle = state->remote->slot_queue_recycle;
  6948. + state->remote->slot_queue[slot_queue_recycle &
  6949. + VCHIQ_SLOT_QUEUE_MASK] =
  6950. + SLOT_INDEX_FROM_INFO(state, slot_info);
  6951. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  6952. + vchiq_log_info(vchiq_core_log_level,
  6953. + "%d: release_slot %d - recycle->%x",
  6954. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  6955. + state->remote->slot_queue_recycle);
  6956. +
  6957. + /* A write barrier is necessary, but remote_event_signal
  6958. + ** contains one. */
  6959. + remote_event_signal(&state->remote->recycle);
  6960. + }
  6961. +
  6962. + mutex_unlock(&state->recycle_mutex);
  6963. +}
  6964. +
  6965. +/* Called by the slot handler - don't hold the bulk mutex */
  6966. +static VCHIQ_STATUS_T
  6967. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  6968. + int retry_poll)
  6969. +{
  6970. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  6971. +
  6972. + vchiq_log_trace(vchiq_core_log_level,
  6973. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  6974. + service->state->id, service->localport,
  6975. + (queue == &service->bulk_tx) ? 't' : 'r',
  6976. + queue->process, queue->remote_notify, queue->remove);
  6977. +
  6978. + if (service->state->is_master) {
  6979. + while (queue->remote_notify != queue->process) {
  6980. + VCHIQ_BULK_T *bulk =
  6981. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  6982. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  6983. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  6984. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  6985. + service->remoteport);
  6986. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  6987. + /* Only reply to non-dummy bulk requests */
  6988. + if (bulk->remote_data) {
  6989. + status = queue_message(service->state, NULL,
  6990. + msgid, &element, 1, 4, 0);
  6991. + if (status != VCHIQ_SUCCESS)
  6992. + break;
  6993. + }
  6994. + queue->remote_notify++;
  6995. + }
  6996. + } else {
  6997. + queue->remote_notify = queue->process;
  6998. + }
  6999. +
  7000. + if (status == VCHIQ_SUCCESS) {
  7001. + while (queue->remove != queue->remote_notify) {
  7002. + VCHIQ_BULK_T *bulk =
  7003. + &queue->bulks[BULK_INDEX(queue->remove)];
  7004. +
  7005. + /* Only generate callbacks for non-dummy bulk
  7006. + ** requests, and non-terminated services */
  7007. + if (bulk->data && service->instance) {
  7008. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  7009. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  7010. + VCHIQ_SERVICE_STATS_INC(service,
  7011. + bulk_tx_count);
  7012. + VCHIQ_SERVICE_STATS_ADD(service,
  7013. + bulk_tx_bytes,
  7014. + bulk->actual);
  7015. + } else {
  7016. + VCHIQ_SERVICE_STATS_INC(service,
  7017. + bulk_rx_count);
  7018. + VCHIQ_SERVICE_STATS_ADD(service,
  7019. + bulk_rx_bytes,
  7020. + bulk->actual);
  7021. + }
  7022. + } else {
  7023. + VCHIQ_SERVICE_STATS_INC(service,
  7024. + bulk_aborted_count);
  7025. + }
  7026. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  7027. + struct bulk_waiter *waiter;
  7028. + spin_lock(&bulk_waiter_spinlock);
  7029. + waiter = bulk->userdata;
  7030. + if (waiter) {
  7031. + waiter->actual = bulk->actual;
  7032. + up(&waiter->event);
  7033. + }
  7034. + spin_unlock(&bulk_waiter_spinlock);
  7035. + } else if (bulk->mode ==
  7036. + VCHIQ_BULK_MODE_CALLBACK) {
  7037. + VCHIQ_REASON_T reason = (bulk->dir ==
  7038. + VCHIQ_BULK_TRANSMIT) ?
  7039. + ((bulk->actual ==
  7040. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  7041. + VCHIQ_BULK_TRANSMIT_ABORTED :
  7042. + VCHIQ_BULK_TRANSMIT_DONE) :
  7043. + ((bulk->actual ==
  7044. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  7045. + VCHIQ_BULK_RECEIVE_ABORTED :
  7046. + VCHIQ_BULK_RECEIVE_DONE);
  7047. + status = make_service_callback(service,
  7048. + reason, NULL, bulk->userdata);
  7049. + if (status == VCHIQ_RETRY)
  7050. + break;
  7051. + }
  7052. + }
  7053. +
  7054. + queue->remove++;
  7055. + up(&service->bulk_remove_event);
  7056. + }
  7057. + if (!retry_poll)
  7058. + status = VCHIQ_SUCCESS;
  7059. + }
  7060. +
  7061. + if (status == VCHIQ_RETRY)
  7062. + request_poll(service->state, service,
  7063. + (queue == &service->bulk_tx) ?
  7064. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  7065. +
  7066. + return status;
  7067. +}
  7068. +
  7069. +/* Called by the slot handler thread */
  7070. +static void
  7071. +poll_services(VCHIQ_STATE_T *state)
  7072. +{
  7073. + int group, i;
  7074. +
  7075. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  7076. + uint32_t flags;
  7077. + flags = atomic_xchg(&state->poll_services[group], 0);
  7078. + for (i = 0; flags; i++) {
  7079. + if (flags & (1 << i)) {
  7080. + VCHIQ_SERVICE_T *service =
  7081. + find_service_by_port(state,
  7082. + (group<<5) + i);
  7083. + uint32_t service_flags;
  7084. + flags &= ~(1 << i);
  7085. + if (!service)
  7086. + continue;
  7087. + service_flags =
  7088. + atomic_xchg(&service->poll_flags, 0);
  7089. + if (service_flags &
  7090. + (1 << VCHIQ_POLL_REMOVE)) {
  7091. + vchiq_log_info(vchiq_core_log_level,
  7092. + "%d: ps - remove %d<->%d",
  7093. + state->id, service->localport,
  7094. + service->remoteport);
  7095. +
  7096. + /* Make it look like a client, because
  7097. + it must be removed and not left in
  7098. + the LISTENING state. */
  7099. + service->public_fourcc =
  7100. + VCHIQ_FOURCC_INVALID;
  7101. +
  7102. + if (vchiq_close_service_internal(
  7103. + service, 0/*!close_recvd*/) !=
  7104. + VCHIQ_SUCCESS)
  7105. + request_poll(state, service,
  7106. + VCHIQ_POLL_REMOVE);
  7107. + } else if (service_flags &
  7108. + (1 << VCHIQ_POLL_TERMINATE)) {
  7109. + vchiq_log_info(vchiq_core_log_level,
  7110. + "%d: ps - terminate %d<->%d",
  7111. + state->id, service->localport,
  7112. + service->remoteport);
  7113. + if (vchiq_close_service_internal(
  7114. + service, 0/*!close_recvd*/) !=
  7115. + VCHIQ_SUCCESS)
  7116. + request_poll(state, service,
  7117. + VCHIQ_POLL_TERMINATE);
  7118. + }
  7119. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  7120. + notify_bulks(service,
  7121. + &service->bulk_tx,
  7122. + 1/*retry_poll*/);
  7123. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  7124. + notify_bulks(service,
  7125. + &service->bulk_rx,
  7126. + 1/*retry_poll*/);
  7127. + unlock_service(service);
  7128. + }
  7129. + }
  7130. + }
  7131. +}
  7132. +
  7133. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  7134. +static int
  7135. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  7136. +{
  7137. + VCHIQ_STATE_T *state = service->state;
  7138. + int resolved = 0;
  7139. + int rc;
  7140. +
  7141. + while ((queue->process != queue->local_insert) &&
  7142. + (queue->process != queue->remote_insert)) {
  7143. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  7144. +
  7145. + vchiq_log_trace(vchiq_core_log_level,
  7146. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  7147. + state->id, service->localport,
  7148. + (queue == &service->bulk_tx) ? 't' : 'r',
  7149. + queue->local_insert, queue->remote_insert,
  7150. + queue->process);
  7151. +
  7152. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  7153. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  7154. +
  7155. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  7156. + if (rc != 0)
  7157. + break;
  7158. +
  7159. + vchiq_transfer_bulk(bulk);
  7160. + mutex_unlock(&state->bulk_transfer_mutex);
  7161. +
  7162. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  7163. + const char *header = (queue == &service->bulk_tx) ?
  7164. + "Send Bulk to" : "Recv Bulk from";
  7165. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  7166. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7167. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  7168. + header,
  7169. + VCHIQ_FOURCC_AS_4CHARS(
  7170. + service->base.fourcc),
  7171. + service->remoteport,
  7172. + bulk->size,
  7173. + (unsigned int)bulk->data,
  7174. + (unsigned int)bulk->remote_data);
  7175. + else
  7176. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7177. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  7178. + " rx len:%d %x<->%x",
  7179. + header,
  7180. + VCHIQ_FOURCC_AS_4CHARS(
  7181. + service->base.fourcc),
  7182. + service->remoteport,
  7183. + bulk->size,
  7184. + bulk->remote_size,
  7185. + (unsigned int)bulk->data,
  7186. + (unsigned int)bulk->remote_data);
  7187. + }
  7188. +
  7189. + vchiq_complete_bulk(bulk);
  7190. + queue->process++;
  7191. + resolved++;
  7192. + }
  7193. + return resolved;
  7194. +}
  7195. +
  7196. +/* Called with the bulk_mutex held */
  7197. +static void
  7198. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  7199. +{
  7200. + int is_tx = (queue == &service->bulk_tx);
  7201. + vchiq_log_trace(vchiq_core_log_level,
  7202. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  7203. + service->state->id, service->localport, is_tx ? 't' : 'r',
  7204. + queue->local_insert, queue->remote_insert, queue->process);
  7205. +
  7206. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  7207. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  7208. +
  7209. + while ((queue->process != queue->local_insert) ||
  7210. + (queue->process != queue->remote_insert)) {
  7211. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  7212. +
  7213. + if (queue->process == queue->remote_insert) {
  7214. + /* fabricate a matching dummy bulk */
  7215. + bulk->remote_data = NULL;
  7216. + bulk->remote_size = 0;
  7217. + queue->remote_insert++;
  7218. + }
  7219. +
  7220. + if (queue->process != queue->local_insert) {
  7221. + vchiq_complete_bulk(bulk);
  7222. +
  7223. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7224. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  7225. + "rx len:%d",
  7226. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  7227. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  7228. + service->remoteport,
  7229. + bulk->size,
  7230. + bulk->remote_size);
  7231. + } else {
  7232. + /* fabricate a matching dummy bulk */
  7233. + bulk->data = NULL;
  7234. + bulk->size = 0;
  7235. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  7236. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  7237. + VCHIQ_BULK_RECEIVE;
  7238. + queue->local_insert++;
  7239. + }
  7240. +
  7241. + queue->process++;
  7242. + }
  7243. +}
  7244. +
  7245. +/* Called from the slot handler thread */
  7246. +static void
  7247. +pause_bulks(VCHIQ_STATE_T *state)
  7248. +{
  7249. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  7250. + WARN_ON_ONCE(1);
  7251. + atomic_set(&pause_bulks_count, 1);
  7252. + return;
  7253. + }
  7254. +
  7255. + /* Block bulk transfers from all services */
  7256. + mutex_lock(&state->bulk_transfer_mutex);
  7257. +}
  7258. +
  7259. +/* Called from the slot handler thread */
  7260. +static void
  7261. +resume_bulks(VCHIQ_STATE_T *state)
  7262. +{
  7263. + int i;
  7264. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  7265. + WARN_ON_ONCE(1);
  7266. + atomic_set(&pause_bulks_count, 0);
  7267. + return;
  7268. + }
  7269. +
  7270. + /* Allow bulk transfers from all services */
  7271. + mutex_unlock(&state->bulk_transfer_mutex);
  7272. +
  7273. + if (state->deferred_bulks == 0)
  7274. + return;
  7275. +
  7276. + /* Deal with any bulks which had to be deferred due to being in
  7277. + * paused state. Don't try to match up to number of deferred bulks
  7278. + * in case we've had something come and close the service in the
  7279. + * interim - just process all bulk queues for all services */
  7280. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  7281. + __func__, state->deferred_bulks);
  7282. +
  7283. + for (i = 0; i < state->unused_service; i++) {
  7284. + VCHIQ_SERVICE_T *service = state->services[i];
  7285. + int resolved_rx = 0;
  7286. + int resolved_tx = 0;
  7287. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  7288. + continue;
  7289. +
  7290. + mutex_lock(&service->bulk_mutex);
  7291. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  7292. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  7293. + mutex_unlock(&service->bulk_mutex);
  7294. + if (resolved_rx)
  7295. + notify_bulks(service, &service->bulk_rx, 1);
  7296. + if (resolved_tx)
  7297. + notify_bulks(service, &service->bulk_tx, 1);
  7298. + }
  7299. + state->deferred_bulks = 0;
  7300. +}
  7301. +
  7302. +static int
  7303. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  7304. +{
  7305. + VCHIQ_SERVICE_T *service = NULL;
  7306. + int msgid, size;
  7307. + int type;
  7308. + unsigned int localport, remoteport;
  7309. +
  7310. + msgid = header->msgid;
  7311. + size = header->size;
  7312. + type = VCHIQ_MSG_TYPE(msgid);
  7313. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7314. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7315. + if (size >= sizeof(struct vchiq_open_payload)) {
  7316. + const struct vchiq_open_payload *payload =
  7317. + (struct vchiq_open_payload *)header->data;
  7318. + unsigned int fourcc;
  7319. +
  7320. + fourcc = payload->fourcc;
  7321. + vchiq_log_info(vchiq_core_log_level,
  7322. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  7323. + state->id, (unsigned int)header,
  7324. + localport,
  7325. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  7326. +
  7327. + service = get_listening_service(state, fourcc);
  7328. +
  7329. + if (service) {
  7330. + /* A matching service exists */
  7331. + short version = payload->version;
  7332. + short version_min = payload->version_min;
  7333. + if ((service->version < version_min) ||
  7334. + (version < service->version_min)) {
  7335. + /* Version mismatch */
  7336. + vchiq_loud_error_header();
  7337. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  7338. + "version mismatch - local (%d, min %d)"
  7339. + " vs. remote (%d, min %d)",
  7340. + state->id, service->localport,
  7341. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  7342. + service->version, service->version_min,
  7343. + version, version_min);
  7344. + vchiq_loud_error_footer();
  7345. + unlock_service(service);
  7346. + service = NULL;
  7347. + goto fail_open;
  7348. + }
  7349. + service->peer_version = version;
  7350. +
  7351. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  7352. + struct vchiq_openack_payload ack_payload = {
  7353. + service->version
  7354. + };
  7355. + VCHIQ_ELEMENT_T body = {
  7356. + &ack_payload,
  7357. + sizeof(ack_payload)
  7358. + };
  7359. +
  7360. + if (state->version_common <
  7361. + VCHIQ_VERSION_SYNCHRONOUS_MODE)
  7362. + service->sync = 0;
  7363. +
  7364. + /* Acknowledge the OPEN */
  7365. + if (service->sync &&
  7366. + (state->version_common >=
  7367. + VCHIQ_VERSION_SYNCHRONOUS_MODE)) {
  7368. + if (queue_message_sync(state, NULL,
  7369. + VCHIQ_MAKE_MSG(
  7370. + VCHIQ_MSG_OPENACK,
  7371. + service->localport,
  7372. + remoteport),
  7373. + &body, 1, sizeof(ack_payload),
  7374. + 0) == VCHIQ_RETRY)
  7375. + goto bail_not_ready;
  7376. + } else {
  7377. + if (queue_message(state, NULL,
  7378. + VCHIQ_MAKE_MSG(
  7379. + VCHIQ_MSG_OPENACK,
  7380. + service->localport,
  7381. + remoteport),
  7382. + &body, 1, sizeof(ack_payload),
  7383. + 0) == VCHIQ_RETRY)
  7384. + goto bail_not_ready;
  7385. + }
  7386. +
  7387. + /* The service is now open */
  7388. + vchiq_set_service_state(service,
  7389. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  7390. + : VCHIQ_SRVSTATE_OPEN);
  7391. + }
  7392. +
  7393. + service->remoteport = remoteport;
  7394. + service->client_id = ((int *)header->data)[1];
  7395. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  7396. + NULL, NULL) == VCHIQ_RETRY) {
  7397. + /* Bail out if not ready */
  7398. + service->remoteport = VCHIQ_PORT_FREE;
  7399. + goto bail_not_ready;
  7400. + }
  7401. +
  7402. + /* Success - the message has been dealt with */
  7403. + unlock_service(service);
  7404. + return 1;
  7405. + }
  7406. + }
  7407. +
  7408. +fail_open:
  7409. + /* No available service, or an invalid request - send a CLOSE */
  7410. + if (queue_message(state, NULL,
  7411. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  7412. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  7413. + goto bail_not_ready;
  7414. +
  7415. + return 1;
  7416. +
  7417. +bail_not_ready:
  7418. + if (service)
  7419. + unlock_service(service);
  7420. +
  7421. + return 0;
  7422. +}
  7423. +
  7424. +/* Called by the slot handler thread */
  7425. +static void
  7426. +parse_rx_slots(VCHIQ_STATE_T *state)
  7427. +{
  7428. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  7429. + VCHIQ_SERVICE_T *service = NULL;
  7430. + int tx_pos;
  7431. + DEBUG_INITIALISE(state->local)
  7432. +
  7433. + tx_pos = remote->tx_pos;
  7434. +
  7435. + while (state->rx_pos != tx_pos) {
  7436. + VCHIQ_HEADER_T *header;
  7437. + int msgid, size;
  7438. + int type;
  7439. + unsigned int localport, remoteport;
  7440. +
  7441. + DEBUG_TRACE(PARSE_LINE);
  7442. + if (!state->rx_data) {
  7443. + int rx_index;
  7444. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  7445. + rx_index = remote->slot_queue[
  7446. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  7447. + VCHIQ_SLOT_QUEUE_MASK];
  7448. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  7449. + rx_index);
  7450. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  7451. +
  7452. + /* Initialise use_count to one, and increment
  7453. + ** release_count at the end of the slot to avoid
  7454. + ** releasing the slot prematurely. */
  7455. + state->rx_info->use_count = 1;
  7456. + state->rx_info->release_count = 0;
  7457. + }
  7458. +
  7459. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  7460. + (state->rx_pos & VCHIQ_SLOT_MASK));
  7461. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  7462. + msgid = header->msgid;
  7463. + DEBUG_VALUE(PARSE_MSGID, msgid);
  7464. + size = header->size;
  7465. + type = VCHIQ_MSG_TYPE(msgid);
  7466. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7467. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7468. +
  7469. + if (type != VCHIQ_MSG_DATA)
  7470. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  7471. +
  7472. + switch (type) {
  7473. + case VCHIQ_MSG_OPENACK:
  7474. + case VCHIQ_MSG_CLOSE:
  7475. + case VCHIQ_MSG_DATA:
  7476. + case VCHIQ_MSG_BULK_RX:
  7477. + case VCHIQ_MSG_BULK_TX:
  7478. + case VCHIQ_MSG_BULK_RX_DONE:
  7479. + case VCHIQ_MSG_BULK_TX_DONE:
  7480. + service = find_service_by_port(state, localport);
  7481. + if ((!service ||
  7482. + ((service->remoteport != remoteport) &&
  7483. + (service->remoteport != VCHIQ_PORT_FREE))) &&
  7484. + (localport == 0) &&
  7485. + (type == VCHIQ_MSG_CLOSE)) {
  7486. + /* This could be a CLOSE from a client which
  7487. + hadn't yet received the OPENACK - look for
  7488. + the connected service */
  7489. + if (service)
  7490. + unlock_service(service);
  7491. + service = get_connected_service(state,
  7492. + remoteport);
  7493. + if (service)
  7494. + vchiq_log_warning(vchiq_core_log_level,
  7495. + "%d: prs %s@%x (%d->%d) - "
  7496. + "found connected service %d",
  7497. + state->id, msg_type_str(type),
  7498. + (unsigned int)header,
  7499. + remoteport, localport,
  7500. + service->localport);
  7501. + }
  7502. +
  7503. + if (!service) {
  7504. + vchiq_log_error(vchiq_core_log_level,
  7505. + "%d: prs %s@%x (%d->%d) - "
  7506. + "invalid/closed service %d",
  7507. + state->id, msg_type_str(type),
  7508. + (unsigned int)header,
  7509. + remoteport, localport, localport);
  7510. + goto skip_message;
  7511. + }
  7512. + break;
  7513. + default:
  7514. + break;
  7515. + }
  7516. +
  7517. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  7518. + int svc_fourcc;
  7519. +
  7520. + svc_fourcc = service
  7521. + ? service->base.fourcc
  7522. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  7523. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7524. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  7525. + "len:%d",
  7526. + msg_type_str(type), type,
  7527. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  7528. + remoteport, localport, size);
  7529. + if (size > 0)
  7530. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  7531. + min(64, size));
  7532. + }
  7533. +
  7534. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  7535. + > VCHIQ_SLOT_SIZE) {
  7536. + vchiq_log_error(vchiq_core_log_level,
  7537. + "header %x (msgid %x) - size %x too big for "
  7538. + "slot",
  7539. + (unsigned int)header, (unsigned int)msgid,
  7540. + (unsigned int)size);
  7541. + WARN(1, "oversized for slot\n");
  7542. + }
  7543. +
  7544. + switch (type) {
  7545. + case VCHIQ_MSG_OPEN:
  7546. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  7547. + if (!parse_open(state, header))
  7548. + goto bail_not_ready;
  7549. + break;
  7550. + case VCHIQ_MSG_OPENACK:
  7551. + if (size >= sizeof(struct vchiq_openack_payload)) {
  7552. + const struct vchiq_openack_payload *payload =
  7553. + (struct vchiq_openack_payload *)
  7554. + header->data;
  7555. + service->peer_version = payload->version;
  7556. + }
  7557. + vchiq_log_info(vchiq_core_log_level,
  7558. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  7559. + state->id, (unsigned int)header, size,
  7560. + remoteport, localport, service->peer_version);
  7561. + if (service->srvstate ==
  7562. + VCHIQ_SRVSTATE_OPENING) {
  7563. + service->remoteport = remoteport;
  7564. + vchiq_set_service_state(service,
  7565. + VCHIQ_SRVSTATE_OPEN);
  7566. + up(&service->remove_event);
  7567. + } else
  7568. + vchiq_log_error(vchiq_core_log_level,
  7569. + "OPENACK received in state %s",
  7570. + srvstate_names[service->srvstate]);
  7571. + break;
  7572. + case VCHIQ_MSG_CLOSE:
  7573. + WARN_ON(size != 0); /* There should be no data */
  7574. +
  7575. + vchiq_log_info(vchiq_core_log_level,
  7576. + "%d: prs CLOSE@%x (%d->%d)",
  7577. + state->id, (unsigned int)header,
  7578. + remoteport, localport);
  7579. +
  7580. + mark_service_closing_internal(service, 1);
  7581. +
  7582. + if (vchiq_close_service_internal(service,
  7583. + 1/*close_recvd*/) == VCHIQ_RETRY)
  7584. + goto bail_not_ready;
  7585. +
  7586. + vchiq_log_info(vchiq_core_log_level,
  7587. + "Close Service %c%c%c%c s:%u d:%d",
  7588. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  7589. + service->localport,
  7590. + service->remoteport);
  7591. + break;
  7592. + case VCHIQ_MSG_DATA:
  7593. + vchiq_log_info(vchiq_core_log_level,
  7594. + "%d: prs DATA@%x,%x (%d->%d)",
  7595. + state->id, (unsigned int)header, size,
  7596. + remoteport, localport);
  7597. +
  7598. + if ((service->remoteport == remoteport)
  7599. + && (service->srvstate ==
  7600. + VCHIQ_SRVSTATE_OPEN)) {
  7601. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  7602. + claim_slot(state->rx_info);
  7603. + DEBUG_TRACE(PARSE_LINE);
  7604. + if (make_service_callback(service,
  7605. + VCHIQ_MESSAGE_AVAILABLE, header,
  7606. + NULL) == VCHIQ_RETRY) {
  7607. + DEBUG_TRACE(PARSE_LINE);
  7608. + goto bail_not_ready;
  7609. + }
  7610. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  7611. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  7612. + size);
  7613. + } else {
  7614. + VCHIQ_STATS_INC(state, error_count);
  7615. + }
  7616. + break;
  7617. + case VCHIQ_MSG_CONNECT:
  7618. + vchiq_log_info(vchiq_core_log_level,
  7619. + "%d: prs CONNECT@%x",
  7620. + state->id, (unsigned int)header);
  7621. + state->version_common = ((VCHIQ_SLOT_ZERO_T *)
  7622. + state->slot_data)->version;
  7623. + up(&state->connect);
  7624. + break;
  7625. + case VCHIQ_MSG_BULK_RX:
  7626. + case VCHIQ_MSG_BULK_TX: {
  7627. + VCHIQ_BULK_QUEUE_T *queue;
  7628. + WARN_ON(!state->is_master);
  7629. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  7630. + &service->bulk_tx : &service->bulk_rx;
  7631. + if ((service->remoteport == remoteport)
  7632. + && (service->srvstate ==
  7633. + VCHIQ_SRVSTATE_OPEN)) {
  7634. + VCHIQ_BULK_T *bulk;
  7635. + int resolved = 0;
  7636. +
  7637. + DEBUG_TRACE(PARSE_LINE);
  7638. + if (mutex_lock_interruptible(
  7639. + &service->bulk_mutex) != 0) {
  7640. + DEBUG_TRACE(PARSE_LINE);
  7641. + goto bail_not_ready;
  7642. + }
  7643. +
  7644. + WARN_ON(!(queue->remote_insert < queue->remove +
  7645. + VCHIQ_NUM_SERVICE_BULKS));
  7646. + bulk = &queue->bulks[
  7647. + BULK_INDEX(queue->remote_insert)];
  7648. + bulk->remote_data =
  7649. + (void *)((int *)header->data)[0];
  7650. + bulk->remote_size = ((int *)header->data)[1];
  7651. + wmb();
  7652. +
  7653. + vchiq_log_info(vchiq_core_log_level,
  7654. + "%d: prs %s@%x (%d->%d) %x@%x",
  7655. + state->id, msg_type_str(type),
  7656. + (unsigned int)header,
  7657. + remoteport, localport,
  7658. + bulk->remote_size,
  7659. + (unsigned int)bulk->remote_data);
  7660. +
  7661. + queue->remote_insert++;
  7662. +
  7663. + if (atomic_read(&pause_bulks_count)) {
  7664. + state->deferred_bulks++;
  7665. + vchiq_log_info(vchiq_core_log_level,
  7666. + "%s: deferring bulk (%d)",
  7667. + __func__,
  7668. + state->deferred_bulks);
  7669. + if (state->conn_state !=
  7670. + VCHIQ_CONNSTATE_PAUSE_SENT)
  7671. + vchiq_log_error(
  7672. + vchiq_core_log_level,
  7673. + "%s: bulks paused in "
  7674. + "unexpected state %s",
  7675. + __func__,
  7676. + conn_state_names[
  7677. + state->conn_state]);
  7678. + } else if (state->conn_state ==
  7679. + VCHIQ_CONNSTATE_CONNECTED) {
  7680. + DEBUG_TRACE(PARSE_LINE);
  7681. + resolved = resolve_bulks(service,
  7682. + queue);
  7683. + }
  7684. +
  7685. + mutex_unlock(&service->bulk_mutex);
  7686. + if (resolved)
  7687. + notify_bulks(service, queue,
  7688. + 1/*retry_poll*/);
  7689. + }
  7690. + } break;
  7691. + case VCHIQ_MSG_BULK_RX_DONE:
  7692. + case VCHIQ_MSG_BULK_TX_DONE:
  7693. + WARN_ON(state->is_master);
  7694. + if ((service->remoteport == remoteport)
  7695. + && (service->srvstate !=
  7696. + VCHIQ_SRVSTATE_FREE)) {
  7697. + VCHIQ_BULK_QUEUE_T *queue;
  7698. + VCHIQ_BULK_T *bulk;
  7699. +
  7700. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  7701. + &service->bulk_rx : &service->bulk_tx;
  7702. +
  7703. + DEBUG_TRACE(PARSE_LINE);
  7704. + if (mutex_lock_interruptible(
  7705. + &service->bulk_mutex) != 0) {
  7706. + DEBUG_TRACE(PARSE_LINE);
  7707. + goto bail_not_ready;
  7708. + }
  7709. + if ((int)(queue->remote_insert -
  7710. + queue->local_insert) >= 0) {
  7711. + vchiq_log_error(vchiq_core_log_level,
  7712. + "%d: prs %s@%x (%d->%d) "
  7713. + "unexpected (ri=%d,li=%d)",
  7714. + state->id, msg_type_str(type),
  7715. + (unsigned int)header,
  7716. + remoteport, localport,
  7717. + queue->remote_insert,
  7718. + queue->local_insert);
  7719. + mutex_unlock(&service->bulk_mutex);
  7720. + break;
  7721. + }
  7722. +
  7723. + BUG_ON(queue->process == queue->local_insert);
  7724. + BUG_ON(queue->process != queue->remote_insert);
  7725. +
  7726. + bulk = &queue->bulks[
  7727. + BULK_INDEX(queue->remote_insert)];
  7728. + bulk->actual = *(int *)header->data;
  7729. + queue->remote_insert++;
  7730. +
  7731. + vchiq_log_info(vchiq_core_log_level,
  7732. + "%d: prs %s@%x (%d->%d) %x@%x",
  7733. + state->id, msg_type_str(type),
  7734. + (unsigned int)header,
  7735. + remoteport, localport,
  7736. + bulk->actual, (unsigned int)bulk->data);
  7737. +
  7738. + vchiq_log_trace(vchiq_core_log_level,
  7739. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  7740. + state->id, localport,
  7741. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  7742. + 'r' : 't',
  7743. + queue->local_insert,
  7744. + queue->remote_insert, queue->process);
  7745. +
  7746. + DEBUG_TRACE(PARSE_LINE);
  7747. + WARN_ON(queue->process == queue->local_insert);
  7748. + vchiq_complete_bulk(bulk);
  7749. + queue->process++;
  7750. + mutex_unlock(&service->bulk_mutex);
  7751. + DEBUG_TRACE(PARSE_LINE);
  7752. + notify_bulks(service, queue, 1/*retry_poll*/);
  7753. + DEBUG_TRACE(PARSE_LINE);
  7754. + }
  7755. + break;
  7756. + case VCHIQ_MSG_PADDING:
  7757. + vchiq_log_trace(vchiq_core_log_level,
  7758. + "%d: prs PADDING@%x,%x",
  7759. + state->id, (unsigned int)header, size);
  7760. + break;
  7761. + case VCHIQ_MSG_PAUSE:
  7762. + /* If initiated, signal the application thread */
  7763. + vchiq_log_trace(vchiq_core_log_level,
  7764. + "%d: prs PAUSE@%x,%x",
  7765. + state->id, (unsigned int)header, size);
  7766. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  7767. + vchiq_log_error(vchiq_core_log_level,
  7768. + "%d: PAUSE received in state PAUSED",
  7769. + state->id);
  7770. + break;
  7771. + }
  7772. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  7773. + /* Send a PAUSE in response */
  7774. + if (queue_message(state, NULL,
  7775. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  7776. + NULL, 0, 0, QMFLAGS_NO_MUTEX_UNLOCK)
  7777. + == VCHIQ_RETRY)
  7778. + goto bail_not_ready;
  7779. + if (state->is_master)
  7780. + pause_bulks(state);
  7781. + }
  7782. + /* At this point slot_mutex is held */
  7783. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  7784. + vchiq_platform_paused(state);
  7785. + break;
  7786. + case VCHIQ_MSG_RESUME:
  7787. + vchiq_log_trace(vchiq_core_log_level,
  7788. + "%d: prs RESUME@%x,%x",
  7789. + state->id, (unsigned int)header, size);
  7790. + /* Release the slot mutex */
  7791. + mutex_unlock(&state->slot_mutex);
  7792. + if (state->is_master)
  7793. + resume_bulks(state);
  7794. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  7795. + vchiq_platform_resumed(state);
  7796. + break;
  7797. +
  7798. + case VCHIQ_MSG_REMOTE_USE:
  7799. + vchiq_on_remote_use(state);
  7800. + break;
  7801. + case VCHIQ_MSG_REMOTE_RELEASE:
  7802. + vchiq_on_remote_release(state);
  7803. + break;
  7804. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  7805. + vchiq_on_remote_use_active(state);
  7806. + break;
  7807. +
  7808. + default:
  7809. + vchiq_log_error(vchiq_core_log_level,
  7810. + "%d: prs invalid msgid %x@%x,%x",
  7811. + state->id, msgid, (unsigned int)header, size);
  7812. + WARN(1, "invalid message\n");
  7813. + break;
  7814. + }
  7815. +
  7816. +skip_message:
  7817. + if (service) {
  7818. + unlock_service(service);
  7819. + service = NULL;
  7820. + }
  7821. +
  7822. + state->rx_pos += calc_stride(size);
  7823. +
  7824. + DEBUG_TRACE(PARSE_LINE);
  7825. + /* Perform some housekeeping when the end of the slot is
  7826. + ** reached. */
  7827. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  7828. + /* Remove the extra reference count. */
  7829. + release_slot(state, state->rx_info, NULL, NULL);
  7830. + state->rx_data = NULL;
  7831. + }
  7832. + }
  7833. +
  7834. +bail_not_ready:
  7835. + if (service)
  7836. + unlock_service(service);
  7837. +}
  7838. +
  7839. +/* Called by the slot handler thread */
  7840. +static int
  7841. +slot_handler_func(void *v)
  7842. +{
  7843. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7844. + VCHIQ_SHARED_STATE_T *local = state->local;
  7845. + DEBUG_INITIALISE(local)
  7846. +
  7847. + while (1) {
  7848. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  7849. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7850. + remote_event_wait(&local->trigger);
  7851. +
  7852. + rmb();
  7853. +
  7854. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7855. + if (state->poll_needed) {
  7856. + /* Check if we need to suspend - may change our
  7857. + * conn_state */
  7858. + vchiq_platform_check_suspend(state);
  7859. +
  7860. + state->poll_needed = 0;
  7861. +
  7862. + /* Handle service polling and other rare conditions here
  7863. + ** out of the mainline code */
  7864. + switch (state->conn_state) {
  7865. + case VCHIQ_CONNSTATE_CONNECTED:
  7866. + /* Poll the services as requested */
  7867. + poll_services(state);
  7868. + break;
  7869. +
  7870. + case VCHIQ_CONNSTATE_PAUSING:
  7871. + if (state->is_master)
  7872. + pause_bulks(state);
  7873. + if (queue_message(state, NULL,
  7874. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  7875. + NULL, 0, 0,
  7876. + QMFLAGS_NO_MUTEX_UNLOCK)
  7877. + != VCHIQ_RETRY) {
  7878. + vchiq_set_conn_state(state,
  7879. + VCHIQ_CONNSTATE_PAUSE_SENT);
  7880. + } else {
  7881. + if (state->is_master)
  7882. + resume_bulks(state);
  7883. + /* Retry later */
  7884. + state->poll_needed = 1;
  7885. + }
  7886. + break;
  7887. +
  7888. + case VCHIQ_CONNSTATE_PAUSED:
  7889. + vchiq_platform_resume(state);
  7890. + break;
  7891. +
  7892. + case VCHIQ_CONNSTATE_RESUMING:
  7893. + if (queue_message(state, NULL,
  7894. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  7895. + NULL, 0, 0, QMFLAGS_NO_MUTEX_LOCK)
  7896. + != VCHIQ_RETRY) {
  7897. + if (state->is_master)
  7898. + resume_bulks(state);
  7899. + vchiq_set_conn_state(state,
  7900. + VCHIQ_CONNSTATE_CONNECTED);
  7901. + vchiq_platform_resumed(state);
  7902. + } else {
  7903. + /* This should really be impossible,
  7904. + ** since the PAUSE should have flushed
  7905. + ** through outstanding messages. */
  7906. + vchiq_log_error(vchiq_core_log_level,
  7907. + "Failed to send RESUME "
  7908. + "message");
  7909. + BUG();
  7910. + }
  7911. + break;
  7912. +
  7913. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  7914. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  7915. + vchiq_platform_handle_timeout(state);
  7916. + break;
  7917. + default:
  7918. + break;
  7919. + }
  7920. +
  7921. +
  7922. + }
  7923. +
  7924. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7925. + parse_rx_slots(state);
  7926. + }
  7927. + return 0;
  7928. +}
  7929. +
  7930. +
  7931. +/* Called by the recycle thread */
  7932. +static int
  7933. +recycle_func(void *v)
  7934. +{
  7935. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7936. + VCHIQ_SHARED_STATE_T *local = state->local;
  7937. +
  7938. + while (1) {
  7939. + remote_event_wait(&local->recycle);
  7940. +
  7941. + process_free_queue(state);
  7942. + }
  7943. + return 0;
  7944. +}
  7945. +
  7946. +
  7947. +/* Called by the sync thread */
  7948. +static int
  7949. +sync_func(void *v)
  7950. +{
  7951. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7952. + VCHIQ_SHARED_STATE_T *local = state->local;
  7953. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  7954. + state->remote->slot_sync);
  7955. +
  7956. + while (1) {
  7957. + VCHIQ_SERVICE_T *service;
  7958. + int msgid, size;
  7959. + int type;
  7960. + unsigned int localport, remoteport;
  7961. +
  7962. + remote_event_wait(&local->sync_trigger);
  7963. +
  7964. + rmb();
  7965. +
  7966. + msgid = header->msgid;
  7967. + size = header->size;
  7968. + type = VCHIQ_MSG_TYPE(msgid);
  7969. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7970. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7971. +
  7972. + service = find_service_by_port(state, localport);
  7973. +
  7974. + if (!service) {
  7975. + vchiq_log_error(vchiq_sync_log_level,
  7976. + "%d: sf %s@%x (%d->%d) - "
  7977. + "invalid/closed service %d",
  7978. + state->id, msg_type_str(type),
  7979. + (unsigned int)header,
  7980. + remoteport, localport, localport);
  7981. + release_message_sync(state, header);
  7982. + continue;
  7983. + }
  7984. +
  7985. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  7986. + int svc_fourcc;
  7987. +
  7988. + svc_fourcc = service
  7989. + ? service->base.fourcc
  7990. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  7991. + vchiq_log_trace(vchiq_sync_log_level,
  7992. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  7993. + msg_type_str(type),
  7994. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  7995. + remoteport, localport, size);
  7996. + if (size > 0)
  7997. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  7998. + min(64, size));
  7999. + }
  8000. +
  8001. + switch (type) {
  8002. + case VCHIQ_MSG_OPENACK:
  8003. + if (size >= sizeof(struct vchiq_openack_payload)) {
  8004. + const struct vchiq_openack_payload *payload =
  8005. + (struct vchiq_openack_payload *)
  8006. + header->data;
  8007. + service->peer_version = payload->version;
  8008. + }
  8009. + vchiq_log_info(vchiq_sync_log_level,
  8010. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  8011. + state->id, (unsigned int)header, size,
  8012. + remoteport, localport, service->peer_version);
  8013. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  8014. + service->remoteport = remoteport;
  8015. + vchiq_set_service_state(service,
  8016. + VCHIQ_SRVSTATE_OPENSYNC);
  8017. + service->sync = 1;
  8018. + up(&service->remove_event);
  8019. + }
  8020. + release_message_sync(state, header);
  8021. + break;
  8022. +
  8023. + case VCHIQ_MSG_DATA:
  8024. + vchiq_log_trace(vchiq_sync_log_level,
  8025. + "%d: sf DATA@%x,%x (%d->%d)",
  8026. + state->id, (unsigned int)header, size,
  8027. + remoteport, localport);
  8028. +
  8029. + if ((service->remoteport == remoteport) &&
  8030. + (service->srvstate ==
  8031. + VCHIQ_SRVSTATE_OPENSYNC)) {
  8032. + if (make_service_callback(service,
  8033. + VCHIQ_MESSAGE_AVAILABLE, header,
  8034. + NULL) == VCHIQ_RETRY)
  8035. + vchiq_log_error(vchiq_sync_log_level,
  8036. + "synchronous callback to "
  8037. + "service %d returns "
  8038. + "VCHIQ_RETRY",
  8039. + localport);
  8040. + }
  8041. + break;
  8042. +
  8043. + default:
  8044. + vchiq_log_error(vchiq_sync_log_level,
  8045. + "%d: sf unexpected msgid %x@%x,%x",
  8046. + state->id, msgid, (unsigned int)header, size);
  8047. + release_message_sync(state, header);
  8048. + break;
  8049. + }
  8050. +
  8051. + unlock_service(service);
  8052. + }
  8053. +
  8054. + return 0;
  8055. +}
  8056. +
  8057. +
  8058. +static void
  8059. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  8060. +{
  8061. + queue->local_insert = 0;
  8062. + queue->remote_insert = 0;
  8063. + queue->process = 0;
  8064. + queue->remote_notify = 0;
  8065. + queue->remove = 0;
  8066. +}
  8067. +
  8068. +
  8069. +inline const char *
  8070. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  8071. +{
  8072. + return conn_state_names[conn_state];
  8073. +}
  8074. +
  8075. +
  8076. +VCHIQ_SLOT_ZERO_T *
  8077. +vchiq_init_slots(void *mem_base, int mem_size)
  8078. +{
  8079. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  8080. + VCHIQ_SLOT_ZERO_T *slot_zero =
  8081. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  8082. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  8083. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  8084. +
  8085. + /* Ensure there is enough memory to run an absolutely minimum system */
  8086. + num_slots -= first_data_slot;
  8087. +
  8088. + if (num_slots < 4) {
  8089. + vchiq_log_error(vchiq_core_log_level,
  8090. + "vchiq_init_slots - insufficient memory %x bytes",
  8091. + mem_size);
  8092. + return NULL;
  8093. + }
  8094. +
  8095. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  8096. +
  8097. + slot_zero->magic = VCHIQ_MAGIC;
  8098. + slot_zero->version = VCHIQ_VERSION;
  8099. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  8100. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  8101. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  8102. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  8103. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  8104. +
  8105. + slot_zero->master.slot_sync = first_data_slot;
  8106. + slot_zero->master.slot_first = first_data_slot + 1;
  8107. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  8108. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  8109. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  8110. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  8111. +
  8112. + return slot_zero;
  8113. +}
  8114. +
  8115. +VCHIQ_STATUS_T
  8116. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  8117. + int is_master)
  8118. +{
  8119. + VCHIQ_SHARED_STATE_T *local;
  8120. + VCHIQ_SHARED_STATE_T *remote;
  8121. + VCHIQ_STATUS_T status;
  8122. + char threadname[10];
  8123. + static int id;
  8124. + int i;
  8125. +
  8126. + vchiq_log_warning(vchiq_core_log_level,
  8127. + "%s: slot_zero = 0x%08lx, is_master = %d",
  8128. + __func__, (unsigned long)slot_zero, is_master);
  8129. +
  8130. + /* Check the input configuration */
  8131. +
  8132. + if (slot_zero->magic != VCHIQ_MAGIC) {
  8133. + vchiq_loud_error_header();
  8134. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  8135. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  8136. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  8137. + vchiq_loud_error_footer();
  8138. + return VCHIQ_ERROR;
  8139. + }
  8140. +
  8141. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  8142. + vchiq_loud_error_header();
  8143. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  8144. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  8145. + "(minimum %d)",
  8146. + (unsigned int)slot_zero, slot_zero->version,
  8147. + VCHIQ_VERSION_MIN);
  8148. + vchiq_loud_error("Restart with a newer VideoCore image.");
  8149. + vchiq_loud_error_footer();
  8150. + return VCHIQ_ERROR;
  8151. + }
  8152. +
  8153. + if (VCHIQ_VERSION < slot_zero->version_min) {
  8154. + vchiq_loud_error_header();
  8155. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  8156. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  8157. + "minimum %d)",
  8158. + (unsigned int)slot_zero, VCHIQ_VERSION,
  8159. + slot_zero->version_min);
  8160. + vchiq_loud_error("Restart with a newer kernel.");
  8161. + vchiq_loud_error_footer();
  8162. + return VCHIQ_ERROR;
  8163. + }
  8164. +
  8165. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  8166. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  8167. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  8168. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  8169. + vchiq_loud_error_header();
  8170. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  8171. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  8172. + "(expected %x)",
  8173. + (unsigned int)slot_zero,
  8174. + slot_zero->slot_zero_size,
  8175. + sizeof(VCHIQ_SLOT_ZERO_T));
  8176. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  8177. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  8178. + "(expected %d",
  8179. + (unsigned int)slot_zero, slot_zero->slot_size,
  8180. + VCHIQ_SLOT_SIZE);
  8181. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  8182. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  8183. + "(expected %d)",
  8184. + (unsigned int)slot_zero, slot_zero->max_slots,
  8185. + VCHIQ_MAX_SLOTS);
  8186. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  8187. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  8188. + "(expected %d)",
  8189. + (unsigned int)slot_zero,
  8190. + slot_zero->max_slots_per_side,
  8191. + VCHIQ_MAX_SLOTS_PER_SIDE);
  8192. + vchiq_loud_error_footer();
  8193. + return VCHIQ_ERROR;
  8194. + }
  8195. +
  8196. + if (VCHIQ_VERSION < slot_zero->version)
  8197. + slot_zero->version = VCHIQ_VERSION;
  8198. +
  8199. + if (is_master) {
  8200. + local = &slot_zero->master;
  8201. + remote = &slot_zero->slave;
  8202. + } else {
  8203. + local = &slot_zero->slave;
  8204. + remote = &slot_zero->master;
  8205. + }
  8206. +
  8207. + if (local->initialised) {
  8208. + vchiq_loud_error_header();
  8209. + if (remote->initialised)
  8210. + vchiq_loud_error("local state has already been "
  8211. + "initialised");
  8212. + else
  8213. + vchiq_loud_error("master/slave mismatch - two %ss",
  8214. + is_master ? "master" : "slave");
  8215. + vchiq_loud_error_footer();
  8216. + return VCHIQ_ERROR;
  8217. + }
  8218. +
  8219. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  8220. +
  8221. + state->id = id++;
  8222. + state->is_master = is_master;
  8223. +
  8224. + /*
  8225. + initialize shared state pointers
  8226. + */
  8227. +
  8228. + state->local = local;
  8229. + state->remote = remote;
  8230. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  8231. +
  8232. + /*
  8233. + initialize events and mutexes
  8234. + */
  8235. +
  8236. + sema_init(&state->connect, 0);
  8237. + mutex_init(&state->mutex);
  8238. + sema_init(&state->trigger_event, 0);
  8239. + sema_init(&state->recycle_event, 0);
  8240. + sema_init(&state->sync_trigger_event, 0);
  8241. + sema_init(&state->sync_release_event, 0);
  8242. +
  8243. + mutex_init(&state->slot_mutex);
  8244. + mutex_init(&state->recycle_mutex);
  8245. + mutex_init(&state->sync_mutex);
  8246. + mutex_init(&state->bulk_transfer_mutex);
  8247. +
  8248. + sema_init(&state->slot_available_event, 0);
  8249. + sema_init(&state->slot_remove_event, 0);
  8250. + sema_init(&state->data_quota_event, 0);
  8251. +
  8252. + state->slot_queue_available = 0;
  8253. +
  8254. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  8255. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  8256. + &state->service_quotas[i];
  8257. + sema_init(&service_quota->quota_event, 0);
  8258. + }
  8259. +
  8260. + for (i = local->slot_first; i <= local->slot_last; i++) {
  8261. + local->slot_queue[state->slot_queue_available++] = i;
  8262. + up(&state->slot_available_event);
  8263. + }
  8264. +
  8265. + state->default_slot_quota = state->slot_queue_available/2;
  8266. + state->default_message_quota =
  8267. + min((unsigned short)(state->default_slot_quota * 256),
  8268. + (unsigned short)~0);
  8269. +
  8270. + state->previous_data_index = -1;
  8271. + state->data_use_count = 0;
  8272. + state->data_quota = state->slot_queue_available - 1;
  8273. +
  8274. + local->trigger.event = &state->trigger_event;
  8275. + remote_event_create(&local->trigger);
  8276. + local->tx_pos = 0;
  8277. +
  8278. + local->recycle.event = &state->recycle_event;
  8279. + remote_event_create(&local->recycle);
  8280. + local->slot_queue_recycle = state->slot_queue_available;
  8281. +
  8282. + local->sync_trigger.event = &state->sync_trigger_event;
  8283. + remote_event_create(&local->sync_trigger);
  8284. +
  8285. + local->sync_release.event = &state->sync_release_event;
  8286. + remote_event_create(&local->sync_release);
  8287. +
  8288. + /* At start-of-day, the slot is empty and available */
  8289. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  8290. + = VCHIQ_MSGID_PADDING;
  8291. + remote_event_signal_local(&local->sync_release);
  8292. +
  8293. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  8294. +
  8295. + status = vchiq_platform_init_state(state);
  8296. +
  8297. + /*
  8298. + bring up slot handler thread
  8299. + */
  8300. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  8301. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  8302. + (void *)state,
  8303. + threadname);
  8304. +
  8305. + if (state->slot_handler_thread == NULL) {
  8306. + vchiq_loud_error_header();
  8307. + vchiq_loud_error("couldn't create thread %s", threadname);
  8308. + vchiq_loud_error_footer();
  8309. + return VCHIQ_ERROR;
  8310. + }
  8311. + set_user_nice(state->slot_handler_thread, -19);
  8312. + wake_up_process(state->slot_handler_thread);
  8313. +
  8314. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  8315. + state->recycle_thread = kthread_create(&recycle_func,
  8316. + (void *)state,
  8317. + threadname);
  8318. + if (state->recycle_thread == NULL) {
  8319. + vchiq_loud_error_header();
  8320. + vchiq_loud_error("couldn't create thread %s", threadname);
  8321. + vchiq_loud_error_footer();
  8322. + return VCHIQ_ERROR;
  8323. + }
  8324. + set_user_nice(state->recycle_thread, -19);
  8325. + wake_up_process(state->recycle_thread);
  8326. +
  8327. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  8328. + state->sync_thread = kthread_create(&sync_func,
  8329. + (void *)state,
  8330. + threadname);
  8331. + if (state->sync_thread == NULL) {
  8332. + vchiq_loud_error_header();
  8333. + vchiq_loud_error("couldn't create thread %s", threadname);
  8334. + vchiq_loud_error_footer();
  8335. + return VCHIQ_ERROR;
  8336. + }
  8337. + set_user_nice(state->sync_thread, -20);
  8338. + wake_up_process(state->sync_thread);
  8339. +
  8340. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  8341. + vchiq_states[state->id] = state;
  8342. +
  8343. + /* Indicate readiness to the other side */
  8344. + local->initialised = 1;
  8345. +
  8346. + return status;
  8347. +}
  8348. +
  8349. +/* Called from application thread when a client or server service is created. */
  8350. +VCHIQ_SERVICE_T *
  8351. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  8352. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  8353. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  8354. +{
  8355. + VCHIQ_SERVICE_T *service;
  8356. +
  8357. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  8358. + if (service) {
  8359. + service->base.fourcc = params->fourcc;
  8360. + service->base.callback = params->callback;
  8361. + service->base.userdata = params->userdata;
  8362. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  8363. + service->ref_count = 1;
  8364. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  8365. + service->userdata_term = userdata_term;
  8366. + service->localport = VCHIQ_PORT_FREE;
  8367. + service->remoteport = VCHIQ_PORT_FREE;
  8368. +
  8369. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  8370. + VCHIQ_FOURCC_INVALID : params->fourcc;
  8371. + service->client_id = 0;
  8372. + service->auto_close = 1;
  8373. + service->sync = 0;
  8374. + service->closing = 0;
  8375. + service->trace = 0;
  8376. + atomic_set(&service->poll_flags, 0);
  8377. + service->version = params->version;
  8378. + service->version_min = params->version_min;
  8379. + service->state = state;
  8380. + service->instance = instance;
  8381. + service->service_use_count = 0;
  8382. + init_bulk_queue(&service->bulk_tx);
  8383. + init_bulk_queue(&service->bulk_rx);
  8384. + sema_init(&service->remove_event, 0);
  8385. + sema_init(&service->bulk_remove_event, 0);
  8386. + mutex_init(&service->bulk_mutex);
  8387. + memset(&service->stats, 0, sizeof(service->stats));
  8388. + } else {
  8389. + vchiq_log_error(vchiq_core_log_level,
  8390. + "Out of memory");
  8391. + }
  8392. +
  8393. + if (service) {
  8394. + VCHIQ_SERVICE_T **pservice = NULL;
  8395. + int i;
  8396. +
  8397. + /* Although it is perfectly possible to use service_spinlock
  8398. + ** to protect the creation of services, it is overkill as it
  8399. + ** disables interrupts while the array is searched.
  8400. + ** The only danger is of another thread trying to create a
  8401. + ** service - service deletion is safe.
  8402. + ** Therefore it is preferable to use state->mutex which,
  8403. + ** although slower to claim, doesn't block interrupts while
  8404. + ** it is held.
  8405. + */
  8406. +
  8407. + mutex_lock(&state->mutex);
  8408. +
  8409. + /* Prepare to use a previously unused service */
  8410. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  8411. + pservice = &state->services[state->unused_service];
  8412. +
  8413. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  8414. + for (i = 0; i < state->unused_service; i++) {
  8415. + VCHIQ_SERVICE_T *srv = state->services[i];
  8416. + if (!srv) {
  8417. + pservice = &state->services[i];
  8418. + break;
  8419. + }
  8420. + }
  8421. + } else {
  8422. + for (i = (state->unused_service - 1); i >= 0; i--) {
  8423. + VCHIQ_SERVICE_T *srv = state->services[i];
  8424. + if (!srv)
  8425. + pservice = &state->services[i];
  8426. + else if ((srv->public_fourcc == params->fourcc)
  8427. + && ((srv->instance != instance) ||
  8428. + (srv->base.callback !=
  8429. + params->callback))) {
  8430. + /* There is another server using this
  8431. + ** fourcc which doesn't match. */
  8432. + pservice = NULL;
  8433. + break;
  8434. + }
  8435. + }
  8436. + }
  8437. +
  8438. + if (pservice) {
  8439. + service->localport = (pservice - state->services);
  8440. + if (!handle_seq)
  8441. + handle_seq = VCHIQ_MAX_STATES *
  8442. + VCHIQ_MAX_SERVICES;
  8443. + service->handle = handle_seq |
  8444. + (state->id * VCHIQ_MAX_SERVICES) |
  8445. + service->localport;
  8446. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  8447. + *pservice = service;
  8448. + if (pservice == &state->services[state->unused_service])
  8449. + state->unused_service++;
  8450. + }
  8451. +
  8452. + mutex_unlock(&state->mutex);
  8453. +
  8454. + if (!pservice) {
  8455. + kfree(service);
  8456. + service = NULL;
  8457. + }
  8458. + }
  8459. +
  8460. + if (service) {
  8461. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  8462. + &state->service_quotas[service->localport];
  8463. + service_quota->slot_quota = state->default_slot_quota;
  8464. + service_quota->message_quota = state->default_message_quota;
  8465. + if (service_quota->slot_use_count == 0)
  8466. + service_quota->previous_tx_index =
  8467. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  8468. + - 1;
  8469. +
  8470. + /* Bring this service online */
  8471. + vchiq_set_service_state(service, srvstate);
  8472. +
  8473. + vchiq_log_info(vchiq_core_msg_log_level,
  8474. + "%s Service %c%c%c%c SrcPort:%d",
  8475. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  8476. + ? "Open" : "Add",
  8477. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  8478. + service->localport);
  8479. + }
  8480. +
  8481. + /* Don't unlock the service - leave it with a ref_count of 1. */
  8482. +
  8483. + return service;
  8484. +}
  8485. +
  8486. +VCHIQ_STATUS_T
  8487. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  8488. +{
  8489. + struct vchiq_open_payload payload = {
  8490. + service->base.fourcc,
  8491. + client_id,
  8492. + service->version,
  8493. + service->version_min
  8494. + };
  8495. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  8496. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8497. +
  8498. + service->client_id = client_id;
  8499. + vchiq_use_service_internal(service);
  8500. + status = queue_message(service->state, NULL,
  8501. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  8502. + &body, 1, sizeof(payload), QMFLAGS_IS_BLOCKING);
  8503. + if (status == VCHIQ_SUCCESS) {
  8504. + /* Wait for the ACK/NAK */
  8505. + if (down_interruptible(&service->remove_event) != 0) {
  8506. + status = VCHIQ_RETRY;
  8507. + vchiq_release_service_internal(service);
  8508. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  8509. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  8510. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  8511. + vchiq_log_error(vchiq_core_log_level,
  8512. + "%d: osi - srvstate = %s (ref %d)",
  8513. + service->state->id,
  8514. + srvstate_names[service->srvstate],
  8515. + service->ref_count);
  8516. + status = VCHIQ_ERROR;
  8517. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  8518. + vchiq_release_service_internal(service);
  8519. + }
  8520. + }
  8521. + return status;
  8522. +}
  8523. +
  8524. +static void
  8525. +release_service_messages(VCHIQ_SERVICE_T *service)
  8526. +{
  8527. + VCHIQ_STATE_T *state = service->state;
  8528. + int slot_last = state->remote->slot_last;
  8529. + int i;
  8530. +
  8531. + /* Release any claimed messages aimed at this service */
  8532. +
  8533. + if (service->sync) {
  8534. + VCHIQ_HEADER_T *header =
  8535. + (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  8536. + state->remote->slot_sync);
  8537. + if (VCHIQ_MSG_DSTPORT(header->msgid) == service->localport)
  8538. + release_message_sync(state, header);
  8539. +
  8540. + return;
  8541. + }
  8542. +
  8543. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  8544. + VCHIQ_SLOT_INFO_T *slot_info =
  8545. + SLOT_INFO_FROM_INDEX(state, i);
  8546. + if (slot_info->release_count != slot_info->use_count) {
  8547. + char *data =
  8548. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  8549. + unsigned int pos, end;
  8550. +
  8551. + end = VCHIQ_SLOT_SIZE;
  8552. + if (data == state->rx_data)
  8553. + /* This buffer is still being read from - stop
  8554. + ** at the current read position */
  8555. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  8556. +
  8557. + pos = 0;
  8558. +
  8559. + while (pos < end) {
  8560. + VCHIQ_HEADER_T *header =
  8561. + (VCHIQ_HEADER_T *)(data + pos);
  8562. + int msgid = header->msgid;
  8563. + int port = VCHIQ_MSG_DSTPORT(msgid);
  8564. + if ((port == service->localport) &&
  8565. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  8566. + vchiq_log_info(vchiq_core_log_level,
  8567. + " fsi - hdr %x",
  8568. + (unsigned int)header);
  8569. + release_slot(state, slot_info, header,
  8570. + NULL);
  8571. + }
  8572. + pos += calc_stride(header->size);
  8573. + if (pos > VCHIQ_SLOT_SIZE) {
  8574. + vchiq_log_error(vchiq_core_log_level,
  8575. + "fsi - pos %x: header %x, "
  8576. + "msgid %x, header->msgid %x, "
  8577. + "header->size %x",
  8578. + pos, (unsigned int)header,
  8579. + msgid, header->msgid,
  8580. + header->size);
  8581. + WARN(1, "invalid slot position\n");
  8582. + }
  8583. + }
  8584. + }
  8585. + }
  8586. +}
  8587. +
  8588. +static int
  8589. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  8590. +{
  8591. + VCHIQ_STATUS_T status;
  8592. +
  8593. + /* Abort any outstanding bulk transfers */
  8594. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  8595. + return 0;
  8596. + abort_outstanding_bulks(service, &service->bulk_tx);
  8597. + abort_outstanding_bulks(service, &service->bulk_rx);
  8598. + mutex_unlock(&service->bulk_mutex);
  8599. +
  8600. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  8601. + if (status == VCHIQ_SUCCESS)
  8602. + status = notify_bulks(service, &service->bulk_rx,
  8603. + 0/*!retry_poll*/);
  8604. + return (status == VCHIQ_SUCCESS);
  8605. +}
  8606. +
  8607. +static VCHIQ_STATUS_T
  8608. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  8609. +{
  8610. + VCHIQ_STATUS_T status;
  8611. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  8612. + int newstate;
  8613. +
  8614. + switch (service->srvstate) {
  8615. + case VCHIQ_SRVSTATE_OPEN:
  8616. + case VCHIQ_SRVSTATE_CLOSESENT:
  8617. + case VCHIQ_SRVSTATE_CLOSERECVD:
  8618. + if (is_server) {
  8619. + if (service->auto_close) {
  8620. + service->client_id = 0;
  8621. + service->remoteport = VCHIQ_PORT_FREE;
  8622. + newstate = VCHIQ_SRVSTATE_LISTENING;
  8623. + } else
  8624. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  8625. + } else
  8626. + newstate = VCHIQ_SRVSTATE_CLOSED;
  8627. + vchiq_set_service_state(service, newstate);
  8628. + break;
  8629. + case VCHIQ_SRVSTATE_LISTENING:
  8630. + break;
  8631. + default:
  8632. + vchiq_log_error(vchiq_core_log_level,
  8633. + "close_service_complete(%x) called in state %s",
  8634. + service->handle, srvstate_names[service->srvstate]);
  8635. + WARN(1, "close_service_complete in unexpected state\n");
  8636. + return VCHIQ_ERROR;
  8637. + }
  8638. +
  8639. + status = make_service_callback(service,
  8640. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  8641. +
  8642. + if (status != VCHIQ_RETRY) {
  8643. + int uc = service->service_use_count;
  8644. + int i;
  8645. + /* Complete the close process */
  8646. + for (i = 0; i < uc; i++)
  8647. + /* cater for cases where close is forced and the
  8648. + ** client may not close all it's handles */
  8649. + vchiq_release_service_internal(service);
  8650. +
  8651. + service->client_id = 0;
  8652. + service->remoteport = VCHIQ_PORT_FREE;
  8653. +
  8654. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  8655. + vchiq_free_service_internal(service);
  8656. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  8657. + if (is_server)
  8658. + service->closing = 0;
  8659. +
  8660. + up(&service->remove_event);
  8661. + }
  8662. + } else
  8663. + vchiq_set_service_state(service, failstate);
  8664. +
  8665. + return status;
  8666. +}
  8667. +
  8668. +/* Called by the slot handler */
  8669. +VCHIQ_STATUS_T
  8670. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  8671. +{
  8672. + VCHIQ_STATE_T *state = service->state;
  8673. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8674. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  8675. +
  8676. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  8677. + service->state->id, service->localport, close_recvd,
  8678. + srvstate_names[service->srvstate]);
  8679. +
  8680. + switch (service->srvstate) {
  8681. + case VCHIQ_SRVSTATE_CLOSED:
  8682. + case VCHIQ_SRVSTATE_HIDDEN:
  8683. + case VCHIQ_SRVSTATE_LISTENING:
  8684. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  8685. + if (close_recvd)
  8686. + vchiq_log_error(vchiq_core_log_level,
  8687. + "vchiq_close_service_internal(1) called "
  8688. + "in state %s",
  8689. + srvstate_names[service->srvstate]);
  8690. + else if (is_server) {
  8691. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  8692. + status = VCHIQ_ERROR;
  8693. + } else {
  8694. + service->client_id = 0;
  8695. + service->remoteport = VCHIQ_PORT_FREE;
  8696. + if (service->srvstate ==
  8697. + VCHIQ_SRVSTATE_CLOSEWAIT)
  8698. + vchiq_set_service_state(service,
  8699. + VCHIQ_SRVSTATE_LISTENING);
  8700. + }
  8701. + up(&service->remove_event);
  8702. + } else
  8703. + vchiq_free_service_internal(service);
  8704. + break;
  8705. + case VCHIQ_SRVSTATE_OPENING:
  8706. + if (close_recvd) {
  8707. + /* The open was rejected - tell the user */
  8708. + vchiq_set_service_state(service,
  8709. + VCHIQ_SRVSTATE_CLOSEWAIT);
  8710. + up(&service->remove_event);
  8711. + } else {
  8712. + /* Shutdown mid-open - let the other side know */
  8713. + status = queue_message(state, service,
  8714. + VCHIQ_MAKE_MSG
  8715. + (VCHIQ_MSG_CLOSE,
  8716. + service->localport,
  8717. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  8718. + NULL, 0, 0, 0);
  8719. + }
  8720. + break;
  8721. +
  8722. + case VCHIQ_SRVSTATE_OPENSYNC:
  8723. + mutex_lock(&state->sync_mutex);
  8724. + /* Drop through */
  8725. +
  8726. + case VCHIQ_SRVSTATE_OPEN:
  8727. + if (state->is_master || close_recvd) {
  8728. + if (!do_abort_bulks(service))
  8729. + status = VCHIQ_RETRY;
  8730. + }
  8731. +
  8732. + release_service_messages(service);
  8733. +
  8734. + if (status == VCHIQ_SUCCESS)
  8735. + status = queue_message(state, service,
  8736. + VCHIQ_MAKE_MSG
  8737. + (VCHIQ_MSG_CLOSE,
  8738. + service->localport,
  8739. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  8740. + NULL, 0, 0, QMFLAGS_NO_MUTEX_UNLOCK);
  8741. +
  8742. + if (status == VCHIQ_SUCCESS) {
  8743. + if (!close_recvd) {
  8744. + /* Change the state while the mutex is
  8745. + still held */
  8746. + vchiq_set_service_state(service,
  8747. + VCHIQ_SRVSTATE_CLOSESENT);
  8748. + mutex_unlock(&state->slot_mutex);
  8749. + if (service->sync)
  8750. + mutex_unlock(&state->sync_mutex);
  8751. + break;
  8752. + }
  8753. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  8754. + mutex_unlock(&state->sync_mutex);
  8755. + break;
  8756. + } else
  8757. + break;
  8758. +
  8759. + /* Change the state while the mutex is still held */
  8760. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSERECVD);
  8761. + mutex_unlock(&state->slot_mutex);
  8762. + if (service->sync)
  8763. + mutex_unlock(&state->sync_mutex);
  8764. +
  8765. + status = close_service_complete(service,
  8766. + VCHIQ_SRVSTATE_CLOSERECVD);
  8767. + break;
  8768. +
  8769. + case VCHIQ_SRVSTATE_CLOSESENT:
  8770. + if (!close_recvd)
  8771. + /* This happens when a process is killed mid-close */
  8772. + break;
  8773. +
  8774. + if (!state->is_master) {
  8775. + if (!do_abort_bulks(service)) {
  8776. + status = VCHIQ_RETRY;
  8777. + break;
  8778. + }
  8779. + }
  8780. +
  8781. + if (status == VCHIQ_SUCCESS)
  8782. + status = close_service_complete(service,
  8783. + VCHIQ_SRVSTATE_CLOSERECVD);
  8784. + break;
  8785. +
  8786. + case VCHIQ_SRVSTATE_CLOSERECVD:
  8787. + if (!close_recvd && is_server)
  8788. + /* Force into LISTENING mode */
  8789. + vchiq_set_service_state(service,
  8790. + VCHIQ_SRVSTATE_LISTENING);
  8791. + status = close_service_complete(service,
  8792. + VCHIQ_SRVSTATE_CLOSERECVD);
  8793. + break;
  8794. +
  8795. + default:
  8796. + vchiq_log_error(vchiq_core_log_level,
  8797. + "vchiq_close_service_internal(%d) called in state %s",
  8798. + close_recvd, srvstate_names[service->srvstate]);
  8799. + break;
  8800. + }
  8801. +
  8802. + return status;
  8803. +}
  8804. +
  8805. +/* Called from the application process upon process death */
  8806. +void
  8807. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  8808. +{
  8809. + VCHIQ_STATE_T *state = service->state;
  8810. +
  8811. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  8812. + state->id, service->localport, service->remoteport);
  8813. +
  8814. + mark_service_closing(service);
  8815. +
  8816. + /* Mark the service for removal by the slot handler */
  8817. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  8818. +}
  8819. +
  8820. +/* Called from the slot handler */
  8821. +void
  8822. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  8823. +{
  8824. + VCHIQ_STATE_T *state = service->state;
  8825. +
  8826. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  8827. + state->id, service->localport);
  8828. +
  8829. + switch (service->srvstate) {
  8830. + case VCHIQ_SRVSTATE_OPENING:
  8831. + case VCHIQ_SRVSTATE_CLOSED:
  8832. + case VCHIQ_SRVSTATE_HIDDEN:
  8833. + case VCHIQ_SRVSTATE_LISTENING:
  8834. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  8835. + break;
  8836. + default:
  8837. + vchiq_log_error(vchiq_core_log_level,
  8838. + "%d: fsi - (%d) in state %s",
  8839. + state->id, service->localport,
  8840. + srvstate_names[service->srvstate]);
  8841. + return;
  8842. + }
  8843. +
  8844. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  8845. +
  8846. + up(&service->remove_event);
  8847. +
  8848. + /* Release the initial lock */
  8849. + unlock_service(service);
  8850. +}
  8851. +
  8852. +VCHIQ_STATUS_T
  8853. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  8854. +{
  8855. + VCHIQ_SERVICE_T *service;
  8856. + int i;
  8857. +
  8858. + /* Find all services registered to this client and enable them. */
  8859. + i = 0;
  8860. + while ((service = next_service_by_instance(state, instance,
  8861. + &i)) != NULL) {
  8862. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  8863. + vchiq_set_service_state(service,
  8864. + VCHIQ_SRVSTATE_LISTENING);
  8865. + unlock_service(service);
  8866. + }
  8867. +
  8868. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  8869. + if (queue_message(state, NULL,
  8870. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  8871. + 0, QMFLAGS_IS_BLOCKING) == VCHIQ_RETRY)
  8872. + return VCHIQ_RETRY;
  8873. +
  8874. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  8875. + }
  8876. +
  8877. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  8878. + if (down_interruptible(&state->connect) != 0)
  8879. + return VCHIQ_RETRY;
  8880. +
  8881. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  8882. + up(&state->connect);
  8883. + }
  8884. +
  8885. + return VCHIQ_SUCCESS;
  8886. +}
  8887. +
  8888. +VCHIQ_STATUS_T
  8889. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  8890. +{
  8891. + VCHIQ_SERVICE_T *service;
  8892. + int i;
  8893. +
  8894. + /* Find all services registered to this client and enable them. */
  8895. + i = 0;
  8896. + while ((service = next_service_by_instance(state, instance,
  8897. + &i)) != NULL) {
  8898. + (void)vchiq_remove_service(service->handle);
  8899. + unlock_service(service);
  8900. + }
  8901. +
  8902. + return VCHIQ_SUCCESS;
  8903. +}
  8904. +
  8905. +VCHIQ_STATUS_T
  8906. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  8907. +{
  8908. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8909. +
  8910. + switch (state->conn_state) {
  8911. + case VCHIQ_CONNSTATE_CONNECTED:
  8912. + /* Request a pause */
  8913. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  8914. + request_poll(state, NULL, 0);
  8915. + break;
  8916. + default:
  8917. + vchiq_log_error(vchiq_core_log_level,
  8918. + "vchiq_pause_internal in state %s\n",
  8919. + conn_state_names[state->conn_state]);
  8920. + status = VCHIQ_ERROR;
  8921. + VCHIQ_STATS_INC(state, error_count);
  8922. + break;
  8923. + }
  8924. +
  8925. + return status;
  8926. +}
  8927. +
  8928. +VCHIQ_STATUS_T
  8929. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  8930. +{
  8931. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8932. +
  8933. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  8934. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  8935. + request_poll(state, NULL, 0);
  8936. + } else {
  8937. + status = VCHIQ_ERROR;
  8938. + VCHIQ_STATS_INC(state, error_count);
  8939. + }
  8940. +
  8941. + return status;
  8942. +}
  8943. +
  8944. +VCHIQ_STATUS_T
  8945. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  8946. +{
  8947. + /* Unregister the service */
  8948. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  8949. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8950. +
  8951. + if (!service)
  8952. + return VCHIQ_ERROR;
  8953. +
  8954. + vchiq_log_info(vchiq_core_log_level,
  8955. + "%d: close_service:%d",
  8956. + service->state->id, service->localport);
  8957. +
  8958. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  8959. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  8960. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  8961. + unlock_service(service);
  8962. + return VCHIQ_ERROR;
  8963. + }
  8964. +
  8965. + mark_service_closing(service);
  8966. +
  8967. + if (current == service->state->slot_handler_thread) {
  8968. + status = vchiq_close_service_internal(service,
  8969. + 0/*!close_recvd*/);
  8970. + BUG_ON(status == VCHIQ_RETRY);
  8971. + } else {
  8972. + /* Mark the service for termination by the slot handler */
  8973. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  8974. + }
  8975. +
  8976. + while (1) {
  8977. + if (down_interruptible(&service->remove_event) != 0) {
  8978. + status = VCHIQ_RETRY;
  8979. + break;
  8980. + }
  8981. +
  8982. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  8983. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  8984. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  8985. + break;
  8986. +
  8987. + vchiq_log_warning(vchiq_core_log_level,
  8988. + "%d: close_service:%d - waiting in state %s",
  8989. + service->state->id, service->localport,
  8990. + srvstate_names[service->srvstate]);
  8991. + }
  8992. +
  8993. + if ((status == VCHIQ_SUCCESS) &&
  8994. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  8995. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  8996. + status = VCHIQ_ERROR;
  8997. +
  8998. + unlock_service(service);
  8999. +
  9000. + return status;
  9001. +}
  9002. +
  9003. +VCHIQ_STATUS_T
  9004. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  9005. +{
  9006. + /* Unregister the service */
  9007. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9008. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  9009. +
  9010. + if (!service)
  9011. + return VCHIQ_ERROR;
  9012. +
  9013. + vchiq_log_info(vchiq_core_log_level,
  9014. + "%d: remove_service:%d",
  9015. + service->state->id, service->localport);
  9016. +
  9017. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  9018. + unlock_service(service);
  9019. + return VCHIQ_ERROR;
  9020. + }
  9021. +
  9022. + mark_service_closing(service);
  9023. +
  9024. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  9025. + (current == service->state->slot_handler_thread)) {
  9026. + /* Make it look like a client, because it must be removed and
  9027. + not left in the LISTENING state. */
  9028. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  9029. +
  9030. + status = vchiq_close_service_internal(service,
  9031. + 0/*!close_recvd*/);
  9032. + BUG_ON(status == VCHIQ_RETRY);
  9033. + } else {
  9034. + /* Mark the service for removal by the slot handler */
  9035. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  9036. + }
  9037. + while (1) {
  9038. + if (down_interruptible(&service->remove_event) != 0) {
  9039. + status = VCHIQ_RETRY;
  9040. + break;
  9041. + }
  9042. +
  9043. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  9044. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  9045. + break;
  9046. +
  9047. + vchiq_log_warning(vchiq_core_log_level,
  9048. + "%d: remove_service:%d - waiting in state %s",
  9049. + service->state->id, service->localport,
  9050. + srvstate_names[service->srvstate]);
  9051. + }
  9052. +
  9053. + if ((status == VCHIQ_SUCCESS) &&
  9054. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  9055. + status = VCHIQ_ERROR;
  9056. +
  9057. + unlock_service(service);
  9058. +
  9059. + return status;
  9060. +}
  9061. +
  9062. +
  9063. +/* This function may be called by kernel threads or user threads.
  9064. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  9065. + * received and the call should be retried after being returned to user
  9066. + * context.
  9067. + * When called in blocking mode, the userdata field points to a bulk_waiter
  9068. + * structure.
  9069. + */
  9070. +VCHIQ_STATUS_T
  9071. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  9072. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  9073. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  9074. +{
  9075. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9076. + VCHIQ_BULK_QUEUE_T *queue;
  9077. + VCHIQ_BULK_T *bulk;
  9078. + VCHIQ_STATE_T *state;
  9079. + struct bulk_waiter *bulk_waiter = NULL;
  9080. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  9081. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  9082. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  9083. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9084. +
  9085. + if (!service ||
  9086. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  9087. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  9088. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  9089. + goto error_exit;
  9090. +
  9091. + switch (mode) {
  9092. + case VCHIQ_BULK_MODE_NOCALLBACK:
  9093. + case VCHIQ_BULK_MODE_CALLBACK:
  9094. + break;
  9095. + case VCHIQ_BULK_MODE_BLOCKING:
  9096. + bulk_waiter = (struct bulk_waiter *)userdata;
  9097. + sema_init(&bulk_waiter->event, 0);
  9098. + bulk_waiter->actual = 0;
  9099. + bulk_waiter->bulk = NULL;
  9100. + break;
  9101. + case VCHIQ_BULK_MODE_WAITING:
  9102. + bulk_waiter = (struct bulk_waiter *)userdata;
  9103. + bulk = bulk_waiter->bulk;
  9104. + goto waiting;
  9105. + default:
  9106. + goto error_exit;
  9107. + }
  9108. +
  9109. + state = service->state;
  9110. +
  9111. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  9112. + &service->bulk_tx : &service->bulk_rx;
  9113. +
  9114. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  9115. + status = VCHIQ_RETRY;
  9116. + goto error_exit;
  9117. + }
  9118. +
  9119. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  9120. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  9121. + do {
  9122. + mutex_unlock(&service->bulk_mutex);
  9123. + if (down_interruptible(&service->bulk_remove_event)
  9124. + != 0) {
  9125. + status = VCHIQ_RETRY;
  9126. + goto error_exit;
  9127. + }
  9128. + if (mutex_lock_interruptible(&service->bulk_mutex)
  9129. + != 0) {
  9130. + status = VCHIQ_RETRY;
  9131. + goto error_exit;
  9132. + }
  9133. + } while (queue->local_insert == queue->remove +
  9134. + VCHIQ_NUM_SERVICE_BULKS);
  9135. + }
  9136. +
  9137. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  9138. +
  9139. + bulk->mode = mode;
  9140. + bulk->dir = dir;
  9141. + bulk->userdata = userdata;
  9142. + bulk->size = size;
  9143. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  9144. +
  9145. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  9146. + VCHIQ_SUCCESS)
  9147. + goto unlock_error_exit;
  9148. +
  9149. + wmb();
  9150. +
  9151. + vchiq_log_info(vchiq_core_log_level,
  9152. + "%d: bt (%d->%d) %cx %x@%x %x",
  9153. + state->id,
  9154. + service->localport, service->remoteport, dir_char,
  9155. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  9156. +
  9157. + /* The slot mutex must be held when the service is being closed, so
  9158. + claim it here to ensure that isn't happening */
  9159. + if (mutex_lock_interruptible(&state->slot_mutex) != 0) {
  9160. + status = VCHIQ_RETRY;
  9161. + goto cancel_bulk_error_exit;
  9162. + }
  9163. +
  9164. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN)
  9165. + goto unlock_both_error_exit;
  9166. +
  9167. + if (state->is_master) {
  9168. + queue->local_insert++;
  9169. + if (resolve_bulks(service, queue))
  9170. + request_poll(state, service,
  9171. + (dir == VCHIQ_BULK_TRANSMIT) ?
  9172. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  9173. + } else {
  9174. + int payload[2] = { (int)bulk->data, bulk->size };
  9175. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  9176. +
  9177. + status = queue_message(state, NULL,
  9178. + VCHIQ_MAKE_MSG(dir_msgtype,
  9179. + service->localport, service->remoteport),
  9180. + &element, 1, sizeof(payload),
  9181. + QMFLAGS_IS_BLOCKING |
  9182. + QMFLAGS_NO_MUTEX_LOCK |
  9183. + QMFLAGS_NO_MUTEX_UNLOCK);
  9184. + if (status != VCHIQ_SUCCESS) {
  9185. + goto unlock_both_error_exit;
  9186. + }
  9187. + queue->local_insert++;
  9188. + }
  9189. +
  9190. + mutex_unlock(&state->slot_mutex);
  9191. + mutex_unlock(&service->bulk_mutex);
  9192. +
  9193. + vchiq_log_trace(vchiq_core_log_level,
  9194. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  9195. + state->id,
  9196. + service->localport, dir_char,
  9197. + queue->local_insert, queue->remote_insert, queue->process);
  9198. +
  9199. +waiting:
  9200. + unlock_service(service);
  9201. +
  9202. + status = VCHIQ_SUCCESS;
  9203. +
  9204. + if (bulk_waiter) {
  9205. + bulk_waiter->bulk = bulk;
  9206. + if (down_interruptible(&bulk_waiter->event) != 0)
  9207. + status = VCHIQ_RETRY;
  9208. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  9209. + status = VCHIQ_ERROR;
  9210. + }
  9211. +
  9212. + return status;
  9213. +
  9214. +unlock_both_error_exit:
  9215. + mutex_unlock(&state->slot_mutex);
  9216. +cancel_bulk_error_exit:
  9217. + vchiq_complete_bulk(bulk);
  9218. +unlock_error_exit:
  9219. + mutex_unlock(&service->bulk_mutex);
  9220. +
  9221. +error_exit:
  9222. + if (service)
  9223. + unlock_service(service);
  9224. + return status;
  9225. +}
  9226. +
  9227. +VCHIQ_STATUS_T
  9228. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  9229. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  9230. +{
  9231. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9232. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9233. +
  9234. + unsigned int size = 0;
  9235. + unsigned int i;
  9236. +
  9237. + if (!service ||
  9238. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  9239. + goto error_exit;
  9240. +
  9241. + for (i = 0; i < (unsigned int)count; i++) {
  9242. + if (elements[i].size) {
  9243. + if (elements[i].data == NULL) {
  9244. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  9245. + goto error_exit;
  9246. + }
  9247. + size += elements[i].size;
  9248. + }
  9249. + }
  9250. +
  9251. + if (size > VCHIQ_MAX_MSG_SIZE) {
  9252. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  9253. + goto error_exit;
  9254. + }
  9255. +
  9256. + switch (service->srvstate) {
  9257. + case VCHIQ_SRVSTATE_OPEN:
  9258. + status = queue_message(service->state, service,
  9259. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  9260. + service->localport,
  9261. + service->remoteport),
  9262. + elements, count, size, 1);
  9263. + break;
  9264. + case VCHIQ_SRVSTATE_OPENSYNC:
  9265. + status = queue_message_sync(service->state, service,
  9266. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  9267. + service->localport,
  9268. + service->remoteport),
  9269. + elements, count, size, 1);
  9270. + break;
  9271. + default:
  9272. + status = VCHIQ_ERROR;
  9273. + break;
  9274. + }
  9275. +
  9276. +error_exit:
  9277. + if (service)
  9278. + unlock_service(service);
  9279. +
  9280. + return status;
  9281. +}
  9282. +
  9283. +void
  9284. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  9285. +{
  9286. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9287. + VCHIQ_SHARED_STATE_T *remote;
  9288. + VCHIQ_STATE_T *state;
  9289. + int slot_index;
  9290. +
  9291. + if (!service)
  9292. + return;
  9293. +
  9294. + state = service->state;
  9295. + remote = state->remote;
  9296. +
  9297. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  9298. +
  9299. + if ((slot_index >= remote->slot_first) &&
  9300. + (slot_index <= remote->slot_last)) {
  9301. + int msgid = header->msgid;
  9302. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  9303. + VCHIQ_SLOT_INFO_T *slot_info =
  9304. + SLOT_INFO_FROM_INDEX(state, slot_index);
  9305. +
  9306. + release_slot(state, slot_info, header, service);
  9307. + }
  9308. + } else if (slot_index == remote->slot_sync)
  9309. + release_message_sync(state, header);
  9310. +
  9311. + unlock_service(service);
  9312. +}
  9313. +
  9314. +static void
  9315. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  9316. +{
  9317. + header->msgid = VCHIQ_MSGID_PADDING;
  9318. + wmb();
  9319. + remote_event_signal(&state->remote->sync_release);
  9320. +}
  9321. +
  9322. +VCHIQ_STATUS_T
  9323. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  9324. +{
  9325. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9326. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9327. +
  9328. + if (!service ||
  9329. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  9330. + !peer_version)
  9331. + goto exit;
  9332. + *peer_version = service->peer_version;
  9333. + status = VCHIQ_SUCCESS;
  9334. +
  9335. +exit:
  9336. + if (service)
  9337. + unlock_service(service);
  9338. + return status;
  9339. +}
  9340. +
  9341. +VCHIQ_STATUS_T
  9342. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  9343. + int config_size, VCHIQ_CONFIG_T *pconfig)
  9344. +{
  9345. + VCHIQ_CONFIG_T config;
  9346. +
  9347. + (void)instance;
  9348. +
  9349. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  9350. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  9351. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  9352. + config.max_services = VCHIQ_MAX_SERVICES;
  9353. + config.version = VCHIQ_VERSION;
  9354. + config.version_min = VCHIQ_VERSION_MIN;
  9355. +
  9356. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  9357. + return VCHIQ_ERROR;
  9358. +
  9359. + memcpy(pconfig, &config,
  9360. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  9361. +
  9362. + return VCHIQ_SUCCESS;
  9363. +}
  9364. +
  9365. +VCHIQ_STATUS_T
  9366. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  9367. + VCHIQ_SERVICE_OPTION_T option, int value)
  9368. +{
  9369. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9370. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9371. +
  9372. + if (service) {
  9373. + switch (option) {
  9374. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  9375. + service->auto_close = value;
  9376. + status = VCHIQ_SUCCESS;
  9377. + break;
  9378. +
  9379. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  9380. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9381. + &service->state->service_quotas[
  9382. + service->localport];
  9383. + if (value == 0)
  9384. + value = service->state->default_slot_quota;
  9385. + if ((value >= service_quota->slot_use_count) &&
  9386. + (value < (unsigned short)~0)) {
  9387. + service_quota->slot_quota = value;
  9388. + if ((value >= service_quota->slot_use_count) &&
  9389. + (service_quota->message_quota >=
  9390. + service_quota->message_use_count)) {
  9391. + /* Signal the service that it may have
  9392. + ** dropped below its quota */
  9393. + up(&service_quota->quota_event);
  9394. + }
  9395. + status = VCHIQ_SUCCESS;
  9396. + }
  9397. + } break;
  9398. +
  9399. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  9400. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9401. + &service->state->service_quotas[
  9402. + service->localport];
  9403. + if (value == 0)
  9404. + value = service->state->default_message_quota;
  9405. + if ((value >= service_quota->message_use_count) &&
  9406. + (value < (unsigned short)~0)) {
  9407. + service_quota->message_quota = value;
  9408. + if ((value >=
  9409. + service_quota->message_use_count) &&
  9410. + (service_quota->slot_quota >=
  9411. + service_quota->slot_use_count))
  9412. + /* Signal the service that it may have
  9413. + ** dropped below its quota */
  9414. + up(&service_quota->quota_event);
  9415. + status = VCHIQ_SUCCESS;
  9416. + }
  9417. + } break;
  9418. +
  9419. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  9420. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  9421. + (service->srvstate ==
  9422. + VCHIQ_SRVSTATE_LISTENING)) {
  9423. + service->sync = value;
  9424. + status = VCHIQ_SUCCESS;
  9425. + }
  9426. + break;
  9427. +
  9428. + case VCHIQ_SERVICE_OPTION_TRACE:
  9429. + service->trace = value;
  9430. + status = VCHIQ_SUCCESS;
  9431. + break;
  9432. +
  9433. + default:
  9434. + break;
  9435. + }
  9436. + unlock_service(service);
  9437. + }
  9438. +
  9439. + return status;
  9440. +}
  9441. +
  9442. +void
  9443. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  9444. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  9445. +{
  9446. + static const char *const debug_names[] = {
  9447. + "<entries>",
  9448. + "SLOT_HANDLER_COUNT",
  9449. + "SLOT_HANDLER_LINE",
  9450. + "PARSE_LINE",
  9451. + "PARSE_HEADER",
  9452. + "PARSE_MSGID",
  9453. + "AWAIT_COMPLETION_LINE",
  9454. + "DEQUEUE_MESSAGE_LINE",
  9455. + "SERVICE_CALLBACK_LINE",
  9456. + "MSG_QUEUE_FULL_COUNT",
  9457. + "COMPLETION_QUEUE_FULL_COUNT"
  9458. + };
  9459. + int i;
  9460. +
  9461. + char buf[80];
  9462. + int len;
  9463. + len = snprintf(buf, sizeof(buf),
  9464. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  9465. + label, shared->slot_first, shared->slot_last,
  9466. + shared->tx_pos, shared->slot_queue_recycle);
  9467. + vchiq_dump(dump_context, buf, len + 1);
  9468. +
  9469. + len = snprintf(buf, sizeof(buf),
  9470. + " Slots claimed:");
  9471. + vchiq_dump(dump_context, buf, len + 1);
  9472. +
  9473. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  9474. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  9475. + if (slot_info.use_count != slot_info.release_count) {
  9476. + len = snprintf(buf, sizeof(buf),
  9477. + " %d: %d/%d", i, slot_info.use_count,
  9478. + slot_info.release_count);
  9479. + vchiq_dump(dump_context, buf, len + 1);
  9480. + }
  9481. + }
  9482. +
  9483. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  9484. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  9485. + debug_names[i], shared->debug[i], shared->debug[i]);
  9486. + vchiq_dump(dump_context, buf, len + 1);
  9487. + }
  9488. +}
  9489. +
  9490. +void
  9491. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  9492. +{
  9493. + char buf[80];
  9494. + int len;
  9495. + int i;
  9496. +
  9497. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  9498. + conn_state_names[state->conn_state]);
  9499. + vchiq_dump(dump_context, buf, len + 1);
  9500. +
  9501. + len = snprintf(buf, sizeof(buf),
  9502. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  9503. + state->local->tx_pos,
  9504. + (uint32_t)state->tx_data +
  9505. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  9506. + state->rx_pos,
  9507. + (uint32_t)state->rx_data +
  9508. + (state->rx_pos & VCHIQ_SLOT_MASK));
  9509. + vchiq_dump(dump_context, buf, len + 1);
  9510. +
  9511. + len = snprintf(buf, sizeof(buf),
  9512. + " Version: %d (min %d)",
  9513. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  9514. + vchiq_dump(dump_context, buf, len + 1);
  9515. +
  9516. + if (VCHIQ_ENABLE_STATS) {
  9517. + len = snprintf(buf, sizeof(buf),
  9518. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  9519. + "error_count=%d",
  9520. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  9521. + state->stats.error_count);
  9522. + vchiq_dump(dump_context, buf, len + 1);
  9523. + }
  9524. +
  9525. + len = snprintf(buf, sizeof(buf),
  9526. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  9527. + "(%d data)",
  9528. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  9529. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  9530. + state->data_quota - state->data_use_count,
  9531. + state->local->slot_queue_recycle - state->slot_queue_available,
  9532. + state->stats.slot_stalls, state->stats.data_stalls);
  9533. + vchiq_dump(dump_context, buf, len + 1);
  9534. +
  9535. + vchiq_dump_platform_state(dump_context);
  9536. +
  9537. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  9538. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  9539. +
  9540. + vchiq_dump_platform_instances(dump_context);
  9541. +
  9542. + for (i = 0; i < state->unused_service; i++) {
  9543. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  9544. +
  9545. + if (service) {
  9546. + vchiq_dump_service_state(dump_context, service);
  9547. + unlock_service(service);
  9548. + }
  9549. + }
  9550. +}
  9551. +
  9552. +void
  9553. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  9554. +{
  9555. + char buf[80];
  9556. + int len;
  9557. +
  9558. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  9559. + service->localport, srvstate_names[service->srvstate],
  9560. + service->ref_count - 1); /*Don't include the lock just taken*/
  9561. +
  9562. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  9563. + char remoteport[30];
  9564. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9565. + &service->state->service_quotas[service->localport];
  9566. + int fourcc = service->base.fourcc;
  9567. + int tx_pending, rx_pending;
  9568. + if (service->remoteport != VCHIQ_PORT_FREE) {
  9569. + int len2 = snprintf(remoteport, sizeof(remoteport),
  9570. + "%d", service->remoteport);
  9571. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  9572. + snprintf(remoteport + len2,
  9573. + sizeof(remoteport) - len2,
  9574. + " (client %x)", service->client_id);
  9575. + } else
  9576. + strcpy(remoteport, "n/a");
  9577. +
  9578. + len += snprintf(buf + len, sizeof(buf) - len,
  9579. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  9580. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  9581. + remoteport,
  9582. + service_quota->message_use_count,
  9583. + service_quota->message_quota,
  9584. + service_quota->slot_use_count,
  9585. + service_quota->slot_quota);
  9586. +
  9587. + vchiq_dump(dump_context, buf, len + 1);
  9588. +
  9589. + tx_pending = service->bulk_tx.local_insert -
  9590. + service->bulk_tx.remote_insert;
  9591. +
  9592. + rx_pending = service->bulk_rx.local_insert -
  9593. + service->bulk_rx.remote_insert;
  9594. +
  9595. + len = snprintf(buf, sizeof(buf),
  9596. + " Bulk: tx_pending=%d (size %d),"
  9597. + " rx_pending=%d (size %d)",
  9598. + tx_pending,
  9599. + tx_pending ? service->bulk_tx.bulks[
  9600. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  9601. + rx_pending,
  9602. + rx_pending ? service->bulk_rx.bulks[
  9603. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  9604. +
  9605. + if (VCHIQ_ENABLE_STATS) {
  9606. + vchiq_dump(dump_context, buf, len + 1);
  9607. +
  9608. + len = snprintf(buf, sizeof(buf),
  9609. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  9610. + "rx_count=%d, rx_bytes=%llu",
  9611. + service->stats.ctrl_tx_count,
  9612. + service->stats.ctrl_tx_bytes,
  9613. + service->stats.ctrl_rx_count,
  9614. + service->stats.ctrl_rx_bytes);
  9615. + vchiq_dump(dump_context, buf, len + 1);
  9616. +
  9617. + len = snprintf(buf, sizeof(buf),
  9618. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  9619. + "rx_count=%d, rx_bytes=%llu",
  9620. + service->stats.bulk_tx_count,
  9621. + service->stats.bulk_tx_bytes,
  9622. + service->stats.bulk_rx_count,
  9623. + service->stats.bulk_rx_bytes);
  9624. + vchiq_dump(dump_context, buf, len + 1);
  9625. +
  9626. + len = snprintf(buf, sizeof(buf),
  9627. + " %d quota stalls, %d slot stalls, "
  9628. + "%d bulk stalls, %d aborted, %d errors",
  9629. + service->stats.quota_stalls,
  9630. + service->stats.slot_stalls,
  9631. + service->stats.bulk_stalls,
  9632. + service->stats.bulk_aborted_count,
  9633. + service->stats.error_count);
  9634. + }
  9635. + }
  9636. +
  9637. + vchiq_dump(dump_context, buf, len + 1);
  9638. +
  9639. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  9640. + vchiq_dump_platform_service_state(dump_context, service);
  9641. +}
  9642. +
  9643. +
  9644. +void
  9645. +vchiq_loud_error_header(void)
  9646. +{
  9647. + vchiq_log_error(vchiq_core_log_level,
  9648. + "============================================================"
  9649. + "================");
  9650. + vchiq_log_error(vchiq_core_log_level,
  9651. + "============================================================"
  9652. + "================");
  9653. + vchiq_log_error(vchiq_core_log_level, "=====");
  9654. +}
  9655. +
  9656. +void
  9657. +vchiq_loud_error_footer(void)
  9658. +{
  9659. + vchiq_log_error(vchiq_core_log_level, "=====");
  9660. + vchiq_log_error(vchiq_core_log_level,
  9661. + "============================================================"
  9662. + "================");
  9663. + vchiq_log_error(vchiq_core_log_level,
  9664. + "============================================================"
  9665. + "================");
  9666. +}
  9667. +
  9668. +
  9669. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  9670. +{
  9671. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9672. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9673. + status = queue_message(state, NULL,
  9674. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  9675. + NULL, 0, 0, 0);
  9676. + return status;
  9677. +}
  9678. +
  9679. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  9680. +{
  9681. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9682. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9683. + status = queue_message(state, NULL,
  9684. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  9685. + NULL, 0, 0, 0);
  9686. + return status;
  9687. +}
  9688. +
  9689. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  9690. +{
  9691. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9692. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9693. + status = queue_message(state, NULL,
  9694. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  9695. + NULL, 0, 0, 0);
  9696. + return status;
  9697. +}
  9698. +
  9699. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  9700. + size_t numBytes)
  9701. +{
  9702. + const uint8_t *mem = (const uint8_t *)voidMem;
  9703. + size_t offset;
  9704. + char lineBuf[100];
  9705. + char *s;
  9706. +
  9707. + while (numBytes > 0) {
  9708. + s = lineBuf;
  9709. +
  9710. + for (offset = 0; offset < 16; offset++) {
  9711. + if (offset < numBytes)
  9712. + s += snprintf(s, 4, "%02x ", mem[offset]);
  9713. + else
  9714. + s += snprintf(s, 4, " ");
  9715. + }
  9716. +
  9717. + for (offset = 0; offset < 16; offset++) {
  9718. + if (offset < numBytes) {
  9719. + uint8_t ch = mem[offset];
  9720. +
  9721. + if ((ch < ' ') || (ch > '~'))
  9722. + ch = '.';
  9723. + *s++ = (char)ch;
  9724. + }
  9725. + }
  9726. + *s++ = '\0';
  9727. +
  9728. + if ((label != NULL) && (*label != '\0'))
  9729. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  9730. + "%s: %08x: %s", label, addr, lineBuf);
  9731. + else
  9732. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  9733. + "%08x: %s", addr, lineBuf);
  9734. +
  9735. + addr += 16;
  9736. + mem += 16;
  9737. + if (numBytes > 16)
  9738. + numBytes -= 16;
  9739. + else
  9740. + numBytes = 0;
  9741. + }
  9742. +}
  9743. --- /dev/null
  9744. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  9745. @@ -0,0 +1,712 @@
  9746. +/**
  9747. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  9748. + *
  9749. + * Redistribution and use in source and binary forms, with or without
  9750. + * modification, are permitted provided that the following conditions
  9751. + * are met:
  9752. + * 1. Redistributions of source code must retain the above copyright
  9753. + * notice, this list of conditions, and the following disclaimer,
  9754. + * without modification.
  9755. + * 2. Redistributions in binary form must reproduce the above copyright
  9756. + * notice, this list of conditions and the following disclaimer in the
  9757. + * documentation and/or other materials provided with the distribution.
  9758. + * 3. The names of the above-listed copyright holders may not be used
  9759. + * to endorse or promote products derived from this software without
  9760. + * specific prior written permission.
  9761. + *
  9762. + * ALTERNATIVELY, this software may be distributed under the terms of the
  9763. + * GNU General Public License ("GPL") version 2, as published by the Free
  9764. + * Software Foundation.
  9765. + *
  9766. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9767. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  9768. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  9769. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  9770. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9771. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9772. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9773. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  9774. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  9775. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  9776. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9777. + */
  9778. +
  9779. +#ifndef VCHIQ_CORE_H
  9780. +#define VCHIQ_CORE_H
  9781. +
  9782. +#include <linux/mutex.h>
  9783. +#include <linux/semaphore.h>
  9784. +#include <linux/kthread.h>
  9785. +
  9786. +#include "vchiq_cfg.h"
  9787. +
  9788. +#include "vchiq.h"
  9789. +
  9790. +/* Run time control of log level, based on KERN_XXX level. */
  9791. +#define VCHIQ_LOG_DEFAULT 4
  9792. +#define VCHIQ_LOG_ERROR 3
  9793. +#define VCHIQ_LOG_WARNING 4
  9794. +#define VCHIQ_LOG_INFO 6
  9795. +#define VCHIQ_LOG_TRACE 7
  9796. +
  9797. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  9798. +
  9799. +#ifndef vchiq_log_error
  9800. +#define vchiq_log_error(cat, fmt, ...) \
  9801. + do { if (cat >= VCHIQ_LOG_ERROR) \
  9802. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9803. +#endif
  9804. +#ifndef vchiq_log_warning
  9805. +#define vchiq_log_warning(cat, fmt, ...) \
  9806. + do { if (cat >= VCHIQ_LOG_WARNING) \
  9807. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9808. +#endif
  9809. +#ifndef vchiq_log_info
  9810. +#define vchiq_log_info(cat, fmt, ...) \
  9811. + do { if (cat >= VCHIQ_LOG_INFO) \
  9812. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9813. +#endif
  9814. +#ifndef vchiq_log_trace
  9815. +#define vchiq_log_trace(cat, fmt, ...) \
  9816. + do { if (cat >= VCHIQ_LOG_TRACE) \
  9817. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9818. +#endif
  9819. +
  9820. +#define vchiq_loud_error(...) \
  9821. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  9822. +
  9823. +#ifndef vchiq_static_assert
  9824. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  9825. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  9826. +#endif
  9827. +
  9828. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  9829. +
  9830. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  9831. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  9832. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  9833. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  9834. +
  9835. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  9836. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  9837. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  9838. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  9839. +
  9840. +#define VCHIQ_MSG_PADDING 0 /* - */
  9841. +#define VCHIQ_MSG_CONNECT 1 /* - */
  9842. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  9843. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  9844. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  9845. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  9846. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  9847. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  9848. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  9849. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  9850. +#define VCHIQ_MSG_PAUSE 10 /* - */
  9851. +#define VCHIQ_MSG_RESUME 11 /* - */
  9852. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  9853. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  9854. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  9855. +
  9856. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  9857. +#define VCHIQ_PORT_FREE 0x1000
  9858. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  9859. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  9860. + ((type<<24) | (srcport<<12) | (dstport<<0))
  9861. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  9862. +#define VCHIQ_MSG_SRCPORT(msgid) \
  9863. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  9864. +#define VCHIQ_MSG_DSTPORT(msgid) \
  9865. + ((unsigned short)msgid & 0xfff)
  9866. +
  9867. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  9868. + ((fourcc) >> 24) & 0xff, \
  9869. + ((fourcc) >> 16) & 0xff, \
  9870. + ((fourcc) >> 8) & 0xff, \
  9871. + (fourcc) & 0xff
  9872. +
  9873. +/* Ensure the fields are wide enough */
  9874. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  9875. + == 0);
  9876. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  9877. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  9878. + (unsigned int)VCHIQ_PORT_FREE);
  9879. +
  9880. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  9881. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  9882. +
  9883. +#define VCHIQ_FOURCC_INVALID 0x00000000
  9884. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  9885. +
  9886. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  9887. +
  9888. +typedef uint32_t BITSET_T;
  9889. +
  9890. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  9891. +
  9892. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  9893. +#define BITSET_WORD(b) (b >> 5)
  9894. +#define BITSET_BIT(b) (1 << (b & 31))
  9895. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  9896. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  9897. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  9898. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  9899. +
  9900. +#if VCHIQ_ENABLE_STATS
  9901. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  9902. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  9903. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  9904. + (service->stats. stat += addend)
  9905. +#else
  9906. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  9907. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  9908. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  9909. +#endif
  9910. +
  9911. +enum {
  9912. + DEBUG_ENTRIES,
  9913. +#if VCHIQ_ENABLE_DEBUG
  9914. + DEBUG_SLOT_HANDLER_COUNT,
  9915. + DEBUG_SLOT_HANDLER_LINE,
  9916. + DEBUG_PARSE_LINE,
  9917. + DEBUG_PARSE_HEADER,
  9918. + DEBUG_PARSE_MSGID,
  9919. + DEBUG_AWAIT_COMPLETION_LINE,
  9920. + DEBUG_DEQUEUE_MESSAGE_LINE,
  9921. + DEBUG_SERVICE_CALLBACK_LINE,
  9922. + DEBUG_MSG_QUEUE_FULL_COUNT,
  9923. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  9924. +#endif
  9925. + DEBUG_MAX
  9926. +};
  9927. +
  9928. +#if VCHIQ_ENABLE_DEBUG
  9929. +
  9930. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  9931. +#define DEBUG_TRACE(d) \
  9932. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  9933. +#define DEBUG_VALUE(d, v) \
  9934. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  9935. +#define DEBUG_COUNT(d) \
  9936. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  9937. +
  9938. +#else /* VCHIQ_ENABLE_DEBUG */
  9939. +
  9940. +#define DEBUG_INITIALISE(local)
  9941. +#define DEBUG_TRACE(d)
  9942. +#define DEBUG_VALUE(d, v)
  9943. +#define DEBUG_COUNT(d)
  9944. +
  9945. +#endif /* VCHIQ_ENABLE_DEBUG */
  9946. +
  9947. +typedef enum {
  9948. + VCHIQ_CONNSTATE_DISCONNECTED,
  9949. + VCHIQ_CONNSTATE_CONNECTING,
  9950. + VCHIQ_CONNSTATE_CONNECTED,
  9951. + VCHIQ_CONNSTATE_PAUSING,
  9952. + VCHIQ_CONNSTATE_PAUSE_SENT,
  9953. + VCHIQ_CONNSTATE_PAUSED,
  9954. + VCHIQ_CONNSTATE_RESUMING,
  9955. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  9956. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  9957. +} VCHIQ_CONNSTATE_T;
  9958. +
  9959. +enum {
  9960. + VCHIQ_SRVSTATE_FREE,
  9961. + VCHIQ_SRVSTATE_HIDDEN,
  9962. + VCHIQ_SRVSTATE_LISTENING,
  9963. + VCHIQ_SRVSTATE_OPENING,
  9964. + VCHIQ_SRVSTATE_OPEN,
  9965. + VCHIQ_SRVSTATE_OPENSYNC,
  9966. + VCHIQ_SRVSTATE_CLOSESENT,
  9967. + VCHIQ_SRVSTATE_CLOSERECVD,
  9968. + VCHIQ_SRVSTATE_CLOSEWAIT,
  9969. + VCHIQ_SRVSTATE_CLOSED
  9970. +};
  9971. +
  9972. +enum {
  9973. + VCHIQ_POLL_TERMINATE,
  9974. + VCHIQ_POLL_REMOVE,
  9975. + VCHIQ_POLL_TXNOTIFY,
  9976. + VCHIQ_POLL_RXNOTIFY,
  9977. + VCHIQ_POLL_COUNT
  9978. +};
  9979. +
  9980. +typedef enum {
  9981. + VCHIQ_BULK_TRANSMIT,
  9982. + VCHIQ_BULK_RECEIVE
  9983. +} VCHIQ_BULK_DIR_T;
  9984. +
  9985. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  9986. +
  9987. +typedef struct vchiq_bulk_struct {
  9988. + short mode;
  9989. + short dir;
  9990. + void *userdata;
  9991. + VCHI_MEM_HANDLE_T handle;
  9992. + void *data;
  9993. + int size;
  9994. + void *remote_data;
  9995. + int remote_size;
  9996. + int actual;
  9997. +} VCHIQ_BULK_T;
  9998. +
  9999. +typedef struct vchiq_bulk_queue_struct {
  10000. + int local_insert; /* Where to insert the next local bulk */
  10001. + int remote_insert; /* Where to insert the next remote bulk (master) */
  10002. + int process; /* Bulk to transfer next */
  10003. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  10004. + int remove; /* Bulk to notify the local client of, and remove,
  10005. + ** next */
  10006. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  10007. +} VCHIQ_BULK_QUEUE_T;
  10008. +
  10009. +typedef struct remote_event_struct {
  10010. + int armed;
  10011. + int fired;
  10012. + struct semaphore *event;
  10013. +} REMOTE_EVENT_T;
  10014. +
  10015. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  10016. +
  10017. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  10018. +
  10019. +typedef struct vchiq_slot_struct {
  10020. + char data[VCHIQ_SLOT_SIZE];
  10021. +} VCHIQ_SLOT_T;
  10022. +
  10023. +typedef struct vchiq_slot_info_struct {
  10024. + /* Use two counters rather than one to avoid the need for a mutex. */
  10025. + short use_count;
  10026. + short release_count;
  10027. +} VCHIQ_SLOT_INFO_T;
  10028. +
  10029. +typedef struct vchiq_service_struct {
  10030. + VCHIQ_SERVICE_BASE_T base;
  10031. + VCHIQ_SERVICE_HANDLE_T handle;
  10032. + unsigned int ref_count;
  10033. + int srvstate;
  10034. + VCHIQ_USERDATA_TERM_T userdata_term;
  10035. + unsigned int localport;
  10036. + unsigned int remoteport;
  10037. + int public_fourcc;
  10038. + int client_id;
  10039. + char auto_close;
  10040. + char sync;
  10041. + char closing;
  10042. + char trace;
  10043. + atomic_t poll_flags;
  10044. + short version;
  10045. + short version_min;
  10046. + short peer_version;
  10047. +
  10048. + VCHIQ_STATE_T *state;
  10049. + VCHIQ_INSTANCE_T instance;
  10050. +
  10051. + int service_use_count;
  10052. +
  10053. + VCHIQ_BULK_QUEUE_T bulk_tx;
  10054. + VCHIQ_BULK_QUEUE_T bulk_rx;
  10055. +
  10056. + struct semaphore remove_event;
  10057. + struct semaphore bulk_remove_event;
  10058. + struct mutex bulk_mutex;
  10059. +
  10060. + struct service_stats_struct {
  10061. + int quota_stalls;
  10062. + int slot_stalls;
  10063. + int bulk_stalls;
  10064. + int error_count;
  10065. + int ctrl_tx_count;
  10066. + int ctrl_rx_count;
  10067. + int bulk_tx_count;
  10068. + int bulk_rx_count;
  10069. + int bulk_aborted_count;
  10070. + uint64_t ctrl_tx_bytes;
  10071. + uint64_t ctrl_rx_bytes;
  10072. + uint64_t bulk_tx_bytes;
  10073. + uint64_t bulk_rx_bytes;
  10074. + } stats;
  10075. +} VCHIQ_SERVICE_T;
  10076. +
  10077. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  10078. + statically allocated, since for accounting reasons a service's slot
  10079. + usage is carried over between users of the same port number.
  10080. + */
  10081. +typedef struct vchiq_service_quota_struct {
  10082. + unsigned short slot_quota;
  10083. + unsigned short slot_use_count;
  10084. + unsigned short message_quota;
  10085. + unsigned short message_use_count;
  10086. + struct semaphore quota_event;
  10087. + int previous_tx_index;
  10088. +} VCHIQ_SERVICE_QUOTA_T;
  10089. +
  10090. +typedef struct vchiq_shared_state_struct {
  10091. +
  10092. + /* A non-zero value here indicates that the content is valid. */
  10093. + int initialised;
  10094. +
  10095. + /* The first and last (inclusive) slots allocated to the owner. */
  10096. + int slot_first;
  10097. + int slot_last;
  10098. +
  10099. + /* The slot allocated to synchronous messages from the owner. */
  10100. + int slot_sync;
  10101. +
  10102. + /* Signalling this event indicates that owner's slot handler thread
  10103. + ** should run. */
  10104. + REMOTE_EVENT_T trigger;
  10105. +
  10106. + /* Indicates the byte position within the stream where the next message
  10107. + ** will be written. The least significant bits are an index into the
  10108. + ** slot. The next bits are the index of the slot in slot_queue. */
  10109. + int tx_pos;
  10110. +
  10111. + /* This event should be signalled when a slot is recycled. */
  10112. + REMOTE_EVENT_T recycle;
  10113. +
  10114. + /* The slot_queue index where the next recycled slot will be written. */
  10115. + int slot_queue_recycle;
  10116. +
  10117. + /* This event should be signalled when a synchronous message is sent. */
  10118. + REMOTE_EVENT_T sync_trigger;
  10119. +
  10120. + /* This event should be signalled when a synchronous message has been
  10121. + ** released. */
  10122. + REMOTE_EVENT_T sync_release;
  10123. +
  10124. + /* A circular buffer of slot indexes. */
  10125. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  10126. +
  10127. + /* Debugging state */
  10128. + int debug[DEBUG_MAX];
  10129. +} VCHIQ_SHARED_STATE_T;
  10130. +
  10131. +typedef struct vchiq_slot_zero_struct {
  10132. + int magic;
  10133. + short version;
  10134. + short version_min;
  10135. + int slot_zero_size;
  10136. + int slot_size;
  10137. + int max_slots;
  10138. + int max_slots_per_side;
  10139. + int platform_data[2];
  10140. + VCHIQ_SHARED_STATE_T master;
  10141. + VCHIQ_SHARED_STATE_T slave;
  10142. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  10143. +} VCHIQ_SLOT_ZERO_T;
  10144. +
  10145. +struct vchiq_state_struct {
  10146. + int id;
  10147. + int initialised;
  10148. + VCHIQ_CONNSTATE_T conn_state;
  10149. + int is_master;
  10150. + short version_common;
  10151. +
  10152. + VCHIQ_SHARED_STATE_T *local;
  10153. + VCHIQ_SHARED_STATE_T *remote;
  10154. + VCHIQ_SLOT_T *slot_data;
  10155. +
  10156. + unsigned short default_slot_quota;
  10157. + unsigned short default_message_quota;
  10158. +
  10159. + /* Event indicating connect message received */
  10160. + struct semaphore connect;
  10161. +
  10162. + /* Mutex protecting services */
  10163. + struct mutex mutex;
  10164. + VCHIQ_INSTANCE_T *instance;
  10165. +
  10166. + /* Processes incoming messages */
  10167. + struct task_struct *slot_handler_thread;
  10168. +
  10169. + /* Processes recycled slots */
  10170. + struct task_struct *recycle_thread;
  10171. +
  10172. + /* Processes synchronous messages */
  10173. + struct task_struct *sync_thread;
  10174. +
  10175. + /* Local implementation of the trigger remote event */
  10176. + struct semaphore trigger_event;
  10177. +
  10178. + /* Local implementation of the recycle remote event */
  10179. + struct semaphore recycle_event;
  10180. +
  10181. + /* Local implementation of the sync trigger remote event */
  10182. + struct semaphore sync_trigger_event;
  10183. +
  10184. + /* Local implementation of the sync release remote event */
  10185. + struct semaphore sync_release_event;
  10186. +
  10187. + char *tx_data;
  10188. + char *rx_data;
  10189. + VCHIQ_SLOT_INFO_T *rx_info;
  10190. +
  10191. + struct mutex slot_mutex;
  10192. +
  10193. + struct mutex recycle_mutex;
  10194. +
  10195. + struct mutex sync_mutex;
  10196. +
  10197. + struct mutex bulk_transfer_mutex;
  10198. +
  10199. + /* Indicates the byte position within the stream from where the next
  10200. + ** message will be read. The least significant bits are an index into
  10201. + ** the slot.The next bits are the index of the slot in
  10202. + ** remote->slot_queue. */
  10203. + int rx_pos;
  10204. +
  10205. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  10206. + from remote->tx_pos. */
  10207. + int local_tx_pos;
  10208. +
  10209. + /* The slot_queue index of the slot to become available next. */
  10210. + int slot_queue_available;
  10211. +
  10212. + /* A flag to indicate if any poll has been requested */
  10213. + int poll_needed;
  10214. +
  10215. + /* Ths index of the previous slot used for data messages. */
  10216. + int previous_data_index;
  10217. +
  10218. + /* The number of slots occupied by data messages. */
  10219. + unsigned short data_use_count;
  10220. +
  10221. + /* The maximum number of slots to be occupied by data messages. */
  10222. + unsigned short data_quota;
  10223. +
  10224. + /* An array of bit sets indicating which services must be polled. */
  10225. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  10226. +
  10227. + /* The number of the first unused service */
  10228. + int unused_service;
  10229. +
  10230. + /* Signalled when a free slot becomes available. */
  10231. + struct semaphore slot_available_event;
  10232. +
  10233. + struct semaphore slot_remove_event;
  10234. +
  10235. + /* Signalled when a free data slot becomes available. */
  10236. + struct semaphore data_quota_event;
  10237. +
  10238. + /* Incremented when there are bulk transfers which cannot be processed
  10239. + * whilst paused and must be processed on resume */
  10240. + int deferred_bulks;
  10241. +
  10242. + struct state_stats_struct {
  10243. + int slot_stalls;
  10244. + int data_stalls;
  10245. + int ctrl_tx_count;
  10246. + int ctrl_rx_count;
  10247. + int error_count;
  10248. + } stats;
  10249. +
  10250. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  10251. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  10252. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  10253. +
  10254. + VCHIQ_PLATFORM_STATE_T platform_state;
  10255. +};
  10256. +
  10257. +struct bulk_waiter {
  10258. + VCHIQ_BULK_T *bulk;
  10259. + struct semaphore event;
  10260. + int actual;
  10261. +};
  10262. +
  10263. +extern spinlock_t bulk_waiter_spinlock;
  10264. +
  10265. +extern int vchiq_core_log_level;
  10266. +extern int vchiq_core_msg_log_level;
  10267. +extern int vchiq_sync_log_level;
  10268. +
  10269. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  10270. +
  10271. +extern const char *
  10272. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  10273. +
  10274. +extern VCHIQ_SLOT_ZERO_T *
  10275. +vchiq_init_slots(void *mem_base, int mem_size);
  10276. +
  10277. +extern VCHIQ_STATUS_T
  10278. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  10279. + int is_master);
  10280. +
  10281. +extern VCHIQ_STATUS_T
  10282. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  10283. +
  10284. +extern VCHIQ_SERVICE_T *
  10285. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  10286. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  10287. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  10288. +
  10289. +extern VCHIQ_STATUS_T
  10290. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  10291. +
  10292. +extern VCHIQ_STATUS_T
  10293. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  10294. +
  10295. +extern void
  10296. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  10297. +
  10298. +extern void
  10299. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  10300. +
  10301. +extern VCHIQ_STATUS_T
  10302. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  10303. +
  10304. +extern VCHIQ_STATUS_T
  10305. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  10306. +
  10307. +extern VCHIQ_STATUS_T
  10308. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  10309. +
  10310. +extern void
  10311. +remote_event_pollall(VCHIQ_STATE_T *state);
  10312. +
  10313. +extern VCHIQ_STATUS_T
  10314. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  10315. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  10316. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  10317. +
  10318. +extern void
  10319. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  10320. +
  10321. +extern void
  10322. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  10323. +
  10324. +extern void
  10325. +vchiq_loud_error_header(void);
  10326. +
  10327. +extern void
  10328. +vchiq_loud_error_footer(void);
  10329. +
  10330. +extern void
  10331. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  10332. +
  10333. +static inline VCHIQ_SERVICE_T *
  10334. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  10335. +{
  10336. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  10337. + (VCHIQ_MAX_STATES - 1)];
  10338. + if (!state)
  10339. + return NULL;
  10340. +
  10341. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  10342. +}
  10343. +
  10344. +extern VCHIQ_SERVICE_T *
  10345. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  10346. +
  10347. +extern VCHIQ_SERVICE_T *
  10348. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  10349. +
  10350. +extern VCHIQ_SERVICE_T *
  10351. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  10352. + VCHIQ_SERVICE_HANDLE_T handle);
  10353. +
  10354. +extern VCHIQ_SERVICE_T *
  10355. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  10356. + VCHIQ_SERVICE_HANDLE_T handle);
  10357. +
  10358. +extern VCHIQ_SERVICE_T *
  10359. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  10360. + int *pidx);
  10361. +
  10362. +extern void
  10363. +lock_service(VCHIQ_SERVICE_T *service);
  10364. +
  10365. +extern void
  10366. +unlock_service(VCHIQ_SERVICE_T *service);
  10367. +
  10368. +/* The following functions are called from vchiq_core, and external
  10369. +** implementations must be provided. */
  10370. +
  10371. +extern VCHIQ_STATUS_T
  10372. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  10373. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  10374. +
  10375. +extern void
  10376. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  10377. +
  10378. +extern void
  10379. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  10380. +
  10381. +extern VCHIQ_STATUS_T
  10382. +vchiq_copy_from_user(void *dst, const void *src, int size);
  10383. +
  10384. +extern void
  10385. +remote_event_signal(REMOTE_EVENT_T *event);
  10386. +
  10387. +void
  10388. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  10389. +
  10390. +extern void
  10391. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  10392. +
  10393. +extern VCHIQ_STATUS_T
  10394. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  10395. +
  10396. +extern void
  10397. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  10398. +
  10399. +extern void
  10400. +vchiq_dump(void *dump_context, const char *str, int len);
  10401. +
  10402. +extern void
  10403. +vchiq_dump_platform_state(void *dump_context);
  10404. +
  10405. +extern void
  10406. +vchiq_dump_platform_instances(void *dump_context);
  10407. +
  10408. +extern void
  10409. +vchiq_dump_platform_service_state(void *dump_context,
  10410. + VCHIQ_SERVICE_T *service);
  10411. +
  10412. +extern VCHIQ_STATUS_T
  10413. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  10414. +
  10415. +extern VCHIQ_STATUS_T
  10416. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  10417. +
  10418. +extern void
  10419. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  10420. +
  10421. +extern void
  10422. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  10423. +
  10424. +extern VCHIQ_STATUS_T
  10425. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  10426. +
  10427. +extern VCHIQ_STATUS_T
  10428. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  10429. +
  10430. +extern void
  10431. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  10432. +
  10433. +extern VCHIQ_STATUS_T
  10434. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  10435. +
  10436. +extern VCHIQ_STATUS_T
  10437. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  10438. +
  10439. +extern VCHIQ_STATUS_T
  10440. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  10441. +
  10442. +extern void
  10443. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  10444. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  10445. +
  10446. +extern void
  10447. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  10448. +
  10449. +extern void
  10450. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  10451. +
  10452. +
  10453. +extern void
  10454. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  10455. + size_t numBytes);
  10456. +
  10457. +#endif
  10458. --- /dev/null
  10459. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
  10460. @@ -0,0 +1,383 @@
  10461. +/**
  10462. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  10463. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  10464. + *
  10465. + * Redistribution and use in source and binary forms, with or without
  10466. + * modification, are permitted provided that the following conditions
  10467. + * are met:
  10468. + * 1. Redistributions of source code must retain the above copyright
  10469. + * notice, this list of conditions, and the following disclaimer,
  10470. + * without modification.
  10471. + * 2. Redistributions in binary form must reproduce the above copyright
  10472. + * notice, this list of conditions and the following disclaimer in the
  10473. + * documentation and/or other materials provided with the distribution.
  10474. + * 3. The names of the above-listed copyright holders may not be used
  10475. + * to endorse or promote products derived from this software without
  10476. + * specific prior written permission.
  10477. + *
  10478. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10479. + * GNU General Public License ("GPL") version 2, as published by the Free
  10480. + * Software Foundation.
  10481. + *
  10482. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10483. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10484. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10485. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10486. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10487. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10488. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10489. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10490. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10491. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10492. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10493. + */
  10494. +
  10495. +
  10496. +#include <linux/debugfs.h>
  10497. +#include "vchiq_core.h"
  10498. +#include "vchiq_arm.h"
  10499. +#include "vchiq_debugfs.h"
  10500. +
  10501. +#ifdef CONFIG_DEBUG_FS
  10502. +
  10503. +/****************************************************************************
  10504. +*
  10505. +* log category entries
  10506. +*
  10507. +***************************************************************************/
  10508. +#define DEBUGFS_WRITE_BUF_SIZE 256
  10509. +
  10510. +#define VCHIQ_LOG_ERROR_STR "error"
  10511. +#define VCHIQ_LOG_WARNING_STR "warning"
  10512. +#define VCHIQ_LOG_INFO_STR "info"
  10513. +#define VCHIQ_LOG_TRACE_STR "trace"
  10514. +
  10515. +
  10516. +/* Top-level debug info */
  10517. +struct vchiq_debugfs_info {
  10518. + /* Global 'vchiq' debugfs entry used by all instances */
  10519. + struct dentry *vchiq_cfg_dir;
  10520. +
  10521. + /* one entry per client process */
  10522. + struct dentry *clients;
  10523. +
  10524. + /* log categories */
  10525. + struct dentry *log_categories;
  10526. +};
  10527. +
  10528. +static struct vchiq_debugfs_info debugfs_info;
  10529. +
  10530. +/* Log category debugfs entries */
  10531. +struct vchiq_debugfs_log_entry {
  10532. + const char *name;
  10533. + int *plevel;
  10534. + struct dentry *dir;
  10535. +};
  10536. +
  10537. +static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
  10538. + { "core", &vchiq_core_log_level },
  10539. + { "msg", &vchiq_core_msg_log_level },
  10540. + { "sync", &vchiq_sync_log_level },
  10541. + { "susp", &vchiq_susp_log_level },
  10542. + { "arm", &vchiq_arm_log_level },
  10543. +};
  10544. +static int n_log_entries =
  10545. + sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
  10546. +
  10547. +
  10548. +static struct dentry *vchiq_clients_top(void);
  10549. +static struct dentry *vchiq_debugfs_top(void);
  10550. +
  10551. +static int debugfs_log_show(struct seq_file *f, void *offset)
  10552. +{
  10553. + int *levp = f->private;
  10554. + char *log_value = NULL;
  10555. +
  10556. + switch (*levp) {
  10557. + case VCHIQ_LOG_ERROR:
  10558. + log_value = VCHIQ_LOG_ERROR_STR;
  10559. + break;
  10560. + case VCHIQ_LOG_WARNING:
  10561. + log_value = VCHIQ_LOG_WARNING_STR;
  10562. + break;
  10563. + case VCHIQ_LOG_INFO:
  10564. + log_value = VCHIQ_LOG_INFO_STR;
  10565. + break;
  10566. + case VCHIQ_LOG_TRACE:
  10567. + log_value = VCHIQ_LOG_TRACE_STR;
  10568. + break;
  10569. + default:
  10570. + break;
  10571. + }
  10572. +
  10573. + seq_printf(f, "%s\n", log_value ? log_value : "(null)");
  10574. +
  10575. + return 0;
  10576. +}
  10577. +
  10578. +static int debugfs_log_open(struct inode *inode, struct file *file)
  10579. +{
  10580. + return single_open(file, debugfs_log_show, inode->i_private);
  10581. +}
  10582. +
  10583. +static int debugfs_log_write(struct file *file,
  10584. + const char __user *buffer,
  10585. + size_t count, loff_t *ppos)
  10586. +{
  10587. + struct seq_file *f = (struct seq_file *)file->private_data;
  10588. + int *levp = f->private;
  10589. + char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
  10590. +
  10591. + memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
  10592. + if (count >= DEBUGFS_WRITE_BUF_SIZE)
  10593. + count = DEBUGFS_WRITE_BUF_SIZE;
  10594. +
  10595. + if (copy_from_user(kbuf, buffer, count) != 0)
  10596. + return -EFAULT;
  10597. + kbuf[count - 1] = 0;
  10598. +
  10599. + if (strncmp("error", kbuf, strlen("error")) == 0)
  10600. + *levp = VCHIQ_LOG_ERROR;
  10601. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  10602. + *levp = VCHIQ_LOG_WARNING;
  10603. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  10604. + *levp = VCHIQ_LOG_INFO;
  10605. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  10606. + *levp = VCHIQ_LOG_TRACE;
  10607. + else
  10608. + *levp = VCHIQ_LOG_DEFAULT;
  10609. +
  10610. + *ppos += count;
  10611. +
  10612. + return count;
  10613. +}
  10614. +
  10615. +static const struct file_operations debugfs_log_fops = {
  10616. + .owner = THIS_MODULE,
  10617. + .open = debugfs_log_open,
  10618. + .write = debugfs_log_write,
  10619. + .read = seq_read,
  10620. + .llseek = seq_lseek,
  10621. + .release = single_release,
  10622. +};
  10623. +
  10624. +/* create an entry under <debugfs>/vchiq/log for each log category */
  10625. +static int vchiq_debugfs_create_log_entries(struct dentry *top)
  10626. +{
  10627. + struct dentry *dir;
  10628. + size_t i;
  10629. + int ret = 0;
  10630. + dir = debugfs_create_dir("log", vchiq_debugfs_top());
  10631. + if (!dir)
  10632. + return -ENOMEM;
  10633. + debugfs_info.log_categories = dir;
  10634. +
  10635. + for (i = 0; i < n_log_entries; i++) {
  10636. + void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
  10637. + dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
  10638. + 0644,
  10639. + debugfs_info.log_categories,
  10640. + levp,
  10641. + &debugfs_log_fops);
  10642. + if (!dir) {
  10643. + ret = -ENOMEM;
  10644. + break;
  10645. + }
  10646. +
  10647. + vchiq_debugfs_log_entries[i].dir = dir;
  10648. + }
  10649. + return ret;
  10650. +}
  10651. +
  10652. +static int debugfs_usecount_show(struct seq_file *f, void *offset)
  10653. +{
  10654. + VCHIQ_INSTANCE_T instance = f->private;
  10655. + int use_count;
  10656. +
  10657. + use_count = vchiq_instance_get_use_count(instance);
  10658. + seq_printf(f, "%d\n", use_count);
  10659. +
  10660. + return 0;
  10661. +}
  10662. +
  10663. +static int debugfs_usecount_open(struct inode *inode, struct file *file)
  10664. +{
  10665. + return single_open(file, debugfs_usecount_show, inode->i_private);
  10666. +}
  10667. +
  10668. +static const struct file_operations debugfs_usecount_fops = {
  10669. + .owner = THIS_MODULE,
  10670. + .open = debugfs_usecount_open,
  10671. + .read = seq_read,
  10672. + .llseek = seq_lseek,
  10673. + .release = single_release,
  10674. +};
  10675. +
  10676. +static int debugfs_trace_show(struct seq_file *f, void *offset)
  10677. +{
  10678. + VCHIQ_INSTANCE_T instance = f->private;
  10679. + int trace;
  10680. +
  10681. + trace = vchiq_instance_get_trace(instance);
  10682. + seq_printf(f, "%s\n", trace ? "Y" : "N");
  10683. +
  10684. + return 0;
  10685. +}
  10686. +
  10687. +static int debugfs_trace_open(struct inode *inode, struct file *file)
  10688. +{
  10689. + return single_open(file, debugfs_trace_show, inode->i_private);
  10690. +}
  10691. +
  10692. +static int debugfs_trace_write(struct file *file,
  10693. + const char __user *buffer,
  10694. + size_t count, loff_t *ppos)
  10695. +{
  10696. + struct seq_file *f = (struct seq_file *)file->private_data;
  10697. + VCHIQ_INSTANCE_T instance = f->private;
  10698. + char firstchar;
  10699. +
  10700. + if (copy_from_user(&firstchar, buffer, 1) != 0)
  10701. + return -EFAULT;
  10702. +
  10703. + switch (firstchar) {
  10704. + case 'Y':
  10705. + case 'y':
  10706. + case '1':
  10707. + vchiq_instance_set_trace(instance, 1);
  10708. + break;
  10709. + case 'N':
  10710. + case 'n':
  10711. + case '0':
  10712. + vchiq_instance_set_trace(instance, 0);
  10713. + break;
  10714. + default:
  10715. + break;
  10716. + }
  10717. +
  10718. + *ppos += count;
  10719. +
  10720. + return count;
  10721. +}
  10722. +
  10723. +static const struct file_operations debugfs_trace_fops = {
  10724. + .owner = THIS_MODULE,
  10725. + .open = debugfs_trace_open,
  10726. + .write = debugfs_trace_write,
  10727. + .read = seq_read,
  10728. + .llseek = seq_lseek,
  10729. + .release = single_release,
  10730. +};
  10731. +
  10732. +/* add an instance (process) to the debugfs entries */
  10733. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  10734. +{
  10735. + char pidstr[16];
  10736. + struct dentry *top, *use_count, *trace;
  10737. + struct dentry *clients = vchiq_clients_top();
  10738. +
  10739. + snprintf(pidstr, sizeof(pidstr), "%d",
  10740. + vchiq_instance_get_pid(instance));
  10741. +
  10742. + top = debugfs_create_dir(pidstr, clients);
  10743. + if (!top)
  10744. + goto fail_top;
  10745. +
  10746. + use_count = debugfs_create_file("use_count",
  10747. + 0444, top,
  10748. + instance,
  10749. + &debugfs_usecount_fops);
  10750. + if (!use_count)
  10751. + goto fail_use_count;
  10752. +
  10753. + trace = debugfs_create_file("trace",
  10754. + 0644, top,
  10755. + instance,
  10756. + &debugfs_trace_fops);
  10757. + if (!trace)
  10758. + goto fail_trace;
  10759. +
  10760. + vchiq_instance_get_debugfs_node(instance)->dentry = top;
  10761. +
  10762. + return 0;
  10763. +
  10764. +fail_trace:
  10765. + debugfs_remove(use_count);
  10766. +fail_use_count:
  10767. + debugfs_remove(top);
  10768. +fail_top:
  10769. + return -ENOMEM;
  10770. +}
  10771. +
  10772. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  10773. +{
  10774. + VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
  10775. + debugfs_remove_recursive(node->dentry);
  10776. +}
  10777. +
  10778. +
  10779. +int vchiq_debugfs_init(void)
  10780. +{
  10781. + BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
  10782. +
  10783. + debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
  10784. + if (debugfs_info.vchiq_cfg_dir == NULL)
  10785. + goto fail;
  10786. +
  10787. + debugfs_info.clients = debugfs_create_dir("clients",
  10788. + vchiq_debugfs_top());
  10789. + if (!debugfs_info.clients)
  10790. + goto fail;
  10791. +
  10792. + if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
  10793. + goto fail;
  10794. +
  10795. + return 0;
  10796. +
  10797. +fail:
  10798. + vchiq_debugfs_deinit();
  10799. + vchiq_log_error(vchiq_arm_log_level,
  10800. + "%s: failed to create debugfs directory",
  10801. + __func__);
  10802. +
  10803. + return -ENOMEM;
  10804. +}
  10805. +
  10806. +/* remove all the debugfs entries */
  10807. +void vchiq_debugfs_deinit(void)
  10808. +{
  10809. + debugfs_remove_recursive(vchiq_debugfs_top());
  10810. +}
  10811. +
  10812. +static struct dentry *vchiq_clients_top(void)
  10813. +{
  10814. + return debugfs_info.clients;
  10815. +}
  10816. +
  10817. +static struct dentry *vchiq_debugfs_top(void)
  10818. +{
  10819. + BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
  10820. + return debugfs_info.vchiq_cfg_dir;
  10821. +}
  10822. +
  10823. +#else /* CONFIG_DEBUG_FS */
  10824. +
  10825. +int vchiq_debugfs_init(void)
  10826. +{
  10827. + return 0;
  10828. +}
  10829. +
  10830. +void vchiq_debugfs_deinit(void)
  10831. +{
  10832. +}
  10833. +
  10834. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  10835. +{
  10836. + return 0;
  10837. +}
  10838. +
  10839. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  10840. +{
  10841. +}
  10842. +
  10843. +#endif /* CONFIG_DEBUG_FS */
  10844. --- /dev/null
  10845. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
  10846. @@ -0,0 +1,52 @@
  10847. +/**
  10848. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  10849. + *
  10850. + * Redistribution and use in source and binary forms, with or without
  10851. + * modification, are permitted provided that the following conditions
  10852. + * are met:
  10853. + * 1. Redistributions of source code must retain the above copyright
  10854. + * notice, this list of conditions, and the following disclaimer,
  10855. + * without modification.
  10856. + * 2. Redistributions in binary form must reproduce the above copyright
  10857. + * notice, this list of conditions and the following disclaimer in the
  10858. + * documentation and/or other materials provided with the distribution.
  10859. + * 3. The names of the above-listed copyright holders may not be used
  10860. + * to endorse or promote products derived from this software without
  10861. + * specific prior written permission.
  10862. + *
  10863. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10864. + * GNU General Public License ("GPL") version 2, as published by the Free
  10865. + * Software Foundation.
  10866. + *
  10867. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10868. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10869. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10870. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10871. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10872. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10873. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10874. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10875. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10876. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10877. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10878. + */
  10879. +
  10880. +#ifndef VCHIQ_DEBUGFS_H
  10881. +#define VCHIQ_DEBUGFS_H
  10882. +
  10883. +#include "vchiq_core.h"
  10884. +
  10885. +typedef struct vchiq_debugfs_node_struct
  10886. +{
  10887. + struct dentry *dentry;
  10888. +} VCHIQ_DEBUGFS_NODE_T;
  10889. +
  10890. +int vchiq_debugfs_init(void);
  10891. +
  10892. +void vchiq_debugfs_deinit(void);
  10893. +
  10894. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance);
  10895. +
  10896. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance);
  10897. +
  10898. +#endif /* VCHIQ_DEBUGFS_H */
  10899. --- /dev/null
  10900. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  10901. @@ -0,0 +1,87 @@
  10902. +#!/usr/bin/perl -w
  10903. +
  10904. +use strict;
  10905. +
  10906. +#
  10907. +# Generate a version from available information
  10908. +#
  10909. +
  10910. +my $prefix = shift @ARGV;
  10911. +my $root = shift @ARGV;
  10912. +
  10913. +
  10914. +if ( not defined $root ) {
  10915. + die "usage: $0 prefix root-dir\n";
  10916. +}
  10917. +
  10918. +if ( ! -d $root ) {
  10919. + die "root directory $root not found\n";
  10920. +}
  10921. +
  10922. +my $version = "unknown";
  10923. +my $tainted = "";
  10924. +
  10925. +if ( -d "$root/.git" ) {
  10926. + # attempt to work out git version. only do so
  10927. + # on a linux build host, as cygwin builds are
  10928. + # already slow enough
  10929. +
  10930. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  10931. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  10932. + $version = "no git version";
  10933. + }
  10934. + else {
  10935. + $version = <F>;
  10936. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10937. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10938. + }
  10939. +
  10940. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  10941. + $tainted = <G>;
  10942. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10943. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10944. + if (length $tainted) {
  10945. + $version = join ' ', $version, "(tainted)";
  10946. + }
  10947. + else {
  10948. + $version = join ' ', $version, "(clean)";
  10949. + }
  10950. + }
  10951. + }
  10952. +}
  10953. +
  10954. +my $hostname = `hostname`;
  10955. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10956. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10957. +
  10958. +
  10959. +print STDERR "Version $version\n";
  10960. +print <<EOF;
  10961. +#include "${prefix}_build_info.h"
  10962. +#include <linux/broadcom/vc_debug_sym.h>
  10963. +
  10964. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  10965. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  10966. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  10967. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  10968. +
  10969. +const char *vchiq_get_build_hostname( void )
  10970. +{
  10971. + return vchiq_build_hostname;
  10972. +}
  10973. +
  10974. +const char *vchiq_get_build_version( void )
  10975. +{
  10976. + return vchiq_build_version;
  10977. +}
  10978. +
  10979. +const char *vchiq_get_build_date( void )
  10980. +{
  10981. + return vchiq_build_date;
  10982. +}
  10983. +
  10984. +const char *vchiq_get_build_time( void )
  10985. +{
  10986. + return vchiq_build_time;
  10987. +}
  10988. +EOF
  10989. --- /dev/null
  10990. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  10991. @@ -0,0 +1,189 @@
  10992. +/**
  10993. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  10994. + *
  10995. + * Redistribution and use in source and binary forms, with or without
  10996. + * modification, are permitted provided that the following conditions
  10997. + * are met:
  10998. + * 1. Redistributions of source code must retain the above copyright
  10999. + * notice, this list of conditions, and the following disclaimer,
  11000. + * without modification.
  11001. + * 2. Redistributions in binary form must reproduce the above copyright
  11002. + * notice, this list of conditions and the following disclaimer in the
  11003. + * documentation and/or other materials provided with the distribution.
  11004. + * 3. The names of the above-listed copyright holders may not be used
  11005. + * to endorse or promote products derived from this software without
  11006. + * specific prior written permission.
  11007. + *
  11008. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11009. + * GNU General Public License ("GPL") version 2, as published by the Free
  11010. + * Software Foundation.
  11011. + *
  11012. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11013. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11014. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11015. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11016. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11017. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11018. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11019. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11020. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11021. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11022. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11023. + */
  11024. +
  11025. +#ifndef VCHIQ_IF_H
  11026. +#define VCHIQ_IF_H
  11027. +
  11028. +#include "interface/vchi/vchi_mh.h"
  11029. +
  11030. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  11031. +
  11032. +#define VCHIQ_SLOT_SIZE 4096
  11033. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  11034. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  11035. +
  11036. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  11037. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  11038. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  11039. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  11040. +
  11041. +typedef enum {
  11042. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  11043. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  11044. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  11045. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  11046. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  11047. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  11048. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  11049. +} VCHIQ_REASON_T;
  11050. +
  11051. +typedef enum {
  11052. + VCHIQ_ERROR = -1,
  11053. + VCHIQ_SUCCESS = 0,
  11054. + VCHIQ_RETRY = 1
  11055. +} VCHIQ_STATUS_T;
  11056. +
  11057. +typedef enum {
  11058. + VCHIQ_BULK_MODE_CALLBACK,
  11059. + VCHIQ_BULK_MODE_BLOCKING,
  11060. + VCHIQ_BULK_MODE_NOCALLBACK,
  11061. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  11062. +} VCHIQ_BULK_MODE_T;
  11063. +
  11064. +typedef enum {
  11065. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  11066. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  11067. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  11068. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS,
  11069. + VCHIQ_SERVICE_OPTION_TRACE
  11070. +} VCHIQ_SERVICE_OPTION_T;
  11071. +
  11072. +typedef struct vchiq_header_struct {
  11073. + /* The message identifier - opaque to applications. */
  11074. + int msgid;
  11075. +
  11076. + /* Size of message data. */
  11077. + unsigned int size;
  11078. +
  11079. + char data[0]; /* message */
  11080. +} VCHIQ_HEADER_T;
  11081. +
  11082. +typedef struct {
  11083. + const void *data;
  11084. + unsigned int size;
  11085. +} VCHIQ_ELEMENT_T;
  11086. +
  11087. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  11088. +
  11089. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  11090. + VCHIQ_SERVICE_HANDLE_T, void *);
  11091. +
  11092. +typedef struct vchiq_service_base_struct {
  11093. + int fourcc;
  11094. + VCHIQ_CALLBACK_T callback;
  11095. + void *userdata;
  11096. +} VCHIQ_SERVICE_BASE_T;
  11097. +
  11098. +typedef struct vchiq_service_params_struct {
  11099. + int fourcc;
  11100. + VCHIQ_CALLBACK_T callback;
  11101. + void *userdata;
  11102. + short version; /* Increment for non-trivial changes */
  11103. + short version_min; /* Update for incompatible changes */
  11104. +} VCHIQ_SERVICE_PARAMS_T;
  11105. +
  11106. +typedef struct vchiq_config_struct {
  11107. + unsigned int max_msg_size;
  11108. + unsigned int bulk_threshold; /* The message size above which it
  11109. + is better to use a bulk transfer
  11110. + (<= max_msg_size) */
  11111. + unsigned int max_outstanding_bulks;
  11112. + unsigned int max_services;
  11113. + short version; /* The version of VCHIQ */
  11114. + short version_min; /* The minimum compatible version of VCHIQ */
  11115. +} VCHIQ_CONFIG_T;
  11116. +
  11117. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  11118. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  11119. +
  11120. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  11121. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  11122. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  11123. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  11124. + const VCHIQ_SERVICE_PARAMS_T *params,
  11125. + VCHIQ_SERVICE_HANDLE_T *pservice);
  11126. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  11127. + const VCHIQ_SERVICE_PARAMS_T *params,
  11128. + VCHIQ_SERVICE_HANDLE_T *pservice);
  11129. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  11130. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  11131. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  11132. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  11133. + VCHIQ_SERVICE_HANDLE_T service);
  11134. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  11135. +
  11136. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  11137. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  11138. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  11139. + VCHIQ_HEADER_T *header);
  11140. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  11141. + const void *data, unsigned int size, void *userdata);
  11142. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  11143. + void *data, unsigned int size, void *userdata);
  11144. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  11145. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  11146. + const void *offset, unsigned int size, void *userdata);
  11147. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  11148. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  11149. + void *offset, unsigned int size, void *userdata);
  11150. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  11151. + const void *data, unsigned int size, void *userdata,
  11152. + VCHIQ_BULK_MODE_T mode);
  11153. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  11154. + void *data, unsigned int size, void *userdata,
  11155. + VCHIQ_BULK_MODE_T mode);
  11156. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  11157. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  11158. + void *userdata, VCHIQ_BULK_MODE_T mode);
  11159. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  11160. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  11161. + void *userdata, VCHIQ_BULK_MODE_T mode);
  11162. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  11163. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  11164. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  11165. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  11166. + int config_size, VCHIQ_CONFIG_T *pconfig);
  11167. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  11168. + VCHIQ_SERVICE_OPTION_T option, int value);
  11169. +
  11170. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  11171. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  11172. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  11173. +
  11174. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  11175. + void *ptr, size_t num_bytes);
  11176. +
  11177. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  11178. + short *peer_version);
  11179. +
  11180. +#endif /* VCHIQ_IF_H */
  11181. --- /dev/null
  11182. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  11183. @@ -0,0 +1,131 @@
  11184. +/**
  11185. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11186. + *
  11187. + * Redistribution and use in source and binary forms, with or without
  11188. + * modification, are permitted provided that the following conditions
  11189. + * are met:
  11190. + * 1. Redistributions of source code must retain the above copyright
  11191. + * notice, this list of conditions, and the following disclaimer,
  11192. + * without modification.
  11193. + * 2. Redistributions in binary form must reproduce the above copyright
  11194. + * notice, this list of conditions and the following disclaimer in the
  11195. + * documentation and/or other materials provided with the distribution.
  11196. + * 3. The names of the above-listed copyright holders may not be used
  11197. + * to endorse or promote products derived from this software without
  11198. + * specific prior written permission.
  11199. + *
  11200. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11201. + * GNU General Public License ("GPL") version 2, as published by the Free
  11202. + * Software Foundation.
  11203. + *
  11204. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11205. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11206. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11207. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11208. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11209. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11210. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11211. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11212. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11213. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11214. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11215. + */
  11216. +
  11217. +#ifndef VCHIQ_IOCTLS_H
  11218. +#define VCHIQ_IOCTLS_H
  11219. +
  11220. +#include <linux/ioctl.h>
  11221. +#include "vchiq_if.h"
  11222. +
  11223. +#define VCHIQ_IOC_MAGIC 0xc4
  11224. +#define VCHIQ_INVALID_HANDLE (~0)
  11225. +
  11226. +typedef struct {
  11227. + VCHIQ_SERVICE_PARAMS_T params;
  11228. + int is_open;
  11229. + int is_vchi;
  11230. + unsigned int handle; /* OUT */
  11231. +} VCHIQ_CREATE_SERVICE_T;
  11232. +
  11233. +typedef struct {
  11234. + unsigned int handle;
  11235. + unsigned int count;
  11236. + const VCHIQ_ELEMENT_T *elements;
  11237. +} VCHIQ_QUEUE_MESSAGE_T;
  11238. +
  11239. +typedef struct {
  11240. + unsigned int handle;
  11241. + void *data;
  11242. + unsigned int size;
  11243. + void *userdata;
  11244. + VCHIQ_BULK_MODE_T mode;
  11245. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  11246. +
  11247. +typedef struct {
  11248. + VCHIQ_REASON_T reason;
  11249. + VCHIQ_HEADER_T *header;
  11250. + void *service_userdata;
  11251. + void *bulk_userdata;
  11252. +} VCHIQ_COMPLETION_DATA_T;
  11253. +
  11254. +typedef struct {
  11255. + unsigned int count;
  11256. + VCHIQ_COMPLETION_DATA_T *buf;
  11257. + unsigned int msgbufsize;
  11258. + unsigned int msgbufcount; /* IN/OUT */
  11259. + void **msgbufs;
  11260. +} VCHIQ_AWAIT_COMPLETION_T;
  11261. +
  11262. +typedef struct {
  11263. + unsigned int handle;
  11264. + int blocking;
  11265. + unsigned int bufsize;
  11266. + void *buf;
  11267. +} VCHIQ_DEQUEUE_MESSAGE_T;
  11268. +
  11269. +typedef struct {
  11270. + unsigned int config_size;
  11271. + VCHIQ_CONFIG_T *pconfig;
  11272. +} VCHIQ_GET_CONFIG_T;
  11273. +
  11274. +typedef struct {
  11275. + unsigned int handle;
  11276. + VCHIQ_SERVICE_OPTION_T option;
  11277. + int value;
  11278. +} VCHIQ_SET_SERVICE_OPTION_T;
  11279. +
  11280. +typedef struct {
  11281. + void *virt_addr;
  11282. + size_t num_bytes;
  11283. +} VCHIQ_DUMP_MEM_T;
  11284. +
  11285. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  11286. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  11287. +#define VCHIQ_IOC_CREATE_SERVICE \
  11288. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  11289. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  11290. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  11291. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  11292. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  11293. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  11294. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  11295. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  11296. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  11297. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  11298. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  11299. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  11300. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  11301. +#define VCHIQ_IOC_GET_CONFIG \
  11302. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  11303. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  11304. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  11305. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  11306. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  11307. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  11308. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  11309. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  11310. +#define VCHIQ_IOC_LIB_VERSION _IO(VCHIQ_IOC_MAGIC, 16)
  11311. +#define VCHIQ_IOC_CLOSE_DELIVERED _IO(VCHIQ_IOC_MAGIC, 17)
  11312. +#define VCHIQ_IOC_MAX 17
  11313. +
  11314. +#endif
  11315. --- /dev/null
  11316. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  11317. @@ -0,0 +1,458 @@
  11318. +/**
  11319. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11320. + *
  11321. + * Redistribution and use in source and binary forms, with or without
  11322. + * modification, are permitted provided that the following conditions
  11323. + * are met:
  11324. + * 1. Redistributions of source code must retain the above copyright
  11325. + * notice, this list of conditions, and the following disclaimer,
  11326. + * without modification.
  11327. + * 2. Redistributions in binary form must reproduce the above copyright
  11328. + * notice, this list of conditions and the following disclaimer in the
  11329. + * documentation and/or other materials provided with the distribution.
  11330. + * 3. The names of the above-listed copyright holders may not be used
  11331. + * to endorse or promote products derived from this software without
  11332. + * specific prior written permission.
  11333. + *
  11334. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11335. + * GNU General Public License ("GPL") version 2, as published by the Free
  11336. + * Software Foundation.
  11337. + *
  11338. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11339. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11340. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11341. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11342. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11343. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11344. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11345. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11346. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11347. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11348. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11349. + */
  11350. +
  11351. +/* ---- Include Files ---------------------------------------------------- */
  11352. +
  11353. +#include <linux/kernel.h>
  11354. +#include <linux/module.h>
  11355. +#include <linux/mutex.h>
  11356. +
  11357. +#include "vchiq_core.h"
  11358. +#include "vchiq_arm.h"
  11359. +#include "vchiq_killable.h"
  11360. +
  11361. +/* ---- Public Variables ------------------------------------------------- */
  11362. +
  11363. +/* ---- Private Constants and Types -------------------------------------- */
  11364. +
  11365. +struct bulk_waiter_node {
  11366. + struct bulk_waiter bulk_waiter;
  11367. + int pid;
  11368. + struct list_head list;
  11369. +};
  11370. +
  11371. +struct vchiq_instance_struct {
  11372. + VCHIQ_STATE_T *state;
  11373. +
  11374. + int connected;
  11375. +
  11376. + struct list_head bulk_waiter_list;
  11377. + struct mutex bulk_waiter_list_mutex;
  11378. +};
  11379. +
  11380. +static VCHIQ_STATUS_T
  11381. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11382. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  11383. +
  11384. +/****************************************************************************
  11385. +*
  11386. +* vchiq_initialise
  11387. +*
  11388. +***************************************************************************/
  11389. +#define VCHIQ_INIT_RETRIES 10
  11390. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  11391. +{
  11392. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  11393. + VCHIQ_STATE_T *state;
  11394. + VCHIQ_INSTANCE_T instance = NULL;
  11395. + int i;
  11396. +
  11397. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  11398. +
  11399. + /* VideoCore may not be ready due to boot up timing.
  11400. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  11401. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  11402. + state = vchiq_get_state();
  11403. + if (state)
  11404. + break;
  11405. + udelay(500);
  11406. + }
  11407. + if (i==VCHIQ_INIT_RETRIES) {
  11408. + vchiq_log_error(vchiq_core_log_level,
  11409. + "%s: videocore not initialized\n", __func__);
  11410. + goto failed;
  11411. + } else if (i>0) {
  11412. + vchiq_log_warning(vchiq_core_log_level,
  11413. + "%s: videocore initialized after %d retries\n", __func__, i);
  11414. + }
  11415. +
  11416. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  11417. + if (!instance) {
  11418. + vchiq_log_error(vchiq_core_log_level,
  11419. + "%s: error allocating vchiq instance\n", __func__);
  11420. + goto failed;
  11421. + }
  11422. +
  11423. + instance->connected = 0;
  11424. + instance->state = state;
  11425. + mutex_init(&instance->bulk_waiter_list_mutex);
  11426. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  11427. +
  11428. + *instanceOut = instance;
  11429. +
  11430. + status = VCHIQ_SUCCESS;
  11431. +
  11432. +failed:
  11433. + vchiq_log_trace(vchiq_core_log_level,
  11434. + "%s(%p): returning %d", __func__, instance, status);
  11435. +
  11436. + return status;
  11437. +}
  11438. +EXPORT_SYMBOL(vchiq_initialise);
  11439. +
  11440. +/****************************************************************************
  11441. +*
  11442. +* vchiq_shutdown
  11443. +*
  11444. +***************************************************************************/
  11445. +
  11446. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  11447. +{
  11448. + VCHIQ_STATUS_T status;
  11449. + VCHIQ_STATE_T *state = instance->state;
  11450. +
  11451. + vchiq_log_trace(vchiq_core_log_level,
  11452. + "%s(%p) called", __func__, instance);
  11453. +
  11454. + if (mutex_lock_interruptible(&state->mutex) != 0)
  11455. + return VCHIQ_RETRY;
  11456. +
  11457. + /* Remove all services */
  11458. + status = vchiq_shutdown_internal(state, instance);
  11459. +
  11460. + mutex_unlock(&state->mutex);
  11461. +
  11462. + vchiq_log_trace(vchiq_core_log_level,
  11463. + "%s(%p): returning %d", __func__, instance, status);
  11464. +
  11465. + if (status == VCHIQ_SUCCESS) {
  11466. + struct list_head *pos, *next;
  11467. + list_for_each_safe(pos, next,
  11468. + &instance->bulk_waiter_list) {
  11469. + struct bulk_waiter_node *waiter;
  11470. + waiter = list_entry(pos,
  11471. + struct bulk_waiter_node,
  11472. + list);
  11473. + list_del(pos);
  11474. + vchiq_log_info(vchiq_arm_log_level,
  11475. + "bulk_waiter - cleaned up %x "
  11476. + "for pid %d",
  11477. + (unsigned int)waiter, waiter->pid);
  11478. + kfree(waiter);
  11479. + }
  11480. + kfree(instance);
  11481. + }
  11482. +
  11483. + return status;
  11484. +}
  11485. +EXPORT_SYMBOL(vchiq_shutdown);
  11486. +
  11487. +/****************************************************************************
  11488. +*
  11489. +* vchiq_is_connected
  11490. +*
  11491. +***************************************************************************/
  11492. +
  11493. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  11494. +{
  11495. + return instance->connected;
  11496. +}
  11497. +
  11498. +/****************************************************************************
  11499. +*
  11500. +* vchiq_connect
  11501. +*
  11502. +***************************************************************************/
  11503. +
  11504. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  11505. +{
  11506. + VCHIQ_STATUS_T status;
  11507. + VCHIQ_STATE_T *state = instance->state;
  11508. +
  11509. + vchiq_log_trace(vchiq_core_log_level,
  11510. + "%s(%p) called", __func__, instance);
  11511. +
  11512. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  11513. + vchiq_log_trace(vchiq_core_log_level,
  11514. + "%s: call to mutex_lock failed", __func__);
  11515. + status = VCHIQ_RETRY;
  11516. + goto failed;
  11517. + }
  11518. + status = vchiq_connect_internal(state, instance);
  11519. +
  11520. + if (status == VCHIQ_SUCCESS)
  11521. + instance->connected = 1;
  11522. +
  11523. + mutex_unlock(&state->mutex);
  11524. +
  11525. +failed:
  11526. + vchiq_log_trace(vchiq_core_log_level,
  11527. + "%s(%p): returning %d", __func__, instance, status);
  11528. +
  11529. + return status;
  11530. +}
  11531. +EXPORT_SYMBOL(vchiq_connect);
  11532. +
  11533. +/****************************************************************************
  11534. +*
  11535. +* vchiq_add_service
  11536. +*
  11537. +***************************************************************************/
  11538. +
  11539. +VCHIQ_STATUS_T vchiq_add_service(
  11540. + VCHIQ_INSTANCE_T instance,
  11541. + const VCHIQ_SERVICE_PARAMS_T *params,
  11542. + VCHIQ_SERVICE_HANDLE_T *phandle)
  11543. +{
  11544. + VCHIQ_STATUS_T status;
  11545. + VCHIQ_STATE_T *state = instance->state;
  11546. + VCHIQ_SERVICE_T *service = NULL;
  11547. + int srvstate;
  11548. +
  11549. + vchiq_log_trace(vchiq_core_log_level,
  11550. + "%s(%p) called", __func__, instance);
  11551. +
  11552. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  11553. +
  11554. + srvstate = vchiq_is_connected(instance)
  11555. + ? VCHIQ_SRVSTATE_LISTENING
  11556. + : VCHIQ_SRVSTATE_HIDDEN;
  11557. +
  11558. + service = vchiq_add_service_internal(
  11559. + state,
  11560. + params,
  11561. + srvstate,
  11562. + instance,
  11563. + NULL);
  11564. +
  11565. + if (service) {
  11566. + *phandle = service->handle;
  11567. + status = VCHIQ_SUCCESS;
  11568. + } else
  11569. + status = VCHIQ_ERROR;
  11570. +
  11571. + vchiq_log_trace(vchiq_core_log_level,
  11572. + "%s(%p): returning %d", __func__, instance, status);
  11573. +
  11574. + return status;
  11575. +}
  11576. +EXPORT_SYMBOL(vchiq_add_service);
  11577. +
  11578. +/****************************************************************************
  11579. +*
  11580. +* vchiq_open_service
  11581. +*
  11582. +***************************************************************************/
  11583. +
  11584. +VCHIQ_STATUS_T vchiq_open_service(
  11585. + VCHIQ_INSTANCE_T instance,
  11586. + const VCHIQ_SERVICE_PARAMS_T *params,
  11587. + VCHIQ_SERVICE_HANDLE_T *phandle)
  11588. +{
  11589. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  11590. + VCHIQ_STATE_T *state = instance->state;
  11591. + VCHIQ_SERVICE_T *service = NULL;
  11592. +
  11593. + vchiq_log_trace(vchiq_core_log_level,
  11594. + "%s(%p) called", __func__, instance);
  11595. +
  11596. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  11597. +
  11598. + if (!vchiq_is_connected(instance))
  11599. + goto failed;
  11600. +
  11601. + service = vchiq_add_service_internal(state,
  11602. + params,
  11603. + VCHIQ_SRVSTATE_OPENING,
  11604. + instance,
  11605. + NULL);
  11606. +
  11607. + if (service) {
  11608. + *phandle = service->handle;
  11609. + status = vchiq_open_service_internal(service, current->pid);
  11610. + if (status != VCHIQ_SUCCESS) {
  11611. + vchiq_remove_service(service->handle);
  11612. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  11613. + }
  11614. + }
  11615. +
  11616. +failed:
  11617. + vchiq_log_trace(vchiq_core_log_level,
  11618. + "%s(%p): returning %d", __func__, instance, status);
  11619. +
  11620. + return status;
  11621. +}
  11622. +EXPORT_SYMBOL(vchiq_open_service);
  11623. +
  11624. +VCHIQ_STATUS_T
  11625. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  11626. + const void *data, unsigned int size, void *userdata)
  11627. +{
  11628. + return vchiq_bulk_transfer(handle,
  11629. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  11630. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  11631. +}
  11632. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  11633. +
  11634. +VCHIQ_STATUS_T
  11635. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11636. + unsigned int size, void *userdata)
  11637. +{
  11638. + return vchiq_bulk_transfer(handle,
  11639. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  11640. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  11641. +}
  11642. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  11643. +
  11644. +VCHIQ_STATUS_T
  11645. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  11646. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  11647. +{
  11648. + VCHIQ_STATUS_T status;
  11649. +
  11650. + switch (mode) {
  11651. + case VCHIQ_BULK_MODE_NOCALLBACK:
  11652. + case VCHIQ_BULK_MODE_CALLBACK:
  11653. + status = vchiq_bulk_transfer(handle,
  11654. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  11655. + mode, VCHIQ_BULK_TRANSMIT);
  11656. + break;
  11657. + case VCHIQ_BULK_MODE_BLOCKING:
  11658. + status = vchiq_blocking_bulk_transfer(handle,
  11659. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  11660. + break;
  11661. + default:
  11662. + return VCHIQ_ERROR;
  11663. + }
  11664. +
  11665. + return status;
  11666. +}
  11667. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  11668. +
  11669. +VCHIQ_STATUS_T
  11670. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11671. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  11672. +{
  11673. + VCHIQ_STATUS_T status;
  11674. +
  11675. + switch (mode) {
  11676. + case VCHIQ_BULK_MODE_NOCALLBACK:
  11677. + case VCHIQ_BULK_MODE_CALLBACK:
  11678. + status = vchiq_bulk_transfer(handle,
  11679. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  11680. + mode, VCHIQ_BULK_RECEIVE);
  11681. + break;
  11682. + case VCHIQ_BULK_MODE_BLOCKING:
  11683. + status = vchiq_blocking_bulk_transfer(handle,
  11684. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  11685. + break;
  11686. + default:
  11687. + return VCHIQ_ERROR;
  11688. + }
  11689. +
  11690. + return status;
  11691. +}
  11692. +EXPORT_SYMBOL(vchiq_bulk_receive);
  11693. +
  11694. +static VCHIQ_STATUS_T
  11695. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11696. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  11697. +{
  11698. + VCHIQ_INSTANCE_T instance;
  11699. + VCHIQ_SERVICE_T *service;
  11700. + VCHIQ_STATUS_T status;
  11701. + struct bulk_waiter_node *waiter = NULL;
  11702. + struct list_head *pos;
  11703. +
  11704. + service = find_service_by_handle(handle);
  11705. + if (!service)
  11706. + return VCHIQ_ERROR;
  11707. +
  11708. + instance = service->instance;
  11709. +
  11710. + unlock_service(service);
  11711. +
  11712. + mutex_lock(&instance->bulk_waiter_list_mutex);
  11713. + list_for_each(pos, &instance->bulk_waiter_list) {
  11714. + if (list_entry(pos, struct bulk_waiter_node,
  11715. + list)->pid == current->pid) {
  11716. + waiter = list_entry(pos,
  11717. + struct bulk_waiter_node,
  11718. + list);
  11719. + list_del(pos);
  11720. + break;
  11721. + }
  11722. + }
  11723. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  11724. +
  11725. + if (waiter) {
  11726. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  11727. + if (bulk) {
  11728. + /* This thread has an outstanding bulk transfer. */
  11729. + if ((bulk->data != data) ||
  11730. + (bulk->size != size)) {
  11731. + /* This is not a retry of the previous one.
  11732. + ** Cancel the signal when the transfer
  11733. + ** completes. */
  11734. + spin_lock(&bulk_waiter_spinlock);
  11735. + bulk->userdata = NULL;
  11736. + spin_unlock(&bulk_waiter_spinlock);
  11737. + }
  11738. + }
  11739. + }
  11740. +
  11741. + if (!waiter) {
  11742. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  11743. + if (!waiter) {
  11744. + vchiq_log_error(vchiq_core_log_level,
  11745. + "%s - out of memory", __func__);
  11746. + return VCHIQ_ERROR;
  11747. + }
  11748. + }
  11749. +
  11750. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  11751. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  11752. + dir);
  11753. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  11754. + !waiter->bulk_waiter.bulk) {
  11755. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  11756. + if (bulk) {
  11757. + /* Cancel the signal when the transfer
  11758. + ** completes. */
  11759. + spin_lock(&bulk_waiter_spinlock);
  11760. + bulk->userdata = NULL;
  11761. + spin_unlock(&bulk_waiter_spinlock);
  11762. + }
  11763. + kfree(waiter);
  11764. + } else {
  11765. + waiter->pid = current->pid;
  11766. + mutex_lock(&instance->bulk_waiter_list_mutex);
  11767. + list_add(&waiter->list, &instance->bulk_waiter_list);
  11768. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  11769. + vchiq_log_info(vchiq_arm_log_level,
  11770. + "saved bulk_waiter %x for pid %d",
  11771. + (unsigned int)waiter, current->pid);
  11772. + }
  11773. +
  11774. + return status;
  11775. +}
  11776. --- /dev/null
  11777. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h
  11778. @@ -0,0 +1,69 @@
  11779. +/**
  11780. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11781. + *
  11782. + * Redistribution and use in source and binary forms, with or without
  11783. + * modification, are permitted provided that the following conditions
  11784. + * are met:
  11785. + * 1. Redistributions of source code must retain the above copyright
  11786. + * notice, this list of conditions, and the following disclaimer,
  11787. + * without modification.
  11788. + * 2. Redistributions in binary form must reproduce the above copyright
  11789. + * notice, this list of conditions and the following disclaimer in the
  11790. + * documentation and/or other materials provided with the distribution.
  11791. + * 3. The names of the above-listed copyright holders may not be used
  11792. + * to endorse or promote products derived from this software without
  11793. + * specific prior written permission.
  11794. + *
  11795. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11796. + * GNU General Public License ("GPL") version 2, as published by the Free
  11797. + * Software Foundation.
  11798. + *
  11799. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11800. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11801. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11802. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11803. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11804. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11805. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11806. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11807. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11808. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11809. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11810. + */
  11811. +
  11812. +#ifndef VCHIQ_KILLABLE_H
  11813. +#define VCHIQ_KILLABLE_H
  11814. +
  11815. +#include <linux/mutex.h>
  11816. +#include <linux/semaphore.h>
  11817. +
  11818. +#define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTRAP) | sigmask(SIGSTOP) | sigmask(SIGCONT))
  11819. +
  11820. +static inline int __must_check down_interruptible_killable(struct semaphore *sem)
  11821. +{
  11822. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  11823. + int ret;
  11824. + sigset_t blocked, oldset;
  11825. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  11826. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  11827. + ret = down_interruptible(sem);
  11828. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  11829. + return ret;
  11830. +}
  11831. +#define down_interruptible down_interruptible_killable
  11832. +
  11833. +
  11834. +static inline int __must_check mutex_lock_interruptible_killable(struct mutex *lock)
  11835. +{
  11836. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  11837. + int ret;
  11838. + sigset_t blocked, oldset;
  11839. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  11840. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  11841. + ret = mutex_lock_interruptible(lock);
  11842. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  11843. + return ret;
  11844. +}
  11845. +#define mutex_lock_interruptible mutex_lock_interruptible_killable
  11846. +
  11847. +#endif
  11848. --- /dev/null
  11849. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  11850. @@ -0,0 +1,71 @@
  11851. +/**
  11852. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11853. + *
  11854. + * Redistribution and use in source and binary forms, with or without
  11855. + * modification, are permitted provided that the following conditions
  11856. + * are met:
  11857. + * 1. Redistributions of source code must retain the above copyright
  11858. + * notice, this list of conditions, and the following disclaimer,
  11859. + * without modification.
  11860. + * 2. Redistributions in binary form must reproduce the above copyright
  11861. + * notice, this list of conditions and the following disclaimer in the
  11862. + * documentation and/or other materials provided with the distribution.
  11863. + * 3. The names of the above-listed copyright holders may not be used
  11864. + * to endorse or promote products derived from this software without
  11865. + * specific prior written permission.
  11866. + *
  11867. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11868. + * GNU General Public License ("GPL") version 2, as published by the Free
  11869. + * Software Foundation.
  11870. + *
  11871. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11872. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11873. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11874. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11875. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11876. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11877. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11878. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11879. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11880. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11881. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11882. + */
  11883. +
  11884. +#ifndef VCHIQ_MEMDRV_H
  11885. +#define VCHIQ_MEMDRV_H
  11886. +
  11887. +/* ---- Include Files ----------------------------------------------------- */
  11888. +
  11889. +#include <linux/kernel.h>
  11890. +#include "vchiq_if.h"
  11891. +
  11892. +/* ---- Constants and Types ---------------------------------------------- */
  11893. +
  11894. +typedef struct {
  11895. + void *armSharedMemVirt;
  11896. + dma_addr_t armSharedMemPhys;
  11897. + size_t armSharedMemSize;
  11898. +
  11899. + void *vcSharedMemVirt;
  11900. + dma_addr_t vcSharedMemPhys;
  11901. + size_t vcSharedMemSize;
  11902. +} VCHIQ_SHARED_MEM_INFO_T;
  11903. +
  11904. +/* ---- Variable Externs ------------------------------------------------- */
  11905. +
  11906. +/* ---- Function Prototypes ---------------------------------------------- */
  11907. +
  11908. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  11909. +
  11910. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  11911. +
  11912. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  11913. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11914. +
  11915. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  11916. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11917. +
  11918. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  11919. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11920. +
  11921. +#endif
  11922. --- /dev/null
  11923. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  11924. @@ -0,0 +1,58 @@
  11925. +/**
  11926. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11927. + *
  11928. + * Redistribution and use in source and binary forms, with or without
  11929. + * modification, are permitted provided that the following conditions
  11930. + * are met:
  11931. + * 1. Redistributions of source code must retain the above copyright
  11932. + * notice, this list of conditions, and the following disclaimer,
  11933. + * without modification.
  11934. + * 2. Redistributions in binary form must reproduce the above copyright
  11935. + * notice, this list of conditions and the following disclaimer in the
  11936. + * documentation and/or other materials provided with the distribution.
  11937. + * 3. The names of the above-listed copyright holders may not be used
  11938. + * to endorse or promote products derived from this software without
  11939. + * specific prior written permission.
  11940. + *
  11941. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11942. + * GNU General Public License ("GPL") version 2, as published by the Free
  11943. + * Software Foundation.
  11944. + *
  11945. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11946. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11947. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11948. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11949. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11950. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11951. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11952. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11953. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11954. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11955. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11956. + */
  11957. +
  11958. +#ifndef VCHIQ_PAGELIST_H
  11959. +#define VCHIQ_PAGELIST_H
  11960. +
  11961. +#ifndef PAGE_SIZE
  11962. +#define PAGE_SIZE 4096
  11963. +#endif
  11964. +#define CACHE_LINE_SIZE 32
  11965. +#define PAGELIST_WRITE 0
  11966. +#define PAGELIST_READ 1
  11967. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  11968. +
  11969. +typedef struct pagelist_struct {
  11970. + unsigned long length;
  11971. + unsigned short type;
  11972. + unsigned short offset;
  11973. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  11974. + pages at consecutive addresses. */
  11975. +} PAGELIST_T;
  11976. +
  11977. +typedef struct fragments_struct {
  11978. + char headbuf[CACHE_LINE_SIZE];
  11979. + char tailbuf[CACHE_LINE_SIZE];
  11980. +} FRAGMENTS_T;
  11981. +
  11982. +#endif /* VCHIQ_PAGELIST_H */
  11983. --- /dev/null
  11984. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  11985. @@ -0,0 +1,860 @@
  11986. +/**
  11987. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11988. + *
  11989. + * Redistribution and use in source and binary forms, with or without
  11990. + * modification, are permitted provided that the following conditions
  11991. + * are met:
  11992. + * 1. Redistributions of source code must retain the above copyright
  11993. + * notice, this list of conditions, and the following disclaimer,
  11994. + * without modification.
  11995. + * 2. Redistributions in binary form must reproduce the above copyright
  11996. + * notice, this list of conditions and the following disclaimer in the
  11997. + * documentation and/or other materials provided with the distribution.
  11998. + * 3. The names of the above-listed copyright holders may not be used
  11999. + * to endorse or promote products derived from this software without
  12000. + * specific prior written permission.
  12001. + *
  12002. + * ALTERNATIVELY, this software may be distributed under the terms of the
  12003. + * GNU General Public License ("GPL") version 2, as published by the Free
  12004. + * Software Foundation.
  12005. + *
  12006. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  12007. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  12008. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  12009. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  12010. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12011. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12012. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12013. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  12014. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  12015. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  12016. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12017. + */
  12018. +#include <linux/module.h>
  12019. +#include <linux/types.h>
  12020. +
  12021. +#include "interface/vchi/vchi.h"
  12022. +#include "vchiq.h"
  12023. +#include "vchiq_core.h"
  12024. +
  12025. +#include "vchiq_util.h"
  12026. +
  12027. +#include <stddef.h>
  12028. +
  12029. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  12030. +
  12031. +typedef struct {
  12032. + VCHIQ_SERVICE_HANDLE_T handle;
  12033. +
  12034. + VCHIU_QUEUE_T queue;
  12035. +
  12036. + VCHI_CALLBACK_T callback;
  12037. + void *callback_param;
  12038. +} SHIM_SERVICE_T;
  12039. +
  12040. +/* ----------------------------------------------------------------------
  12041. + * return pointer to the mphi message driver function table
  12042. + * -------------------------------------------------------------------- */
  12043. +const VCHI_MESSAGE_DRIVER_T *
  12044. +vchi_mphi_message_driver_func_table(void)
  12045. +{
  12046. + return NULL;
  12047. +}
  12048. +
  12049. +/* ----------------------------------------------------------------------
  12050. + * return a pointer to the 'single' connection driver fops
  12051. + * -------------------------------------------------------------------- */
  12052. +const VCHI_CONNECTION_API_T *
  12053. +single_get_func_table(void)
  12054. +{
  12055. + return NULL;
  12056. +}
  12057. +
  12058. +VCHI_CONNECTION_T *vchi_create_connection(
  12059. + const VCHI_CONNECTION_API_T *function_table,
  12060. + const VCHI_MESSAGE_DRIVER_T *low_level)
  12061. +{
  12062. + (void)function_table;
  12063. + (void)low_level;
  12064. + return NULL;
  12065. +}
  12066. +
  12067. +/***********************************************************
  12068. + * Name: vchi_msg_peek
  12069. + *
  12070. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  12071. + * void **data,
  12072. + * uint32_t *msg_size,
  12073. +
  12074. +
  12075. + * VCHI_FLAGS_T flags
  12076. + *
  12077. + * Description: Routine to return a pointer to the current message (to allow in
  12078. + * place processing). The message can be removed using
  12079. + * vchi_msg_remove when you're finished
  12080. + *
  12081. + * Returns: int32_t - success == 0
  12082. + *
  12083. + ***********************************************************/
  12084. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  12085. + void **data,
  12086. + uint32_t *msg_size,
  12087. + VCHI_FLAGS_T flags)
  12088. +{
  12089. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12090. + VCHIQ_HEADER_T *header;
  12091. +
  12092. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  12093. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  12094. +
  12095. + if (flags == VCHI_FLAGS_NONE)
  12096. + if (vchiu_queue_is_empty(&service->queue))
  12097. + return -1;
  12098. +
  12099. + header = vchiu_queue_peek(&service->queue);
  12100. +
  12101. + *data = header->data;
  12102. + *msg_size = header->size;
  12103. +
  12104. + return 0;
  12105. +}
  12106. +EXPORT_SYMBOL(vchi_msg_peek);
  12107. +
  12108. +/***********************************************************
  12109. + * Name: vchi_msg_remove
  12110. + *
  12111. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  12112. + *
  12113. + * Description: Routine to remove a message (after it has been read with
  12114. + * vchi_msg_peek)
  12115. + *
  12116. + * Returns: int32_t - success == 0
  12117. + *
  12118. + ***********************************************************/
  12119. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  12120. +{
  12121. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12122. + VCHIQ_HEADER_T *header;
  12123. +
  12124. + header = vchiu_queue_pop(&service->queue);
  12125. +
  12126. + vchiq_release_message(service->handle, header);
  12127. +
  12128. + return 0;
  12129. +}
  12130. +EXPORT_SYMBOL(vchi_msg_remove);
  12131. +
  12132. +/***********************************************************
  12133. + * Name: vchi_msg_queue
  12134. + *
  12135. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12136. + * const void *data,
  12137. + * uint32_t data_size,
  12138. + * VCHI_FLAGS_T flags,
  12139. + * void *msg_handle,
  12140. + *
  12141. + * Description: Thin wrapper to queue a message onto a connection
  12142. + *
  12143. + * Returns: int32_t - success == 0
  12144. + *
  12145. + ***********************************************************/
  12146. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  12147. + const void *data,
  12148. + uint32_t data_size,
  12149. + VCHI_FLAGS_T flags,
  12150. + void *msg_handle)
  12151. +{
  12152. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12153. + VCHIQ_ELEMENT_T element = {data, data_size};
  12154. + VCHIQ_STATUS_T status;
  12155. +
  12156. + (void)msg_handle;
  12157. +
  12158. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  12159. +
  12160. + status = vchiq_queue_message(service->handle, &element, 1);
  12161. +
  12162. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  12163. + ** implement a retry mechanism since this function is supposed
  12164. + ** to block until queued
  12165. + */
  12166. + while (status == VCHIQ_RETRY) {
  12167. + msleep(1);
  12168. + status = vchiq_queue_message(service->handle, &element, 1);
  12169. + }
  12170. +
  12171. + return vchiq_status_to_vchi(status);
  12172. +}
  12173. +EXPORT_SYMBOL(vchi_msg_queue);
  12174. +
  12175. +/***********************************************************
  12176. + * Name: vchi_bulk_queue_receive
  12177. + *
  12178. + * Arguments: VCHI_BULK_HANDLE_T handle,
  12179. + * void *data_dst,
  12180. + * const uint32_t data_size,
  12181. + * VCHI_FLAGS_T flags
  12182. + * void *bulk_handle
  12183. + *
  12184. + * Description: Routine to setup a rcv buffer
  12185. + *
  12186. + * Returns: int32_t - success == 0
  12187. + *
  12188. + ***********************************************************/
  12189. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  12190. + void *data_dst,
  12191. + uint32_t data_size,
  12192. + VCHI_FLAGS_T flags,
  12193. + void *bulk_handle)
  12194. +{
  12195. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12196. + VCHIQ_BULK_MODE_T mode;
  12197. + VCHIQ_STATUS_T status;
  12198. +
  12199. + switch ((int)flags) {
  12200. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  12201. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  12202. + WARN_ON(!service->callback);
  12203. + mode = VCHIQ_BULK_MODE_CALLBACK;
  12204. + break;
  12205. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  12206. + mode = VCHIQ_BULK_MODE_BLOCKING;
  12207. + break;
  12208. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  12209. + case VCHI_FLAGS_NONE:
  12210. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  12211. + break;
  12212. + default:
  12213. + WARN(1, "unsupported message\n");
  12214. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  12215. + }
  12216. +
  12217. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  12218. + bulk_handle, mode);
  12219. +
  12220. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  12221. + ** implement a retry mechanism since this function is supposed
  12222. + ** to block until queued
  12223. + */
  12224. + while (status == VCHIQ_RETRY) {
  12225. + msleep(1);
  12226. + status = vchiq_bulk_receive(service->handle, data_dst,
  12227. + data_size, bulk_handle, mode);
  12228. + }
  12229. +
  12230. + return vchiq_status_to_vchi(status);
  12231. +}
  12232. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  12233. +
  12234. +/***********************************************************
  12235. + * Name: vchi_bulk_queue_transmit
  12236. + *
  12237. + * Arguments: VCHI_BULK_HANDLE_T handle,
  12238. + * const void *data_src,
  12239. + * uint32_t data_size,
  12240. + * VCHI_FLAGS_T flags,
  12241. + * void *bulk_handle
  12242. + *
  12243. + * Description: Routine to transmit some data
  12244. + *
  12245. + * Returns: int32_t - success == 0
  12246. + *
  12247. + ***********************************************************/
  12248. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  12249. + const void *data_src,
  12250. + uint32_t data_size,
  12251. + VCHI_FLAGS_T flags,
  12252. + void *bulk_handle)
  12253. +{
  12254. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12255. + VCHIQ_BULK_MODE_T mode;
  12256. + VCHIQ_STATUS_T status;
  12257. +
  12258. + switch ((int)flags) {
  12259. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  12260. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  12261. + WARN_ON(!service->callback);
  12262. + mode = VCHIQ_BULK_MODE_CALLBACK;
  12263. + break;
  12264. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  12265. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  12266. + mode = VCHIQ_BULK_MODE_BLOCKING;
  12267. + break;
  12268. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  12269. + case VCHI_FLAGS_NONE:
  12270. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  12271. + break;
  12272. + default:
  12273. + WARN(1, "unsupported message\n");
  12274. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  12275. + }
  12276. +
  12277. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  12278. + bulk_handle, mode);
  12279. +
  12280. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  12281. + ** implement a retry mechanism since this function is supposed
  12282. + ** to block until queued
  12283. + */
  12284. + while (status == VCHIQ_RETRY) {
  12285. + msleep(1);
  12286. + status = vchiq_bulk_transmit(service->handle, data_src,
  12287. + data_size, bulk_handle, mode);
  12288. + }
  12289. +
  12290. + return vchiq_status_to_vchi(status);
  12291. +}
  12292. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  12293. +
  12294. +/***********************************************************
  12295. + * Name: vchi_msg_dequeue
  12296. + *
  12297. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12298. + * void *data,
  12299. + * uint32_t max_data_size_to_read,
  12300. + * uint32_t *actual_msg_size
  12301. + * VCHI_FLAGS_T flags
  12302. + *
  12303. + * Description: Routine to dequeue a message into the supplied buffer
  12304. + *
  12305. + * Returns: int32_t - success == 0
  12306. + *
  12307. + ***********************************************************/
  12308. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  12309. + void *data,
  12310. + uint32_t max_data_size_to_read,
  12311. + uint32_t *actual_msg_size,
  12312. + VCHI_FLAGS_T flags)
  12313. +{
  12314. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12315. + VCHIQ_HEADER_T *header;
  12316. +
  12317. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  12318. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  12319. +
  12320. + if (flags == VCHI_FLAGS_NONE)
  12321. + if (vchiu_queue_is_empty(&service->queue))
  12322. + return -1;
  12323. +
  12324. + header = vchiu_queue_pop(&service->queue);
  12325. +
  12326. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  12327. + header->size : max_data_size_to_read);
  12328. +
  12329. + *actual_msg_size = header->size;
  12330. +
  12331. + vchiq_release_message(service->handle, header);
  12332. +
  12333. + return 0;
  12334. +}
  12335. +EXPORT_SYMBOL(vchi_msg_dequeue);
  12336. +
  12337. +/***********************************************************
  12338. + * Name: vchi_msg_queuev
  12339. + *
  12340. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12341. + * VCHI_MSG_VECTOR_T *vector,
  12342. + * uint32_t count,
  12343. + * VCHI_FLAGS_T flags,
  12344. + * void *msg_handle
  12345. + *
  12346. + * Description: Thin wrapper to queue a message onto a connection
  12347. + *
  12348. + * Returns: int32_t - success == 0
  12349. + *
  12350. + ***********************************************************/
  12351. +
  12352. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  12353. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  12354. + offsetof(VCHIQ_ELEMENT_T, data));
  12355. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  12356. + offsetof(VCHIQ_ELEMENT_T, size));
  12357. +
  12358. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  12359. + VCHI_MSG_VECTOR_T *vector,
  12360. + uint32_t count,
  12361. + VCHI_FLAGS_T flags,
  12362. + void *msg_handle)
  12363. +{
  12364. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12365. +
  12366. + (void)msg_handle;
  12367. +
  12368. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  12369. +
  12370. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  12371. + (const VCHIQ_ELEMENT_T *)vector, count));
  12372. +}
  12373. +EXPORT_SYMBOL(vchi_msg_queuev);
  12374. +
  12375. +/***********************************************************
  12376. + * Name: vchi_held_msg_release
  12377. + *
  12378. + * Arguments: VCHI_HELD_MSG_T *message
  12379. + *
  12380. + * Description: Routine to release a held message (after it has been read with
  12381. + * vchi_msg_hold)
  12382. + *
  12383. + * Returns: int32_t - success == 0
  12384. + *
  12385. + ***********************************************************/
  12386. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  12387. +{
  12388. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  12389. + (VCHIQ_HEADER_T *)message->message);
  12390. +
  12391. + return 0;
  12392. +}
  12393. +EXPORT_SYMBOL(vchi_held_msg_release);
  12394. +
  12395. +/***********************************************************
  12396. + * Name: vchi_msg_hold
  12397. + *
  12398. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12399. + * void **data,
  12400. + * uint32_t *msg_size,
  12401. + * VCHI_FLAGS_T flags,
  12402. + * VCHI_HELD_MSG_T *message_handle
  12403. + *
  12404. + * Description: Routine to return a pointer to the current message (to allow
  12405. + * in place processing). The message is dequeued - don't forget
  12406. + * to release the message using vchi_held_msg_release when you're
  12407. + * finished.
  12408. + *
  12409. + * Returns: int32_t - success == 0
  12410. + *
  12411. + ***********************************************************/
  12412. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  12413. + void **data,
  12414. + uint32_t *msg_size,
  12415. + VCHI_FLAGS_T flags,
  12416. + VCHI_HELD_MSG_T *message_handle)
  12417. +{
  12418. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12419. + VCHIQ_HEADER_T *header;
  12420. +
  12421. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  12422. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  12423. +
  12424. + if (flags == VCHI_FLAGS_NONE)
  12425. + if (vchiu_queue_is_empty(&service->queue))
  12426. + return -1;
  12427. +
  12428. + header = vchiu_queue_pop(&service->queue);
  12429. +
  12430. + *data = header->data;
  12431. + *msg_size = header->size;
  12432. +
  12433. + message_handle->service =
  12434. + (struct opaque_vchi_service_t *)service->handle;
  12435. + message_handle->message = header;
  12436. +
  12437. + return 0;
  12438. +}
  12439. +EXPORT_SYMBOL(vchi_msg_hold);
  12440. +
  12441. +/***********************************************************
  12442. + * Name: vchi_initialise
  12443. + *
  12444. + * Arguments: VCHI_INSTANCE_T *instance_handle
  12445. + *
  12446. + * Description: Initialises the hardware but does not transmit anything
  12447. + * When run as a Host App this will be called twice hence the need
  12448. + * to malloc the state information
  12449. + *
  12450. + * Returns: 0 if successful, failure otherwise
  12451. + *
  12452. + ***********************************************************/
  12453. +
  12454. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  12455. +{
  12456. + VCHIQ_INSTANCE_T instance;
  12457. + VCHIQ_STATUS_T status;
  12458. +
  12459. + status = vchiq_initialise(&instance);
  12460. +
  12461. + *instance_handle = (VCHI_INSTANCE_T)instance;
  12462. +
  12463. + return vchiq_status_to_vchi(status);
  12464. +}
  12465. +EXPORT_SYMBOL(vchi_initialise);
  12466. +
  12467. +/***********************************************************
  12468. + * Name: vchi_connect
  12469. + *
  12470. + * Arguments: VCHI_CONNECTION_T **connections
  12471. + * const uint32_t num_connections
  12472. + * VCHI_INSTANCE_T instance_handle)
  12473. + *
  12474. + * Description: Starts the command service on each connection,
  12475. + * causing INIT messages to be pinged back and forth
  12476. + *
  12477. + * Returns: 0 if successful, failure otherwise
  12478. + *
  12479. + ***********************************************************/
  12480. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  12481. + const uint32_t num_connections,
  12482. + VCHI_INSTANCE_T instance_handle)
  12483. +{
  12484. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12485. +
  12486. + (void)connections;
  12487. + (void)num_connections;
  12488. +
  12489. + return vchiq_connect(instance);
  12490. +}
  12491. +EXPORT_SYMBOL(vchi_connect);
  12492. +
  12493. +
  12494. +/***********************************************************
  12495. + * Name: vchi_disconnect
  12496. + *
  12497. + * Arguments: VCHI_INSTANCE_T instance_handle
  12498. + *
  12499. + * Description: Stops the command service on each connection,
  12500. + * causing DE-INIT messages to be pinged back and forth
  12501. + *
  12502. + * Returns: 0 if successful, failure otherwise
  12503. + *
  12504. + ***********************************************************/
  12505. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  12506. +{
  12507. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12508. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  12509. +}
  12510. +EXPORT_SYMBOL(vchi_disconnect);
  12511. +
  12512. +
  12513. +/***********************************************************
  12514. + * Name: vchi_service_open
  12515. + * Name: vchi_service_create
  12516. + *
  12517. + * Arguments: VCHI_INSTANCE_T *instance_handle
  12518. + * SERVICE_CREATION_T *setup,
  12519. + * VCHI_SERVICE_HANDLE_T *handle
  12520. + *
  12521. + * Description: Routine to open a service
  12522. + *
  12523. + * Returns: int32_t - success == 0
  12524. + *
  12525. + ***********************************************************/
  12526. +
  12527. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  12528. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  12529. +{
  12530. + SHIM_SERVICE_T *service =
  12531. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  12532. +
  12533. + if (!service->callback)
  12534. + goto release;
  12535. +
  12536. + switch (reason) {
  12537. + case VCHIQ_MESSAGE_AVAILABLE:
  12538. + vchiu_queue_push(&service->queue, header);
  12539. +
  12540. + service->callback(service->callback_param,
  12541. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  12542. +
  12543. + goto done;
  12544. + break;
  12545. +
  12546. + case VCHIQ_BULK_TRANSMIT_DONE:
  12547. + service->callback(service->callback_param,
  12548. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  12549. + break;
  12550. +
  12551. + case VCHIQ_BULK_RECEIVE_DONE:
  12552. + service->callback(service->callback_param,
  12553. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  12554. + break;
  12555. +
  12556. + case VCHIQ_SERVICE_CLOSED:
  12557. + service->callback(service->callback_param,
  12558. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  12559. + break;
  12560. +
  12561. + case VCHIQ_SERVICE_OPENED:
  12562. + /* No equivalent VCHI reason */
  12563. + break;
  12564. +
  12565. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  12566. + service->callback(service->callback_param,
  12567. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  12568. + bulk_user);
  12569. + break;
  12570. +
  12571. + case VCHIQ_BULK_RECEIVE_ABORTED:
  12572. + service->callback(service->callback_param,
  12573. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  12574. + bulk_user);
  12575. + break;
  12576. +
  12577. + default:
  12578. + WARN(1, "not supported\n");
  12579. + break;
  12580. + }
  12581. +
  12582. +release:
  12583. + vchiq_release_message(service->handle, header);
  12584. +done:
  12585. + return VCHIQ_SUCCESS;
  12586. +}
  12587. +
  12588. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  12589. + SERVICE_CREATION_T *setup)
  12590. +{
  12591. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  12592. +
  12593. + (void)instance;
  12594. +
  12595. + if (service) {
  12596. + if (vchiu_queue_init(&service->queue, 64)) {
  12597. + service->callback = setup->callback;
  12598. + service->callback_param = setup->callback_param;
  12599. + } else {
  12600. + kfree(service);
  12601. + service = NULL;
  12602. + }
  12603. + }
  12604. +
  12605. + return service;
  12606. +}
  12607. +
  12608. +static void service_free(SHIM_SERVICE_T *service)
  12609. +{
  12610. + if (service) {
  12611. + vchiu_queue_delete(&service->queue);
  12612. + kfree(service);
  12613. + }
  12614. +}
  12615. +
  12616. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  12617. + SERVICE_CREATION_T *setup,
  12618. + VCHI_SERVICE_HANDLE_T *handle)
  12619. +{
  12620. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12621. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  12622. +
  12623. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  12624. +
  12625. + if (service) {
  12626. + VCHIQ_SERVICE_PARAMS_T params;
  12627. + VCHIQ_STATUS_T status;
  12628. +
  12629. + memset(&params, 0, sizeof(params));
  12630. + params.fourcc = setup->service_id;
  12631. + params.callback = shim_callback;
  12632. + params.userdata = service;
  12633. + params.version = setup->version.version;
  12634. + params.version_min = setup->version.version_min;
  12635. +
  12636. + status = vchiq_open_service(instance, &params,
  12637. + &service->handle);
  12638. + if (status != VCHIQ_SUCCESS) {
  12639. + service_free(service);
  12640. + service = NULL;
  12641. + *handle = NULL;
  12642. + }
  12643. + }
  12644. +
  12645. + return (service != NULL) ? 0 : -1;
  12646. +}
  12647. +EXPORT_SYMBOL(vchi_service_open);
  12648. +
  12649. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  12650. + SERVICE_CREATION_T *setup,
  12651. + VCHI_SERVICE_HANDLE_T *handle)
  12652. +{
  12653. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12654. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  12655. +
  12656. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  12657. +
  12658. + if (service) {
  12659. + VCHIQ_SERVICE_PARAMS_T params;
  12660. + VCHIQ_STATUS_T status;
  12661. +
  12662. + memset(&params, 0, sizeof(params));
  12663. + params.fourcc = setup->service_id;
  12664. + params.callback = shim_callback;
  12665. + params.userdata = service;
  12666. + params.version = setup->version.version;
  12667. + params.version_min = setup->version.version_min;
  12668. + status = vchiq_add_service(instance, &params, &service->handle);
  12669. +
  12670. + if (status != VCHIQ_SUCCESS) {
  12671. + service_free(service);
  12672. + service = NULL;
  12673. + *handle = NULL;
  12674. + }
  12675. + }
  12676. +
  12677. + return (service != NULL) ? 0 : -1;
  12678. +}
  12679. +EXPORT_SYMBOL(vchi_service_create);
  12680. +
  12681. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  12682. +{
  12683. + int32_t ret = -1;
  12684. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12685. + if (service) {
  12686. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  12687. + if (status == VCHIQ_SUCCESS) {
  12688. + service_free(service);
  12689. + service = NULL;
  12690. + }
  12691. +
  12692. + ret = vchiq_status_to_vchi(status);
  12693. + }
  12694. + return ret;
  12695. +}
  12696. +EXPORT_SYMBOL(vchi_service_close);
  12697. +
  12698. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  12699. +{
  12700. + int32_t ret = -1;
  12701. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12702. + if (service) {
  12703. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  12704. + if (status == VCHIQ_SUCCESS) {
  12705. + service_free(service);
  12706. + service = NULL;
  12707. + }
  12708. +
  12709. + ret = vchiq_status_to_vchi(status);
  12710. + }
  12711. + return ret;
  12712. +}
  12713. +EXPORT_SYMBOL(vchi_service_destroy);
  12714. +
  12715. +int32_t vchi_service_set_option(const VCHI_SERVICE_HANDLE_T handle,
  12716. + VCHI_SERVICE_OPTION_T option,
  12717. + int value)
  12718. +{
  12719. + int32_t ret = -1;
  12720. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12721. + VCHIQ_SERVICE_OPTION_T vchiq_option;
  12722. + switch (option) {
  12723. + case VCHI_SERVICE_OPTION_TRACE:
  12724. + vchiq_option = VCHIQ_SERVICE_OPTION_TRACE;
  12725. + break;
  12726. + case VCHI_SERVICE_OPTION_SYNCHRONOUS:
  12727. + vchiq_option = VCHIQ_SERVICE_OPTION_SYNCHRONOUS;
  12728. + break;
  12729. + default:
  12730. + service = NULL;
  12731. + break;
  12732. + }
  12733. + if (service) {
  12734. + VCHIQ_STATUS_T status =
  12735. + vchiq_set_service_option(service->handle,
  12736. + vchiq_option,
  12737. + value);
  12738. +
  12739. + ret = vchiq_status_to_vchi(status);
  12740. + }
  12741. + return ret;
  12742. +}
  12743. +EXPORT_SYMBOL(vchi_service_set_option);
  12744. +
  12745. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  12746. +{
  12747. + int32_t ret = -1;
  12748. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12749. + if(service)
  12750. + {
  12751. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  12752. + ret = vchiq_status_to_vchi( status );
  12753. + }
  12754. + return ret;
  12755. +}
  12756. +EXPORT_SYMBOL(vchi_get_peer_version);
  12757. +
  12758. +/* ----------------------------------------------------------------------
  12759. + * read a uint32_t from buffer.
  12760. + * network format is defined to be little endian
  12761. + * -------------------------------------------------------------------- */
  12762. +uint32_t
  12763. +vchi_readbuf_uint32(const void *_ptr)
  12764. +{
  12765. + const unsigned char *ptr = _ptr;
  12766. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  12767. +}
  12768. +
  12769. +/* ----------------------------------------------------------------------
  12770. + * write a uint32_t to buffer.
  12771. + * network format is defined to be little endian
  12772. + * -------------------------------------------------------------------- */
  12773. +void
  12774. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  12775. +{
  12776. + unsigned char *ptr = _ptr;
  12777. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  12778. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  12779. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  12780. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  12781. +}
  12782. +
  12783. +/* ----------------------------------------------------------------------
  12784. + * read a uint16_t from buffer.
  12785. + * network format is defined to be little endian
  12786. + * -------------------------------------------------------------------- */
  12787. +uint16_t
  12788. +vchi_readbuf_uint16(const void *_ptr)
  12789. +{
  12790. + const unsigned char *ptr = _ptr;
  12791. + return ptr[0] | (ptr[1] << 8);
  12792. +}
  12793. +
  12794. +/* ----------------------------------------------------------------------
  12795. + * write a uint16_t into the buffer.
  12796. + * network format is defined to be little endian
  12797. + * -------------------------------------------------------------------- */
  12798. +void
  12799. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  12800. +{
  12801. + unsigned char *ptr = _ptr;
  12802. + ptr[0] = (value >> 0) & 0xFF;
  12803. + ptr[1] = (value >> 8) & 0xFF;
  12804. +}
  12805. +
  12806. +/***********************************************************
  12807. + * Name: vchi_service_use
  12808. + *
  12809. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  12810. + *
  12811. + * Description: Routine to increment refcount on a service
  12812. + *
  12813. + * Returns: void
  12814. + *
  12815. + ***********************************************************/
  12816. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  12817. +{
  12818. + int32_t ret = -1;
  12819. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12820. + if (service)
  12821. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  12822. + return ret;
  12823. +}
  12824. +EXPORT_SYMBOL(vchi_service_use);
  12825. +
  12826. +/***********************************************************
  12827. + * Name: vchi_service_release
  12828. + *
  12829. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  12830. + *
  12831. + * Description: Routine to decrement refcount on a service
  12832. + *
  12833. + * Returns: void
  12834. + *
  12835. + ***********************************************************/
  12836. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  12837. +{
  12838. + int32_t ret = -1;
  12839. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12840. + if (service)
  12841. + ret = vchiq_status_to_vchi(
  12842. + vchiq_release_service(service->handle));
  12843. + return ret;
  12844. +}
  12845. +EXPORT_SYMBOL(vchi_service_release);
  12846. --- /dev/null
  12847. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  12848. @@ -0,0 +1,156 @@
  12849. +/**
  12850. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  12851. + *
  12852. + * Redistribution and use in source and binary forms, with or without
  12853. + * modification, are permitted provided that the following conditions
  12854. + * are met:
  12855. + * 1. Redistributions of source code must retain the above copyright
  12856. + * notice, this list of conditions, and the following disclaimer,
  12857. + * without modification.
  12858. + * 2. Redistributions in binary form must reproduce the above copyright
  12859. + * notice, this list of conditions and the following disclaimer in the
  12860. + * documentation and/or other materials provided with the distribution.
  12861. + * 3. The names of the above-listed copyright holders may not be used
  12862. + * to endorse or promote products derived from this software without
  12863. + * specific prior written permission.
  12864. + *
  12865. + * ALTERNATIVELY, this software may be distributed under the terms of the
  12866. + * GNU General Public License ("GPL") version 2, as published by the Free
  12867. + * Software Foundation.
  12868. + *
  12869. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  12870. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  12871. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  12872. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  12873. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12874. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12875. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12876. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  12877. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  12878. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  12879. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12880. + */
  12881. +
  12882. +#include "vchiq_util.h"
  12883. +#include "vchiq_killable.h"
  12884. +
  12885. +static inline int is_pow2(int i)
  12886. +{
  12887. + return i && !(i & (i - 1));
  12888. +}
  12889. +
  12890. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  12891. +{
  12892. + WARN_ON(!is_pow2(size));
  12893. +
  12894. + queue->size = size;
  12895. + queue->read = 0;
  12896. + queue->write = 0;
  12897. + queue->initialized = 1;
  12898. +
  12899. + sema_init(&queue->pop, 0);
  12900. + sema_init(&queue->push, 0);
  12901. +
  12902. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  12903. + if (queue->storage == NULL) {
  12904. + vchiu_queue_delete(queue);
  12905. + return 0;
  12906. + }
  12907. + return 1;
  12908. +}
  12909. +
  12910. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  12911. +{
  12912. + if (queue->storage != NULL)
  12913. + kfree(queue->storage);
  12914. +}
  12915. +
  12916. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  12917. +{
  12918. + return queue->read == queue->write;
  12919. +}
  12920. +
  12921. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  12922. +{
  12923. + return queue->write == queue->read + queue->size;
  12924. +}
  12925. +
  12926. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  12927. +{
  12928. + if (!queue->initialized)
  12929. + return;
  12930. +
  12931. + while (queue->write == queue->read + queue->size) {
  12932. + if (down_interruptible(&queue->pop) != 0) {
  12933. + flush_signals(current);
  12934. + }
  12935. + }
  12936. +
  12937. + /*
  12938. + * Write to queue->storage must be visible after read from
  12939. + * queue->read
  12940. + */
  12941. + smp_mb();
  12942. +
  12943. + queue->storage[queue->write & (queue->size - 1)] = header;
  12944. +
  12945. + /*
  12946. + * Write to queue->storage must be visible before write to
  12947. + * queue->write
  12948. + */
  12949. + smp_wmb();
  12950. +
  12951. + queue->write++;
  12952. +
  12953. + up(&queue->push);
  12954. +}
  12955. +
  12956. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  12957. +{
  12958. + while (queue->write == queue->read) {
  12959. + if (down_interruptible(&queue->push) != 0) {
  12960. + flush_signals(current);
  12961. + }
  12962. + }
  12963. +
  12964. + up(&queue->push); // We haven't removed anything from the queue.
  12965. +
  12966. + /*
  12967. + * Read from queue->storage must be visible after read from
  12968. + * queue->write
  12969. + */
  12970. + smp_rmb();
  12971. +
  12972. + return queue->storage[queue->read & (queue->size - 1)];
  12973. +}
  12974. +
  12975. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  12976. +{
  12977. + VCHIQ_HEADER_T *header;
  12978. +
  12979. + while (queue->write == queue->read) {
  12980. + if (down_interruptible(&queue->push) != 0) {
  12981. + flush_signals(current);
  12982. + }
  12983. + }
  12984. +
  12985. + /*
  12986. + * Read from queue->storage must be visible after read from
  12987. + * queue->write
  12988. + */
  12989. + smp_rmb();
  12990. +
  12991. + header = queue->storage[queue->read & (queue->size - 1)];
  12992. +
  12993. + /*
  12994. + * Read from queue->storage must be visible before write to
  12995. + * queue->read
  12996. + */
  12997. + smp_mb();
  12998. +
  12999. + queue->read++;
  13000. +
  13001. + up(&queue->pop);
  13002. +
  13003. + return header;
  13004. +}
  13005. --- /dev/null
  13006. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  13007. @@ -0,0 +1,82 @@
  13008. +/**
  13009. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  13010. + *
  13011. + * Redistribution and use in source and binary forms, with or without
  13012. + * modification, are permitted provided that the following conditions
  13013. + * are met:
  13014. + * 1. Redistributions of source code must retain the above copyright
  13015. + * notice, this list of conditions, and the following disclaimer,
  13016. + * without modification.
  13017. + * 2. Redistributions in binary form must reproduce the above copyright
  13018. + * notice, this list of conditions and the following disclaimer in the
  13019. + * documentation and/or other materials provided with the distribution.
  13020. + * 3. The names of the above-listed copyright holders may not be used
  13021. + * to endorse or promote products derived from this software without
  13022. + * specific prior written permission.
  13023. + *
  13024. + * ALTERNATIVELY, this software may be distributed under the terms of the
  13025. + * GNU General Public License ("GPL") version 2, as published by the Free
  13026. + * Software Foundation.
  13027. + *
  13028. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  13029. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  13030. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  13031. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  13032. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13033. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  13034. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  13035. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  13036. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  13037. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  13038. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13039. + */
  13040. +
  13041. +#ifndef VCHIQ_UTIL_H
  13042. +#define VCHIQ_UTIL_H
  13043. +
  13044. +#include <linux/types.h>
  13045. +#include <linux/semaphore.h>
  13046. +#include <linux/mutex.h>
  13047. +#include <linux/bitops.h>
  13048. +#include <linux/kthread.h>
  13049. +#include <linux/wait.h>
  13050. +#include <linux/vmalloc.h>
  13051. +#include <linux/jiffies.h>
  13052. +#include <linux/delay.h>
  13053. +#include <linux/string.h>
  13054. +#include <linux/types.h>
  13055. +#include <linux/interrupt.h>
  13056. +#include <linux/random.h>
  13057. +#include <linux/sched.h>
  13058. +#include <linux/ctype.h>
  13059. +#include <linux/uaccess.h>
  13060. +#include <linux/time.h> /* for time_t */
  13061. +#include <linux/slab.h>
  13062. +#include <linux/vmalloc.h>
  13063. +
  13064. +#include "vchiq_if.h"
  13065. +
  13066. +typedef struct {
  13067. + int size;
  13068. + int read;
  13069. + int write;
  13070. + int initialized;
  13071. +
  13072. + struct semaphore pop;
  13073. + struct semaphore push;
  13074. +
  13075. + VCHIQ_HEADER_T **storage;
  13076. +} VCHIU_QUEUE_T;
  13077. +
  13078. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  13079. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  13080. +
  13081. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  13082. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  13083. +
  13084. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  13085. +
  13086. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  13087. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  13088. +
  13089. +#endif
  13090. --- /dev/null
  13091. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  13092. @@ -0,0 +1,59 @@
  13093. +/**
  13094. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  13095. + *
  13096. + * Redistribution and use in source and binary forms, with or without
  13097. + * modification, are permitted provided that the following conditions
  13098. + * are met:
  13099. + * 1. Redistributions of source code must retain the above copyright
  13100. + * notice, this list of conditions, and the following disclaimer,
  13101. + * without modification.
  13102. + * 2. Redistributions in binary form must reproduce the above copyright
  13103. + * notice, this list of conditions and the following disclaimer in the
  13104. + * documentation and/or other materials provided with the distribution.
  13105. + * 3. The names of the above-listed copyright holders may not be used
  13106. + * to endorse or promote products derived from this software without
  13107. + * specific prior written permission.
  13108. + *
  13109. + * ALTERNATIVELY, this software may be distributed under the terms of the
  13110. + * GNU General Public License ("GPL") version 2, as published by the Free
  13111. + * Software Foundation.
  13112. + *
  13113. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  13114. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  13115. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  13116. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  13117. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13118. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  13119. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  13120. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  13121. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  13122. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  13123. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13124. + */
  13125. +#include "vchiq_build_info.h"
  13126. +#include <linux/broadcom/vc_debug_sym.h>
  13127. +
  13128. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  13129. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  13130. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  13131. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  13132. +
  13133. +const char *vchiq_get_build_hostname( void )
  13134. +{
  13135. + return vchiq_build_hostname;
  13136. +}
  13137. +
  13138. +const char *vchiq_get_build_version( void )
  13139. +{
  13140. + return vchiq_build_version;
  13141. +}
  13142. +
  13143. +const char *vchiq_get_build_date( void )
  13144. +{
  13145. + return vchiq_build_date;
  13146. +}
  13147. +
  13148. +const char *vchiq_get_build_time( void )
  13149. +{
  13150. + return vchiq_build_time;
  13151. +}